...

Source file src/edge-infra.dev/pkg/f8n/warehouse/oci/validate/errors.go

Documentation: edge-infra.dev/pkg/f8n/warehouse/oci/validate

     1  package validate
     2  
     3  import (
     4  	"errors"
     5  	"fmt"
     6  	"strings"
     7  
     8  	wh "edge-infra.dev/pkg/f8n/warehouse"
     9  )
    10  
    11  // Annotation errors
    12  var (
    13  	errEmptyAnnotation            = errors.New("must be non-empty")
    14  	errMissingAnnotation          = errors.New("must be specified")
    15  	errInvalidURLAnnotation       = errors.New("must be valid URL")
    16  	errInvalidVersionAnnotation   = errors.New("must be valid SemVer version")
    17  	errInvalidTimestampAnnotation = errors.New("must be valid RFC3339 timestamp")
    18  	errInvalidBinaryAnnotation    = errors.New("must be 'true' or 'false'")
    19  	errInvalidListAnnotation      = fmt.Errorf("must be comma separated list matching %s", csvListRegex)
    20  	errInvalidRevisionAnnotation  = fmt.Errorf("must be valid revision hash matching %s", revisionHashRegex)
    21  	errInvalidSchemaAnnotation    = fmt.Errorf("must be '%s'", wh.Schema1)
    22  )
    23  
    24  // Pallet specific errors
    25  var (
    26  	errInvalidPalletKindAnnotation = fmt.Errorf("must be '%s'", wh.PalletKind)
    27  	errMultipleBaseLayers          = fmt.Errorf("there can only be one base runtime layer, non-default layers must have '%s' set", wh.AnnotationLayerRuntimeCapability)
    28  	errMultipleInfraLayers         = errors.New("there can only be one infrastructure layer")
    29  	errLayersMustBeK8sManifests    = errors.New("layer must describe valid K8s manifests")
    30  )
    31  
    32  // AnnotationError occurs when an annotation value does pass its associated validation function.
    33  // e.g. when a required annotation is missing, or it does not have the expected format.
    34  type AnnotationError struct {
    35  	Err        error
    36  	Annotation string
    37  	Values     []string
    38  }
    39  
    40  func (err AnnotationError) Error() string {
    41  	if len(err.Values) == 0 {
    42  		return fmt.Sprintf("annotation '%s' is invalid: %v", err.Annotation, err.Err)
    43  	}
    44  
    45  	values := strings.Join(err.Values, ",")
    46  	return fmt.Sprintf("'%s' is invalid for annotation '%s': %v", values, err.Annotation, err.Err)
    47  }
    48  
    49  func NewAnnotationError(err error, annotation string, values ...string) AnnotationError {
    50  	return AnnotationError{
    51  		Err:        err,
    52  		Annotation: annotation,
    53  		Values:     values,
    54  	}
    55  }
    56  
    57  // ImageLayersError occurs when one or more layers in a v1.Image are not valid.
    58  // e.g. when there are one or more base or infrastructure layers defined.
    59  type ImageLayersError struct {
    60  	Err   error
    61  	Count int
    62  }
    63  
    64  func (err ImageLayersError) Error() string {
    65  	return fmt.Sprintf("%d layers are invalid: %v", err.Count, err.Err)
    66  }
    67  
    68  func NewImageLayersError(err error, count int) ImageLayersError {
    69  	return ImageLayersError{
    70  		Err:   err,
    71  		Count: count,
    72  	}
    73  }
    74  
    75  // DescriptorError occurs when a descriptor defines values in an annotation that are not present on the root node.
    76  // e.g. an provider image has "gke" provider, but the parent index only has "sds,generic".
    77  type DescriptorError struct {
    78  	Annotation string
    79  	Value      string
    80  	RootValues []string
    81  }
    82  
    83  func (err DescriptorError) Error() string {
    84  	if len(err.RootValues) == 0 || err.RootValues[0] == "" {
    85  		return fmt.Sprintf("descriptor has '%s' but root does not", err.Annotation)
    86  	}
    87  	return fmt.Sprintf("descriptor has '%s'='%s' which is not in root set '%s'", err.Annotation, err.Value, err.RootValues)
    88  }
    89  
    90  func NewDescriptorError(annotation, value string, rootValues []string) DescriptorError {
    91  	return DescriptorError{
    92  		Annotation: annotation,
    93  		Value:      value,
    94  		RootValues: rootValues,
    95  	}
    96  }
    97  

View as plain text