/
actuator_reconcile.go
263 lines (222 loc) · 9.8 KB
/
actuator_reconcile.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
// Copyright (c) 2019 SAP SE or an SAP affiliate company. All rights reserved. This file is licensed under the Apache Software License, v. 2 except as noted otherwise in the LICENSE file
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package infrastructure
import (
"context"
"fmt"
"strings"
extensionscontroller "github.com/gardener/gardener/extensions/pkg/controller"
"github.com/gardener/gardener/extensions/pkg/terraformer"
"github.com/gardener/gardener/extensions/pkg/util"
extensionsv1alpha1 "github.com/gardener/gardener/pkg/apis/extensions/v1alpha1"
"github.com/go-logr/logr"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/controller-runtime/pkg/client"
"github.com/gardener/gardener-extension-provider-openstack/pkg/apis/openstack/helper"
openstackv1alpha1 "github.com/gardener/gardener-extension-provider-openstack/pkg/apis/openstack/v1alpha1"
"github.com/gardener/gardener-extension-provider-openstack/pkg/controller/infrastructure/infraflow"
"github.com/gardener/gardener-extension-provider-openstack/pkg/controller/infrastructure/infraflow/shared"
"github.com/gardener/gardener-extension-provider-openstack/pkg/internal"
"github.com/gardener/gardener-extension-provider-openstack/pkg/internal/infrastructure"
"github.com/gardener/gardener-extension-provider-openstack/pkg/openstack"
openstackclient "github.com/gardener/gardener-extension-provider-openstack/pkg/openstack/client"
)
func (a *actuator) Reconcile(ctx context.Context, log logr.Logger, infra *extensionsv1alpha1.Infrastructure, cluster *extensionscontroller.Cluster) error {
flowState, err := a.getStateFromInfraStatus(ctx, infra)
if err != nil {
return err
}
if flowState != nil {
return a.reconcileWithFlow(ctx, log, infra, cluster, flowState)
}
if a.shouldUseFlow(infra, cluster) {
flowState, err = a.migrateFromTerraformerState(ctx, log, infra)
if err != nil {
return err
}
return a.reconcileWithFlow(ctx, log, infra, cluster, flowState)
}
return a.reconcileWithTerraformer(ctx, log, infra, cluster, terraformer.StateConfigMapInitializerFunc(terraformer.CreateState))
}
func (a *actuator) shouldUseFlow(infrastructure *extensionsv1alpha1.Infrastructure, cluster *extensionscontroller.Cluster) bool {
return (infrastructure.Annotations != nil && strings.EqualFold(infrastructure.Annotations[AnnotationKeyUseFlow], "true")) ||
(cluster.Shoot != nil && cluster.Shoot.Annotations != nil && strings.EqualFold(cluster.Shoot.Annotations[AnnotationKeyUseFlow], "true"))
}
func (a *actuator) getStateFromInfraStatus(_ context.Context, infrastructure *extensionsv1alpha1.Infrastructure) (*infraflow.PersistentState, error) {
if infrastructure.Status.State != nil {
return infraflow.NewPersistentStateFromJSON(infrastructure.Status.State.Raw)
}
return nil, nil
}
func (a *actuator) migrateFromTerraformerState(ctx context.Context, log logr.Logger, infra *extensionsv1alpha1.Infrastructure) (*infraflow.PersistentState, error) {
log.Info("starting terraform state migration")
// just explore the infrastructure objects by starting with an empty state
state := infraflow.NewPersistentState()
if err := a.updateStatusState(ctx, infra, state); err != nil {
return nil, fmt.Errorf("updating status state failed: %w", err)
}
log.Info("terraform state migrated successfully")
return state, nil
}
func (a *actuator) reconcileWithFlow(ctx context.Context, log logr.Logger, infra *extensionsv1alpha1.Infrastructure,
cluster *extensionscontroller.Cluster, oldState *infraflow.PersistentState) error {
log.Info("reconcileWithFlow")
flowContext, err := a.createFlowContext(ctx, log, infra, cluster, oldState)
if err != nil {
return err
}
if err = flowContext.Reconcile(ctx); err != nil {
_ = flowContext.PersistState(ctx, true)
return util.DetermineError(err, helper.KnownCodes)
}
return flowContext.PersistState(ctx, true)
}
func (a *actuator) createFlowContext(ctx context.Context, log logr.Logger,
infra *extensionsv1alpha1.Infrastructure, cluster *extensionscontroller.Cluster,
oldState *infraflow.PersistentState) (*infraflow.FlowContext, error) {
cloudProfileConfig, err := helper.CloudProfileConfigFromCluster(cluster)
if err != nil {
return nil, err
}
if oldState.MigratedFromTerraform() && !oldState.TerraformCleanedUp() {
err := a.cleanupTerraformerResources(ctx, log, infra)
if err != nil {
return nil, fmt.Errorf("cleaning up terraformer resources failed: %w", err)
}
oldState.SetTerraformCleanedUp()
if err := a.updateStatusState(ctx, infra, oldState); err != nil {
return nil, fmt.Errorf("updating status state failed: %w", err)
}
}
config, err := helper.InfrastructureConfigFromInfrastructure(infra)
if err != nil {
return nil, err
}
credentials, err := openstack.GetCredentials(ctx, a.client, infra.Spec.SecretRef, false)
if err != nil {
return nil, fmt.Errorf("could not get Openstack credentials: %w", err)
}
clientFactory, err := openstackclient.NewOpenstackClientFromCredentials(credentials)
if err != nil {
return nil, err
}
infraObjectKey := client.ObjectKey{
Namespace: infra.Namespace,
Name: infra.Name,
}
persistor := func(ctx context.Context, flatState shared.FlatMap) error {
state := infraflow.NewPersistentStateFromFlatMap(flatState)
infra := &extensionsv1alpha1.Infrastructure{}
if err := a.client.Get(ctx, infraObjectKey, infra); err != nil {
return err
}
return a.updateStatusState(ctx, infra, state)
}
var oldFlatState shared.FlatMap
if oldState != nil {
if valid, err := oldState.HasValidVersion(); !valid {
return nil, err
}
oldFlatState = oldState.ToFlatMap()
}
return infraflow.NewFlowContext(log, clientFactory, infra, config, cloudProfileConfig, oldFlatState, persistor)
}
func (a *actuator) updateStatusState(ctx context.Context, infra *extensionsv1alpha1.Infrastructure, state *infraflow.PersistentState) error {
status, err := computeProviderStatusFromFlowState(state)
if err != nil {
return err
}
stateBytes, err := state.ToJSON()
if err != nil {
return err
}
return a.updateProviderStatus(ctx, infra, status, stateBytes)
}
func (a *actuator) cleanupTerraformerResources(ctx context.Context, log logr.Logger, infra *extensionsv1alpha1.Infrastructure) error {
credentials, err := openstack.GetCredentials(ctx, a.client, infra.Spec.SecretRef, false)
if err != nil {
return err
}
tf, err := internal.NewTerraformerWithAuth(log, a.restConfig, infrastructure.TerraformerPurpose, infra, credentials, a.disableProjectedTokenMount)
if err != nil {
return fmt.Errorf("could not create terraformer object: %w", err)
}
if err := tf.CleanupConfiguration(ctx); err != nil {
return err
}
return tf.RemoveTerraformerFinalizerFromConfig(ctx)
}
func (a *actuator) reconcileWithTerraformer(ctx context.Context, log logr.Logger, infra *extensionsv1alpha1.Infrastructure, cluster *extensionscontroller.Cluster, stateInitializer terraformer.StateConfigMapInitializer) error {
config, err := helper.InfrastructureConfigFromInfrastructure(infra)
if err != nil {
return err
}
terraformFiles, err := infrastructure.RenderTerraformerTemplate(infra, config, cluster)
if err != nil {
return err
}
// need to know if application credentials are used
credentials, err := openstack.GetCredentials(ctx, a.client, infra.Spec.SecretRef, false)
if err != nil {
return err
}
tf, err := internal.NewTerraformerWithAuth(log, a.restConfig, infrastructure.TerraformerPurpose, infra, credentials, a.disableProjectedTokenMount)
if err != nil {
return util.DetermineError(err, helper.KnownCodes)
}
if err := tf.
InitializeWith(ctx, terraformer.DefaultInitializer(a.client, terraformFiles.Main, terraformFiles.Variables, terraformFiles.TFVars, stateInitializer)).
Apply(ctx); err != nil {
return util.DetermineError(fmt.Errorf("failed to apply the terraform config: %w", err), helper.KnownCodes)
}
return a.updateProviderStatusWithTerraformer(ctx, tf, infra, config)
}
func computeProviderStatusFromFlowState(state *infraflow.PersistentState) (*openstackv1alpha1.InfrastructureStatus, error) {
if len(state.Data) == 0 {
return nil, nil
}
status := &openstackv1alpha1.InfrastructureStatus{
TypeMeta: metav1.TypeMeta{
APIVersion: openstackv1alpha1.SchemeGroupVersion.String(),
Kind: "InfrastructureStatus",
},
}
status.Networks.ID = shared.ValidValue(state.Data[infraflow.IdentifierNetwork])
status.Networks.Name = shared.ValidValue(state.Data[infraflow.NameNetwork])
status.Networks.Router.ID = shared.ValidValue(state.Data[infraflow.IdentifierRouter])
status.Networks.Router.IP = shared.ValidValue(state.Data[infraflow.RouterIP])
status.Networks.FloatingPool.ID = shared.ValidValue(state.Data[infraflow.IdentifierFloatingNetwork])
status.Networks.FloatingPool.Name = shared.ValidValue(state.Data[infraflow.NameFloatingNetwork])
subnetID := shared.ValidValue(state.Data[infraflow.IdentifierSubnet])
if subnetID != "" {
status.Networks.Subnets = []openstackv1alpha1.Subnet{
{
Purpose: openstackv1alpha1.PurposeNodes,
ID: subnetID,
},
}
}
secGroupID := shared.ValidValue(state.Data[infraflow.IdentifierSecGroup])
if secGroupID != "" {
status.SecurityGroups = []openstackv1alpha1.SecurityGroup{
{
Purpose: openstackv1alpha1.PurposeNodes,
ID: secGroupID,
Name: shared.ValidValue(state.Data[infraflow.NameSecGroup]),
},
}
}
status.Node.KeyName = shared.ValidValue(state.Data[infraflow.NameKeyPair])
return status, nil
}