Skip to content

Commit e34a291

Browse files
committed
fix golangci reported issues
Signed-off-by: Martin Schuppert <[email protected]>
1 parent 4f2b454 commit e34a291

File tree

9 files changed

+53
-38
lines changed

9 files changed

+53
-38
lines changed

controllers/galera_controller.go

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,14 @@ See the License for the specific language governing permissions and
1414
limitations under the License.
1515
*/
1616

17+
// Package controllers contains the Galera and MariaDB account controllers for the mariadb-operator.
1718
package controllers
1819

1920
import (
2021
"bytes"
2122
"context"
2223
"encoding/json"
24+
"errors"
2325
"fmt"
2426
"sort"
2527
"strconv"
@@ -63,16 +65,23 @@ import (
6365
"sigs.k8s.io/controller-runtime/pkg/reconcile"
6466

6567
topologyv1 "github.com/openstack-k8s-operators/infra-operator/apis/topology/v1beta1"
66-
databasev1beta1 "github.com/openstack-k8s-operators/mariadb-operator/api/v1beta1"
6768
mariadbv1 "github.com/openstack-k8s-operators/mariadb-operator/api/v1beta1"
6869
mariadb "github.com/openstack-k8s-operators/mariadb-operator/pkg/mariadb"
6970
)
7071

7172
// fields to index to reconcile on CR change
7273
const (
73-
serviceSecretNameField = ".spec.tls.genericService.SecretName"
74-
caSecretNameField = ".spec.tls.ca.caBundleSecretName"
75-
topologyField = ".spec.topologyRef.Name"
74+
// serviceSecretNameField specifies the field path for TLS service secret name
75+
serviceSecretNameField = ".spec.tls.genericService.SecretName" // #nosec G101 -- This is a field path, not a credential
76+
// caSecretNameField specifies the field path for CA bundle secret name
77+
caSecretNameField = ".spec.tls.ca.caBundleSecretName" // #nosec G101 -- This is a field path, not a credential
78+
topologyField = ".spec.topologyRef.Name"
79+
)
80+
81+
// Static errors
82+
var (
83+
// ErrOpenStackSecretNotFound indicates that the OpenStack secret was not found
84+
ErrOpenStackSecretNotFound = errors.New("OpenStack secret not found")
7685
)
7786

7887
var allWatchFields = []string{
@@ -574,7 +583,7 @@ func (r *GaleraReconciler) Reconcile(ctx context.Context, req ctrl.Request) (res
574583
condition.RequestedReason,
575584
condition.SeverityInfo,
576585
condition.InputReadyWaitingMessage))
577-
return res, fmt.Errorf("OpenStack secret %s not found", instance.Spec.Secret)
586+
return res, fmt.Errorf("%w: %s", ErrOpenStackSecretNotFound, instance.Spec.Secret)
578587
}
579588
instance.Status.Conditions.Set(condition.FalseCondition(
580589
condition.InputReadyCondition,
@@ -605,7 +614,7 @@ func (r *GaleraReconciler) Reconcile(ctx context.Context, req ctrl.Request) (res
605614
condition.TLSInputReadyCondition,
606615
condition.RequestedReason,
607616
condition.SeverityInfo,
608-
fmt.Sprintf(condition.TLSInputReadyWaitingMessage, instance.Spec.TLS.CaBundleSecretName)))
617+
condition.TLSInputReadyWaitingMessage, instance.Spec.TLS.CaBundleSecretName))
609618
return ctrl.Result{}, nil
610619
}
611620
instance.Status.Conditions.Set(condition.FalseCondition(
@@ -631,7 +640,7 @@ func (r *GaleraReconciler) Reconcile(ctx context.Context, req ctrl.Request) (res
631640
condition.TLSInputReadyCondition,
632641
condition.RequestedReason,
633642
condition.SeverityInfo,
634-
fmt.Sprintf(condition.TLSInputReadyWaitingMessage, err.Error())))
643+
condition.TLSInputReadyWaitingMessage, err.Error()))
635644
return ctrl.Result{}, nil
636645
}
637646
instance.Status.Conditions.Set(condition.FalseCondition(
@@ -662,7 +671,7 @@ func (r *GaleraReconciler) Reconcile(ctx context.Context, req ctrl.Request) (res
662671

663672
// build state of the restart hash. this is used to decide whether the
664673
// statefulset must stop all its pods before applying a config update
665-
clusterPropertiesEnv["GCommTLS"] = env.SetValue(strconv.FormatBool(instance.Spec.TLS.Enabled() && instance.Spec.TLS.Ca.CaBundleSecretName != ""))
674+
clusterPropertiesEnv["GCommTLS"] = env.SetValue(strconv.FormatBool(instance.Spec.TLS.Enabled() && instance.Spec.TLS.CaBundleSecretName != ""))
666675
clusterPropertiesHash, err := util.HashOfInputHashes(clusterPropertiesEnv)
667676
if err != nil {
668677
return ctrl.Result{}, err
@@ -981,8 +990,8 @@ func (r *GaleraReconciler) SetupWithManager(mgr ctrl.Manager) error {
981990
// Extract the secret name from the spec, if one is provided
982991
cr := rawObj.(*mariadbv1.Galera)
983992
tls := &cr.Spec.TLS
984-
if tls.Ca.CaBundleSecretName != "" {
985-
return []string{tls.Ca.CaBundleSecretName}
993+
if tls.CaBundleSecretName != "" {
994+
return []string{tls.CaBundleSecretName}
986995
}
987996
return nil
988997
}); err != nil {
@@ -994,7 +1003,7 @@ func (r *GaleraReconciler) SetupWithManager(mgr ctrl.Manager) error {
9941003
cr := rawObj.(*mariadbv1.Galera)
9951004
tls := &cr.Spec.TLS
9961005
if tls.Enabled() {
997-
return []string{*tls.GenericService.SecretName}
1006+
return []string{*tls.SecretName}
9981007
}
9991008
return nil
10001009
}); err != nil {
@@ -1036,8 +1045,8 @@ func (r *GaleraReconciler) SetupWithManager(mgr ctrl.Manager) error {
10361045
// GetDatabaseObject - returns either a Galera or MariaDB object (and an associated client.Object interface).
10371046
// used by both MariaDBDatabaseReconciler and MariaDBAccountReconciler
10381047
// this will later return only Galera objects, so as a lookup it's part of the galera controller
1039-
func GetDatabaseObject(ctx context.Context, clientObj client.Client, name string, namespace string) (*databasev1beta1.Galera, error) {
1040-
dbGalera := &databasev1beta1.Galera{
1048+
func GetDatabaseObject(ctx context.Context, clientObj client.Client, name string, namespace string) (*mariadbv1.Galera, error) {
1049+
dbGalera := &mariadbv1.Galera{
10411050
ObjectMeta: metav1.ObjectMeta{
10421051
Name: name,
10431052
Namespace: namespace,
@@ -1089,7 +1098,7 @@ func (r *GaleraReconciler) findObjectsForSrc(ctx context.Context, src client.Obj
10891098
return requests
10901099
}
10911100

1092-
func (r *GaleraReconciler) reconcileDelete(ctx context.Context, instance *databasev1beta1.Galera, helper *helper.Helper) (ctrl.Result, error) {
1101+
func (r *GaleraReconciler) reconcileDelete(ctx context.Context, instance *mariadbv1.Galera, helper *helper.Helper) (ctrl.Result, error) {
10931102
helper.GetLogger().Info("Reconciling Service delete")
10941103

10951104
// Remove our finalizer from the db svc

controllers/mariadbaccount_controller.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -59,14 +59,14 @@ func (r *MariaDBAccountReconciler) SetupWithManager(mgr ctrl.Manager) error {
5959
//+kubebuilder:rbac:groups=mariadb.openstack.org,resources=mariadbaccounts/finalizers,verbs=update;patch
6060
//+kubebuilder:rbac:groups=core,resources=secrets,verbs=get;list;create;update;delete;patch
6161

62-
// Reconcile
62+
// Reconcile reconciles a MariaDBAccount object
6363
func (r *MariaDBAccountReconciler) Reconcile(ctx context.Context, req ctrl.Request) (result ctrl.Result, _err error) {
6464
log := GetLog(ctx, "MariaDBAccount")
6565

6666
var err error
6767

6868
instance := &databasev1beta1.MariaDBAccount{}
69-
err = r.Client.Get(ctx, req.NamespacedName, instance)
69+
err = r.Get(ctx, req.NamespacedName, instance)
7070
if err != nil {
7171
return ctrl.Result{}, client.IgnoreNotFound(err)
7272
}
@@ -142,7 +142,7 @@ func (r *MariaDBAccountReconciler) reconcileCreate(
142142
// the reference to the secret itself is given in the spec
143143
// the convention appears to be: "things we are dependent on are named in labels,
144144
// things we are setting up are named in the spec"
145-
mariadbDatabaseName := instance.ObjectMeta.Labels["mariaDBDatabaseName"]
145+
mariadbDatabaseName := instance.Labels["mariaDBDatabaseName"]
146146
if mariadbDatabaseName == "" {
147147

148148
log.Info(fmt.Sprintf(
@@ -169,7 +169,7 @@ func (r *MariaDBAccountReconciler) reconcileCreate(
169169

170170
log.Info(fmt.Sprintf(
171171
"MariaDBAccount '%s' didn't find MariaDBDatabase '%s'; requeueing",
172-
instance.Name, instance.ObjectMeta.Labels["mariaDBDatabaseName"]))
172+
instance.Name, instance.Labels["mariaDBDatabaseName"]))
173173

174174
return ctrl.Result{RequeueAfter: time.Duration(10) * time.Second}, nil
175175
} else if err == nil && !mariadbDatabase.Status.Conditions.IsTrue(databasev1beta1.MariaDBDatabaseReadyCondition) {
@@ -186,7 +186,7 @@ func (r *MariaDBAccountReconciler) reconcileCreate(
186186

187187
log.Info(fmt.Sprintf(
188188
"MariaDBAccount '%s' MariaDBDatabase '%s' not yet complete; requeueing",
189-
instance.Name, instance.ObjectMeta.Labels["mariaDBDatabaseName"]))
189+
instance.Name, instance.Labels["mariaDBDatabaseName"]))
190190

191191
return ctrl.Result{RequeueAfter: time.Duration(10) * time.Second}, nil
192192
} else if err != nil {
@@ -345,7 +345,7 @@ func (r *MariaDBAccountReconciler) reconcileDelete(
345345
// the reference to the secret itself is given in the spec
346346
// the convention appears to be: "things we are dependent on are named in labels,
347347
// things we are setting up are named in the spec"
348-
mariadbDatabaseName := instance.ObjectMeta.Labels["mariaDBDatabaseName"]
348+
mariadbDatabaseName := instance.Labels["mariaDBDatabaseName"]
349349
if mariadbDatabaseName == "" {
350350

351351
log.Info(fmt.Sprintf(
@@ -367,7 +367,7 @@ func (r *MariaDBAccountReconciler) reconcileDelete(
367367
// our own instance and return
368368
log.Info(fmt.Sprintf(
369369
"MariaDBAccount '%s' Didn't find MariaDBDatabase '%s'; no account delete needed",
370-
instance.Name, instance.ObjectMeta.Labels["mariaDBDatabaseName"]))
370+
instance.Name, instance.Labels["mariaDBDatabaseName"]))
371371

372372
controllerutil.RemoveFinalizer(instance, helper.GetFinalizer())
373373

@@ -380,7 +380,7 @@ func (r *MariaDBAccountReconciler) reconcileDelete(
380380
// our own instance and return
381381
log.Info(fmt.Sprintf(
382382
"MariaDBAccount '%s' MariaDBDatabase '%s' not yet complete; no account delete needed",
383-
instance.Name, instance.ObjectMeta.Labels["mariaDBDatabaseName"]))
383+
instance.Name, instance.Labels["mariaDBDatabaseName"]))
384384

385385
// first, remove finalizer from the MariaDBDatabase instance
386386
if controllerutil.RemoveFinalizer(mariadbDatabase, fmt.Sprintf("%s-%s", helper.GetFinalizer(), instance.Name)) {
@@ -557,7 +557,7 @@ func (r *MariaDBAccountReconciler) reconcileDelete(
557557

558558
// getDatabaseObject - returns either a Galera or MariaDB object (and an associated client.Object interface)
559559
func (r *MariaDBAccountReconciler) getDatabaseObject(ctx context.Context, mariaDBDatabase *databasev1beta1.MariaDBDatabase, instance *databasev1beta1.MariaDBAccount) (*databasev1beta1.Galera, error) {
560-
dbName := mariaDBDatabase.ObjectMeta.Labels["dbName"]
560+
dbName := mariaDBDatabase.Labels["dbName"]
561561
return GetDatabaseObject(
562562
ctx, r.Client,
563563
dbName,
@@ -578,7 +578,7 @@ func (r *MariaDBAccountReconciler) getMariaDBDatabaseObject(ctx context.Context,
578578

579579
objectKey := client.ObjectKeyFromObject(mariaDBDatabase)
580580

581-
err := r.Client.Get(ctx, objectKey, mariaDBDatabase)
581+
err := r.Get(ctx, objectKey, mariaDBDatabase)
582582
if err != nil {
583583
return nil, err
584584
}

controllers/mariadbdatabase_controller.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ func (r *MariaDBDatabaseReconciler) Reconcile(ctx context.Context, req ctrl.Requ
5656

5757
// Fetch the MariaDBDatabase instance
5858
instance := &databasev1beta1.MariaDBDatabase{}
59-
err = r.Client.Get(ctx, req.NamespacedName, instance)
59+
err = r.Get(ctx, req.NamespacedName, instance)
6060
if err != nil {
6161
return ctrl.Result{}, client.IgnoreNotFound(err)
6262
}
@@ -258,7 +258,7 @@ func (r *MariaDBDatabaseReconciler) SetupWithManager(mgr ctrl.Manager) error {
258258
func (r *MariaDBDatabaseReconciler) getDatabaseObject(ctx context.Context, instance *databasev1beta1.MariaDBDatabase) (*databasev1beta1.Galera, error) {
259259
return GetDatabaseObject(
260260
ctx, r.Client,
261-
instance.ObjectMeta.Labels["dbName"],
261+
instance.Labels["dbName"],
262262
instance.Namespace,
263263
)
264264
}

main.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ See the License for the specific language governing permissions and
1414
limitations under the License.
1515
*/
1616

17+
// Package main implements the mariadb-operator controller manager.
1718
package main
1819

1920
import (

pkg/mariadb/account.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// Package mariadb contains MariaDB database management functionality.
12
package mariadb
23

34
import (
@@ -18,6 +19,7 @@ type accountCreateOrDeleteOptions struct {
1819
RequireTLS string
1920
}
2021

22+
// CreateDbAccountJob creates a Kubernetes job for creating a MariaDB database account
2123
func CreateDbAccountJob(account *databasev1beta1.MariaDBAccount, databaseName string, databaseHostName string, databaseSecret string, containerImage string, serviceAccountName string, nodeSelector *map[string]string) (*batchv1.Job, error) {
2224
var tlsStatement string
2325
if account.Spec.RequireTLS {
@@ -45,7 +47,7 @@ func CreateDbAccountJob(account *databasev1beta1.MariaDBAccount, databaseName st
4547
// provided db name is used as metadata name where underscore is a not allowed
4648
// character. Lets replace all underscores with hypen. Underscores in the db name are
4749
// possible.
48-
Name: strings.Replace(account.Spec.UserName, "_", "-", -1) + "-account-create",
50+
Name: strings.ReplaceAll(account.Spec.UserName, "_", "-") + "-account-create",
4951
Namespace: account.Namespace,
5052
Labels: labels,
5153
},
@@ -97,6 +99,7 @@ func CreateDbAccountJob(account *databasev1beta1.MariaDBAccount, databaseName st
9799
return job, nil
98100
}
99101

102+
// DeleteDbAccountJob creates a Kubernetes job for deleting a MariaDB database account
100103
func DeleteDbAccountJob(account *databasev1beta1.MariaDBAccount, databaseName string, databaseHostName string, databaseSecret string, containerImage string, serviceAccountName string, nodeSelector *map[string]string) (*batchv1.Job, error) {
101104

102105
opts := accountCreateOrDeleteOptions{account.Spec.UserName, databaseName, databaseHostName, "root", ""}
@@ -110,7 +113,7 @@ func DeleteDbAccountJob(account *databasev1beta1.MariaDBAccount, databaseName st
110113
}
111114
job := &batchv1.Job{
112115
ObjectMeta: metav1.ObjectMeta{
113-
Name: strings.Replace(account.Spec.UserName, "_", "", -1) + "-account-delete",
116+
Name: strings.ReplaceAll(account.Spec.UserName, "_", "") + "-account-delete",
114117
Namespace: account.Namespace,
115118
Labels: labels,
116119
},

pkg/mariadb/const.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@ const (
77
// ActivePodSelectorKey - Selector key used to configure A/P service behavior
88
ActivePodSelectorKey = "statefulset.kubernetes.io/pod-name"
99

10-
// Time allowed during a the startup probe (in seconds)
10+
// StartupProbeTimeout is the time allowed during the startup probe (in seconds)
1111
StartupProbeTimeout = 240
1212
)

pkg/mariadb/database.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ func DbDatabaseJob(database *databasev1beta1.MariaDBDatabase, databaseHostName s
9393
// provided db name is used as metadata name where underscore is a not allowed
9494
// character. Lets replace all underscores with hypen. Underscores in the db name are
9595
// possible.
96-
Name: strings.Replace(database.Spec.Name, "_", "-", -1) + "-db-create",
96+
Name: strings.ReplaceAll(database.Spec.Name, "_", "-") + "-db-create",
9797
Namespace: database.Namespace,
9898
Labels: labels,
9999
},
@@ -188,7 +188,7 @@ func DeleteDbDatabaseJob(database *databasev1beta1.MariaDBDatabase, databaseHost
188188

189189
job := &batchv1.Job{
190190
ObjectMeta: metav1.ObjectMeta{
191-
Name: strings.Replace(database.Spec.Name, "_", "", -1) + "-database-delete",
191+
Name: strings.ReplaceAll(database.Spec.Name, "_", "") + "-database-delete",
192192
Namespace: database.Namespace,
193193
Labels: labels,
194194
},

pkg/mariadb/service.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
66
)
77

8+
// Service creates a Kubernetes service for MariaDB database
89
func Service(db metav1.Object) *corev1.Service {
910
selectors := LabelSelectors(db, "galera")
1011
// NOTE(dciabrin) we currently deploy the Galera cluster as A/P,

pkg/mariadb/volumes.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
)
88

99
const (
10+
// GaleraCertPrefix is the prefix used for Galera cluster certificates
1011
GaleraCertPrefix = "galera"
1112
)
1213

@@ -23,7 +24,7 @@ func getGaleraVolumes(g *mariadbv1.Galera) []corev1.Volume {
2324
}
2425

2526
if g.Spec.TLS.Enabled() {
26-
if g.Spec.TLS.Ca.CaBundleSecretName != "" {
27+
if g.Spec.TLS.CaBundleSecretName != "" {
2728
configTemplates = append(configTemplates, corev1.KeyToPath{
2829
Key: "galera_tls.cnf.in",
2930
Path: "galera_tls.cnf.in",
@@ -125,15 +126,15 @@ func getGaleraVolumes(g *mariadbv1.Galera) []corev1.Volume {
125126

126127
if g.Spec.TLS.Enabled() {
127128
svc := tls.Service{
128-
SecretName: *g.Spec.TLS.GenericService.SecretName,
129+
SecretName: *g.Spec.TLS.SecretName,
129130
CertMount: nil,
130131
KeyMount: nil,
131132
CaMount: nil,
132133
}
133134
serviceVolume := svc.CreateVolume(GaleraCertPrefix)
134135
volumes = append(volumes, serviceVolume)
135-
if g.Spec.TLS.Ca.CaBundleSecretName != "" {
136-
caVolume := g.Spec.TLS.Ca.CreateVolume()
136+
if g.Spec.TLS.CaBundleSecretName != "" {
137+
caVolume := g.Spec.TLS.CreateVolume()
137138
volumes = append(volumes, caVolume)
138139
}
139140
}
@@ -175,15 +176,15 @@ func getGaleraVolumeMounts(g *mariadbv1.Galera) []corev1.VolumeMount {
175176

176177
if g.Spec.TLS.Enabled() {
177178
svc := tls.Service{
178-
SecretName: *g.Spec.TLS.GenericService.SecretName,
179+
SecretName: *g.Spec.TLS.SecretName,
179180
CertMount: nil,
180181
KeyMount: nil,
181182
CaMount: nil,
182183
}
183184
serviceVolumeMounts := svc.CreateVolumeMounts(GaleraCertPrefix)
184185
volumeMounts = append(volumeMounts, serviceVolumeMounts...)
185-
if g.Spec.TLS.Ca.CaBundleSecretName != "" {
186-
caVolumeMounts := g.Spec.TLS.Ca.CreateVolumeMounts(nil)
186+
if g.Spec.TLS.CaBundleSecretName != "" {
187+
caVolumeMounts := g.Spec.TLS.CreateVolumeMounts(nil)
187188
volumeMounts = append(volumeMounts, caVolumeMounts...)
188189
}
189190
}

0 commit comments

Comments
 (0)