Skip to content

Commit

Permalink
Merge pull request #1 from syself/feature/baremetal
Browse files Browse the repository at this point in the history
Add support for Hetzner dedicated servers
  • Loading branch information
janiskemper authored Mar 24, 2022
2 parents f7d624e + 3d02121 commit 1adee43
Show file tree
Hide file tree
Showing 15 changed files with 1,018 additions and 134 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ require (
github.com/hetznercloud/hcloud-go v1.33.0
github.com/spf13/pflag v1.0.5
github.com/stretchr/testify v1.7.0
github.com/syself/hrobot-go v0.2.4
golang.org/x/crypto v0.0.0-20210220033148-5ea612d1eb83
k8s.io/api v0.20.13
k8s.io/apimachinery v0.20.13
Expand Down
8 changes: 6 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -260,8 +260,9 @@ github.com/konsorten/go-windows-terminal-sequences v1.0.3 h1:CE8S1cTafDpPvMhIxNJ
github.com/konsorten/go-windows-terminal-sequences v1.0.3/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
github.com/kr/logfmt v0.0.0-20140226030751-b84e30acd515/go.mod h1:+0opPa2QZZtGFBFZlji/RkVcI2GknAs/DXo4wKdlNEc=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pretty v0.2.0 h1:s5hAObm+yFO5uHYt5dYjxi2rXrsnmRpJx4OYvIWUaQs=
github.com/kr/pretty v0.2.0/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
github.com/kr/pretty v0.2.1 h1:Fmg33tUaq4/8ym9TJN1x7sLJnHVwhP33CNkpYV/7rwI=
github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/pty v1.1.5/go.mod h1:9r2w37qlBe7rQ6e1fg1S/9xpWHSnaqNdHD3WcMdbPDA=
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
Expand Down Expand Up @@ -386,6 +387,8 @@ github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/subosito/gotenv v1.2.0/go.mod h1:N0PQaV/YGNqwC0u51sEeR/aUtSLEXKX9iv69rRypqCw=
github.com/syself/hrobot-go v0.2.4 h1:/XjtE7UjXPfp4NCM3NNjfKrex0biKBUVgHRNPp7kmXg=
github.com/syself/hrobot-go v0.2.4/go.mod h1:Oy47yZs+fJKcSh38S3OiNJdY34MXb0pkk796UnpYBnc=
github.com/tmc/grpc-websocket-proxy v0.0.0-20170815181823-89b8d40f7ca8/go.mod h1:ncp9v5uamzpCO7NfCPTXjqaC+bZgJeR0sMTm6dMHP7U=
github.com/tmc/grpc-websocket-proxy v0.0.0-20190109142713-0ad062ec5ee5 h1:LnC5Kc/wtumK+WB441p7ynQJzVuNRJiqddSIE3IlSEQ=
github.com/tmc/grpc-websocket-proxy v0.0.0-20190109142713-0ad062ec5ee5/go.mod h1:ncp9v5uamzpCO7NfCPTXjqaC+bZgJeR0sMTm6dMHP7U=
Expand Down Expand Up @@ -652,8 +655,9 @@ google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp0
gopkg.in/alecthomas/kingpin.v2 v2.2.6/go.mod h1:FMv+mEhP44yOT+4EoQTLFTRgOQ1FBLkstjWtayDeSgw=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 h1:YR8cESwS4TdDjEe65xsg0ogRM/Nc3DYOhEAlW+xobZo=
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
gopkg.in/cheggaaa/pb.v1 v1.0.25/go.mod h1:V/YB90LKu/1FcN3WVnfiiE5oMCibMjukxqG/qStrOgw=
gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI=
gopkg.in/fsnotify.v1 v1.4.7 h1:xOHLXZwVvI9hhs+cLKq5+I5onOuwQLhQwiu63xxlHs4=
Expand Down
27 changes: 25 additions & 2 deletions hcloud/cloud.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"github.com/hetznercloud/hcloud-cloud-controller-manager/internal/hcops"
"github.com/hetznercloud/hcloud-go/hcloud"
"github.com/hetznercloud/hcloud-go/hcloud/metadata"
hrobot "github.com/syself/hrobot-go"
cloudprovider "k8s.io/cloud-provider"
"k8s.io/klog/v2"
)
Expand All @@ -37,6 +38,10 @@ const (
hcloudEndpointENVVar = "HCLOUD_ENDPOINT"
hcloudNetworkENVVar = "HCLOUD_NETWORK"
hcloudDebugENVVar = "HCLOUD_DEBUG"

robotUserNameENVVar = "ROBOT_USER_NAME"
robotPasswordENVVar = "ROBOT_PASSWORD"

// Disable the "master/server is attached to the network" check against the metadata service.
hcloudNetworkDisableAttachedCheckENVVar = "HCLOUD_NETWORK_DISABLE_ATTACHED_CHECK"
hcloudInstancesAddressFamily = "HCLOUD_INSTANCES_ADDRESS_FAMILY"
Expand All @@ -48,11 +53,19 @@ const (
hcloudLoadBalancersDisableIPv6 = "HCLOUD_LOAD_BALANCERS_DISABLE_IPV6"
nodeNameENVVar = "NODE_NAME"
providerName = "hcloud"
providerNameRobot = "hetzner"
providerVersion = "v1.9.1"
hostNamePrefixHCloud = "hcloud//"
hostNamePrefixRobot = "robot//"
)

var (
errMissingRobotCredentials = errors.New("missing robot credentials - cannot connect to robot API")
)

type cloud struct {
client *hcloud.Client
robotClient hrobot.RobotClient
instances *instances
zones *zones
routes *routes
Expand Down Expand Up @@ -88,6 +101,14 @@ func newCloud(config io.Reader) (cloudprovider.Interface, error) {
client := hcloud.NewClient(opts...)
metadataClient := metadata.NewClient()

robotUserName, foundRobotUserName := os.LookupEnv(robotUserNameENVVar)
robotPassword, foundRobotPassword := os.LookupEnv(robotPasswordENVVar)

var robotClient hrobot.RobotClient
if foundRobotUserName && foundRobotPassword {
robotClient = hrobot.NewBasicAuthClient(robotUserName, robotPassword)
}

var networkID int
if v, ok := os.LookupEnv(hcloudNetworkENVVar); ok {
n, _, err := client.Network.Get(context.Background(), v)
Expand Down Expand Up @@ -134,6 +155,7 @@ func newCloud(config io.Reader) (cloudprovider.Interface, error) {
CertOps: &hcops.CertificateOps{CertClient: &client.Certificate},
ActionClient: &client.Action,
NetworkClient: &client.Network,
RobotClient: robotClient,
NetworkID: networkID,
Defaults: lbOpsDefaults,
}
Expand All @@ -150,8 +172,9 @@ func newCloud(config io.Reader) (cloudprovider.Interface, error) {

return &cloud{
client: client,
zones: newZones(client, nodeName),
instances: newInstances(client, instancesAddressFamily),
robotClient: robotClient,
zones: newZones(client, robotClient, nodeName),
instances: newInstances(client, robotClient, instancesAddressFamily),
loadBalancer: loadBalancers,
routes: nil,
networkID: networkID,
Expand Down
20 changes: 14 additions & 6 deletions hcloud/cloud_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,19 +29,22 @@ import (
"github.com/hetznercloud/hcloud-go/hcloud"
"github.com/hetznercloud/hcloud-go/hcloud/schema"
"github.com/stretchr/testify/assert"
hrobot "github.com/syself/hrobot-go"
)

type testEnv struct {
Server *httptest.Server
Mux *http.ServeMux
Client *hcloud.Client
Server *httptest.Server
Mux *http.ServeMux
Client *hcloud.Client
RobotClient hrobot.RobotClient
}

func (env *testEnv) Teardown() {
env.Server.Close()
env.Server = nil
env.Mux = nil
env.Client = nil
env.RobotClient = nil
}

func newTestEnv() testEnv {
Expand All @@ -53,10 +56,13 @@ func newTestEnv() testEnv {
hcloud.WithBackoffFunc(func(_ int) time.Duration { return 0 }),
hcloud.WithDebugWriter(os.Stdout),
)
robotClient := hrobot.NewBasicAuthClient("", "")
robotClient.SetBaseURL(server.URL + "/robot")
return testEnv{
Server: server,
Mux: mux,
Client: client,
Server: server,
Mux: mux,
Client: client,
RobotClient: robotClient,
}
}

Expand Down Expand Up @@ -143,6 +149,8 @@ func TestCloud(t *testing.T) {
"HCLOUD_ENDPOINT", env.Server.URL,
"HCLOUD_TOKEN", "jr5g7ZHpPptyhJzZyHw2Pqu4g9gTqDvEceYpngPf79jN_NOT_VALID_dzhepnahq",
"NODE_NAME", "test",
"ROBOT_USER_NAME", "user",
"ROBOT_PASSWORD", "pass123",
)
defer resetEnv()
env.Mux.HandleFunc("/servers", func(w http.ResponseWriter, r *http.Request) {
Expand Down
Loading

0 comments on commit 1adee43

Please sign in to comment.