mirror of
https://github.com/kubernetes-sigs/kustomize.git
synced 2026-06-11 17:12:51 +00:00
avoid error when the node has local-config
This commit is contained in:
@@ -107,7 +107,49 @@ func (p *HelmChartInflationGeneratorPlugin) EncodeValues(w io.Writer) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
//
|
// useValuesLocal process (merge) inflator config provided values with chart default values.yaml
|
||||||
|
func (p *HelmChartInflationGeneratorPlugin) useValuesLocal() error {
|
||||||
|
fn := path.Join(p.ChartHome, p.ChartName, "kustomize-values.yaml")
|
||||||
|
vf, err := os.Create(fn)
|
||||||
|
defer vf.Close()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
// override, merge, none
|
||||||
|
if p.ValuesMerge == "none" || p.ValuesMerge == "no" || p.ValuesMerge == "false" {
|
||||||
|
p.Values = fn
|
||||||
|
} else {
|
||||||
|
pValues, err := ioutil.ReadFile(p.Values)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
chValues := make(map[string]interface{})
|
||||||
|
err = yaml.Unmarshal(pValues, &chValues)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if p.ValuesMerge == "override" {
|
||||||
|
err = mergo.Merge(&chValues, p.ValuesLocal, mergo.WithOverride)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if p.ValuesMerge == "merge" {
|
||||||
|
err = mergo.Merge(&chValues, p.ValuesLocal)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
p.ValuesLocal = chValues
|
||||||
|
p.Values = fn
|
||||||
|
}
|
||||||
|
err = p.EncodeValues(vf)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
vf.Sync()
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
// Generate implements generator
|
// Generate implements generator
|
||||||
func (p *HelmChartInflationGeneratorPlugin) Generate() (resmap.ResMap, error) {
|
func (p *HelmChartInflationGeneratorPlugin) Generate() (resmap.ResMap, error) {
|
||||||
@@ -126,47 +168,12 @@ func (p *HelmChartInflationGeneratorPlugin) Generate() (resmap.ResMap, error) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// values
|
// inflator config valuesLocal
|
||||||
if len(p.ValuesLocal) > 0 {
|
if len(p.ValuesLocal) > 0 {
|
||||||
fn := path.Join(p.ChartHome, p.ChartName, "kustomize-values.yaml")
|
err := p.useValuesLocal()
|
||||||
vf, err := os.Create(fn)
|
|
||||||
defer vf.Close()
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
// override, merge, none
|
|
||||||
if p.ValuesMerge == "none" || p.ValuesMerge == "no" || p.ValuesMerge == "false" {
|
|
||||||
p.Values = fn
|
|
||||||
} else {
|
|
||||||
pValues, err := ioutil.ReadFile(p.Values)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
chValues := make(map[string]interface{})
|
|
||||||
err = yaml.Unmarshal(pValues, &chValues)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
if p.ValuesMerge == "override" {
|
|
||||||
err = mergo.Merge(&chValues, p.ValuesLocal, mergo.WithOverride)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if p.ValuesMerge == "merge" {
|
|
||||||
err = mergo.Merge(&chValues, p.ValuesLocal)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
p.ValuesLocal = chValues
|
|
||||||
p.Values = fn
|
|
||||||
}
|
|
||||||
err = p.EncodeValues(vf)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
vf.Sync()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// render the charts
|
// render the charts
|
||||||
|
|||||||
@@ -220,6 +220,7 @@ github.com/hashicorp/golang-lru v0.5.1/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ
|
|||||||
github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ=
|
github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ=
|
||||||
github.com/hpcloud/tail v1.0.0 h1:nfCOvKYfkgYP8hkirhJocXT2+zOD8yUNjXaWfTlyFKI=
|
github.com/hpcloud/tail v1.0.0 h1:nfCOvKYfkgYP8hkirhJocXT2+zOD8yUNjXaWfTlyFKI=
|
||||||
github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
|
github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
|
||||||
|
github.com/imdario/mergo v0.3.5 h1:JboBksRwiiAJWvIYJVo46AfV+IAIKZpfrSzVKj42R4Q=
|
||||||
github.com/imdario/mergo v0.3.5/go.mod h1:2EnlNZ0deacrJVfApfmtdGgDfMuh/nq6Ok1EcJh5FfA=
|
github.com/imdario/mergo v0.3.5/go.mod h1:2EnlNZ0deacrJVfApfmtdGgDfMuh/nq6Ok1EcJh5FfA=
|
||||||
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
|
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
|
||||||
github.com/jonboulle/clockwork v0.1.0/go.mod h1:Ii8DK3G1RaLaWxj9trq07+26W01tbo22gdxWY5EU2bo=
|
github.com/jonboulle/clockwork v0.1.0/go.mod h1:Ii8DK3G1RaLaWxj9trq07+26W01tbo22gdxWY5EU2bo=
|
||||||
|
|||||||
@@ -152,11 +152,11 @@ func (rmF *Factory) NewResMapFromRNodeSlice(rnodes []*yaml.RNode) (ResMap, error
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
r, err := rmF.resF.FromBytes([]byte(s))
|
r, err := rmF.resF.SliceFromBytes([]byte(s))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
resources = append(resources, r)
|
resources = append(resources, r...)
|
||||||
}
|
}
|
||||||
return newResMapFromResourceSlice(resources)
|
return newResMapFromResourceSlice(resources)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -277,7 +277,17 @@ func TestNewResMapFromSecretArgs(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestFromRNodeSlice(t *testing.T) {
|
func TestFromRNodeSlice(t *testing.T) {
|
||||||
input := `apiVersion: rbac.authorization.k8s.io/v1
|
type testcase struct {
|
||||||
|
input string
|
||||||
|
expected ResMap
|
||||||
|
}
|
||||||
|
testcases := map[string]testcase{
|
||||||
|
"no resource": {
|
||||||
|
input: "---",
|
||||||
|
expected: resmaptest_test.NewRmBuilder(t, rf).ResMap(),
|
||||||
|
},
|
||||||
|
"single resource": {
|
||||||
|
input: `apiVersion: rbac.authorization.k8s.io/v1
|
||||||
kind: ClusterRole
|
kind: ClusterRole
|
||||||
metadata:
|
metadata:
|
||||||
name: namespace-reader
|
name: namespace-reader
|
||||||
@@ -290,42 +300,54 @@ rules:
|
|||||||
- get
|
- get
|
||||||
- watch
|
- watch
|
||||||
- list
|
- list
|
||||||
`
|
`,
|
||||||
rnodes := []*yaml.RNode{
|
expected: resmaptest_test.NewRmBuilder(t, rf).Add(
|
||||||
yaml.MustParse(input),
|
|
||||||
}
|
|
||||||
|
|
||||||
rm, err := rmF.NewResMapFromRNodeSlice(rnodes)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("unexpected error: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
expected := resmaptest_test.NewRmBuilder(t, rf).Add(
|
|
||||||
map[string]interface{}{
|
|
||||||
"apiVersion": "rbac.authorization.k8s.io/v1",
|
|
||||||
"kind": "ClusterRole",
|
|
||||||
"metadata": map[string]interface{}{
|
|
||||||
"name": "namespace-reader",
|
|
||||||
},
|
|
||||||
"rules": []interface{}{
|
|
||||||
map[string]interface{}{
|
map[string]interface{}{
|
||||||
"apiGroups": []interface{}{
|
"apiVersion": "rbac.authorization.k8s.io/v1",
|
||||||
"",
|
"kind": "ClusterRole",
|
||||||
|
"metadata": map[string]interface{}{
|
||||||
|
"name": "namespace-reader",
|
||||||
},
|
},
|
||||||
"resources": []interface{}{
|
"rules": []interface{}{
|
||||||
"namespaces",
|
map[string]interface{}{
|
||||||
|
"apiGroups": []interface{}{
|
||||||
|
"",
|
||||||
|
},
|
||||||
|
"resources": []interface{}{
|
||||||
|
"namespaces",
|
||||||
|
},
|
||||||
|
"verbs": []interface{}{
|
||||||
|
"get",
|
||||||
|
"watch",
|
||||||
|
"list",
|
||||||
|
},
|
||||||
|
},
|
||||||
},
|
},
|
||||||
"verbs": []interface{}{
|
}).ResMap(),
|
||||||
"get",
|
},
|
||||||
"watch",
|
"local config": {
|
||||||
"list",
|
// local config should be ignored
|
||||||
},
|
input: `apiVersion: v1
|
||||||
},
|
kind: ConfigMap
|
||||||
},
|
metadata:
|
||||||
}).ResMap()
|
name: my-config
|
||||||
|
annotations:
|
||||||
if err = expected.ErrorIfNotEqualLists(rm); err != nil {
|
config.kubernetes.io/local-config: 'true'
|
||||||
t.Fatalf("error: %s", err)
|
`,
|
||||||
|
expected: resmaptest_test.NewRmBuilder(t, rf).ResMap(),
|
||||||
|
},
|
||||||
|
}
|
||||||
|
for name, tc := range testcases {
|
||||||
|
rnodes := []*yaml.RNode{
|
||||||
|
yaml.MustParse(tc.input),
|
||||||
|
}
|
||||||
|
rm, err := rmF.NewResMapFromRNodeSlice(rnodes)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("unexpected error in test case [%s]: %v", name, err)
|
||||||
|
}
|
||||||
|
if err = tc.expected.ErrorIfNotEqualLists(rm); err != nil {
|
||||||
|
t.Fatalf("error in test case [%s]: %s", name, err)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user