Skip to content

Commit

Permalink
feat: add resource rules to krm-kcl spec
Browse files Browse the repository at this point in the history
Signed-off-by: shruti2522 <shruti.apc01@gmail.com>

add resource matching rules to config.go

Signed-off-by: shruti2522 <shruti.apc01@gmail.com>

fix build and test errors

Signed-off-by: shruti2522 <shruti.apc01@gmail.com>

add test case for missing resource rules

Signed-off-by: shruti2522 <shruti.apc01@gmail.com>

updated yaml test files

Signed-off-by: shruti2522 <shruti.apc01@gmail.com>

test case set to optional

Signed-off-by: shruti2522 <shruti.apc01@gmail.com>

corrected syntax error in yaml

Signed-off-by: shruti2522 <shruti.apc01@gmail.com>

fixed yaml formatting in config_test.go

Signed-off-by: shruti2522 <shruti.apc01@gmail.com>

fixed yaml formatting

Signed-off-by: shruti2522 <shruti.apc01@gmail.com>

updated logic handling for MatchResourceRules

Signed-off-by: shruti2522 <shruti.apc01@gmail.com>

feat: add resource rules to krm-kcl spec

Signed-off-by: shruti2522 <shruti.apc01@gmail.com>
  • Loading branch information
shruti2522 committed May 2, 2024
1 parent 88821b6 commit 7f3681b
Show file tree
Hide file tree
Showing 11 changed files with 107 additions and 1 deletion.
3 changes: 3 additions & 0 deletions pkg/api/v1alpha1/kclrun_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,7 @@ const (

// ParamsKey is the key for the params field in the KCLRun resource.
ParamsKey = "params"

// MatchConstraintsKey is the key for the match constraints field in the KCLRun resource.
MatchConstraintsKey = "matchConstraints"
)
50 changes: 49 additions & 1 deletion pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,23 @@ type KCLRun struct {
Source string `json:"source" yaml:"source"`
// Params are the parameters in key-value pairs format.
Params map[string]interface{} `json:"params,omitempty" yaml:"params,omitempty"`
// MatchConstraints defines the resource matching rules.
MatchConstraints MatchConstraints `json:"matchConstraints,omitempty" yaml:"matchConstraints,omitempty"`
} `json:"spec" yaml:"spec"`
}

// MatchConstraints defines the resource matching rules.
type MatchConstraints struct {
ResourceRules []ResourceRule `json:"resourceRules,omitempty" yaml:"resourceRules,omitempty"`
}

// ResourceRule defines a rule for matching resources.
type ResourceRule struct {
APIGroups []string `json:"apiGroups,omitempty" yaml:"apiGroups,omitempty"`
APIVersions []string `json:"apiVersions,omitempty" yaml:"apiVersions,omitempty"`
Resources []string `json:"resources,omitempty" yaml:"resources,omitempty"`
}

// Config is used to configure the KCLRun instance based on the given FunctionConfig.
// It converts ConfigMap to KCLRun or assigns values directly from KCLRun.
// If an error occurs during the configuration process, an error message will be returned.
Expand Down Expand Up @@ -123,19 +137,43 @@ func (r *KCLRun) Transform(rl *fn.ResourceList) error {
if err != nil {
return err
}
transformedObjects = append(transformedObjects, obj)

// Check if the transformed object matches the resource rules
if r.MatchResourceRules(obj) {
transformedObjects = append(transformedObjects, obj)
}
}
rl.Items = transformedObjects
return nil
}

// MatchResourceRules checks if the given Kubernetes object matches the resource rules specified in KCLRun.
func (r *KCLRun) MatchResourceRules(obj *fn.KubeObject) bool {
// if MatchConstraints.ResourceRules is not set (nil or empty), return true by default
if r.Spec.MatchConstraints.ResourceRules == nil || len(r.Spec.MatchConstraints.ResourceRules) == 0 {
return true
}
// iterate through each resource rule
for _, rule := range r.Spec.MatchConstraints.ResourceRules {
if containsString(rule.APIGroups, obj.GroupKind().Group) &&
containsString(rule.APIVersions, obj.GetAPIVersion()) &&
containsString(rule.Resources, obj.GetKind()) {
return true
}
}
// if no match is found, return false
return false
}

// DealAnnotations handles annotations, e.g., allow-insecure-source.
func (r *KCLRun) DealAnnotations() {
// Deal the allow-insecure-source annotation
if v, ok := r.ObjectMeta.Annotations[AnnotationAllowInSecureSource]; ok && isOk(v) {
os.Setenv(settings.DEFAULT_OCI_PLAIN_HTTP_ENV, settings.ON)
}
}

