This repository has been archived by the owner on Apr 29, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
desired.go
126 lines (103 loc) · 2.99 KB
/
desired.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package kubernetes
import (
"fmt"
"github.com/caos/orbos/internal/secret"
"github.com/caos/orbos/internal/tree"
"github.com/pkg/errors"
core "k8s.io/api/core/v1"
"github.com/caos/orbos/internal/operator/orbiter"
)
var ipPartRegex = `([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])`
var ipRegex = fmt.Sprintf(`%s\.%s\.%s\.%s`, ipPartRegex, ipPartRegex, ipPartRegex, ipPartRegex)
var cidrRegex = fmt.Sprintf(`%s/([1-2][0-9]|3[0-2]|[0-9])`, ipRegex)
type DesiredV0 struct {
Common tree.Common `yaml:",inline"`
Spec Spec
}
type Spec struct {
ControlPlane Pool
Kubeconfig *secret.Secret `yaml:",omitempty"`
Networking struct {
DNSDomain string
Network string
ServiceCidr orbiter.CIDR
PodCidr orbiter.CIDR
}
Verbose bool
Versions struct {
Kubernetes string
Orbiter string
}
Workers []*Pool
}
func parseDesiredV0(desiredTree *tree.Tree) (*DesiredV0, error) {
desiredKind := &DesiredV0{
Common: *desiredTree.Common,
Spec: Spec{},
}
if err := desiredTree.Original.Decode(desiredKind); err != nil {
return nil, errors.Wrap(err, "parsing desired state failed")
}
return desiredKind, nil
}
func (d *DesiredV0) validate() error {
if d.Spec.ControlPlane.Nodes != 1 && d.Spec.ControlPlane.Nodes != 3 && d.Spec.ControlPlane.Nodes != 5 {
return errors.Errorf("Controlplane nodes can only be scaled to 1, 3 or 5 but desired are %d", d.Spec.ControlPlane.Nodes)
}
if ParseString(d.Spec.Versions.Kubernetes) == Unknown {
return errors.Errorf("Unknown kubernetes version %s", d.Spec.Versions.Kubernetes)
}
if d.Spec.Networking.Network != "cilium" && d.Spec.Networking.Network != "calico" {
return errors.Errorf("Network must eighter be calico or cilium, but got %s", d.Spec.Networking.Network)
}
if err := d.Spec.Networking.ServiceCidr.Validate(); err != nil {
return err
}
if err := d.Spec.Networking.PodCidr.Validate(); err != nil {
return err
}
seenPools := map[string][]string{
d.Spec.ControlPlane.Provider: []string{d.Spec.ControlPlane.Pool},
}
for _, worker := range d.Spec.Workers {
pools, ok := seenPools[worker.Provider]
if !ok {
seenPools[worker.Provider] = []string{worker.Pool}
continue
}
for _, seenPool := range pools {
if seenPool == worker.Pool {
return errors.Errorf("Pool %s from provider %s is used multiple times", worker.Pool, worker.Provider)
}
}
seenPools[worker.Provider] = append(pools, worker.Pool)
}
return nil
}
type Pool struct {
UpdatesDisabled bool
Provider string
Nodes int
Pool string
Taints *Taints `yaml:"taints,omitempty"`
}
type Taint struct {
Key string `yaml:"key"`
Value string `yaml:"value,omitempty"`
Effect core.TaintEffect `yaml:"effect"`
}
type Taints []Taint
func (t *Taints) ToK8sTaints() []core.Taint {
if t == nil {
return nil
}
taints := make([]core.Taint, len(*t))
for idx, taint := range *t {
taints[idx] = core.Taint{
Key: taint.Key,
Value: taint.Value,
Effect: taint.Effect,
}
}
return taints
}