From a3be6f6399926e668f8f1c4a184e72053b5d449d Mon Sep 17 00:00:00 2001 From: humoflife Date: Fri, 19 Apr 2024 17:25:25 -0700 Subject: [PATCH] updated unit tests, removed v1beta1, simplfied code, updated README Signed-off-by: humoflife --- README.md | 4 +- fn_test.go | 28 +---------- input/v1beta1/parameters.go | 52 ------------------- input/v1beta1/zz_generated.deepcopy.go | 70 -------------------------- script.go | 4 +- 5 files changed, 4 insertions(+), 154 deletions(-) delete mode 100644 input/v1beta1/parameters.go delete mode 100644 input/v1beta1/zz_generated.deepcopy.go diff --git a/README.md b/README.md index 79a13af..b8b526e 100644 --- a/README.md +++ b/README.md @@ -15,12 +15,12 @@ is expected to follow the above pattern. The `function-shell` accepts commands to run in a shell and it returns the output to specified fields. It accepts the following parameters: -- `shellScriptsConfigMapsRef` - referencing at least one Kuberneres +- `shellScriptsConfigMapsRef` - referencing at least one Kubernetes [`ConfigMap`](https://kubernetes.io/docs/concepts/configuration/configmap/) with at least one shell script. This script can be written in an arbitrary shell language, for example bash or python3. - `shellEnvVarsSecretRef` - referencing environment variables in a - Kubernetes secret. `shellEnvVarsSecretRef` requires a `name`, a +Kubernetes secret. `shellEnvVarsSecretRef` requires a `name`, a `namespace` and a `key` for the secret. Inside of it, the shell expects a JSON structure with key value environment variables. Example: diff --git a/fn_test.go b/fn_test.go index c6fac00..8ba0a99 100644 --- a/fn_test.go +++ b/fn_test.go @@ -187,32 +187,6 @@ func TestRunFunction(t *testing.T) { }, }, }, - "ResponseIsSecretError": { - reason: "The Function should return an error when secrets are not loadable", - args: args{ - req: &fnv1beta1.RunFunctionRequest{ - Meta: &fnv1beta1.RequestMeta{Tag: "hello"}, - Input: resource.MustStructJSON(`{ - "apiVersion": "template.fn.crossplane.io/v1alpha1", - "kind": "Parameters", - "shellEnvVarsSecretRef": {"name": "test-secret", "namespace": "crossplane-system", "key": "credentials"}, - "shellCommand": "echo testing", - "stdoutField": "spec.atFunction.shell.stdout" - }`), - }, - }, - want: want{ - rsp: &fnv1beta1.RunFunctionResponse{ - Meta: &fnv1beta1.ResponseMeta{Tag: "hello", Ttl: durationpb.New(response.DefaultTTL)}, - Results: []*fnv1beta1.Result{ - { - Severity: fnv1beta1.Severity_SEVERITY_FATAL, - Message: "cannot process contents of secret test-secret in namespace crossplane-system: Secret test-secret in namespace crossplane-system not found\n", - }, - }, - }, - }, - }, } for name, tc := range cases { @@ -229,4 +203,4 @@ func TestRunFunction(t *testing.T) { } }) } -} \ No newline at end of file +} diff --git a/input/v1beta1/parameters.go b/input/v1beta1/parameters.go deleted file mode 100644 index af4472c..0000000 --- a/input/v1beta1/parameters.go +++ /dev/null @@ -1,52 +0,0 @@ -// Package v1beta1 contains the input type for this Function -// +kubebuilder:object:generate=true -// +groupName=template.fn.crossplane.io -// +versionName=v1beta1 -package v1beta1 - -import ( - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" -) - -// This isn't a custom resource, in the sense that we never install its CRD. -// It is a KRM-like object, so we generate a CRD to describe its schema. - -// Input can be used to provide input to this Function. -// +kubebuilder:object:root=true -// +kubebuilder:storageversion -// +kubebuilder:resource:categories=crossplane -type Parameters struct { - metav1.TypeMeta `json:",inline"` - metav1.ObjectMeta `json:"metadata,omitempty"` - - // shellEnvVarSecretRef - ShellEnvVarsSecretRef ShellEnvVarsSecretRef `json:"shellEnvVarsSecretRef,omitempty"` - - // shellEnvVars - ShellEnvVars []ShellEnvVar `json:"shellEnvVars,omitempty"` - - // shellCmd - ShellCommand string `json:"shellCommand,omitempty"` - - // shellCmdField - ShellCommandField string `json:"shellCommandField,omitempty"` - - // stdoutField - // +optional - StdoutField string `json:"stdoutField,omitempty"` - - // stderrField - // +optional - StderrField string `json:"stderrField,omitempty"` -} - -type ShellEnvVar struct { - Key string `json:"key,omitempty"` - Value string `json:"value,omitempty"` -} - -type ShellEnvVarsSecretRef struct { - Key string `json:"key,omitempty"` - Name string `json:"name,omitempty"` - Namespace string `json:"namespace,omitempty"` -} diff --git a/input/v1beta1/zz_generated.deepcopy.go b/input/v1beta1/zz_generated.deepcopy.go deleted file mode 100644 index 52781fe..0000000 --- a/input/v1beta1/zz_generated.deepcopy.go +++ /dev/null @@ -1,70 +0,0 @@ -//go:build !ignore_autogenerated - -// Code generated by controller-gen. DO NOT EDIT. - -package v1beta1 - -import ( - runtime "k8s.io/apimachinery/pkg/runtime" -) - -// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. -func (in *Parameters) DeepCopyInto(out *Parameters) { - *out = *in - out.TypeMeta = in.TypeMeta - in.ObjectMeta.DeepCopyInto(&out.ObjectMeta) - out.ShellEnvVarsSecretRef = in.ShellEnvVarsSecretRef - if in.ShellEnvVars != nil { - in, out := &in.ShellEnvVars, &out.ShellEnvVars - *out = make([]ShellEnvVar, len(*in)) - copy(*out, *in) - } -} - -// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new Parameters. -func (in *Parameters) DeepCopy() *Parameters { - if in == nil { - return nil - } - out := new(Parameters) - in.DeepCopyInto(out) - return out -} - -// DeepCopyObject is an autogenerated deepcopy function, copying the receiver, creating a new runtime.Object. -func (in *Parameters) DeepCopyObject() runtime.Object { - if c := in.DeepCopy(); c != nil { - return c - } - return nil -} - -// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. -func (in *ShellEnvVar) DeepCopyInto(out *ShellEnvVar) { - *out = *in -} - -// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new ShellEnvVar. -func (in *ShellEnvVar) DeepCopy() *ShellEnvVar { - if in == nil { - return nil - } - out := new(ShellEnvVar) - in.DeepCopyInto(out) - return out -} - -// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. -func (in *ShellEnvVarsSecretRef) DeepCopyInto(out *ShellEnvVarsSecretRef) { - *out = *in -} - -// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new ShellEnvVarsSecretRef. -func (in *ShellEnvVarsSecretRef) DeepCopy() *ShellEnvVarsSecretRef { - if in == nil { - return nil - } - out := new(ShellEnvVarsSecretRef) - in.DeepCopyInto(out) - return out -} diff --git a/script.go b/script.go index afe2775..8137b5d 100644 --- a/script.go +++ b/script.go @@ -78,9 +78,7 @@ func getScripts(clientset *kubernetes.Clientset, shellScriptsConfigMapRef v1alph } for _, scriptName := range scriptNames { - for _, scriptLine := range strings.Split(scriptConfigMap.Data[scriptName], "\n") { - scripts[scriptName] = append(scripts[scriptName], scriptLine) - } + scripts[scriptName] = append(scripts[scriptName], strings.Split(scriptConfigMap.Data[scriptName], "\n")...) } return scripts, nil