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

Fix the bug where the namespace cannot be deleted #800

Open
wants to merge 1 commit into
base: v4.2.0
Choose a base branch
from
Open
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
31 changes: 30 additions & 1 deletion pkg/controllers/namespace/namespace_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,33 @@ func (r *NamespaceReconciler) createDefaultSubnetSet(ns string, defaultSubnetSiz
return nil
}

func (r *NamespaceReconciler) deleteDefaultSubnetSet(ns string) error {
subnetSets := []string{
types.DefaultVMSubnetSet,
types.DefaultPodSubnetSet,
}
for _, name := range subnetSets {
if err := retry.OnError(retry.DefaultRetry, func(err error) bool {
return err != nil
}, func() error {
obj := &v1alpha1.SubnetSet{}
err := r.Client.Get(context.Background(), client.ObjectKey{
Namespace: ns,
Name: name,
}, obj)
if err != nil {
return client.IgnoreNotFound(err)
}
log.Info("delete default SubnetSet", "Namespace", ns, "Name", name)
return r.Client.Delete(context.Background(), obj)
}); err != nil {
log.Error(err, "failed to delete SubnetSet", "Namespace", ns, "Name", name)
return err
}
}
return nil
}

func (r *NamespaceReconciler) namespaceError(ctx context.Context, k8sObj client.Object, msg string, err error) {
logErr := util.If(err == nil, errors.New(msg), err).(error)
log.Error(logErr, msg)
Expand Down Expand Up @@ -237,9 +264,11 @@ func (r *NamespaceReconciler) Reconcile(ctx context.Context, req ctrl.Request) (
}
return common.ResultNormal, nil
} else {
log.Info("skip ns deletion event for ns", "Namespace", ns)
metrics.CounterInc(r.NSXConfig, metrics.ControllerDeleteTotal, common.MetricResTypeNamespace)
r.VPCService.UnRegisterNamespaceNetworkconfigBinding(obj.GetNamespace())
if err := r.deleteDefaultSubnetSet(ns); err != nil {
return common.ResultRequeueAfter10sec, nil
}
return common.ResultNormal, nil
}
}
Expand Down
12 changes: 4 additions & 8 deletions pkg/controllers/subnetset/subnetset_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,12 @@ import (

admissionv1 "k8s.io/api/admission/v1"
"sigs.k8s.io/controller-runtime/pkg/client"
logf "sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"

"github.com/vmware-tanzu/nsx-operator/pkg/apis/vpc/v1alpha1"
"github.com/vmware-tanzu/nsx-operator/pkg/nsx/services/common"
)

// log is for logging in this package.
var subnetsetlog = logf.Log.WithName("subnetset-webhook")

var NSXOperatorSA = "system:serviceaccount:vmware-system-nsx:ncp-svc-account"

// Create validator instead of using the existing one in controller-runtime because the existing one can't
Expand Down Expand Up @@ -65,17 +61,17 @@ func (v *SubnetSetValidator) Handle(ctx context.Context, req admission.Request)
if req.Operation == admissionv1.Delete {
err := v.decoder.DecodeRaw(req.OldObject, subnetSet)
if err != nil {
subnetsetlog.Error(err, "error while decoding SubnetSet", "SubnetSet", req.Namespace+"/"+req.Name)
log.Error(err, "error while decoding SubnetSet", "SubnetSet", req.Namespace+"/"+req.Name)
return admission.Errored(http.StatusBadRequest, err)
}
} else {
err := v.decoder.Decode(req, subnetSet)
if err != nil {
subnetsetlog.Error(err, "error while decoding SubnetSet", "SubnetSet", req.Namespace+"/"+req.Name)
log.Error(err, "error while decoding SubnetSet", "SubnetSet", req.Namespace+"/"+req.Name)
return admission.Errored(http.StatusBadRequest, err)
}
}
subnetsetlog.Info("request user-info", "name", req.UserInfo.Username)
log.Info("request user-info", "name", req.UserInfo.Username)
switch req.Operation {
case admissionv1.Create:
if !isDefaultSubnetSet(subnetSet) {
Expand All @@ -88,7 +84,7 @@ func (v *SubnetSetValidator) Handle(ctx context.Context, req admission.Request)
case admissionv1.Update:
oldSubnetSet := &v1alpha1.SubnetSet{}
if err := v.decoder.DecodeRaw(req.OldObject, oldSubnetSet); err != nil {
subnetsetlog.Error(err, "error while decoding SubnetSet", "SubnetSet", req.Namespace+"/"+req.Name)
log.Error(err, "error while decoding SubnetSet", "SubnetSet", req.Namespace+"/"+req.Name)
return admission.Errored(http.StatusBadRequest, err)
}
if defaultSubnetSetLabelChanged(oldSubnetSet, subnetSet) {
Expand Down
Loading