Skip to content

Commit 54c584e

Browse files
authored
Merge pull request #495 from vshn/fix/proxysql-version
Make proxysql report the actual mariadb server version
2 parents 129170f + 2322f61 commit 54c584e

File tree

8 files changed

+118
-15
lines changed

8 files changed

+118
-15
lines changed

apis/vshn/v1/dbaas_vshn_mariadb.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,10 @@ type VSHNMariaDBStatus struct {
145145
// CurrentInstances tracks the current amount of instances.
146146
// Mainly used to detect if there was a change in instances
147147
CurrentInstances int `json:"currentInstances,omitempty"`
148+
// MariaDBVersion contains the current MariaDB server version
149+
MariaDBVersion string `json:"mariadbVersion,omitempty"`
150+
// InitialMaintenanceRan tracks if the initial maintenance job has been triggered
151+
InitialMaintenanceRan bool `json:"initialMaintenanceRan,omitempty"`
148152
// ResourceStatus represents the observed state of a managed resource.
149153
xpv1.ResourceStatus `json:",inline"`
150154
}

crds/vshn.appcat.vshn.io_vshnmariadbs.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4895,6 +4895,9 @@ spec:
48954895
CurrentInstances tracks the current amount of instances.
48964896
Mainly used to detect if there was a change in instances
48974897
type: integer
4898+
initialMaintenanceRan:
4899+
description: InitialMaintenanceRan tracks if the initial maintenance job has been triggered
4900+
type: boolean
48984901
instanceNamespace:
48994902
description: InstanceNamespace contains the name of the namespace where the instance resides
49004903
type: string
@@ -4935,6 +4938,9 @@ spec:
49354938
type: string
49364939
type: object
49374940
type: array
4941+
mariadbVersion:
4942+
description: MariaDBVersion contains the current MariaDB server version
4943+
type: string
49384944
namespaceConditions:
49394945
items:
49404946
properties:

crds/vshn.appcat.vshn.io_xvshnmariadbs.yaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5804,6 +5804,10 @@ spec:
58045804
CurrentInstances tracks the current amount of instances.
58055805
Mainly used to detect if there was a change in instances
58065806
type: integer
5807+
initialMaintenanceRan:
5808+
description: InitialMaintenanceRan tracks if the initial maintenance
5809+
job has been triggered
5810+
type: boolean
58075811
instanceNamespace:
58085812
description: InstanceNamespace contains the name of the namespace
58095813
where the instance resides
@@ -5848,6 +5852,9 @@ spec:
58485852
type: string
58495853
type: object
58505854
type: array
5855+
mariadbVersion:
5856+
description: MariaDBVersion contains the current MariaDB server version
5857+
type: string
58515858
namespaceConditions:
58525859
items:
58535860
properties:

pkg/comp-functions/functions/vshnmariadb/files/proxysql.conf.tmpl

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,12 @@ admin_variables=
99
cluster_password="{{ .RootPassword }}"
1010
}
1111

12-
{{ if eq .TLS 1 }}
1312
mysql_variables = {
13+
{{ if eq .TLS 1 }}
1414
ssl_p2s_ca = "/var/lib/proxysql/proxysql-ca.pem"
15+
{{ end }}
16+
server_version = "{{ .MariaDBVersion }}"
1517
}
16-
{{ end }}
1718

1819
mysql_servers =
1920
(

pkg/comp-functions/functions/vshnmariadb/maintenance.go

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ import (
99
"github.com/vshn/appcat/v4/pkg/comp-functions/functions/common"
1010
"github.com/vshn/appcat/v4/pkg/comp-functions/functions/common/maintenance"
1111
"github.com/vshn/appcat/v4/pkg/comp-functions/runtime"
12+
batchv1 "k8s.io/api/batch/v1"
13+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
14+
"k8s.io/utils/ptr"
1215
)
1316

1417
// AddMaintenanceJob will add a job to do the maintenance for the instance
@@ -27,7 +30,58 @@ func AddMaintenanceJob(ctx context.Context, comp *vshnv1.VSHNMariaDB, svc *runti
2730
instanceNamespace := comp.GetInstanceNamespace()
2831
schedule := comp.GetFullMaintenanceSchedule()
2932

30-
return maintenance.New(comp, svc, schedule, instanceNamespace, comp.GetServiceName()).
33+
result := maintenance.New(comp, svc, schedule, instanceNamespace, comp.GetServiceName()).
3134
WithHelmBasedService().
3235
Run(ctx)
36+
37+
if result != nil {
38+
return result
39+
}
40+
41+
return AddInitialMaintenanceJob(ctx, comp, svc)
42+
}
43+
44+
// AddInitialMaintenanceJob creates a one-time job from the CronJob template to run maintenance immediately after provisioning
45+
func AddInitialMaintenanceJob(ctx context.Context, comp *vshnv1.VSHNMariaDB, svc *runtime.ServiceRuntime) *xfnproto.Result {
46+
// Check if initial maintenance has already been triggered
47+
if comp.Status.InitialMaintenanceRan {
48+
return nil
49+
}
50+
51+
cronJobName := comp.GetName() + "-maintenancejob"
52+
initialJobName := comp.GetName() + "-initial-maintenance"
53+
54+
observedCronJob := &batchv1.CronJob{}
55+
err := svc.GetObservedKubeObject(observedCronJob, cronJobName)
56+
if err != nil {
57+
return nil
58+
}
59+
60+
svc.Log.Info("Creating initial maintenance job from CronJob template")
61+
62+
job := &batchv1.Job{
63+
ObjectMeta: metav1.ObjectMeta{
64+
Name: initialJobName,
65+
Namespace: observedCronJob.Namespace,
66+
Labels: observedCronJob.Labels,
67+
},
68+
Spec: batchv1.JobSpec{
69+
BackoffLimit: ptr.To(int32(2)),
70+
TTLSecondsAfterFinished: ptr.To(int32(3600)),
71+
Template: observedCronJob.Spec.JobTemplate.Spec.Template,
72+
},
73+
}
74+
75+
errSet := svc.SetDesiredKubeObject(job, initialJobName, runtime.KubeOptionAllowDeletion)
76+
if errSet != nil {
77+
return runtime.NewFatalResult(fmt.Errorf("failed to set desired kube object: %w", errSet))
78+
}
79+
80+
// Mark that initial maintenance has been triggered
81+
comp.Status.InitialMaintenanceRan = true
82+
if err := svc.SetDesiredCompositeStatus(comp); err != nil {
83+
return runtime.NewWarningResult(fmt.Sprintf("cannot update InitialMaintenanceRan status: %v", err))
84+
}
85+
86+
return nil
3387
}

pkg/comp-functions/functions/vshnmariadb/mariadb_deploy.go

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"errors"
1010
"fmt"
1111
"io"
12+
"regexp"
1213
"strconv"
1314

1415
xfnproto "github.com/crossplane/function-sdk-go/proto/v1"
@@ -101,6 +102,7 @@ func DeployMariadb(ctx context.Context, comp *vshnv1.VSHNMariaDB, svc *runtime.S
101102
return runtime.NewWarningResult(fmt.Errorf("cannot set custom MariaDB settings: %w", err).Error())
102103
}
103104
}
105+
104106
return nil
105107
}
106108

