Skip to content

Commit fe7cc2b

Browse files
authored
Merge pull request #175 from tetrateio/feature/add-config-serviceAccountName
Support serviceAccountName configuration overwrite
2 parents 65c489f + 2b79aec commit fe7cc2b

12 files changed

+343
-27
lines changed

api/v1/kubegres_types.go

+15-14
Original file line numberDiff line numberDiff line change
@@ -62,20 +62,21 @@ type Probe struct {
6262
}
6363

6464
type KubegresSpec struct {
65-
Replicas *int32 `json:"replicas,omitempty"`
66-
Image string `json:"image,omitempty"`
67-
Port int32 `json:"port,omitempty"`
68-
ImagePullSecrets []v1.LocalObjectReference `json:"imagePullSecrets,omitempty"`
69-
CustomConfig string `json:"customConfig,omitempty"`
70-
Database KubegresDatabase `json:"database,omitempty"`
71-
Failover KubegresFailover `json:"failover,omitempty"`
72-
Backup KubegresBackUp `json:"backup,omitempty"`
73-
Env []v1.EnvVar `json:"env,omitempty"`
74-
Scheduler KubegresScheduler `json:"scheduler,omitempty"`
75-
Resources v1.ResourceRequirements `json:"resources,omitempty"`
76-
Volume Volume `json:"volume,omitempty"`
77-
SecurityContext *v1.PodSecurityContext `json:"securityContext,omitempty"`
78-
Probe Probe `json:"probe,omitempty"`
65+
Replicas *int32 `json:"replicas,omitempty"`
66+
Image string `json:"image,omitempty"`
67+
Port int32 `json:"port,omitempty"`
68+
ImagePullSecrets []v1.LocalObjectReference `json:"imagePullSecrets,omitempty"`
69+
CustomConfig string `json:"customConfig,omitempty"`
70+
Database KubegresDatabase `json:"database,omitempty"`
71+
Failover KubegresFailover `json:"failover,omitempty"`
72+
Backup KubegresBackUp `json:"backup,omitempty"`
73+
Env []v1.EnvVar `json:"env,omitempty"`
74+
Scheduler KubegresScheduler `json:"scheduler,omitempty"`
75+
Resources v1.ResourceRequirements `json:"resources,omitempty"`
76+
Volume Volume `json:"volume,omitempty"`
77+
SecurityContext *v1.PodSecurityContext `json:"securityContext,omitempty"`
78+
Probe Probe `json:"probe,omitempty"`
79+
ServiceAccountName string `json:"serviceAccountName,omitempty"`
7980
}
8081

8182
// ----------------------- STATUS -----------------------------------------

config/crd/bases/kubegres.reactive-tech.io_kubegres.yaml

+2
Original file line numberDiff line numberDiff line change
@@ -1619,6 +1619,8 @@ spec:
16191619
type: string
16201620
type: object
16211621
type: object
1622+
serviceAccountName:
1623+
type: string
16221624
volume:
16231625
properties:
16241626
volumeClaimTemplates:

internal/controller/ctx/KubegresContext.go

+4-2
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,13 @@ package ctx
2222

2323
import (
2424
"context"
25+
"strconv"
26+
"strings"
27+
2528
"reactive-tech.io/kubegres/api/v1"
2629
"reactive-tech.io/kubegres/internal/controller/ctx/log"
2730
"reactive-tech.io/kubegres/internal/controller/ctx/status"
2831
"sigs.k8s.io/controller-runtime/pkg/client"
29-
"strconv"
30-
"strings"
3132
)
3233

