From d7e0b1ac3160510c424dd46f59943238075e89e1 Mon Sep 17 00:00:00 2001 From: Phillip Wittrock Date: Thu, 13 Feb 2020 16:25:20 -0800 Subject: [PATCH] setter utilities for simplifying commands --- kyaml/setters2/settersutil/fieldsetter.go | 43 +++++++++++++ kyaml/setters2/settersutil/settercreator.go | 60 +++++++++++++++++ .../settersutil/substitutioncreator.go | 64 +++++++++++++++++++ 3 files changed, 167 insertions(+) create mode 100644 kyaml/setters2/settersutil/fieldsetter.go create mode 100644 kyaml/setters2/settersutil/settercreator.go create mode 100644 kyaml/setters2/settersutil/substitutioncreator.go diff --git a/kyaml/setters2/settersutil/fieldsetter.go b/kyaml/setters2/settersutil/fieldsetter.go new file mode 100644 index 000000000..fdaa7c84c --- /dev/null +++ b/kyaml/setters2/settersutil/fieldsetter.go @@ -0,0 +1,43 @@ +// Copyright 2019 The Kubernetes Authors. +// SPDX-License-Identifier: Apache-2.0 + +package settersutil + +import ( + "sigs.k8s.io/kustomize/kyaml/kio" + "sigs.k8s.io/kustomize/kyaml/openapi" + "sigs.k8s.io/kustomize/kyaml/setters2" +) + +// FieldSetter sets the value for a field setter. +type FieldSetter struct { + // Name is the name of the setter to set + Name string + + // Value is the value to set + Value string +} + +// Set updates the OpenAPI definitions and resources with the new setter value +func (fs FieldSetter) Set(openAPIPath, resourcesPath string) (int, error) { + // Update the OpenAPI definitions + soa := setters2.SetOpenAPI{Name: fs.Name, Value: fs.Value} + if err := soa.UpdateFile(openAPIPath); err != nil { + return 0, err + } + + // Load the updated definitions + if err := openapi.AddSchemaFromFile(openAPIPath); err != nil { + return 0, err + } + + // Update the resources with the new value + inout := &kio.LocalPackageReadWriter{PackagePath: resourcesPath} + s := &setters2.Set{Name: fs.Name} + err := kio.Pipeline{ + Inputs: []kio.Reader{inout}, + Filters: []kio.Filter{kio.FilterAll(s)}, + Outputs: []kio.Writer{inout}, + }.Execute() + return s.Count, err +} diff --git a/kyaml/setters2/settersutil/settercreator.go b/kyaml/setters2/settersutil/settercreator.go new file mode 100644 index 000000000..121be30ce --- /dev/null +++ b/kyaml/setters2/settersutil/settercreator.go @@ -0,0 +1,60 @@ +// Copyright 2019 The Kubernetes Authors. +// SPDX-License-Identifier: Apache-2.0 + +package settersutil + +import ( + "sigs.k8s.io/kustomize/kyaml/kio" + "sigs.k8s.io/kustomize/kyaml/openapi" + "sigs.k8s.io/kustomize/kyaml/setters2" +) + +// SetterCreator creates or updates a setter in the OpenAPI definitions, and inserts references +// to the setter from matching resource fields. +type SetterCreator struct { + // Name is the name of the setter to create or update. + Name string + + Description string + + SetBy string + + // FieldName if set will add the OpenAPI reference to fields with this name or path + // FieldName may be the full name of the field, full path to the field, or the path suffix. + // e.g. all of the following would match spec.template.spec.containers.image -- + // [image, containers.image, spec.containers.image, template.spec.containers.image, + // spec.template.spec.containers.image] + // Optional. If unspecified match all field names. + FieldName string + + // FieldValue if set will add the OpenAPI reference to fields if they have this value. + // Optional. If unspecified match all field values. + FieldValue string +} + +func (c SetterCreator) Create(openAPIPath, resourcesPath string) error { + // Update the OpenAPI definitions to hace the setter + sd := setters2.SetterDefinition{ + Name: c.Name, Value: c.FieldValue, Description: c.Description, SetBy: c.SetBy} + if err := sd.AddToFile(openAPIPath); err != nil { + return err + } + + // Load the updated definitions + if err := openapi.AddSchemaFromFile(openAPIPath); err != nil { + return err + } + + // Update the resources with the setter reference + inout := &kio.LocalPackageReadWriter{PackagePath: resourcesPath} + return kio.Pipeline{ + Inputs: []kio.Reader{inout}, + Filters: []kio.Filter{kio.FilterAll( + &setters2.Add{ + FieldName: c.FieldName, + FieldValue: c.FieldValue, + Ref: setters2.DefinitionsPrefix + setters2.SetterDefinitionPrefix + c.Name, + })}, + Outputs: []kio.Writer{inout}, + }.Execute() +} diff --git a/kyaml/setters2/settersutil/substitutioncreator.go b/kyaml/setters2/settersutil/substitutioncreator.go new file mode 100644 index 000000000..094f34d1c --- /dev/null +++ b/kyaml/setters2/settersutil/substitutioncreator.go @@ -0,0 +1,64 @@ +// Copyright 2019 The Kubernetes Authors. +// SPDX-License-Identifier: Apache-2.0 + +package settersutil + +import ( + "sigs.k8s.io/kustomize/kyaml/kio" + "sigs.k8s.io/kustomize/kyaml/openapi" + "sigs.k8s.io/kustomize/kyaml/setters2" +) + +// SubstitutionCreator creates or updates a substitution in the OpenAPI definitions, and +// inserts references to the substitution from matching resource fields. +type SubstitutionCreator struct { + // Name is the name of the substitution to create + Name string + + // Pattern is the substitution pattern + Pattern string + + // Values are the substitution values for the pattern + Values []setters2.Value + + // FieldName if set will add the OpenAPI reference to fields with this name or path + // FieldName may be the full name of the field, full path to the field, or the path suffix. + // e.g. all of the following would match spec.template.spec.containers.image -- + // [image, containers.image, spec.containers.image, template.spec.containers.image, + // spec.template.spec.containers.image] + // Optional. If unspecified match all field names. + FieldName string + + // FieldValue if set will add the OpenAPI reference to fields if they have this value. + // Optional. If unspecified match all field values. + FieldValue string +} + +func (c SubstitutionCreator) Create(openAPIPath, resourcesPath string) error { + d := setters2.SubstitutionDefinition{ + Name: c.Name, + Values: c.Values, + Pattern: c.Pattern, + } + if err := d.AddToFile(openAPIPath); err != nil { + return err + } + + // Load the updated definitions + if err := openapi.AddSchemaFromFile(openAPIPath); err != nil { + return err + } + + // Update the resources with the setter reference + inout := &kio.LocalPackageReadWriter{PackagePath: resourcesPath} + return kio.Pipeline{ + Inputs: []kio.Reader{inout}, + Filters: []kio.Filter{kio.FilterAll( + &setters2.Add{ + FieldName: c.FieldName, + FieldValue: c.FieldValue, + Ref: setters2.DefinitionsPrefix + setters2.SubstitutionDefinitionPrefix + c.Name, + })}, + Outputs: []kio.Writer{inout}, + }.Execute() +}