/
seed.go
218 lines (192 loc) · 8.41 KB
/
seed.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
// Copyright 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 seed
import (
"context"
"fmt"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
"sigs.k8s.io/controller-runtime/pkg/client"
gardencorev1beta1 "github.com/gardener/gardener/pkg/apis/core/v1beta1"
"github.com/gardener/gardener/pkg/utils"
)
// NewBuilder returns a new Builder.
func NewBuilder() *Builder {
return &Builder{
seedObjectFunc: func(_ context.Context) (*gardencorev1beta1.Seed, error) {
return nil, fmt.Errorf("seed object is required but not set")
},
}
}
// WithSeedObject sets the seedObjectFunc attribute at the Builder.
func (b *Builder) WithSeedObject(seedObject *gardencorev1beta1.Seed) *Builder {
b.seedObjectFunc = func(ctx context.Context) (*gardencorev1beta1.Seed, error) { return seedObject, nil }
return b
}
// WithSeedObjectFrom sets the seedObjectFunc attribute at the Builder after fetching it from the given lister.
func (b *Builder) WithSeedObjectFrom(gardenClient client.Reader, seedName string) *Builder {
b.seedObjectFunc = func(ctx context.Context) (*gardencorev1beta1.Seed, error) {
seed := &gardencorev1beta1.Seed{}
return seed, gardenClient.Get(ctx, client.ObjectKey{Name: seedName}, seed)
}
return b
}
// Build initializes a new Seed object.
func (b *Builder) Build(ctx context.Context) (*Seed, error) {
seed := &Seed{}
seedObject, err := b.seedObjectFunc(ctx)
if err != nil {
return nil, err
}
seed.SetInfo(seedObject)
return seed, nil
}
// GetInfo returns the seed resource of this Seed in a concurrency safe way.
// This method should be used only for reading the data of the returned seed resource. The returned seed
// resource MUST NOT BE MODIFIED (except in test code) since this might interfere with other concurrent reads and writes.
// To properly update the seed resource of this Seed use UpdateInfo or UpdateInfoStatus.
func (s *Seed) GetInfo() *gardencorev1beta1.Seed {
return s.info.Load().(*gardencorev1beta1.Seed)
}
// SetInfo sets the seed resource of this Seed in a concurrency safe way.
// This method is not protected by a mutex and does not update the seed resource in the cluster and so
// should be used only in exceptional situations, or as a convenience in test code. The seed passed as a parameter
// MUST NOT BE MODIFIED after the call to SetInfo (except in test code) since this might interfere with other concurrent reads and writes.
// To properly update the seed resource of this Seed use UpdateInfo or UpdateInfoStatus.
func (s *Seed) SetInfo(seed *gardencorev1beta1.Seed) {
s.info.Store(seed)
}
// UpdateInfo updates the seed resource of this Seed in a concurrency safe way,
// using the given context, client, and mutate function.
// It copies the current seed resource and then uses the copy to patch the resource in the cluster
// using either client.MergeFrom or client.StrategicMergeFrom depending on useStrategicMerge.
// This method is protected by a mutex, so only a single UpdateInfo or UpdateInfoStatus operation can be
// executed at any point in time.
func (s *Seed) UpdateInfo(ctx context.Context, c client.Client, useStrategicMerge bool, f func(*gardencorev1beta1.Seed) error) error {
s.infoMutex.Lock()
defer s.infoMutex.Unlock()
seed := s.info.Load().(*gardencorev1beta1.Seed).DeepCopy()
var patch client.Patch
if useStrategicMerge {
patch = client.StrategicMergeFrom(seed.DeepCopy())
} else {
patch = client.MergeFrom(seed.DeepCopy())
}
if err := f(seed); err != nil {
return err
}
if err := c.Patch(ctx, seed, patch); err != nil {
return err
}
s.info.Store(seed)
return nil
}
// UpdateInfoStatus updates the status of the seed resource of this Seed in a concurrency safe way,
// using the given context, client, and mutate function.
// It copies the current seed resource and then uses the copy to patch the resource in the cluster
// using either client.MergeFrom or client.StrategicMergeFrom depending on useStrategicMerge.
// This method is protected by a mutex, so only a single UpdateInfo or UpdateInfoStatus operation can be
// executed at any point in time.
func (s *Seed) UpdateInfoStatus(ctx context.Context, c client.Client, useStrategicMerge bool, f func(*gardencorev1beta1.Seed) error) error {
s.infoMutex.Lock()
defer s.infoMutex.Unlock()
seed := s.info.Load().(*gardencorev1beta1.Seed).DeepCopy()
var patch client.Patch
if useStrategicMerge {
patch = client.StrategicMergeFrom(seed.DeepCopy())
} else {
patch = client.MergeFrom(seed.DeepCopy())
}
if err := f(seed); err != nil {
return err
}
if err := c.Status().Patch(ctx, seed, patch); err != nil {
return err
}
s.info.Store(seed)
return nil
}
// GetIngressFQDN returns the fully qualified domain name of ingress sub-resource for the Seed cluster. The
// end result is '<subDomain>.<shootName>.<projectName>.<seed-ingress-domain>'.
func (s *Seed) GetIngressFQDN(subDomain string) string {
return fmt.Sprintf("%s.%s", subDomain, s.IngressDomain())
}
// IngressDomain returns the ingress domain for the seed.
func (s *Seed) IngressDomain() string {
seed := s.GetInfo()
if seed.Spec.Ingress != nil {
return seed.Spec.Ingress.Domain
}
return ""
}
// GetValidVolumeSize is to get a valid volume size.
// If the given size is smaller than the minimum volume size permitted by cloud provider on which seed cluster is running, it will return the minimum size.
func (s *Seed) GetValidVolumeSize(size string) string {
seed := s.GetInfo()
if seed.Spec.Volume == nil || seed.Spec.Volume.MinimumSize == nil {
return size
}
qs, err := resource.ParseQuantity(size)
if err == nil && qs.Cmp(*seed.Spec.Volume.MinimumSize) < 0 {
return seed.Spec.Volume.MinimumSize.String()
}
return size
}
// GetLoadBalancerServiceAnnotations returns the load balancer annotations set for the seed if any.
func (s *Seed) GetLoadBalancerServiceAnnotations() map[string]string {
seed := s.GetInfo()
if seed.Spec.Settings != nil && seed.Spec.Settings.LoadBalancerServices != nil {
// return copy of annotations to prevent any accidental mutation by components
return utils.MergeStringMaps(seed.Spec.Settings.LoadBalancerServices.Annotations)
}
return nil
}
// GetLoadBalancerServiceExternalTrafficPolicy indicates the external traffic policy for the seed if any.
func (s *Seed) GetLoadBalancerServiceExternalTrafficPolicy() *corev1.ServiceExternalTrafficPolicyType {
seed := s.GetInfo()
if seed.Spec.Settings != nil && seed.Spec.Settings.LoadBalancerServices != nil {
return seed.Spec.Settings.LoadBalancerServices.ExternalTrafficPolicy
}
return nil
}
// GetZonalLoadBalancerServiceAnnotations returns the zonal load balancer annotations set for the seed if any.
func (s *Seed) GetZonalLoadBalancerServiceAnnotations(zone string) map[string]string {
seed := s.GetInfo()
if seed.Spec.Settings != nil && seed.Spec.Settings.LoadBalancerServices != nil {
for _, zoneSettings := range seed.Spec.Settings.LoadBalancerServices.Zones {
if zoneSettings.Name == zone {
// return copy of annotations to prevent any accidental mutation by components
return utils.MergeStringMaps(zoneSettings.Annotations)
}
}
}
return s.GetLoadBalancerServiceAnnotations()
}
// GetZonalLoadBalancerServiceExternalTrafficPolicy indicates the zonal external traffic policy for the seed if any.
func (s *Seed) GetZonalLoadBalancerServiceExternalTrafficPolicy(zone string) *corev1.ServiceExternalTrafficPolicyType {
seed := s.GetInfo()
if seed.Spec.Settings != nil && seed.Spec.Settings.LoadBalancerServices != nil {
for _, zoneSettings := range seed.Spec.Settings.LoadBalancerServices.Zones {
if zoneSettings.Name == zone {
return zoneSettings.ExternalTrafficPolicy
}
}
}
return s.GetLoadBalancerServiceExternalTrafficPolicy()
}
// IsDualStack checks if the seed is a dual-stack seed.
func (s *Seed) IsDualStack() bool {
seed := s.GetInfo()
return len(seed.Spec.Networks.IPFamilies) == 2
}