Skip to content

Commit

Permalink
feat: add option to disable creation of services
Browse files Browse the repository at this point in the history
Fix #943

Signed-off-by: Rory Z <[email protected]>
  • Loading branch information
Rory-Z committed Sep 8, 2023
1 parent de8d31d commit 17aaf5f
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 21 deletions.
3 changes: 3 additions & 0 deletions apis/apps/v2beta1/emqx_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,9 @@ type EMQXReplicantTemplateSpec struct {
}

type ServiceTemplate struct {
// EMQX Operator will create a service for EMQX nodes.
//+kubebuilder:default:=true
Enabled bool `json:"enabled,omitempty"`
// Standard object's metadata.
// More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#metadata
metav1.ObjectMeta `json:"metadata,omitempty"`
Expand Down
6 changes: 6 additions & 0 deletions config/crd/bases/apps.emqx.io_emqxes.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9572,6 +9572,9 @@ spec:
type: object
dashboardServiceTemplate:
properties:
enabled:
default: true
type: boolean
metadata:
properties:
annotations:
Expand Down Expand Up @@ -9695,6 +9698,9 @@ spec:
type: array
listenersServiceTemplate:
properties:
enabled:
default: true
type: boolean
metadata:
properties:
annotations:
Expand Down
49 changes: 28 additions & 21 deletions controllers/apps/v2beta1/add_svc.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,17 @@ func (a *addSvc) reconcile(ctx context.Context, instance *appsv2beta1.EMQX, _ in
}

configStr := configMap.Data["emqx.conf"]
resources := []client.Object{
generateHeadlessService(instance),
generateDashboardService(instance, configStr),
generateListenerService(instance, configStr),
resources := []client.Object{generateHeadlessService(instance)}
if dashboard := generateDashboardService(instance, configStr); dashboard != nil {
resources = append(resources, dashboard)
}
if listeners := generateListenerService(instance, configStr); listeners != nil {
resources = append(resources, listeners)
}

if err := a.CreateOrUpdateList(instance, a.Scheme, resources); err != nil {
return subResult{err: emperror.Wrap(err, "failed to create or update services")}
}

return subResult{}
}

Expand Down Expand Up @@ -73,6 +74,15 @@ func generateHeadlessService(instance *appsv2beta1.EMQX) *corev1.Service {
}

func generateDashboardService(instance *appsv2beta1.EMQX, configStr string) *corev1.Service {
svc := &corev1.Service{}
if instance.Spec.DashboardServiceTemplate != nil {
if !instance.Spec.DashboardServiceTemplate.Enabled {
return nil
}
svc.ObjectMeta = *instance.Spec.DashboardServiceTemplate.ObjectMeta.DeepCopy()
svc.Spec = *instance.Spec.DashboardServiceTemplate.Spec.DeepCopy()
}

port, err := appsv2beta1.GetDashboardServicePort(configStr)
if err != nil {
port = &corev1.ServicePort{
Expand All @@ -83,12 +93,6 @@ func generateDashboardService(instance *appsv2beta1.EMQX, configStr string) *cor
}
}

svc := &corev1.Service{}
if instance.Spec.DashboardServiceTemplate != nil {
svc.ObjectMeta = *instance.Spec.DashboardServiceTemplate.ObjectMeta.DeepCopy()
svc.Spec = *instance.Spec.DashboardServiceTemplate.Spec.DeepCopy()
}

svc.Spec.Ports = appsv2beta1.MergeServicePorts(
svc.Spec.Ports,
[]corev1.ServicePort{
Expand All @@ -105,14 +109,23 @@ func generateDashboardService(instance *appsv2beta1.EMQX, configStr string) *cor
ObjectMeta: metav1.ObjectMeta{
Namespace: instance.Namespace,
Name: instance.DashboardServiceNamespacedName().Name,
Labels: appsv2beta1.CloneAndMergeMap(appsv2beta1.DefaultLabels(instance), svc.Labels),
Annotations: svc.Annotations,
Labels: appsv2beta1.CloneAndMergeMap(appsv2beta1.DefaultLabels(instance), svc.ObjectMeta.Labels),
Annotations: svc.ObjectMeta.Annotations,
},
Spec: svc.Spec,
}
}

func generateListenerService(instance *appsv2beta1.EMQX, configStr string) *corev1.Service {
svc := &corev1.Service{}
if instance.Spec.ListenersServiceTemplate != nil {
if !instance.Spec.ListenersServiceTemplate.Enabled {
return nil
}
svc.ObjectMeta = *instance.Spec.ListenersServiceTemplate.ObjectMeta.DeepCopy()
svc.Spec = *instance.Spec.ListenersServiceTemplate.Spec.DeepCopy()
}

ports, _ := appsv2beta1.GetListenersServicePorts(configStr)
if len(ports) == 0 {
ports = append(ports, []corev1.ServicePort{
Expand Down Expand Up @@ -143,12 +156,6 @@ func generateListenerService(instance *appsv2beta1.EMQX, configStr string) *core
}...)
}

svc := &corev1.Service{}
if instance.Spec.ListenersServiceTemplate != nil {
svc.ObjectMeta = *instance.Spec.ListenersServiceTemplate.ObjectMeta.DeepCopy()
svc.Spec = *instance.Spec.ListenersServiceTemplate.Spec.DeepCopy()
}

svc.Spec.Ports = appsv2beta1.MergeServicePorts(
svc.Spec.Ports,
ports,
Expand All @@ -165,8 +172,8 @@ func generateListenerService(instance *appsv2beta1.EMQX, configStr string) *core
ObjectMeta: metav1.ObjectMeta{
Namespace: instance.Namespace,
Name: instance.ListenersServiceNamespacedName().Name,
Labels: appsv2beta1.CloneAndMergeMap(appsv2beta1.DefaultLabels(instance), svc.Labels),
Annotations: svc.Annotations,
Labels: appsv2beta1.CloneAndMergeMap(appsv2beta1.DefaultLabels(instance), svc.ObjectMeta.Labels),
Annotations: svc.ObjectMeta.Annotations,
},
Spec: svc.Spec,
}
Expand Down
1 change: 1 addition & 0 deletions controllers/apps/v2beta1/add_svc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ func TestGenerateDashboardService(t *testing.T) {
},
},
DashboardServiceTemplate: &appsv2beta1.ServiceTemplate{
Enabled: true,
ObjectMeta: metav1.ObjectMeta{
Name: "emqx-dashboard",
Labels: map[string]string{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9584,6 +9584,9 @@ spec:
type: object
dashboardServiceTemplate:
properties:
enabled:
default: true
type: boolean
metadata:
properties:
annotations:
Expand Down Expand Up @@ -9707,6 +9710,9 @@ spec:
type: array
listenersServiceTemplate:
properties:
enabled:
default: true
type: boolean
metadata:
properties:
annotations:
Expand Down
1 change: 1 addition & 0 deletions docs/en_US/reference/v2beta1-reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -486,6 +486,7 @@ _Appears in:_

| Field | Description |
| --- | --- |
| `enabled` _boolean_ | EMQX Operator will create a service for EMQX nodes. |
| `metadata` _[ObjectMeta](https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.22/#objectmeta-v1-meta)_ | Refer to Kubernetes API documentation for fields of `metadata`. |
| `spec` _[ServiceSpec](https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.22/#servicespec-v1-core)_ | Spec defines the behavior of a service. https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#spec-and-status |

Expand Down
1 change: 1 addition & 0 deletions docs/zh_CN/reference/v2beta1-reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -486,6 +486,7 @@ _Appears in:_

| Field | Description |
| --- | --- |
| `enabled` _boolean_ | EMQX Operator will create a service for EMQX nodes. |
| `metadata` _[ObjectMeta](https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.22/#objectmeta-v1-meta)_ | Refer to Kubernetes API documentation for fields of `metadata`. |
| `spec` _[ServiceSpec](https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.22/#servicespec-v1-core)_ | Spec defines the behavior of a service. https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#spec-and-status |

Expand Down

0 comments on commit 17aaf5f

Please sign in to comment.