github.com/argoproj/argo-events@v1.9.1/eventsources/sources/resource/validate.go (about)

     1  /*
     2  Copyright 2018 BlackRock, Inc.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8  	http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package resource
    18  
    19  import (
    20  	"context"
    21  	"fmt"
    22  
    23  	"k8s.io/apimachinery/pkg/selection"
    24  
    25  	"github.com/argoproj/argo-events/common"
    26  	"github.com/argoproj/argo-events/pkg/apis/eventsource/v1alpha1"
    27  )
    28  
    29  // ValidateEventSource validates a resource event source
    30  func (listener *EventListener) ValidateEventSource(ctx context.Context) error {
    31  	return validate(&listener.ResourceEventSource)
    32  }
    33  
    34  func validate(eventSource *v1alpha1.ResourceEventSource) error {
    35  	if eventSource == nil {
    36  		return common.ErrNilEventSource
    37  	}
    38  	if eventSource.Version == "" {
    39  		return fmt.Errorf("version must be specified")
    40  	}
    41  	if eventSource.Resource == "" {
    42  		return fmt.Errorf("resource must be specified")
    43  	}
    44  	if eventSource.EventTypes == nil {
    45  		return fmt.Errorf("event types must be specified")
    46  	}
    47  	if eventSource.Filter != nil {
    48  		if eventSource.Filter.Labels != nil {
    49  			if err := validateSelectors(eventSource.Filter.Labels); err != nil {
    50  				return err
    51  			}
    52  		}
    53  		if eventSource.Filter.Fields != nil {
    54  			if err := validateSelectors(eventSource.Filter.Fields); err != nil {
    55  				return err
    56  			}
    57  		}
    58  	}
    59  	return nil
    60  }
    61  
    62  func validateSelectors(selectors []v1alpha1.Selector) error {
    63  	for _, sel := range selectors {
    64  		if sel.Key == "" {
    65  			return fmt.Errorf("key can't be empty for selector")
    66  		}
    67  		if sel.Operation == "" {
    68  			continue
    69  		}
    70  		if selection.Operator(sel.Operation) == "" {
    71  			return fmt.Errorf("unknown selection operation %s", sel.Operation)
    72  		}
    73  	}
    74  	return nil
    75  }