Repository: hetznercloud/hcloud-cloud-controller-manager
Pull request #794 (2024-11-19 08:01:48)

refactor: simplified hccm init code
Removed the `cloudprovider.InitCloudProvider` initialization process to simplify the codebase and enhance maintainability.

codecov[bot]: ## [Codecov](https://app.codecov.io/gh/hetznercloud/hcloud-cloud-controller-manager/pull/794?dropdown=coverage&src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=hetznercloud) Report
Attention: Patch coverage is `50.00000%` with `1 line` in your changes missing coverage. Please review.
> Project coverage is 70.45%. Comparing base [(`27dafac`)](https://app.codecov.io/gh/hetznercloud/hcloud-cloud-controller-manager/commit/27dafac54ae230dfe8ce43b6799b79dafedccc20?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=hetznercloud) to head [(`78d9dbf`)](https://app.codecov.io/gh/hetznercloud/hcloud-cloud-controller-manager/commit/78d9dbff858af20f58cd371add66a6b81b2b8a1d?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=hetznercloud).

| [Files with missing lines](https://app.codecov.io/gh/hetznercloud/hcloud-cloud-controller-manager/pull/794?dropdown=coverage&src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=hetznercloud) | Patch % | Lines |
|---|---|---|
| [main.go](https://app.codecov.io/gh/hetznercloud/hcloud-cloud-controller-manager/pull/794?src=pr&el=tree&filepath=main.go&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=hetznercloud#diff-bWFpbi5nbw==) | 0.00% | [1 Missing :warning: ](https://app.codecov.io/gh/hetznercloud/hcloud-cloud-controller-manager/pull/794?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=hetznercloud) |

<details><summary>Additional details and impacted files</summary>


```diff
@@            Coverage Diff             @@
##             main     #794      +/-   ##
==========================================
+ Coverage   70.41%   70.45%   +0.04%     
==========================================
  Files          31       31              
  Lines        3231     3226       -5     
==========================================
- Hits         2275     2273       -2     
+ Misses        782      779       -3     
  Partials      174      174              
```

</details>

[:umbrella: View full report in Codecov by Sentry](https://app.codecov.io/gh/hetznercloud/hcloud-cloud-controller-manager/pull/794?dropdown=coverage&src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=hetznercloud).   
:loudspeaker: Have feedback on the report? [Share it here](https://about.codecov.io/codecov-pr-comment-feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=hetznercloud).

----
🚨 Try these New Features:

- [Flaky Tests Detection](https://docs.codecov.com/docs/test-result-ingestion-beta) - Detect and resolve failed and flaky tests

Patch:
diff --git a/.golangci.yaml b/.golangci.yaml
index 3cbd3b0d..e5d7cd6a 100644
--- a/.golangci.yaml
+++ b/.golangci.yaml
@@ -29,10 +29,10 @@ linters:
   disable-all: true
   enable:
     - bodyclose
+    - copyloopvar
     - errcheck
     - errname
     - exhaustive
-    - exportloopref
     - gci
     - gocritic
     - godot
diff --git a/hcloud/cloud.go b/hcloud/cloud.go
index 4fc18f20..0de19b5b 100644
--- a/hcloud/cloud.go
+++ b/hcloud/cloud.go
@@ -19,7 +19,6 @@ package hcloud
 import (
 	"context"
 	"fmt"
-	"io"
 	"os"
 	"strings"
 
@@ -54,7 +53,7 @@ type cloud struct {
 	networkID   int64
 }
 
-func newCloud(_ io.Reader) (cloudprovider.Interface, error) {
+func NewCloud() (cloudprovider.Interface, error) {
 	const op = "hcloud/newCloud"
 	metrics.OperationCalled.WithLabelValues(op).Inc()
 
@@ -222,7 +221,3 @@ func serverIsAttachedToNetwork(metadataClient *metadata.Client, networkID int64)
 	}
 	return strings.Contains(serverPrivateNetworks, fmt.Sprintf("network_id: %d\n", networkID)), nil
 }
-
-func init() {
-	cloudprovider.RegisterCloudProvider(providerName, newCloud)
-}
diff --git a/main.go b/main.go
index fdca1f79..30578593 100644
--- a/main.go
+++ b/main.go
@@ -36,7 +36,7 @@ import (
 	_ "k8s.io/component-base/metrics/prometheus/version"
 	"k8s.io/klog/v2"
 
-	_ "github.com/hetznercloud/hcloud-cloud-controller-manager/hcloud"
+	hcloud "github.com/hetznercloud/hcloud-cloud-controller-manager/hcloud"
 )
 
 func main() {
@@ -59,10 +59,7 @@ func main() {
 }
 
 func cloudInitializer(config *config.CompletedConfig) cloudprovider.Interface {
-	cloudConfig := config.ComponentConfig.KubeCloudShared.CloudProvider
-
-	// initialize cloud provider with the cloud provider name and config file provided
-	cloud, err := cloudprovider.InitCloudProvider(cloudConfig.Name, cloudConfig.CloudConfigFile)
+	cloud, err := hcloud.NewCloud()
 	if err != nil {
 		klog.Fatalf("Cloud provider could not be initialized: %v", err)
 	}


Test patch:
diff --git a/hcloud/cloud_test.go b/hcloud/cloud_test.go
index b7bca712..7b2af7c5 100644
--- a/hcloud/cloud_test.go
+++ b/hcloud/cloud_test.go
@@ -17,7 +17,6 @@ limitations under the License.
 package hcloud
 
 import (
-	"bytes"
 	"encoding/json"
 	"net/http"
 	"net/http/httptest"
@@ -92,8 +91,8 @@ func TestNewCloud(t *testing.T) {
 			},
 		)
 	})
-	var config bytes.Buffer
-	_, err := newCloud(&config)
+
+	_, err := NewCloud()
 	assert.NoError(t, err)
 }
 
@@ -105,7 +104,7 @@ func TestNewCloudConnectionNotPossible(t *testing.T) {
 	)
 	defer resetEnv()
 
-	_, err := newCloud(&bytes.Buffer{})
+	_, err := NewCloud()
 	assert.EqualError(t, err,
 		`hcloud/newCloud: Get "http://127.0.0.1:4711/v1/servers?": dial tcp 127.0.0.1:4711: connect: connection refused`)
 }
@@ -133,7 +132,7 @@ func TestNewCloudInvalidToken(t *testing.T) {
 		)
 	})
 
-	_, err := newCloud(&bytes.Buffer{})
+	_, err := NewCloud()
 	assert.EqualError(t, err, "hcloud/newCloud: unable to authenticate (unauthorized)")
 }
 
@@ -196,7 +195,7 @@ func TestCloud(t *testing.T) {
 		)
 	})
 
