Skip to content

Commit

Permalink
Remove the wrappy layer.
Browse files Browse the repository at this point in the history
  • Loading branch information
monopole committed Mar 15, 2021
1 parent e4bbd04 commit 123a5d6
Show file tree
Hide file tree
Showing 37 changed files with 1,535 additions and 1,748 deletions.
2 changes: 1 addition & 1 deletion api/builtins/HashTransformer.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 3 additions & 8 deletions api/builtins/NamespaceTransformer.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 0 additions & 2 deletions api/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,11 @@ require (
github.com/evanphx/json-patch v4.5.0+incompatible
github.com/go-errors/errors v1.0.1
github.com/go-openapi/spec v0.19.5
github.com/google/go-cmp v0.4.0
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510
github.com/imdario/mergo v0.3.5
github.com/pkg/errors v0.9.1
github.com/stretchr/testify v1.4.0
gopkg.in/yaml.v2 v2.4.0
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c
sigs.k8s.io/kustomize/kyaml v0.10.15
sigs.k8s.io/yaml v1.2.0
)
Expand Down
1 change: 0 additions & 1 deletion api/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,6 @@ golang.org/x/tools v0.0.0-20190125232054-d66bd3c5d5a6/go.mod h1:n7NCudcB/nEzxVGm
golang.org/x/tools v0.0.0-20190311212946-11955173bddd/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs=
golang.org/x/tools v0.0.0-20190614205625-5aca471b1d59/go.mod h1:/rFqwRUd4F7ZHNgwSSTFct+R/Kf4OFW1sUzUTQQTgfc=
golang.org/x/tools v0.0.0-20190617190820-da514acc4774/go.mod h1:/rFqwRUd4F7ZHNgwSSTFct+R/Kf4OFW1sUzUTQQTgfc=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM=
google.golang.org/genproto v0.0.0-20180817151627-c66870c02cf8/go.mod h1:JiN7NxoALGmiZfu7CAH4rXhgtRTLTxftemlI0sWmxmc=
Expand Down
37 changes: 18 additions & 19 deletions api/hasher/hasher.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@ func SortArrayAndComputeHash(s []string) (string, error) {
if err != nil {
return "", err
}
return Encode(Hash(string(data)))
return encode(hex256(string(data)))
}

