From c8de7e2454a04b30b961516b869330b95dbe3cc5 Mon Sep 17 00:00:00 2001 From: Kfir Toledo Date: Wed, 26 Jun 2024 16:01:22 +0300 Subject: [PATCH] Create single dervice to dataplane pod Signed-off-by: Kfir Toledo --- cmd/cl-dataplane/app/server.go | 2 ++ pkg/bootstrap/platform/k8s.go | 24 +++++++------------ pkg/controlplane/control/manager.go | 2 +- .../controller/instance_controller.go | 11 +++------ .../controller/instance_controller_test.go | 5 ++-- tests/e2e/k8s/test_import.go | 3 ++- tests/e2e/k8s/util/fabric.go | 4 ++-- 7 files changed, 21 insertions(+), 30 deletions(-) diff --git a/cmd/cl-dataplane/app/server.go b/cmd/cl-dataplane/app/server.go index ffac48945..c53363ae0 100644 --- a/cmd/cl-dataplane/app/server.go +++ b/cmd/cl-dataplane/app/server.go @@ -38,6 +38,8 @@ const ( // Name is the app label of dataplane pods. Name = "cl-dataplane" + // IngressSvcName is the ingress service name for the dataplane pods. + IngressSvcName = "cl-dataplane" ) // Options contains everything necessary to create and run a dataplane. diff --git a/pkg/bootstrap/platform/k8s.go b/pkg/bootstrap/platform/k8s.go index 09ac1f288..ed07cd239 100644 --- a/pkg/bootstrap/platform/k8s.go +++ b/pkg/bootstrap/platform/k8s.go @@ -24,6 +24,7 @@ import ( apis "github.com/clusterlink-net/clusterlink/pkg/apis/clusterlink.net/v1alpha1" cpapi "github.com/clusterlink-net/clusterlink/pkg/controlplane/api" dpapi "github.com/clusterlink-net/clusterlink/pkg/dataplane/api" + corev1 "k8s.io/api/core/v1" ) const ( @@ -186,18 +187,6 @@ spec: - name: controlplane port: {{.controlplanePort}} --- -apiVersion: v1 -kind: Service -metadata: - name: cl-dataplane - namespace: {{.namespace}} -spec: - selector: - app: {{ .dataplaneAppName }} - ports: - - name: dataplane - port: {{.dataplanePort}} ---- apiVersion: rbac.authorization.k8s.io/v1 kind: ClusterRole metadata: @@ -273,7 +262,7 @@ spec: apiVersion: v1 kind: Service metadata: - name: clusterlink + name: {{.dataplaneService}} namespace: {{.namespace}} spec: type: {{.ingressType}} @@ -450,15 +439,18 @@ func K8SEmptyCertificateConfig(config *Config) ([]byte, error) { // k8SIngressConfig returns a kubernetes ingress service. func k8SIngressConfig(config *Config) ([]byte, error) { var ingressConfig bytes.Buffer - if config.IngressType == "" { - return ingressConfig.Bytes(), nil + + ingressType := string(corev1.ServiceTypeClusterIP) + if config.IngressType == string(apis.IngressTypeNodePort) || config.IngressType == string(apis.IngressTypeLoadBalancer) { + ingressType = config.IngressType } args := map[string]interface{}{ "namespace": config.Namespace, "ingressPort": apis.DefaultExternalPort, - "ingressType": config.IngressType, + "ingressType": ingressType, + "dataplaneService": dpapp.IngressSvcName, "dataplaneAppName": dpapp.Name, "dataplanePort": dpapi.ListenPort, } diff --git a/pkg/controlplane/control/manager.go b/pkg/controlplane/control/manager.go index 4453f7677..53fb68335 100644 --- a/pkg/controlplane/control/manager.go +++ b/pkg/controlplane/control/manager.go @@ -808,7 +808,7 @@ func (m *Manager) addImportEndpointSlices(ctx context.Context, imp *v1alpha1.Imp err := m.client.List( ctx, &dataplaneEndpointSliceList, - client.MatchingLabels{discv1.LabelServiceName: dpapp.Name}, + client.MatchingLabels{discv1.LabelServiceName: dpapp.IngressSvcName}, client.InNamespace(m.namespace)) if err != nil { return err diff --git a/pkg/operator/controller/instance_controller.go b/pkg/operator/controller/instance_controller.go index f3b128268..bdb44acbf 100644 --- a/pkg/operator/controller/instance_controller.go +++ b/pkg/operator/controller/instance_controller.go @@ -44,7 +44,6 @@ const ( ControlPlaneName = "cl-controlplane" DataPlaneName = "cl-dataplane" GoDataPlaneName = "cl-go-dataplane" - IngressName = "clusterlink" OperatorNamespace = "clusterlink-operator" InstanceNamespace = "clusterlink-system" FinalizerName = "instance.clusterlink.net/finalizer" @@ -204,10 +203,6 @@ func (r *InstanceReconciler) applyClusterLink(ctx context.Context, instance *clu } // Create datapalne components - if err := r.createService(ctx, DataPlaneName, instance.Spec.Namespace, dpapi.ListenPort); err != nil { - return err - } - if err := r.applyDataplane(ctx, instance); err != nil { return err } @@ -516,7 +511,7 @@ func (r *InstanceReconciler) createExternalService(ctx context.Context, instance // Create a Service object service := &corev1.Service{ ObjectMeta: metav1.ObjectMeta{ - Name: IngressName, + Name: dpapp.IngressSvcName, Namespace: instance.Spec.Namespace, Annotations: instance.Spec.Ingress.Annotations, }, @@ -632,7 +627,7 @@ func (r *InstanceReconciler) deleteClusterLink(ctx context.Context, namespace st } // Delete external ingress service - ingerssObj := metav1.ObjectMeta{Name: IngressName, Namespace: namespace} + ingerssObj := metav1.ObjectMeta{Name: dpapp.IngressSvcName, Namespace: namespace} return r.deleteResource(ctx, &corev1.Service{ObjectMeta: ingerssObj}) } @@ -720,7 +715,7 @@ func (r *InstanceReconciler) checkDataplaneStatus(ctx context.Context, instance // checkIngressStatus check the status of the ingress components. func (r *InstanceReconciler) checkIngressStatus(ctx context.Context, instance *clusterlink.Instance) (bool, error) { - ingress := types.NamespacedName{Name: IngressName, Namespace: instance.Spec.Namespace} + ingress := types.NamespacedName{Name: dpapp.IngressSvcName, Namespace: instance.Spec.Namespace} serviceStatus, err := r.checkExternalServiceStatus(ctx, ingress, &instance.Status.Ingress) if err != nil { return false, err diff --git a/pkg/operator/controller/instance_controller_test.go b/pkg/operator/controller/instance_controller_test.go index 523219e5e..a00e21ed0 100644 --- a/pkg/operator/controller/instance_controller_test.go +++ b/pkg/operator/controller/instance_controller_test.go @@ -35,6 +35,7 @@ import ( "sigs.k8s.io/controller-runtime/pkg/client" "sigs.k8s.io/controller-runtime/pkg/envtest" + dpapp "github.com/clusterlink-net/clusterlink/cmd/cl-dataplane/app" clusterlink "github.com/clusterlink-net/clusterlink/pkg/apis/clusterlink.net/v1alpha1" "github.com/clusterlink-net/clusterlink/pkg/operator/controller" ) @@ -150,8 +151,8 @@ func TestClusterLinkController(t *testing.T) { } roleResource := []client.Object{&rbacv1.ClusterRole{}, &rbacv1.ClusterRoleBinding{}} dpID := types.NamespacedName{Name: controller.DataPlaneName, Namespace: controller.InstanceNamespace} - dpResource := []client.Object{&appsv1.Deployment{}, &corev1.Service{}} - ingressID := types.NamespacedName{Name: controller.IngressName, Namespace: controller.InstanceNamespace} + dpResource := []client.Object{&appsv1.Deployment{}} + ingressID := types.NamespacedName{Name: dpapp.IngressSvcName, Namespace: controller.InstanceNamespace} t.Run("Create ClusterLink deployment", func(t *testing.T) { // Create ClusterLink namespaces diff --git a/tests/e2e/k8s/test_import.go b/tests/e2e/k8s/test_import.go index 5bad2b92c..4d6f0f8eb 100644 --- a/tests/e2e/k8s/test_import.go +++ b/tests/e2e/k8s/test_import.go @@ -23,6 +23,7 @@ import ( metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/types" + dpapp "github.com/clusterlink-net/clusterlink/cmd/cl-dataplane/app" "github.com/clusterlink-net/clusterlink/pkg/apis/clusterlink.net/v1alpha1" "github.com/clusterlink-net/clusterlink/pkg/controlplane/control" "github.com/clusterlink-net/clusterlink/tests/e2e/k8s/services" @@ -407,7 +408,7 @@ func (s *TestSuite) TestImportMerge() { // delete dataplane endpoint slice by deleting the dataplane service var dataplaneService v1.Service require.Nil(s.T(), cl[0].Cluster().Resources().Get( - context.Background(), "cl-dataplane", cl[0].Namespace(), &dataplaneService)) + context.Background(), dpapp.IngressSvcName, cl[0].Namespace(), &dataplaneService)) require.Nil(s.T(), cl[0].Cluster().Resources().Delete( context.Background(), &dataplaneService)) diff --git a/tests/e2e/k8s/util/fabric.go b/tests/e2e/k8s/util/fabric.go index 15b953053..e53491faa 100644 --- a/tests/e2e/k8s/util/fabric.go +++ b/tests/e2e/k8s/util/fabric.go @@ -25,6 +25,7 @@ import ( "sigs.k8s.io/e2e-framework/klient/wait" "sigs.k8s.io/e2e-framework/klient/wait/conditions" + dpapp "github.com/clusterlink-net/clusterlink/cmd/cl-dataplane/app" "github.com/clusterlink-net/clusterlink/cmd/clusterlink/config" "github.com/clusterlink-net/clusterlink/pkg/apis/clusterlink.net/v1alpha1" "github.com/clusterlink-net/clusterlink/pkg/bootstrap" @@ -258,9 +259,8 @@ func (f *Fabric) deployClusterLink(target *peer, cfg *PeerConfig) (*ClusterLink, return nil, fmt.Errorf("namespace not set") } - svcNodePort := "cl-dataplane" + svcNodePort := dpapp.IngressSvcName if cfg.DeployWithOperator { - svcNodePort = controller.IngressName deployFunc = f.deployUsingOperator } else { deployFunc = f.deployUsingK8sYAML