Skip to content

Commit

Permalink
fix: fix always update configmap
Browse files Browse the repository at this point in the history
Signed-off-by: Rory Z <[email protected]>
  • Loading branch information
Rory-Z committed Feb 21, 2024
1 parent a3ec997 commit f840106
Showing 1 changed file with 20 additions and 23 deletions.
43 changes: 20 additions & 23 deletions controllers/apps/v2beta1/sync_emqx_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
k8sErrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
ctrl "sigs.k8s.io/controller-runtime"
)

type syncConfig struct {
Expand All @@ -32,8 +33,12 @@ func (s *syncConfig) reconcile(ctx context.Context, logger logr.Logger, instance
Namespace: instance.Namespace,
}, configMap); err != nil {
if k8sErrors.IsNotFound(err) {
if err := s.update(ctx, logger, instance, confStr); err != nil {
return subResult{err: emperror.Wrap(err, "failed to update emqx config")}
configMap = generateConfigMap(instance, confStr)
if err := ctrl.SetControllerReference(instance, configMap, s.Scheme); err != nil {
return subResult{err: emperror.Wrap(err, "failed to set controller reference for configMap")}
}

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

View check run for this annotation

Codecov / codecov/patch

controllers/apps/v2beta1/sync_emqx_config.go#L38-L39

Added lines #L38 - L39 were not covered by tests
if err := s.Client.Create(ctx, configMap); err != nil {
return subResult{err: emperror.Wrap(err, "failed to create configMap")}

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

View check run for this annotation

Codecov / codecov/patch

controllers/apps/v2beta1/sync_emqx_config.go#L41

Added line #L41 was not covered by tests
}
return subResult{}
}
Expand All @@ -42,8 +47,12 @@ func (s *syncConfig) reconcile(ctx context.Context, logger logr.Logger, instance

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

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

View check run for this annotation

Codecov / codecov/patch

controllers/apps/v2beta1/sync_emqx_config.go#L55

Added line #L55 was not covered by tests
}
return subResult{}
}
Expand Down Expand Up @@ -78,8 +87,13 @@ func (s *syncConfig) reconcile(ctx context.Context, logger logr.Logger, instance
return subResult{err: emperror.Wrap(err, "failed to put emqx config")}
}

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

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

View check run for this annotation

Codecov / codecov/patch

controllers/apps/v2beta1/sync_emqx_config.go#L91-L92

Added lines #L91 - L92 were not covered by tests

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

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

View check run for this annotation

Codecov / codecov/patch

controllers/apps/v2beta1/sync_emqx_config.go#L96

Added line #L96 was not covered by tests
}

return subResult{}
Expand All @@ -88,23 +102,6 @@ func (s *syncConfig) reconcile(ctx context.Context, logger logr.Logger, instance
return subResult{}
}

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

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")
}

return nil
}

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

0 comments on commit f840106

Please sign in to comment.