Skip to content

Commit

Permalink
Remove dependence on kustomize/setters2
Browse files Browse the repository at this point in the history
The package setters2 has been removed from kustomize:
kubernetes-sigs/kustomize#5291

This commit removes the need to import setters2, by reproducing the
last link with that code, which is a minor parsing helper.

I have not changed the comment explaining what was changed from the
original, since it's still accurate. The parsing func is a buried
detail.

Signed-off-by: Michael Bridgen <[email protected]>
  • Loading branch information
squaremo authored and souleb committed Mar 12, 2024
1 parent 370992d commit 156c59b
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 5 deletions.
32 changes: 29 additions & 3 deletions pkg/update/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,12 @@ limitations under the License.
package update

import (
"encoding/json"

"github.com/go-logr/logr"
"k8s.io/kube-openapi/pkg/validation/spec"
"sigs.k8s.io/kustomize/kyaml/fieldmeta"
"sigs.k8s.io/kustomize/kyaml/openapi"
"sigs.k8s.io/kustomize/kyaml/setters2"
"sigs.k8s.io/kustomize/kyaml/yaml"
)

Expand Down Expand Up @@ -112,8 +113,17 @@ func accept(v visitor, object *yaml.RNode, p string, settersSchema *spec.Schema)
return nil
}

type setter struct {
Name string `json:"name"`
Value string `json:"value"`
}

type extension struct {
Setter *setter `json:"setter,omitempty"`
}

// set applies the value from ext to field
func (s *SetAllCallback) set(field *yaml.RNode, ext *setters2.CliExtension, sch *spec.Schema) (bool, error) {
func (s *SetAllCallback) set(field *yaml.RNode, ext *extension, sch *spec.Schema) (bool, error) {
// check full setter
if ext.Setter == nil {
return false, nil
Expand All @@ -139,7 +149,7 @@ func (s *SetAllCallback) visitScalar(object *yaml.RNode, p string, fieldSchema *
return nil
}
// get the openAPI for this field describing how to apply the setter
ext, err := setters2.GetExtFromSchema(fieldSchema.Schema)
ext, err := getExtFromSchema(fieldSchema.Schema)
if err != nil {
return err
}
Expand All @@ -152,3 +162,19 @@ func (s *SetAllCallback) visitScalar(object *yaml.RNode, p string, fieldSchema *
_, err = s.set(object, ext, fieldSchema.Schema)
return err
}

func getExtFromSchema(schema *spec.Schema) (*extension, error) {
cep := schema.VendorExtensible.Extensions[K8sCliExtensionKey]
if cep == nil {
return nil, nil
}
b, err := json.Marshal(cep)
if err != nil {
return nil, err
}
val := &extension{}
if err := json.Unmarshal(b, val); err != nil {
return nil, err
}
return val, nil
}
6 changes: 4 additions & 2 deletions pkg/update/setters.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,15 @@ import (
"sigs.k8s.io/kustomize/kyaml/kio/kioutil"
"sigs.k8s.io/kustomize/kyaml/openapi"
"sigs.k8s.io/kustomize/kyaml/sets"
"sigs.k8s.io/kustomize/kyaml/setters2"
"sigs.k8s.io/kustomize/kyaml/yaml"

imagev1_reflect "github.com/fluxcd/image-reflector-controller/api/v1beta2"
)

const (
// This is preserved from setters2
K8sCliExtensionKey = "x-k8s-cli"

// SetterShortHand is a shorthand that can be used to mark
// setters; instead of
// # { "$ref": "#/definitions/
Expand Down Expand Up @@ -256,7 +258,7 @@ func setAll(schema *spec.Schema, tracelog logr.Logger, callback func(file, sette
func setterSchema(name, value string) spec.Schema {
schema := spec.StringProperty()
schema.Extensions = map[string]interface{}{}
schema.Extensions.Add(setters2.K8sCliExtensionKey, map[string]interface{}{
schema.Extensions.Add(K8sCliExtensionKey, map[string]interface{}{
"setter": map[string]string{
"name": name,
"value": value,
Expand Down

0 comments on commit 156c59b

Please sign in to comment.