/
deny_delete_cluster_config_resource.go
53 lines (45 loc) · 1.67 KB
/
deny_delete_cluster_config_resource.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
package config
import (
"fmt"
"io"
"k8s.io/apiserver/pkg/admission"
)
const PluginName = "config.openshift.io/DenyDeleteClusterConfiguration"
// Register registers an admission plugin factory whose plugin prevents the deletion of cluster configuration resources.
func Register(plugins *admission.Plugins) {
plugins.Register(PluginName, func(config io.Reader) (admission.Interface, error) {
return newAdmissionPlugin(), nil
})
}
var _ admission.ValidationInterface = &admissionPlugin{}
type admissionPlugin struct {
*admission.Handler
}
func newAdmissionPlugin() *admissionPlugin {
return &admissionPlugin{Handler: admission.NewHandler(admission.Delete)}
}
// Validate returns an error if there is an attempt to delete a cluster configuration resource.
func (p *admissionPlugin) Validate(attributes admission.Attributes) error {
if len(attributes.GetSubresource()) > 0 {
return nil
}
if attributes.GetResource().Group != "config.openshift.io" {
return nil
}
// clusteroperators can be deleted so that we can force status refreshes and change over time.
// clusterversions not named `version` can be deleted (none are expected to exist).
// other config.openshift.io resources not named `cluster` can be deleted (none are expected to exist).
switch attributes.GetResource().Resource {
case "clusteroperators":
return nil
case "clusterversions":
if attributes.GetName() != "version" {
return nil
}
default:
if attributes.GetName() != "cluster" {
return nil
}
}
return admission.NewForbidden(attributes, fmt.Errorf("deleting required %s.%s resource, named %s, is not allowed", attributes.GetResource().Resource, attributes.GetResource().Group, attributes.GetName()))
}