-
Notifications
You must be signed in to change notification settings - Fork 451
/
namespaces.go
111 lines (96 loc) · 4.49 KB
/
namespaces.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
// Copyright (c) 2020 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"
v1beta1constants "github.com/gardener/gardener/pkg/apis/core/v1beta1/constants"
"github.com/gardener/gardener/pkg/client/kubernetes"
"github.com/gardener/gardener/pkg/operation/botanist/component"
"github.com/gardener/gardener/pkg/operation/botanist/component/namespaces"
"github.com/gardener/gardener/pkg/utils/retry"
corev1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
)
// DeploySeedNamespace creates a namespace in the Seed cluster which is used to deploy all the control plane
// components for the Shoot cluster. Moreover, the cloud provider configuration and all the secrets will be
// stored as ConfigMaps/Secrets.
func (b *Botanist) DeploySeedNamespace(ctx context.Context) error {
namespace := &corev1.Namespace{
ObjectMeta: metav1.ObjectMeta{
Name: b.Shoot.SeedNamespace,
},
}
if _, err := controllerutil.CreateOrUpdate(ctx, b.K8sSeedClient.Client(), namespace, func() error {
namespace.Annotations = map[string]string{
v1beta1constants.ShootUID: string(b.Shoot.Info.Status.UID),
v1beta1constants.DeprecatedShootUID: string(b.Shoot.Info.Status.UID),
}
namespace.Labels = map[string]string{
v1beta1constants.GardenRole: v1beta1constants.GardenRoleShoot,
v1beta1constants.LabelSeedProvider: b.Seed.Info.Spec.Provider.Type,
v1beta1constants.LabelShootProvider: b.Shoot.Info.Spec.Provider.Type,
v1beta1constants.LabelNetworkingProvider: b.Shoot.Info.Spec.Networking.Type,
v1beta1constants.LabelBackupProvider: b.Seed.Info.Spec.Provider.Type,
}
if b.Seed.Info.Spec.Backup != nil {
namespace.Labels[v1beta1constants.LabelBackupProvider] = b.Seed.Info.Spec.Backup.Provider
}
return nil
}); err != nil {
return err
}
b.SeedNamespaceObject = namespace
return nil
}
// DeleteSeedNamespace deletes the namespace in the Seed cluster which holds the control plane components. The built-in
// garbage collection in Kubernetes will automatically delete all resources which belong to this namespace. This
// comprises volumes and load balancers as well.
func (b *Botanist) DeleteSeedNamespace(ctx context.Context) error {
namespace := &corev1.Namespace{
ObjectMeta: metav1.ObjectMeta{
Name: b.Shoot.SeedNamespace,
},
}
err := b.K8sSeedClient.Client().Delete(ctx, namespace, kubernetes.DefaultDeleteOptions...)
if apierrors.IsNotFound(err) || apierrors.IsConflict(err) {
return nil
}
return err
}
// WaitUntilSeedNamespaceDeleted waits until the namespace of the Shoot cluster within the Seed cluster is deleted.
func (b *Botanist) WaitUntilSeedNamespaceDeleted(ctx context.Context) error {
return b.waitUntilNamespaceDeleted(ctx, b.Shoot.SeedNamespace)
}
// WaitUntilNamespaceDeleted waits until the <namespace> within the Seed cluster is deleted.
func (b *Botanist) waitUntilNamespaceDeleted(ctx context.Context, namespace string) error {
return retry.UntilTimeout(ctx, 5*time.Second, 900*time.Second, func(ctx context.Context) (done bool, err error) {
if err := b.K8sSeedClient.Client().Get(ctx, client.ObjectKey{Name: namespace}, &corev1.Namespace{}); err != nil {
if apierrors.IsNotFound(err) {
return retry.Ok()
}
return retry.SevereError(err)
}
b.Logger.Infof("Waiting until the namespace '%s' has been cleaned up and deleted in the Seed cluster...", namespace)
return retry.MinorError(fmt.Errorf("namespace %q is not yet cleaned up", namespace))
})
}
// DefaultShootNamespaces returns a deployer for the shoot namespaces.
func (b *Botanist) DefaultShootNamespaces() component.DeployWaiter {
return namespaces.New(b.K8sSeedClient.Client(), b.Shoot.SeedNamespace)
}