-
Notifications
You must be signed in to change notification settings - Fork 462
/
backupentry.go
188 lines (161 loc) · 6.47 KB
/
backupentry.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
// 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 backupentry
import (
"context"
"time"
gardencorev1alpha1 "github.com/gardener/gardener/pkg/apis/core/v1alpha1"
gardencorev1beta1 "github.com/gardener/gardener/pkg/apis/core/v1beta1"
v1beta1constants "github.com/gardener/gardener/pkg/apis/core/v1beta1/constants"
"github.com/gardener/gardener/pkg/controllerutils"
"github.com/gardener/gardener/pkg/extensions"
"github.com/gardener/gardener/pkg/operation/botanist/component"
kutil "github.com/gardener/gardener/pkg/utils/kubernetes"
"github.com/gardener/gardener/pkg/utils/kubernetes/health"
"github.com/sirupsen/logrus"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/controller-runtime/pkg/client"
)
const (
// DefaultInterval is the default interval for retry operations.
DefaultInterval = 5 * time.Second
// DefaultTimeout is the default timeout and defines how long Gardener should wait
// for a successful reconciliation of a BackupEntry resource.
DefaultTimeout = 10 * time.Minute
)
// TimeNow returns the current time. Exposed for testing.
var TimeNow = time.Now
// Values contains the values used to create a BackupEntry resource.
type Values struct {
// Namespace is the namespace of the BackupEntry resource.
Namespace string
// Name is the name of the BackupEntry resource.
Name string
// ShootPurpose is the purpose of the shoot.
ShootPurpose *gardencorev1beta1.ShootPurpose
// OwnerReference is a reference to an owner for BackupEntry resource.
OwnerReference *metav1.OwnerReference
// SeedName is the name of the seed to which the BackupEntry shall be scheduled.
SeedName *string
// BucketName is the name of the bucket in which the BackupEntry shall be reconciled. This value is only used if the
// BackupEntry does not exist yet. Otherwise, the existing `.spec.bucketName` will be kept even if the BucketName in
// these values differs.
BucketName string
}
// New creates a new instance of DeployWaiter for a BackupEntry.
func New(
logger logrus.FieldLogger,
client client.Client,
values *Values,
waitInterval time.Duration,
waitTimeout time.Duration,
) component.DeployMigrateWaiter {
return &backupEntry{
client: client,
logger: logger,
values: values,
waitInterval: waitInterval,
waitTimeout: waitTimeout,
backupEntry: &gardencorev1beta1.BackupEntry{
ObjectMeta: metav1.ObjectMeta{
Name: values.Name,
Namespace: values.Namespace,
},
},
}
}
type backupEntry struct {
values *Values
logger logrus.FieldLogger
client client.Client
waitInterval time.Duration
waitTimeout time.Duration
backupEntry *gardencorev1beta1.BackupEntry
}
// Deploy uses the garden client to create or update the BackupEntry resource in the project namespace in the Garden.
func (b *backupEntry) Deploy(ctx context.Context) error {
var (
bucketName = b.values.BucketName
seedName = b.values.SeedName
)
if err := b.client.Get(ctx, client.ObjectKeyFromObject(b.backupEntry), b.backupEntry); err == nil {
bucketName = b.backupEntry.Spec.BucketName
seedName = b.backupEntry.Spec.SeedName
} else if client.IgnoreNotFound(err) != nil {
return err
}
return b.reconcile(ctx, b.backupEntry, seedName, bucketName, v1beta1constants.GardenerOperationReconcile)
}
// Wait waits until the BackupEntry resource is ready.
func (b *backupEntry) Wait(ctx context.Context) error {
if err := extensions.WaitUntilObjectReadyWithHealthFunction(
ctx,
b.client,
b.logger,
health.CheckBackupEntry,
b.backupEntry,
"BackupEntry",
b.waitInterval,
b.waitTimeout,
b.waitTimeout,
nil,
); err != nil {
b.logger.Error(err)
return err
}
return nil
}
// Migrate uses the garden client to deschedule the BackupEntry from its current seed.
func (b *backupEntry) Migrate(ctx context.Context) error {
_, err := controllerutils.GetAndCreateOrMergePatch(ctx, b.client, b.backupEntry, func() error {
b.backupEntry.Spec.SeedName = b.values.SeedName
return nil
})
return err
}
// WaitMigrate waits until the BackupEntry is migrated
func (b *backupEntry) WaitMigrate(ctx context.Context) error {
return b.Wait(ctx)
}
// Restore uses the garden client to update the BackupEntry and set the name of the new seed to which it shall be scheduled.
// If the BackupEntry was deleted it will be recreated.
func (b *backupEntry) Restore(ctx context.Context, _ *gardencorev1alpha1.ShootState) error {
bucketName := b.values.BucketName
if err := b.client.Get(ctx, kutil.Key(b.values.Namespace, b.values.Name), b.backupEntry); err == nil {
bucketName = b.backupEntry.Spec.BucketName
} else if client.IgnoreNotFound(err) != nil {
return err
}
return b.reconcile(ctx, b.backupEntry, b.values.SeedName, bucketName, v1beta1constants.GardenerOperationRestore)
}
func (b *backupEntry) reconcile(ctx context.Context, backupEntry *gardencorev1beta1.BackupEntry, seedName *string, bucketName string, operation string) error {
_, err := controllerutils.GetAndCreateOrStrategicMergePatch(ctx, b.client, backupEntry, func() error {
metav1.SetMetaDataAnnotation(&backupEntry.ObjectMeta, v1beta1constants.GardenerOperation, operation)
metav1.SetMetaDataAnnotation(&backupEntry.ObjectMeta, v1beta1constants.GardenerTimestamp, TimeNow().UTC().String())
if b.values.ShootPurpose != nil {
metav1.SetMetaDataAnnotation(&backupEntry.ObjectMeta, v1beta1constants.ShootPurpose, string(*b.values.ShootPurpose))
}
if b.values.OwnerReference != nil {
backupEntry.ObjectMeta.OwnerReferences = []metav1.OwnerReference{*b.values.OwnerReference}
}
backupEntry.Spec.BucketName = bucketName
backupEntry.Spec.SeedName = seedName
return nil
})
return err
}
// Destroy is not implemented yet.
func (b *backupEntry) Destroy(_ context.Context) error { return nil }
// WaitCleanup is not implemented yet.
func (b *backupEntry) WaitCleanup(_ context.Context) error { return nil }