Skip to content

Commit

Permalink
++
Browse files Browse the repository at this point in the history
Signed-off-by: Ivan Mikheykin <[email protected]>
  • Loading branch information
diafour committed Jun 17, 2024
1 parent 4bc5e67 commit 9a24b12
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 78 deletions.
66 changes: 0 additions & 66 deletions images/kube-api-proxy/pkg/rewriter/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -358,72 +358,6 @@ func RenameManagedFields(rules *RewriteRules, obj []byte) ([]byte, error) {
return sjson.SetRawBytes(obj, "metadata.managedFields", newFields)
}

func RewriteMetadata(rules *RewriteRules, obj []byte, action Action) ([]byte, error) {
newObj, err := RewriteMetadataLabels(rules, obj, action)
if err != nil {
return nil, err
}
return RewriteMetadataAnnotations(rules, newObj, action)
}

func RewriteMetadataLabels(rules *RewriteRules, obj []byte, action Action) ([]byte, error) {
labels := gjson.GetBytes(obj, "metadata.labels").Map()
if len(labels) == 0 {
return obj, nil
}

newLabels := make(map[string]string, len(labels))
for k, v := range labels {
newLabels[k] = v.String()
}
switch action {
case Rename:
newLabels = rules.RenameLabels(newLabels)
case Restore:
newLabels = rules.RestoreLabels(newLabels)
}

return sjson.SetBytes(obj, "metadata.labels", newLabels)
}

func RewriteMetadataAnnotations(rules *RewriteRules, obj []byte, action Action) ([]byte, error) {
annos := gjson.GetBytes(obj, "metadata.annotations").Map()
if len(annos) == 0 {
return obj, nil
}
newAnnos := make(map[string]string, len(annos))
for k, v := range annos {
newAnnos[k] = v.String()
}
switch action {
case Rename:
newAnnos = rules.RenameAnnotations(newAnnos)
case Restore:
newAnnos = rules.RestoreAnnotations(newAnnos)
}

return sjson.SetBytes(obj, "metadata.annotations", newAnnos)
}

func RewriteFinalizers(rules *RewriteRules, obj []byte, action Action) ([]byte, error) {
fins := gjson.GetBytes(obj, "spec.finalizers").Array()
if len(fins) == 0 {
return obj, nil
}
newFins := make([]string, len(fins))
for i, f := range fins {
newFins[i] = f.String()
}
switch action {
case Rename:
newFins = rules.RenameFinalizers(newFins)
case Restore:
newFins = rules.RestoreFinalizers(newFins)
}

return sjson.SetBytes(obj, "spec.finalizers", newFins)
}

func RenameResourcePatch(rules *RewriteRules, patch []byte) ([]byte, error) {
return RenameMetadataPatch(rules, patch)
}
15 changes: 3 additions & 12 deletions images/kube-api-proxy/pkg/rewriter/rule_rewriter.go
Original file line number Diff line number Diff line change
Expand Up @@ -288,20 +288,11 @@ func (rw *RuleBasedRewriter) RewriteJSONPayload(targetReq *TargetRequest, obj []
return obj, err
}

rwrBytes, err = RewriteResourceOrList2(rwrBytes, func(singleObj []byte) ([]byte, error) {
return RewriteFinalizers(rw.Rules, singleObj, action)
})
if err != nil {
return obj, err
}

if shouldRewriteOwnerReferences(kind) {
rwrBytes, err = RewriteOwnerReferences(rw.Rules, rwrBytes, action)
}

// Return obj bytes as-is in case of the error.
if err != nil {
return obj, err
if err != nil {
return obj, err
}
}

return rwrBytes, nil
Expand Down

0 comments on commit 9a24b12

Please sign in to comment.