diff --git a/Gopkg.lock b/Gopkg.lock index 8168877158..3959ca97a6 100644 --- a/Gopkg.lock +++ b/Gopkg.lock @@ -1291,12 +1291,13 @@ [[projects]] branch = "release-7.0" - digest = "1:6e37d35d68d68b1368b803617d346633830eb13b50120f68ead2c029c05eeea0" + digest = "1:7336dcb25cac9940ac84ea6c59c4c36d1b6b6ca4a3aba38d6d8c74bc53699a40" name = "k8s.io/client-go" packages = [ "discovery", "discovery/fake", "dynamic", + "dynamic/fake", "informers", "informers/admissionregistration", "informers/admissionregistration/v1alpha1", @@ -1596,6 +1597,7 @@ "k8s.io/client-go/discovery", "k8s.io/client-go/discovery/fake", "k8s.io/client-go/dynamic", + "k8s.io/client-go/dynamic/fake", "k8s.io/client-go/informers", "k8s.io/client-go/informers/core/v1", "k8s.io/client-go/kubernetes", diff --git a/sensors/event-handler_test.go b/sensors/event-handler_test.go index 18e4f1942b..e22fa1b3d0 100644 --- a/sensors/event-handler_test.go +++ b/sensors/event-handler_test.go @@ -37,6 +37,7 @@ import ( var sensorStr = `apiVersion: argoproj.io/v1alpha1 kind: Sensor metadata: + namespace: argo-events name: test-sensor labels: sensors.argoproj.io/sensor-controller-instanceid: argo-events @@ -78,6 +79,13 @@ spec: image: "docker/whalesay:latest" name: whalesay` +var podResourceList = metav1.APIResourceList{ + GroupVersion: metav1.GroupVersion{Group: "", Version: "v1"}.String(), + APIResources: []metav1.APIResource{ + {Kind: "Pod", Namespaced: true, Name: "pods", SingularName: "pod", Group: "", Version: "v1", Verbs: []string{"create", "get"}}, + }, +} + func getSensor() (*v1alpha1.Sensor, error) { var sensor v1alpha1.Sensor err := yaml.Unmarshal([]byte(sensorStr), &sensor) @@ -101,15 +109,10 @@ func (m *mockHttpWriter) WriteHeader(statusCode int) { func getsensorExecutionCtx(sensor *v1alpha1.Sensor) *sensorExecutionCtx { kubeClientset := fake.NewSimpleClientset() fakeDiscoveryClient := kubeClientset.Discovery().(*discoveryFake.FakeDiscovery) - resourceList := &metav1.APIResourceList{ - TypeMeta: metav1.TypeMeta{Kind: "Pod", APIVersion: "v1"}, - GroupVersion: metav1.GroupVersion{Group: "", Version: "v1"}.String(), - APIResources: []metav1.APIResource{{Kind: "Pod"}}, - } clientPool := &FakeClientPool{ - kubeClientset.Fake, + Fake: kubeClientset.Fake, } - fakeDiscoveryClient.Resources = append(fakeDiscoveryClient.Resources, resourceList) + fakeDiscoveryClient.Resources = append(fakeDiscoveryClient.Resources, &podResourceList) return &sensorExecutionCtx{ kubeClient: kubeClientset, discoveryClient: fakeDiscoveryClient, diff --git a/sensors/trigger_test.go b/sensors/trigger_test.go index 07c0cc6e5a..b52fd6dd8d 100644 --- a/sensors/trigger_test.go +++ b/sensors/trigger_test.go @@ -30,6 +30,7 @@ import ( "k8s.io/apimachinery/pkg/types" "k8s.io/apimachinery/pkg/watch" "k8s.io/client-go/dynamic" + dynamicfake "k8s.io/client-go/dynamic/fake" "k8s.io/client-go/kubernetes/fake" kTesting "k8s.io/client-go/testing" "k8s.io/client-go/util/flowcontrol" @@ -197,20 +198,14 @@ func (p *FakeClientPool) ClientForGroupVersionKind(kind schema.GroupVersionKind) } var testWf = ` -apiVersion: argoproj.io/v1alpha1 -kind: Workflow +apiVersion: v1 +kind: Pod metadata: generateName: hello-world- spec: -entrypoint: whalesay -templates: -- name: whalesay -container: - args: - - "hello world" - command: - - cowsay - image: "docker/whalesay:latest" + containers: + - name: whalesay + image: "docker/whalesay:latest" ` var testTrigger = v1alpha1.Trigger{ @@ -218,9 +213,8 @@ var testTrigger = v1alpha1.Trigger{ Resource: &v1alpha1.ResourceObject{ Namespace: corev1.NamespaceDefault, GroupVersionKind: v1alpha1.GroupVersionKind{ - Group: "argoproj.io", - Version: "v1alpha1", - Kind: "workflow", + Version: "v1", + Kind: "Pod", }, Source: v1alpha1.ArtifactLocation{ Inline: &testWf, @@ -247,35 +241,50 @@ func TestCreateResourceObject(t *testing.T) { testSensor, err := getSensor() convey.So(err, convey.ShouldBeNil) soc := getsensorExecutionCtx(testSensor) - - rObj := testTrigger.Resource.DeepCopy() + fakeclient := soc.clientPool.(*FakeClientPool).Fake + dynamicClient := dynamicfake.FakeResourceClient{Resource: schema.GroupVersionResource{Version: "v1", Resource: "pods"}, Fake: &fakeclient} convey.Convey("Given a pod", func() { - namespace := "foo" + rObj := testTrigger.Resource.DeepCopy() + rObj.Namespace = "foo" pod := &corev1.Pod{ TypeMeta: metav1.TypeMeta{Kind: "Pod", APIVersion: "v1"}, - ObjectMeta: metav1.ObjectMeta{Namespace: namespace, Name: "my-pod"}, + ObjectMeta: metav1.ObjectMeta{Namespace: rObj.Namespace, Name: "my-pod"}, } - uObj, err := runtime.DefaultUnstructuredConverter.ToUnstructured(pod) + uObj, err := getUnstructuredPod(pod) convey.So(err, convey.ShouldBeNil) - err = soc.createResourceObject(rObj, &unstructured.Unstructured{Object: uObj}) + + err = soc.createResourceObject(rObj, uObj) convey.So(err, convey.ShouldBeNil) - pod, err = soc.kubeClient.CoreV1().Pods(namespace).Get(pod.Name, metav1.GetOptions{}) + + unstructuredPod, err := dynamicClient.Get(pod.Name, metav1.GetOptions{}) convey.So(err, convey.ShouldBeNil) - convey.So(pod.Namespace, convey.ShouldEqual, namespace) + convey.So(unstructuredPod.GetNamespace(), convey.ShouldEqual, rObj.Namespace) }) convey.Convey("Given a pod without namespace, use sensor namespace", func() { + rObj := testTrigger.Resource.DeepCopy() + rObj.Namespace = "" pod := &corev1.Pod{ TypeMeta: metav1.TypeMeta{Kind: "Pod", APIVersion: "v1"}, - ObjectMeta: metav1.ObjectMeta{Name: "my-pod"}, + ObjectMeta: metav1.ObjectMeta{Name: "my-pod-without-namespace"}, } - uObj, err := runtime.DefaultUnstructuredConverter.ToUnstructured(pod) + uObj, err := getUnstructuredPod(pod) convey.So(err, convey.ShouldBeNil) - err = soc.createResourceObject(rObj, &unstructured.Unstructured{Object: uObj}) + + err = soc.createResourceObject(rObj, uObj) convey.So(err, convey.ShouldBeNil) - pod, err = soc.kubeClient.CoreV1().Pods(testSensor.Namespace).Get(pod.Name, metav1.GetOptions{}) + + unstructuredPod, err := dynamicClient.Get(pod.Name, metav1.GetOptions{}) convey.So(err, convey.ShouldBeNil) - convey.So(pod.Namespace, convey.ShouldEqual, testSensor.Namespace) + convey.So(unstructuredPod.GetNamespace(), convey.ShouldEqual, testSensor.Namespace) }) }) } + +func getUnstructuredPod(pod *corev1.Pod) (*unstructured.Unstructured, error) { + obj, err := runtime.DefaultUnstructuredConverter.ToUnstructured(pod) + if err != nil { + return nil, err + } + return &unstructured.Unstructured{Object: obj}, nil +}