// isOk checks if a given string is in the list of "OK" values.
func isOk(value string) bool {
okValues := []string{"ok", "yes", "true", "1", "on"}
for _, v := range okValues {
Expand All @@ -145,3 +183,13 @@ func isOk(value string) bool {
}
return false
}

// containsString checks if a slice contains a string.
func containsString(slice []string, str string) bool {
for _, s := range slice {
if s == str {
return true
}
}
return false
}
15 changes: 15 additions & 0 deletions pkg/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ metadata:
spec:
source: |
[item | {metadata.namespace = "baz"} for item in option("resource_list")]
matchConstraints:
resourceRules:
`,
},
{
Expand All @@ -34,6 +36,19 @@ metadata:
`,
expectErrMsg: "`source` must not be empty",
},
{
name: "KCLRun missing matchConstraints",
config: `apiVersion: krm.kcl.dev/v1alpha1
kind: KCLRun
metadata:
name: my-kcl-fn
namespace: foo
spec:
source: |
[item | {metadata.namespace = "baz"} for item in option("resource_list")]
`,
expectErrMsg: "",
},
{
name: "valid ConfigMap",
config: `apiVersion: v1
Expand Down
5 changes: 5 additions & 0 deletions pkg/options/testdata/resource_list/kcl-run-code.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,8 @@ functionConfig:
spec:
source: |
[resource | {if resource.kind == "Deployment": metadata.annotations: {"managed-by" = "krm-kcl"}} for resource in option("resource_list").items]
matchConstraints:
resourceRules:
- apiGroups: ["apps"]
apiVersions: ["v1"]
resources: ["deployments"]
5 changes: 5 additions & 0 deletions pkg/options/testdata/resource_list/kcl-run-git.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,8 @@ functionConfig:
annotations:
config.kubernetes.io/local-config: "true"
source: github.com/kcl-lang/krm-kcl/tests/mutation/set-annotations/
matchConstraints:
resourceRules:
- apiGroups: ["apps"]
apiVersions: ["v1"]
resources: ["deployments"]
5 changes: 5 additions & 0 deletions pkg/options/testdata/resource_list/kcl-run-https.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,8 @@ functionConfig:
annotations:
config.kubernetes.io/local-config: "true"
source: https://raw.githubusercontent.com/kcl-lang/krm-kcl/main/tests/mutation/set-annotations/main.k
matchConstraints:
resourceRules:
- apiGroups: ["apps"]
apiVersions: ["v1"]
resources: ["deployments"]
5 changes: 5 additions & 0 deletions pkg/options/testdata/resource_list/kcl-run-oci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,8 @@ functionConfig:
annotations:
config.kubernetes.io/local-config: "true"
source: oci://ghcr.io/kcl-lang/set-annotation
matchConstraints:
resourceRules:
- apiGroups: ["apps"]
apiVersions: ["v1"]
resources: ["deployments"]
5 changes: 5 additions & 0 deletions pkg/options/testdata/yaml_stream/kcl-run-code.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,8 @@ metadata:
spec:
source: |
[resource | {if resource.kind == "Deployment": metadata.annotations: {"managed-by" = "krm-kcl"}} for resource in option("resource_list").items]
matchConstraints:
resourceRules:
- apiGroups: ["apps"]
apiVersions: ["v1"]
resources: ["deployments"]
5 changes: 5 additions & 0 deletions pkg/options/testdata/yaml_stream/kcl-run-git.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,8 @@ spec:
annotations:
config.kubernetes.io/local-config: "true"
source: github.com/kcl-lang/krm-kcl/tests/mutation/set-annotations/
matchConstraints:
resourceRules:
- apiGroups: ["apps"]
apiVersions: ["v1"]
resources: ["deployments"]
5 changes: 5 additions & 0 deletions pkg/options/testdata/yaml_stream/kcl-run-https.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,8 @@ spec:
annotations:
config.kubernetes.io/local-config: "true"
source: https://raw.githubusercontent.com/kcl-lang/krm-kcl/main/tests/mutation/set-annotations/main.k
matchConstraints:
resourceRules:
- apiGroups: ["apps"]
apiVersions: ["v1"]
resources: ["deployments"]
5 changes: 5 additions & 0 deletions pkg/options/testdata/yaml_stream/kcl-run-oci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,8 @@ spec:
annotations:
config.kubernetes.io/local-config: "true"
source: oci://ghcr.io/kcl-lang/set-annotation
matchConstraints:
resourceRules:
- apiGroups: ["apps"]
apiVersions: ["v1"]
resources: ["deployments"]

0 comments on commit 7f3681b

Please sign in to comment.