Skip to content
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

allow deleting default quota in snappcloud namespaces #108

Merged
merged 2 commits into from
Sep 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 14 additions & 9 deletions custom_webhooks/resourcequota_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,20 @@ type ResourceQuotaValidator struct {
}

const (
teamLabel = "snappcloud.io/team"
enforceLabel = "quota.snappcloud.io/enforce"
teamLabel = "snappcloud.io/team"
enforceLabel = "quota.snappcloud.io/enforce"
snappcloudTeamName = "snappcloud"
)

func (v *ResourceQuotaValidator) Handle(ctx context.Context, req admission.Request) admission.Response {
log := log.FromContext(ctx)
ns := &corev1.Namespace{}
err := v.Client.Get(context.TODO(), types.NamespacedName{Name: req.Namespace}, ns)
if err != nil {
log.Error(err, "error getting namespace", "name", req.Namespace)
return admission.Denied("error on getting namespace")
}
if req.Operation == "UPDATE" {
ns := &corev1.Namespace{}
err := v.Client.Get(context.TODO(), types.NamespacedName{Name: req.Namespace}, ns)
if err != nil {
log.Error(err, "error getting namespace", "name", req.Namespace)
return admission.Denied("error on getting namespace")
}
if l, ok := ns.GetLabels()[enforceLabel]; ok {
if l == "true" {
return admission.Allowed("updating resourcequota")
Expand All @@ -51,7 +52,11 @@ func (v *ResourceQuotaValidator) Handle(ctx context.Context, req admission.Reque
return admission.Allowed("updating resourcequota")
}
} else if req.Operation == "DELETE" {
if req.Name == "default" {
teamName, ok := ns.GetLabels()[teamLabel]
if !ok {
return admission.Denied("no team found for the project. please join your project to a team")
}
if req.Name == "default" && teamName != snappcloudTeamName {
return admission.Denied("default resourcequota cannot be deleted")
}
return admission.Allowed("DELETE")
Expand Down
4 changes: 2 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package main
import (
"flag"
"os"
"sigs.k8s.io/controller-runtime/pkg/metrics/server"

// Import all Kubernetes client auth plugins (e.g. Azure, GCP, OIDC, etc.)
// to ensure that exec-entrypoint and run can make use of them.
Expand Down Expand Up @@ -67,8 +68,7 @@ func main() {

mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{
Scheme: scheme,
MetricsBindAddress: metricsAddr,
Port: 9443,
Metrics: server.Options{BindAddress: metricsAddr},
HealthProbeBindAddress: probeAddr,
LeaderElection: enableLeaderElection,
LeaderElectionID: "bc6545ad.snappcloud.io",
Expand Down
Loading