|
| 1 | +package kubernetes |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "io" |
| 6 | + "strings" |
| 7 | + |
| 8 | + "github.com/pkg/errors" |
| 9 | + "github.com/stretchr/objx" |
| 10 | + yaml "gopkg.in/yaml.v2" |
| 11 | + |
| 12 | + "github.com/sh0rez/tanka/pkg/util" |
| 13 | +) |
| 14 | + |
| 15 | +type difference struct { |
| 16 | + live, merged string |
| 17 | +} |
| 18 | + |
| 19 | +func (k Kubectl) SubsetDiff(y string) (string, error) { |
| 20 | + docs := map[string]difference{} |
| 21 | + d := yaml.NewDecoder(strings.NewReader(y)) |
| 22 | + for { |
| 23 | + |
| 24 | + // jsonnet output -> desired state |
| 25 | + var rawShould map[interface{}]interface{} |
| 26 | + err := d.Decode(&rawShould) |
| 27 | + if err == io.EOF { |
| 28 | + break |
| 29 | + } |
| 30 | + |
| 31 | + if err != nil { |
| 32 | + return "", errors.Wrap(err, "decoding yaml") |
| 33 | + } |
| 34 | + |
| 35 | + // filename |
| 36 | + m := objx.New(util.CleanupInterfaceMap(rawShould)) |
| 37 | + name := strings.Replace(fmt.Sprintf("%s.%s.%s.%s", |
| 38 | + m.Get("apiVersion").MustStr(), |
| 39 | + m.Get("kind").MustStr(), |
| 40 | + m.Get("metadata.namespace").MustStr(), |
| 41 | + m.Get("metadata.name").MustStr(), |
| 42 | + ), "/", "-", -1) |
| 43 | + |
| 44 | + // kubectl output -> current state |
| 45 | + rawIs, err := k.Get( |
| 46 | + m.Get("metadata.namespace").MustStr(), |
| 47 | + m.Get("kind").MustStr(), |
| 48 | + m.Get("metadata.name").MustStr(), |
| 49 | + ) |
| 50 | + if err != nil { |
| 51 | + if _, ok := err.(ErrorNotFound); ok { |
| 52 | + rawIs = map[string]interface{}{} |
| 53 | + } else { |
| 54 | + return "", errors.Wrap(err, "getting state from cluster") |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + should, err := yaml.Marshal(rawShould) |
| 59 | + if err != nil { |
| 60 | + return "", err |
| 61 | + } |
| 62 | + |
| 63 | + is, err := yaml.Marshal(subset(m, rawIs)) |
| 64 | + if err != nil { |
| 65 | + return "", err |
| 66 | + } |
| 67 | + if string(is) == "{}\n" { |
| 68 | + is = []byte("") |
| 69 | + } |
| 70 | + docs[name] = difference{string(is), string(should)} |
| 71 | + } |
| 72 | + |
| 73 | + s := "" |
| 74 | + for k, v := range docs { |
| 75 | + d, err := diff(k, v.live, v.merged) |
| 76 | + if err != nil { |
| 77 | + return "", errors.Wrap(err, "invoking diff") |
| 78 | + } |
| 79 | + if d != "" { |
| 80 | + d += "\n" |
| 81 | + } |
| 82 | + s += d |
| 83 | + } |
| 84 | + |
| 85 | + return s, nil |
| 86 | +} |
| 87 | + |
| 88 | +// subset removes all keys from is, that are not present in should. |
| 89 | +// It makes is a subset of should. |
| 90 | +// Kubernetes returns more keys than we can know about. |
| 91 | +// This means, we need to remove all keys from the kubectl output, that are not present locally. |
| 92 | +func subset(should, is map[string]interface{}) map[string]interface{} { |
| 93 | + if should["namespace"] != nil { |
| 94 | + is["namespace"] = should["namespace"] |
| 95 | + } |
| 96 | + for k, v := range is { |
| 97 | + if should[k] == nil { |
| 98 | + delete(is, k) |
| 99 | + continue |
| 100 | + } |
| 101 | + |
| 102 | + switch b := v.(type) { |
| 103 | + case map[string]interface{}: |
| 104 | + if a, ok := should[k].(map[string]interface{}); ok { |
| 105 | + is[k] = subset(a, b) |
| 106 | + } |
| 107 | + case []map[string]interface{}: |
| 108 | + for i := range b { |
| 109 | + if a, ok := should[k].([]map[string]interface{}); ok { |
| 110 | + b[i] = subset(a[i], b[i]) |
| 111 | + } |
| 112 | + } |
| 113 | + case []interface{}: |
| 114 | + for i := range b { |
| 115 | + if a, ok := should[k].([]interface{}); ok { |
| 116 | + aa, ok := a[i].(map[string]interface{}) |
| 117 | + if !ok { |
| 118 | + continue |
| 119 | + } |
| 120 | + bb, ok := b[i].(map[string]interface{}) |
| 121 | + if !ok { |
| 122 | + continue |
| 123 | + } |
| 124 | + b[i] = subset(aa, bb) |
| 125 | + } |
| 126 | + } |
| 127 | + } |
| 128 | + } |
| 129 | + return is |
| 130 | +} |
0 commit comments