diff --git a/controllers/sensor/validate.go b/controllers/sensor/validate.go index c2deeab4a5..d57f466c0d 100644 --- a/controllers/sensor/validate.go +++ b/controllers/sensor/validate.go @@ -75,7 +75,13 @@ func validateTriggers(triggers []v1alpha1.Trigger) error { return errors.Errorf("no triggers found") } + trigNames := make(map[string]bool) + for _, trigger := range triggers { + if _, ok := trigNames[trigger.Template.Name]; ok { + return fmt.Errorf("duplicate trigger name: %s", trigger.Template.Name) + } + trigNames[trigger.Template.Name] = true if err := validateTriggerTemplate(trigger.Template); err != nil { return err } @@ -152,10 +158,10 @@ func validateTriggerTemplate(template *v1alpha1.TriggerTemplate) error { // validateK8STrigger validates a kubernetes trigger func validateK8STrigger(trigger *v1alpha1.StandardK8STrigger) error { if trigger == nil { - return errors.New("k8s trigger for can't be nil") + return errors.New("k8s trigger can't be nil") } if trigger.Source == nil { - return errors.New("k8s trigger for does not contain an absolute action") + return errors.New("k8s trigger does not contain an absolute action") } if trigger.GroupVersionResource.Size() == 0 { return errors.New("must provide group, version and resource for the resource") @@ -179,10 +185,10 @@ func validateK8STrigger(trigger *v1alpha1.StandardK8STrigger) error { // validateArgoWorkflowTrigger validates an Argo workflow trigger func validateArgoWorkflowTrigger(trigger *v1alpha1.ArgoWorkflowTrigger) error { if trigger == nil { - return errors.New("k8s trigger for can't be nil") + return errors.New("argoWorkflow trigger can't be nil") } if trigger.Source == nil { - return errors.New("k8s trigger for does not contain an absolute action") + return errors.New("argoWorkflow trigger does not contain an absolute action") } if trigger.GroupVersionResource.Size() == 0 { return errors.New("must provide group, version and resource for the resource") @@ -205,7 +211,7 @@ func validateArgoWorkflowTrigger(trigger *v1alpha1.ArgoWorkflowTrigger) error { // validateHTTPTrigger validates the HTTP trigger func validateHTTPTrigger(trigger *v1alpha1.HTTPTrigger) error { if trigger == nil { - return errors.New("openfaas trigger for can't be nil") + return errors.New("HTTP trigger for can't be nil") } if trigger.URL == "" { return errors.New("server URL is not specified") diff --git a/controllers/sensor/validate_test.go b/controllers/sensor/validate_test.go index c5a44cca4b..d31a02a9e7 100644 --- a/controllers/sensor/validate_test.go +++ b/controllers/sensor/validate_test.go @@ -25,6 +25,7 @@ import ( "github.com/argoproj/argo-events/pkg/apis/sensor/v1alpha1" "github.com/ghodss/yaml" "github.com/stretchr/testify/assert" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" ) func TestValidateSensor(t *testing.T) { @@ -88,3 +89,41 @@ func TestValidDepencies(t *testing.T) { assert.Equal(t, true, strings.Contains(err.Error(), "must define a name")) }) } + +func TestValidTriggers(t *testing.T) { + t.Run("duplicate trigger names", func(t *testing.T) { + triggers := []v1alpha1.Trigger{ + { + Template: &v1alpha1.TriggerTemplate{ + Name: "fake-trigger", + K8s: &v1alpha1.StandardK8STrigger{ + GroupVersionResource: metav1.GroupVersionResource{ + Group: "k8s.io", + Version: "", + Resource: "pods", + }, + Operation: "create", + Source: &v1alpha1.ArtifactLocation{}, + }, + }, + }, + { + Template: &v1alpha1.TriggerTemplate{ + Name: "fake-trigger", + K8s: &v1alpha1.StandardK8STrigger{ + GroupVersionResource: metav1.GroupVersionResource{ + Group: "k8s.io", + Version: "", + Resource: "pods", + }, + Operation: "create", + Source: &v1alpha1.ArtifactLocation{}, + }, + }, + }, + } + err := validateTriggers(triggers) + assert.NotNil(t, err) + assert.Equal(t, true, strings.Contains(err.Error(), "duplicate trigger name:")) + }) +}