From 7547e6858ce15cd88267119becb484648d861b75 Mon Sep 17 00:00:00 2001 From: Rafal Korepta Date: Wed, 17 Mar 2021 05:24:47 +0100 Subject: [PATCH] (split) operator: Change the external connectivity occurrence In the source code where the boolean external connectivity was used the Enabled access was added. In the test the new struct was initialized. --- apis/redpanda/v1alpha1/cluster_webhook.go | 4 ++-- .../redpanda/v1alpha1/cluster_webhook_test.go | 23 +++++++++++++------ controllers/redpanda/cluster_controller.go | 2 +- .../redpanda/cluster_controller_test.go | 5 +++- pkg/resources/cluster_role.go | 2 +- pkg/resources/cluster_role_binding.go | 2 +- pkg/resources/node_port_service.go | 2 +- pkg/resources/service_account.go | 2 +- pkg/resources/statefulset.go | 10 ++++---- 9 files changed, 32 insertions(+), 20 deletions(-) diff --git a/apis/redpanda/v1alpha1/cluster_webhook.go b/apis/redpanda/v1alpha1/cluster_webhook.go index 152669cdf362c..2313fbf8aab8d 100644 --- a/apis/redpanda/v1alpha1/cluster_webhook.go +++ b/apis/redpanda/v1alpha1/cluster_webhook.go @@ -162,14 +162,14 @@ func (r *Cluster) checkCollidingPorts() field.ErrorList { "admin port collide with Spec.Configuration.RPCServer.Port")) } - if r.Spec.ExternalConnectivity && r.Spec.Configuration.KafkaAPI.Port+1 == r.Spec.Configuration.RPCServer.Port { + if r.Spec.ExternalConnectivity.Enabled && r.Spec.Configuration.KafkaAPI.Port+1 == r.Spec.Configuration.RPCServer.Port { allErrs = append(allErrs, field.Invalid(field.NewPath("spec").Child("configuration", "rpcServer", "port"), r.Spec.Configuration.RPCServer.Port, "rpc port collide with external Kafka API that is not visible in the Cluster CR")) } - if r.Spec.ExternalConnectivity && r.Spec.Configuration.KafkaAPI.Port+1 == r.Spec.Configuration.AdminAPI.Port { + if r.Spec.ExternalConnectivity.Enabled && r.Spec.Configuration.KafkaAPI.Port+1 == r.Spec.Configuration.AdminAPI.Port { allErrs = append(allErrs, field.Invalid(field.NewPath("spec").Child("configuration", "admin", "port"), r.Spec.Configuration.AdminAPI.Port, diff --git a/apis/redpanda/v1alpha1/cluster_webhook_test.go b/apis/redpanda/v1alpha1/cluster_webhook_test.go index 688743e7d316c..a51c843e98893 100644 --- a/apis/redpanda/v1alpha1/cluster_webhook_test.go +++ b/apis/redpanda/v1alpha1/cluster_webhook_test.go @@ -41,7 +41,10 @@ func TestValidateUpdate(t *testing.T) { corev1.ResourceMemory: resource.MustParse("1Gi"), }, }, - ExternalConnectivity: false, + ExternalConnectivity: v1alpha1.ExternalConnectivityConfig{ + Enabled: false, + Subdomain: "", + }, }, } @@ -101,7 +104,10 @@ func TestValidateUpdate_NoError(t *testing.T) { corev1.ResourceMemory: resource.MustParse("2Gi"), }, }, - ExternalConnectivity: false, + ExternalConnectivity: v1alpha1.ExternalConnectivityConfig{ + Enabled: false, + Subdomain: "", + }, }, } @@ -138,7 +144,7 @@ func TestValidateUpdate_NoError(t *testing.T) { t.Run("collision in the port when external connectivity is enabled", func(t *testing.T) { updatePort := redpandaCluster.DeepCopy() - updatePort.Spec.ExternalConnectivity = true + updatePort.Spec.ExternalConnectivity.Enabled = true updatePort.Spec.Configuration.KafkaAPI.Port = 200 updatePort.Spec.Configuration.AdminAPI.Port = 201 updatePort.Spec.Configuration.RPCServer.Port = 300 @@ -149,7 +155,7 @@ func TestValidateUpdate_NoError(t *testing.T) { t.Run("collision in the port when external connectivity is enabled", func(t *testing.T) { updatePort := redpandaCluster.DeepCopy() - updatePort.Spec.ExternalConnectivity = true + updatePort.Spec.ExternalConnectivity.Enabled = true updatePort.Spec.Configuration.KafkaAPI.Port = 200 updatePort.Spec.Configuration.AdminAPI.Port = 300 updatePort.Spec.Configuration.RPCServer.Port = 201 @@ -185,7 +191,10 @@ func TestCreation(t *testing.T) { corev1.ResourceMemory: resource.MustParse("2G"), }, }, - ExternalConnectivity: false, + ExternalConnectivity: v1alpha1.ExternalConnectivityConfig{ + Enabled: false, + Subdomain: "", + }, }, } @@ -209,7 +218,7 @@ func TestCreation(t *testing.T) { t.Run("collision in the port when external connectivity is enabled", func(t *testing.T) { newPort := redpandaCluster.DeepCopy() - newPort.Spec.ExternalConnectivity = true + newPort.Spec.ExternalConnectivity.Enabled = true newPort.Spec.Configuration.KafkaAPI.Port = 200 newPort.Spec.Configuration.AdminAPI.Port = 201 newPort.Spec.Configuration.RPCServer.Port = 300 @@ -220,7 +229,7 @@ func TestCreation(t *testing.T) { t.Run("collision in the port when external connectivity is enabled", func(t *testing.T) { newPort := redpandaCluster.DeepCopy() - newPort.Spec.ExternalConnectivity = true + newPort.Spec.ExternalConnectivity.Enabled = true newPort.Spec.Configuration.KafkaAPI.Port = 200 newPort.Spec.Configuration.AdminAPI.Port = 300 newPort.Spec.Configuration.RPCServer.Port = 201 diff --git a/controllers/redpanda/cluster_controller.go b/controllers/redpanda/cluster_controller.go index 34ea3b4ff9d20..6a33e1b7401aa 100644 --- a/controllers/redpanda/cluster_controller.go +++ b/controllers/redpanda/cluster_controller.go @@ -201,7 +201,7 @@ func (r *ClusterReconciler) createExternalNodesList( pandaCluster *redpandav1alpha1.Cluster, nodePortName types.NamespacedName, ) ([]string, error) { - if !pandaCluster.Spec.ExternalConnectivity { + if !pandaCluster.Spec.ExternalConnectivity.Enabled { return []string{}, nil } diff --git a/controllers/redpanda/cluster_controller_test.go b/controllers/redpanda/cluster_controller_test.go index b77f00ec3b577..9421dc97da852 100644 --- a/controllers/redpanda/cluster_controller_test.go +++ b/controllers/redpanda/cluster_controller_test.go @@ -80,7 +80,10 @@ var _ = Describe("RedPandaCluster controller", func() { Limits: resources, Requests: resources, }, - ExternalConnectivity: true, + ExternalConnectivity: v1alpha1.ExternalConnectivityConfig{ + Enabled: true, + Subdomain: "", + }, }, } Expect(k8sClient.Create(context.Background(), redpandaCluster)).Should(Succeed()) diff --git a/pkg/resources/cluster_role.go b/pkg/resources/cluster_role.go index 3e3fe7b8fb300..f0caba0bb44c7 100644 --- a/pkg/resources/cluster_role.go +++ b/pkg/resources/cluster_role.go @@ -50,7 +50,7 @@ func NewClusterRole( // Ensure manages v1.ClusterRole that is assigned to v1.ServiceAccount used in initContainer func (r *ClusterRoleResource) Ensure(ctx context.Context) error { - if !r.pandaCluster.Spec.ExternalConnectivity { + if !r.pandaCluster.Spec.ExternalConnectivity.Enabled { return nil } _, err := CreateIfNotExists(ctx, r, r.obj(), r.logger) diff --git a/pkg/resources/cluster_role_binding.go b/pkg/resources/cluster_role_binding.go index 6d328f8edddb1..99b1445e054a0 100644 --- a/pkg/resources/cluster_role_binding.go +++ b/pkg/resources/cluster_role_binding.go @@ -51,7 +51,7 @@ func NewClusterRoleBinding( // Ensure manages v1.ClusterRoleBinding that is assigned to v1.ServiceAccount used in initContainer func (r *ClusterRoleBindingResource) Ensure(ctx context.Context) error { - if !r.pandaCluster.Spec.ExternalConnectivity { + if !r.pandaCluster.Spec.ExternalConnectivity.Enabled { return nil } diff --git a/pkg/resources/node_port_service.go b/pkg/resources/node_port_service.go index c6dc254045055..8a1853d720cd7 100644 --- a/pkg/resources/node_port_service.go +++ b/pkg/resources/node_port_service.go @@ -53,7 +53,7 @@ func NewNodePortService( // Ensure will manage kubernetes v1.Service for redpanda.vectorized.io custom resource func (r *NodePortServiceResource) Ensure(ctx context.Context) error { - if !r.pandaCluster.Spec.ExternalConnectivity { + if !r.pandaCluster.Spec.ExternalConnectivity.Enabled { return nil } diff --git a/pkg/resources/service_account.go b/pkg/resources/service_account.go index 3a08f754621b6..4ae08023bb39d 100644 --- a/pkg/resources/service_account.go +++ b/pkg/resources/service_account.go @@ -51,7 +51,7 @@ func NewServiceAccount( // Ensure manages ServiceAccount that is used in initContainer func (s *ServiceAccountResource) Ensure(ctx context.Context) error { - if !s.pandaCluster.Spec.ExternalConnectivity { + if !s.pandaCluster.Spec.ExternalConnectivity.Enabled { return nil } diff --git a/pkg/resources/statefulset.go b/pkg/resources/statefulset.go index 3dabebff4cbeb..9064afd4d97a6 100644 --- a/pkg/resources/statefulset.go +++ b/pkg/resources/statefulset.go @@ -106,7 +106,7 @@ func NewStatefulSet( func (r *StatefulSetResource) Ensure(ctx context.Context) error { var sts appsv1.StatefulSet - if r.pandaCluster.Spec.ExternalConnectivity { + if r.pandaCluster.Spec.ExternalConnectivity.Enabled { err := r.Get(ctx, r.nodePortName, &r.nodePortSvc) if err != nil { return fmt.Errorf("failed to retrieve node port service %s: %w", r.nodePortName, err) @@ -289,7 +289,7 @@ func (r *StatefulSetResource) obj() (k8sclient.Object, error) { }, { Name: "EXTERNAL_CONNECTIVITY", - Value: strconv.FormatBool(r.pandaCluster.Spec.ExternalConnectivity), + Value: strconv.FormatBool(r.pandaCluster.Spec.ExternalConnectivity.Enabled), }, { Name: "HOST_PORT", @@ -493,14 +493,14 @@ func (r *StatefulSetResource) secretVolumes() []corev1.Volume { } func (r *StatefulSetResource) getNodePort() string { - if r.pandaCluster.Spec.ExternalConnectivity { + if r.pandaCluster.Spec.ExternalConnectivity.Enabled { return strconv.FormatInt(int64(r.nodePortSvc.Spec.Ports[0].NodePort), 10) } return "" } func (r *StatefulSetResource) getServiceAccountName() string { - if r.pandaCluster.Spec.ExternalConnectivity { + if r.pandaCluster.Spec.ExternalConnectivity.Enabled { return r.serviceAccountName } return "" @@ -522,7 +522,7 @@ func (r *StatefulSetResource) portsConfiguration() string { } func (r *StatefulSetResource) getPorts() []corev1.ContainerPort { - if r.pandaCluster.Spec.ExternalConnectivity && + if r.pandaCluster.Spec.ExternalConnectivity.Enabled && len(r.nodePortSvc.Spec.Ports) > 0 { return []corev1.ContainerPort{ {