-
Notifications
You must be signed in to change notification settings - Fork 277
/
cluster.go
119 lines (101 loc) · 4.3 KB
/
cluster.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
package tinkerbell
import (
"github.com/aws/eks-anywhere/pkg/api/v1alpha1"
"github.com/aws/eks-anywhere/pkg/cluster"
)
// ClusterSpec represents a cluster configuration for Tinkerbell provisioning.
type ClusterSpec struct {
*cluster.Spec
// DatacenterConfig configured in the cluster configuration YAML.
DatacenterConfig *v1alpha1.TinkerbellDatacenterConfig
// MachineConfigs configured in the cluster configuration YAML whether they're used or not.
MachineConfigs map[string]*v1alpha1.TinkerbellMachineConfig
}
// NewClusterSpec creates a ClusterSpec instance.
func NewClusterSpec(
clusterSpec *cluster.Spec,
machineConfigs map[string]*v1alpha1.TinkerbellMachineConfig,
datacenterConfig *v1alpha1.TinkerbellDatacenterConfig,
) *ClusterSpec {
return &ClusterSpec{
Spec: clusterSpec,
DatacenterConfig: datacenterConfig,
MachineConfigs: machineConfigs,
}
}
// ControlPlaneMachineConfig retrieves the TinkerbellMachineConfig referenced by the cluster
// control plane machine reference.
func (s *ClusterSpec) ControlPlaneMachineConfig() *v1alpha1.TinkerbellMachineConfig {
return s.MachineConfigs[s.Cluster.Spec.ControlPlaneConfiguration.MachineGroupRef.Name]
}
// ControlPlaneConfiguration retrieves the control plane configuration from s.
func (s *ClusterSpec) ControlPlaneConfiguration() *v1alpha1.ControlPlaneConfiguration {
return &s.Cluster.Spec.ControlPlaneConfiguration
}
// HasExternalEtcd returns true if there is an external etcd configuration.
func (s *ClusterSpec) HasExternalEtcd() bool {
return s.Spec.Cluster.Spec.ExternalEtcdConfiguration != nil
}
// ExternalEtcdConfiguration returns the etcd configuration. The configuration may be nil. Consumers
// should check if external etcd configuration is present using HasExternalEtcd().
func (s *ClusterSpec) ExternalEtcdConfiguration() *v1alpha1.ExternalEtcdConfiguration {
return s.Cluster.Spec.ExternalEtcdConfiguration
}
// ExternalEtcdMachineConfig retrieves the TinkerbellMachineConfig referenced by the cluster etcd machine
// reference.
func (s *ClusterSpec) ExternalEtcdMachineConfig() *v1alpha1.TinkerbellMachineConfig {
if !s.HasExternalEtcd() {
return nil
}
return s.MachineConfigs[s.Cluster.Spec.ExternalEtcdConfiguration.MachineGroupRef.Name]
}
// WorkerNodeGroupConfigurations retrieves all worker node group configurations in s.
func (s *ClusterSpec) WorkerNodeGroupConfigurations() []v1alpha1.WorkerNodeGroupConfiguration {
return s.Cluster.Spec.WorkerNodeGroupConfigurations
}
// WorkerNodeGroupMachineConfig retrieves the machine group associated with conf.
func (s *ClusterSpec) WorkerNodeGroupMachineConfig(conf v1alpha1.WorkerNodeGroupConfiguration) *v1alpha1.TinkerbellMachineConfig {
return s.MachineConfigs[conf.MachineGroupRef.Name]
}
// ClusterSpecAssertion makes an assertion on spec.
type ClusterSpecAssertion func(spec *ClusterSpec) error
// ClusterSpecValidator is composed of a set of ClusterSpecAssertions to be run on a ClusterSpec
// instance.
type ClusterSpecValidator []ClusterSpecAssertion
// Register registers assertions with v.
func (v *ClusterSpecValidator) Register(assertions ...ClusterSpecAssertion) {
*v = append(*v, assertions...)
}
// Validate validates spec with all assertions registered on v.
func (v *ClusterSpecValidator) Validate(spec *ClusterSpec) error {
for _, a := range *v {
if err := a(spec); err != nil {
return err
}
}
return nil
}
// NewClusterSpecValidator creates a ClusterSpecValidator instance with a set of default assertions.
// Any assertions passed will be registered in addition to the default assertions.
func NewClusterSpecValidator(assertions ...ClusterSpecAssertion) *ClusterSpecValidator {
var v ClusterSpecValidator
// Register mandatory assertions. If an assertion becomes optional dependent on context move it
// to a New* func and register it dynamically. See assert.go for examples.
v.Register(
AssertK8SVersionNot120,
AssertDatacenterConfigValid,
AssertControlPlaneMachineRefExists,
AssertEtcdMachineRefExists,
AssertWorkerNodeGroupMachineRefsExists,
AssertMachineConfigsValid,
AssertMachineConfigNamespaceMatchesDatacenterConfig,
AssertOsFamilyValid,
AssertOSImageURL,
AssertTinkerbellIPAndControlPlaneIPNotSame,
AssertHookRetrievableWithoutProxy,
AssertUpgradeRolloutStrategyValid,
AssertAutoScalerDisabledForInPlace,
)
v.Register(assertions...)
return &v
}