-
Notifications
You must be signed in to change notification settings - Fork 453
/
system.go
206 lines (180 loc) · 7.6 KB
/
system.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
// Copyright 2022 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 system
import (
"context"
"fmt"
"time"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
schedulingv1 "k8s.io/api/scheduling/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/utils/ptr"
"sigs.k8s.io/controller-runtime/pkg/client"
gardencorev1beta1 "github.com/gardener/gardener/pkg/apis/core/v1beta1"
v1beta1constants "github.com/gardener/gardener/pkg/apis/core/v1beta1/constants"
resourcesv1alpha1 "github.com/gardener/gardener/pkg/apis/resources/v1alpha1"
"github.com/gardener/gardener/pkg/client/kubernetes"
"github.com/gardener/gardener/pkg/component"
"github.com/gardener/gardener/pkg/utils/managedresources"
)
// ManagedResourceName is the name of the ManagedResource containing the resource specifications.
const ManagedResourceName = "system"
// Values is a set of configuration values for the system resources.
type Values struct {
// ReserveExcessCapacity contains configuration for the deployment of the excess capacity reservation resources.
ReserveExcessCapacity ReserveExcessCapacityValues
}
// ReserveExcessCapacityValues contains configuration for the deployment of the excess capacity reservation resources.
type ReserveExcessCapacityValues struct {
// Enabled specifies whether excess capacity reservation should be enabled.
Enabled bool
// Image is the container image.
Image string
// Replicas is the number of replicas.
Replicas int32
// Configs configures additional excess capacity reservation deployments for shoot control planes in the seed.
Configs []gardencorev1beta1.SeedSettingExcessCapacityReservationConfig
}
// New creates a new instance of DeployWaiter for seed system resources.
func New(
client client.Client,
namespace string,
values Values,
) component.DeployWaiter {
return &seedSystem{
client: client,
namespace: namespace,
values: values,
}
}
type seedSystem struct {
client client.Client
namespace string
values Values
}
func (s *seedSystem) Deploy(ctx context.Context) error {
data, err := s.computeResourcesData()
if err != nil {
return err
}
return managedresources.CreateForSeed(ctx, s.client, s.namespace, ManagedResourceName, false, data)
}
func (s *seedSystem) Destroy(ctx context.Context) error {
return managedresources.DeleteForSeed(ctx, s.client, s.namespace, ManagedResourceName)
}
// TimeoutWaitForManagedResource is the timeout used while waiting for the ManagedResources to become healthy
// or deleted.
var TimeoutWaitForManagedResource = 2 * time.Minute
func (s *seedSystem) Wait(ctx context.Context) error {
timeoutCtx, cancel := context.WithTimeout(ctx, TimeoutWaitForManagedResource)
defer cancel()
return managedresources.WaitUntilHealthy(timeoutCtx, s.client, s.namespace, ManagedResourceName)
}
func (s *seedSystem) WaitCleanup(ctx context.Context) error {
timeoutCtx, cancel := context.WithTimeout(ctx, TimeoutWaitForManagedResource)
defer cancel()
return managedresources.WaitUntilDeleted(timeoutCtx, s.client, s.namespace, ManagedResourceName)
}
func (s *seedSystem) computeResourcesData() (map[string][]byte, error) {
var registry = managedresources.NewRegistry(kubernetes.SeedScheme, kubernetes.SeedCodec, kubernetes.SeedSerializer)
if s.values.ReserveExcessCapacity.Enabled {
for i, config := range s.values.ReserveExcessCapacity.Configs {
name := fmt.Sprintf("reserve-excess-capacity-%d", i)
if err := s.addReserveExcessCapacityDeployment(registry, name, config); err != nil {
return nil, err
}
}
}
if err := addPriorityClasses(registry); err != nil {
return nil, err
}
return registry.SerializedObjects(), nil
}
func (s *seedSystem) addReserveExcessCapacityDeployment(registry *managedresources.Registry, name string, config gardencorev1beta1.SeedSettingExcessCapacityReservationConfig) error {
return registry.Add(
&appsv1.Deployment{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Namespace: s.namespace,
Labels: getExcessCapacityReservationLabels(),
Annotations: map[string]string{
resourcesv1alpha1.SkipHealthCheck: "true",
},
},
Spec: appsv1.DeploymentSpec{
Replicas: &s.values.ReserveExcessCapacity.Replicas,
RevisionHistoryLimit: ptr.To(int32(2)),
Selector: &metav1.LabelSelector{MatchLabels: getExcessCapacityReservationLabels()},
Template: corev1.PodTemplateSpec{
ObjectMeta: metav1.ObjectMeta{
Labels: getExcessCapacityReservationLabels(),
},
Spec: corev1.PodSpec{
TerminationGracePeriodSeconds: ptr.To(int64(5)),
Containers: []corev1.Container{{
Name: "pause-container",
Image: s.values.ReserveExcessCapacity.Image,
ImagePullPolicy: corev1.PullIfNotPresent,
Resources: corev1.ResourceRequirements{
Requests: config.Resources,
Limits: config.Resources,
},
}},
NodeSelector: config.NodeSelector,
PriorityClassName: v1beta1constants.PriorityClassNameReserveExcessCapacity,
Tolerations: config.Tolerations,
},
},
},
})
}
// remember to update docs/development/priority-classes.md when making changes here
var gardenletManagedPriorityClasses = []struct {
name string
value int32
description string
}{
{v1beta1constants.PriorityClassNameSeedSystem900, 999998900, "PriorityClass for Seed system components"},
{v1beta1constants.PriorityClassNameSeedSystem800, 999998800, "PriorityClass for Seed system components"},
{v1beta1constants.PriorityClassNameSeedSystem700, 999998700, "PriorityClass for Seed system components"},
{v1beta1constants.PriorityClassNameSeedSystem600, 999998600, "PriorityClass for Seed system components"},
{v1beta1constants.PriorityClassNameReserveExcessCapacity, -5, "PriorityClass for reserving excess capacity on a Seed cluster"},
{v1beta1constants.PriorityClassNameShootControlPlane500, 999998500, "PriorityClass for Shoot control plane components"},
{v1beta1constants.PriorityClassNameShootControlPlane400, 999998400, "PriorityClass for Shoot control plane components"},
{v1beta1constants.PriorityClassNameShootControlPlane300, 999998300, "PriorityClass for Shoot control plane components"},
{v1beta1constants.PriorityClassNameShootControlPlane200, 999998200, "PriorityClass for Shoot control plane components"},
{v1beta1constants.PriorityClassNameShootControlPlane100, 999998100, "PriorityClass for Shoot control plane components"},
}
func addPriorityClasses(registry *managedresources.Registry) error {
for _, class := range gardenletManagedPriorityClasses {
if err := registry.Add(&schedulingv1.PriorityClass{
ObjectMeta: metav1.ObjectMeta{
Name: class.name,
},
Description: class.description,
GlobalDefault: false,
Value: class.value,
}); err != nil {
return err
}
}
return nil
}
func getExcessCapacityReservationLabels() map[string]string {
return map[string]string{
v1beta1constants.LabelApp: v1beta1constants.LabelKubernetes,
v1beta1constants.LabelRole: "reserve-excess-capacity",
}
}