From 67a2466c32668e9dba55383f263402deaacdfc1d Mon Sep 17 00:00:00 2001 From: Bricktop Date: Mon, 11 Oct 2021 21:44:43 +0200 Subject: [PATCH] Remove dead code in various packages (#2514) --- pkg/dclient/client.go | 10 ---------- pkg/engine/common/utils.go | 14 ++++++++++++++ pkg/engine/mutate/mutation.go | 24 ------------------------ pkg/engine/mutate/overlay.go | 3 ++- pkg/engine/mutate/utils.go | 14 -------------- pkg/engine/utils.go | 7 ++++--- pkg/engine/validate/common.go | 14 -------------- pkg/engine/validate/validate.go | 2 +- 8 files changed, 21 insertions(+), 67 deletions(-) diff --git a/pkg/dclient/client.go b/pkg/dclient/client.go index c342b57a11..16d1e98742 100644 --- a/pkg/dclient/client.go +++ b/pkg/dclient/client.go @@ -8,7 +8,6 @@ import ( "github.com/go-logr/logr" openapiv2 "github.com/googleapis/gnostic/openapiv2" - certificates "k8s.io/api/certificates/v1beta1" helperv1 "k8s.io/apimachinery/pkg/apis/meta/v1" meta "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" @@ -195,15 +194,6 @@ func convertToUnstructured(obj interface{}) *unstructured.Unstructured { return &unstructured.Unstructured{Object: unstructuredObj} } -//To-Do remove this to use unstructured type -func convertToCSR(obj *unstructured.Unstructured) (*certificates.CertificateSigningRequest, error) { - csr := certificates.CertificateSigningRequest{} - if err := runtime.DefaultUnstructuredConverter.FromUnstructured(obj.UnstructuredContent(), &csr); err != nil { - return nil, err - } - return &csr, nil -} - //IDiscovery provides interface to mange Kind and GVR mapping type IDiscovery interface { FindResource(apiVersion string, kind string) (*meta.APIResource, schema.GroupVersionResource, error) diff --git a/pkg/engine/common/utils.go b/pkg/engine/common/utils.go index 9ec28bf676..47cca700ef 100644 --- a/pkg/engine/common/utils.go +++ b/pkg/engine/common/utils.go @@ -48,3 +48,17 @@ func ToMap(data interface{}) (map[string]interface{}, error) { return mapData, nil } + +func GetRawKeyIfWrappedWithAttributes(str string) string { + if len(str) < 2 { + return str + } + + if str[0] == '(' && str[len(str)-1] == ')' { + return str[1 : len(str)-1] + } else if (str[0] == '$' || str[0] == '^' || str[0] == '+' || str[0] == '=') && (str[1] == '(' && str[len(str)-1] == ')') { + return str[2 : len(str)-1] + } else { + return str + } +} diff --git a/pkg/engine/mutate/mutation.go b/pkg/engine/mutate/mutation.go index 80ee5a101e..51e6e3b5b2 100644 --- a/pkg/engine/mutate/mutation.go +++ b/pkg/engine/mutate/mutation.go @@ -82,31 +82,11 @@ func (h forEachHandler) Handle() (response.RuleResponse, unstructured.Unstructur return ProcessStrategicMergePatch(h.ruleName, h.mutation.ForEachMutation.PatchStrategicMerge, h.patchedResource, h.logger) } -// overlayHandler -type overlayHandler struct { - ruleName string - mutation *kyverno.Mutation - patchedResource unstructured.Unstructured - evalCtx context.EvalInterface - logger logr.Logger -} - -func newOverlayHandler(ruleName string, mutate *kyverno.Mutation, patchedResource unstructured.Unstructured, context context.EvalInterface, logger logr.Logger) Handler { - return overlayHandler{ - ruleName: ruleName, - mutation: mutate, - patchedResource: patchedResource, - evalCtx: context, - logger: logger, - } -} - // patchesJSON6902Handler type patchesJSON6902Handler struct { ruleName string mutation *kyverno.Mutation patchedResource unstructured.Unstructured - evalCtx context.EvalInterface logger logr.Logger } @@ -134,10 +114,6 @@ func (h patchesJSON6902Handler) Handle() (resp response.RuleResponse, patchedRes return ProcessPatchJSON6902(h.ruleName, patchesJSON6902, h.patchedResource, h.logger) } -func (h overlayHandler) Handle() (response.RuleResponse, unstructured.Unstructured) { - return ProcessOverlay(h.logger, h.ruleName, h.mutation.Overlay, h.patchedResource) -} - // patchesHandler type patchesHandler struct { ruleName string diff --git a/pkg/engine/mutate/overlay.go b/pkg/engine/mutate/overlay.go index 766a902a31..d103705eab 100644 --- a/pkg/engine/mutate/overlay.go +++ b/pkg/engine/mutate/overlay.go @@ -13,6 +13,7 @@ import ( jsonpatch "github.com/evanphx/json-patch/v5" "github.com/go-logr/logr" commonAnchors "github.com/kyverno/kyverno/pkg/engine/anchor/common" + "github.com/kyverno/kyverno/pkg/engine/common" "github.com/kyverno/kyverno/pkg/engine/response" "github.com/kyverno/kyverno/pkg/engine/utils" "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" @@ -426,7 +427,7 @@ func removeAnchorFromSubTree(overlay interface{}) interface{} { func removeAnchroFromMap(overlay map[string]interface{}) map[string]interface{} { result := make(map[string]interface{}) for k, v := range overlay { - result[getRawKeyIfWrappedWithAttributes(k)] = removeAnchorFromSubTree(v) + result[common.GetRawKeyIfWrappedWithAttributes(k)] = removeAnchorFromSubTree(v) } return result } diff --git a/pkg/engine/mutate/utils.go b/pkg/engine/mutate/utils.go index 887fa8c5bf..b29ccf8183 100644 --- a/pkg/engine/mutate/utils.go +++ b/pkg/engine/mutate/utils.go @@ -33,20 +33,6 @@ func removeAnchor(key string) string { return key } -func getRawKeyIfWrappedWithAttributes(str string) string { - if len(str) < 2 { - return str - } - - if str[0] == '(' && str[len(str)-1] == ')' { - return str[1 : len(str)-1] - } else if (str[0] == '$' || str[0] == '^' || str[0] == '+' || str[0] == '=') && (str[1] == '(' && str[len(str)-1] == ')') { - return str[2 : len(str)-1] - } else { - return str - } -} - // getAnchorAndElementsFromMap gets the condition anchor map and resource map without anchor func getAnchorAndElementsFromMap(anchorsMap map[string]interface{}) (map[string]interface{}, map[string]interface{}) { anchors := make(map[string]interface{}) diff --git a/pkg/engine/utils.go b/pkg/engine/utils.go index de8be81230..9e71518447 100644 --- a/pkg/engine/utils.go +++ b/pkg/engine/utils.go @@ -2,15 +2,16 @@ package engine import ( "fmt" + "reflect" + "strings" + "time" + "github.com/go-logr/logr" "github.com/kyverno/kyverno/pkg/engine/context" "github.com/kyverno/kyverno/pkg/engine/response" engineUtils "github.com/kyverno/kyverno/pkg/engine/utils" "github.com/kyverno/kyverno/pkg/engine/variables" "github.com/pkg/errors" - "reflect" - "strings" - "time" kyverno "github.com/kyverno/kyverno/pkg/api/kyverno/v1" "github.com/kyverno/kyverno/pkg/engine/wildcards" diff --git a/pkg/engine/validate/common.go b/pkg/engine/validate/common.go index 3009fc3fb7..9e4e581189 100644 --- a/pkg/engine/validate/common.go +++ b/pkg/engine/validate/common.go @@ -26,17 +26,3 @@ func convertNumberToString(value interface{}) (string, error) { return "", fmt.Errorf("could not convert %v to string", typed) } } - -func getRawKeyIfWrappedWithAttributes(str string) string { - if len(str) < 2 { - return str - } - - if str[0] == '(' && str[len(str)-1] == ')' { - return str[1 : len(str)-1] - } else if (str[0] == '$' || str[0] == '^' || str[0] == '+' || str[0] == '=') && (str[1] == '(' && str[len(str)-1] == ')') { - return str[2 : len(str)-1] - } else { - return str - } -} diff --git a/pkg/engine/validate/validate.go b/pkg/engine/validate/validate.go index e11d239539..45b7d5957a 100644 --- a/pkg/engine/validate/validate.go +++ b/pkg/engine/validate/validate.go @@ -180,7 +180,7 @@ func validateArray(log logr.Logger, resourceArray, patternArray []interface{}, o func getValueFromPattern(log logr.Logger, patternMap map[string]interface{}, keys []string, currentKeyIndex int) (interface{}, error) { for key, pattern := range patternMap { - rawKey := getRawKeyIfWrappedWithAttributes(key) + rawKey := common.GetRawKeyIfWrappedWithAttributes(key) if rawKey == keys[len(keys)-1] && currentKeyIndex == len(keys)-1 { return pattern, nil