Skip to content

Commit

Permalink
refactor: simplified hccm init code (#794)
Browse files Browse the repository at this point in the history
Removed the `cloudprovider.InitCloudProvider` initialization process to
simplify the codebase and enhance maintainability.
  • Loading branch information
lukasmetzner authored Nov 19, 2024
1 parent b2b1ffd commit 674696f
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 18 deletions.
7 changes: 1 addition & 6 deletions hcloud/cloud.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ package hcloud
import (
"context"
"fmt"
"io"
"os"
"strings"

Expand Down Expand Up @@ -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()

Expand Down Expand Up @@ -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)
}
13 changes: 6 additions & 7 deletions hcloud/cloud_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ limitations under the License.
package hcloud

import (
"bytes"
"encoding/json"
"net/http"
"net/http/httptest"
Expand Down Expand Up @@ -92,8 +91,8 @@ func TestNewCloud(t *testing.T) {
},
)
})
var config bytes.Buffer
_, err := newCloud(&config)

_, err := NewCloud()
assert.NoError(t, err)
}

Expand All @@ -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`)
}
Expand Down Expand Up @@ -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)")
}

Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)
}
Expand Down
7 changes: 2 additions & 5 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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)
}
Expand Down

0 comments on commit 674696f

Please sign in to comment.