...

Source file src/edge-infra.dev/pkg/f8n/warehouse/capability/capability.go

Documentation: edge-infra.dev/pkg/f8n/warehouse/capability

     1  package capability
     2  
     3  import (
     4  	"fmt"
     5  	"sort"
     6  	"strings"
     7  
     8  	wh "edge-infra.dev/pkg/f8n/warehouse"
     9  )
    10  
    11  type (
    12  	// Capability is a K8s cluster capability that a package integrates with.
    13  	Capability   string
    14  	Capabilities []Capability
    15  
    16  	// Type is a string that categorizes capabilities. A capability can
    17  	// either be infrastructure (and subject to split scheduling), or a runtime
    18  	// capability (optionally applied to the target cluster).
    19  	Type string
    20  )
    21  
    22  const (
    23  	TypeInfra   Type = "infrastructure"
    24  	TypeRuntime Type = "runtime"
    25  
    26  	// Infrastructure capability is handled specially and is always referred to by
    27  	// the constant "infrastructure"
    28  	//
    29  	// TODO(aw185176): Support multiple infrastructure providers
    30  	CapabilityInfra Capability = Capability(TypeInfra)
    31  )
    32  
    33  // OCIAnnotations converts the set of capabilities to the OCI annotation added
    34  // to Pallet artifacts. If the set of capabilities is empty, nil is returned.
    35  func (c Capabilities) OCIAnnotations() map[string]string {
    36  	if len(c) == 0 {
    37  		return nil
    38  	}
    39  	return map[string]string{
    40  		wh.AnnotationCapabilities: strings.Join(c.StrArray(), ","),
    41  	}
    42  }
    43  
    44  func (c Capabilities) StrArray() []string {
    45  	s := make([]string, len(c))
    46  	for i, capability := range c {
    47  		s[i] = string(capability)
    48  	}
    49  	sort.Strings(s)
    50  	return s
    51  }
    52  
    53  func (c Capabilities) String() string {
    54  	return strings.Join(c.StrArray(), ",")
    55  }
    56  
    57  // Set implements [flag.Value]. It accepts comma separated strings and accumulates
    58  // values from repeated occurrences of the flag. It also handles edge cases
    59  // like trailing / leading commas and duplicate values.
    60  func (c *Capabilities) Set(v string) error {
    61  	nc := append(*c, FromString(v)...)
    62  	j := 0
    63  	for i := 1; i < len(nc); i++ {
    64  		if nc[j] == nc[i] {
    65  			continue
    66  		}
    67  		j++
    68  		nc[j] = nc[i]
    69  	}
    70  
    71  	*c = nc[:j+1]
    72  	return nil
    73  }
    74  
    75  // Get implements [flag.Getter]
    76  func (c *Capabilities) Get() any {
    77  	return c
    78  }
    79  
    80  // Type implements [edge-infra.dev/pkg/lib/cli/rags.TypedValue]
    81  func (c *Capabilities) Type() string {
    82  	return "capabilities"
    83  }
    84  
    85  // Assert that Capabilities implements the sort interface
    86  var _ sort.Interface = Capabilities{}
    87  
    88  func (c Capabilities) Len() int           { return len(c) }
    89  func (c Capabilities) Less(i, j int) bool { return c[i] < c[j] }
    90  func (c Capabilities) Swap(i, j int)      { c[i], c[j] = c[j], c[i] }
    91  
    92  // IsValid returns an error if the Type isn't either "runtime" or
    93  // "infrastructure"
    94  func (c Type) IsValid() error {
    95  	if c != TypeInfra && c != TypeRuntime {
    96  		return fmt.Errorf(
    97  			"%s is not a valid capability type: expected one of [%s, %s]",
    98  			c, TypeInfra, TypeRuntime,
    99  		)
   100  	}
   101  	return nil
   102  }
   103  
   104  // FromAnnotations parses the capabilities that a package integrates with from
   105  // an OCI annotation map.
   106  func FromAnnotations(a map[string]string) Capabilities {
   107  	switch s := a[wh.AnnotationCapabilities]; s {
   108  	case "":
   109  		return nil
   110  	default:
   111  		var cc Capabilities
   112  		tt := strings.Split(s, ",")
   113  		for _, t := range tt {
   114  			cc = append(cc, Capability(t))
   115  		}
   116  		return cc
   117  	}
   118  }
   119  
   120  // FromString parses an array of Capabilities from a comma separated string.
   121  func FromString(s string) Capabilities {
   122  	tokens := strings.Split(strings.Trim(s, ","), ",")
   123  	c := make([]Capability, 0, len(tokens))
   124  	for _, cap := range strings.Split(strings.Trim(s, ","), ",") {
   125  		c = append(c, Capability(cap))
   126  	}
   127  	return c
   128  }
   129  

View as plain text