Skip to content

Commit

Permalink
fix: jpKubernetesExists not setting apiVersion/kind
Browse files Browse the repository at this point in the history
Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com>
  • Loading branch information
eddycharly committed Mar 1, 2024
1 parent a3a33a9 commit 85bff79
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 3 deletions.
5 changes: 4 additions & 1 deletion pkg/runner/functions/k8s.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,10 @@ func jpKubernetesExists(arguments []any) (any, error) {
if err := getArg(arguments, 4, &key.Name); err != nil {
return nil, err
}
err := client.Get(context.TODO(), key, &unstructured.Unstructured{})
var obj unstructured.Unstructured
obj.SetAPIVersion(apiVersion)
obj.SetKind(kind)
err := client.Get(context.TODO(), key, &obj)
if err == nil {
return true, nil
}
Expand Down
85 changes: 83 additions & 2 deletions pkg/runner/functions/k8s_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@ import (
)

func Test_jpKubernetesResourceExists(t *testing.T) {
config, _ := restutils.DefaultConfig(clientcmd.ConfigOverrides{})
client, _ := client.New(config)
config, err := restutils.DefaultConfig(clientcmd.ConfigOverrides{})
assert.NoError(t, err)
client, err := client.New(config)
assert.NoError(t, err)
tests := []struct {
name string
args []any
Expand Down Expand Up @@ -57,3 +59,82 @@ func Test_jpKubernetesResourceExists(t *testing.T) {
})
}
}

func Test_jpKubernetesExists(t *testing.T) {
config, err := restutils.DefaultConfig(clientcmd.ConfigOverrides{})
assert.NoError(t, err)
client, err := client.New(config)
assert.NoError(t, err)
tests := []struct {
name string
args []any
want any
wantErr bool
}{{
name: "exist",
args: []any{
client,
"v1",
"Pod",
"kube-system",
"kube-apiserver-kind-control-plane",
},
want: true,
wantErr: false,
}, {
name: "namespace not exists",
args: []any{
client,
"v1",
"Pod",
"foo",
"kube-apiserver-kind-control-plane",
},
want: false,
wantErr: false,
}, {
name: "name not exists",
args: []any{
client,
"v1",
"Pod",
"kube-system",
"foo",
},
want: false,
wantErr: false,
}, {
name: "kind not exists",
args: []any{
client,
"v1",
"Foo",
"kube-system",
"foo",
},
want: nil,
wantErr: true,
}, {
name: "apiVersion not exists",
args: []any{
client,
"v1alpha1",
"Pod",
"kube-system",
"kube-apiserver-kind-control-plane",
},
want: nil,
wantErr: true,
}}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := jpKubernetesExists(tt.args)
assert.Equal(t, tt.want, got)
if tt.wantErr {
assert.Error(t, err)
} else {
assert.NoError(t, err)
}
})
}
}

0 comments on commit 85bff79

Please sign in to comment.