-
Notifications
You must be signed in to change notification settings - Fork 81
/
actuator.go
151 lines (124 loc) · 4.29 KB
/
actuator.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
// SPDX-FileCopyrightText: 2024 SAP SE or an SAP affiliate company and Gardener contributors
//
// SPDX-License-Identifier: Apache-2.0
package worker
import (
"context"
"fmt"
extensionscontroller "github.com/gardener/gardener/extensions/pkg/controller"
"github.com/gardener/gardener/extensions/pkg/controller/worker"
"github.com/gardener/gardener/extensions/pkg/controller/worker/genericactuator"
"github.com/gardener/gardener/extensions/pkg/util"
gardencorev1beta1 "github.com/gardener/gardener/pkg/apis/core/v1beta1"
extensionsv1alpha1 "github.com/gardener/gardener/pkg/apis/extensions/v1alpha1"
gardener "github.com/gardener/gardener/pkg/client/kubernetes"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/serializer"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/cluster"
"sigs.k8s.io/controller-runtime/pkg/manager"
api "github.com/gardener/gardener-extension-provider-openstack/pkg/apis/openstack"
"github.com/gardener/gardener-extension-provider-openstack/pkg/apis/openstack/helper"
openstackclient "github.com/gardener/gardener-extension-provider-openstack/pkg/openstack/client"
)
type delegateFactory struct {
seedClient client.Client
restConfig *rest.Config
scheme *runtime.Scheme
gardenReader client.Reader
}
// NewActuator creates a new Actuator that updates the status of the handled WorkerPoolConfigs.
func NewActuator(mgr manager.Manager, gardenCluster cluster.Cluster) worker.Actuator {
var (
workerDelegate = &delegateFactory{
seedClient: mgr.GetClient(),
restConfig: mgr.GetConfig(),
scheme: mgr.GetScheme(),
}
)
return genericactuator.NewActuator(
mgr,
gardenCluster,
workerDelegate,
func(err error) []gardencorev1beta1.ErrorCode {
return util.DetermineErrorCodes(err, helper.KnownCodes)
},
)
}
func (d *delegateFactory) WorkerDelegate(ctx context.Context, worker *extensionsv1alpha1.Worker, cluster *extensionscontroller.Cluster) (genericactuator.WorkerDelegate, error) {
clientset, err := kubernetes.NewForConfig(d.restConfig)
if err != nil {
return nil, err
}
serverVersion, err := clientset.Discovery().ServerVersion()
if err != nil {
return nil, err
}
seedChartApplier, err := gardener.NewChartApplierForConfig(d.restConfig)
if err != nil {
return nil, err
}
cloudProfileConfig, err := helper.CloudProfileConfigFromCluster(cluster)
if err != nil {
return nil, err
}
keyStoneURL, err := helper.FindKeyStoneURL(cloudProfileConfig.KeyStoneURLs, cloudProfileConfig.KeyStoneURL, worker.Spec.Region)
if err != nil {
return nil, err
}
openstackClient, err := openstackclient.NewOpenStackClientFromSecretRef(ctx, d.seedClient, worker.Spec.SecretRef, &keyStoneURL)
if err != nil {
return nil, fmt.Errorf("failed to create openstack seedClient: %w", err)
}
return NewWorkerDelegate(
d.seedClient,
d.scheme,
seedChartApplier,
serverVersion.GitVersion,
worker,
cluster,
openstackClient,
)
}
type workerDelegate struct {
seedClient client.Client
scheme *runtime.Scheme
decoder runtime.Decoder
seedChartApplier gardener.ChartApplier
serverVersion string
cloudProfileConfig *api.CloudProfileConfig
cluster *extensionscontroller.Cluster
worker *extensionsv1alpha1.Worker
machineClasses []map[string]interface{}
machineDeployments worker.MachineDeployments
machineImages []api.MachineImage
openstackClient openstackclient.Factory
}
// NewWorkerDelegate creates a new context for a worker reconciliation.
func NewWorkerDelegate(
seedClient client.Client,
scheme *runtime.Scheme,
seedChartApplier gardener.ChartApplier,
serverVersion string,
worker *extensionsv1alpha1.Worker,
cluster *extensionscontroller.Cluster,
openstackClient openstackclient.Factory,
) (genericactuator.WorkerDelegate, error) {
config, err := helper.CloudProfileConfigFromCluster(cluster)
if err != nil {
return nil, err
}
return &workerDelegate{
seedClient: seedClient,
scheme: scheme,
decoder: serializer.NewCodecFactory(scheme, serializer.EnableStrict).UniversalDecoder(),
seedChartApplier: seedChartApplier,
serverVersion: serverVersion,
cloudProfileConfig: config,
cluster: cluster,
worker: worker,
openstackClient: openstackClient,
}, nil
}