-
Notifications
You must be signed in to change notification settings - Fork 462
/
add.go
60 lines (52 loc) · 2.16 KB
/
add.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
// SPDX-FileCopyrightText: 2024 SAP SE or an SAP affiliate company and Gardener contributors
//
// SPDX-License-Identifier: Apache-2.0
package vpaevictionrequirements
import (
"fmt"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
vpaautoscalingv1 "k8s.io/autoscaler/vertical-pod-autoscaler/pkg/apis/autoscaling.k8s.io/v1"
"k8s.io/utils/clock"
"k8s.io/utils/ptr"
"sigs.k8s.io/controller-runtime/pkg/builder"
"sigs.k8s.io/controller-runtime/pkg/cluster"
"sigs.k8s.io/controller-runtime/pkg/controller"
"sigs.k8s.io/controller-runtime/pkg/handler"
"sigs.k8s.io/controller-runtime/pkg/manager"
"sigs.k8s.io/controller-runtime/pkg/predicate"
"sigs.k8s.io/controller-runtime/pkg/source"
"github.com/gardener/gardener/pkg/apis/core/v1beta1/constants"
predicateutils "github.com/gardener/gardener/pkg/controllerutils/predicate"
)
// ControllerName is the name of this controller.
const ControllerName = "vpa-eviction-requirements"
// AddToManager adds Reconciler to the given manager.
func (r *Reconciler) AddToManager(mgr manager.Manager, seedCluster cluster.Cluster) error {
if r.SeedClient == nil {
r.SeedClient = seedCluster.GetClient()
}
if r.Clock == nil {
r.Clock = clock.RealClock{}
}
vpaEvictionRequirementsManagedByControllerPredicate, err := predicate.LabelSelectorPredicate(metav1.LabelSelector{
MatchLabels: map[string]string{constants.LabelVPAEvictionRequirementsController: constants.EvictionRequirementManagedByController},
})
if err != nil {
return fmt.Errorf("failed computing label selector predicate for eviction requirements managed by controller: %w", err)
}
return builder.
ControllerManagedBy(mgr).
Named(ControllerName).
WithOptions(controller.Options{
MaxConcurrentReconciles: ptr.Deref(r.ConcurrentSyncs, 0),
}).
WatchesRawSource(
source.Kind(seedCluster.GetCache(), &vpaautoscalingv1.VerticalPodAutoscaler{}),
&handler.EnqueueRequestForObject{}, builder.WithPredicates(
vpaEvictionRequirementsManagedByControllerPredicate,
predicateutils.ForEventTypes(predicateutils.Create, predicateutils.Update),
predicate.Or(predicate.GenerationChangedPredicate{}, predicate.AnnotationChangedPredicate{}),
),
).
Complete(r)
}