Merge pull request #3795 from jlandowner/master

Support immutable attribute on generators
This commit is contained in:
Kubernetes Prow Robot
2021-04-15 22:36:22 -07:00
committed by GitHub
7 changed files with 37 additions and 0 deletions

View File

@@ -41,5 +41,6 @@ func MakeConfigMap(
return nil, err
}
copyLabelsAndAnnotations(rn, args.Options)
setImmutable(rn, args.Options)
return rn, nil
}

View File

@@ -163,6 +163,7 @@ data:
"river": "Missouri",
"city": "Iowa City",
},
Immutable: true,
},
},
},
@@ -183,6 +184,7 @@ data:
b: y
c: Hello World
d: "true"
immutable: true
`,
},
},

View File

@@ -54,5 +54,6 @@ func MakeSecret(
return nil, err
}
copyLabelsAndAnnotations(rn, args.Options)
setImmutable(rn, args.Options)
return rn, nil
}

View File

@@ -170,6 +170,7 @@ data:
"river": "Missouri",
"city": "Iowa City",
},
Immutable: true,
},
},
},
@@ -191,6 +192,7 @@ data:
b: eQ==
c: SGVsbG8gV29ybGQ=
d: dHJ1ZQ==
immutable: true
`,
},
},

View File

@@ -76,3 +76,16 @@ func copyLabelsAndAnnotations(
}
return nil
}
func setImmutable(
rn *yaml.RNode, opts *types.GeneratorOptions) error {
if opts == nil {
return nil
}
if opts.Immutable {
if _, err := rn.Pipe(yaml.SetField("immutable", yaml.NewScalarRNode("true"))); err != nil {
return err
}
}
return nil
}

View File

@@ -15,6 +15,9 @@ type GeneratorOptions struct {
// suffix to the names of generated resources that is a hash of the
// resource contents.
DisableNameSuffixHash bool `json:"disableNameSuffixHash,omitempty" yaml:"disableNameSuffixHash,omitempty"`
// Immutable if true add to all generated resources.
Immutable bool `json:"immutable,omitempty" yaml:"immutable,omitempty"`
}
// MergeGlobalOptionsIntoLocal merges two instances of GeneratorOptions.
@@ -42,6 +45,9 @@ func MergeGlobalOptionsIntoLocal(
if globalOpts.DisableNameSuffixHash {
localOpts.DisableNameSuffixHash = true
}
if globalOpts.Immutable {
localOpts.Immutable = true
}
return localOpts
}

View File

@@ -34,6 +34,7 @@ func TestMergeGlobalOptionsIntoLocal(t *testing.T) {
Labels: map[string]string{"pet": "dog"},
Annotations: map[string]string{"fruit": "apple"},
DisableNameSuffixHash: false,
Immutable: false,
},
},
{
@@ -47,6 +48,7 @@ func TestMergeGlobalOptionsIntoLocal(t *testing.T) {
Labels: map[string]string{"pet": "dog"},
Annotations: map[string]string{"fruit": "apple"},
DisableNameSuffixHash: false,
Immutable: false,
},
},
{
@@ -76,42 +78,52 @@ func TestMergeGlobalOptionsIntoLocal(t *testing.T) {
"tesla": "Y",
},
DisableNameSuffixHash: false,
Immutable: false,
},
},
{
name: "global disable trumps local",
local: &GeneratorOptions{
DisableNameSuffixHash: false,
Immutable: false,
},
global: &GeneratorOptions{
DisableNameSuffixHash: true,
Immutable: true,
},
expected: &GeneratorOptions{
DisableNameSuffixHash: true,
Immutable: true,
},
},
{
name: "local disable works",
local: &GeneratorOptions{
DisableNameSuffixHash: true,
Immutable: true,
},
global: &GeneratorOptions{
DisableNameSuffixHash: false,
Immutable: false,
},
expected: &GeneratorOptions{
DisableNameSuffixHash: true,
Immutable: true,
},
},
{
name: "everyone wants disable",
local: &GeneratorOptions{
DisableNameSuffixHash: true,
Immutable: true,
},
global: &GeneratorOptions{
DisableNameSuffixHash: true,
Immutable: true,
},
expected: &GeneratorOptions{
DisableNameSuffixHash: true,
Immutable: true,
},
},
}