3334
type KubegresContext struct {
@@ -48,6 +49,7 @@ const (
4849
BaseConfigMapName = "base-kubegres-config"
4950
CronJobNamePrefix = "backup-"
5051
DefaultContainerPortNumber = 5432
52+
DefaultPodServiceAccountName = "default"
5153
DefaultDatabaseVolumeMount = "/var/lib/postgresql/data"
5254
DefaultDatabaseFolder = "pgdata"
5355
EnvVarNamePgData = "PGDATA"

internal/controller/ctx/resources/ResourcesContext.go

+3
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ package resources
2222

2323
import (
2424
"context"
25+
2526
ctx2 "reactive-tech.io/kubegres/internal/controller/ctx"
2627
"reactive-tech.io/kubegres/internal/controller/ctx/log"
2728
"reactive-tech.io/kubegres/internal/controller/ctx/status"
@@ -166,6 +167,7 @@ func addStatefulSetSpecEnforcers(rc *ResourcesContext) {
166167
securityContextSpecEnforcer := statefulset_spec2.CreateSecurityContextSpecEnforcer(rc.KubegresContext)
167168
livenessProbeSpecEnforcer := statefulset_spec2.CreateLivenessProbeSpecEnforcer(rc.KubegresContext)
168169
readinessProbeSpecEnforcer := statefulset_spec2.CreateReadinessProbeSpecEnforcer(rc.KubegresContext)
170+
serviAccountNameSpecEnforcer := statefulset_spec2.CreateServiceAccountNameSpecEnforcer(rc.KubegresContext)
169171

170172
rc.StatefulSetsSpecsEnforcer = statefulset_spec2.CreateStatefulSetsSpecsEnforcer(rc.KubegresContext)
171173
rc.StatefulSetsSpecsEnforcer.AddSpecEnforcer(&imageSpecEnforcer)
@@ -179,6 +181,7 @@ func addStatefulSetSpecEnforcers(rc *ResourcesContext) {
179181
rc.StatefulSetsSpecsEnforcer.AddSpecEnforcer(&securityContextSpecEnforcer)
180182
rc.StatefulSetsSpecsEnforcer.AddSpecEnforcer(&livenessProbeSpecEnforcer)
181183
rc.StatefulSetsSpecsEnforcer.AddSpecEnforcer(&readinessProbeSpecEnforcer)
184+
rc.StatefulSetsSpecsEnforcer.AddSpecEnforcer(&serviAccountNameSpecEnforcer)
182185

183186
rc.AllStatefulSetsSpecEnforcer = statefulset_spec2.CreateAllStatefulSetsSpecEnforcer(rc.KubegresContext, rc.ResourcesStates, rc.BlockingOperation, rc.StatefulSetsSpecsEnforcer)
184187
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
Copyright 2023 Reactive Tech Limited.
3+
"Reactive Tech Limited" is a company located in England, United Kingdom.
4+
https://www.reactive-tech.io
5+
6+
Lead Developer: Alex Arica
7+
8+
Licensed under the Apache License, Version 2.0 (the "License");
9+
you may not use this file except in compliance with the License.
10+
You may obtain a copy of the License at
11+
12+
http://www.apache.org/licenses/LICENSE-2.0
13+
14+
Unless required by applicable law or agreed to in writing, software
15+
distributed under the License is distributed on an "AS IS" BASIS,
16+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17+
See the License for the specific language governing permissions and
18+
limitations under the License.
19+
*/
20+
21+
package statefulset_spec
22+
23+
import (
24+
apps "k8s.io/api/apps/v1"
25+
"reactive-tech.io/kubegres/internal/controller/ctx"
26+
)
27+
28+
type ServiceAccountNameSpecEnforcer struct {
29+
kubegresContext ctx.KubegresContext
30+
}
31+
32+
func CreateServiceAccountNameSpecEnforcer(kubegresContext ctx.KubegresContext) ServiceAccountNameSpecEnforcer {
33+
return ServiceAccountNameSpecEnforcer{kubegresContext: kubegresContext}
34+
}
35+
36+
func (r *ServiceAccountNameSpecEnforcer) GetSpecName() string {
37+
return "ServiceAccountName"
38+
}
39+
40+
func (r *ServiceAccountNameSpecEnforcer) CheckForSpecDifference(statefulSet *apps.StatefulSet) StatefulSetSpecDifference {
41+
42+
current := statefulSet.Spec.Template.Spec.ServiceAccountName
43+
expected := r.kubegresContext.Kubegres.Spec.ServiceAccountName
44+
45+
if current != expected {
46+
return StatefulSetSpecDifference{
47+
SpecName: r.GetSpecName(),
48+
Current: current,
49+
Expected: expected,
50+
}
51+
}
52+
53+
return StatefulSetSpecDifference{}
54+
}
55+
56+
func (r *ServiceAccountNameSpecEnforcer) EnforceSpec(statefulSet *apps.StatefulSet) (wasSpecUpdated bool, err error) {
57+
statefulSet.Spec.Template.Spec.ServiceAccountName = r.kubegresContext.Kubegres.Spec.ServiceAccountName
58+
return true, nil
59+
}
60+
61+
func (r *ServiceAccountNameSpecEnforcer) OnSpecEnforcedSuccessfully(_ *apps.StatefulSet) error {
62+
return nil
63+
}

internal/controller/spec/template/ResourcesCreatorFromTemplate.go

+6-1
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,10 @@ limitations under the License.
2121
package template
2222

2323
import (
24-
"reactive-tech.io/kubegres/internal/controller/ctx"
2524
"strconv"
2625

26+
"reactive-tech.io/kubegres/internal/controller/ctx"
27+
2728
apps "k8s.io/api/apps/v1"
2829
batch "k8s.io/api/batch/v1"
2930
core "k8s.io/api/core/v1"
@@ -264,6 +265,10 @@ func (r *ResourcesCreatorFromTemplate) initStatefulSet(
264265
if postgresSpec.Probe.ReadinessProbe != nil {
265266
statefulSetTemplate.Spec.Template.Spec.Containers[0].ReadinessProbe = postgresSpec.Probe.ReadinessProbe
266267
}
268+
269+
if postgresSpec.ServiceAccountName != "" {
270+
statefulSetTemplate.Spec.Template.Spec.ServiceAccountName = postgresSpec.ServiceAccountName
271+
}
267272
}
268273

269274
// Extract annotations set in Kubegres YAML by

internal/controller/spec/template/yaml/Templates.go

+4-6
Original file line numberDiff line numberDiff line change
@@ -152,12 +152,10 @@ data:
152152
#echo "$dt - Running: psql -v ON_ERROR_STOP=1 --username $POSTGRES_USER --dbname $POSTGRES_DB ...";
153153
154154
#psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" --dbname "$POSTGRES_DB" <<-EOSQL
155-
#CREATE DATABASE $customDatabaseName;
156-
#\connect $customDatabaseName;
157-
#CREATE USER $customUserName WITH PASSWORD '$POSTGRES_MYAPP_PASSWORD';
158-
#GRANT SELECT, INSERT, UPDATE, DELETE, TRUNCATE, REFERENCES, TRIGGER ON ALL TABLES IN SCHEMA public TO $customUserName;
159-
#GRANT USAGE, SELECT, UPDATE ON ALL SEQUENCES IN SCHEMA public TO $customUserName;
160-
#GRANT USAGE, CREATE ON SCHEMA public TO $customUserName;
155+
#CREATE USER $customUserName WITH PASSWORD '$POSTGRES_MYAPP_PASSWORD';
156+
#CREATE DATABASE $customDatabaseName;
157+
#\connect $customDatabaseName;
158+
#GRANT ALL ON SCHEMA public TO $customUserName;
161159
#EOSQL
162160
163161
#echo "$dt - Init script is completed";

internal/test/resourceConfigs/ConfigForTest.go

+3
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ const (
4040
SecretYamlFile = "resourceConfigs/secret.yaml"
4141
SecretResourceName = "my-kubegres-secret"
4242

43+
ServiceAccountYamlFile = "resourceConfigs/serviceAccount.yaml"
44+
ServiceAccountResourceName = "my-kubegres"
45+
4346
ServiceToSqlQueryPrimaryDbYamlFile = "resourceConfigs/primaryService.yaml"
4447
ServiceToSqlQueryPrimaryDbResourceName = "test-kubegres-primary"
4548
ServiceToSqlQueryPrimaryDbNodePort = 30007

internal/test/resourceConfigs/LoadTestYaml.go

+8-1
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,11 @@ package resourceConfigs
2222

2323
import (
2424
"io/ioutil"
25+
"log"
26+
2527
v1 "k8s.io/api/core/v1"
2628
"k8s.io/apimachinery/pkg/runtime"
2729
"k8s.io/client-go/kubernetes/scheme"
28-
"log"
2930
kubegresv1 "reactive-tech.io/kubegres/api/v1"
3031
)
3132

@@ -53,6 +54,12 @@ func LoadSecretYaml() v1.Secret {
5354
return *obj.(*v1.Secret)
5455
}
5556

57+
func LoadServiceAccountYaml() v1.ServiceAccount {
58+
fileContents := getFileContents(ServiceAccountYamlFile)
59+
obj := decodeYaml(fileContents)
60+
return *obj.(*v1.ServiceAccount)
61+
}
62+
5663
func LoadYamlServiceToSqlQueryPrimaryDb() v1.Service {
5764
fileContents := getFileContents(ServiceToSqlQueryPrimaryDbYamlFile)
5865
obj := decodeYaml(fileContents)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
apiVersion: v1
2+
kind: ServiceAccount
3+
metadata:
4+
name: my-kubegres
5+
namespace: default

0 commit comments

Comments
 (0)