...

Source file src/edge-infra.dev/pkg/edge/constants/api/cluster/cluster.go

Documentation: edge-infra.dev/pkg/edge/constants/api/cluster

     1  // Package cluster contains Edge Cluster enum constants.
     2  package cluster
     3  
     4  import (
     5  	"flag"
     6  	"fmt"
     7  
     8  	"edge-infra.dev/pkg/edge/api/graph/model"
     9  )
    10  
    11  const (
    12  	GKE     = "gke"
    13  	Generic = "generic"
    14  	DSDS    = "dsds"
    15  
    16  	Label = "cluster.edge.ncr.com/type"
    17  )
    18  
    19  // LabelType is the type of the label in the edge database for the cluster type
    20  const LabelType = "edge-type"
    21  
    22  // Type is the enum type representing Edge Cluster Types
    23  type Type string
    24  
    25  var Types = []Type{GKE, Generic, DSDS}
    26  
    27  // String is a convenience function for casting a cluster.Type to a string
    28  func (t Type) String() string {
    29  	return string(t)
    30  }
    31  
    32  // StrArray converts an array of cluster.Type into strings
    33  func StrArray(tt []Type) (s []string) {
    34  	for _, t := range tt {
    35  		s = append(s, t.String())
    36  	}
    37  	return
    38  }
    39  
    40  // TenantClusterTypeNameFormat is the name format for ClusterSelector tenant cluster types
    41  const TenantClusterTypeNameFormat = "tenant-%s-cluster-type-%s"
    42  
    43  var ErrInvalidType = fmt.Errorf("invalid cluster type, expected one of %s", Types)
    44  
    45  // IsValid checks that a Cluster Type enum value is correct.
    46  func (t Type) IsValid() error {
    47  	found := false
    48  	for _, ct := range Types {
    49  		if t == ct {
    50  			found = true
    51  		}
    52  	}
    53  	if found {
    54  		return nil
    55  	}
    56  	return fmt.Errorf("%v: %w", t, ErrInvalidType)
    57  }
    58  
    59  func (t Type) IsGKE() bool {
    60  	return string(t) == GKE
    61  }
    62  
    63  func (t Type) IsDSDS() bool {
    64  	return string(t) == DSDS
    65  }
    66  
    67  // RegisterTypeFlag registers a flag for specifying the cluster type at the command
    68  // line, binding the value to the provided destination pointer.
    69  func RegisterTypeFlag(fs *flag.FlagSet, dest *string) {
    70  	fs.StringVar(dest, "cluster-type", Generic,
    71  		fmt.Sprintf("what type of cluster, must be one of: %s", Types))
    72  }
    73  
    74  func GetClusterTypeFromLabels(labels []*model.Label) Type {
    75  	for _, l := range labels {
    76  		t := Type(l.Key)
    77  		if err := t.IsValid(); err == nil && l.Type == "edge" {
    78  			return t
    79  		}
    80  	}
    81  	return ""
    82  }
    83  

View as plain text