Skip to content

Commit 509c435

Browse files
committed
Add ROBOT_USE_LEGACY_PROVIDER_ID to use legacy provider ID
For some clusters it might be challenging to update the tool creating new nodes (like Cluster API Provider Hetzner) and the CCM so that both agree on the provider ID format. If the env var ROBOT_USE_LEGACY_PROVIDER_ID is set to a true value ("1", "t", "T", "true", "TRUE", "True"), then the CCM will use the legacy format ("hcloud://bm-NNNN") when returning InstanceMetadata. This applies only, when the node is created. Once a provider ID is set, it won't be changed again.
1 parent f66b1ce commit 509c435

5 files changed

Lines changed: 75 additions & 9 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,4 @@ hcloud-cloud-controller-manager
88
*.tgz
99
hack/.*
1010
coverage/
11+
.vscode

hcloud/instances.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -374,8 +374,14 @@ func (s robotServer) IsShutdown() (bool, error) {
374374
}
375375

376376
func (s robotServer) Metadata(_ int64, node *corev1.Node, cfg config.HCCMConfiguration) (*cloudprovider.InstanceMetadata, error) {
377+
var providerID string
378+
if cfg.Robot.UseLegacyProviderID {
379+
providerID = providerid.FromRobotLegacyServerNumber(s.ServerNumber)
380+
} else {
381+
providerID = providerid.FromRobotServerNumber(s.ServerNumber)
382+
}
377383
return &cloudprovider.InstanceMetadata{
378-
ProviderID: providerid.FromRobotServerNumber(s.ServerNumber),
384+
ProviderID: providerID,
379385
InstanceType: getInstanceTypeOfRobotServer(s.Server),
380386
NodeAddresses: robotNodeAddresses(s.Server, node, cfg, s.recorder),
381387
Zone: getZoneOfRobotServer(s.Server),

hcloud/instances_test.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -420,6 +420,55 @@ func TestInstances_InstanceMetadataRobotServer(t *testing.T) {
420420
}
421421
}
422422

423+
func TestInstances_InstanceMetadataRobotServer_UseLegacyProviderID(t *testing.T) {
424+
env := newTestEnv()
425+
defer env.Teardown()
426+
env.Mux.HandleFunc("/robot/server/321", func(w http.ResponseWriter, _ *http.Request) {
427+
json.NewEncoder(w).Encode(hrobotmodels.ServerResponse{
428+
Server: hrobotmodels.Server{
429+
ServerIP: "233.252.0.123",
430+
ServerIPv6Net: "2a01:f48:111:4221::",
431+
ServerNumber: 321,
432+
Product: "Robot Server™ 1",
433+
Name: "robot-server1",
434+
Dc: "NBG1-DC1",
435+
},
436+
})
437+
})
438+
439+
env.Cfg.Robot.UseLegacyProviderID = true
440+
441+
instances := newInstances(env.Client, env.RobotClient, env.Recorder, 0, env.Cfg)
442+
443+
metadata, err := instances.InstanceMetadata(context.TODO(), &corev1.Node{
444+
ObjectMeta: metav1.ObjectMeta{
445+
Name: "robot-server1",
446+
},
447+
Spec: corev1.NodeSpec{ProviderID: "hrobot://321"},
448+
})
449+
if err != nil {
450+
t.Fatalf("Unexpected error: %v", err)
451+
}
452+
453+
expectedMetadata := &cloudprovider.InstanceMetadata{
454+
ProviderID: "hcloud://bm-321",
455+
InstanceType: "Robot-Server-1",
456+
NodeAddresses: []corev1.NodeAddress{
457+
{Type: corev1.NodeHostName, Address: "robot-server1"},
458+
{Type: corev1.NodeExternalIP, Address: "233.252.0.123"},
459+
},
460+
Zone: "nbg1-dc1",
461+
Region: "nbg1",
462+
AdditionalLabels: map[string]string{
463+
"instance.hetzner.cloud/provided-by": "robot",
464+
},
465+
}
466+
467+
if !reflect.DeepEqual(metadata, expectedMetadata) {
468+
t.Fatalf("Expected metadata %+v but got %+v", *expectedMetadata, *metadata)
469+
}
470+
}
471+
423472
func TestNodeAddresses(t *testing.T) {
424473
tests := []struct {
425474
name string

internal/config/config.go

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,13 @@ const (
2121
hcloudNetwork = "HCLOUD_NETWORK"
2222
hcloudDebug = "HCLOUD_DEBUG"
2323

24-
robotEnabled = "ROBOT_ENABLED"
25-
robotUser = "ROBOT_USER"
26-
robotPassword = "ROBOT_PASSWORD"
27-
robotCacheTimeout = "ROBOT_CACHE_TIMEOUT"
28-
robotRateLimitWaitTime = "ROBOT_RATE_LIMIT_WAIT_TIME"
29-
robotForwardInternalIPs = "ROBOT_FORWARD_INTERNAL_IPS"
24+
robotEnabled = "ROBOT_ENABLED"
25+
robotUser = "ROBOT_USER"
26+
robotPassword = "ROBOT_PASSWORD"
27+
robotCacheTimeout = "ROBOT_CACHE_TIMEOUT"
28+
robotRateLimitWaitTime = "ROBOT_RATE_LIMIT_WAIT_TIME"
29+
robotForwardInternalIPs = "ROBOT_FORWARD_INTERNAL_IPS"
30+
robotUseLegacyProviderID = "ROBOT_USE_LEGACY_PROVIDER_ID"
3031

3132
hcloudInstancesAddressFamily = "HCLOUD_INSTANCES_ADDRESS_FAMILY"
3233

@@ -51,7 +52,8 @@ type RobotConfiguration struct {
5152
CacheTimeout time.Duration
5253
RateLimitWaitTime time.Duration
5354
// ForwardInternalIPs is enabled by default.
54-
ForwardInternalIPs bool
55+
ForwardInternalIPs bool
56+
UseLegacyProviderID bool
5557
}
5658

5759
type MetricsConfiguration struct {
@@ -156,7 +158,10 @@ func Read() (HCCMConfiguration, error) {
156158
}
157159
// Robot needs to be enabled
158160
cfg.Robot.ForwardInternalIPs = cfg.Robot.ForwardInternalIPs && cfg.Robot.Enabled
159-
161+
cfg.Robot.UseLegacyProviderID, err = getEnvBool(robotUseLegacyProviderID, false)
162+
if err != nil {
163+
errs = append(errs, err)
164+
}
160165
cfg.Metrics.Enabled, err = getEnvBool(hcloudMetricsEnabled, true)
161166
if err != nil {
162167
errs = append(errs, err)

internal/providerid/providerid.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,3 +81,8 @@ func FromCloudServerID(serverID int64) string {
8181
func FromRobotServerNumber(serverNumber int) string {
8282
return fmt.Sprintf("%s%d", prefixRobot, serverNumber)
8383
}
84+
85+
// FromRobotLegacyServerNumber generates the canonical ProviderID for a Robot Server.
86+
func FromRobotLegacyServerNumber(serverNumber int) string {
87+
return fmt.Sprintf("%s%d", prefixRobotLegacy, serverNumber)
88+
}

0 commit comments

Comments
 (0)