/
associations.go
107 lines (92 loc) · 4.2 KB
/
associations.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
// SPDX-FileCopyrightText: 2024 SAP SE or an SAP affiliate company and Gardener contributors
//
// SPDX-License-Identifier: Apache-2.0
package controllerutils
import (
"context"
"fmt"
"k8s.io/apimachinery/pkg/api/meta"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/utils/ptr"
"sigs.k8s.io/controller-runtime/pkg/client"
"github.com/gardener/gardener/pkg/apis/core"
gardencorev1beta1 "github.com/gardener/gardener/pkg/apis/core/v1beta1"
)
// DetermineShootsAssociatedTo gets a <shootLister> to determine the Shoots resources which are associated
// to given <obj> (either a CloudProfile, Seed, Secretbinding a or a ExposureClass object).
func DetermineShootsAssociatedTo(ctx context.Context, gardenClient client.Reader, obj interface{}) ([]string, error) {
shootList := &gardencorev1beta1.ShootList{}
if err := gardenClient.List(ctx, shootList); err != nil {
return nil, err
}
var associatedShoots []string
for _, shoot := range shootList.Items {
switch t := obj.(type) {
case *gardencorev1beta1.CloudProfile:
cloudProfile := obj.(*gardencorev1beta1.CloudProfile)
if shoot.Spec.CloudProfileName == cloudProfile.Name {
associatedShoots = append(associatedShoots, fmt.Sprintf("%s/%s", shoot.Namespace, shoot.Name))
}
case *gardencorev1beta1.Seed:
seed := obj.(*gardencorev1beta1.Seed)
if shoot.Spec.SeedName != nil && *shoot.Spec.SeedName == seed.Name {
associatedShoots = append(associatedShoots, fmt.Sprintf("%s/%s", shoot.Namespace, shoot.Name))
}
case *gardencorev1beta1.SecretBinding:
binding := obj.(*gardencorev1beta1.SecretBinding)
if ptr.Deref(shoot.Spec.SecretBindingName, "") == binding.Name && shoot.Namespace == binding.Namespace {
associatedShoots = append(associatedShoots, fmt.Sprintf("%s/%s", shoot.Namespace, shoot.Name))
}
case *gardencorev1beta1.ExposureClass:
exposureClass := obj.(*gardencorev1beta1.ExposureClass)
if shoot.Spec.ExposureClassName != nil && *shoot.Spec.ExposureClassName == exposureClass.Name {
associatedShoots = append(associatedShoots, fmt.Sprintf("%s/%s", shoot.Namespace, shoot.Name))
}
default:
return nil, fmt.Errorf("unable to determine Shoot associations, due to unknown type %t", t)
}
}
return associatedShoots, nil
}
// DetermineSecretBindingAssociations gets a <bindingLister> to determine the SecretBinding
// resources which are associated to given Quota <obj>.
func DetermineSecretBindingAssociations(ctx context.Context, c client.Client, quota *gardencorev1beta1.Quota) ([]string, error) {
bindings := &gardencorev1beta1.SecretBindingList{}
if err := c.List(ctx, bindings); err != nil {
return nil, err
}
var associatedBindings []string
for _, binding := range bindings.Items {
for _, quotaRef := range binding.Quotas {
if quotaRef.Name == quota.Name && quotaRef.Namespace == quota.Namespace {
associatedBindings = append(associatedBindings, fmt.Sprintf("%s/%s", binding.Namespace, binding.Name))
}
}
}
return associatedBindings, nil
}
// DetermineBackupBucketAssociations determine the BackupBucket resources which are associated
// to seed with name <seedName>
func DetermineBackupBucketAssociations(ctx context.Context, c client.Client, seedName string) ([]string, error) {
return determineAssociations(ctx, c, &gardencorev1beta1.BackupBucketList{}, client.MatchingFields{core.BackupBucketSeedName: seedName})
}
// DetermineControllerInstallationAssociations determine the ControllerInstallation resources which are associated
// to seed with name <seedName>
func DetermineControllerInstallationAssociations(ctx context.Context, c client.Client, seedName string) ([]string, error) {
return determineAssociations(ctx, c, &gardencorev1beta1.ControllerInstallationList{}, client.MatchingFields{core.SeedRefName: seedName})
}
func determineAssociations(ctx context.Context, c client.Client, listObj client.ObjectList, fieldSelector client.MatchingFields) ([]string, error) {
if err := c.List(ctx, listObj, fieldSelector); err != nil {
return nil, err
}
var associations []string
err := meta.EachListItem(listObj, func(obj runtime.Object) error {
accessor, err := meta.Accessor(obj)
if err != nil {
return err
}
associations = append(associations, accessor.GetName())
return nil
})
return associations, err
}