-
Notifications
You must be signed in to change notification settings - Fork 463
/
seedfilter.go
154 lines (139 loc) · 5.97 KB
/
seedfilter.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
// Copyright (c) 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 controllerutils
import (
"context"
gardencorev1beta1 "github.com/gardener/gardener/pkg/apis/core/v1beta1"
gardencorelisters "github.com/gardener/gardener/pkg/client/core/listers/core/v1beta1"
"github.com/gardener/gardener/pkg/gardenlet/apis/config"
confighelper "github.com/gardener/gardener/pkg/gardenlet/apis/config/helper"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"sigs.k8s.io/controller-runtime/pkg/client"
)
// LabelsMatchFor checks whether the given label selector matches for the given set of labels.
func LabelsMatchFor(l map[string]string, labelSelector *metav1.LabelSelector) bool {
selector, err := metav1.LabelSelectorAsSelector(labelSelector)
if err != nil {
return false
}
return selector.Matches(labels.Set(l))
}
// SeedFilterFunc returns a filtering func for the seeds and the given label selector.
func SeedFilterFunc(seedName string, labelSelector *metav1.LabelSelector) func(obj interface{}) bool {
return func(obj interface{}) bool {
seed, ok := obj.(*gardencorev1beta1.Seed)
if !ok {
return false
}
if len(seedName) > 0 {
return seed.Name == seedName
}
return LabelsMatchFor(seed.Labels, labelSelector)
}
}
// ShootFilterFunc returns a filtering func for the seeds and the given label selector.
func ShootFilterFunc(seedName string, seedLister gardencorelisters.SeedLister, labelSelector *metav1.LabelSelector) func(obj interface{}) bool {
return func(obj interface{}) bool {
shoot, ok := obj.(*gardencorev1beta1.Shoot)
if !ok {
return false
}
if shoot.Spec.SeedName == nil {
return false
}
if len(seedName) > 0 {
if shoot.Status.SeedName == nil || *shoot.Spec.SeedName == *shoot.Status.SeedName {
return *shoot.Spec.SeedName == seedName
}
return *shoot.Status.SeedName == seedName
}
if shoot.Status.SeedName == nil || *shoot.Spec.SeedName == *shoot.Status.SeedName {
return SeedLabelsMatch(seedLister, *shoot.Spec.SeedName, labelSelector)
}
return SeedLabelsMatch(seedLister, *shoot.Status.SeedName, labelSelector)
}
}
// ShootIsManagedByThisGardenlet checks if the given shoot is managed by this gardenlet by comparing it with the seed name from the GardenletConfiguration
// or by checking whether the seed labels mathes the seed seoector from the GardenletConfiguration.
func ShootIsManagedByThisGardenlet(shoot *gardencorev1beta1.Shoot, gc *config.GardenletConfiguration, seedLister gardencorelisters.SeedLister) bool {
seedName := confighelper.SeedNameFromSeedConfig(gc.SeedConfig)
if len(seedName) > 0 {
return *shoot.Spec.SeedName == seedName
}
return SeedLabelsMatch(seedLister, *shoot.Spec.SeedName, gc.SeedSelector)
}
// SeedLabelsMatch fetches the given seed via a lister by its name and then checks whether the given label selector matches
// the seed labels.
func SeedLabelsMatch(seedLister gardencorelisters.SeedLister, seedName string, labelSelector *metav1.LabelSelector) bool {
seed, err := seedLister.Get(seedName)
if err != nil {
return false
}
return LabelsMatchFor(seed.Labels, labelSelector)
}
// seedLabelsMatchWithClient fetches the given seed by its name from the client and then checks whether the given
// label selector matches the seed labels.
func seedLabelsMatchWithClient(ctx context.Context, c client.Client, seedName string, labelSelector *metav1.LabelSelector) bool {
seed := &gardencorev1beta1.Seed{}
if err := c.Get(ctx, client.ObjectKey{Name: seedName}, seed); err != nil {
return false
}
return LabelsMatchFor(seed.Labels, labelSelector)
}
// ControllerInstallationFilterFunc returns a filtering func for the seeds and the given label selector.
func ControllerInstallationFilterFunc(seedName string, seedLister gardencorelisters.SeedLister, labelSelector *metav1.LabelSelector) func(obj interface{}) bool {
return func(obj interface{}) bool {
controllerInstallation, ok := obj.(*gardencorev1beta1.ControllerInstallation)
if !ok {
return false
}
if len(seedName) > 0 {
return controllerInstallation.Spec.SeedRef.Name == seedName
}
return SeedLabelsMatch(seedLister, controllerInstallation.Spec.SeedRef.Name, labelSelector)
}
}
// BackupBucketFilterFunc returns a filtering func for the seeds and the given label selector.
func BackupBucketFilterFunc(ctx context.Context, c client.Client, seedName string, labelSelector *metav1.LabelSelector) func(obj interface{}) bool {
return func(obj interface{}) bool {
backupBucket, ok := obj.(*gardencorev1beta1.BackupBucket)
if !ok {
return false
}
if backupBucket.Spec.SeedName == nil {
return false
}
if len(seedName) > 0 {
return *backupBucket.Spec.SeedName == seedName
}
return seedLabelsMatchWithClient(ctx, c, *backupBucket.Spec.SeedName, labelSelector)
}
}
// BackupEntryFilterFunc returns a filtering func for the seeds and the given label selector.
func BackupEntryFilterFunc(ctx context.Context, c client.Client, seedName string, labelSelector *metav1.LabelSelector) func(obj interface{}) bool {
return func(obj interface{}) bool {
backupEntry, ok := obj.(*gardencorev1beta1.BackupEntry)
if !ok {
return false
}
if backupEntry.Spec.SeedName == nil {
return false
}
if len(seedName) > 0 {
return *backupEntry.Spec.SeedName == seedName
}
return seedLabelsMatchWithClient(ctx, c, *backupEntry.Spec.SeedName, labelSelector)
}
}