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

chores: optimizing the error returns in the seataserver controller #23

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
63 changes: 23 additions & 40 deletions controllers/seataserver_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ package controllers
import (
"context"
"fmt"
"time"

"github.com/apache/seata-k8s/pkg/seata"
appsv1 "k8s.io/api/apps/v1"
apiv1 "k8s.io/api/core/v1"
Expand All @@ -32,7 +34,6 @@ import (
"sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/predicate"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
"time"

seatav1alpha1 "github.com/apache/seata-k8s/api/v1alpha1"
)
Expand Down Expand Up @@ -98,71 +99,54 @@ func (r *SeataServerReconciler) Reconcile(ctx context.Context, req ctrl.Request)
return ctrl.Result{}, nil
}

func (r *SeataServerReconciler) reconcileHeadlessService(ctx context.Context, s *seatav1alpha1.SeataServer) (err error) {
func (r *SeataServerReconciler) reconcileHeadlessService(ctx context.Context, s *seatav1alpha1.SeataServer) error {
logger := log.FromContext(ctx)

svc := seata.MakeHeadlessService(s)
if err := controllerutil.SetControllerReference(s, svc, r.Scheme); err != nil {
return err
}
foundSvc := &apiv1.Service{}
err = r.Client.Get(ctx, types.NamespacedName{
err := r.Client.Get(ctx, types.NamespacedName{
Name: svc.Name,
Namespace: svc.Namespace,
}, foundSvc)
if err != nil && errors.IsNotFound(err) {
logger.Info(fmt.Sprintf("Creating a new SeataServer Service {%s:%s}",
svc.Namespace, svc.Name))
err = r.Client.Create(ctx, svc)
if err != nil {
return err
if err != nil {
if errors.IsNotFound(err) {
logger.Info(fmt.Sprintf("Creating a new SeataServer Service {%s:%s}",
svc.Namespace, svc.Name))
return r.Client.Create(ctx, svc)
}
return nil
} else if err != nil {
return err
} else {
logger.Info(fmt.Sprintf("Updating existing SeataServer StatefulSet {%s:%s}",
foundSvc.Namespace, foundSvc.Name))

seata.SyncService(foundSvc, svc)
err = r.Client.Update(ctx, foundSvc)
if err != nil {
return err
}
}
return nil

logger.Info(fmt.Sprintf("Updating existing SeataServer StatefulSet {%s:%s}",
foundSvc.Namespace, foundSvc.Name))
seata.SyncService(foundSvc, svc)
return r.Client.Update(ctx, foundSvc)
}

func (r *SeataServerReconciler) reconcileStatefulSet(ctx context.Context, s *seatav1alpha1.SeataServer) (err error) {
func (r *SeataServerReconciler) reconcileStatefulSet(ctx context.Context, s *seatav1alpha1.SeataServer) error {
logger := log.FromContext(ctx)

sts := seata.MakeStatefulSet(s)
if err := controllerutil.SetControllerReference(s, sts, r.Scheme); err != nil {
return err
}
foundSts := &appsv1.StatefulSet{}
err = r.Client.Get(ctx, types.NamespacedName{
err := r.Client.Get(ctx, types.NamespacedName{
Name: sts.Name,
Namespace: sts.Namespace,
}, foundSts)
if err != nil && errors.IsNotFound(err) {
logger.Info(fmt.Sprintf("Creating a new SeataServer StatefulSet {%s:%s}",
sts.Namespace, sts.Name))
err = r.Client.Create(ctx, sts)
if err != nil {
return err
if err != nil {
if errors.IsNotFound(err) {
logger.Info(fmt.Sprintf("Creating a new SeataServer StatefulSet {%s:%s}",
sts.Namespace, sts.Name))
return r.Client.Create(ctx, sts)
}
return nil
} else if err != nil {
return err
} else {
err = r.updateStatefulSet(ctx, s, foundSts, sts)
if err != nil {
return err
}
}

return nil
return r.updateStatefulSet(ctx, s, foundSts, sts)
}

func (r *SeataServerReconciler) updateStatefulSet(ctx context.Context, s *seatav1alpha1.SeataServer,
Expand All @@ -183,8 +167,7 @@ func (r *SeataServerReconciler) updateStatefulSet(ctx context.Context, s *seatav
newSize := *sts.Spec.Replicas
if readySize != newSize {
s.Status.Synchronized = false
}
if readySize == newSize && !s.Status.Synchronized {
} else if !s.Status.Synchronized {
username, password := "seata", "seata"
for _, env := range s.Spec.Env {
if env.Name == "console.user.username" {
Expand Down
Loading