-
Notifications
You must be signed in to change notification settings - Fork 200
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(webhook): add validation for namspace delete requests #1754
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,6 +27,7 @@ import ( | |
|
||
snapshot "github.com/openebs/maya/pkg/apis/openebs.io/snapshot/v1" | ||
"github.com/openebs/maya/pkg/apis/openebs.io/v1alpha1" | ||
blockdeviceclaim "github.com/openebs/maya/pkg/blockdeviceclaim/v1alpha1" | ||
clientset "github.com/openebs/maya/pkg/client/generated/clientset/versioned" | ||
snapclient "github.com/openebs/maya/pkg/client/generated/openebs.io/snapshot/v1/clientset/internalclientset" | ||
"github.com/pkg/errors" | ||
|
@@ -421,6 +422,9 @@ func (wh *webhook) validate(ar *v1beta1.AdmissionReview) *v1beta1.AdmissionRespo | |
response.Allowed = true | ||
klog.Info("Admission webhook request received") | ||
switch req.Kind.Kind { | ||
case "Namespace": | ||
klog.V(2).Infof("Admission webhook request for type %s", req.Kind.Kind) | ||
return wh.validateNamespace(ar) | ||
case "PersistentVolumeClaim": | ||
klog.V(2).Infof("Admission webhook request for type %s", req.Kind.Kind) | ||
return wh.validatePVC(ar) | ||
|
@@ -453,6 +457,72 @@ func (wh *webhook) validatePVC(ar *v1beta1.AdmissionReview) *v1beta1.AdmissionRe | |
return response | ||
} | ||
|
||
func (wh *webhook) validateNamespace(ar *v1beta1.AdmissionReview) *v1beta1.AdmissionResponse { | ||
req := ar.Request | ||
response := &v1beta1.AdmissionResponse{} | ||
response.Allowed = true | ||
// validates only if requested operation is CREATE or DELETE | ||
shubham14bajpai marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if req.Operation == v1beta1.Delete { | ||
return wh.validateNamespaceDeleteRequest(req) | ||
} | ||
klog.V(2).Info("Admission wehbook for PVC module not " + | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you also check that this rule only applies to openebs namespace? Other namespace requests shouldn't get impacted by this. Add a test to verify that when BDCs are present, a test namespace can be created and deleted without any issues. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed the log message and added the manual test result to the PR description. |
||
"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 := blockdeviceclaim.NewKubeClient(). | ||
shubham14bajpai marked this conversation as resolved.
Show resolved
Hide resolved
|
||
WithNamespace(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()), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Failure is here in case of helm chart deletion |
||
} | ||
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 | ||
} | ||
return response | ||
} | ||
|
||
// getCstorVolumes gets the list of CstorVolumes based in the source-volume labels | ||
func (wh *webhook) getCstorVolumes(listOptions metav1.ListOptions) (*v1alpha1.CStorVolumeList, error) { | ||
return wh.clientset.OpenebsV1alpha1().CStorVolumes("").List(listOptions) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this validate kicks in for any namespace deletion , can we use the webhook namespace itself to get the openebs namespace and skip for other namespace validations