/
admission.go
138 lines (114 loc) · 4.16 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
// Copyright (c) 2020 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 validator
import (
"context"
"errors"
"fmt"
"io"
"github.com/gardener/gardener/pkg/apis/core"
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"
admissionutils "github.com/gardener/gardener/plugin/pkg/utils"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apiserver/pkg/admission"
)
const (
// PluginName is the name of this admission plugin.
PluginName = "SeedValidator"
)
// Register registers a plugin.
func Register(plugins *admission.Plugins) {
plugins.Register(PluginName, func(config io.Reader) (admission.Interface, error) {
return New()
})
}
// ValidateSeed contains listers and and admission handler.
type ValidateSeed struct {
*admission.Handler
seedLister corelisters.SeedLister
shootLister corelisters.ShootLister
readyFunc admission.ReadyFunc
}
var (
_ = admissioninitializer.WantsInternalCoreInformerFactory(&ValidateSeed{})
readyFuncs = []admission.ReadyFunc{}
)
// New creates a new ValidateSeed admission plugin.
func New() (*ValidateSeed, error) {
return &ValidateSeed{
Handler: admission.NewHandler(admission.Delete),
}, nil
}
// AssignReadyFunc assigns the ready function to the admission handler.
func (v *ValidateSeed) AssignReadyFunc(f admission.ReadyFunc) {
v.readyFunc = f
v.SetReadyFunc(f)
}
// SetInternalCoreInformerFactory gets Lister from SharedInformerFactory.
func (v *ValidateSeed) SetInternalCoreInformerFactory(f coreinformers.SharedInformerFactory) {
seedInformer := f.Core().InternalVersion().Seeds()
v.seedLister = seedInformer.Lister()
shootInformer := f.Core().InternalVersion().Shoots()
v.shootLister = shootInformer.Lister()
backupBucketInformer := f.Core().InternalVersion().BackupBuckets()
readyFuncs = append(readyFuncs, seedInformer.Informer().HasSynced, shootInformer.Informer().HasSynced, backupBucketInformer.Informer().HasSynced)
}
// ValidateInitialization checks whether the plugin was correctly initialized.
func (v *ValidateSeed) ValidateInitialization() error {
if v.seedLister == nil {
return errors.New("missing seed lister")
}
if v.shootLister == nil {
return errors.New("missing shoot lister")
}
return nil
}
var _ admission.ValidationInterface = &ValidateSeed{}
// Validate validates the Seed details against existing Shoots and BackupBuckets
func (v *ValidateSeed) Validate(ctx context.Context, a admission.Attributes, o 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 Seed
if a.GetKind().GroupKind() != core.Kind("Seed") {
return nil
}
// Ignore updates to status or other subresources
if a.GetSubresource() != "" {
return nil
}
seedName := a.GetName()
shoots, err := v.shootLister.List(labels.Everything())
if err != nil {
return apierrors.NewInternalError(err)
}
if admissionutils.IsSeedUsedByShoot(seedName, shoots) {
return admission.NewForbidden(a, fmt.Errorf("cannot delete seed '%s' which is still used by shoot(s)", seedName))
}
return nil
}