Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle empty namespace on trigger resources #224

Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 5 additions & 4 deletions pkg/apis/sensor/v1alpha1/openapi_generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pkg/apis/sensor/v1alpha1/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ type ResourceObject struct {
GroupVersionKind `json:",inline" protobuf:"bytes,5,opt,name=groupVersionKind"`

// Namespace in which to create this object
// defaults to the service account namespace
// defaults to the sensor namespace
Namespace string `json:"namespace" protobuf:"bytes,1,opt,name=namespace"`

// Source of the K8 resource file(s)
Expand Down
14 changes: 12 additions & 2 deletions sensors/event-handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,10 +100,20 @@ 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,
}
fakeDiscoveryClient.Resources = append(fakeDiscoveryClient.Resources, resourceList)
return &sensorExecutionCtx{
kubeClient: kubeClientset,
discoveryClient: kubeClientset.Discovery().(*discoveryFake.FakeDiscovery),
clientPool: NewFakeClientPool(),
discoveryClient: fakeDiscoveryClient,
clientPool: clientPool,
log: common.GetLoggerContext(common.LoggerConf()).Logger(),
sensorClient: sensorFake.NewSimpleClientset(),
sensor: sensor,
Expand Down
10 changes: 7 additions & 3 deletions sensors/trigger.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,9 +178,13 @@ func (sec *sensorExecutionCtx) executeTrigger(trigger v1alpha1.Trigger) error {

// createResourceObject creates K8s object for trigger
func (sec *sensorExecutionCtx) createResourceObject(resource *v1alpha1.ResourceObject, obj *unstructured.Unstructured) error {
if resource.Namespace != "" {
obj.SetNamespace(resource.Namespace)
namespace := resource.Namespace
// Defaults to sensor's namespace
if namespace == "" {
namespace = sec.sensor.Namespace
}
obj.SetNamespace(namespace)

if resource.Labels != nil {
labels := obj.GetLabels()
if labels != nil {
Expand Down Expand Up @@ -225,7 +229,7 @@ func (sec *sensorExecutionCtx) createResourceObject(resource *v1alpha1.ResourceO
}
sec.log.Info().Str("api", apiResource.Name).Str("group-version", gvk.Version).Msg("created api resource")

reIf := client.Resource(apiResource, resource.Namespace)
reIf := client.Resource(apiResource, namespace)
liveObj, err := reIf.Create(obj)
if err != nil {
return fmt.Errorf("failed to create resource object. err: %+v", err)
Expand Down
38 changes: 38 additions & 0 deletions sensors/trigger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -241,3 +241,41 @@ func TestProcessTrigger(t *testing.T) {
convey.So(err, convey.ShouldNotBeNil)
})
}

func TestCreateResourceObject(t *testing.T) {
convey.Convey("Given a resource object", t, func() {
testSensor, err := getSensor()
convey.So(err, convey.ShouldBeNil)
soc := getsensorExecutionCtx(testSensor)

rObj := testTrigger.Resource.DeepCopy()

convey.Convey("Given a pod", func() {
namespace := "foo"
pod := &corev1.Pod{
TypeMeta: metav1.TypeMeta{Kind: "Pod", APIVersion: "v1"},
ObjectMeta: metav1.ObjectMeta{Namespace: namespace, Name: "my-pod"},
}
uObj, err := runtime.DefaultUnstructuredConverter.ToUnstructured(pod)
convey.So(err, convey.ShouldBeNil)
err = soc.createResourceObject(rObj, &unstructured.Unstructured{Object: uObj})
convey.So(err, convey.ShouldBeNil)
pod, err = soc.kubeClient.CoreV1().Pods(namespace).Get(pod.Name, metav1.GetOptions{})
convey.So(err, convey.ShouldBeNil)
convey.So(pod.Namespace, convey.ShouldEqual, namespace)
})
convey.Convey("Given a pod without namespace, use sensor namespace", func() {
pod := &corev1.Pod{
TypeMeta: metav1.TypeMeta{Kind: "Pod", APIVersion: "v1"},
ObjectMeta: metav1.ObjectMeta{Name: "my-pod"},
}
uObj, err := runtime.DefaultUnstructuredConverter.ToUnstructured(pod)
convey.So(err, convey.ShouldBeNil)
err = soc.createResourceObject(rObj, &unstructured.Unstructured{Object: uObj})
convey.So(err, convey.ShouldBeNil)
pod, err = soc.kubeClient.CoreV1().Pods(testSensor.Namespace).Get(pod.Name, metav1.GetOptions{})
convey.So(err, convey.ShouldBeNil)
convey.So(pod.Namespace, convey.ShouldEqual, testSensor.Namespace)
})
})
}