Skip to content

Commit

Permalink
fix(k8s): Fix label selector logic bug where regular label is ignored. (
Browse files Browse the repository at this point in the history
#461)

* fix(k8s): Fix label selector logic bug where regular label is ignored.

Signed-off-by: Simar <simar@linux.com>

* chore(deps): Bump deps.

Signed-off-by: Simar <simar@linux.com>

Signed-off-by: Simar <simar@linux.com>
  • Loading branch information
simar7 committed Sep 14, 2022
1 parent c1b75f0 commit d9e5865
Show file tree
Hide file tree
Showing 6 changed files with 226 additions and 1,970 deletions.
1 change: 1 addition & 0 deletions actions/kubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ func jsonOrString(input map[string]string, filter string) string {
func (k KubernetesClient) prepareInputs(input map[string]string) (string, map[string]map[string]string) {
retAction := make(map[string]map[string]string)
var retLabelSelector string
retLabelSelector = k.KubeLabelSelector

if strings.Contains(k.KubeLabelSelector, regoInputPrefix) {
retLabelSelector = jsonOrString(input, strings.TrimPrefix(k.KubeLabelSelector, regoInputPrefix+"."))
Expand Down
313 changes: 162 additions & 151 deletions actions/kubernetes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,172 +15,183 @@ import (
k8stesting "k8s.io/client-go/testing"
)

func TestKubernetesClient_Send(t *testing.T) {
t.Run("labels", func(t *testing.T) {
testCases := []struct {
name string
inputEvent string
reactorFunc func(k8stesting.Action) (bool, runtime.Object, error)
inputActions map[string]map[string]string
inputLabelSelector string
expectedLabels map[string]string
}{
{
name: "happy path, labels are added",
inputEvent: `{"SigMetadata":{"ID":"TRC-2"}}`,
inputActions: map[string]map[string]string{
"labels": {"foo": "bar"},
},
inputLabelSelector: "app=nginx",
expectedLabels: map[string]string{
"app": "nginx",
"foo": "bar",
},
func TestKubernetesClientSend_Labels(t *testing.T) {
testCases := []struct {
name string
inputEvent string
reactorFunc func(k8stesting.Action) (bool, runtime.Object, error)
inputActions map[string]map[string]string
inputLabelSelector string
expectedLabels map[string]string
}{
{
name: "happy path, labels are added",
inputEvent: `{"SigMetadata":{"ID":"TRC-2"}}`,
inputActions: map[string]map[string]string{
"labels": {"foo": "bar"},
},
{
name: "happy path, relative label selector and labels are added",
inputEvent: `{"SigMetadata":{"ID":"TRC-2", "Hostname":"nginx"}}`,
inputActions: map[string]map[string]string{
"labels": {"foo": "bar"},
},
inputLabelSelector: "app=event.input.SigMetadata.Hostname",
expectedLabels: map[string]string{
"app": "nginx",
"foo": "bar",
},
inputLabelSelector: "app=nginx",
expectedLabels: map[string]string{
"app": "nginx",
"foo": "bar",
},
{
name: "happy path, json input event, relative input labels are added",
inputEvent: `{"SigMetadata":{"ID":"TRC-2", "Hostname":"foo.com"}}`,
inputActions: map[string]map[string]string{
"labels": {
"foo": "event.input.SigMetadata.ID",
"hostname": "event.input.SigMetadata.Hostname",
},
},
inputLabelSelector: "app=nginx",
expectedLabels: map[string]string{
"app": "nginx",
"foo": "TRC-2",
"hostname": "foo.com",
},
},
{
name: "happy path, relative label selector and labels are added",
inputEvent: `{"SigMetadata":{"ID":"TRC-2", "Hostname":"nginx"}}`,
inputActions: map[string]map[string]string{
"labels": {"foo": "bar"},
},
{
name: "happy path, string input event, relative input labels are added",
inputEvent: `foo bar baz`,
inputActions: map[string]map[string]string{
"labels": {"foo": "event.input"},
},
inputLabelSelector: "app=nginx",
expectedLabels: map[string]string{
"app": "nginx",
"foo": "foo bar baz",
},
inputLabelSelector: "app=event.input.SigMetadata.Hostname",
expectedLabels: map[string]string{
"app": "nginx",
"foo": "bar",
},
{
name: "sad path, unable to add label",
inputEvent: `{"SigMetadata":{"ID":"TRC-2"}}`,
inputLabelSelector: "app=nginx",
reactorFunc: func(action k8stesting.Action) (handled bool, ret runtime.Object, err error) {
return true, nil, fmt.Errorf("failed to update label")
},
expectedLabels: map[string]string{
"app": "nginx",
},
{
name: "happy path, json input event, relative input labels are added",
inputEvent: `{"SigMetadata":{"ID":"TRC-2", "Hostname":"foo.com"}}`,
inputActions: map[string]map[string]string{
"labels": {
"foo": "event.input.SigMetadata.ID",
"hostname": "event.input.SigMetadata.Hostname",
},
},
}
inputLabelSelector: "app=nginx",
expectedLabels: map[string]string{
"app": "nginx",
"foo": "TRC-2",
"hostname": "foo.com",
},
},
{
name: "happy path, string input event, relative input labels are added",
inputEvent: `foo bar baz`,
inputActions: map[string]map[string]string{
"labels": {"foo": "event.input"},
},
inputLabelSelector: "app=nginx",
expectedLabels: map[string]string{
"app": "nginx",
"foo": "foo bar baz",
},
},
{
name: "sad path, unable to add label",
inputEvent: `{"SigMetadata":{"ID":"TRC-2"}}`,
inputLabelSelector: "app=nginx",
reactorFunc: func(action k8stesting.Action) (handled bool, ret runtime.Object, err error) {
return true, nil, fmt.Errorf("failed to update label")
},
expectedLabels: map[string]string{
"app": "nginx",
},
},
{
name: "sad path, no matching label selector and no labels are added",
inputEvent: `{"SigMetadata":{"ID":"TRC-2"}}`,
inputActions: map[string]map[string]string{
"labels": {"foo": "bar"},
},
inputLabelSelector: "app=doesntexist",
expectedLabels: map[string]string{
"app": "nginx",
},
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
k := KubernetesClient{
clientset: fake.NewSimpleClientset(),
KubeNamespace: "testing",
KubeActions: tc.inputActions,
KubeLabelSelector: tc.inputLabelSelector,
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
k := KubernetesClient{
clientset: fake.NewSimpleClientset(),
KubeNamespace: "testing",
KubeActions: tc.inputActions,
KubeLabelSelector: tc.inputLabelSelector,
}

if tc.reactorFunc != nil {
k.clientset.CoreV1().(*fake2.FakeCoreV1).Fake.PrependReactor("update", "pods", func(action k8stesting.Action) (handled bool, ret runtime.Object, err error) {
return true, nil, fmt.Errorf("failed to update label")
})
}
if tc.reactorFunc != nil {
k.clientset.CoreV1().(*fake2.FakeCoreV1).Fake.PrependReactor("update", "pods", func(action k8stesting.Action) (handled bool, ret runtime.Object, err error) {
return true, nil, fmt.Errorf("failed to update label")
})
}

pod := &v1.Pod{
TypeMeta: metav1.TypeMeta{
Kind: "Pod",
APIVersion: "v1",
},
ObjectMeta: metav1.ObjectMeta{
Name: "test-pod",
Namespace: "testing",
Labels: map[string]string{"app": "nginx"},
},
}
pod := &v1.Pod{
TypeMeta: metav1.TypeMeta{
Kind: "Pod",
APIVersion: "v1",
},
ObjectMeta: metav1.ObjectMeta{
Name: "test-pod",
Namespace: "testing",
Labels: map[string]string{"app": "nginx"},
},
}

_, err := k.clientset.CoreV1().Pods("testing").Create(context.TODO(), pod, metav1.CreateOptions{})
require.NoError(t, err, tc.name)
require.NoError(t, k.Send(map[string]string{"description": tc.inputEvent}), tc.name)
_, err := k.clientset.CoreV1().Pods("testing").Create(context.TODO(), pod, metav1.CreateOptions{})
require.NoError(t, err, tc.name)
require.NoError(t, k.Send(map[string]string{"description": tc.inputEvent}), tc.name)

pods, _ := k.clientset.CoreV1().Pods("testing").Get(context.TODO(), "test-pod", metav1.GetOptions{})
assert.Equal(t, tc.expectedLabels, pods.Labels, tc.name)
})
}
})
pods, _ := k.clientset.CoreV1().Pods("testing").Get(context.TODO(), "test-pod", metav1.GetOptions{})
assert.Equal(t, tc.expectedLabels, pods.Labels, tc.name)
})
}
}

t.Run("annotations", func(t *testing.T) {
testCases := []struct {
name string
inputEvent string
reactorFunc func(k8stesting.Action) (bool, runtime.Object, error)
inputActions map[string]map[string]string
expectedAnnotations map[string]string
}{
{
name: "happy path, labels are added",
inputEvent: `{"SigMetadata":{"ID":"TRC-2"}}`,
inputActions: map[string]map[string]string{
"annotations": {"foo": "bar"},
},
expectedAnnotations: map[string]string{
"app": "nginx",
"foo": "bar",
},
func TestKubernetesClientSend_Annotations(t *testing.T) {
testCases := []struct {
name string
inputEvent string
reactorFunc func(k8stesting.Action) (bool, runtime.Object, error)
inputActions map[string]map[string]string
expectedAnnotations map[string]string
}{
{
name: "happy path, labels are added",
inputEvent: `{"SigMetadata":{"ID":"TRC-2"}}`,
inputActions: map[string]map[string]string{
"annotations": {"foo": "bar"},
},
{
name: "happy path, json input event, relative input annotations are added",
inputEvent: `{"SigMetadata":{"ID":"TRC-2"}}`,
inputActions: map[string]map[string]string{
"annotations": {"foo": "event.input.SigMetadata.ID"},
},
expectedAnnotations: map[string]string{
"app": "nginx",
"foo": "TRC-2",
},
expectedAnnotations: map[string]string{
"app": "nginx",
"foo": "bar",
},
{
name: "happy path, string input event, relative input annotations are added",
inputEvent: `foo bar baz`,
inputActions: map[string]map[string]string{
"annotations": {"foo": "event.input"},
},
expectedAnnotations: map[string]string{
"app": "nginx",
"foo": "foo bar baz",
},
},
{
name: "happy path, json input event, relative input annotations are added",
inputEvent: `{"SigMetadata":{"ID":"TRC-2"}}`,
inputActions: map[string]map[string]string{
"annotations": {"foo": "event.input.SigMetadata.ID"},
},
{
name: "sad path, unable to add annotations",
inputEvent: `{"SigMetadata":{"ID":"TRC-2"}}`,
reactorFunc: func(action k8stesting.Action) (handled bool, ret runtime.Object, err error) {
return true, nil, fmt.Errorf("failed to update label")
},
expectedAnnotations: map[string]string{
"app": "nginx",
},
expectedAnnotations: map[string]string{
"app": "nginx",
"foo": "TRC-2",
},
},
{
name: "happy path, string input event, relative input annotations are added",
inputEvent: `foo bar baz`,
inputActions: map[string]map[string]string{
"annotations": {"foo": "event.input"},
},
expectedAnnotations: map[string]string{
"app": "nginx",
"foo": "foo bar baz",
},
},
{
name: "sad path, unable to add annotations",
inputEvent: `{"SigMetadata":{"ID":"TRC-2"}}`,
reactorFunc: func(action k8stesting.Action) (handled bool, ret runtime.Object, err error) {
return true, nil, fmt.Errorf("failed to update label")
},
expectedAnnotations: map[string]string{
"app": "nginx",
},
}
},
}

for _, tc := range testCases {
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
k := KubernetesClient{
clientset: fake.NewSimpleClientset(),
KubeNamespace: "testing",
Expand Down Expand Up @@ -210,6 +221,6 @@ func TestKubernetesClient_Send(t *testing.T) {

pods, _ := k.clientset.CoreV1().Pods("testing").Get(context.TODO(), "test-pod", metav1.GetOptions{})
assert.Equal(t, tc.expectedAnnotations, pods.Annotations, tc.name)
}
})
})
}
}
14 changes: 7 additions & 7 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ require (
github.com/nats-io/nats-server/v2 v2.7.4
github.com/nats-io/nats.go v1.13.1-0.20220308171302-2f2f6968e98d
github.com/nats-io/nkeys v0.3.0
github.com/open-policy-agent/opa v0.42.0
github.com/open-policy-agent/opa v0.44.0
github.com/opencontainers/image-spec v1.0.3-0.20211202183452-c5a74bcca799
github.com/opsgenie/opsgenie-go-sdk-v2 v1.2.10
github.com/spf13/cobra v1.5.0
Expand All @@ -30,7 +30,7 @@ require (
require (
github.com/Microsoft/go-winio v0.5.1 // indirect
github.com/OneOfOne/xxhash v1.2.8 // indirect
github.com/agnivade/levenshtein v1.0.1 // indirect
github.com/agnivade/levenshtein v1.1.1 // indirect
github.com/aws/aws-sdk-go-v2/credentials v1.12.14 // indirect
github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.12.12 // indirect
github.com/aws/aws-sdk-go-v2/internal/configsources v1.1.18 // indirect
Expand Down Expand Up @@ -71,24 +71,24 @@ require (
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/rcrowley/go-metrics v0.0.0-20200313005456-10cdbea86bc0 // indirect
github.com/sirupsen/logrus v1.8.1 // indirect
github.com/sirupsen/logrus v1.9.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/tchap/go-patricia/v2 v2.3.1 // indirect
github.com/tidwall/match v1.1.1 // indirect
github.com/tidwall/pretty v1.2.0 // indirect
github.com/trivago/tgo v1.0.7 // indirect
github.com/vektah/gqlparser/v2 v2.4.5 // indirect
github.com/xeipuuv/gojsonpointer v0.0.0-20190905194746-02993c407bfb // indirect
github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415 // indirect
github.com/yashtewari/glob-intersection v0.1.0 // indirect
golang.org/x/crypto v0.0.0-20220525230936-793ad666bf5e // indirect
golang.org/x/net v0.0.0-20220127200216-cd36cc0744dd // indirect
golang.org/x/net v0.0.0-20220906165146-f3363e06e74c // indirect
golang.org/x/oauth2 v0.0.0-20211104180415-d3ed0bb246c8 // indirect
golang.org/x/sys v0.0.0-20220412211240-33da011f77ad // indirect
golang.org/x/sys v0.0.0-20220728004956-3c1f35247d10 // indirect
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211 // indirect
golang.org/x/text v0.3.7 // indirect
golang.org/x/time v0.0.0-20211116232009-f0f3c7e86c11 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/protobuf v1.28.0 // indirect
google.golang.org/protobuf v1.28.1 // indirect
gopkg.in/inf.v0 v0.9.1 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
Expand Down
Loading

0 comments on commit d9e5865

Please sign in to comment.