-
Notifications
You must be signed in to change notification settings - Fork 451
/
infrastructure.go
163 lines (146 loc) · 6.11 KB
/
infrastructure.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
// Copyright (c) 2018 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 botanist
import (
"context"
"fmt"
"time"
gardencorev1beta1 "github.com/gardener/gardener/pkg/apis/core/v1beta1"
v1beta1constants "github.com/gardener/gardener/pkg/apis/core/v1beta1/constants"
gardencorev1beta1helper "github.com/gardener/gardener/pkg/apis/core/v1beta1/helper"
extensionsv1alpha1 "github.com/gardener/gardener/pkg/apis/extensions/v1alpha1"
"github.com/gardener/gardener/pkg/controllerutils"
"github.com/gardener/gardener/pkg/operation/common"
"github.com/gardener/gardener/pkg/utils/secrets"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
)
// InfrastructureDefaultTimeout is the default timeout and defines how long Gardener should wait
// for a successful reconciliation of an infrastructure resource.
const InfrastructureDefaultTimeout = 5 * time.Minute
// DeployInfrastructure creates the `Infrastructure` extension resource in the shoot namespace in the seed
// cluster. Gardener waits until an external controller did reconcile the cluster successfully.
func (b *Botanist) DeployInfrastructure(ctx context.Context) error {
var (
operation = v1beta1constants.GardenerOperationReconcile
lastOperation = b.Shoot.Info.Status.LastOperation
creationPhase = lastOperation != nil && lastOperation.Type == gardencorev1beta1.LastOperationTypeCreate
shootIsWakingUp = !gardencorev1beta1helper.HibernationIsEnabled(b.Shoot.Info) && b.Shoot.Info.Status.IsHibernated
restorePhase = b.isRestorePhase()
requestInfrastructureOperation = creationPhase || shootIsWakingUp || restorePhase || controllerutils.HasTask(b.Shoot.Info.Annotations, common.ShootTaskDeployInfrastructure)
infrastructure = &extensionsv1alpha1.Infrastructure{
ObjectMeta: metav1.ObjectMeta{
Name: b.Shoot.Info.Name,
Namespace: b.Shoot.SeedNamespace,
},
}
providerConfig *runtime.RawExtension
)
if cfg := b.Shoot.Info.Spec.Provider.InfrastructureConfig; cfg != nil {
providerConfig = &runtime.RawExtension{
Raw: cfg.Raw,
}
}
if restorePhase {
operation = v1beta1constants.GardenerOperationWaitForState
}
_, err := controllerutil.CreateOrUpdate(ctx, b.K8sSeedClient.Client(), infrastructure, func() error {
if requestInfrastructureOperation {
metav1.SetMetaDataAnnotation(&infrastructure.ObjectMeta, v1beta1constants.GardenerOperation, operation)
metav1.SetMetaDataAnnotation(&infrastructure.ObjectMeta, v1beta1constants.GardenerTimestamp, time.Now().UTC().String())
}
infrastructure.Spec = extensionsv1alpha1.InfrastructureSpec{
DefaultSpec: extensionsv1alpha1.DefaultSpec{
Type: b.Shoot.Info.Spec.Provider.Type,
ProviderConfig: providerConfig,
},
Region: b.Shoot.Info.Spec.Region,
SSHPublicKey: b.Secrets[v1beta1constants.SecretNameSSHKeyPair].Data[secrets.DataKeySSHAuthorizedKeys],
SecretRef: corev1.SecretReference{
Name: v1beta1constants.SecretNameCloudProvider,
Namespace: infrastructure.Namespace,
},
}
return nil
})
if err != nil {
return err
}
if restorePhase {
return b.restoreExtensionObject(ctx, b.K8sSeedClient.Client(), infrastructure, extensionsv1alpha1.InfrastructureResource)
}
return nil
}
// DestroyInfrastructure deletes the `Infrastructure` extension resource in the shoot namespace in the seed cluster,
// and it waits for a maximum of 10m until it is deleted.
func (b *Botanist) DestroyInfrastructure(ctx context.Context) error {
return common.DeleteExtensionCR(
ctx,
b.K8sSeedClient.Client(),
func() extensionsv1alpha1.Object { return &extensionsv1alpha1.Infrastructure{} },
b.Shoot.SeedNamespace,
b.Shoot.Info.Name,
)
}
// WaitUntilInfrastructureReady waits until the infrastructure resource has been reconciled successfully.
func (b *Botanist) WaitUntilInfrastructureReady(ctx context.Context) error {
return common.WaitUntilExtensionCRReady(
ctx,
b.K8sSeedClient.Client(),
b.Logger,
func() runtime.Object { return &extensionsv1alpha1.Infrastructure{} },
"Infrastructure",
b.Shoot.SeedNamespace,
b.Shoot.Info.Name,
DefaultInterval,
DefaultSevereThreshold,
InfrastructureDefaultTimeout,
func(obj runtime.Object) error {
infrastructure, ok := obj.(*extensionsv1alpha1.Infrastructure)
if !ok {
return fmt.Errorf("expected extensionsv1alpha1.Infrastructure but got %T", infrastructure)
}
if infrastructure.Status.ProviderStatus != nil {
b.Shoot.InfrastructureStatus = infrastructure.Status.ProviderStatus.Raw
}
if infrastructure.Status.NodesCIDR != nil {
shootCopy := b.Shoot.Info.DeepCopy()
if _, err := controllerutil.CreateOrUpdate(ctx, b.K8sGardenClient.Client(), shootCopy, func() error {
shootCopy.Spec.Networking.Nodes = infrastructure.Status.NodesCIDR
return nil
}); err != nil {
return err
}
b.Shoot.Info = shootCopy
}
return nil
},
)
}
// WaitUntilInfrastructureDeleted waits until the infrastructure resource has been deleted.
func (b *Botanist) WaitUntilInfrastructureDeleted(ctx context.Context) error {
return common.WaitUntilExtensionCRDeleted(
ctx,
b.K8sSeedClient.Client(),
b.Logger,
func() extensionsv1alpha1.Object { return &extensionsv1alpha1.Infrastructure{} },
"Infrastructure",
b.Shoot.SeedNamespace,
b.Shoot.Info.Name,
DefaultInterval,
InfrastructureDefaultTimeout,
)
}