This repository has been archived by the owner on Nov 30, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
controller.go
289 lines (249 loc) · 8.08 KB
/
controller.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
package azurecluster
import (
"context"
"time"
"github.com/giantswarm/certs/v3/pkg/certs"
"github.com/giantswarm/k8sclient/v5/pkg/k8sclient"
"github.com/giantswarm/microerror"
"github.com/giantswarm/micrologger"
"github.com/giantswarm/operatorkit/v4/pkg/controller"
"github.com/giantswarm/operatorkit/v4/pkg/resource"
"github.com/giantswarm/operatorkit/v4/pkg/resource/wrapper/metricsresource"
"github.com/giantswarm/operatorkit/v4/pkg/resource/wrapper/retryresource"
"github.com/spf13/viper"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/runtime"
"sigs.k8s.io/cluster-api-provider-azure/api/v1alpha3"
"github.com/giantswarm/azure-operator/v5/client"
"github.com/giantswarm/azure-operator/v5/flag"
"github.com/giantswarm/azure-operator/v5/pkg/credential"
"github.com/giantswarm/azure-operator/v5/pkg/handler/release"
"github.com/giantswarm/azure-operator/v5/pkg/label"
"github.com/giantswarm/azure-operator/v5/pkg/project"
"github.com/giantswarm/azure-operator/v5/service/collector"
"github.com/giantswarm/azure-operator/v5/service/controller/azurecluster/handler/azureclusterconditions"
"github.com/giantswarm/azure-operator/v5/service/controller/azurecluster/handler/azureclusterconfig"
"github.com/giantswarm/azure-operator/v5/service/controller/azurecluster/handler/azureclusteridentity"
"github.com/giantswarm/azure-operator/v5/service/controller/azurecluster/handler/azureclusterupgrade"
"github.com/giantswarm/azure-operator/v5/service/controller/azurecluster/handler/azureconfig"
"github.com/giantswarm/azure-operator/v5/service/controller/azurecluster/handler/subnet"
"github.com/giantswarm/azure-operator/v5/service/controller/controllercontext"
"github.com/giantswarm/azure-operator/v5/service/controller/debugger"
"github.com/giantswarm/azure-operator/v5/service/controller/setting"
)
type ControllerConfig struct {
CredentialProvider credential.Provider
InstallationName string
K8sClient k8sclient.Interface
Logger micrologger.Logger
Flag *flag.Flag
Viper *viper.Viper
Azure setting.Azure
AzureMetricsCollector collector.AzureAPIMetrics
CPAzureClientSet client.AzureClientSet
ProjectName string
RegistryDomain string
Ignition setting.Ignition
OIDC setting.OIDC
SSOPublicKey string
TemplateVersion string
SentryDSN string
}
func NewController(config ControllerConfig) (*controller.Controller, error) {
var err error
var certsSearcher *certs.Searcher
{
c := certs.Config{
K8sClient: config.K8sClient.K8sClient(),
Logger: config.Logger,
}
certsSearcher, err = certs.NewSearcher(c)
if err != nil {
return nil, microerror.Mask(err)
}
}
var resources []resource.Interface
{
resources, err = newAzureClusterResources(config, certsSearcher)
if err != nil {
return nil, microerror.Mask(err)
}
}
var operatorkitController *controller.Controller
{
c := controller.Config{
InitCtx: func(ctx context.Context, obj interface{}) (context.Context, error) {
return controllercontext.NewContext(ctx, controllercontext.Context{}), nil
},
K8sClient: config.K8sClient,
Logger: config.Logger,
// Name is used to compute finalizer names. This results in something
// like operatorkit.giantswarm.io/azure-operator-azurecluster-controller.
Name: project.Name() + "-azurecluster-controller",
NewRuntimeObjectFunc: func() runtime.Object {
return new(v1alpha3.AzureCluster)
},
Resources: resources,
Selector: labels.SelectorFromSet(map[string]string{
label.OperatorVersion: project.Version(),
}),
SentryDSN: config.SentryDSN,
}
operatorkitController, err = controller.New(c)
if err != nil {
return nil, microerror.Mask(err)
}
}
return operatorkitController, nil
}
func newAzureClusterResources(config ControllerConfig, certsSearcher certs.Interface) ([]resource.Interface, error) {
var err error
var clientFactory *client.Factory
{
c := client.FactoryConfig{
AzureAPIMetrics: config.AzureMetricsCollector,
CacheDuration: 30 * time.Minute,
CredentialProvider: config.CredentialProvider,
Logger: config.Logger,
}
clientFactory, err = client.NewFactory(c)
if err != nil {
return nil, microerror.Mask(err)
}
}
var organizationClientFactory client.OrganizationFactory
{
c := client.OrganizationFactoryConfig{
CtrlClient: config.K8sClient.CtrlClient(),
Factory: clientFactory,
Logger: config.Logger,
}
organizationClientFactory = client.NewOrganizationFactory(c)
}
var azureClusterConditionsResource resource.Interface
{
c := azureclusterconditions.Config{
AzureClientsFactory: &organizationClientFactory,
CtrlClient: config.K8sClient.CtrlClient(),
Logger: config.Logger,
}
azureClusterConditionsResource, err = azureclusterconditions.New(c)
if err != nil {
return nil, microerror.Mask(err)
}
}
var azureClusterConfigResource *azureclusterconfig.Resource
{
c := azureclusterconfig.Config{
CtrlClient: config.K8sClient.CtrlClient(),
Logger: config.Logger,
}
azureClusterConfigResource, err = azureclusterconfig.New(c)
if err != nil {
return nil, microerror.Mask(err)
}
}
var azureClusterUpgradeResource resource.Interface
{
c := azureclusterupgrade.Config{
CtrlClient: config.K8sClient.CtrlClient(),
Logger: config.Logger,
}
azureClusterUpgradeResource, err = azureclusterupgrade.New(c)
if err != nil {
return nil, microerror.Mask(err)
}
}
var azureConfigResource *azureconfig.Resource
{
c := azureconfig.Config{
CtrlClient: config.K8sClient.CtrlClient(),
Logger: config.Logger,
APIServerSecurePort: config.Viper.GetInt(config.Flag.Service.Cluster.Kubernetes.API.SecurePort),
Calico: azureconfig.CalicoConfig{
CIDRSize: config.Viper.GetInt(config.Flag.Service.Cluster.Calico.CIDR),
MTU: config.Viper.GetInt(config.Flag.Service.Cluster.Calico.MTU),
Subnet: config.Viper.GetString(config.Flag.Service.Cluster.Calico.Subnet),
},
ClusterIPRange: config.Viper.GetString(config.Flag.Service.Cluster.Kubernetes.API.ClusterIPRange),
EtcdPrefix: config.Viper.GetString(config.Flag.Service.Cluster.Etcd.Prefix),
ManagementClusterResourceGroup: config.Viper.GetString(config.Flag.Service.Azure.HostCluster.ResourceGroup),
}
azureConfigResource, err = azureconfig.New(c)
if err != nil {
return nil, microerror.Mask(err)
}
}
var releaseResource resource.Interface
{
c := release.Config{
K8sClient: config.K8sClient,
Logger: config.Logger,
}
releaseResource, err = release.New(c)
if err != nil {
return nil, microerror.Mask(err)
}
}
var newDebugger *debugger.Debugger
{
c := debugger.Config{
Logger: config.Logger,
}
newDebugger, err = debugger.New(c)
if err != nil {
return nil, microerror.Mask(err)
}
}
var subnetResource resource.Interface
{
c := subnet.Config{
AzureClientsFactory: organizationClientFactory,
CtrlClient: config.K8sClient.CtrlClient(),
Debugger: newDebugger,
Logger: config.Logger,
}
subnetResource, err = subnet.New(c)
if err != nil {
return nil, microerror.Mask(err)
}
}
var azureclusteridentityResource resource.Interface
{
c := azureclusteridentity.Config{
AzureClientsFactory: organizationClientFactory,
CtrlClient: config.K8sClient.CtrlClient(),
Logger: config.Logger,
}
azureclusteridentityResource, err = azureclusteridentity.New(c)
if err != nil {
return nil, microerror.Mask(err)
}
}
resources := []resource.Interface{
azureClusterUpgradeResource,
azureClusterConditionsResource,
releaseResource,
azureclusteridentityResource,
azureClusterConfigResource,
azureConfigResource,
subnetResource,
}
{
c := retryresource.WrapConfig{
Logger: config.Logger,
}
resources, err = retryresource.Wrap(resources, c)
if err != nil {
return nil, microerror.Mask(err)
}
}
{
c := metricsresource.WrapConfig{}
resources, err = metricsresource.Wrap(resources, c)
if err != nil {
return nil, microerror.Mask(err)
}
}
return resources, nil
}