Skip to content

Commit 37ef86d

Browse files
authored
Merge pull request #43 from GDATASoftwareAG/cleanup-code
cleanup code
2 parents 95038d1 + 43468e8 commit 37ef86d

File tree

7 files changed

+24
-28
lines changed

7 files changed

+24
-28
lines changed

main.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@ package main
22

33
import (
44
"fmt"
5-
"math/rand"
65
"os"
7-
"time"
86

97
"github.com/spf13/cobra"
108
"github.com/spf13/pflag"
@@ -29,16 +27,14 @@ const AppName string = "ionoscloud-cloud-controller-manager"
2927
var version string
3028

3129
func main() {
32-
rand.Seed(time.Now().UTC().UnixNano())
33-
3430
ccmOptions, err := options.NewCloudControllerManagerOptions()
3531
if err != nil {
3632
klog.Fatalf("unable to initialize command options: %v", err)
3733
}
3834

3935
command := &cobra.Command{
4036
Use: AppName,
41-
Long: fmt.Sprintf("%s manages vSphere cloud resources for a Kubernetes cluster.", AppName),
37+
Long: fmt.Sprintf("%s manages ionos cloud resources for a Kubernetes cluster.", AppName),
4238
Args: func(cmd *cobra.Command, args []string) error {
4339
for _, arg := range args {
4440
if len(arg) > 0 {

pkg/client/client.go

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -195,16 +195,15 @@ func (a *IONOSClient) GetServerByIP(ctx context.Context, loadBalancerIP string)
195195
}
196196

197197
for _, server := range *servers.Items {
198-
klog.Infof("Checking server %s", server.Properties.Name)
198+
klog.Infof("checking server %s and looking for loadbalancer ip %s", *server.Properties.Name, loadBalancerIP)
199199
if !server.Entities.HasNics() {
200200
continue
201201
}
202202
for _, nic := range *server.Entities.Nics.Items {
203203
if nic.Properties.HasIps() {
204204
for _, ip := range *nic.Properties.Ips {
205-
klog.Infof("Found ip %s", ip)
205+
klog.Infof("found ip %s", ip)
206206
if loadBalancerIP == ip {
207-
klog.Info("Its a match!")
208207
return &Server{
209208
Name: *server.Properties.Name,
210209
ProviderID: *server.Id,
@@ -248,14 +247,14 @@ func (a *IONOSClient) convertServerToInstanceMetadata(ctx context.Context, serve
248247
}
249248

250249
var addresses []v1.NodeAddress
251-
klog.Infof("Found %v nics", len(*server.Entities.Nics.Items))
250+
klog.Infof("found %v nics", len(*server.Entities.Nics.Items))
252251
for _, nic := range *server.Entities.Nics.Items {
253252
ips := *nic.Properties.Ips
254253
nicName := "unknown"
255254
if nic.Properties.Name != nil {
256255
nicName = *nic.Properties.Name
257256
}
258-
klog.Infof("Found %v ips for nic %s. Only using the first one as the remaining ones are failover ips", len(ips), nicName)
257+
klog.Infof("found %v ips for nic %s. Only using the first one as the remaining ones are failover ips", len(ips), nicName)
259258
if len(ips) > 0 {
260259
ipStr := ips[0]
261260
ip := net.ParseIP(ipStr)
@@ -282,8 +281,6 @@ func (a *IONOSClient) convertServerToInstanceMetadata(ctx context.Context, serve
282281
Zone: *server.Properties.AvailabilityZone,
283282
Region: strings.Replace(location, "/", "-", 1),
284283
}
285-
286-
klog.InfoDepth(1, metadata)
287284
return metadata, err
288285
}
289286

pkg/config/types.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ package config
33
const (
44
// RegisteredProviderName is the name of the cloud provider registered with
55
// Kubernetes.
6-
RegisteredProviderName string = "ionos"
7-
ProviderPrefix = "ionos://"
6+
RegisteredProviderName = "ionos"
7+
ProviderPrefix = "ionos://"
88
// ClientName is the user agent passed into the controller client builder.
9-
ClientName string = "ionoscloud-cloud-controller-manager"
9+
ClientName = "ionoscloud-cloud-controller-manager"
1010
)
1111

1212
type Config struct {

pkg/ionos/cloud.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import (
44
"context"
55
"encoding/json"
66
"io"
7+
"math/rand"
8+
"time"
79

810
client2 "github.com/GDATASoftwareAG/cloud-provider-ionoscloud/pkg/client"
911
"github.com/GDATASoftwareAG/cloud-provider-ionoscloud/pkg/config"
@@ -15,6 +17,7 @@ import (
1517

1618
func init() {
1719
cloudprovider.RegisterCloudProvider(config.RegisteredProviderName, func(cfg io.Reader) (cloudprovider.Interface, error) {
20+
r := rand.New(rand.NewSource(time.Now().UnixNano()))
1821
byConfig, err := io.ReadAll(cfg)
1922
if err != nil {
2023
klog.Errorf("ReadAll failed: %s", err)
@@ -26,19 +29,20 @@ func init() {
2629
return nil, err
2730
}
2831

29-
return newProvider(conf), nil
32+
return newProvider(conf, r), nil
3033
})
3134
}
3235

3336
var _ cloudprovider.Interface = &IONOS{}
3437

35-
func newProvider(config config.Config) cloudprovider.Interface {
38+
func newProvider(config config.Config, r *rand.Rand) cloudprovider.Interface {
3639
return IONOS{
3740
config: config,
3841
instances: instances{
3942
ionosClients: map[string]*client2.IONOSClient{},
4043
},
4144
loadbalancer: loadbalancer{
45+
r: r,
4246
ionosClients: map[string]*client2.IONOSClient{},
4347
},
4448
}

pkg/ionos/instances.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ func (i instances) discoverNode(ctx context.Context, node *v1.Node) (*cloudprovi
4848
server, err = client.GetServerByName(ctx, node.Name)
4949
}
5050
if err != nil {
51-
return nil, errors.New(fmt.Sprintf("failed to discoverNode %v", err))
51+
return nil, fmt.Errorf("failed to discoverNode %v", err)
5252
}
5353
if server == nil {
5454
continue

pkg/ionos/loadbalancer.go

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,7 @@ package ionos
33
import (
44
"context"
55
"errors"
6-
"math/rand"
76
"strings"
8-
"time"
97

108
v1 "k8s.io/api/core/v1"
119
cloudprovider "k8s.io/cloud-provider"
@@ -36,7 +34,7 @@ func (l loadbalancer) AddClient(datacenterId string, token []byte) error {
3634
// For the given LB service, the GetLoadBalancer must return "exists=True" if
3735
// there exists a LoadBalancer instance created by ServiceController.
3836
// In all other cases, GetLoadBalancer must return a NotFound error.
39-
func (l loadbalancer) GetLoadBalancer(ctx context.Context, clusterName string, service *v1.Service) (status *v1.LoadBalancerStatus, exists bool, err error) {
37+
func (l loadbalancer) GetLoadBalancer(ctx context.Context, _ string, service *v1.Service) (status *v1.LoadBalancerStatus, exists bool, err error) {
4038
klog.Infof("getLoadBalancer (service %s/%s)", service.Namespace, service.Name)
4139

4240
server, err := l.ServerWithLoadBalancer(ctx, service.Spec.LoadBalancerIP)
@@ -53,7 +51,7 @@ func (l loadbalancer) GetLoadBalancer(ctx context.Context, clusterName string, s
5351

5452
// GetLoadBalancerName returns the name of the load balancer. Implementations must treat the
5553
// *v1.Service parameter as read-only and not modify it.
56-
func (l loadbalancer) GetLoadBalancerName(ctx context.Context, clusterName string, service *v1.Service) string {
54+
func (l loadbalancer) GetLoadBalancerName(_ context.Context, _ string, service *v1.Service) string {
5755
return cloudprovider.DefaultLoadBalancerName(service)
5856
}
5957

@@ -90,7 +88,7 @@ func (l loadbalancer) UpdateLoadBalancer(ctx context.Context, clusterName string
9088
// Parameter 'clusterName' is the name of the cluster as presented to kube-controller-manager
9189
// EnsureLoadBalancerDeleted must not return ImplementedElsewhere to ensure
9290
// proper teardown of resources that were allocated by the ServiceController.
93-
func (l loadbalancer) EnsureLoadBalancerDeleted(ctx context.Context, clusterName string, service *v1.Service) error {
91+
func (l loadbalancer) EnsureLoadBalancerDeleted(ctx context.Context, _ string, service *v1.Service) error {
9492
klog.Infof("ensureLoadBalancerDeleted (service %s/%s)", service.Namespace, service.Name)
9593

9694
if len(service.Status.LoadBalancer.Ingress) > 0 {
@@ -130,7 +128,7 @@ func (l loadbalancer) deleteLoadBalancerFromNode(ctx context.Context, loadBalanc
130128
return nil
131129
}
132130

133-
func (l loadbalancer) syncLoadBalancer(ctx context.Context, clusterName string, service *v1.Service, nodes []*v1.Node) (*v1.LoadBalancerStatus, error) {
131+
func (l loadbalancer) syncLoadBalancer(ctx context.Context, _ string, service *v1.Service, nodes []*v1.Node) (*v1.LoadBalancerStatus, error) {
134132
klog.Infof("syncLoadBalancer (service %s/%s, nodes %s)", service.Namespace, service.Name, nodes)
135133

136134
if len(service.Status.LoadBalancer.Ingress) > 0 && service.Status.LoadBalancer.Ingress[0].IP != service.Spec.LoadBalancerIP {
@@ -192,7 +190,7 @@ func (l loadbalancer) syncLoadBalancer(ctx context.Context, clusterName string,
192190
}
193191
}
194192

195-
klog.Infof("Could not attach ip %s to any node", service.Spec.LoadBalancerIP)
193+
klog.Infof("could not attach ip %s to any node", service.Spec.LoadBalancerIP)
196194
return nil, nil
197195
}
198196

@@ -215,8 +213,7 @@ func (l loadbalancer) GetLoadBalancerNode(nodes []*v1.Node) *v1.Node {
215213
if candidates == nil && len(candidates) == 0 {
216214
return nil
217215
}
218-
rand.Seed(time.Now().UnixNano())
219-
randomIndex := rand.Intn(len(candidates))
216+
randomIndex := l.r.Intn(len(candidates))
220217
return candidates[randomIndex]
221218
}
222219

pkg/ionos/types.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package ionos
22

33
import (
4+
"math/rand"
5+
46
"github.com/GDATASoftwareAG/cloud-provider-ionoscloud/pkg/client"
57
"github.com/GDATASoftwareAG/cloud-provider-ionoscloud/pkg/config"
68
)
@@ -9,13 +11,13 @@ type IONOS struct {
911
config config.Config
1012
instances instances
1113
loadbalancer loadbalancer
12-
client *client.IONOSClient
1314
}
1415

1516
type instances struct {
1617
ionosClients map[string]*client.IONOSClient
1718
}
1819

1920
type loadbalancer struct {
21+
r *rand.Rand
2022
ionosClients map[string]*client.IONOSClient
2123
}

0 commit comments

Comments
 (0)