Cleanup the replica plugin implementation.

This commit is contained in:
Damien Robichaud
2019-05-31 11:20:36 -07:00
parent 5000a2e503
commit d4842ebd90
6 changed files with 51 additions and 53 deletions

View File

@@ -1,13 +0,0 @@
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"`
}

View File

@@ -7,7 +7,6 @@ import (
"github.com/pkg/errors" "github.com/pkg/errors"
"sigs.k8s.io/kustomize/pkg/image" "sigs.k8s.io/kustomize/pkg/image"
"sigs.k8s.io/kustomize/pkg/plugins" "sigs.k8s.io/kustomize/pkg/plugins"
"sigs.k8s.io/kustomize/pkg/replica"
"sigs.k8s.io/kustomize/pkg/transformers" "sigs.k8s.io/kustomize/pkg/transformers"
"sigs.k8s.io/kustomize/pkg/transformers/config" "sigs.k8s.io/kustomize/pkg/transformers/config"
"sigs.k8s.io/kustomize/pkg/types" "sigs.k8s.io/kustomize/pkg/types"
@@ -80,6 +79,7 @@ func (kt *KustTarget) configureBuiltinTransformers(
} }
result = append(result, r...) result = append(result, r...)
} }
return result, nil return result, nil
} }
@@ -239,12 +239,10 @@ func (kt *KustTarget) configureBuiltinReplicaCountTransformer(
tConfig *config.TransformerConfig) ( tConfig *config.TransformerConfig) (
result []transformers.Transformer, err error) { result []transformers.Transformer, err error) {
var c struct { var c struct {
Replica replica.Replica Replica types.Replica
FieldSpecs []config.FieldSpec
} }
for _, args := range kt.kustomization.Replicas { for _, args := range kt.kustomization.Replicas {
c.Replica = args c.Replica = args
c.FieldSpecs = tConfig.Replicas
p := builtin.NewReplicaCountTransformerPlugin() p := builtin.NewReplicaCountTransformerPlugin()
err = kt.configureBuiltinPlugin(p, c, "replica") err = kt.configureBuiltinPlugin(p, c, "replica")
if err != nil { if err != nil {

View File

@@ -35,7 +35,6 @@ type TransformerConfig struct {
NameReference nbrSlice `json:"nameReference,omitempty" yaml:"nameReference,omitempty"` NameReference nbrSlice `json:"nameReference,omitempty" yaml:"nameReference,omitempty"`
VarReference fsSlice `json:"varReference,omitempty" yaml:"varReference,omitempty"` VarReference fsSlice `json:"varReference,omitempty" yaml:"varReference,omitempty"`
Images fsSlice `json:"images,omitempty" yaml:"images,omitempty"` Images fsSlice `json:"images,omitempty" yaml:"images,omitempty"`
Replicas fsSlice `json:"replicas,omitempty" yaml:"replicas,omitempty"`
} }
// MakeEmptyConfig returns an empty TransformerConfig object // MakeEmptyConfig returns an empty TransformerConfig object
@@ -62,7 +61,6 @@ func (t *TransformerConfig) sortFields() {
sort.Sort(t.NameReference) sort.Sort(t.NameReference)
sort.Sort(t.VarReference) sort.Sort(t.VarReference)
sort.Sort(t.Images) sort.Sort(t.Images)
sort.Sort(t.Replicas)
} }
// AddPrefixFieldSpec adds a FieldSpec to NamePrefix // AddPrefixFieldSpec adds a FieldSpec to NamePrefix

View File

@@ -22,7 +22,6 @@ import (
"sigs.k8s.io/kustomize/pkg/gvk" "sigs.k8s.io/kustomize/pkg/gvk"
"sigs.k8s.io/kustomize/pkg/image" "sigs.k8s.io/kustomize/pkg/image"
"sigs.k8s.io/kustomize/pkg/replica"
) )
const ( const (
@@ -82,7 +81,7 @@ type Kustomization struct {
// Replicas is a list of {resourcename, count} that allows for simpler replica // Replicas is a list of {resourcename, count} that allows for simpler replica
// specification. This can also be done with a patch. // specification. This can also be done with a patch.
Replicas []replica.Replica `json:"replicas,omitempty" yaml:"replicas,omitempty"` Replicas []Replica `json:"replicas,omitempty" yaml:"replicas,omitempty"`
// Vars allow things modified by kustomize to be injected into a // Vars allow things modified by kustomize to be injected into a
// container specification. A var is a name (e.g. FOO) associated // container specification. A var is a name (e.g. FOO) associated
@@ -360,3 +359,15 @@ type PatchTarget struct {
// stategic merge patch with the format // stategic merge patch with the format
// https://github.com/kubernetes/community/blob/master/contributors/devel/strategic-merge-patch.md // https://github.com/kubernetes/community/blob/master/contributors/devel/strategic-merge-patch.md
type PatchStrategicMerge string type PatchStrategicMerge string
// 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"`
}

View File

@@ -2,20 +2,24 @@
package builtin package builtin
import ( import (
"github.com/pkg/errors" "fmt"
"sigs.k8s.io/kustomize/pkg/ifc" "sigs.k8s.io/kustomize/pkg/ifc"
"sigs.k8s.io/kustomize/pkg/replica"
"sigs.k8s.io/kustomize/pkg/resid" "sigs.k8s.io/kustomize/pkg/resid"
"sigs.k8s.io/kustomize/pkg/resmap" "sigs.k8s.io/kustomize/pkg/resmap"
"sigs.k8s.io/kustomize/pkg/transformers/config" "sigs.k8s.io/kustomize/pkg/types"
"sigs.k8s.io/yaml" "sigs.k8s.io/yaml"
) )
// Find matching replicas declarations and replace const (
// the count. fldReplica = "replicas"
fldSpec = "spec"
)
// Find matching replicas declarations and replace the count.
// Eases the kustomization configuration of replica changes.
type ReplicaCountTransformerPlugin struct { type ReplicaCountTransformerPlugin struct {
Replica replica.Replica `json:"replica,omitempty" yaml:"replica,omitempty"` Replica types.Replica `json:"replica,omitempty" yaml:"replica,omitempty"`
FieldSpecs []config.FieldSpec `json:"fieldSpecs,omitempty" yaml:"fieldSpecs,omitempty"`
} }
func NewReplicaCountTransformerPlugin() *ReplicaCountTransformerPlugin { func NewReplicaCountTransformerPlugin() *ReplicaCountTransformerPlugin {
@@ -25,8 +29,7 @@ func NewReplicaCountTransformerPlugin() *ReplicaCountTransformerPlugin {
func (p *ReplicaCountTransformerPlugin) Config( func (p *ReplicaCountTransformerPlugin) Config(
ldr ifc.Loader, rf *resmap.Factory, c []byte) (err error) { ldr ifc.Loader, rf *resmap.Factory, c []byte) (err error) {
p.Replica = replica.Replica{} p.Replica = types.Replica{}
p.FieldSpecs = nil
return yaml.Unmarshal(c, p) return yaml.Unmarshal(c, p)
} }
@@ -36,21 +39,20 @@ func (p *ReplicaCountTransformerPlugin) Transform(m resmap.ResMap) error {
} }
for _, r := range m.GetMatchingIds(matcher) { for _, r := range m.GetMatchingIds(matcher) {
kMap := m[r].Kunstructured.Map() kMap := m[r].Map()
specInterface, ok := kMap["spec"] specInterface, ok := kMap[fldSpec]
if !ok { if !ok {
return errors.New("'spec' not specified, replicas cannot be modified") return fmt.Errorf("object %s missing field %s, cannot update %s",
p.Replica.Name, fldSpec, fldReplica)
} }
if spec, ok := specInterface.(map[string]interface{}); ok { if spec, ok := specInterface.(map[string]interface{}); ok {
spec["replicas"] = p.Replica.Count spec[fldReplica] = p.Replica.Count
kMap["spec"] = spec kMap[fldSpec] = spec
} else { } else {
return errors.New("'spec' not structured as expected") return fmt.Errorf("object %s has a malformed %s", p.Replica.Name, fldSpec)
} }
m[r].Kunstructured.SetMap(kMap)
} }
return nil return nil

View File

@@ -5,20 +5,24 @@
package main package main
import ( import (
"github.com/pkg/errors" "fmt"
"sigs.k8s.io/kustomize/pkg/ifc" "sigs.k8s.io/kustomize/pkg/ifc"
"sigs.k8s.io/kustomize/pkg/replica"
"sigs.k8s.io/kustomize/pkg/resid" "sigs.k8s.io/kustomize/pkg/resid"
"sigs.k8s.io/kustomize/pkg/resmap" "sigs.k8s.io/kustomize/pkg/resmap"
"sigs.k8s.io/kustomize/pkg/transformers/config" "sigs.k8s.io/kustomize/pkg/types"
"sigs.k8s.io/yaml" "sigs.k8s.io/yaml"
) )
const (
fldReplica = "replicas"
fldSpec = "spec"
)
// Find matching replicas declarations and replace the count. // Find matching replicas declarations and replace the count.
// Eases the kustomization configuration of replica changes. // Eases the kustomization configuration of replica changes.
type plugin struct { type plugin struct {
Replica replica.Replica `json:"replica,omitempty" yaml:"replica,omitempty"` Replica types.Replica `json:"replica,omitempty" yaml:"replica,omitempty"`
FieldSpecs []config.FieldSpec `json:"fieldSpecs,omitempty" yaml:"fieldSpecs,omitempty"`
} }
var KustomizePlugin plugin var KustomizePlugin plugin
@@ -26,8 +30,7 @@ var KustomizePlugin plugin
func (p *plugin) Config( func (p *plugin) Config(
ldr ifc.Loader, rf *resmap.Factory, c []byte) (err error) { ldr ifc.Loader, rf *resmap.Factory, c []byte) (err error) {
p.Replica = replica.Replica{} p.Replica = types.Replica{}
p.FieldSpecs = nil
return yaml.Unmarshal(c, p) return yaml.Unmarshal(c, p)
} }
@@ -37,21 +40,20 @@ func (p *plugin) Transform(m resmap.ResMap) error {
} }
for _, r := range m.GetMatchingIds(matcher) { for _, r := range m.GetMatchingIds(matcher) {
kMap := m[r].Kunstructured.Map() kMap := m[r].Map()
specInterface, ok := kMap["spec"] specInterface, ok := kMap[fldSpec]
if !ok { if !ok {
return errors.New("'spec' not specified, replicas cannot be modified") return fmt.Errorf("object %s missing field %s, cannot update %s",
p.Replica.Name, fldSpec, fldReplica)
} }
if spec, ok := specInterface.(map[string]interface{}); ok { if spec, ok := specInterface.(map[string]interface{}); ok {
spec["replicas"] = p.Replica.Count spec[fldReplica] = p.Replica.Count
kMap["spec"] = spec kMap[fldSpec] = spec
} else { } else {
return errors.New("'spec' not structured as expected") return fmt.Errorf("object %s has a malformed %s", p.Replica.Name, fldSpec)
} }
m[r].Kunstructured.SetMap(kMap)
} }
return nil return nil