-
Notifications
You must be signed in to change notification settings - Fork 208
Remarshal manifest to decrease false-positive #3370
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 17 commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
05675a7
add remarshal
Hosshii 2cefae5
make gazelle
Hosshii 0f87ddb
output log
Hosshii a35ad8b
add license
Hosshii 428e2a4
add test
Hosshii b7c439a
add t.parallel
Hosshii b999bec
add t.parallel 2
Hosshii 91bae9d
fix test
Hosshii e0bac72
Update pkg/app/piped/cloudprovider/kubernetes/diff.go
Hosshii 1da2261
move file
Hosshii dca7151
fix comment
Hosshii 8efafd9
make gazelle
Hosshii 8f3e50a
fix
Hosshii adba142
return error and add logger
Hosshii dafefe1
delete non top level t.parallel
Hosshii b38e8b0
make gazelle
Hosshii 037b69a
delete unnecessary
Hosshii a9ade50
Update pkg/app/piped/cloudprovider/kubernetes/diff.go
Hosshii f859a26
Update pkg/app/piped/cloudprovider/kubernetes/diff.go
Hosshii 72d7cfc
Format unfmt-ed files
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,120 @@ | ||
| // Copyright 2022 The PipeCD Authors. | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| package kubernetes | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "encoding/json" | ||
| "reflect" | ||
|
|
||
| "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" | ||
| "k8s.io/apimachinery/pkg/runtime" | ||
| "k8s.io/client-go/kubernetes/scheme" | ||
| ) | ||
|
|
||
| // All functions in this file is borrowed from argocd/gitops-engine and modified | ||
| // All function except `remarshal` is borrowed from | ||
| // https://github.com/argoproj/gitops-engine/blob/0bc2f8c395f67123156d4ce6b667bf730618307f/pkg/utils/json/json.go | ||
| // and `remarshal` function is borrowed from | ||
| // https://github.com/argoproj/gitops-engine/blob/b0c5e00ccfa5d1e73087a18dc59e2e4c72f5f175/pkg/diff/diff.go#L685-L723 | ||
|
|
||
| // https://github.com/ksonnet/ksonnet/blob/master/pkg/kubecfg/diff.go | ||
| func removeFields(config, live interface{}) interface{} { | ||
| switch c := config.(type) { | ||
| case map[string]interface{}: | ||
| l, ok := live.(map[string]interface{}) | ||
| if ok { | ||
| return removeMapFields(c, l) | ||
| } | ||
| return live | ||
| case []interface{}: | ||
| l, ok := live.([]interface{}) | ||
| if ok { | ||
| return removeListFields(c, l) | ||
| } | ||
| return live | ||
| default: | ||
| return live | ||
| } | ||
|
|
||
| } | ||
|
|
||
| // removeMapFields remove all non-existent fields in the live that don't exist in the config | ||
| func removeMapFields(config, live map[string]interface{}) map[string]interface{} { | ||
| result := map[string]interface{}{} | ||
| for k, v1 := range config { | ||
| v2, ok := live[k] | ||
| if !ok { | ||
| continue | ||
| } | ||
| if v2 != nil { | ||
| v2 = removeFields(v1, v2) | ||
| } | ||
| result[k] = v2 | ||
| } | ||
| return result | ||
| } | ||
|
|
||
| func removeListFields(config, live []interface{}) []interface{} { | ||
| // If live is longer than config, then the extra elements at the end of the | ||
| // list will be returned as-is so they appear in the diff. | ||
| result := make([]interface{}, 0, len(live)) | ||
| for i, v2 := range live { | ||
| if len(config) > i { | ||
| if v2 != nil { | ||
| v2 = removeFields(config[i], v2) | ||
| } | ||
| result = append(result, v2) | ||
| } else { | ||
| result = append(result, v2) | ||
| } | ||
| } | ||
| return result | ||
| } | ||
|
|
||
| // remarshal checks resource kind and version and re-marshal using corresponding struct custom marshaller. | ||
| // This ensures that expected resource state is formatter same as actual resource state in kubernetes | ||
| // and allows to find differences between actual and target states more accurately. | ||
| // Remarshalling also strips any type information (e.g. float64 vs. int) from the unstructured | ||
| // object. This is important for diffing since it will cause godiff to report a false difference. | ||
| func remarshal(obj *unstructured.Unstructured) (*unstructured.Unstructured, error) { | ||
| data, err := json.Marshal(obj) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| item, err := scheme.Scheme.New(obj.GroupVersionKind()) | ||
| if err != nil { | ||
| // This is common. the scheme is not registered | ||
| return nil, err | ||
| } | ||
| // This will drop any omitempty fields, perform resource conversion etc... | ||
| unmarshalledObj := reflect.New(reflect.TypeOf(item).Elem()).Interface() | ||
| // Unmarshal data into unmarshalledObj, but detect if there are any unknown fields that are not | ||
| // found in the target GVK object. | ||
| decoder := json.NewDecoder(bytes.NewReader(data)) | ||
| decoder.DisallowUnknownFields() | ||
| if err := decoder.Decode(&unmarshalledObj); err != nil { | ||
| // Likely a field present in obj that is not present in the GVK type, or user | ||
| // may have specified an invalid spec in git, so return original object | ||
| return nil, err | ||
| } | ||
| unstrBody, err := runtime.DefaultUnstructuredConverter.ToUnstructured(unmarshalledObj) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| // Remove all default values specified by custom formatter (e.g. creationTimestamp) | ||
| unstrBody = removeMapFields(obj.Object, unstrBody) | ||
| return &unstructured.Unstructured{Object: unstrBody}, nil | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.