Skip to content

Commit

Permalink
feat(singleNode): Support running single node. radondb#361
Browse files Browse the repository at this point in the history
  • Loading branch information
acekingke committed Aug 25, 2022
1 parent 5369cbc commit e10560f
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 8 deletions.
2 changes: 1 addition & 1 deletion api/v1alpha1/mysqlcluster_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ type MysqlClusterSpec struct {

// Replicas is the number of pods.
// +optional
// +kubebuilder:validation:Enum=0;2;3;5
// +kubebuilder:validation:Enum=0;1;2;3;5
// +kubebuilder:default:=3
Replicas *int32 `json:"replicas,omitempty"`

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1259,6 +1259,7 @@ spec:
description: Replicas is the number of pods.
enum:
- 0
- 1
- 2
- 3
- 5
Expand Down
1 change: 1 addition & 0 deletions config/crd/bases/mysql.radondb.com_mysqlclusters.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1259,6 +1259,7 @@ spec:
description: Replicas is the number of pods.
enum:
- 0
- 1
- 2
- 3
- 5
Expand Down
44 changes: 39 additions & 5 deletions controllers/mysqlcluster_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import (
"github.com/radondb/radondb-mysql-kubernetes/internal"
"github.com/radondb/radondb-mysql-kubernetes/mysqlcluster"
clustersyncer "github.com/radondb/radondb-mysql-kubernetes/mysqlcluster/syncer"
"github.com/radondb/radondb-mysql-kubernetes/utils"
)

var clusterFinalizer string = "mysqlcluster-finalizer"
Expand Down Expand Up @@ -137,12 +138,23 @@ func (r *MysqlClusterReconciler) Reconcile(ctx context.Context, req ctrl.Request
clustersyncer.NewServiceAccountSyncer(r.Client, instance),
clustersyncer.NewHeadlessSVCSyncer(r.Client, instance),
clustersyncer.NewLeaderSVCSyncer(r.Client, instance),
clustersyncer.NewFollowerSVCSyncer(r.Client, instance),
clustersyncer.NewStatefulSetSyncer(r.Client, instance, cmRev, sctRev, r.SQLRunnerFactory, r.XenonExecutor),
clustersyncer.NewPDBSyncer(r.Client, instance),
clustersyncer.NewXenonCMSyncer(r.Client, instance),
}

if *instance.Unwrap().Spec.Replicas == 1 {
// Delete follower service
r.deleteFollowerService(ctx, req, instance.Unwrap())
syncers = append(syncers,
clustersyncer.NewStatefulSetSyncer(r.Client, instance, cmRev, sctRev, r.SQLRunnerFactory, r.XenonExecutor),
clustersyncer.NewPDBSyncer(r.Client, instance),
clustersyncer.NewXenonCMSyncer(r.Client, instance),
)
} else {
syncers = append(syncers,
clustersyncer.NewFollowerSVCSyncer(r.Client, instance),
clustersyncer.NewStatefulSetSyncer(r.Client, instance, cmRev, sctRev, r.SQLRunnerFactory, r.XenonExecutor),
clustersyncer.NewPDBSyncer(r.Client, instance),
clustersyncer.NewXenonCMSyncer(r.Client, instance),
)
}
if instance.Spec.MetricsOpts.Enabled {
syncers = append(syncers, clustersyncer.NewMetricsSVCSyncer(r.Client, instance))
}
Expand Down Expand Up @@ -204,3 +216,25 @@ func (r *MysqlClusterReconciler) deleteAllBackup(ctx context.Context, req ctrl.R

return nil
}

// For SingleNode, follower service do not need.
func (r *MysqlClusterReconciler) deleteFollowerService(ctx context.Context, req ctrl.Request, instance *apiv1alpha1.MysqlCluster) error {
log := log.FromContext(ctx).WithName("controllers").WithName("MysqlCluster")
labelSet := labels.Set{"mysql.radondb.com/service-type": string(utils.FollowerService)}
serviceList := corev1.ServiceList{}
if err := r.List(ctx,
&serviceList,
&client.ListOptions{
Namespace: instance.Namespace,
LabelSelector: labelSet.AsSelector(),
},
); err != nil {
return err
}
for _, svc := range serviceList.Items {
if err := r.Delete(context.TODO(), &svc); err != nil {
log.Error(err, "failed to delete a backup", "backup", svc)
}
}
return nil
}
6 changes: 5 additions & 1 deletion mysqlcluster/container/xenon.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,11 @@ func (c *xenon) getImage() string {

// getCommand get the container command.
func (c *xenon) getCommand() []string {
return nil
if *c.Spec.Replicas == 1 {
return []string{"xenon", "-c", "/etc/xenon/xenon.json", "-r", "LEADER"}
}
// If return nil, statefulset never update command , And I don't know why.
return []string{"xenon", "-c", "/etc/xenon/xenon.json"}
}

// getEnvVars get the container env.
Expand Down
2 changes: 1 addition & 1 deletion mysqlcluster/container/xenon_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func TestGetXenonImage(t *testing.T) {
}

func TestGetXenonCommand(t *testing.T) {
assert.Nil(t, xenonCase.Command)
assert.Equal(t, []string{"xenon", "-c", "/etc/xenon/xenon.json"}, xenonCase.Command)
}

func TestGetXenonEnvVar(t *testing.T) {
Expand Down

0 comments on commit e10560f

Please sign in to comment.