// Copied from https://github.com/kubernetes/kubernetes
// /blob/master/pkg/kubectl/util/hash/hash.go
func Encode(hex string) (string, error) {
func encode(hex string) (string, error) {
if len(hex) < 10 {
return "", fmt.Errorf(
"input length must be at least 10")
Expand All @@ -48,23 +48,18 @@ func Encode(hex string) (string, error) {
return string(enc), nil
}

// Hash returns the hex form of the sha256 of the argument.
func Hash(data string) string {
// hex256 returns the hex form of the sha256 of the argument.
func hex256(data string) string {
return fmt.Sprintf("%x", sha256.Sum256([]byte(data)))
}

// HashRNode returns the hash value of input RNode
func HashRNode(node *yaml.RNode) (string, error) {
// get node kind
kindNode, err := node.Pipe(yaml.FieldMatcher{Name: "kind"})
if err != nil {
return "", err
}
kind := kindNode.YNode().Value
// Hasher computes the hash of an RNode.
type Hasher struct{}

// calculate hash for different kinds
encoded := ""
switch kind {
// Hash returns a hash of the argument.
func (h *Hasher) Hash(node *yaml.RNode) (r string, err error) {
var encoded string
switch node.GetKind() {
case "ConfigMap":
encoded, err = encodeConfigMap(node)
case "Secret":
Expand All @@ -77,10 +72,11 @@ func HashRNode(node *yaml.RNode) (string, error) {
if err != nil {
return "", err
}
return Encode(Hash(encoded))
return encode(hex256(encoded))
}

func getNodeValues(node *yaml.RNode, paths []string) (map[string]interface{}, error) {
func getNodeValues(
node *yaml.RNode, paths []string) (map[string]interface{}, error) {
values := make(map[string]interface{})
for _, p := range paths {
vn, err := node.Pipe(yaml.Lookup(p))
Expand Down Expand Up @@ -117,8 +113,11 @@ func encodeConfigMap(node *yaml.RNode) (string, error) {
if err != nil {
return "", err
}
m := map[string]interface{}{"kind": "ConfigMap", "name": values["metadata/name"],
"data": values["data"]}
m := map[string]interface{}{
"kind": "ConfigMap",
"name": values["metadata/name"],
"data": values["data"],
}
if _, ok := values["binaryData"].(map[string]interface{}); ok {
m["binaryData"] = values["binaryData"]
}
Expand Down
29 changes: 15 additions & 14 deletions api/hasher/hasher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ func TestSortArrayAndComputeHash(t *testing.T) {
}
}

func TestHash(t *testing.T) {
func Test_hex256(t *testing.T) {
// hash the empty string to be sure that sha256 is being used
expect := "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"
sum := Hash("")
sum := hex256("")
if expect != sum {
t.Errorf("expected hash %q but got %q", expect, sum)
}
Expand Down Expand Up @@ -93,17 +93,17 @@ data:
binaryData:
two: ""`, "698h7c7t9m", ""},
}

h := &Hasher{}
for _, c := range cases {
node, err := yaml.Parse(c.cmYaml)
if err != nil {
t.Fatal(err)
}
h, err := HashRNode(node)
hashed, err := h.Hash(node)
if SkipRest(t, c.desc, err, c.err) {
continue
}
if c.hash != h {
if c.hash != hashed {
t.Errorf("case %q, expect hash %q but got %q", c.desc, c.hash, h)
}
}
Expand Down Expand Up @@ -154,17 +154,17 @@ type: my-type
data:
one: ""`, "74bd68bm66", ""},
}

h := &Hasher{}
for _, c := range cases {
node, err := yaml.Parse(c.secretYaml)
if err != nil {
t.Fatal(err)
}
h, err := HashRNode(node)
hashed, err := h.Hash(node)
if SkipRest(t, c.desc, err, c.err) {
continue
}
if c.hash != h {
if c.hash != hashed {
t.Errorf("case %q, expect hash %q but got %q", c.desc, c.hash, h)
}
}
Expand All @@ -191,17 +191,17 @@ spec:
foo: 1
bar: abc`, "59m2mdccg4", ""},
}

h := &Hasher{}
for _, c := range cases {
node, err := yaml.Parse(c.unstructured)
if err != nil {
t.Fatal(err)
}
h, err := HashRNode(node)
hashed, err := h.Hash(node)
if SkipRest(t, c.desc, err, c.err) {
continue
}
if c.hash != h {
if c.hash != hashed {
t.Errorf("case %q, expect hash %q but got %q", c.desc, c.hash, h)
}
}
Expand Down Expand Up @@ -334,9 +334,10 @@ data:
}
}

// SkipRest returns true if there was a non-nil error or if we expected an error that didn't happen,
// and logs the appropriate error on the test object.
// The return value indicates whether we should skip the rest of the test case due to the error result.
// SkipRest returns true if there was a non-nil error or if we expected an
// error that didn't happen, and logs the appropriate error on the test object.
// The return value indicates whether we should skip the rest of the test case
// due to the error result.
func SkipRest(t *testing.T, desc string, err error, contains string) bool {
if err != nil {
if len(contains) == 0 {
Expand Down
3 changes: 2 additions & 1 deletion api/ifc/ifc.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package ifc
import (
"sigs.k8s.io/kustomize/api/resid"
"sigs.k8s.io/kustomize/api/types"
"sigs.k8s.io/kustomize/kyaml/yaml"
)

// Validator provides functions to validate annotations and labels
Expand Down Expand Up @@ -123,7 +124,7 @@ type KunstructuredFactory interface {
// KunstructuredHasher returns a hash of the argument
// or an error.
type KunstructuredHasher interface {
Hash(Kunstructured) (string, error)
Hash(*yaml.RNode) (string, error)
}

// See core.v1.SecretTypeOpaque
Expand Down
16 changes: 2 additions & 14 deletions api/internal/target/multitransformer.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,22 +37,10 @@ func (o *multiTransformer) Transform(m resmap.ResMap) error {

func (o *multiTransformer) transform(m resmap.ResMap) error {
for _, t := range o.transformers {
err := t.Transform(m)
if err != nil {
if err := t.Transform(m); err != nil {
return err
}
}
for _, r := range m.Resources() {
empty, err := r.IsEmpty()
if err != nil {
return err
}
if empty {
err := m.Remove(r.CurId())
if err != nil {
return err
}
}
m.DropEmpties()
}
return nil
}
Expand Down
108 changes: 0 additions & 108 deletions api/internal/wrappy/factory.go

This file was deleted.

0 comments on commit 123a5d6

Please sign in to comment.