mirror of
https://github.com/kubernetes-sigs/kustomize.git
synced 2026-05-17 18:25:26 +00:00
Implement replica transformer as patch alternative
This commit is contained in:
57
plugin/builtin/ReplicaCountTransformer.go
Normal file
57
plugin/builtin/ReplicaCountTransformer.go
Normal file
@@ -0,0 +1,57 @@
|
||||
// Code generated by pluginator on ReplicaCountTransformer; DO NOT EDIT.
|
||||
package builtin
|
||||
|
||||
import (
|
||||
"github.com/pkg/errors"
|
||||
"sigs.k8s.io/kustomize/pkg/ifc"
|
||||
"sigs.k8s.io/kustomize/pkg/replica"
|
||||
"sigs.k8s.io/kustomize/pkg/resid"
|
||||
"sigs.k8s.io/kustomize/pkg/resmap"
|
||||
"sigs.k8s.io/kustomize/pkg/transformers/config"
|
||||
"sigs.k8s.io/yaml"
|
||||
)
|
||||
|
||||
// Find matching replicas declarations and replace
|
||||
// the count.
|
||||
type ReplicaCountTransformerPlugin struct {
|
||||
Replica replica.Replica `json:"replica,omitempty" yaml:"replica,omitempty"`
|
||||
FieldSpecs []config.FieldSpec `json:"fieldSpecs,omitempty" yaml:"fieldSpecs,omitempty"`
|
||||
}
|
||||
|
||||
func NewReplicaCountTransformerPlugin() *ReplicaCountTransformerPlugin {
|
||||
return &ReplicaCountTransformerPlugin{}
|
||||
}
|
||||
|
||||
func (p *ReplicaCountTransformerPlugin) Config(
|
||||
ldr ifc.Loader, rf *resmap.Factory, c []byte) (err error) {
|
||||
|
||||
p.Replica = replica.Replica{}
|
||||
p.FieldSpecs = nil
|
||||
return yaml.Unmarshal(c, p)
|
||||
}
|
||||
|
||||
func (p *ReplicaCountTransformerPlugin) Transform(m resmap.ResMap) error {
|
||||
matcher := func(r resid.ResId) bool {
|
||||
return r.ItemId.Name == p.Replica.Name
|
||||
}
|
||||
|
||||
for _, r := range m.GetMatchingIds(matcher) {
|
||||
kMap := m[r].Kunstructured.Map()
|
||||
|
||||
specInterface, ok := kMap["spec"]
|
||||
if !ok {
|
||||
return errors.New("'spec' not specified, replicas cannot be modified")
|
||||
}
|
||||
|
||||
if spec, ok := specInterface.(map[string]interface{}); ok {
|
||||
spec["replicas"] = p.Replica.Count
|
||||
kMap["spec"] = spec
|
||||
} else {
|
||||
return errors.New("'spec' not structured as expected")
|
||||
}
|
||||
|
||||
m[r].Kunstructured.SetMap(kMap)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
// Copyright 2019 The Kubernetes Authors.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
//go:generate go run sigs.k8s.io/kustomize/plugin/pluginator
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/pkg/errors"
|
||||
"sigs.k8s.io/kustomize/pkg/ifc"
|
||||
"sigs.k8s.io/kustomize/pkg/replica"
|
||||
"sigs.k8s.io/kustomize/pkg/resid"
|
||||
"sigs.k8s.io/kustomize/pkg/resmap"
|
||||
"sigs.k8s.io/kustomize/pkg/transformers/config"
|
||||
"sigs.k8s.io/yaml"
|
||||
)
|
||||
|
||||
// Find matching replicas declarations and replace the count.
|
||||
// Eases the kustomization configuration of replica changes.
|
||||
type plugin struct {
|
||||
Replica replica.Replica `json:"replica,omitempty" yaml:"replica,omitempty"`
|
||||
FieldSpecs []config.FieldSpec `json:"fieldSpecs,omitempty" yaml:"fieldSpecs,omitempty"`
|
||||
}
|
||||
|
||||
var KustomizePlugin plugin
|
||||
|
||||
func (p *plugin) Config(
|
||||
ldr ifc.Loader, rf *resmap.Factory, c []byte) (err error) {
|
||||
|
||||
p.Replica = replica.Replica{}
|
||||
p.FieldSpecs = nil
|
||||
return yaml.Unmarshal(c, p)
|
||||
}
|
||||
|
||||
func (p *plugin) Transform(m resmap.ResMap) error {
|
||||
matcher := func(r resid.ResId) bool {
|
||||
return r.ItemId.Name == p.Replica.Name
|
||||
}
|
||||
|
||||
for _, r := range m.GetMatchingIds(matcher) {
|
||||
kMap := m[r].Kunstructured.Map()
|
||||
|
||||
specInterface, ok := kMap["spec"]
|
||||
if !ok {
|
||||
return errors.New("'spec' not specified, replicas cannot be modified")
|
||||
}
|
||||
|
||||
if spec, ok := specInterface.(map[string]interface{}); ok {
|
||||
spec["replicas"] = p.Replica.Count
|
||||
kMap["spec"] = spec
|
||||
} else {
|
||||
return errors.New("'spec' not structured as expected")
|
||||
}
|
||||
|
||||
m[r].Kunstructured.SetMap(kMap)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
// Copyright 2019 The Kubernetes Authors.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
package main_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"sigs.k8s.io/kustomize/pkg/kusttest"
|
||||
"sigs.k8s.io/kustomize/plugin"
|
||||
)
|
||||
|
||||
func TestReplicaCountTransformer(t *testing.T) {
|
||||
tc := plugin.NewEnvForTest(t).Set()
|
||||
defer tc.Reset()
|
||||
|
||||
tc.BuildGoPlugin(
|
||||
"builtin", "", "ReplicaCountTransformer")
|
||||
|
||||
th := kusttest_test.NewKustTestPluginHarness(t, "/app")
|
||||
|
||||
rm := th.LoadAndRunTransformer(`
|
||||
apiVersion: builtin
|
||||
kind: ReplicaCountTransformer
|
||||
metadata:
|
||||
name: notImportantHere
|
||||
replica:
|
||||
name: deploy1
|
||||
count: 23
|
||||
`, `
|
||||
group: apps
|
||||
apiVersion: v1
|
||||
kind: Deployment
|
||||
metadata:
|
||||
name: deploy1
|
||||
spec:
|
||||
replicas: 1
|
||||
`)
|
||||
|
||||
th.AssertActualEqualsExpected(rm, `
|
||||
apiVersion: v1
|
||||
group: apps
|
||||
kind: Deployment
|
||||
metadata:
|
||||
name: deploy1
|
||||
spec:
|
||||
replicas: 23
|
||||
`)
|
||||
}
|
||||
Reference in New Issue
Block a user