Skip to content

Commit

Permalink
fix(add_svc): fix service port error
Browse files Browse the repository at this point in the history
fix can not update kube service port when user update emqx's port by
emqx dashboard.

Signed-off-by: Rory Z <[email protected]>
  • Loading branch information
Rory-Z committed Apr 9, 2024
1 parent a761bf3 commit acaa34e
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 6 deletions.
33 changes: 28 additions & 5 deletions controllers/apps/v2beta1/add_svc.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package v2beta1

import (
"context"
"net/http"

emperror "emperror.dev/errors"
appsv2beta1 "github.com/emqx/emqx-operator/apis/apps/v2beta1"
Expand All @@ -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)
Expand All @@ -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{
Expand Down
2 changes: 1 addition & 1 deletion controllers/apps/v2beta1/emqx_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down

0 comments on commit acaa34e

Please sign in to comment.