Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: fix sometimes will update emqx config #992

Merged
merged 1 commit into from
Jan 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions apis/apps/v2beta1/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ const (
LabelsPodTemplateHashKey string = "apps.emqx.io/pod-template-hash"
)

const (
// annotations
AnnotationsLastEMQXConfigKey string = "apps.emqx.io/last-emqx-configuration"
)

const (
// https://kubernetes.io/docs/concepts/workloads/pods/pod-lifecycle/#pod-readiness-gate
PodOnServing corev1.PodConditionType = "apps.emqx.io/on-serving"
Expand Down
67 changes: 42 additions & 25 deletions controllers/apps/v2beta1/sync_emqx_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@
"context"
"fmt"
"net/http"
"reflect"
"strings"

emperror "emperror.dev/errors"
appsv2beta1 "github.com/emqx/emqx-operator/apis/apps/v2beta1"
innerReq "github.com/emqx/emqx-operator/internal/requester"
"github.com/rory-z/go-hocon"
corev1 "k8s.io/api/core/v1"
k8sErrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/controller-runtime/pkg/client"
)
Expand All @@ -21,32 +21,19 @@
}

func (s *syncConfig) reconcile(ctx context.Context, instance *appsv2beta1.EMQX, r innerReq.RequesterInterface) subResult {
defaultListenerConfig := ""
defaultListenerConfig += fmt.Sprintln("listeners.tcp.default.bind = 1883")
defaultListenerConfig += fmt.Sprintln("listeners.ssl.default.bind = 8883")
defaultListenerConfig += fmt.Sprintln("listeners.ws.default.bind = 8083")
defaultListenerConfig += fmt.Sprintln("listeners.wss.default.bind = 8084")

hoconConfig, _ := hocon.ParseString(defaultListenerConfig + instance.Spec.Config.Data)
configMap := generateConfigMap(instance, hoconConfig.String())
hoconConfig := mergeDefaultConfig(instance.Spec.Config.Data)
confStr := hoconConfig.String()

storageConfigMap := &corev1.ConfigMap{}
if err := s.Client.Get(ctx, client.ObjectKeyFromObject(configMap), storageConfigMap); err != nil {
if k8sErrors.IsNotFound(err) {
if err := s.Handler.Create(configMap); err != nil {
return subResult{err: emperror.Wrap(err, "failed to create configMap")}
}
return subResult{}
lastConfigStr, ok := instance.Annotations[appsv2beta1.AnnotationsLastEMQXConfigKey]
if !ok {
if err := s.update(ctx, instance, confStr); err != nil {
return subResult{err: emperror.Wrap(err, "failed to update emqx config")}

Check warning on line 30 in controllers/apps/v2beta1/sync_emqx_config.go

View check run for this annotation

Codecov / codecov/patch

controllers/apps/v2beta1/sync_emqx_config.go#L30

Added line #L30 was not covered by tests
}
return subResult{err: emperror.Wrap(err, "failed to get configMap")}
return subResult{}
}

patchResult, _ := s.Patcher.Calculate(
storageConfigMap.DeepCopy(),
configMap.DeepCopy(),
)

if !patchResult.IsEmpty() && r != nil {
lastHoconConfig, _ := hocon.ParseString(lastConfigStr)
if !reflect.DeepEqual(hoconConfig, lastHoconConfig) {
_, coreReady := instance.Status.GetCondition(appsv2beta1.CoreNodesReady)
if coreReady == nil || !instance.Status.IsConditionTrue(appsv2beta1.CoreNodesReady) {
return subResult{}
Expand All @@ -62,14 +49,44 @@
return subResult{err: emperror.Wrap(err, "failed to put emqx config")}
}

if err := s.Client.Update(ctx, configMap); err != nil {
return subResult{err: emperror.Wrap(err, "failed to update configMap")}
if err := s.update(ctx, instance, confStr); err != nil {
return subResult{err: emperror.Wrap(err, "failed to update emqx config")}

Check warning on line 53 in controllers/apps/v2beta1/sync_emqx_config.go

View check run for this annotation

Codecov / codecov/patch

controllers/apps/v2beta1/sync_emqx_config.go#L53

Added line #L53 was not covered by tests
}

return subResult{}
}

return subResult{}
}

func (s *syncConfig) update(ctx context.Context, instance *appsv2beta1.EMQX, confStr string) error {
configMap := generateConfigMap(instance, confStr)
if err := s.Handler.CreateOrUpdateList(instance, s.Scheme, []client.Object{configMap}); err != nil {
return emperror.Wrap(err, "failed to create or update configMap")
}

Check warning on line 66 in controllers/apps/v2beta1/sync_emqx_config.go

View check run for this annotation

Codecov / codecov/patch

controllers/apps/v2beta1/sync_emqx_config.go#L65-L66

Added lines #L65 - L66 were not covered by tests

if instance.Annotations == nil {
instance.Annotations = map[string]string{}
}
instance.Annotations[appsv2beta1.AnnotationsLastEMQXConfigKey] = confStr
if err := s.Client.Update(ctx, instance); err != nil {
return emperror.Wrap(err, "failed to update emqx instance annotation")
}

Check warning on line 74 in controllers/apps/v2beta1/sync_emqx_config.go

View check run for this annotation

Codecov / codecov/patch

controllers/apps/v2beta1/sync_emqx_config.go#L73-L74

Added lines #L73 - L74 were not covered by tests

return nil
}

func mergeDefaultConfig(config string) *hocon.Config {
defaultListenerConfig := ""
defaultListenerConfig += fmt.Sprintln("listeners.tcp.default.bind = 1883")
defaultListenerConfig += fmt.Sprintln("listeners.ssl.default.bind = 8883")
defaultListenerConfig += fmt.Sprintln("listeners.ws.default.bind = 8083")
defaultListenerConfig += fmt.Sprintln("listeners.wss.default.bind = 8084")

hoconConfig, _ := hocon.ParseString(defaultListenerConfig + config)
return hoconConfig
}

func generateConfigMap(instance *appsv2beta1.EMQX, data string) *corev1.ConfigMap {
return &corev1.ConfigMap{
TypeMeta: metav1.TypeMeta{
Expand Down