1
0
Fork 0
mirror of https://github.com/kyverno/kyverno.git synced 2025-03-31 03:45:17 +00:00

handles array parsing in configmap value

This commit is contained in:
Shuting Zhao 2020-09-22 18:26:52 -07:00
parent bd406f5bb8
commit b8b1d81df0
5 changed files with 23 additions and 14 deletions

View file

@ -29,7 +29,6 @@ func generatePatches(src, dst []byte) ([][]byte, error) {
}
patchesBytes = append(patchesBytes, pbytes)
// fmt.Printf("generated patch %s\n", p)
}
return patchesBytes, err

View file

@ -4,14 +4,15 @@ import (
"encoding/json"
"errors"
"fmt"
"reflect"
"strings"
"time"
"github.com/go-logr/logr"
"github.com/nirmata/kyverno/pkg/utils"
authenticationv1 "k8s.io/api/authentication/v1"
rbacv1 "k8s.io/api/rbac/v1"
"reflect"
"sigs.k8s.io/controller-runtime/pkg/log"
"strings"
"time"
"github.com/minio/minio/pkg/wildcard"
kyverno "github.com/nirmata/kyverno/pkg/api/kyverno/v1"

View file

@ -1,9 +1,9 @@
package operator
import (
"encoding/json"
"fmt"
"reflect"
"strings"
"github.com/go-logr/logr"
"github.com/nirmata/kyverno/pkg/engine/context"
@ -48,7 +48,7 @@ func (in InHandler) Evaluate(key, value interface{}) bool {
}
func (in InHandler) validateValuewithStringPattern(key string, value interface{}) (keyExists bool) {
invalidType, keyExists := ValidateStringPattern(key, value)
invalidType, keyExists := ValidateStringPattern(key, value, in.log)
if invalidType {
in.log.Info("expected type []string", "value", value, "type", fmt.Sprintf("%T", value))
return false
@ -57,7 +57,7 @@ func (in InHandler) validateValuewithStringPattern(key string, value interface{}
return keyExists
}
func ValidateStringPattern(key string, value interface{}) (invalidType bool, keyExists bool) {
func ValidateStringPattern(key string, value interface{}, log logr.Logger) (invalidType bool, keyExists bool) {
stringType := reflect.TypeOf("")
switch valuesAvaliable := value.(type) {
case []interface{}:
@ -69,10 +69,19 @@ func ValidateStringPattern(key string, value interface{}) (invalidType bool, key
keyExists = true
}
}
// add to handle the configMap lookup, as configmap.data
// takes string-string map, when looking for a value of array
// data:
// key: "[\"value1\", \"value2\"]"
// it will first unmarshal it to string slice, then compare
case string:
valuesAvaliable = strings.TrimSpace(valuesAvaliable)
vars := strings.Split(valuesAvaliable, ",")
for _, val := range vars {
var arr []string
if err := json.Unmarshal([]byte(valuesAvaliable), &arr); err != nil {
log.Error(err, "failed to unmarshal to string slice", "value", value)
return invalidType, keyExists
}
for _, val := range arr {
if key == val {
keyExists = true
}

View file

@ -47,7 +47,7 @@ func (nin NotInHandler) Evaluate(key, value interface{}) bool {
}
func (nin NotInHandler) validateValuewithStringPattern(key string, value interface{}) bool {
invalidType, keyExists := ValidateStringPattern(key, value)
invalidType, keyExists := ValidateStringPattern(key, value, nin.log)
if invalidType {
nin.log.Info("expected type []string", "value", value, "type", fmt.Sprintf("%T", value))
return false

View file

@ -1,14 +1,15 @@
package generate
import (
"os"
"time"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/client-go/dynamic"
"k8s.io/client-go/tools/clientcmd"
"os"
"sigs.k8s.io/yaml"
"time"
)
type E2EClient struct {
@ -103,7 +104,6 @@ func (e2e *E2EClient) ListNamespacedResources(gvr schema.GroupVersionResource, n
func (e2e *E2EClient) CreateNamespacedResourceYaml(gvr schema.GroupVersionResource, namespace string, resourceData []byte) (*unstructured.Unstructured, error) {
resource := unstructured.Unstructured{}
err := yaml.Unmarshal(resourceData, &resource)
// fmt.Println(resource)
if err != nil {
return nil, err
}