From b56b5b2934b420b2a685a0064d4900bfa372202d Mon Sep 17 00:00:00 2001 From: Vaibhav Date: Sun, 5 Jan 2020 17:22:13 -0500 Subject: [PATCH] feat(update-operation): support update operation for triggers (#456) --- pkg/apis/sensor/v1alpha1/openapi_generated.go | 7 +++++++ pkg/apis/sensor/v1alpha1/types.go | 13 +++++++++++++ sensors/event-notifications.go | 5 ++++- sensors/triggers/execute.go | 12 ++++++++++-- sensors/triggers/execute_test.go | 2 +- 5 files changed, 35 insertions(+), 4 deletions(-) diff --git a/pkg/apis/sensor/v1alpha1/openapi_generated.go b/pkg/apis/sensor/v1alpha1/openapi_generated.go index 46cc544e40..9f7616b74e 100644 --- a/pkg/apis/sensor/v1alpha1/openapi_generated.go +++ b/pkg/apis/sensor/v1alpha1/openapi_generated.go @@ -1240,6 +1240,13 @@ func schema_pkg_apis_sensor_v1alpha1_TriggerTemplate(ref common.ReferenceCallbac Ref: ref("github.com/argoproj/argo-events/pkg/apis/sensor/v1alpha1.ArtifactLocation"), }, }, + "operation": { + SchemaProps: spec.SchemaProps{ + Description: "Operation refers to the type of operation performed on the trigger resource. Default value is Create.", + Type: []string{"string"}, + Format: "", + }, + }, }, Required: []string{"name", "source"}, }, diff --git a/pkg/apis/sensor/v1alpha1/types.go b/pkg/apis/sensor/v1alpha1/types.go index f1d1d34d19..65044f3347 100644 --- a/pkg/apis/sensor/v1alpha1/types.go +++ b/pkg/apis/sensor/v1alpha1/types.go @@ -70,6 +70,15 @@ const ( TriggerCycleFailure TriggerCycleState = "Failure" // one or more triggers failed ) +// KubernetesResourceOperation refers to the type of operation performed on the K8s resource +type KubernetesResourceOperation string + +// possible values for KubernetesResourceOperation +const ( + Create KubernetesResourceOperation = "Create" // creates the resource + Update KubernetesResourceOperation = "Update" // updates the resource +) + // Sensor is the definition of a sensor resource // +genclient // +genclient:noStatus @@ -219,6 +228,10 @@ type TriggerTemplate struct { *metav1.GroupVersionResource `json:",inline" protobuf:"bytes,3,opt,name=groupVersionResource"` // Source of the K8 resource file(s) Source *ArtifactLocation `json:"source" protobuf:"bytes,4,opt,name=source"` + // Operation refers to the type of operation performed on the trigger resource. + // Default value is Create. + // +optional + Operation KubernetesResourceOperation `json:"operation,omitempty" protobuf:"bytes,5,opt,name=operation"` } // TriggerSwitch describes condition which must be satisfied in order to execute a trigger. diff --git a/sensors/event-notifications.go b/sensors/event-notifications.go index 066e170a0f..9e98fc3123 100644 --- a/sensors/event-notifications.go +++ b/sensors/event-notifications.go @@ -100,7 +100,10 @@ func (sensorCtx *SensorContext) operateEventNotification(notification *types.Not Version: trigger.Template.GroupVersionResource.Version, Resource: trigger.Template.GroupVersionResource.Resource, }) - newObj, err := triggers.Execute(sensorCtx.Sensor, uObj, client) + if trigger.Template.Operation == "" { + trigger.Template.Operation = v1alpha1.Create + } + newObj, err := triggers.Execute(sensorCtx.Sensor, uObj, client, trigger.Template.Operation) if err != nil { return err } diff --git a/sensors/triggers/execute.go b/sensors/triggers/execute.go index dc2c9d32c5..c86496fc61 100644 --- a/sensors/triggers/execute.go +++ b/sensors/triggers/execute.go @@ -19,6 +19,7 @@ package triggers import ( "github.com/argoproj/argo-events/pkg/apis/sensor/v1alpha1" "github.com/argoproj/argo-events/store" + "github.com/pkg/errors" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" "k8s.io/client-go/dynamic" @@ -45,7 +46,7 @@ func FetchResource(kubeClient kubernetes.Interface, sensor *v1alpha1.Sensor, tri return nil, nil } -func Execute(sensor *v1alpha1.Sensor, obj *unstructured.Unstructured, client dynamic.NamespaceableResourceInterface) (*unstructured.Unstructured, error) { +func Execute(sensor *v1alpha1.Sensor, obj *unstructured.Unstructured, client dynamic.NamespaceableResourceInterface, op v1alpha1.KubernetesResourceOperation) (*unstructured.Unstructured, error) { namespace := obj.GetNamespace() // Defaults to sensor's namespace if namespace == "" { @@ -53,5 +54,12 @@ func Execute(sensor *v1alpha1.Sensor, obj *unstructured.Unstructured, client dyn } obj.SetNamespace(namespace) - return client.Namespace(namespace).Create(obj, metav1.CreateOptions{}) + switch op { + case v1alpha1.Create: + return client.Namespace(namespace).Create(obj, metav1.CreateOptions{}) + case v1alpha1.Update: + return client.Namespace(namespace).Update(obj, metav1.UpdateOptions{}) + default: + return nil, errors.Errorf("unknown operation type %s", string(op)) + } } diff --git a/sensors/triggers/execute_test.go b/sensors/triggers/execute_test.go index 1a654589d5..7316c2345a 100644 --- a/sensors/triggers/execute_test.go +++ b/sensors/triggers/execute_test.go @@ -90,7 +90,7 @@ func TestExecute(t *testing.T) { Version: trigger.Template.GroupVersionResource.Version, Group: trigger.Template.GroupVersionResource.Group, }) - uObj, err := Execute(sensorObj, deployment, namespacableClient) + uObj, err := Execute(sensorObj, deployment, namespacableClient, v1alpha1.Create) assert.Nil(t, err) assert.NotNil(t, uObj) assert.Equal(t, uObj.GetName(), deployment.GetName())