From 564a2e4f68ad6894b1ff35ef0936822b1a2e98d0 Mon Sep 17 00:00:00 2001 From: qiuwei Date: Thu, 25 Apr 2024 16:19:46 +0800 Subject: [PATCH] add ReleaseHostPort for api server and service Signed-off-by: qiuwei --- pkg/kubenest/controlplane/apiserver.go | 7 ++++++- pkg/kubenest/controlplane/service.go | 7 ++++++- pkg/kubenest/tasks/apiserver.go | 1 + pkg/kubenest/tasks/service.go | 1 + 4 files changed, 14 insertions(+), 2 deletions(-) diff --git a/pkg/kubenest/controlplane/apiserver.go b/pkg/kubenest/controlplane/apiserver.go index ca6cc2bc4..8bab406b0 100644 --- a/pkg/kubenest/controlplane/apiserver.go +++ b/pkg/kubenest/controlplane/apiserver.go @@ -8,6 +8,7 @@ import ( kuberuntime "k8s.io/apimachinery/pkg/runtime" clientset "k8s.io/client-go/kubernetes" clientsetscheme "k8s.io/client-go/kubernetes/scheme" + "k8s.io/klog/v2" "github.com/kosmos.io/kosmos/pkg/kubenest/constants" vcnodecontroller "github.com/kosmos.io/kosmos/pkg/kubenest/controller/virtualcluster.node.controller" @@ -27,11 +28,15 @@ func EnsureVirtualClusterAPIServer(client clientset.Interface, name, namespace s return nil } -func DeleteVirtualClusterAPIServer(client clientset.Interface, name, namespace string) error { +func DeleteVirtualClusterAPIServer(client clientset.Interface, name, namespace string, manager *vcnodecontroller.HostPortManager) error { deployName := fmt.Sprintf("%s-%s", name, "apiserver") if err := util.DeleteDeployment(client, deployName, namespace); err != nil { return errors.Wrapf(err, "Failed to delete deployment %s/%s", deployName, namespace) } + err := manager.ReleaseHostPort(name) + if err != nil { + klog.Errorf("Error releasing host port for cluster %s: %v", name, err) + } return nil } diff --git a/pkg/kubenest/controlplane/service.go b/pkg/kubenest/controlplane/service.go index be0822960..eeb412324 100644 --- a/pkg/kubenest/controlplane/service.go +++ b/pkg/kubenest/controlplane/service.go @@ -32,7 +32,7 @@ func EnsureVirtualClusterService(client clientset.Interface, name, namespace str return nil } -func DeleteVirtualClusterService(client clientset.Interface, name, namespace string) error { +func DeleteVirtualClusterService(client clientset.Interface, name, namespace string, manager *vcnodecontroller.HostPortManager) error { services := []string{ fmt.Sprintf("%s-%s", name, "apiserver"), fmt.Sprintf("%s-%s", name, "etcd"), @@ -49,6 +49,11 @@ func DeleteVirtualClusterService(client clientset.Interface, name, namespace str } } + err := manager.ReleaseHostPort(name) + if err != nil { + klog.Errorf("Error releasing host port for cluster %s: %v", name, err) + } + klog.V(2).Infof("Successfully uninstalled service for virtualcluster %s", name) return nil } diff --git a/pkg/kubenest/tasks/apiserver.go b/pkg/kubenest/tasks/apiserver.go index 8cc3a4cc8..6efa79024 100644 --- a/pkg/kubenest/tasks/apiserver.go +++ b/pkg/kubenest/tasks/apiserver.go @@ -101,6 +101,7 @@ func uninstallVirtualClusterAPIServer(r workflow.RunData) error { data.RemoteClient(), data.GetName(), data.GetNamespace(), + data.GetHostPortManager(), ) if err != nil { return fmt.Errorf("failed to install virtual cluster apiserver component, err: %w", err) diff --git a/pkg/kubenest/tasks/service.go b/pkg/kubenest/tasks/service.go index c906da217..b38018b57 100644 --- a/pkg/kubenest/tasks/service.go +++ b/pkg/kubenest/tasks/service.go @@ -78,6 +78,7 @@ func uninstallVirtualClusterService(r workflow.RunData) error { data.RemoteClient(), data.GetName(), data.GetNamespace(), + data.GetHostPortManager(), ) if err != nil { return fmt.Errorf("failed to uninstall virtual cluster service , err: %w", err)