/
admission.go
195 lines (166 loc) · 5.86 KB
/
admission.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
// Copyright (c) 2021 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 shoot
import (
"context"
"errors"
"fmt"
"io"
"github.com/gardener/gardener/pkg/apis/core"
seedmanagementv1alpha1 "github.com/gardener/gardener/pkg/apis/seedmanagement/v1alpha1"
admissioninitializer "github.com/gardener/gardener/pkg/apiserver/admission/initializer"
coreinformers "github.com/gardener/gardener/pkg/client/core/informers/internalversion"
corelisters "github.com/gardener/gardener/pkg/client/core/listers/core/internalversion"
seedmanagementclientset "github.com/gardener/gardener/pkg/client/seedmanagement/clientset/versioned"
admissionutils "github.com/gardener/gardener/plugin/pkg/utils"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apiserver/pkg/admission"
)
const (
// PluginName is the name of this admission plugin.
PluginName = "ManagedSeedShoot"
)
// Register registers a plugin.
func Register(plugins *admission.Plugins) {
plugins.Register(PluginName, func(config io.Reader) (admission.Interface, error) {
return New()
})
}
// Shoot contains listers and and admission handler.
type Shoot struct {
*admission.Handler
shootLister corelisters.ShootLister
seedManagementClient seedmanagementclientset.Interface
readyFunc admission.ReadyFunc
}
var (
_ = admissioninitializer.WantsInternalCoreInformerFactory(&Shoot{})
_ = admissioninitializer.WantsSeedManagementClientset(&Shoot{})
readyFuncs = []admission.ReadyFunc{}
)
// New creates a new Shoot admission plugin.
func New() (*Shoot, error) {
return &Shoot{
Handler: admission.NewHandler(admission.Delete),
}, nil
}
// AssignReadyFunc assigns the ready function to the admission handler.
func (v *Shoot) AssignReadyFunc(f admission.ReadyFunc) {
v.readyFunc = f
v.SetReadyFunc(f)
}
// SetInternalCoreInformerFactory gets Lister from SharedInformerFactory.
func (v *Shoot) SetInternalCoreInformerFactory(f coreinformers.SharedInformerFactory) {
shootInformer := f.Core().InternalVersion().Shoots()
v.shootLister = shootInformer.Lister()
readyFuncs = append(readyFuncs, shootInformer.Informer().HasSynced)
}
// SetSeedManagementClientset sets the garden seedmanagement clientset.
func (v *Shoot) SetSeedManagementClientset(c seedmanagementclientset.Interface) {
v.seedManagementClient = c
}
// ValidateInitialization checks whether the plugin was correctly initialized.
func (v *Shoot) ValidateInitialization() error {
if v.shootLister == nil {
return errors.New("missing shoot lister")
}
if v.seedManagementClient == nil {
return errors.New("missing garden seedmanagement client")
}
return nil
}
var _ admission.ValidationInterface = &Shoot{}
// Validate validates if the Shoot can be deleted. If the
func (v *Shoot) Validate(ctx context.Context, a admission.Attributes, _ admission.ObjectInterfaces) error {
// Wait until the caches have been synced
if v.readyFunc == nil {
v.AssignReadyFunc(func() bool {
for _, readyFunc := range readyFuncs {
if !readyFunc() {
return false
}
}
return true
})
}
if !v.WaitForReady() {
return admission.NewForbidden(a, errors.New("not yet ready to handle request"))
}
// Ignore all kinds other than ManagedSeed
if a.GetKind().GroupKind() != seedmanagementv1alpha1.Kind("ManagedSeed") {
return nil
}
// Ignore updates to status or other subresources
if a.GetSubresource() != "" {
return nil
}
switch {
case a.GetName() == "":
return v.validateDeleteCollection(ctx, a)
default:
return v.validateDelete(ctx, a)
}
}
func (v *Shoot) validateDeleteCollection(ctx context.Context, a admission.Attributes) error {
managedSeeds, err := v.getManagedSeeds(ctx, labels.Everything())
if err != nil {
return err
}
for _, managedSeed := range managedSeeds {
if err := v.validateDelete(ctx, newAttributesWithName(a, managedSeed.Name)); err != nil {
return err
}
}
return nil
}
func (v *Shoot) validateDelete(ctx context.Context, a admission.Attributes) error {
seedName := a.GetName()
shoots, err := v.getShoots(labels.Everything())
if err != nil {
return err
}
if admissionutils.IsSeedUsedByShoot(seedName, shoots) {
return admission.NewForbidden(a, fmt.Errorf("cannot delete managed seed %s/%s since its seed %s is still used by shoot(s)", a.GetNamespace(), a.GetName(), a.GetName()))
}
return nil
}
func newAttributesWithName(a admission.Attributes, name string) admission.Attributes {
return admission.NewAttributesRecord(a.GetObject(),
a.GetOldObject(),
a.GetKind(),
a.GetNamespace(),
name,
a.GetResource(),
a.GetSubresource(),
a.GetOperation(),
a.GetOperationOptions(),
a.IsDryRun(),
a.GetUserInfo())
}
func (v *Shoot) getManagedSeeds(ctx context.Context, selector labels.Selector) ([]seedmanagementv1alpha1.ManagedSeed, error) {
managedSeedList, err := v.seedManagementClient.SeedmanagementV1alpha1().ManagedSeeds("").List(ctx, metav1.ListOptions{LabelSelector: selector.String()})
if err != nil {
return nil, err
}
return managedSeedList.Items, nil
}
func (v *Shoot) getShoots(selector labels.Selector) ([]*core.Shoot, error) {
shoots, err := v.shootLister.List(selector)
if err != nil {
return nil, apierrors.NewInternalError(err)
}
return shoots, nil
}