@@ -117,11 +119,16 @@ func createObjectHelmRelease(ctx context.Context, comp *vshnv1.VSHNMariaDB, svc
117119
return fmt.Errorf("cannot get observed release values: %w", err)
118120
}
119121

120-
_, err = maintenance.SetReleaseVersion(ctx, comp.Spec.Parameters.Service.Version, values, observedValues, []string{"image", "tag"})
122+
versionTag, err := maintenance.SetReleaseVersion(ctx, comp.Spec.Parameters.Service.Version, values, observedValues, []string{"image", "tag"})
121123
if err != nil {
122124
return fmt.Errorf("cannot set mariadb version for release: %w", err)
123125
}
124126

127+
// Extract and update the MariaDB version in status
128+
if err := updateMariaDBVersionFromTag(comp, svc, versionTag); err != nil {
129+
svc.Log.Error(err, "cannot update MariaDB version in status")
130+
}
131+
125132
r, err := newRelease(ctx, svc, values, comp)
126133
if err != nil {
127134
return err
@@ -528,3 +535,25 @@ func getMariaDBRootPassword(secretName string, svc *runtime.ServiceRuntime) ([]b
528535
}
529536
return secret.Data["mariadb-root-password"], nil
530537
}
538+
539+
// updateMariaDBVersionFromTag extracts the semantic version from a tag and appends MariaDB-log suffix
540+
func updateMariaDBVersionFromTag(comp *vshnv1.VSHNMariaDB, svc *runtime.ServiceRuntime, tag string) error {
541+
if tag == "" {
542+
return nil
543+
}
544+
545+
re := regexp.MustCompile(`^(\d+\.\d+\.\d+)`)
546+
matches := re.FindStringSubmatch(tag)
547+
if len(matches) <= 1 {
548+
return nil
549+
}
550+
551+
version := matches[1] + "-MariaDB-log"
552+
if version != comp.Status.MariaDBVersion {
553+
svc.Log.Info("Updating MariaDB version in status", "version", version)
554+
comp.Status.MariaDBVersion = version
555+
return svc.SetDesiredCompositeStatus(comp)
556+
}
557+
558+
return nil
559+
}

pkg/comp-functions/functions/vshnmariadb/proxysql.go

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,12 @@ var (
3333
)
3434

3535
type proxySQLConfigParams struct {
36-
CompName string
37-
RootPassword string
38-
Namespace string
39-
TLS int
40-
Users []proxySQLUsers
36+
CompName string
37+
RootPassword string
38+
Namespace string
39+
TLS int
40+
Users []proxySQLUsers
41+
MariaDBVersion string
4142
}
4243

4344
type proxySQLUsers struct {
@@ -119,11 +120,12 @@ func createProxySQLConfig(comp *vshnv1.VSHNMariaDB, svc *runtime.ServiceRuntime,
119120
}
120121

121122
confParams := proxySQLConfigParams{
122-
CompName: comp.GetName(),
123-
RootPassword: string(cd["MARIADB_PASSWORD"]),
124-
Namespace: comp.GetInstanceNamespace(),
125-
Users: userList,
126-
TLS: tls,
123+
CompName: comp.GetName(),
124+
RootPassword: string(cd["MARIADB_PASSWORD"]),
125+
Namespace: comp.GetInstanceNamespace(),
126+
Users: userList,
127+
TLS: tls,
128+
MariaDBVersion: comp.Status.MariaDBVersion,
127129
}
128130

129131
var buf bytes.Buffer

pkg/comp-functions/functions/vshnmariadb/proxysql_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ func Test_createProxySQLConfig(t *testing.T) {
5353
svc := commontest.LoadRuntimeFromFile(t, "empty.yaml")
5454
comp := getComp()
5555

56-
expectedHash := "56e11403faab20eb54184eccef557c85"
56+
expectedHash := "1bf00f243388da2aa7ba6b8d14bf7ad3"
5757

5858
hash, err := createProxySQLConfig(comp, svc, false)
5959
assert.NoError(t, err)

0 commit comments

Comments
 (0)