-
Notifications
You must be signed in to change notification settings - Fork 11
/
ensurer.go
99 lines (84 loc) · 3.44 KB
/
ensurer.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
package controlplaneexposure
import (
"context"
"time"
druidv1alpha1 "github.com/gardener/etcd-druid/api/v1alpha1"
"github.com/gardener/gardener/extensions/pkg/webhook/controlplane/genericmutator"
"github.com/go-logr/logr"
"github.com/metal-stack/gardener-extension-provider-metal/pkg/apis/config"
"github.com/pkg/errors"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/controller-runtime/pkg/client"
extensionswebhook "github.com/gardener/gardener/extensions/pkg/webhook"
v1beta1constants "github.com/gardener/gardener/pkg/apis/core/v1beta1/constants"
v1beta1helper "github.com/gardener/gardener/pkg/apis/core/v1beta1/helper"
kutil "github.com/gardener/gardener/pkg/utils/kubernetes"
)
// NewEnsurer creates a new controlplaneexposure ensurer.
func NewEnsurer(etcdStorage *config.ETCD, logger logr.Logger) genericmutator.Ensurer {
return &ensurer{
c: etcdStorage,
logger: logger.WithName("metal-controlplaneexposure-ensurer"),
}
}
type ensurer struct {
genericmutator.NoopEnsurer
c *config.ETCD
client client.Client
logger logr.Logger
}
// InjectClient injects the given client into the ensurer.
func (e *ensurer) InjectClient(client client.Client) error {
e.client = client
return nil
}
// EnsureKubeAPIServerService ensures that the kube-apiserver service conforms to the provider requirements.
func (e *ensurer) EnsureKubeAPIServerService(ctx context.Context, ectx genericmutator.EnsurerContext, new, old *corev1.Service) error {
return nil
}
// EnsureKubeAPIServerDeployment ensures that the kube-apiserver deployment conforms to the provider requirements.
func (e *ensurer) EnsureKubeAPIServerDeployment(ctx context.Context, ectx genericmutator.EnsurerContext, new, old *appsv1.Deployment) error {
// ignore gardener managed (APIServerSNI-enabled) apiservers.
if v1beta1helper.IsAPIServerExposureManaged(new) {
return nil
}
// Get load balancer address of the kube-apiserver service
address, err := kutil.GetLoadBalancerIngress(ctx, e.client, new.Namespace, v1beta1constants.DeploymentNameKubeAPIServer)
if err != nil {
return errors.Wrap(err, "could not get kube-apiserver service load balancer address")
}
if c := extensionswebhook.ContainerWithName(new.Spec.Template.Spec.Containers, "kube-apiserver"); c != nil {
c.Command = extensionswebhook.EnsureStringWithPrefix(c.Command, "--advertise-address=", address)
c.Command = extensionswebhook.EnsureStringWithPrefix(c.Command, "--external-hostname=", address)
}
return nil
}
// EnsureETCD ensures that the etcd conform to the provider requirements.
func (e *ensurer) EnsureETCD(ctx context.Context, ectx genericmutator.EnsurerContext, new, old *druidv1alpha1.Etcd) error {
capacity := resource.MustParse("16Gi")
class := ""
if new.Name == v1beta1constants.ETCDMain && e.c != nil {
if e.c.Storage.Capacity != nil {
capacity = *e.c.Storage.Capacity
}
if e.c.Storage.ClassName != nil {
class = *e.c.Storage.ClassName
}
if e.c.Backup.DeltaSnapshotPeriod != nil {
d, err := time.ParseDuration(*e.c.Backup.DeltaSnapshotPeriod)
if err != nil {
return errors.Wrap(err, "unable to set delta snapshot period")
}
new.Spec.Backup.DeltaSnapshotPeriod = &v1.Duration{Duration: d}
}
if e.c.Backup.Schedule != nil {
new.Spec.Backup.FullSnapshotSchedule = e.c.Backup.Schedule
}
}
new.Spec.StorageClass = &class
new.Spec.StorageCapacity = &capacity
return nil
}