Skip to content

Commit 6eafe19

Browse files
fmountclaude
andcommitted
Modernize Go code with latest style conventions
Applied automated modernization using gopls modernize tool to update: - Replace interface{} with any type alias (Go 1.18+) - Update range loops to use idiomatic range syntax - Replace fmt.Sprintf with fmt.Appendf where appropriate These changes improve code readability and align with current Go best practices. Co-Authored-By: Claude <[email protected]> Signed-off-by: Francesco Pantano <[email protected]>
1 parent a329ec6 commit 6eafe19

11 files changed

+49
-53
lines changed

controllers/amphoracontroller_controller.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ package controllers
2020
import (
2121
"context"
2222
"fmt"
23+
"maps"
2324
"sort"
2425
"strings"
2526
"time"
@@ -572,9 +573,7 @@ func (r *OctaviaAmphoraControllerReconciler) generateServiceSecrets(
572573
common.CustomServiceConfigFileName: instance.Spec.CustomServiceConfig,
573574
"my.cnf": db.GetDatabaseClientConfig(tlsCfg), //(mschuppert) for now just get the default my.cnf
574575
}
575-
for key, data := range instance.Spec.DefaultConfigOverwrite {
576-
customData[key] = data
577-
}
576+
maps.Copy(customData, instance.Spec.DefaultConfigOverwrite)
578577

579578
databaseAccount, dbSecret, err := mariadbv1.GetAccountAndSecret(
580579
ctx, helper, instance.Spec.DatabaseAccount, instance.Namespace)
@@ -608,7 +607,7 @@ func (r *OctaviaAmphoraControllerReconciler) generateServiceSecrets(
608607
mariadbv1.MariaDBAccountReadyCondition,
609608
mariadbv1.MariaDBAccountReadyMessage)
610609

611-
templateParameters := map[string]interface{}{
610+
templateParameters := map[string]any{
612611
"DatabaseConnection": fmt.Sprintf("mysql+pymysql://%s:%s@%s/%s?read_default_file=/etc/my.cnf",
613612
databaseAccount.Spec.UserName,
614613
string(dbSecret.Data[mariadbv1.DatabasePasswordSelector]),

controllers/octavia_common.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import (
2525

2626
type conditionUpdater interface {
2727
Set(c *condition.Condition)
28-
MarkTrue(t condition.Type, messageFormat string, messageArgs ...interface{})
28+
MarkTrue(t condition.Type, messageFormat string, messageArgs ...any)
2929
}
3030

3131
type topologyHandler interface {

controllers/octavia_controller.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"bufio"
2121
"context"
2222
"fmt"
23+
"maps"
2324
"net/http"
2425
"sort"
2526
"strings"
@@ -1435,9 +1436,7 @@ func (r *OctaviaReconciler) generateServiceSecrets(
14351436
common.CustomServiceConfigFileName: instance.Spec.CustomServiceConfig,
14361437
"my.cnf": octaviaDb.GetDatabaseClientConfig(tlsCfg), //(mschuppert) for now just get the default my.cnf
14371438
}
1438-
for key, data := range instance.Spec.DefaultConfigOverwrite {
1439-
customData[key] = data
1440-
}
1439+
maps.Copy(customData, instance.Spec.DefaultConfigOverwrite)
14411440

14421441
databaseAccount := octaviaDb.GetAccount()
14431442
dbSecret := octaviaDb.GetSecret()
@@ -1446,7 +1445,7 @@ func (r *OctaviaReconciler) generateServiceSecrets(
14461445

14471446
// We only need a minimal 00-config.conf that is only used by db-sync job,
14481447
// hence only passing the database related parameters
1449-
templateParameters := map[string]interface{}{
1448+
templateParameters := map[string]any{
14501449
"MinimalConfig": true, // This tells the template to generate a minimal config
14511450
"DatabaseConnection": fmt.Sprintf("mysql+pymysql://%s:%s@%s/%s?read_default_file=/etc/my.cnf",
14521451
databaseAccount.Spec.UserName,

controllers/octaviaapi_controller.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ package controllers
2020
import (
2121
"context"
2222
"fmt"
23+
"maps"
2324
"time"
2425

2526
"github.com/go-logr/logr"
@@ -1016,9 +1017,7 @@ func (r *OctaviaAPIReconciler) generateServiceSecrets(
10161017
common.CustomServiceConfigFileName: instance.Spec.CustomServiceConfig,
10171018
"my.cnf": db.GetDatabaseClientConfig(tlsCfg), //(mschuppert) for now just get the default my.cnf
10181019
}
1019-
for key, data := range instance.Spec.DefaultConfigOverwrite {
1020-
customData[key] = data
1021-
}
1020+
maps.Copy(customData, instance.Spec.DefaultConfigOverwrite)
10221021

10231022
keystoneAPI, err := keystonev1.GetKeystoneAPI(ctx, h, instance.Namespace, map[string]string{})
10241023
if err != nil {
@@ -1073,7 +1072,7 @@ func (r *OctaviaAPIReconciler) generateServiceSecrets(
10731072
mariadbv1.MariaDBAccountReadyCondition,
10741073
mariadbv1.MariaDBAccountReadyMessage)
10751074

1076-
templateParameters := map[string]interface{}{
1075+
templateParameters := map[string]any{
10771076
"DatabaseConnection": fmt.Sprintf("mysql+pymysql://%s:%s@%s/%s?read_default_file=/etc/my.cnf",
10781077
databaseAccount.Spec.UserName,
10791078
string(dbSecret.Data[mariadbv1.DatabasePasswordSelector]),
@@ -1111,9 +1110,9 @@ func (r *OctaviaAPIReconciler) generateServiceSecrets(
11111110
templateParameters["TimeOut"] = instance.Spec.APITimeout
11121111

11131112
// create httpd vhost template parameters
1114-
httpdVhostConfig := map[string]interface{}{}
1113+
httpdVhostConfig := map[string]any{}
11151114
for _, endpt := range []service.Endpoint{service.EndpointInternal, service.EndpointPublic} {
1116-
endptConfig := map[string]interface{}{}
1115+
endptConfig := map[string]any{}
11171116
endptConfig["ServerName"] = fmt.Sprintf("%s-%s.%s.svc", octavia.ServiceName, endpt.String(), instance.Namespace)
11181117
endptConfig["TLS"] = false // default TLS to false, and set it bellow to true if enabled
11191118
if instance.Spec.TLS.API.Enabled(endpt) {

controllers/octaviarsyslog_controller.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package controllers
1919
import (
2020
"context"
2121
"fmt"
22+
"maps"
2223
"time"
2324

2425
"github.com/go-logr/logr"
@@ -428,11 +429,9 @@ func (r *OctaviaRsyslogReconciler) generateServiceSecrets(
428429
cmLabels := labels.GetLabels(instance, labels.GetGroupLabel(instance.Name), map[string]string{})
429430

430431
customData := map[string]string{}
431-
for key, data := range instance.Spec.DefaultConfigOverwrite {
432-
customData[key] = data
433-
}
432+
maps.Copy(customData, instance.Spec.DefaultConfigOverwrite)
434433

435-
templateParameters := map[string]interface{}{}
434+
templateParameters := map[string]any{}
436435
templateParameters["AdminLogTargets"] = instance.Spec.AdminLogTargets
437436
templateParameters["TenantLogTargets"] = instance.Spec.TenantLogTargets
438437

pkg/octavia/amphora_certs.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ func EnsureAmphoraCerts(
224224
"client_ca.cert.pem": clientCACert,
225225
// Unencrypted client key and cert
226226
"client.cert-and-key.pem": clientKeyAndCert,
227-
"version": []byte(fmt.Sprintf("%d", OctaviaCertSecretVersion)),
227+
"version": fmt.Appendf(nil, "%d", OctaviaCertSecretVersion),
228228
},
229229
}
230230

pkg/octavia/lb_mgmt_network.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1123,7 +1123,7 @@ func GetPredictableIPAM(networkParameters *NetworkParameters) (*NADIpam, error)
11231123
predParams.CIDR = networkParameters.ProviderCIDR
11241124
predParams.RangeStart = networkParameters.ProviderAllocationEnd.Next()
11251125
endRange := predParams.RangeStart
1126-
for i := 0; i < LbProvPredictablePoolSize; i++ {
1126+
for range LbProvPredictablePoolSize {
11271127
if !predParams.CIDR.Contains(endRange) {
11281128
return nil, fmt.Errorf("%w: %d in %s", ErrPredictableIPAllocation, LbProvPredictablePoolSize, predParams.CIDR)
11291129
}

pkg/octavia/network_parameters.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ func GetRangeFromCIDR(
5959
) (start netip.Addr, end netip.Addr) {
6060
// start is the 5th address of the Cidr
6161
start = cidr.Masked().Addr()
62-
for i := 0; i < 5; i++ {
62+
for range 5 {
6363
start = start.Next()
6464
}
6565

@@ -108,7 +108,7 @@ func GetNetworkParametersFromNAD(
108108
// RangeEnd.
109109
networkParameters.ProviderAllocationStart = nadConfig.IPAM.RangeEnd.Next()
110110
end := networkParameters.ProviderAllocationStart
111-
for i := 0; i < LbProvSubnetPoolSize; i++ {
111+
for range LbProvSubnetPoolSize {
112112
if !networkParameters.ProviderCIDR.Contains(end) {
113113
return nil, fmt.Errorf("%w: %d in %s", ErrCannotAllocateIPAddresses, LbProvSubnetPoolSize, networkParameters.ProviderCIDR)
114114
}

tests/functional/base_test.go

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ func CreateTransportURLSecret(name types.NamespacedName) *corev1.Secret {
7171
secret := th.CreateSecret(
7272
name,
7373
map[string][]byte{
74-
"transport_url": []byte(fmt.Sprintf("rabbit://%s/", name)),
74+
"transport_url": fmt.Appendf(nil, "rabbit://%s/", name),
7575
},
7676
)
7777
logger.Info("Created TransportURLSecret", "secret", secret)
@@ -107,14 +107,14 @@ func SimulateKeystoneReady(
107107
}, timeout, interval).Should(Succeed())
108108
}
109109

110-
func GetDefaultOctaviaSpec() map[string]interface{} {
111-
return map[string]interface{}{
110+
func GetDefaultOctaviaSpec() map[string]any {
111+
return map[string]any{
112112
"databaseInstance": "test-octavia-db-instance",
113113
"secret": SecretName,
114114
"octaviaNetworkAttachment": "octavia-attachement",
115115
"databaseAccount": "octavia-db-account",
116116
"persistenceDatabaseAccount": "octavia-persistence-db-account",
117-
"lbMgmtNetwork": map[string]interface{}{
117+
"lbMgmtNetwork": map[string]any{
118118
"availabilityZones": []string{"az0"},
119119
// It seems that the functional tests don't use the correct default
120120
// values for nested structures, when the default is defined in the
@@ -125,12 +125,12 @@ func GetDefaultOctaviaSpec() map[string]interface{} {
125125
}
126126
}
127127

128-
func CreateOctavia(name types.NamespacedName, spec map[string]interface{}) client.Object {
128+
func CreateOctavia(name types.NamespacedName, spec map[string]any) client.Object {
129129

130-
raw := map[string]interface{}{
130+
raw := map[string]any{
131131
"apiVersion": "octavia.openstack.org/v1beta1",
132132
"kind": "Octavia",
133-
"metadata": map[string]interface{}{
133+
"metadata": map[string]any{
134134
"name": name.Name,
135135
"namespace": name.Namespace,
136136
},
@@ -198,8 +198,8 @@ func SimulateOctaviaCertsSecret(namespace string, name string) *corev1.Secret {
198198
}
199199

200200
// OctaviaAPI
201-
func GetDefaultOctaviaAPISpec() map[string]interface{} {
202-
return map[string]interface{}{
201+
func GetDefaultOctaviaAPISpec() map[string]any {
202+
return map[string]any{
203203
"databaseHostname": "hostname-for-octavia-api",
204204
"databaseInstance": "test-octavia-db-instance",
205205
"secret": SecretName,
@@ -208,11 +208,11 @@ func GetDefaultOctaviaAPISpec() map[string]interface{} {
208208
}
209209
}
210210

211-
func CreateOctaviaAPI(name types.NamespacedName, spec map[string]interface{}) client.Object {
212-
raw := map[string]interface{}{
211+
func CreateOctaviaAPI(name types.NamespacedName, spec map[string]any) client.Object {
212+
raw := map[string]any{
213213
"apiVersion": "octavia.openstack.org/v1beta1",
214214
"kind": "OctaviaAPI",
215-
"metadata": map[string]interface{}{
215+
"metadata": map[string]any{
216216
"name": name.Name,
217217
"namespace": name.Namespace,
218218
},
@@ -244,14 +244,14 @@ func SimulateOctaviaAPIReady(name types.NamespacedName) {
244244
}
245245

246246
func CreateNAD(name types.NamespacedName) client.Object {
247-
raw := map[string]interface{}{
247+
raw := map[string]any{
248248
"apiVersion": "k8s.cni.cncf.io/v1",
249249
"kind": "NetworkAttachmentDefinition",
250-
"metadata": map[string]interface{}{
250+
"metadata": map[string]any{
251251
"name": name.Name,
252252
"namespace": name.Namespace,
253253
},
254-
"spec": map[string]interface{}{
254+
"spec": map[string]any{
255255
"config": `{
256256
"cniVersion": "0.3.1",
257257
"name": "octavia",
@@ -290,14 +290,14 @@ func GetNADConfig(name types.NamespacedName) *octavia.NADConfig {
290290
}
291291

292292
func CreateNode(name types.NamespacedName) client.Object {
293-
raw := map[string]interface{}{
293+
raw := map[string]any{
294294
"apiVersion": "v1",
295295
"kind": "Node",
296-
"metadata": map[string]interface{}{
296+
"metadata": map[string]any{
297297
"name": name.Name,
298298
"namespace": name.Namespace,
299299
},
300-
"spec": map[string]interface{}{},
300+
"spec": map[string]any{},
301301
}
302302
return th.CreateUnstructured(raw)
303303

@@ -307,7 +307,7 @@ func CreateSSHPubKey() client.Object {
307307
return th.CreateConfigMap(types.NamespacedName{
308308
Name: "octavia-ssh-pubkey",
309309
Namespace: namespace,
310-
}, map[string]interface{}{
310+
}, map[string]any{
311311
"key": "public key",
312312
})
313313
}

tests/functional/octavia_controller_test.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ func createAndSimulateTransportURL(
7474
infra.SimulateTransportURLReady(transportURLName)
7575
}
7676

77-
func createAndSimulateDB(spec map[string]interface{}) {
77+
func createAndSimulateDB(spec map[string]any) {
7878
DeferCleanup(
7979
mariadb.DeleteDBService,
8080
mariadb.CreateDBService(
@@ -112,7 +112,7 @@ func createAndSimulateOctaviaAPI(octaviaName types.NamespacedName) {
112112

113113
var _ = Describe("Octavia controller", func() {
114114
var name string
115-
var spec map[string]interface{}
115+
var spec map[string]any
116116
var octaviaName types.NamespacedName
117117
var transportURLName types.NamespacedName
118118
var transportURLSecretName types.NamespacedName
@@ -705,11 +705,11 @@ var _ = Describe("Octavia controller", func() {
705705
Name: "node1",
706706
}))
707707

708-
spec["lbMgmtNetwork"].(map[string]interface{})["availabilityZoneCIDRs"] = map[string]string{
708+
spec["lbMgmtNetwork"].(map[string]any)["availabilityZoneCIDRs"] = map[string]string{
709709
"az1": "172.34.0.0/16",
710710
"az2": "172.44.8.0/21",
711711
}
712-
spec["lbMgmtNetwork"].(map[string]interface{})["createDefaultLbMgmtNetwork"] = false
712+
spec["lbMgmtNetwork"].(map[string]any)["createDefaultLbMgmtNetwork"] = false
713713
DeferCleanup(th.DeleteInstance, CreateOctavia(octaviaName, spec))
714714

715715
th.SimulateJobSuccess(octaviaDBSyncName)
@@ -886,7 +886,7 @@ var _ = Describe("Octavia controller", func() {
886886

887887
When("Octavia is created with nodeSelector", func() {
888888
BeforeEach(func() {
889-
spec["nodeSelector"] = map[string]interface{}{
889+
spec["nodeSelector"] = map[string]any{
890890
"foo": "bar",
891891
}
892892

@@ -1088,24 +1088,24 @@ var _ = Describe("Octavia controller", func() {
10881088

10891089
// API, Worker and KeystoneListener
10901090
if component != "top-level" {
1091-
spec[component] = map[string]interface{}{
1092-
"topologyRef": map[string]interface{}{
1091+
spec[component] = map[string]any{
1092+
"topologyRef": map[string]any{
10931093
"name": "bar",
10941094
"namespace": "foo",
10951095
},
10961096
}
10971097
// top-level topologyRef
10981098
} else {
1099-
spec["topologyRef"] = map[string]interface{}{
1099+
spec["topologyRef"] = map[string]any{
11001100
"name": "bar",
11011101
"namespace": "foo",
11021102
}
11031103
}
11041104
// Build the octavia CR
1105-
raw := map[string]interface{}{
1105+
raw := map[string]any{
11061106
"apiVersion": "octavia.openstack.org/v1beta1",
11071107
"kind": "Octavia",
1108-
"metadata": map[string]interface{}{
1108+
"metadata": map[string]any{
11091109
"name": octaviaName.Name,
11101110
"namespace": octaviaName.Namespace,
11111111
},

0 commit comments

Comments
 (0)