-
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
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
feat(webhook): add validation for namspace delete requests |
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 |
---|---|---|
|
@@ -28,6 +28,7 @@ import ( | |
snapshot "github.com/openebs/maya/pkg/apis/openebs.io/snapshot/v1" | ||
"github.com/openebs/maya/pkg/apis/openebs.io/v1alpha1" | ||
clientset "github.com/openebs/maya/pkg/client/generated/clientset/versioned" | ||
ndmclientset "github.com/openebs/maya/pkg/client/generated/openebs.io/ndm/v1alpha1/clientset/internalclientset" | ||
snapclient "github.com/openebs/maya/pkg/client/generated/openebs.io/snapshot/v1/clientset/internalclientset" | ||
"github.com/pkg/errors" | ||
"k8s.io/api/admission/v1beta1" | ||
|
@@ -80,6 +81,8 @@ type webhook struct { | |
|
||
// snapClientSet is a snaphot custom resource package generated from custom API group. | ||
snapClientSet snapclient.Interface | ||
|
||
ndmClientset ndmclientset.Interface | ||
} | ||
|
||
// Parameters are server configures parameters | ||
|
@@ -105,7 +108,8 @@ func init() { | |
// set up secret (for TLS certs) k8s resource. This function runs forever. | ||
func New(p Parameters, kubeClient kubernetes.Interface, | ||
openebsClient clientset.Interface, | ||
snapClient snapclient.Interface) ( | ||
snapClient snapclient.Interface, | ||
ndmClient ndmclientset.Interface) ( | ||
*webhook, error) { | ||
|
||
admNamespace, err := getOpenebsNamespace() | ||
|
@@ -173,6 +177,7 @@ func New(p Parameters, kubeClient kubernetes.Interface, | |
kubeClient: kubeClient, | ||
clientset: openebsClient, | ||
snapClientSet: snapClient, | ||
ndmClientset: ndmClient, | ||
} | ||
return wh, nil | ||
} | ||
|
@@ -421,6 +426,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 +461,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 DELETE | ||
if 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.ndmClientset.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()), | ||
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) | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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