From 3343d73b76cbda8d3b12a7c18087f4bfc306b1fe Mon Sep 17 00:00:00 2001
From: shuting <shutting06@gmail.com>
Date: Mon, 27 Jan 2020 08:58:53 -0800
Subject: [PATCH] linter fix (#657)

---
 pkg/api/kyverno/v1/types.go          |  2 +-
 pkg/dclient/utils.go                 |  3 --
 pkg/engine/mutate/overlay.go         |  5 +-
 pkg/engine/mutation.go               |  2 +-
 pkg/engine/policy/validate.go        |  4 +-
 pkg/engine/validate/pattern.go       |  2 +-
 pkg/engine/validate/validate_test.go | 76 ++++++++++++++--------------
 pkg/namespace/generation.go          |  2 +-
 pkg/policy/existing.go               |  2 +-
 9 files changed, 48 insertions(+), 50 deletions(-)

diff --git a/pkg/api/kyverno/v1/types.go b/pkg/api/kyverno/v1/types.go
index 122c807cdd..b3ba284b7d 100644
--- a/pkg/api/kyverno/v1/types.go
+++ b/pkg/api/kyverno/v1/types.go
@@ -241,7 +241,7 @@ type PolicyStatus struct {
 	// average time required to process the policy Validation rules on a resource
 	AvgExecutionTimeGeneration string `json:"averageGenerationRulesExecutionTime"`
 	// statistics per rule
-	Rules []RuleStats `json:"ruleStatus`
+	Rules []RuleStats `json:"ruleStatus"`
 }
 
 //RuleStats provides status per rule
diff --git a/pkg/dclient/utils.go b/pkg/dclient/utils.go
index 1da07b232e..8fda0e3010 100644
--- a/pkg/dclient/utils.go
+++ b/pkg/dclient/utils.go
@@ -2,7 +2,6 @@ package client
 
 import (
 	"strings"
-	"time"
 
 	"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
 	"k8s.io/apimachinery/pkg/runtime"
@@ -21,8 +20,6 @@ const (
 	// Namespaces Namespace
 	Namespaces string = "Namespace"
 )
-const namespaceCreationMaxWaitTime time.Duration = 30 * time.Second
-const namespaceCreationWaitInterval time.Duration = 100 * time.Millisecond
 
 //NewMockClient ---testing utilities
 func NewMockClient(scheme *runtime.Scheme, objects ...runtime.Object) (*Client, error) {
diff --git a/pkg/engine/mutate/overlay.go b/pkg/engine/mutate/overlay.go
index a5bc33d942..0ffa924545 100644
--- a/pkg/engine/mutate/overlay.go
+++ b/pkg/engine/mutate/overlay.go
@@ -150,7 +150,7 @@ func mutateResourceWithOverlay(resource, pattern interface{}) ([][]byte, error)
 
 // applyOverlay detects type of current item and goes down through overlay and resource trees applying overlay
 func applyOverlay(resource, overlay interface{}, path string) ([][]byte, error) {
-	var appliedPatches [][]byte
+
 	// resource item exists but has different type - replace
 	// all subtree within this path by overlay
 	if reflect.TypeOf(resource) != reflect.TypeOf(overlay) {
@@ -159,8 +159,7 @@ func applyOverlay(resource, overlay interface{}, path string) ([][]byte, error)
 			return nil, err
 		}
 
-		appliedPatches = append(appliedPatches, patch)
-		//TODO : check if return is needed ?
+		return [][]byte{patch}, nil
 	}
 	return applyOverlayForSameTypes(resource, overlay, path)
 }
diff --git a/pkg/engine/mutation.go b/pkg/engine/mutation.go
index 0ae5fb5cdf..0764847838 100644
--- a/pkg/engine/mutation.go
+++ b/pkg/engine/mutation.go
@@ -83,7 +83,7 @@ func Mutate(policyContext PolicyContext) (resp response.EngineResponse) {
 		if rule.Mutation.Overlay != nil {
 			var ruleResponse response.RuleResponse
 			ruleResponse, patchedResource = mutate.ProcessOverlay(ctx, rule, patchedResource)
-			if ruleResponse.Success == true {
+			if ruleResponse.Success {
 				// - variable substitution path is not present
 				if ruleResponse.PathNotPresent {
 					glog.V(4).Infof(ruleResponse.Message)
diff --git a/pkg/engine/policy/validate.go b/pkg/engine/policy/validate.go
index 0011aaa900..ee151b8120 100644
--- a/pkg/engine/policy/validate.go
+++ b/pkg/engine/policy/validate.go
@@ -359,10 +359,12 @@ func validateMap(patternMap map[string]interface{}, path string, supportedAnchor
 		// check regex () -> this is anchor
 		// ()
 		// single char ()
-		matched, err := regexp.MatchString(`^.?\(.+\)$`, key)
+		re, err := regexp.Compile(`^.?\(.+\)$`)
 		if err != nil {
 			return path + "/" + key, fmt.Errorf("Unable to parse the field %s: %v", key, err)
 		}
+
+		matched := re.MatchString(key)
 		// check the type of anchor
 		if matched {
 			// some type of anchor
diff --git a/pkg/engine/validate/pattern.go b/pkg/engine/validate/pattern.go
index d7b33af248..e7b37283bd 100644
--- a/pkg/engine/validate/pattern.go
+++ b/pkg/engine/validate/pattern.go
@@ -139,7 +139,7 @@ func validateValueWithNilPattern(value interface{}) bool {
 	case string:
 		return typed == ""
 	case bool:
-		return typed == false
+		return !typed
 	case nil:
 		return true
 	case map[string]interface{}, []interface{}:
diff --git a/pkg/engine/validate/validate_test.go b/pkg/engine/validate/validate_test.go
index 4942ce7899..acabd87ac1 100644
--- a/pkg/engine/validate/validate_test.go
+++ b/pkg/engine/validate/validate_test.go
@@ -97,8 +97,8 @@ func TestValidateMap(t *testing.T) {
 	}`)
 
 	var pattern, resource map[string]interface{}
-	json.Unmarshal(rawPattern, &pattern)
-	json.Unmarshal(rawMap, &resource)
+	assert.Assert(t, json.Unmarshal(rawPattern, &pattern))
+	assert.Assert(t, json.Unmarshal(rawMap, &resource))
 
 	path, err := validateMap(resource, pattern, pattern, "/")
 	assert.Equal(t, path, "")
@@ -193,8 +193,8 @@ func TestValidateMap_AsteriskForInt(t *testing.T) {
 	`)
 
 	var pattern, resource map[string]interface{}
-	json.Unmarshal(rawPattern, &pattern)
-	json.Unmarshal(rawMap, &resource)
+	assert.Assert(t, json.Unmarshal(rawPattern, &pattern))
+	assert.Assert(t, json.Unmarshal(rawMap, &resource))
 
 	path, err := validateMap(resource, pattern, pattern, "/")
 	t.Log(path)
@@ -286,8 +286,8 @@ func TestValidateMap_AsteriskForMap(t *testing.T) {
 	}`)
 
 	var pattern, resource map[string]interface{}
-	json.Unmarshal(rawPattern, &pattern)
-	json.Unmarshal(rawMap, &resource)
+	assert.Assert(t, json.Unmarshal(rawPattern, &pattern))
+	assert.Assert(t, json.Unmarshal(rawMap, &resource))
 
 	path, err := validateMap(resource, pattern, pattern, "/")
 	assert.Equal(t, path, "")
@@ -374,8 +374,8 @@ func TestValidateMap_AsteriskForArray(t *testing.T) {
 	}`)
 
 	var pattern, resource map[string]interface{}
-	json.Unmarshal(rawPattern, &pattern)
-	json.Unmarshal(rawMap, &resource)
+	assert.Assert(t, json.Unmarshal(rawPattern, &pattern))
+	assert.Assert(t, json.Unmarshal(rawMap, &resource))
 
 	path, err := validateMap(resource, pattern, pattern, "/")
 	assert.Equal(t, path, "")
@@ -465,8 +465,8 @@ func TestValidateMap_AsteriskFieldIsMissing(t *testing.T) {
 	}`)
 
 	var pattern, resource map[string]interface{}
-	json.Unmarshal(rawPattern, &pattern)
-	json.Unmarshal(rawMap, &resource)
+	assert.Assert(t, json.Unmarshal(rawPattern, &pattern))
+	assert.Assert(t, json.Unmarshal(rawMap, &resource))
 
 	path, err := validateMap(resource, pattern, pattern, "/")
 	assert.Equal(t, path, "/spec/template/spec/containers/0/")
@@ -556,7 +556,7 @@ func TestValidateMap_livenessProbeIsNull(t *testing.T) {
 	}`)
 
 	var pattern, resource map[string]interface{}
-	json.Unmarshal(rawPattern, &pattern)
+	assert.Assert(t, json.Unmarshal(rawPattern, &pattern))
 	json.Unmarshal(rawMap, &resource)
 
 	path, err := validateMap(resource, pattern, pattern, "/")
@@ -646,8 +646,8 @@ func TestValidateMap_livenessProbeIsMissing(t *testing.T) {
 	}`)
 
 	var pattern, resource map[string]interface{}
-	json.Unmarshal(rawPattern, &pattern)
-	json.Unmarshal(rawMap, &resource)
+	assert.Assert(t, json.Unmarshal(rawPattern, &pattern))
+	assert.Assert(t, json.Unmarshal(rawMap, &resource))
 
 	path, err := validateMap(resource, pattern, pattern, "/")
 	assert.Equal(t, path, "")
@@ -692,8 +692,8 @@ func TestValidateMapElement_TwoElementsInArrayOnePass(t *testing.T) {
 	  }`)
 
 	var pattern, resource interface{}
-	json.Unmarshal(rawPattern, &pattern)
-	json.Unmarshal(rawMap, &resource)
+	assert.Assert(t, json.Unmarshal(rawPattern, &pattern))
+	assert.Assert(t, json.Unmarshal(rawMap, &resource))
 
 	path, err := validateResourceElement(resource, pattern, pattern, "/")
 	assert.Equal(t, path, "")
@@ -727,8 +727,8 @@ func TestValidateMapElement_OneElementInArrayPass(t *testing.T) {
 	]`)
 
 	var pattern, resource interface{}
-	json.Unmarshal(rawPattern, &pattern)
-	json.Unmarshal(rawMap, &resource)
+	assert.Assert(t, json.Unmarshal(rawPattern, &pattern))
+	assert.Assert(t, json.Unmarshal(rawMap, &resource))
 
 	path, err := validateResourceElement(resource, pattern, pattern, "/")
 	assert.Equal(t, path, "")
@@ -781,8 +781,8 @@ func TestValidateMap_CorrectRelativePathInConfig(t *testing.T) {
 	}`)
 
 	var pattern, resource interface{}
-	json.Unmarshal(rawPattern, &pattern)
-	json.Unmarshal(rawMap, &resource)
+	assert.Assert(t, json.Unmarshal(rawPattern, &pattern))
+	assert.Assert(t, json.Unmarshal(rawMap, &resource))
 
 	path, err := validateResourceElement(resource, pattern, pattern, "/")
 	assert.Equal(t, path, "")
@@ -835,8 +835,8 @@ func TestValidateMap_RelativePathDoesNotExists(t *testing.T) {
 	}`)
 
 	var pattern, resource interface{}
-	json.Unmarshal(rawPattern, &pattern)
-	json.Unmarshal(rawMap, &resource)
+	assert.Assert(t, json.Unmarshal(rawPattern, &pattern))
+	assert.Assert(t, json.Unmarshal(rawMap, &resource))
 
 	path, err := validateResourceElement(resource, pattern, pattern, "/")
 	assert.Equal(t, path, "/spec/containers/0/resources/requests/memory/")
@@ -889,8 +889,8 @@ func TestValidateMap_OnlyAnchorsInPath(t *testing.T) {
 	}`)
 
 	var pattern, resource interface{}
-	json.Unmarshal(rawPattern, &pattern)
-	json.Unmarshal(rawMap, &resource)
+	assert.Assert(t, json.Unmarshal(rawPattern, &pattern))
+	assert.Assert(t, json.Unmarshal(rawMap, &resource))
 
 	path, err := validateResourceElement(resource, pattern, pattern, "/")
 	assert.Equal(t, path, "/spec/containers/0/resources/requests/memory/")
@@ -943,8 +943,8 @@ func TestValidateMap_MalformedReferenceOnlyDolarMark(t *testing.T) {
 	}`)
 
 	var pattern, resource interface{}
-	json.Unmarshal(rawPattern, &pattern)
-	json.Unmarshal(rawMap, &resource)
+	assert.Assert(t, json.Unmarshal(rawPattern, &pattern))
+	assert.Assert(t, json.Unmarshal(rawMap, &resource))
 
 	path, err := validateResourceElement(resource, pattern, pattern, "/")
 	assert.Equal(t, path, "/spec/containers/0/resources/requests/memory/")
@@ -997,8 +997,8 @@ func TestValidateMap_RelativePathWithParentheses(t *testing.T) {
 	}`)
 
 	var pattern, resource interface{}
-	json.Unmarshal(rawPattern, &pattern)
-	json.Unmarshal(rawMap, &resource)
+	assert.Assert(t, json.Unmarshal(rawPattern, &pattern))
+	assert.Assert(t, json.Unmarshal(rawMap, &resource))
 
 	path, err := validateResourceElement(resource, pattern, pattern, "/")
 	assert.Equal(t, path, "")
@@ -1051,8 +1051,8 @@ func TestValidateMap_MalformedPath(t *testing.T) {
 	}`)
 
 	var pattern, resource interface{}
-	json.Unmarshal(rawPattern, &pattern)
-	json.Unmarshal(rawMap, &resource)
+	assert.Assert(t, json.Unmarshal(rawPattern, &pattern))
+	assert.Assert(t, json.Unmarshal(rawMap, &resource))
 
 	path, err := validateResourceElement(resource, pattern, pattern, "/")
 	assert.Equal(t, path, "/spec/containers/0/resources/requests/memory/")
@@ -1105,8 +1105,8 @@ func TestValidateMap_AbosolutePathExists(t *testing.T) {
 	}`)
 
 	var pattern, resource interface{}
-	json.Unmarshal(rawPattern, &pattern)
-	json.Unmarshal(rawMap, &resource)
+	assert.Assert(t, json.Unmarshal(rawPattern, &pattern))
+	assert.Assert(t, json.Unmarshal(rawMap, &resource))
 
 	path, err := validateResourceElement(resource, pattern, pattern, "/")
 	assert.Equal(t, path, "")
@@ -1146,8 +1146,8 @@ func TestValidateMap_AbsolutePathToMetadata(t *testing.T) {
 	}`)
 
 	var pattern, resource interface{}
-	json.Unmarshal(rawPattern, &pattern)
-	json.Unmarshal(rawMap, &resource)
+	assert.Assert(t, json.Unmarshal(rawPattern, &pattern))
+	assert.Assert(t, json.Unmarshal(rawMap, &resource))
 
 	path, err := validateResourceElement(resource, pattern, pattern, "/")
 	assert.Equal(t, path, "")
@@ -1188,8 +1188,8 @@ func TestValidateMap_AbsolutePathToMetadata_fail(t *testing.T) {
 	}`)
 
 	var pattern, resource interface{}
-	json.Unmarshal(rawPattern, &pattern)
-	json.Unmarshal(rawMap, &resource)
+	assert.Assert(t, json.Unmarshal(rawPattern, &pattern))
+	assert.Assert(t, json.Unmarshal(rawMap, &resource))
 
 	path, err := validateResourceElement(resource, pattern, pattern, "/")
 	assert.Equal(t, path, "/spec/containers/0/image/")
@@ -1242,8 +1242,8 @@ func TestValidateMap_AbosolutePathDoesNotExists(t *testing.T) {
 	}`)
 
 	var pattern, resource interface{}
-	json.Unmarshal(rawPattern, &pattern)
-	json.Unmarshal(rawMap, &resource)
+	assert.Assert(t, json.Unmarshal(rawPattern, &pattern))
+	assert.Assert(t, json.Unmarshal(rawMap, &resource))
 
 	path, err := validateResourceElement(resource, pattern, pattern, "/")
 	assert.Equal(t, path, "/spec/containers/0/resources/requests/memory/")
@@ -1274,7 +1274,7 @@ func TestActualizePattern_GivenRelativePathThatExists(t *testing.T) {
 
 	var pattern interface{}
 
-	json.Unmarshal(rawPattern, &pattern)
+	assert.Assert(t, json.Unmarshal(rawPattern, &pattern))
 
 	pattern, err := actualizePattern(pattern, referencePath, absolutePath)
 
diff --git a/pkg/namespace/generation.go b/pkg/namespace/generation.go
index a5441fd446..f5a96fa245 100644
--- a/pkg/namespace/generation.go
+++ b/pkg/namespace/generation.go
@@ -66,7 +66,7 @@ func (rm *ResourceManager) ProcessResource(policy, pv, kind, ns, name, rv string
 
 	key := buildKey(policy, pv, kind, ns, name, rv)
 	_, ok := rm.data[key]
-	return ok == false
+	return !ok
 }
 
 //Drop drop the cache after every rebuild interval mins
diff --git a/pkg/policy/existing.go b/pkg/policy/existing.go
index f8e91bafee..9165978ca4 100644
--- a/pkg/policy/existing.go
+++ b/pkg/policy/existing.go
@@ -320,7 +320,7 @@ func (rm *ResourceManager) ProcessResource(policy, pv, kind, ns, name, rv string
 
 	key := buildKey(policy, pv, kind, ns, name, rv)
 	_, ok := rm.data[key]
-	return ok == false
+	return !ok
 }
 
 func buildKey(policy, pv, kind, ns, name, rv string) string {