-	cloud, err := newCloud(&bytes.Buffer{})
+	cloud, err := NewCloud()
 	if err != nil {
 		t.Fatalf("Unexpected error: %v", err)
 	}
@@ -253,7 +252,7 @@ func TestCloud(t *testing.T) {
 		)
 		defer resetEnv()
 
-		c, err := newCloud(&bytes.Buffer{})
+		c, err := NewCloud()
 		if err != nil {
 			t.Errorf("%s", err)
 		}
diff --git a/hcloud/load_balancers_test.go b/hcloud/load_balancers_test.go
index 1c862928..749211a2 100644
--- a/hcloud/load_balancers_test.go
+++ b/hcloud/load_balancers_test.go
@@ -948,7 +948,6 @@ func TestLoadBalancer_matchNodeSelector(t *testing.T) {
 	}
 
 	for _, c := range cases {
-		c := c // prevent scopelint from complaining
 		t.Run(c.name, func(t *testing.T) {
 			nodes, err := matchNodeSelector(c.service, c.k8sNodes)
 			if err != nil {
diff --git a/internal/annotation/load_balancer_test.go b/internal/annotation/load_balancer_test.go
index d8ee624a..292e2713 100644
--- a/internal/annotation/load_balancer_test.go
+++ b/internal/annotation/load_balancer_test.go
@@ -159,7 +159,6 @@ func TestLBToService_AddAnnotations(t *testing.T) {
 	}
 
 	for _, tt := range tests {
-		tt := tt
 		t.Run(tt.name, func(t *testing.T) {
 			err := annotation.LBToService(&tt.svc, &tt.lb)
 			assert.NoError(t, err)
diff --git a/internal/annotation/name_test.go b/internal/annotation/name_test.go
index 2d57e28d..b6cc27a9 100644
--- a/internal/annotation/name_test.go
+++ b/internal/annotation/name_test.go
@@ -88,7 +88,6 @@ func TestName_AddToService(t *testing.T) {
 	}
 
 	for _, tt := range tests {
-		tt := tt
 		t.Run(tt.name, func(t *testing.T) {
 			err := ann.AnnotateService(&tt.svc, tt.value)
 			if tt.err != nil {
@@ -123,7 +122,6 @@ func TestName_StringFromService(t *testing.T) {
 	}
 
 	for _, tt := range tests {
-		tt := tt
 		t.Run(tt.name, func(t *testing.T) {
 			var svc corev1.Service
 
@@ -478,7 +476,6 @@ func runAllTypedAccessorTests(
 	t.Helper()
 
 	for _, tt := range tests {
-		tt := tt
 		t.Run(tt.name, func(t *testing.T) {
 			tt.run(t, call)
 		})
diff --git a/internal/hcops/certificates_test.go b/internal/hcops/certificates_test.go
index e4d693c7..5e7c2a30 100644
--- a/internal/hcops/certificates_test.go
+++ b/internal/hcops/certificates_test.go
@@ -227,7 +227,6 @@ func (tt *certificateOpsTestCase) run(t *testing.T) {
 
 func runCertificateOpsTestCases(t *testing.T, tests []certificateOpsTestCase) {
 	for _, tt := range tests {
-		tt := tt
 		t.Run(tt.Name, tt.run)
 	}
 }
diff --git a/internal/hcops/load_balancer_internal_test.go b/internal/hcops/load_balancer_internal_test.go
index 6c024b8c..e1fa4c33 100644
--- a/internal/hcops/load_balancer_internal_test.go
+++ b/internal/hcops/load_balancer_internal_test.go
@@ -363,7 +363,6 @@ func TestHCLBServiceOptsBuilder(t *testing.T) {
 	}
 
 	for _, tt := range tests {
-		tt := tt
 		t.Run(tt.name, func(t *testing.T) {
 			tt.certClient = &mocks.CertificateClient{}
 			tt.certClient.Test(t)
diff --git a/internal/hcops/load_balancer_test.go b/internal/hcops/load_balancer_test.go
index 8c735729..487d84a9 100644
--- a/internal/hcops/load_balancer_test.go
+++ b/internal/hcops/load_balancer_test.go
@@ -76,7 +76,6 @@ func TestLoadBalancerOps_GetByName(t *testing.T) {
 	}
 
 	for _, tt := range tests {
-		tt := tt
 		t.Run(tt.name, func(t *testing.T) {
 			fx := hcops.NewLoadBalancerOpsFixture(t)
 			if tt.mock != nil {
@@ -146,7 +145,6 @@ func TestLoadBalancerOps_GetByID(t *testing.T) {
 	}
 
 	for _, tt := range tests {
-		tt := tt
 		t.Run(tt.name, func(t *testing.T) {
 			fx := hcops.NewLoadBalancerOpsFixture(t)
 			if tt.mock != nil {
@@ -201,7 +199,6 @@ func TestGetByK8SServiceUID(t *testing.T) {
 	}
 
 	for _, tt := range tests {
-		tt := tt
 		t.Run(tt.name, func(t *testing.T) {
 			fx := hcops.NewLoadBalancerOpsFixture(t)
 
@@ -463,7 +460,6 @@ func TestLoadBalancerOps_Create(t *testing.T) {
 	}
 
 	for _, tt := range tests {
-		tt := tt
 		t.Run(tt.name, func(t *testing.T) {
 			fx := hcops.NewLoadBalancerOpsFixture(t)
 
@@ -525,7 +521,6 @@ func TestLoadBalancerOps_Delete(t *testing.T) {
 	}
 
 	for _, tt := range tests {
-		tt := tt
 		t.Run(tt.name, func(t *testing.T) {
 			fx := hcops.NewLoadBalancerOpsFixture(t)
 			ctx := context.Background()
@@ -1137,7 +1132,6 @@ func TestLoadBalancerOps_ReconcileHCLB(t *testing.T) {
 	}
 
 	for _, tt := range tests {
-		tt := tt
 		t.Run(tt.name, tt.run)
 	}
 }
@@ -1421,7 +1415,6 @@ func TestLoadBalancerOps_ReconcileHCLBTargets(t *testing.T) {
 	}
 
 	for _, tt := range tests {
-		tt := tt
 		t.Run(tt.name, tt.run)
 	}
 }
@@ -1700,7 +1693,6 @@ func TestLoadBalancerOps_ReconcileHCLBServices(t *testing.T) {
 	}
 
 	for _, tt := range tests {
-		tt := tt
 		t.Run(tt.name, tt.run)
 	}
 }