Skip to content

Commit

Permalink
feat: implement delete function for k8s objects (#1041)
Browse files Browse the repository at this point in the history
* feat: implement delete function for k8s objects

This implements `delete` functionality for the standard-k8s trigger.

resolves #1040
fixes #638

Signed-off-by: tinkerborg <[email protected]>
  • Loading branch information
tinkerborg authored Jan 29, 2021
1 parent 93f0688 commit 706b746
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 1 deletion.
3 changes: 2 additions & 1 deletion controllers/sensor/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
Expand Down
1 change: 1 addition & 0 deletions pkg/apis/sensor/v1alpha1/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
17 changes: 17 additions & 0 deletions sensors/triggers/standard-k8s/standar-k8s.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
Expand Down
20 changes: 20 additions & 0 deletions sensors/triggers/standard-k8s/standar-k8s_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down Expand Up @@ -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)
}

0 comments on commit 706b746

Please sign in to comment.