replace genargs with two separate annotations

This commit is contained in:
Natasha Sarkar
2021-07-29 15:46:09 -07:00
parent 3ebdb3fcef
commit 9a27a9f19f
8 changed files with 62 additions and 220 deletions

View File

@@ -64,19 +64,22 @@ func (rf *Factory) FromMapAndOption(
// TODO: return err instead of log.
log.Fatal(err)
}
return rf.makeOne(n, types.NewGenArgs(args))
return rf.makeOne(n, args)
}
// makeOne returns a new instance of Resource.
func (rf *Factory) makeOne(rn *yaml.RNode, o *types.GenArgs) *Resource {
func (rf *Factory) makeOne(rn *yaml.RNode, o *types.GeneratorArgs) *Resource {
if rn == nil {
log.Fatal("RNode must not be null")
}
if o == nil {
o = types.NewGenArgs(nil)
}
resource := &Resource{RNode: *rn}
resource.SetOptions(o)
if o != nil {
if o.Options == nil || !o.Options.DisableNameSuffixHash {
resource.EnableHashSuffix()
}
resource.SetBehavior(types.NewGenerationBehavior(o.Behavior))
}
return resource
}
@@ -267,7 +270,7 @@ func (rf *Factory) MakeConfigMap(kvLdr ifc.KvLoader, args *types.ConfigMapArgs)
if err != nil {
return nil, err
}
return rf.makeOne(rn, types.NewGenArgs(&args.GeneratorArgs)), nil
return rf.makeOne(rn, &args.GeneratorArgs), nil
}
// MakeSecret makes an instance of Resource for Secret
@@ -276,5 +279,5 @@ func (rf *Factory) MakeSecret(kvLdr ifc.KvLoader, args *types.SecretArgs) (*Reso
if err != nil {
return nil, err
}
return rf.makeOne(rn, types.NewGenArgs(&args.GeneratorArgs)), nil
return rf.makeOne(rn, &args.GeneratorArgs), nil
}

View File

