Skip to content

Commit e6c3ed5

Browse files
egeguneshors
andauthored
K8SPS-370: Unsafe improvements (#690)
* K8SPS-370: Unsafe improvements * fix tests * fix haproxy check in async * address review comments * fix tls-cert-manager --------- Co-authored-by: Viacheslav Sarzhan <[email protected]>
1 parent 59c524c commit e6c3ed5

File tree

7 files changed

+61
-49
lines changed

7 files changed

+61
-49
lines changed

api/v1alpha1/perconaservermysql_types.go

Lines changed: 46 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ import (
3333
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
3434
"k8s.io/apimachinery/pkg/util/intstr"
3535
"sigs.k8s.io/controller-runtime/pkg/client"
36-
logf "sigs.k8s.io/controller-runtime/pkg/log"
3736

3837
"github.com/percona/percona-server-mysql-operator/pkg/naming"
3938
"github.com/percona/percona-server-mysql-operator/pkg/platform"
@@ -93,6 +92,7 @@ const (
9392
MinSafeProxySize = 2
9493
MinSafeGRSize = 3
9594
MaxSafeGRSize = 9
95+
MinSafeAsyncSize = 2
9696
)
9797

9898
// Checks if the provided ClusterType is valid.
@@ -526,7 +526,6 @@ func (cr *PerconaServerMySQL) SetVersion() {
526526

527527
// CheckNSetDefaults validates and sets default values for the PerconaServerMySQL custom resource.
528528
func (cr *PerconaServerMySQL) CheckNSetDefaults(ctx context.Context, serverVersion *platform.ServerVersion) error {
529-
log := logf.FromContext(ctx).WithName("CheckNSetDefaults")
530529
if len(cr.Spec.MySQL.ClusterType) == 0 {
531530
cr.Spec.MySQL.ClusterType = ClusterTypeAsync
532531
}
@@ -691,38 +690,66 @@ func (cr *PerconaServerMySQL) CheckNSetDefaults(ctx context.Context, serverVersi
691690
cr.Spec.MySQL.reconcileAffinityOpts()
692691
cr.Spec.Orchestrator.reconcileAffinityOpts()
693692

694-
if oSize := int(cr.Spec.Orchestrator.Size); cr.OrchestratorEnabled() && (oSize < 3 || oSize%2 == 0) && oSize != 0 && !cr.Spec.Unsafe.OrchestratorSize {
695-
return errors.New("Orchestrator size must be 3 or greater and an odd number for raft setup. Enable spec.unsafeFlags.orchestratorSize to bypass this check")
693+
if cr.Spec.MySQL.Size == 1 {
694+
cr.Spec.UpdateStrategy = appsv1.RollingUpdateStatefulSetStrategyType
696695
}
697696

698-
if cr.Spec.MySQL.ClusterType == ClusterTypeGR && cr.Spec.Proxy.Router == nil {
699-
return errors.New("router section is needed for group replication")
697+
if cr.Spec.MySQL.ClusterType == ClusterTypeGR {
698+
if !cr.Spec.Proxy.Router.Enabled && !cr.Spec.Proxy.HAProxy.Enabled && !cr.Spec.Unsafe.Proxy {
699+
return errors.New("MySQL Router or HAProxy must be enabled for Group Replication. Enable spec.unsafeFlags.proxy to bypass this check")
700+
}
701+
702+
if cr.RouterEnabled() && !cr.Spec.Unsafe.ProxySize {
703+
if cr.Spec.Proxy.Router.Size < MinSafeProxySize {
704+
return errors.Errorf("Router size should be %d or greater. Enable spec.unsafeFlags.proxySize to set a lower size", MinSafeProxySize)
705+
}
706+
}
707+
708+
if !cr.Spec.Unsafe.MySQLSize {
709+
if cr.Spec.MySQL.Size < MinSafeGRSize {
710+
return errors.Errorf("MySQL size should be %d or greater for Group Replication. Enable spec.unsafeFlags.mysqlSize to set a lower size", MinSafeGRSize)
711+
}
712+
713+
if cr.Spec.MySQL.Size > MaxSafeGRSize {
714+
return errors.Errorf("MySQL size should be %d or lower for Group Replication. Enable spec.unsafeFlags.mysqlSize to set a higher size", MaxSafeGRSize)
715+
}
716+
717+
if cr.Spec.MySQL.Size%2 == 0 {
718+
return errors.New("MySQL size should be an odd number for Group Replication. Enable spec.unsafeFlags.mysqlSize to set an even number")
719+
}
720+
}
700721
}
701722

702-
if cr.Spec.MySQL.ClusterType == ClusterTypeGR && !cr.Spec.Unsafe.MySQLSize {
703-
if cr.Spec.MySQL.Size < MinSafeGRSize {
704-
return errors.Errorf("MySQL size should be %d or greater for Group Replication. Enable spec.unsafeFlags.mysqlSize to set a lower size", MinSafeGRSize)
723+
if cr.Spec.MySQL.ClusterType == ClusterTypeAsync {
724+
if !cr.Spec.Unsafe.MySQLSize && cr.Spec.MySQL.Size < MinSafeAsyncSize {
725+
return errors.Errorf("MySQL size should be %d or greater for asynchronous replication. Enable spec.unsafeFlags.mysqlSize to set a lower size", MinSafeAsyncSize)
726+
}
727+
728+
if !cr.Spec.Unsafe.Orchestrator && !cr.Spec.Orchestrator.Enabled {
729+
return errors.New("Orchestrator must be enabled for asynchronous replication. Enable spec.unsafeFlags.orchestrator to bypass this check")
730+
}
731+
732+
if oSize := int(cr.Spec.Orchestrator.Size); cr.OrchestratorEnabled() && (oSize < 3 || oSize%2 == 0) && oSize != 0 && !cr.Spec.Unsafe.OrchestratorSize {
733+
return errors.New("Orchestrator size must be 3 or greater and an odd number for raft setup. Enable spec.unsafeFlags.orchestratorSize to bypass this check")
734+
}
735+
736+
if !cr.Spec.Orchestrator.Enabled && cr.Spec.UpdateStrategy == SmartUpdateStatefulSetStrategyType {
737+
return errors.Errorf("Orchestrator must be enabled to use SmartUpdate. Either enable Orchestrator or set spec.updateStrategy to %s", appsv1.RollingUpdateStatefulSetStrategyType)
705738
}
706739

707-
if cr.Spec.MySQL.Size > MaxSafeGRSize {
708-
return errors.Errorf("MySQL size should be %d or lower for Group Replication. Enable spec.unsafeFlags.mysqlSize to set a higher size", MaxSafeGRSize)
740+
if !cr.Spec.Unsafe.Proxy && !cr.HAProxyEnabled() {
741+
return errors.New("HAProxy must be enabled for asynchronous replication. Enable spec.unsafeFlags.proxy to bypass this check")
709742
}
710743

711-
if cr.Spec.MySQL.Size%2 == 0 {
712-
return errors.New("MySQL size should be an odd number for Group Replication. Enable spec.unsafeFlags.mysqlSize to set an even number")
744+
if cr.RouterEnabled() {
745+
return errors.New("MySQL Router can't be enabled for asynchronous replication")
713746
}
714747
}
715748

716749
if cr.RouterEnabled() && cr.HAProxyEnabled() {
717750
return errors.New("MySQL Router and HAProxy can't be enabled at the same time")
718751
}
719752

720-
if cr.RouterEnabled() && !cr.Spec.Unsafe.ProxySize {
721-
if cr.Spec.Proxy.Router.Size < MinSafeProxySize {
722-
return errors.Errorf("Router size should be %d or greater. Enable spec.unsafeFlags.proxySize to set a lower size", MinSafeProxySize)
723-
}
724-
}
725-
726753
if cr.Spec.Proxy.HAProxy == nil {
727754
cr.Spec.Proxy.HAProxy = new(HAProxySpec)
728755
}
@@ -733,18 +760,6 @@ func (cr *PerconaServerMySQL) CheckNSetDefaults(ctx context.Context, serverVersi
733760
}
734761
}
735762

736-
if cr.Spec.MySQL.ClusterType != ClusterTypeGR && !cr.OrchestratorEnabled() {
737-
switch cr.Generation {
738-
case 1:
739-
if cr.Spec.MySQL.Size > 1 {
740-
return errors.New("mysql size should be 1 when orchestrator is disabled")
741-
}
742-
default:
743-
cr.Spec.Orchestrator.Enabled = true
744-
log.Info("orchestrator can't be disabled on an existing cluster")
745-
}
746-
}
747-
748763
if cr.Spec.PMM == nil {
749764
cr.Spec.PMM = new(PMMSpec)
750765
}
@@ -764,12 +779,6 @@ func (cr *PerconaServerMySQL) CheckNSetDefaults(ctx context.Context, serverVersi
764779
cr.Spec.SSLSecretName = cr.Name + "-ssl"
765780
}
766781

767-
if cr.Spec.UpdateStrategy == SmartUpdateStatefulSetStrategyType &&
768-
!cr.HAProxyEnabled() &&
769-
!cr.RouterEnabled() {
770-
return errors.Errorf("MySQL Router or HAProxy should be enabled if SmartUpdate set")
771-
}
772-
773782
return nil
774783
}
775784

@@ -960,19 +969,11 @@ func (pmm *PMMSpec) HasSecret(secret *corev1.Secret) bool {
960969

961970
// RouterEnabled checks if the router is enabled, considering the MySQL configuration.
962971
func (cr *PerconaServerMySQL) RouterEnabled() bool {
963-
if cr.MySQLSpec().IsAsync() {
964-
return false
965-
}
966-
967972
return cr.Spec.Proxy.Router != nil && cr.Spec.Proxy.Router.Enabled
968973
}
969974

970975
// HAProxyEnabled verifies if HAProxy is enabled based on MySQL configuration and safety settings.
971976
func (cr *PerconaServerMySQL) HAProxyEnabled() bool {
972-
if cr.MySQLSpec().IsAsync() && !cr.Spec.Unsafe.Proxy {
973-
return true
974-
}
975-
976977
return cr.Spec.Proxy.HAProxy != nil && cr.Spec.Proxy.HAProxy.Enabled
977978
}
978979

deploy/cr.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,7 @@ spec:
305305
# - 10.0.0.0/8
306306

307307
orchestrator:
308-
enabled: false
308+
enabled: true
309309

310310
image: perconalab/percona-server-mysql-operator:main-orchestrator
311311
imagePullPolicy: Always

e2e-tests/tests/tls-cert-manager/02-assert.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ spec:
7676
haproxy:
7777
enabled: true
7878
orchestrator:
79-
enabled: false
79+
enabled: true
8080
status:
8181
haproxy:
8282
ready: 3

e2e-tests/tests/tls-cert-manager/04-assert.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ spec:
7676
haproxy:
7777
enabled: true
7878
orchestrator:
79-
enabled: false
79+
enabled: true
8080
status:
8181
haproxy:
8282
ready: 3

e2e-tests/tests/tls-cert-manager/06-assert.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ spec:
7676
haproxy:
7777
enabled: true
7878
orchestrator:
79-
enabled: false
79+
enabled: true
8080
status:
8181
haproxy:
8282
ready: 3

pkg/controller/ps/controller_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -378,6 +378,7 @@ var _ = Describe("Reconcile HAProxy", Ordered, func() {
378378
cr, err := readDefaultCR(crName, ns)
379379
cr.Spec.MySQL.ClusterType = psv1alpha1.ClusterTypeAsync
380380
cr.Spec.Proxy.HAProxy.Enabled = true
381+
cr.Spec.Orchestrator.Enabled = true
381382
cr.Spec.UpdateStrategy = appsv1.RollingUpdateStatefulSetStrategyType
382383
It("should read and create defautl cr.yaml", func() {
383384
Expect(err).NotTo(HaveOccurred())

pkg/controller/ps/version_test.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ func TestReconcileVersions(t *testing.T) {
7373
Image: "some-image",
7474
},
7575
MySQL: apiv1alpha1.MySQLSpec{
76+
ClusterType: apiv1alpha1.ClusterTypeGR,
7677
PodSpec: apiv1alpha1.PodSpec{
7778
Size: 3,
7879
VolumeSpec: &apiv1alpha1.VolumeSpec{
@@ -88,6 +89,7 @@ func TestReconcileVersions(t *testing.T) {
8889
},
8990
Proxy: apiv1alpha1.ProxySpec{
9091
HAProxy: &apiv1alpha1.HAProxySpec{
92+
Enabled: true,
9193
PodSpec: apiv1alpha1.PodSpec{
9294
Size: 2,
9395
},
@@ -115,6 +117,7 @@ func TestReconcileVersions(t *testing.T) {
115117
Image: "some-image",
116118
},
117119
MySQL: apiv1alpha1.MySQLSpec{
120+
ClusterType: apiv1alpha1.ClusterTypeGR,
118121
PodSpec: apiv1alpha1.PodSpec{
119122
Size: 3,
120123
VolumeSpec: &apiv1alpha1.VolumeSpec{
@@ -130,6 +133,7 @@ func TestReconcileVersions(t *testing.T) {
130133
},
131134
Proxy: apiv1alpha1.ProxySpec{
132135
HAProxy: &apiv1alpha1.HAProxySpec{
136+
Enabled: true,
133137
PodSpec: apiv1alpha1.PodSpec{
134138
Size: 2,
135139
},
@@ -157,6 +161,7 @@ func TestReconcileVersions(t *testing.T) {
157161
Image: "some-image",
158162
},
159163
MySQL: apiv1alpha1.MySQLSpec{
164+
ClusterType: apiv1alpha1.ClusterTypeGR,
160165
PodSpec: apiv1alpha1.PodSpec{
161166
Size: 3,
162167
VolumeSpec: &apiv1alpha1.VolumeSpec{
@@ -172,6 +177,7 @@ func TestReconcileVersions(t *testing.T) {
172177
},
173178
Proxy: apiv1alpha1.ProxySpec{
174179
HAProxy: &apiv1alpha1.HAProxySpec{
180+
Enabled: true,
175181
PodSpec: apiv1alpha1.PodSpec{
176182
Size: 2,
177183
},
@@ -200,6 +206,7 @@ func TestReconcileVersions(t *testing.T) {
200206
Image: "some-image",
201207
},
202208
MySQL: apiv1alpha1.MySQLSpec{
209+
ClusterType: apiv1alpha1.ClusterTypeGR,
203210
PodSpec: apiv1alpha1.PodSpec{
204211
Size: 3,
205212
VolumeSpec: &apiv1alpha1.VolumeSpec{
@@ -215,6 +222,7 @@ func TestReconcileVersions(t *testing.T) {
215222
},
216223
Proxy: apiv1alpha1.ProxySpec{
217224
HAProxy: &apiv1alpha1.HAProxySpec{
225+
Enabled: true,
218226
PodSpec: apiv1alpha1.PodSpec{
219227
Size: 2,
220228
},
@@ -340,6 +348,7 @@ func TestGetVersion(t *testing.T) {
340348
Image: "some-image",
341349
},
342350
MySQL: apiv1alpha1.MySQLSpec{
351+
ClusterType: apiv1alpha1.ClusterTypeGR,
343352
PodSpec: apiv1alpha1.PodSpec{
344353
Size: 3,
345354
VolumeSpec: &apiv1alpha1.VolumeSpec{
@@ -355,6 +364,7 @@ func TestGetVersion(t *testing.T) {
355364
},
356365
Proxy: apiv1alpha1.ProxySpec{
357366
HAProxy: &apiv1alpha1.HAProxySpec{
367+
Enabled: true,
358368
PodSpec: apiv1alpha1.PodSpec{
359369
Size: 2,
360370
},

0 commit comments

Comments
 (0)