...
1
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
20 const LabelType = "edge-type"
21
22
23 type Type string
24
25 var Types = []Type{GKE, Generic, DSDS}
26
27
28 func (t Type) String() string {
29 return string(t)
30 }
31
32
33 func StrArray(tt []Type) (s []string) {
34 for _, t := range tt {
35 s = append(s, t.String())
36 }
37 return
38 }
39
40
41 const TenantClusterTypeNameFormat = "tenant-%s-cluster-type-%s"
42
43 var ErrInvalidType = fmt.Errorf("invalid cluster type, expected one of %s", Types)
44
45
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
68
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