@@ -35,7 +35,8 @@ var BuildAnnotations = []string{
utils.BuildAnnotationAllowNameChange,
utils.BuildAnnotationAllowKindChange,
utils.BuildAnnotationsRefBy,
utils.BuildAnnotationsGenOptions,
utils.BuildAnnotationsGenBehavior,
utils.BuildAnnotationsGenAddHashSuffix,
kioutil.PathAnnotation,
kioutil.IndexAnnotation,
@@ -246,34 +247,38 @@ func (r *Resource) setPreviousId(ns string, n string, k string) *Resource {
// AllowNameChange allows name changes to the resource.
func (r *Resource) AllowNameChange() {
annotations := r.GetAnnotations()
annotations[utils.BuildAnnotationAllowNameChange] = utils.Allowed
if err := r.SetAnnotations(annotations); err != nil {
panic(err)
}
r.enable(utils.BuildAnnotationAllowNameChange)
}
// NameChangeAllowed checks if a patch resource is allowed to change another resource's name.
func (r *Resource) NameChangeAllowed() bool {
annotations := r.GetAnnotations()
v, ok := annotations[utils.BuildAnnotationAllowNameChange]
return ok && v == utils.Allowed
return r.isEnabled(utils.BuildAnnotationAllowNameChange)
}
// AllowKindChange allows kind changes to the resource.
func (r *Resource) AllowKindChange() {
r.enable(utils.BuildAnnotationAllowKindChange)
}
// KindChangeAllowed checks if a patch resource is allowed to change another resource's kind.
func (r *Resource) KindChangeAllowed() bool {
return r.isEnabled(utils.BuildAnnotationAllowKindChange)
}
func (r *Resource) isEnabled(annoKey string) bool {
annotations := r.GetAnnotations()
annotations[utils.BuildAnnotationAllowKindChange] = utils.Allowed
v, ok := annotations[annoKey]
return ok && v == utils.Enabled
}
func (r *Resource) enable(annoKey string) {
annotations := r.GetAnnotations()
annotations[annoKey] = utils.Enabled
if err := r.SetAnnotations(annotations); err != nil {
panic(err)
}
}
func (r *Resource) KindChangeAllowed() bool {
annotations := r.GetAnnotations()
v, ok := annotations[utils.BuildAnnotationAllowKindChange]
return ok && v == utils.Allowed
}
// String returns resource as JSON.
func (r *Resource) String() string {
bs, err := r.MarshalJSON()
@@ -302,43 +307,34 @@ func (r *Resource) MustYaml() string {
return string(yml)
}
func (r *Resource) getGenArgs() *types.GenArgs {
annotations := r.GetAnnotations()
if genOptsAnno, ok := annotations[utils.BuildAnnotationsGenOptions]; ok {
var genOpts types.GeneratorArgs
yaml.Unmarshal([]byte(genOptsAnno), &genOpts)
return types.NewGenArgs(&genOpts)
}
return nil
}
// SetOptions updates the generator options for the resource.
func (r *Resource) SetOptions(o *types.GenArgs) {
annotations := r.GetAnnotations()
if o.IsNilOrEmpty() {
if len(annotations) == 0 {
return
}
if o == nil {
delete(annotations, utils.BuildAnnotationsGenOptions)
}
} else {
b, _ := o.AsYaml()
annotations[utils.BuildAnnotationsGenOptions] = string(b)
}
r.SetAnnotations(annotations)
}
// Behavior returns the behavior for the resource.
func (r *Resource) Behavior() types.GenerationBehavior {
return r.getGenArgs().Behavior()
annotations := r.GetAnnotations()
if v, ok := annotations[utils.BuildAnnotationsGenBehavior]; ok {
return types.NewGenerationBehavior(v)
}
return types.NewGenerationBehavior("")
}
// SetBehavior sets the behavior for the resource.
func (r *Resource) SetBehavior(behavior types.GenerationBehavior) {
annotations := r.GetAnnotations()
annotations[utils.BuildAnnotationsGenBehavior] = behavior.String()
if err := r.SetAnnotations(annotations); err != nil {
panic(err)
}
}
// NeedHashSuffix returns true if a resource content
// hash should be appended to the name of the resource.
func (r *Resource) NeedHashSuffix() bool {
options := r.getGenArgs()
return options != nil && options.ShouldAddHashSuffixToName()
return r.isEnabled(utils.BuildAnnotationsGenAddHashSuffix)
}
// EnableHashSuffix marks the resource as needing a content
// hash to be appended to the name of the resource.
func (r *Resource) EnableHashSuffix() {
r.enable(utils.BuildAnnotationsGenAddHashSuffix)
}
// OrgId returns the original, immutable ResId for the resource.

View File

@@ -30,8 +30,6 @@ var testConfigMap = factory.FromMap(
//nolint:gosec
const configMapAsString = `{"apiVersion":"v1","kind":"ConfigMap","metadata":{"name":"winnie","namespace":"hundred-acre-wood"}}`
const configMapAsStringWithOptions = `{"apiVersion":"v1","kind":"ConfigMap","metadata":{"annotations":` +
`{"internal.config.kubernetes.io/generatorOptions":"{}\n"},"name":"winnie","namespace":"hundred-acre-wood"}}`
var testDeployment = factory.FromMap(
map[string]interface{}{
@@ -43,8 +41,6 @@ var testDeployment = factory.FromMap(
})
const deploymentAsString = `{"apiVersion":"apps/v1","kind":"Deployment","metadata":{"name":"pooh"}}`
const deploymentAsStringWithOptions = `{"apiVersion":"apps/v1","kind":"Deployment","metadata":{"annotations":` +
`{"internal.config.kubernetes.io/generatorOptions":"{}\n"},"name":"pooh"}}`
func TestAsYAML(t *testing.T) {
expected := `apiVersion: apps/v1
@@ -80,28 +76,6 @@ func TestResourceString(t *testing.T) {
}
}
func TestResourceStringWithOptionsAnnotations(t *testing.T) {
tests := []struct {
in *Resource
s string
}{
{
in: testConfigMap,
s: configMapAsStringWithOptions,
},
{
in: testDeployment,
s: deploymentAsStringWithOptions,
},
}
for _, test := range tests {
args := &types.GeneratorArgs{}
options := types.NewGenArgs(args)
test.in.SetOptions(options)
assert.Equal(t, test.in.String(), test.s)
}
}
func TestResourceId(t *testing.T) {
tests := []struct {
in *Resource
@@ -1193,35 +1167,3 @@ spec:
resid.FromString("gr2_ver2_knd2|ns2|name2"),
})
}
func TestOptions(t *testing.T) {
r, err := factory.FromBytes([]byte(`
apiVersion: v1
kind: ConfigMap
metadata:
name: example-configmap-test
`))
assert.NoError(t, err)
args := &types.GeneratorArgs{
Behavior: "merge",
Options: &types.GeneratorOptions{
DisableNameSuffixHash: true,
},
}
options := types.NewGenArgs(args)
r.SetOptions(options)
assert.Equal(t, r.RNode.MustString(), `apiVersion: v1
kind: ConfigMap
metadata:
name: example-configmap-test
annotations:
internal.config.kubernetes.io/generatorOptions: |
behavior: merge
options:
disableNameSuffixHash: true
`)
assert.Equal(t, r.Behavior(), types.BehaviorMerge)
assert.Equal(t, r.NeedHashSuffix(), !args.Options.DisableNameSuffixHash)
}