@@ -26,6 +26,7 @@ import (
26
26
apierrors "k8s.io/apimachinery/pkg/api/errors"
27
27
"k8s.io/apimachinery/pkg/api/meta"
28
28
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
29
+ "k8s.io/apimachinery/pkg/runtime/schema"
29
30
utilerrors "k8s.io/apimachinery/pkg/util/errors"
30
31
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
31
32
"k8s.io/apimachinery/pkg/util/sets"
@@ -41,6 +42,16 @@ import (
41
42
listers "k8s.io/apiextensions-apiserver/pkg/client/listers/apiextensions/internalversion"
42
43
)
43
44
45
+ // OverlappingBuiltInResources returns the set of built-in group/resources that are persisted
46
+ // in storage paths that overlap with CRD storage paths, and should not be deleted
47
+ // by this controller if an associated CRD is deleted.
48
+ func OverlappingBuiltInResources () map [schema.GroupResource ]bool {
49
+ return map [schema.GroupResource ]bool {
50
+ {Group : "apiregistration.k8s.io" , Resource : "apiservices" }: true ,
51
+ {Group : "apiextensions.k8s.io" , Resource : "customresourcedefinitions" }: true ,
52
+ }
53
+ }
54
+
44
55
// CRDFinalizer is a controller that finalizes the CRD by deleting all the CRs associated with it.
45
56
type CRDFinalizer struct {
46
57
crdClient client.CustomResourceDefinitionsGetter
@@ -126,7 +137,15 @@ func (c *CRDFinalizer) sync(key string) error {
126
137
127
138
// Now we can start deleting items. We should use the REST API to ensure that all normal admission runs.
128
139
// Since we control the endpoints, we know that delete collection works. No need to delete if not established.
129
- if apiextensions .IsCRDConditionTrue (crd , apiextensions .Established ) {
140
+ if OverlappingBuiltInResources ()[schema.GroupResource {Group : crd .Spec .Group , Resource : crd .Spec .Names .Plural }] {
141
+ // Skip deletion, explain why, and proceed to remove the finalizer and delete the CRD
142
+ apiextensions .SetCRDCondition (crd , apiextensions.CustomResourceDefinitionCondition {
143
+ Type : apiextensions .Terminating ,
144
+ Status : apiextensions .ConditionFalse ,
145
+ Reason : "OverlappingBuiltInResource" ,
146
+ Message : "instances overlap with built-in resources in storage" ,
147
+ })
148
+ } else if apiextensions .IsCRDConditionTrue (crd , apiextensions .Established ) {
130
149
cond , deleteErr := c .deleteInstances (crd )
131
150
apiextensions .SetCRDCondition (crd , cond )
132
151
if deleteErr != nil {
0 commit comments