mirror of
https://github.com/kubernetes-sigs/kustomize.git
synced 2026-09-18 13:22:17 +00:00
Implement replica transformer as patch alternative
This commit is contained in:
13
pkg/replica/replica.go
Normal file
13
pkg/replica/replica.go
Normal file
@@ -0,0 +1,13 @@
|
||||
package replica
|
||||
|
||||
// Replica specifies a modification to a replica config.
|
||||
// The number of replicas of a resource whose name matches will be set to count.
|
||||
// This struct is used by the ReplicaCountTransform, and is meant to supplement
|
||||
// the existing patch functionality with a simpler syntax for replica configuration.
|
||||
type Replica struct {
|
||||
// The name of the resource to change the replica count
|
||||
Name string `json:"name,omitempty" yaml:"name,omitempty"`
|
||||
|
||||
// The number of replicas required.
|
||||
Count uint `json:"count,omitempty" yaml:"count,omitempty"`
|
||||
}
|
||||
@@ -7,6 +7,7 @@ import (
|
||||
"github.com/pkg/errors"
|
||||
"sigs.k8s.io/kustomize/pkg/image"
|
||||
"sigs.k8s.io/kustomize/pkg/plugins"
|
||||
"sigs.k8s.io/kustomize/pkg/replica"
|
||||
"sigs.k8s.io/kustomize/pkg/transformers"
|
||||
"sigs.k8s.io/kustomize/pkg/transformers/config"
|
||||
"sigs.k8s.io/kustomize/pkg/types"
|
||||
@@ -69,6 +70,7 @@ func (kt *KustTarget) configureBuiltinTransformers(
|
||||
kt.configureBuiltinLabelTransformer,
|
||||
kt.configureBuiltinAnnotationsTransformer,
|
||||
kt.configureBuiltinPatchJson6902Transformer,
|
||||
kt.configureBuiltinReplicaCountTransformer,
|
||||
}
|
||||
var result []transformers.Transformer
|
||||
for _, f := range configurators {
|
||||
@@ -233,6 +235,26 @@ func (kt *KustTarget) configureBuiltinImageTagTransformer(
|
||||
return
|
||||
}
|
||||
|
||||
func (kt *KustTarget) configureBuiltinReplicaCountTransformer(
|
||||
tConfig *config.TransformerConfig) (
|
||||
result []transformers.Transformer, err error) {
|
||||
var c struct {
|
||||
Replica replica.Replica
|
||||
FieldSpecs []config.FieldSpec
|
||||
}
|
||||
for _, args := range kt.kustomization.Replicas {
|
||||
c.Replica = args
|
||||
c.FieldSpecs = tConfig.Replicas
|
||||
p := builtin.NewReplicaCountTransformerPlugin()
|
||||
err = kt.configureBuiltinPlugin(p, c, "replica")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
result = append(result, p)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (kt *KustTarget) configureBuiltinPlugin(
|
||||
p plugins.Configurable, c interface{}, id string) (err error) {
|
||||
var y []byte
|
||||
|
||||
@@ -35,6 +35,7 @@ type TransformerConfig struct {
|
||||
NameReference nbrSlice `json:"nameReference,omitempty" yaml:"nameReference,omitempty"`
|
||||
VarReference fsSlice `json:"varReference,omitempty" yaml:"varReference,omitempty"`
|
||||
Images fsSlice `json:"images,omitempty" yaml:"images,omitempty"`
|
||||
Replicas fsSlice `json:"replicas,omitempty" yaml:"replicas,omitempty"`
|
||||
}
|
||||
|
||||
// MakeEmptyConfig returns an empty TransformerConfig object
|
||||
@@ -61,6 +62,7 @@ func (t *TransformerConfig) sortFields() {
|
||||
sort.Sort(t.NameReference)
|
||||
sort.Sort(t.VarReference)
|
||||
sort.Sort(t.Images)
|
||||
sort.Sort(t.Replicas)
|
||||
}
|
||||
|
||||
// AddPrefixFieldSpec adds a FieldSpec to NamePrefix
|
||||
|
||||
@@ -22,6 +22,7 @@ import (
|
||||
|
||||
"sigs.k8s.io/kustomize/pkg/gvk"
|
||||
"sigs.k8s.io/kustomize/pkg/image"
|
||||
"sigs.k8s.io/kustomize/pkg/replica"
|
||||
)
|
||||
|
||||
const (
|
||||
@@ -79,6 +80,10 @@ type Kustomization struct {
|
||||
// patch, but this operator is simpler to specify.
|
||||
Images []image.Image `json:"images,omitempty" yaml:"images,omitempty"`
|
||||
|
||||
// Replicas is a list of {resourcename, count} that allows for simpler replica
|
||||
// specification. This can also be done with a patch.
|
||||
Replicas []replica.Replica `json:"replicas,omitempty" yaml:"replicas,omitempty"`
|
||||
|
||||
// Vars allow things modified by kustomize to be injected into a
|
||||
// container specification. A var is a name (e.g. FOO) associated
|
||||
// with a field in a specific resource instance. The field must
|
||||
|
||||
Reference in New Issue
Block a user