Skip to content

Commit

Permalink
feat(update-operation): support update operation for triggers (#456)
Browse files Browse the repository at this point in the history
  • Loading branch information
VaibhavPage authored Jan 5, 2020
1 parent 21cd57b commit b56b5b2
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 4 deletions.
7 changes: 7 additions & 0 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.

13 changes: 13 additions & 0 deletions pkg/apis/sensor/v1alpha1/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand Down
5 changes: 4 additions & 1 deletion sensors/event-notifications.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
12 changes: 10 additions & 2 deletions sensors/triggers/execute.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -45,13 +46,20 @@ 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 == "" {
namespace = sensor.Namespace
}
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))
}
}
2 changes: 1 addition & 1 deletion sensors/triggers/execute_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down

0 comments on commit b56b5b2

Please sign in to comment.