diff --git a/controllers/sensor/validate.go b/controllers/sensor/validate.go index 3ae333cad9..be38010a8f 100644 --- a/controllers/sensor/validate.go +++ b/controllers/sensor/validate.go @@ -161,7 +161,8 @@ func validateK8STrigger(trigger *v1alpha1.StandardK8STrigger) error { return errors.New("must provide group, version and resource for the resource") } switch trigger.Operation { - case "", v1alpha1.Create, v1alpha1.Patch, v1alpha1.Update: + case "", v1alpha1.Create, v1alpha1.Patch, v1alpha1.Update, v1alpha1.Delete: + default: return errors.Errorf("unknown operation type %s", string(trigger.Operation)) } diff --git a/pkg/apis/sensor/v1alpha1/types.go b/pkg/apis/sensor/v1alpha1/types.go index 21865064c3..7d233a7cb3 100644 --- a/pkg/apis/sensor/v1alpha1/types.go +++ b/pkg/apis/sensor/v1alpha1/types.go @@ -38,6 +38,7 @@ const ( Create KubernetesResourceOperation = "create" // create the resource Update KubernetesResourceOperation = "update" // updates the resource Patch KubernetesResourceOperation = "patch" // patch resource + Delete KubernetesResourceOperation = "delete" // deletes the resource ) // ArgoWorkflowOperation refers to the type of the operation performed on the Argo Workflow diff --git a/sensors/triggers/standard-k8s/standar-k8s.go b/sensors/triggers/standard-k8s/standar-k8s.go index 9d767eb0e1..b8a6cd967f 100644 --- a/sensors/triggers/standard-k8s/standar-k8s.go +++ b/sensors/triggers/standard-k8s/standar-k8s.go @@ -212,6 +212,23 @@ func (k8sTrigger *StandardK8sTrigger) Execute(ctx context.Context, events map[st return k8sTrigger.namespableDynamicClient.Namespace(namespace).Patch(ctx, obj.GetName(), k8sTrigger.Trigger.Template.K8s.PatchStrategy, body, metav1.PatchOptions{}) + case v1alpha1.Delete: + k8sTrigger.Logger.Info("deleting the object...") + _, err := k8sTrigger.namespableDynamicClient.Namespace(namespace).Get(ctx, obj.GetName(), metav1.GetOptions{}) + + if err != nil && apierrors.IsNotFound(err) { + k8sTrigger.Logger.Info("object not found, nothing to delete...") + return nil, nil + } else if err != nil { + return nil, errors.Errorf("failed to retrieve existing object. err: %+v\n", err) + } + + err = k8sTrigger.namespableDynamicClient.Namespace(namespace).Delete(ctx, obj.GetName(), metav1.DeleteOptions{}) + if err != nil { + return nil, errors.Errorf("failed to delete object. err: %+v\n", err) + } + return nil, nil + default: return nil, errors.Errorf("unknown operation type %s", string(op)) } diff --git a/sensors/triggers/standard-k8s/standar-k8s_test.go b/sensors/triggers/standard-k8s/standar-k8s_test.go index 04489f0569..07a5e2c3fe 100644 --- a/sensors/triggers/standard-k8s/standar-k8s_test.go +++ b/sensors/triggers/standard-k8s/standar-k8s_test.go @@ -34,6 +34,8 @@ import ( "github.com/argoproj/argo-events/common/logging" apicommon "github.com/argoproj/argo-events/pkg/apis/common" "github.com/argoproj/argo-events/pkg/apis/sensor/v1alpha1" + core "k8s.io/client-go/testing" + k8stesting "k8s.io/client-go/testing" ) var sensorObj = &v1alpha1.Sensor{ @@ -193,4 +195,22 @@ func TestStandardK8sTrigger_Execute(t *testing.T) { uObj, ok = resource.(*unstructured.Unstructured) assert.Equal(t, true, ok) assert.Equal(t, "bar", uObj.GetLabels()["foo"]) + + deleted := false + + sensorObj.Spec.Triggers[0].Template.K8s.Operation = v1alpha1.Delete + + client.Fake.PrependReactor("delete", "deployments", func(action k8stesting.Action) (handled bool, ret runtime.Object, err error) { + deleteAction := action.(core.DeleteAction) + if deleteAction.GetName() == deployment.GetName() && deleteAction.GetNamespace() == deployment.GetNamespace() { + deleted = true + } + return true, nil, nil + }) + + impl = NewStandardK8sTrigger(fake.NewSimpleClientset(), client, sensorObj, &sensorObj.Spec.Triggers[0], logging.NewArgoEventsLogger().Desugar()) + resource, err = impl.Execute(ctx, nil, uObj) + assert.Nil(t, err) + assert.Nil(t, resource) + assert.True(t, deleted) }