-
Notifications
You must be signed in to change notification settings - Fork 69
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(webhook): cleanup validatingconfig on openebs namespace deletion
from k8s 1.20+ onwards Cluster-scoped dependents can only specify cluster-scoped owners. In v1.20+, if a cluster-scoped dependent specifies a namespaced kind as an owner, it is treated as having an unresolveable owner reference, and is not able to be garbage collected. In v1.20+, if the garbage collector detects an invalid cross-namespace ownerReference, or a cluster-scoped dependent with an ownerReference referencing a namespaced kind, a warning Event with a reason of OwnerRefInvalidNamespace and an involvedObject of the invalid dependent is reported. You can check for that kind of Event by running kubectl get events -A --field-selector=reason=OwnerRefInvalidNamespace. Signed-off-by: prateekpandey14 <[email protected]>
- Loading branch information
1 parent
291f6f4
commit d4e2f71
Showing
4 changed files
with
144 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
/* | ||
Copyright 2021 The OpenEBS Authors. | ||
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 webhook | ||
|
||
import ( | ||
"fmt" | ||
|
||
"k8s.io/api/admission/v1beta1" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/klog" | ||
) | ||
|
||
func (wh *webhook) validateNamespace(ar *v1beta1.AdmissionReview) *v1beta1.AdmissionResponse { | ||
req := ar.Request | ||
response := &v1beta1.AdmissionResponse{} | ||
response.Allowed = true | ||
openebsNamespace, err := getOpenebsNamespace() | ||
if err != nil { | ||
response.Allowed = false | ||
response.Result = &metav1.Status{ | ||
Message: fmt.Sprintf("error getting OPENEBS_NAMESPACE env %s: %v", req.Name, err.Error()), | ||
} | ||
return response | ||
} | ||
// validates only if requested operation is DELETE | ||
if openebsNamespace == req.Name && req.Operation == v1beta1.Delete { | ||
return wh.validateNamespaceDeleteRequest(req) | ||
} | ||
klog.V(2).Info("Admission wehbook for Namespace module not " + | ||
"configured for operations other than DELETE") | ||
return response | ||
} | ||
|
||
func (wh *webhook) validateNamespaceDeleteRequest(req *v1beta1.AdmissionRequest) *v1beta1.AdmissionResponse { | ||
response := &v1beta1.AdmissionResponse{} | ||
response.Allowed = true | ||
svcLabel := "openebs.io/controller-service=jiva-controller-svc" | ||
|
||
msg := fmt.Sprintf("either BDCs or services with the label %s exists in the namespace %s.", svcLabel, req.Name) | ||
|
||
// ignore the Delete request of Namespace if resource name is empty | ||
if req.Name == "" { | ||
return response | ||
} | ||
|
||
bdcList, err := wh.clientset.OpenebsV1alpha1(). | ||
BlockDeviceClaims(req.Name). | ||
List(metav1.ListOptions{}) | ||
if err != nil { | ||
response.Allowed = false | ||
response.Result = &metav1.Status{ | ||
Message: fmt.Sprintf("error listing BDC in namespace %s: %v", req.Name, err.Error()), | ||
} | ||
return response | ||
} | ||
|
||
if len(bdcList.Items) != 0 { | ||
response.Allowed = false | ||
response.Result = &metav1.Status{ | ||
Message: msg, | ||
} | ||
return response | ||
} | ||
|
||
svcList, err := wh.kubeClient.CoreV1().Services(req.Name). | ||
List(metav1.ListOptions{ | ||
LabelSelector: svcLabel, | ||
}) | ||
if err != nil { | ||
response.Allowed = false | ||
response.Result = &metav1.Status{ | ||
Message: fmt.Sprintf("error listing svc in namespace %s: %v", req.Name, err.Error()), | ||
} | ||
return response | ||
} | ||
|
||
if len(svcList.Items) != 0 { | ||
response.Allowed = false | ||
response.Result = &metav1.Status{ | ||
Message: msg, | ||
} | ||
return response | ||
} | ||
// Delete the validatingWebhookConfiguration only if its a delete request to | ||
// delete openebs namespace | ||
err = wh.kubeClient.AdmissionregistrationV1(). | ||
ValidatingWebhookConfigurations(). | ||
Delete(validatorWebhook, &metav1.DeleteOptions{}) | ||
if err != nil { | ||
response.Allowed = false | ||
response.Result = &metav1.Status{ | ||
Message: err.Error(), | ||
} | ||
return response | ||
} | ||
return response | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters