Skip to content

Commit e135465

Browse files
echoziosmira
authored andcommitted
feat: add infrastructurename hostname source
Allow hostname to follow infrastructure name. Signed-off-by: Andrey Smirnov <[email protected]> Signed-off-by: Chris <[email protected]>
1 parent 54cda17 commit e135465

File tree

7 files changed

+54
-6
lines changed

7 files changed

+54
-6
lines changed

api/v1alpha3/talosconfig_types.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,16 @@ type HostnameSource string
3333
// HostnameSourceMachineName sets the hostname in the generated configuration to the machine name.
3434
const HostnameSourceMachineName HostnameSource = "MachineName"
3535

36+
// HostnameSourceInfrastructureName sets the hostname in the generated configuration to the name of the machine's infrastructure.
37+
const HostnameSourceInfrastructureName HostnameSource = "InfrastructureName"
38+
3639
// HostnameSpec defines the hostname source.
3740
type HostnameSpec struct {
3841
// Source of the hostname.
3942
//
40-
// Allowed values: "MachineName" (use linked Machine's Name).
43+
// Allowed values:
44+
// "MachineName" (use linked Machine's Name).
45+
// "InfrastructureName" (use linked Machine's infrastructure's name).
4146
Source HostnameSource `json:"source,omitempty"`
4247
}
4348

api/v1alpha3/talosconfig_webhook.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,11 @@ func (r *TalosConfig) validate() error {
5454
switch r.Spec.Hostname.Source {
5555
case "":
5656
case HostnameSourceMachineName:
57+
case HostnameSourceInfrastructureName:
5758
default:
5859
allErrs = append(allErrs,
5960
field.Invalid(field.NewPath("spec").Child("hostname").Child("source"), r.Spec.Hostname.Source,
60-
fmt.Sprintf("valid values are: %q", []HostnameSource{HostnameSourceMachineName}),
61+
fmt.Sprintf("valid values are: %q", []HostnameSource{HostnameSourceMachineName, HostnameSourceInfrastructureName}),
6162
),
6263
)
6364
}

config/crd/bases/bootstrap.cluster.x-k8s.io_talosconfigs.yaml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,9 @@ spec:
125125
description: |-
126126
Source of the hostname.
127127
128-
Allowed values: "MachineName" (use linked Machine's Name).
128+
Allowed values:
129+
"MachineName" (use linked Machine's Name).
130+
"InfrastructureName" (use linked Machine's infrastructure's name).
129131
type: string
130132
type: object
131133
strategicPatches:

config/crd/bases/bootstrap.cluster.x-k8s.io_talosconfigtemplates.yaml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,9 @@ spec:
120120
description: |-
121121
Source of the hostname.
122122
123-
Allowed values: "MachineName" (use linked Machine's Name).
123+
Allowed values:
124+
"MachineName" (use linked Machine's Name).
125+
"InfrastructureName" (use linked Machine's infrastructure's name).
124126
type: string
125127
type: object
126128
strategicPatches:

controllers/talosconfig_controller.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -544,12 +544,22 @@ func (r *TalosConfigReconciler) genConfigs(ctx context.Context, scope *TalosConf
544544
data.RawV1Alpha1().ClusterConfig.ClusterNetwork.ServiceSubnet = scope.Cluster.Spec.ClusterNetwork.Services.CIDRBlocks
545545
}
546546

547-
if !scope.ConfigOwner.IsMachinePool() && scope.Config.Spec.Hostname.Source == v1alpha3.HostnameSourceMachineName {
547+
if !scope.ConfigOwner.IsMachinePool() && scope.Config.Spec.Hostname.Source != "" {
548548
if data.RawV1Alpha1().MachineConfig.MachineNetwork == nil {
549549
data.RawV1Alpha1().MachineConfig.MachineNetwork = &v1alpha1.NetworkConfig{}
550550
}
551551

552-
data.RawV1Alpha1().MachineConfig.MachineNetwork.NetworkHostname = scope.ConfigOwner.GetName()
552+
if scope.Config.Spec.Hostname.Source == v1alpha3.HostnameSourceMachineName {
553+
data.RawV1Alpha1().MachineConfig.MachineNetwork.NetworkHostname = scope.ConfigOwner.GetName()
554+
}
555+
556+
if scope.Config.Spec.Hostname.Source == v1alpha3.HostnameSourceInfrastructureName {
557+
machine := &capiv1.Machine{}
558+
if err := runtime.DefaultUnstructuredConverter.FromUnstructured(scope.ConfigOwner.Object, machine); err != nil {
559+
return retBundle, err
560+
}
561+
data.RawV1Alpha1().MachineConfig.MachineNetwork.NetworkHostname = machine.Spec.InfrastructureRef.Name
562+
}
553563
}
554564

555565
dataOut, err := data.EncodeString(encoder.WithComments(encoder.CommentsDisabled))

internal/integration/helpers_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,10 @@ func createMachine(ctx context.Context, t *testing.T, c client.Client, cluster *
118118
UID: talosconfig.GetUID(),
119119
},
120120
},
121+
InfrastructureRef: corev1.ObjectReference{
122+
Name: generateName(t, "infrastructure"),
123+
Namespace: cluster.Namespace,
124+
},
121125
},
122126
}
123127

internal/integration/integration_test.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -456,6 +456,30 @@ func TestIntegration(t *testing.T) {
456456

457457
assert.Equal(t, machine.Name, provider.Machine().Network().Hostname())
458458
})
459+
t.Run("HostnameFromInfraName", func(t *testing.T) {
460+
t.Parallel()
461+
462+
namespaceName := setupTest(ctx, t, c)
463+
cluster := createCluster(ctx, t, c, namespaceName, &capiv1.ClusterSpec{
464+
ControlPlaneEndpoint: capiv1.APIEndpoint{
465+
Host: "example.com",
466+
Port: 443,
467+
},
468+
})
469+
talosConfig := createTalosConfig(ctx, t, c, namespaceName, bootstrapv1alpha3.TalosConfigSpec{
470+
GenerateType: talosmachine.TypeControlPlane.String(),
471+
TalosVersion: TalosVersion,
472+
Hostname: bootstrapv1alpha3.HostnameSpec{
473+
Source: bootstrapv1alpha3.HostnameSourceInfrastructureName,
474+
},
475+
})
476+
machine := createMachine(ctx, t, c, cluster, talosConfig, true)
477+
waitForReady(ctx, t, c, talosConfig)
478+
479+
provider := assertMachineConfiguration(ctx, t, c, talosConfig)
480+
481+
assert.Equal(t, machine.Spec.InfrastructureRef.Name, provider.Machine().Network().Hostname())
482+
})
459483
t.Run("TalosConfigValidate", func(t *testing.T) {
460484
t.Parallel()
461485

0 commit comments

Comments
 (0)