Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 15 additions & 7 deletions transform/kubernetes/kubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ var fieldsToStrip = [...][]string{
{metadata, "creationTimestamp"},
{metadata, "generation"},
{metadata, "managedFields"},
{metadata, "annotations", "kubectl.kubernetes.io/last-applied-configuration"},
{"status"},
}

Expand Down Expand Up @@ -569,12 +570,13 @@ func (k *KubernetesTransformPlugin) getKubernetesTransforms(obj unstructured.Uns
return jsonPatch, nil
}

func interfaceSlice(inStrings []string) []interface{} {
var outSlice []interface{}
for _, str := range inStrings {
outSlice = append(outSlice, str)
}
return outSlice
// escapeJSONPointer escapes a string for use in a JSON Pointer path according to RFC 6901
// ~ must be escaped as ~0
// / must be escaped as ~1
func escapeJSONPointer(s string) string {
s = strings.ReplaceAll(s, "~", "~0")
s = strings.ReplaceAll(s, "/", "~1")
return s
}

func stripFields(obj unstructured.Unstructured) (jsonpatch.Patch, error) {
Expand All @@ -585,7 +587,13 @@ func stripFields(obj unstructured.Unstructured) (jsonpatch.Patch, error) {
return patches, err
}
if found {
patch, err := jsonpatch.DecodePatch([]byte(fmt.Sprintf(opRemove, fmt.Sprintf(strings.Repeat("/%v", len(field)), interfaceSlice(field)...))))
// Build the JSON Pointer path with proper escaping
var pathParts []string
for _, f := range field {
pathParts = append(pathParts, escapeJSONPointer(f))
}
path := "/" + strings.Join(pathParts, "/")
patch, err := jsonpatch.DecodePatch([]byte(fmt.Sprintf(opRemove, path)))
if err != nil {
return nil, err
}
Expand Down
26 changes: 24 additions & 2 deletions transform/kubernetes/kubernetes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -446,6 +446,28 @@ func TestRun(t *testing.T) {
"multiple-testing",
},
},
{
Name: "RemoveLastAppliedConfigurationAnnotation",
Object: &unstructured.Unstructured{
Object: map[string]interface{}{
"kind": "Deployment",
"apiVersion": "apps/v1",
"metadata": map[string]interface{}{
"name": "test-deployment",
"namespace": "default",
"annotations": map[string]interface{}{
"kubectl.kubernetes.io/last-applied-configuration": `{"apiVersion":"apps/v1","kind":"Deployment","metadata":{"name":"test-deployment"}}`,
"other-annotation": "keep-this",
},
},
},
},
Response: transform.PluginResponse{
IsWhiteOut: false,
Version: "v1",
},
PatchResponseJson: `[{"op":"remove","path":"/metadata/annotations/kubectl.kubernetes.io~1last-applied-configuration"}]`,
},
{
Name: "HandlePod",
Object: &unstructured.Unstructured{
Expand Down Expand Up @@ -619,7 +641,7 @@ func TestRun(t *testing.T) {
IsWhiteOut: false,
Version: "v1",
},
PatchResponseJson: `[{"op": "remove", "path": "/spec/ports/0/nodePort"}]`,
PatchResponseJson: `[{"op":"remove","path":"/metadata/annotations/kubectl.kubernetes.io~1last-applied-configuration"},{"op": "remove", "path": "/spec/ports/0/nodePort"}]`,
},
{
Name: "HandleNodePortNamedAnnotation",
Expand Down Expand Up @@ -656,7 +678,7 @@ func TestRun(t *testing.T) {
IsWhiteOut: false,
Version: "v1",
},
PatchResponseJson: `[{"op": "remove", "path": "/spec/ports/1/nodePort"}]`,
PatchResponseJson: `[{"op":"remove","path":"/metadata/annotations/kubectl.kubernetes.io~1last-applied-configuration"},{"op": "remove", "path": "/spec/ports/1/nodePort"}]`,
},
}

Expand Down
Loading