|
| 1 | +// Copyright 2022 The PipeCD Authors. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package kubernetes |
| 16 | + |
| 17 | +import ( |
| 18 | + "bytes" |
| 19 | + "encoding/json" |
| 20 | + "reflect" |
| 21 | + |
| 22 | + "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" |
| 23 | + "k8s.io/apimachinery/pkg/runtime" |
| 24 | + "k8s.io/client-go/kubernetes/scheme" |
| 25 | +) |
| 26 | + |
| 27 | +// All functions in this file is borrowed from argocd/gitops-engine and modified |
| 28 | +// All function except `remarshal` is borrowed from |
| 29 | +// https://github.com/argoproj/gitops-engine/blob/0bc2f8c395f67123156d4ce6b667bf730618307f/pkg/utils/json/json.go |
| 30 | +// and `remarshal` function is borrowed from |
| 31 | +// https://github.com/argoproj/gitops-engine/blob/b0c5e00ccfa5d1e73087a18dc59e2e4c72f5f175/pkg/diff/diff.go#L685-L723 |
| 32 | + |
| 33 | +// https://github.com/ksonnet/ksonnet/blob/master/pkg/kubecfg/diff.go |
| 34 | +func removeFields(config, live interface{}) interface{} { |
| 35 | + switch c := config.(type) { |
| 36 | + case map[string]interface{}: |
| 37 | + l, ok := live.(map[string]interface{}) |
| 38 | + if ok { |
| 39 | + return removeMapFields(c, l) |
| 40 | + } |
| 41 | + return live |
| 42 | + case []interface{}: |
| 43 | + l, ok := live.([]interface{}) |
| 44 | + if ok { |
| 45 | + return removeListFields(c, l) |
| 46 | + } |
| 47 | + return live |
| 48 | + default: |
| 49 | + return live |
| 50 | + } |
| 51 | + |
| 52 | +} |
| 53 | + |
| 54 | +// removeMapFields remove all non-existent fields in the live that don't exist in the config |
| 55 | +func removeMapFields(config, live map[string]interface{}) map[string]interface{} { |
| 56 | + result := map[string]interface{}{} |
| 57 | + for k, v1 := range config { |
| 58 | + v2, ok := live[k] |
| 59 | + if !ok { |
| 60 | + continue |
| 61 | + } |
| 62 | + if v2 != nil { |
| 63 | + v2 = removeFields(v1, v2) |
| 64 | + } |
| 65 | + result[k] = v2 |
| 66 | + } |
| 67 | + return result |
| 68 | +} |
| 69 | + |
| 70 | +func removeListFields(config, live []interface{}) []interface{} { |
| 71 | + // If live is longer than config, then the extra elements at the end of the |
| 72 | + // list will be returned as-is so they appear in the diff. |
| 73 | + result := make([]interface{}, 0, len(live)) |
| 74 | + for i, v2 := range live { |
| 75 | + if len(config) > i { |
| 76 | + if v2 != nil { |
| 77 | + v2 = removeFields(config[i], v2) |
| 78 | + } |
| 79 | + result = append(result, v2) |
| 80 | + } else { |
| 81 | + result = append(result, v2) |
| 82 | + } |
| 83 | + } |
| 84 | + return result |
| 85 | +} |
| 86 | + |
| 87 | +// remarshal checks resource kind and version and re-marshal using corresponding struct custom marshaller. |
| 88 | +// This ensures that expected resource state is formatter same as actual resource state in kubernetes |
| 89 | +// and allows to find differences between actual and target states more accurately. |
| 90 | +// Remarshalling also strips any type information (e.g. float64 vs. int) from the unstructured |
| 91 | +// object. This is important for diffing since it will cause godiff to report a false difference. |
| 92 | +func remarshal(obj *unstructured.Unstructured) (*unstructured.Unstructured, error) { |
| 93 | + data, err := json.Marshal(obj) |
| 94 | + if err != nil { |
| 95 | + return nil, err |
| 96 | + } |
| 97 | + item, err := scheme.Scheme.New(obj.GroupVersionKind()) |
| 98 | + if err != nil { |
| 99 | + // This is common. the scheme is not registered |
| 100 | + return nil, err |
| 101 | + } |
| 102 | + // This will drop any omitempty fields, perform resource conversion etc... |
| 103 | + unmarshalledObj := reflect.New(reflect.TypeOf(item).Elem()).Interface() |
| 104 | + // Unmarshal data into unmarshalledObj, but detect if there are any unknown fields that are not |
| 105 | + // found in the target GVK object. |
| 106 | + decoder := json.NewDecoder(bytes.NewReader(data)) |
| 107 | + decoder.DisallowUnknownFields() |
| 108 | + if err := decoder.Decode(&unmarshalledObj); err != nil { |
| 109 | + // Likely a field present in obj that is not present in the GVK type, or user |
| 110 | + // may have specified an invalid spec in git, so return original object |
| 111 | + return nil, err |
| 112 | + } |
| 113 | + unstrBody, err := runtime.DefaultUnstructuredConverter.ToUnstructured(unmarshalledObj) |
| 114 | + if err != nil { |
| 115 | + return nil, err |
| 116 | + } |
| 117 | + // Remove all default values specified by custom formatter (e.g. creationTimestamp) |
| 118 | + unstrBody = removeMapFields(obj.Object, unstrBody) |
| 119 | + return &unstructured.Unstructured{Object: unstrBody}, nil |
| 120 | +} |
0 commit comments