Skip to content

Commit

Permalink
Merge pull request #108 from snapp-incubator/allow-deleting-default-q…
Browse files Browse the repository at this point in the history
…uota-on-snappcloud

allow deleting default quota in snappcloud namespaces
  • Loading branch information
sinamna authored Sep 7, 2024
2 parents 0558225 + 4aaad63 commit 6d66f2b
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 11 deletions.
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

0 comments on commit 6d66f2b

Please sign in to comment.