-
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.
Prevent deletion of namespaces and cluster (#651)
* Ignore namespace delete operation Signed-off-by: Dharmit Shah <[email protected]> * Prevent deletion of `local` and `fleet-local` namespaces Signed-off-by: Dharmit Shah <[email protected]> * Prevent deletion of local cluster It prevents deletion of both clusters.provisioning.cattle.io and cluster.management.cattle.io resources of the named local. * Fixes to tests based on CI feedback --------- Signed-off-by: Dharmit Shah <[email protected]>
- Loading branch information
Showing
9 changed files
with
149 additions
and
9 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package namespace | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/rancher/webhook/pkg/admission" | ||
objectsv1 "github.com/rancher/webhook/pkg/generated/objects/core/v1" | ||
admissionv1 "k8s.io/api/admission/v1" | ||
"k8s.io/utils/trace" | ||
) | ||
|
||
// deleteNamespaceAdmitter handles namespace deletion scenarios | ||
type deleteNamespaceAdmitter struct{} | ||
|
||
func (d deleteNamespaceAdmitter) Admit(request *admission.Request) (*admissionv1.AdmissionResponse, error) { | ||
listTrace := trace.New("Namespace Admit", trace.Field{Key: "user", Value: request.UserInfo.Username}) | ||
defer listTrace.LogIfLong(admission.SlowTraceDuration) | ||
|
||
if request.Operation != admissionv1.Delete { | ||
return admission.ResponseAllowed(), nil | ||
} | ||
|
||
oldNs, _, err := objectsv1.NamespaceOldAndNewFromRequest(&request.AdmissionRequest) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to decode namespace from request: %w", err) | ||
} | ||
|
||
return admission.ResponseBadRequest(fmt.Sprintf("%q namespace my not be deleted\n", oldNs.Name)), nil | ||
} |
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,65 @@ | ||
package namespace | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/rancher/webhook/pkg/admission" | ||
"github.com/stretchr/testify/assert" | ||
admissionv1 "k8s.io/api/admission/v1" | ||
) | ||
|
||
func Test_Admit(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
namespaceName string | ||
operationType admissionv1.Operation | ||
wantAllowed bool | ||
wantErr bool | ||
}{ | ||
{ | ||
name: "Allow creating namespace", | ||
namespaceName: "local", | ||
operationType: admissionv1.Create, | ||
wantAllowed: true, | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "Prevent deletion of 'local' namespace", | ||
namespaceName: "local", | ||
operationType: admissionv1.Delete, | ||
wantAllowed: false, | ||
wantErr: true, | ||
}, | ||
{ | ||
name: "Prevent deletion of 'fleet-local' namespace", | ||
namespaceName: "fleet-local", | ||
operationType: admissionv1.Delete, | ||
wantAllowed: false, | ||
wantErr: true, | ||
}, | ||
} | ||
for _, test := range tests { | ||
test := test | ||
t.Run(test.name, func(t *testing.T) { | ||
d := deleteNamespaceAdmitter{} | ||
request := createRequest(test.name, test.namespaceName, test.operationType) | ||
response, err := d.Admit(request) | ||
if test.wantErr { | ||
assert.Error(t, err) | ||
} else { | ||
assert.NoError(t, err) | ||
assert.Equal(t, test.wantAllowed, response.Allowed) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func createRequest(name, namespaceName string, operation admissionv1.Operation) *admission.Request { | ||
return &admission.Request{ | ||
AdmissionRequest: admissionv1.AdmissionRequest{ | ||
Name: name, | ||
Namespace: namespaceName, | ||
Operation: operation, | ||
}, | ||
} | ||
} |
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
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
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