-
Notifications
You must be signed in to change notification settings - Fork 280
/
workers.go
118 lines (95 loc) Β· 4.49 KB
/
workers.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
package nutanix
import (
"context"
"fmt"
"time"
"github.com/go-logr/logr"
"github.com/nutanix-cloud-native/cluster-api-provider-nutanix/api/v1beta1"
"k8s.io/apimachinery/pkg/api/equality"
"github.com/aws/eks-anywhere/pkg/api/v1alpha1"
"github.com/aws/eks-anywhere/pkg/clients/kubernetes"
"github.com/aws/eks-anywhere/pkg/cluster"
"github.com/aws/eks-anywhere/pkg/clusterapi"
capiyaml "github.com/aws/eks-anywhere/pkg/clusterapi/yaml"
"github.com/aws/eks-anywhere/pkg/providers/common"
"github.com/aws/eks-anywhere/pkg/yamlutil"
)
type (
// Workers represents the nutanix specific CAPI spec for worker nodes.
Workers = clusterapi.Workers[*v1beta1.NutanixMachineTemplate]
workersBuilder = capiyaml.WorkersBuilder[*v1beta1.NutanixMachineTemplate]
)
// WorkersSpec generates a nutanix specific CAPI spec for an eks-a cluster worker nodes.
// It talks to the cluster with a client to detect changes in immutable objects and generates new
// names for them.
func WorkersSpec(ctx context.Context, logger logr.Logger, client kubernetes.Client, spec *cluster.Spec) (*Workers, error) {
ndcs := spec.NutanixDatacenter.Spec
machineConfigs := spec.NutanixMachineConfigs
wnmcs := make(map[string]v1alpha1.NutanixMachineConfigSpec, len(spec.Cluster.Spec.WorkerNodeGroupConfigurations))
for _, machineConfig := range machineConfigs {
machineConfig.SetDefaults()
}
creds := GetCredsFromEnv()
templateBuilder := NewNutanixTemplateBuilder(&ndcs, nil, nil, wnmcs, creds, time.Now)
workloadTemplateNames, kubeadmconfigTemplateNames := getTemplateNames(spec, templateBuilder, machineConfigs)
workersYaml, err := templateBuilder.GenerateCAPISpecWorkers(spec, workloadTemplateNames, kubeadmconfigTemplateNames)
if err != nil {
return nil, err
}
workers, err := parseWorkersYaml(logger, workersYaml)
if err != nil {
return nil, fmt.Errorf("parsing nutanix CAPI workers yaml: %w", err)
}
if err = workers.UpdateImmutableObjectNames(ctx, client, getMachineTemplate, machineTemplateEquals); err != nil {
return nil, fmt.Errorf("updating nutanix worker immutable object names: %w", err)
}
return workers, nil
}
func getTemplateNames(spec *cluster.Spec, templateBuilder *TemplateBuilder, machineConfigs map[string]*v1alpha1.NutanixMachineConfig) (map[string]string, map[string]string) {
workloadTemplateNames := make(map[string]string, len(spec.Cluster.Spec.WorkerNodeGroupConfigurations))
kubeadmconfigTemplateNames := make(map[string]string, len(spec.Cluster.Spec.WorkerNodeGroupConfigurations))
for _, workerNodeGroupConfiguration := range spec.Cluster.Spec.WorkerNodeGroupConfigurations {
workloadTemplateNames[workerNodeGroupConfiguration.Name] = common.WorkerMachineTemplateName(spec.Cluster.Name, workerNodeGroupConfiguration.Name, templateBuilder.now)
kubeadmconfigTemplateNames[workerNodeGroupConfiguration.Name] = common.KubeadmConfigTemplateName(spec.Cluster.Name, workerNodeGroupConfiguration.Name, templateBuilder.now)
templateBuilder.workerNodeGroupMachineSpecs[workerNodeGroupConfiguration.MachineGroupRef.Name] = machineConfigs[workerNodeGroupConfiguration.MachineGroupRef.Name].Spec
}
return workloadTemplateNames, kubeadmconfigTemplateNames
}
func parseWorkersYaml(logger logr.Logger, workersYaml []byte) (*Workers, error) {
parser, builder, err := newWorkersParserAndBuilder(logger)
if err != nil {
return nil, err
}
if err = parser.Parse(workersYaml, builder); err != nil {
return nil, fmt.Errorf("parsing nutanix CAPI workers yaml: %w", err)
}
return builder.Workers, nil
}
func newWorkersParserAndBuilder(logger logr.Logger) (*yamlutil.Parser, *workersBuilder, error) {
parser, builder, err := capiyaml.NewWorkersParserAndBuilder(
logger,
machineTemplateMapping(),
)
if err != nil {
return nil, nil, fmt.Errorf("building nutanix workers parser and builder: %w", err)
}
return parser, builder, nil
}
func machineTemplateMapping() yamlutil.Mapping[*v1beta1.NutanixMachineTemplate] {
return yamlutil.NewMapping(
"NutanixMachineTemplate",
func() *v1beta1.NutanixMachineTemplate {
return &v1beta1.NutanixMachineTemplate{}
},
)
}
func getMachineTemplate(ctx context.Context, client kubernetes.Client, name, namespace string) (*v1beta1.NutanixMachineTemplate, error) {
m := &v1beta1.NutanixMachineTemplate{}
if err := client.Get(ctx, name, namespace, m); err != nil {
return nil, fmt.Errorf("reading nutanixMachineTemplate: %w", err)
}
return m, nil
}
func machineTemplateEquals(new, old *v1beta1.NutanixMachineTemplate) bool {
return equality.Semantic.DeepDerivative(new.Spec, old.Spec)
}