Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ hcloud-cloud-controller-manager
*.tgz
hack/.*
coverage/
.vscode
8 changes: 7 additions & 1 deletion hcloud/instances.go
Original file line number Diff line number Diff line change
Expand Up @@ -374,8 +374,14 @@ func (s robotServer) IsShutdown() (bool, error) {
}

func (s robotServer) Metadata(_ int64, node *corev1.Node, cfg config.HCCMConfiguration) (*cloudprovider.InstanceMetadata, error) {
var providerID string
if cfg.Robot.UseLegacyProviderID {
providerID = providerid.FromRobotLegacyServerNumber(s.ServerNumber)
} else {
providerID = providerid.FromRobotServerNumber(s.ServerNumber)
}
return &cloudprovider.InstanceMetadata{
ProviderID: providerid.FromRobotServerNumber(s.ServerNumber),
ProviderID: providerID,
InstanceType: getInstanceTypeOfRobotServer(s.Server),
NodeAddresses: robotNodeAddresses(s.Server, node, cfg, s.recorder),
Zone: getZoneOfRobotServer(s.Server),
Expand Down
49 changes: 49 additions & 0 deletions hcloud/instances_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -420,6 +420,55 @@ func TestInstances_InstanceMetadataRobotServer(t *testing.T) {
}
}

func TestInstances_InstanceMetadataRobotServer_UseLegacyProviderID(t *testing.T) {
env := newTestEnv()
defer env.Teardown()
env.Mux.HandleFunc("/robot/server/321", func(w http.ResponseWriter, _ *http.Request) {
json.NewEncoder(w).Encode(hrobotmodels.ServerResponse{
Server: hrobotmodels.Server{
ServerIP: "233.252.0.123",
ServerIPv6Net: "2a01:f48:111:4221::",
ServerNumber: 321,
Product: "Robot Server™ 1",
Name: "robot-server1",
Dc: "NBG1-DC1",
},
})
})

env.Cfg.Robot.UseLegacyProviderID = true

instances := newInstances(env.Client, env.RobotClient, env.Recorder, 0, env.Cfg)

metadata, err := instances.InstanceMetadata(context.TODO(), &corev1.Node{
ObjectMeta: metav1.ObjectMeta{
Name: "robot-server1",
},
Spec: corev1.NodeSpec{ProviderID: "hrobot://321"},
})
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}

expectedMetadata := &cloudprovider.InstanceMetadata{
ProviderID: "hcloud://bm-321",
InstanceType: "Robot-Server-1",
NodeAddresses: []corev1.NodeAddress{
{Type: corev1.NodeHostName, Address: "robot-server1"},
{Type: corev1.NodeExternalIP, Address: "233.252.0.123"},
},
Zone: "nbg1-dc1",
Region: "nbg1",
AdditionalLabels: map[string]string{
"instance.hetzner.cloud/provided-by": "robot",
},
}

if !reflect.DeepEqual(metadata, expectedMetadata) {
t.Fatalf("Expected metadata %+v but got %+v", *expectedMetadata, *metadata)
}
}

func TestNodeAddresses(t *testing.T) {
tests := []struct {
name string
Expand Down
21 changes: 13 additions & 8 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,13 @@ const (
hcloudNetwork = "HCLOUD_NETWORK"
hcloudDebug = "HCLOUD_DEBUG"

robotEnabled = "ROBOT_ENABLED"
robotUser = "ROBOT_USER"
robotPassword = "ROBOT_PASSWORD"
robotCacheTimeout = "ROBOT_CACHE_TIMEOUT"
robotRateLimitWaitTime = "ROBOT_RATE_LIMIT_WAIT_TIME"
robotForwardInternalIPs = "ROBOT_FORWARD_INTERNAL_IPS"
robotEnabled = "ROBOT_ENABLED"
robotUser = "ROBOT_USER"
robotPassword = "ROBOT_PASSWORD"
robotCacheTimeout = "ROBOT_CACHE_TIMEOUT"
robotRateLimitWaitTime = "ROBOT_RATE_LIMIT_WAIT_TIME"
robotForwardInternalIPs = "ROBOT_FORWARD_INTERNAL_IPS"
robotUseLegacyProviderID = "ROBOT_USE_LEGACY_PROVIDER_ID"

hcloudInstancesAddressFamily = "HCLOUD_INSTANCES_ADDRESS_FAMILY"

Expand All @@ -51,7 +52,8 @@ type RobotConfiguration struct {
CacheTimeout time.Duration
RateLimitWaitTime time.Duration
// ForwardInternalIPs is enabled by default.
ForwardInternalIPs bool
ForwardInternalIPs bool
UseLegacyProviderID bool
}

type MetricsConfiguration struct {
Expand Down Expand Up @@ -156,7 +158,10 @@ func Read() (HCCMConfiguration, error) {
}
// Robot needs to be enabled
cfg.Robot.ForwardInternalIPs = cfg.Robot.ForwardInternalIPs && cfg.Robot.Enabled

cfg.Robot.UseLegacyProviderID, err = getEnvBool(robotUseLegacyProviderID, false)
if err != nil {
errs = append(errs, err)
}
cfg.Metrics.Enabled, err = getEnvBool(hcloudMetricsEnabled, true)
if err != nil {
errs = append(errs, err)
Expand Down
5 changes: 5 additions & 0 deletions internal/providerid/providerid.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,3 +81,8 @@ func FromCloudServerID(serverID int64) string {
func FromRobotServerNumber(serverNumber int) string {
return fmt.Sprintf("%s%d", prefixRobot, serverNumber)
}

// FromRobotLegacyServerNumber generates the canonical ProviderID for a Robot Server.
func FromRobotLegacyServerNumber(serverNumber int) string {
return fmt.Sprintf("%s%d", prefixRobotLegacy, serverNumber)
}
Loading