From 1a12fe2d7aa8352c86648953b8d2841c72df82af Mon Sep 17 00:00:00 2001 From: Rory Z <16801068+Rory-Z@users.noreply.github.com> Date: Tue, 9 Apr 2024 13:59:38 +0800 Subject: [PATCH] fix(add_svc): fix service port error fix can not update kube service port when user update emqx's port by emqx dashboard. Signed-off-by: Rory Z <16801068+Rory-Z@users.noreply.github.com> --- config/samples/emqx/v2alpha1/emqx-full.yaml | 2 +- config/samples/emqx/v2alpha1/emqx-slim.yaml | 2 +- controllers/apps/v2beta1/add_svc.go | 33 +++++++++++++++++---- controllers/apps/v2beta1/emqx_controller.go | 2 +- 4 files changed, 31 insertions(+), 8 deletions(-) diff --git a/config/samples/emqx/v2alpha1/emqx-full.yaml b/config/samples/emqx/v2alpha1/emqx-full.yaml index a0deac021..88d0812e5 100644 --- a/config/samples/emqx/v2alpha1/emqx-full.yaml +++ b/config/samples/emqx/v2alpha1/emqx-full.yaml @@ -3,7 +3,7 @@ kind: EMQX metadata: name: emqx spec: - image: "emqx:5.1" + image: "emqx:5" imagePullPolicy: IfNotPresent # imagePullSecrets: # - name: fake-secrets diff --git a/config/samples/emqx/v2alpha1/emqx-slim.yaml b/config/samples/emqx/v2alpha1/emqx-slim.yaml index a309add30..848205bfe 100644 --- a/config/samples/emqx/v2alpha1/emqx-slim.yaml +++ b/config/samples/emqx/v2alpha1/emqx-slim.yaml @@ -3,4 +3,4 @@ kind: EMQX metadata: name: emqx spec: - image: emqx:5.1 + image: emqx:5 diff --git a/controllers/apps/v2beta1/add_svc.go b/controllers/apps/v2beta1/add_svc.go index 419d549e1..60d10d6de 100644 --- a/controllers/apps/v2beta1/add_svc.go +++ b/controllers/apps/v2beta1/add_svc.go @@ -2,6 +2,7 @@ package v2beta1 import ( "context" + "net/http" emperror "emperror.dev/errors" appsv2beta1 "github.com/emqx/emqx-operator/apis/apps/v2beta1" @@ -17,13 +18,20 @@ type addSvc struct { *EMQXReconciler } -func (a *addSvc) reconcile(ctx context.Context, logger logr.Logger, instance *appsv2beta1.EMQX, _ innerReq.RequesterInterface) subResult { - configMap := &corev1.ConfigMap{} - if err := a.Client.Get(ctx, instance.ConfigsNamespacedName(), configMap); err != nil { - return subResult{err: emperror.Wrap(err, "failed to get configmap")} +func (a *addSvc) reconcile(ctx context.Context, logger logr.Logger, instance *appsv2beta1.EMQX, r innerReq.RequesterInterface) subResult { + if r == nil { + return subResult{} + } + + if !instance.Status.IsConditionTrue(appsv2beta1.CoreNodesReady) { + return subResult{} + } + + configStr, err := a.getEMQXConfigsByAPI(r) + if err != nil { + return subResult{err: emperror.Wrap(err, "failed to get emqx configs by api")} } - configStr := configMap.Data["emqx.conf"] resources := []client.Object{generateHeadlessService(instance)} if dashboard := generateDashboardService(instance, configStr); dashboard != nil { resources = append(resources, dashboard) @@ -38,6 +46,21 @@ func (a *addSvc) reconcile(ctx context.Context, logger logr.Logger, instance *ap return subResult{} } +func (a *addSvc) getEMQXConfigsByAPI(r innerReq.RequesterInterface) (string, error) { + url := r.GetURL("api/v5/configs") + + resp, body, err := r.Request("GET", url, nil, http.Header{ + "Accept": []string{"text/plain"}, + }) + if err != nil { + return "", emperror.Wrapf(err, "failed to get API %s", url.String()) + } + if resp.StatusCode != 200 { + return "", emperror.Errorf("failed to get API %s, status : %s, body: %s", url.String(), resp.Status, body) + } + return string(body), nil +} + func generateHeadlessService(instance *appsv2beta1.EMQX) *corev1.Service { headlessSvc := &corev1.Service{ TypeMeta: metav1.TypeMeta{ diff --git a/controllers/apps/v2beta1/emqx_controller.go b/controllers/apps/v2beta1/emqx_controller.go index 08c76b31a..d64d7f6f1 100644 --- a/controllers/apps/v2beta1/emqx_controller.go +++ b/controllers/apps/v2beta1/emqx_controller.go @@ -123,10 +123,10 @@ func (r *EMQXReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl. &addBootstrap{r}, &updateStatus{r}, &syncConfig{r}, - &addSvc{r}, &addCore{r}, &addRepl{r}, &addPdb{r}, + &addSvc{r}, &updatePodConditions{r}, &updateStatus{r}, &syncPods{r},