| 1 |
// Code generated by pluginator on ReplicaCountTransformer; DO NOT EDIT. |
| 2 |
package builtins |
| 3 |
|
| 4 |
import ( |
| 5 |
"fmt" |
| 6 |
|
| 7 |
"sigs.k8s.io/kustomize/api/filters/replicacount" |
| 8 |
"sigs.k8s.io/kustomize/api/resmap" |
| 9 |
"sigs.k8s.io/kustomize/api/types" |
| 10 |
"sigs.k8s.io/kustomize/kyaml/resid" |
| 11 |
"sigs.k8s.io/yaml" |
| 12 |
) |
| 13 |
|
| 14 |
// Find matching replicas declarations and replace the count. |
| 15 |
// Eases the kustomization configuration of replica changes. |
| 16 |
type ReplicaCountTransformerPlugin struct { |
| 17 |
Replica types.Replica `json:"replica,omitempty" yaml:"replica,omitempty"` |
| 18 |
FieldSpecs []types.FieldSpec `json:"fieldSpecs,omitempty" yaml:"fieldSpecs,omitempty"` |
| 19 |
} |
| 20 |
|
| 21 |
func (p *ReplicaCountTransformerPlugin) Config( |
| 22 |
_ *resmap.PluginHelpers, c []byte) (err error) { |
| 23 |
p.Replica = types.Replica{} |
| 24 |
p.FieldSpecs = nil |
| 25 |
return yaml.Unmarshal(c, p) |
| 26 |
} |
| 27 |
|
| 28 |
func (p *ReplicaCountTransformerPlugin) Transform(m resmap.ResMap) error { |
| 29 |
found := false |
| 30 |
for _, fs := range p.FieldSpecs { |
| 31 |
matcher := p.createMatcher(fs) |
| 32 |
resList := m.GetMatchingResourcesByAnyId(matcher) |
| 33 |
if len(resList) > 0 { |
| 34 |
found = true |
| 35 |
for _, r := range resList { |
| 36 |
// There are redundant checks in the filter |
| 37 |
// that we'll live with until resolution of |
| 38 |
// https://github.com/kubernetes-sigs/kustomize/issues/2506 |
| 39 |
err := r.ApplyFilter(replicacount.Filter{ |
| 40 |
Replica: p.Replica, |
| 41 |
FieldSpec: fs, |
| 42 |
}) |
| 43 |
if err != nil { |
| 44 |
return err |
| 45 |
} |
| 46 |
} |
| 47 |
} |
| 48 |
} |
| 49 |
|
| 50 |
if !found { |
| 51 |
gvks := make([]string, len(p.FieldSpecs)) |
| 52 |
for i, replicaSpec := range p.FieldSpecs { |
| 53 |
gvks[i] = replicaSpec.Gvk.String() |
| 54 |
} |
| 55 |
return fmt.Errorf("resource with name %s does not match a config with the following GVK %v", |
| 56 |
p.Replica.Name, gvks) |
| 57 |
} |
| 58 |
|
| 59 |
return nil |
| 60 |
} |
| 61 |
|
| 62 |
// Match Replica.Name and FieldSpec |
| 63 |
func (p *ReplicaCountTransformerPlugin) createMatcher(fs types.FieldSpec) resmap.IdMatcher { |
| 64 |
return func(r resid.ResId) bool { |
| 65 |
return r.Name == p.Replica.Name && r.Gvk.IsSelected(&fs.Gvk) |
| 66 |
} |
| 67 |
} |
| 68 |
|
| 69 |
func NewReplicaCountTransformerPlugin() resmap.TransformerPlugin { |
| 70 |
return &ReplicaCountTransformerPlugin{} |
| 71 |
} |