Skip to content

Commit 619dc53

Browse files
committed
ProviderID of infra machines should not change: Validate that.
1 parent 42e9483 commit 619dc53

File tree

4 files changed

+58
-4
lines changed

4 files changed

+58
-4
lines changed

api/v1beta1/hetznerbaremetalmachine_types.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,10 @@ type HetznerBareMetalMachineSpec struct {
7070
// ProviderID will be the hetznerbaremetalmachine which is set by the controller in the
7171
// `hrobot://<server-id>` format. Before caph v1.1.0 the ProviderID had the format
7272
// `hcloud://bm-NNNNN`. Starting with caph v1.1.x this was changed to `hrobot://NNNNN`. This
73-
// aligns to the upstream hcloud ccm. In the long run we want to discontinue our ccm fork.
73+
// aligns to the upstream hcloud ccm. In the long run we want to discontinue our ccm fork. After
74+
// the ProviderID was set once, it never changes. It is safe to update the ccm in a running
75+
// workload-cluster. Only new nodes will get the new format.
76+
//
7477
// +optional
7578
ProviderID *string `json:"providerID,omitempty"`
7679

api/v1beta1/hetznerbaremetalmachine_validation.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,5 +86,14 @@ func validateHetznerBareMetalMachineSpecUpdate(oldSpec, newSpec HetznerBareMetal
8686
)
8787
}
8888

89+
if oldSpec.ProviderID != nil && *oldSpec.ProviderID != "" {
90+
// once the Provider was set, the value must not change.
91+
if newSpec.ProviderID == nil || *oldSpec.ProviderID != *newSpec.ProviderID {
92+
allErrs = append(allErrs,
93+
field.Invalid(field.NewPath("spec", "providerID"), newSpec.ProviderID, "providerID immutable"),
94+
)
95+
}
96+
}
97+
8998
return allErrs
9099
}

api/v1beta1/hetznerbaremetalmachine_validation_test.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,14 @@ limitations under the License.
1717
package v1beta1
1818

1919
import (
20+
"fmt"
2021
"testing"
2122

2223
"github.com/stretchr/testify/assert"
24+
"github.com/stretchr/testify/require"
2325
"k8s.io/apimachinery/pkg/selection"
2426
"k8s.io/apimachinery/pkg/util/validation/field"
27+
"k8s.io/utils/ptr"
2528
)
2629

2730
func TestValidateHetznerBareMetalMachineSpecCreate(t *testing.T) {
@@ -429,3 +432,43 @@ func TestValidateHetznerBareMetalMachineSpecUpdate(t *testing.T) {
429432
})
430433
}
431434
}
435+
436+
func TestValidateHetznerBareMetalMachineSpecUpdate_ProviderID(t *testing.T) {
437+
got := validateHetznerBareMetalMachineSpecUpdate(
438+
HetznerBareMetalMachineSpec{
439+
ProviderID: ptr.To("provider://foo"),
440+
},
441+
HetznerBareMetalMachineSpec{})
442+
require.Equal(t, `[spec.providerID: Invalid value: "null": providerID immutable]`, fmt.Sprintf("%+v", got))
443+
444+
got = validateHetznerBareMetalMachineSpecUpdate(
445+
HetznerBareMetalMachineSpec{
446+
ProviderID: ptr.To("provider://foo"),
447+
},
448+
HetznerBareMetalMachineSpec{
449+
ProviderID: ptr.To("provider://bar"),
450+
})
451+
require.Equal(t, `[spec.providerID: Invalid value: "provider://bar": providerID immutable]`, fmt.Sprintf("%+v", got))
452+
453+
// Allowed Updates
454+
got = validateHetznerBareMetalMachineSpecUpdate(
455+
HetznerBareMetalMachineSpec{},
456+
HetznerBareMetalMachineSpec{})
457+
require.Equal(t, `[]`, fmt.Sprintf("%+v", got))
458+
459+
got = validateHetznerBareMetalMachineSpecUpdate(
460+
HetznerBareMetalMachineSpec{},
461+
HetznerBareMetalMachineSpec{
462+
ProviderID: ptr.To("provider://bar"),
463+
})
464+
require.Equal(t, `[]`, fmt.Sprintf("%+v", got))
465+
466+
got = validateHetznerBareMetalMachineSpecUpdate(
467+
HetznerBareMetalMachineSpec{
468+
ProviderID: ptr.To("provider://bar"),
469+
},
470+
HetznerBareMetalMachineSpec{
471+
ProviderID: ptr.To("provider://bar"),
472+
})
473+
require.Equal(t, `[]`, fmt.Sprintf("%+v", got))
474+
}

pkg/services/baremetal/baremetal/baremetal.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -899,9 +899,8 @@ func checkForRequeueError(err error, errMessage string) (res reconcile.Result, r
899899
return reconcile.Result{}, fmt.Errorf("%s: %w", errMessage, err)
900900
}
901901

902-
// providerIDFromServerID returns the ProviderID. Before caph v1.1.0 the ProviderID had the format
903-
// `hcloud://bm-NNNNN`. Starting with caph v1.1.x this was changed to `hrobot://NNNNN`. This aligns
904-
// to the upstream hcloud ccm. In the long run we want to discontinue our ccm fork.
902+
// providerIDFromServerID returns the ProviderID. See HetznerBareMetalMachineSpec.ProviderID for
903+
// docs.
905904
func providerIDFromServerID(serverID int) string {
906905
return fmt.Sprintf("%s%d", providerIDPrefix, serverID)
907906
}

0 commit comments

Comments
 (0)