...

Source file src/edge-infra.dev/pkg/edge/capabilities/capability_types.go

Documentation: edge-infra.dev/pkg/edge/capabilities

     1  package capabilities
     2  
     3  import (
     4  	"slices"
     5  
     6  	version "github.com/hashicorp/go-version"
     7  
     8  	"edge-infra.dev/pkg/edge/api/graph/model"
     9  )
    10  
    11  // EdgeCapability denotes a pallet that can be deployed on a cluster as well as associated labels, visibility, and editability
    12  type EdgeCapability struct {
    13  	ArtifactName      string
    14  	Editable          bool
    15  	Visible           bool
    16  	VersionIntroduced string
    17  }
    18  
    19  // edge capability label constant
    20  const EdgeCapabilitiesLabel = "edge-capabilities"
    21  
    22  // optional pallet names to be inserted in edge capabilities
    23  const (
    24  	VirtualMachinesPallet    = "virtual-machines"
    25  	DeschedulerPallet        = "descheduler"
    26  	MultiInterfacePallet     = "multi-interface"
    27  	DistributedStoragePallet = "distributed-storage"
    28  	EdgeIDPallet             = "edge-iam"
    29  	BSLShimsPallet           = "bsl-shims"
    30  	DataSyncPallet           = "data-sync"
    31  	MetricsPallet            = "metrics"
    32  )
    33  
    34  const (
    35  	capabilityLabelColor         = "#eeeeee"
    36  	distributedStorageLabelColor = "ff5f00"
    37  	vmLabelColor                 = "541675"
    38  	metricsLabelColor            = "ff0000"
    39  )
    40  
    41  // DefaultStoreEdgeCapabilities is the list of optional pallets that by default are added as labels to a cluster
    42  var DefaultStoreEdgeCapabilities = []string{"edge-iam", "bsl-shims", "metrics", "data-sync"}
    43  
    44  var EdgeAutomatedCapabilityLabelTypes = []string{EdgeCapabilitiesLabel, "virtual-machines", "distributed-storage"}
    45  var EdgeAutomatedCapabilityLabels = []*model.LabelInput{
    46  	{
    47  		Key:         "virtual.machines/schedulable",
    48  		Color:       vmLabelColor,
    49  		Visible:     true,
    50  		Editable:    false,
    51  		Unique:      false,
    52  		Description: "enables the vm control plane pods to be scheduled to nodes with this label",
    53  		Type:        "virtual-machines",
    54  	},
    55  	{
    56  		Key:         "distributed.storage/schedulable",
    57  		Color:       distributedStorageLabelColor,
    58  		Visible:     true,
    59  		Editable:    false,
    60  		Unique:      false,
    61  		Description: "marks nodes to deploy distributed storage satellites to",
    62  		Type:        "distributed-storage",
    63  	},
    64  	{
    65  		Key:         "distributed.storage/diskless",
    66  		Color:       distributedStorageLabelColor,
    67  		Visible:     true,
    68  		Editable:    false,
    69  		Unique:      false,
    70  		Description: "marks a node a diskless",
    71  		Type:        "distributed-storage",
    72  	},
    73  }
    74  
    75  // ADD OPTIONAL PALLET EDGE CAPABILITIES HERE
    76  // list of optional pallets for edge capability
    77  var EdgeCapabilities = []*EdgeCapability{
    78  	{
    79  		ArtifactName:      VirtualMachinesPallet,
    80  		Visible:           true,
    81  		Editable:          false,
    82  		VersionIntroduced: "0.20",
    83  	},
    84  	{
    85  		ArtifactName:      DeschedulerPallet,
    86  		Visible:           false,
    87  		Editable:          false,
    88  		VersionIntroduced: "0.20",
    89  	},
    90  	{
    91  		ArtifactName:      MultiInterfacePallet,
    92  		Visible:           false,
    93  		Editable:          false,
    94  		VersionIntroduced: "0.20",
    95  	},
    96  	{
    97  		ArtifactName:      DistributedStoragePallet,
    98  		Visible:           true,
    99  		Editable:          false,
   100  		VersionIntroduced: "0.20",
   101  	},
   102  	{
   103  		ArtifactName:      EdgeIDPallet,
   104  		Visible:           true,
   105  		Editable:          false,
   106  		VersionIntroduced: "0.21",
   107  	},
   108  	{
   109  		ArtifactName:      BSLShimsPallet,
   110  		Visible:           true,
   111  		Editable:          false,
   112  		VersionIntroduced: "0.21",
   113  	},
   114  	{
   115  		ArtifactName:      DataSyncPallet,
   116  		Visible:           true,
   117  		Editable:          false,
   118  		VersionIntroduced: "0.21",
   119  	},
   120  	{
   121  		ArtifactName:      MetricsPallet,
   122  		Visible:           true,
   123  		Editable:          false,
   124  		VersionIntroduced: "0.21",
   125  	},
   126  }
   127  
   128  func GetCapability(label string) *EdgeCapability {
   129  	idx := slices.IndexFunc(EdgeCapabilities, func(capability *EdgeCapability) bool { return capability.ArtifactName == label })
   130  	if idx != -1 {
   131  		return EdgeCapabilities[idx]
   132  	}
   133  	return nil
   134  }
   135  
   136  // GetCapabilityLabels gets all labels of type edge-capabilities. If any capabilities are passed in,
   137  // function only returns edge capability labels corresponding to said capabilities
   138  func GetCapabilityLabels(labels []*model.Label, capabilities ...string) []*model.Label {
   139  	var edgeCapabilityLabels []*model.Label
   140  	for _, label := range labels {
   141  		if label.Type == EdgeCapabilitiesLabel {
   142  			edgeCapabilityLabels = append(edgeCapabilityLabels, label)
   143  		}
   144  	}
   145  	if len(capabilities) == 0 || edgeCapabilityLabels == nil {
   146  		return edgeCapabilityLabels
   147  	}
   148  	var specificEdgeCapabilityLabels []*model.Label
   149  	for _, capability := range capabilities {
   150  		idx := slices.IndexFunc(edgeCapabilityLabels, func(label *model.Label) bool {
   151  			return label.Key == capability
   152  		})
   153  		if idx != -1 {
   154  			specificEdgeCapabilityLabels = append(specificEdgeCapabilityLabels, edgeCapabilityLabels[idx])
   155  		}
   156  	}
   157  	return specificEdgeCapabilityLabels
   158  }
   159  
   160  // GetCapabilityLabelsForSupportedVersion gets all supported version capabilities for requested fleet version. If aboveVersion is passed in,
   161  // function only returns supported versions above that version and equal to or below fleet Version. This is useful for upgrading clusters that
   162  // already have some edge capabilities but have not all (i.e. have some removed), to prevent re-adding already
   163  // removed capabilities and only add new ones introduced between aboveVersion and fleetVersion
   164  func GetCapabilityLabelsForSupportedVersion(edgeCapabilityLabels []*model.Label, fleetVersion string, aboveVersion *string) ([]*model.Label, error) {
   165  	var supportedEdgeCapabilityLabels []*model.Label
   166  	var unsupportedVersion *version.Version
   167  	//the static fleetVersion to compare against
   168  	staticVersion, err := version.NewVersion(fleetVersion)
   169  	if err != nil {
   170  		return nil, err
   171  	}
   172  	if aboveVersion != nil {
   173  		unsupportedVersion, err = version.NewVersion(*aboveVersion)
   174  		if err != nil {
   175  			return nil, err
   176  		}
   177  	}
   178  	for _, label := range edgeCapabilityLabels {
   179  		capability := GetCapability(label.Key)
   180  		supportedCapabilityVersion, err := version.NewVersion(capability.VersionIntroduced)
   181  		if err != nil {
   182  			return nil, err
   183  		}
   184  
   185  		if unsupportedVersion != nil && !(supportedCapabilityVersion.LessThanOrEqual(staticVersion) && supportedCapabilityVersion.GreaterThan(unsupportedVersion)) {
   186  			continue
   187  		}
   188  
   189  		if !staticVersion.GreaterThanOrEqual(supportedCapabilityVersion) {
   190  			continue
   191  		}
   192  		supportedEdgeCapabilityLabels = append(supportedEdgeCapabilityLabels, label)
   193  	}
   194  	return supportedEdgeCapabilityLabels, nil
   195  }
   196  
   197  func NewCapabilityLabel(capability *EdgeCapability) *model.LabelInput {
   198  	return &model.LabelInput{
   199  		Key:         capability.ArtifactName,
   200  		Color:       capabilityLabelColor,
   201  		Visible:     capability.Visible,
   202  		Editable:    capability.Editable,
   203  		Unique:      false,
   204  		Description: "optional pallet edge capability",
   205  		Type:        EdgeCapabilitiesLabel,
   206  	}
   207  }
   208  

View as plain text