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 MySQL PXC and HAProxy configs #309

Merged
merged 22 commits into from
Mar 15, 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
78 changes: 72 additions & 6 deletions controllers/databasecluster_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,10 @@ const (
// PerconaPGClusterKind represents postgresql kind.
PerconaPGClusterKind = "PerconaPGCluster"

pxcDeploymentName = "percona-xtradb-cluster-operator"
psmdbDeploymentName = "percona-server-mongodb-operator"
pgDeploymentName = "percona-postgresql-operator"
pxcDeploymentName = "percona-xtradb-cluster-operator"
pxcHAProxyEnvSecretName = "haproxy-env-secret" //nolint:gosec // This is not a credential, only a secret name.
psmdbDeploymentName = "percona-server-mongodb-operator"
pgDeploymentName = "percona-postgresql-operator"

defaultPMMClientImage = "percona/pmm-client:2"

Expand Down Expand Up @@ -936,12 +937,24 @@ func (r *DatabaseClusterReconciler) createOrUpdateSecretData(
}

func (r *DatabaseClusterReconciler) genPXCHAProxySpec(
ctx context.Context,
database *everestv1alpha1.DatabaseCluster,
engine *everestv1alpha1.DatabaseEngine,
clusterType ClusterType,
) (*pxcv1.HAProxySpec, error) {
haProxy := r.defaultPXCSpec().HAProxy

// Set configuration based on database size.
if database.Spec.Engine.Resources.Memory.Cmp(memoryLargeSize) == 0 {
haProxy.PodSpec.Resources = haProxyResourceRequirementsLarge
haProxy.PodSpec.LivenessProbes.TimeoutSeconds = 30
haProxy.PodSpec.ReadinessProbes.TimeoutSeconds = 30
}
if database.Spec.Engine.Resources.Memory.Cmp(memoryMediumSize) == 0 {
haProxy.PodSpec.Resources = haProxyResourceRequirementsMedium
}
if database.Spec.Engine.Resources.Memory.Cmp(memorySmallSize) == 0 {
haProxy.PodSpec.Resources = haProxyResourceRequirementsSmall
}
haProxy.PodSpec.Enabled = true

if database.Spec.Proxy.Replicas == nil {
Expand All @@ -968,6 +981,27 @@ func (r *DatabaseClusterReconciler) genPXCHAProxySpec(
}

haProxy.PodSpec.Configuration = database.Spec.Proxy.Config
if haProxy.PodSpec.Configuration == "" {
haProxy.PodSpec.Configuration = haProxyConfigDefault
}

// Ensure there is a env vars secret for HAProxy
secret := &corev1.Secret{
ObjectMeta: metav1.ObjectMeta{
Name: pxcHAProxyEnvSecretName,
Namespace: database.GetNamespace(),
},
}
if err := controllerutil.SetControllerReference(database, secret, r.Client.Scheme()); err != nil {
return nil, fmt.Errorf("failed to set controller reference for secret %s", pxcHAProxyEnvSecretName)
}
if _, err := controllerutil.CreateOrUpdate(ctx, r.Client, secret, func() error {
secret.Data = haProxyEnvVars
return nil
}); err != nil {
return nil, fmt.Errorf("failed to create or update secret %w", err)
}
haProxy.PodSpec.EnvVarsSecretName = pxcHAProxyEnvSecretName

haProxyAvailVersions, ok := engine.Status.AvailableVersions.Proxy[everestv1alpha1.ProxyTypeHAProxy]
if !ok {
Expand Down Expand Up @@ -1413,10 +1447,27 @@ func (r *DatabaseClusterReconciler) reconcilePXC(ctx context.Context, req ctrl.R
pxc.Spec.PXC.PodSpec.Resources.Limits[corev1.ResourceMemory] = database.Spec.Engine.Resources.Memory
}

// Set timeouts and resources based on the database size.
if database.Spec.Engine.Resources.Memory.Cmp(memorySmallSize) == 0 {
pxc.Spec.PXC.PodSpec.LivenessProbes.TimeoutSeconds = 450
pxc.Spec.PXC.PodSpec.ReadinessProbes.TimeoutSeconds = 450
pxc.Spec.PXC.PodSpec.Resources = pxcResourceRequirementsSmall
}
if database.Spec.Engine.Resources.Memory.Cmp(memoryMediumSize) == 0 {
pxc.Spec.PXC.PodSpec.LivenessProbes.TimeoutSeconds = 451
pxc.Spec.PXC.PodSpec.ReadinessProbes.TimeoutSeconds = 451
pxc.Spec.PXC.PodSpec.Resources = pxcResourceRequirementsMedium
}
if database.Spec.Engine.Resources.Memory.Cmp(memoryLargeSize) == 0 {
pxc.Spec.PXC.PodSpec.LivenessProbes.TimeoutSeconds = 600
pxc.Spec.PXC.PodSpec.ReadinessProbes.TimeoutSeconds = 600
pxc.Spec.PXC.PodSpec.Resources = pxcResourceRequirementsLarge
}

switch database.Spec.Proxy.Type {
case everestv1alpha1.ProxyTypeHAProxy:
pxc.Spec.ProxySQL.Enabled = false
pxc.Spec.HAProxy, err = r.genPXCHAProxySpec(database, engine, clusterType)
pxc.Spec.HAProxy, err = r.genPXCHAProxySpec(ctx, database, engine, clusterType)
if err != nil {
return err
}
Expand Down Expand Up @@ -1444,12 +1495,15 @@ func (r *DatabaseClusterReconciler) reconcilePXC(ctx context.Context, req ctrl.R
}
}

if monitoring.Spec.Type == everestv1alpha1.PMMMonitoringType {
if monitoring.Spec.Type == everestv1alpha1.PMMMonitoringType { //nolint:nestif
image := defaultPMMClientImage
if monitoring.Spec.PMM.Image != "" {
image = monitoring.Spec.PMM.Image
}

//nolint:godox
// TODO (K8SPXC-1367): Set PMM container LivenessProbes timeouts once possible.

pxc.Spec.PMM.Enabled = true
pmmURL, err := url.Parse(monitoring.Spec.PMM.URL)
if err != nil {
Expand All @@ -1458,6 +1512,16 @@ func (r *DatabaseClusterReconciler) reconcilePXC(ctx context.Context, req ctrl.R
pxc.Spec.PMM.ServerHost = pmmURL.Hostname()
pxc.Spec.PMM.Image = image
pxc.Spec.PMM.Resources = database.Spec.Monitoring.Resources
// Set resources based on cluster size.
if database.Spec.Engine.Resources.Memory.Cmp(memorySmallSize) == 0 {
pxc.Spec.PMM.Resources = pmmResourceRequirementsSmall
}
if database.Spec.Engine.Resources.Memory.Cmp(memoryMediumSize) == 0 {
pxc.Spec.PMM.Resources = pmmResourceRequirementsMedium
}
if database.Spec.Engine.Resources.Memory.Cmp(memoryLargeSize) == 0 {
pxc.Spec.PMM.Resources = pmmResourceRequirementsLarge
}

apiKey, err := r.getSecretFromMonitoringConfig(ctx, monitoring)
if err != nil {
Expand Down Expand Up @@ -3441,6 +3505,8 @@ func (r *DatabaseClusterReconciler) defaultPXCSpec() *pxcv1.PerconaXtraDBCluster
corev1.ResourceCPU: resource.MustParse("600m"),
},
},
ReadinessProbes: corev1.Probe{TimeoutSeconds: 23},
LivenessProbes: corev1.Probe{TimeoutSeconds: 23},
},
},
ProxySQL: &pxcv1.PodSpec{
Expand Down
78 changes: 78 additions & 0 deletions controllers/haproxy_config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
// everest-operator
// Copyright (C) 2022 Percona LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package controllers

import (
"strconv"

corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
)

const (
// A haproxyConfigDefault is the default HAProxy configuration.
haProxyConfigDefault = `
global
maxconn 4048
defaults
timeout connect 100500ms
timeout client 28800ms
timeout server 28800ms
`
)

// A haProxyEnvVars contains the environment variables to be set in the HAProxy container.
var haProxyEnvVars = map[string][]byte{
"HA_CONNECTION_TIMEOUT": []byte(strconv.Itoa(5000)),
}

var ( //nolint:dupl
// A haProxyResourceRequirementsSmall is the resource requirements for HAProxy for small clusters.
haProxyResourceRequirementsSmall = corev1.ResourceRequirements{
Requests: corev1.ResourceList{
corev1.ResourceMemory: resource.MustParse("195Mi"),
corev1.ResourceCPU: resource.MustParse("95m"),
},
Limits: corev1.ResourceList{
corev1.ResourceMemory: resource.MustParse("204Mi"),
corev1.ResourceCPU: resource.MustParse("100m"),
},
}

// A haProxyResourceRequirementsMedium is the resource requirements for HAProxy for medium clusters.
haProxyResourceRequirementsMedium = corev1.ResourceRequirements{
Requests: corev1.ResourceList{
corev1.ResourceCPU: resource.MustParse("778Mi"),
corev1.ResourceMemory: resource.MustParse("228m"),
},
Limits: corev1.ResourceList{
corev1.ResourceCPU: resource.MustParse("819Mi"),
corev1.ResourceMemory: resource.MustParse("240m"),
},
}

// A haProxyResourceRequirementsLarge is the resource requirements for HAProxy for large clusters.
haProxyResourceRequirementsLarge = corev1.ResourceRequirements{
Requests: corev1.ResourceList{
corev1.ResourceMemory: resource.MustParse("3.19Gi"),
corev1.ResourceCPU: resource.MustParse("228m"),
},
Limits: corev1.ResourceList{
corev1.ResourceMemory: resource.MustParse("3.04Gi"),
corev1.ResourceCPU: resource.MustParse("240m"),
},
}
)
59 changes: 59 additions & 0 deletions controllers/pmm_config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// everest-operator
// Copyright (C) 2022 Percona LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package controllers

import (
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
)

var ( //nolint:dupl
// A pmmResourceRequirementsSmall is the resource requirements for PMM for small clusters.
pmmResourceRequirementsSmall = corev1.ResourceRequirements{
Requests: corev1.ResourceList{
corev1.ResourceMemory: resource.MustParse("97.27Mi"),
corev1.ResourceCPU: resource.MustParse("95m"),
},
Limits: corev1.ResourceList{
corev1.ResourceMemory: resource.MustParse("102.4Mi"),
corev1.ResourceCPU: resource.MustParse("100m"),
},
}

// A pmmResourceRequirementsMedium is the resource requirements for PMM for medium clusters.
pmmResourceRequirementsMedium = corev1.ResourceRequirements{
Requests: corev1.ResourceList{
corev1.ResourceMemory: resource.MustParse("194.5Mi"),
corev1.ResourceCPU: resource.MustParse("228m"),
},
Limits: corev1.ResourceList{
corev1.ResourceMemory: resource.MustParse("204.7Mi"),
corev1.ResourceCPU: resource.MustParse("240m"),
},
}

// A pmmResourceRequirementsLarge is the resource requirements for PMM for large clusters.
pmmResourceRequirementsLarge = corev1.ResourceRequirements{
Requests: corev1.ResourceList{
corev1.ResourceMemory: resource.MustParse("778.23Mi"),
corev1.ResourceCPU: resource.MustParse("228m"),
},
Limits: corev1.ResourceList{
corev1.ResourceMemory: resource.MustParse("819.19Mi"),
corev1.ResourceCPU: resource.MustParse("240m"),
},
}
)
Loading
Loading