Skip to content

Commit c7a7265

Browse files
committed
feat: support setting hostname to the machine name
Fixes #131 Signed-off-by: Andrey Smirnov <[email protected]>
1 parent 36fb7cc commit c7a7265

14 files changed

+246
-29
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,8 @@ Fields available in the `TalosConfigTemplate` (and `TalosConfig`) resources:
8686
It is recommended to always set this field explicitly to avoid issues when CABPT is upgraded to the version which supports new Talos version.
8787
- `configPatches` (optional): set of machine configuration patches to apply to the generated configuration.
8888
- `data` (only for `generateType: none`): user-supplied machine configuration.
89+
- `hostname` (optional): configure hostname in the generate machine configuration:
90+
- `source` (`MachineName`): set the hostname in the generated machine configuration to the `Machine` name (not supported with `MachinePool` deployments)
8991

9092
### Generated Machine Configuration
9193

api/v1alpha3/talosconfig_types.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,25 @@ type TalosConfigSpec struct {
2020
GenerateType string `json:"generateType"` //none,init,controlplane,worker mutually exclusive w/ data
2121
Data string `json:"data,omitempty"`
2222
ConfigPatches []ConfigPatches `json:"configPatches,omitempty"`
23+
// Set hostname in the machine configuration to some value.
24+
Hostname HostnameSpec `json:"hostname,omitempty"`
2325
// Important: Run "make" to regenerate code after modifying this file
2426
}
2527

28+
// HostnameSource is the definition of hostname source.
29+
type HostnameSource string
30+
31+
// HostnameSourceMachineName sets the hostname in the generated configuration to the machine name.
32+
const HostnameSourceMachineName HostnameSource = "MachineName"
33+
34+
// HostnameSpec defines the hostname source.
35+
type HostnameSpec struct {
36+
// Source of the hostname.
37+
//
38+
// Allowed values: "MachineName" (use linked Machine's Name).
39+
Source HostnameSource `json:"source,omitempty"`
40+
}
41+
2642
// TalosConfigStatus defines the observed state of TalosConfig
2743
type TalosConfigStatus struct {
2844
// Ready indicates the BootstrapData field is ready to be consumed

api/v1alpha3/talosconfig_webhook.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,60 @@
55
package v1alpha3
66

77
import (
8+
"fmt"
9+
10+
apierrors "k8s.io/apimachinery/pkg/api/errors"
11+
runtime "k8s.io/apimachinery/pkg/runtime"
12+
"k8s.io/apimachinery/pkg/runtime/schema"
13+
"k8s.io/apimachinery/pkg/util/validation/field"
814
ctrl "sigs.k8s.io/controller-runtime"
15+
"sigs.k8s.io/controller-runtime/pkg/webhook"
916
)
1017

1118
func (r *TalosConfig) SetupWebhookWithManager(mgr ctrl.Manager) error {
1219
return ctrl.NewWebhookManagedBy(mgr).
1320
For(r).
1421
Complete()
1522
}
23+
24+
//+kubebuilder:webhook:verbs=create;update;delete,path=/validate-bootstrap-cluster-x-k8s-io-v1alpha3-talosconfig,mutating=false,failurePolicy=fail,groups=bootstrap.cluster.x-k8s.io,resources=talosconfigs,versions=v1alpha3,name=vtalosconfig.cluster.x-k8s.io,sideEffects=None,admissionReviewVersions=v1
25+
26+
var _ webhook.Validator = &TalosConfig{}
27+
28+
// ValidateCreate implements webhook.Validator so a webhook will be registered for the type
29+
func (r *TalosConfig) ValidateCreate() error {
30+
return r.validate()
31+
}
32+
33+
// ValidateUpdate implements webhook.Validator so a webhook will be registered for the type
34+
func (r *TalosConfig) ValidateUpdate(old runtime.Object) error {
35+
return r.validate()
36+
}
37+
38+
// ValidateDelete implements webhook.Validator so a webhook will be registered for the type
39+
func (r *TalosConfig) ValidateDelete() error {
40+
return nil
41+
}
42+
43+
func (r *TalosConfig) validate() error {
44+
var allErrs field.ErrorList
45+
46+
switch r.Spec.Hostname.Source {
47+
case "":
48+
case HostnameSourceMachineName:
49+
default:
50+
allErrs = append(allErrs,
51+
field.Invalid(field.NewPath("spec").Child("hostname").Child("source"), r.Spec.Hostname.Source,
52+
fmt.Sprintf("valid values are: %q", []HostnameSource{HostnameSourceMachineName}),
53+
),
54+
)
55+
}
56+
57+
if len(allErrs) == 0 {
58+
return nil
59+
}
60+
61+
return apierrors.NewInvalid(
62+
schema.GroupKind{Group: GroupVersion.Group, Kind: "TalosConfig"},
63+
r.Name, allErrs)
64+
}

api/v1alpha3/zz_generated.deepcopy.go

Lines changed: 17 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,14 @@ spec:
110110
type: string
111111
generateType:
112112
type: string
113+
hostname:
114+
description: Set hostname in the machine configuration to some value.
115+
properties:
116+
source:
117+
description: "Source of the hostname. \n Allowed values: \"MachineName\"
118+
(use linked Machine's Name)."
119+
type: string
120+
type: object
113121
talosVersion:
114122
type: string
115123
required:

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,15 @@ spec:
104104
type: string
105105
generateType:
106106
type: string
107+
hostname:
108+
description: Set hostname in the machine configuration to
109+
some value.
110+
properties:
111+
source:
112+
description: "Source of the hostname. \n Allowed values:
113+
\"MachineName\" (use linked Machine's Name)."
114+
type: string
115+
type: object
107116
talosVersion:
108117
type: string
109118
required:

config/webhook/manifests.yaml

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,13 @@ metadata:
88
webhooks:
99
- admissionReviewVersions:
1010
- v1
11-
- v1beta1
1211
clientConfig:
13-
caBundle: Cg==
1412
service:
1513
name: webhook-service
1614
namespace: system
17-
path: /validate-infrastructure-cluster-x-k8s-io-v1alpha3-talosconfig
15+
path: /validate-bootstrap-cluster-x-k8s-io-v1alpha3-talosconfig
1816
failurePolicy: Fail
19-
matchPolicy: Equivalent
20-
name: validation.talosconfig.bootstrap.cluster.x-k8s.io
17+
name: vtalosconfig.cluster.x-k8s.io
2118
rules:
2219
- apiGroups:
2320
- bootstrap.cluster.x-k8s.io
@@ -26,6 +23,7 @@ webhooks:
2623
operations:
2724
- CREATE
2825
- UPDATE
26+
- DELETE
2927
resources:
30-
- talosconfig
31-
sideEffects: None
28+
- talosconfigs
29+
sideEffects: None

controllers/talosconfig_controller.go

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ import (
4141
"sigs.k8s.io/controller-runtime/pkg/handler"
4242
"sigs.k8s.io/controller-runtime/pkg/source"
4343

44+
"github.com/talos-systems/cluster-api-bootstrap-provider-talos/api/v1alpha3"
4445
bootstrapv1alpha3 "github.com/talos-systems/cluster-api-bootstrap-provider-talos/api/v1alpha3"
4546
// +kubebuilder:scaffold:imports
4647
)
@@ -463,7 +464,7 @@ func (r *TalosConfigReconciler) genConfigs(ctx context.Context, scope *TalosConf
463464
}
464465

465466
clusterDNS := constants.DefaultDNSDomain
466-
if scope.Cluster.Spec.ClusterNetwork.ServiceDomain != "" {
467+
if scope.Cluster.Spec.ClusterNetwork != nil && scope.Cluster.Spec.ClusterNetwork.ServiceDomain != "" {
467468
clusterDNS = scope.Cluster.Spec.ClusterNetwork.ServiceDomain
468469
}
469470

@@ -516,13 +517,21 @@ func (r *TalosConfigReconciler) genConfigs(ctx context.Context, scope *TalosConf
516517
return retBundle, err
517518
}
518519

519-
if scope.Cluster.Spec.ClusterNetwork.Pods != nil {
520+
if scope.Cluster.Spec.ClusterNetwork != nil && scope.Cluster.Spec.ClusterNetwork.Pods != nil {
520521
data.ClusterConfig.ClusterNetwork.PodSubnet = scope.Cluster.Spec.ClusterNetwork.Pods.CIDRBlocks
521522
}
522-
if scope.Cluster.Spec.ClusterNetwork.Services != nil {
523+
if scope.Cluster.Spec.ClusterNetwork != nil && scope.Cluster.Spec.ClusterNetwork.Services != nil {
523524
data.ClusterConfig.ClusterNetwork.ServiceSubnet = scope.Cluster.Spec.ClusterNetwork.Services.CIDRBlocks
524525
}
525526

527+
if !scope.ConfigOwner.IsMachinePool() && scope.Config.Spec.Hostname.Source == v1alpha3.HostnameSourceMachineName {
528+
if data.MachineConfig.MachineNetwork == nil {
529+
data.MachineConfig.MachineNetwork = &v1alpha1.NetworkConfig{}
530+
}
531+
532+
data.MachineConfig.MachineNetwork.NetworkHostname = scope.ConfigOwner.GetName()
533+
}
534+
526535
dataOut, err := data.String()
527536
if err != nil {
528537
return retBundle, err

go.mod

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ require (
1717
k8s.io/apiextensions-apiserver v0.22.2
1818
k8s.io/apimachinery v0.22.2
1919
k8s.io/client-go v0.22.2
20-
sigs.k8s.io/cluster-api v1.0.0
21-
sigs.k8s.io/controller-runtime v0.10.2
20+
sigs.k8s.io/cluster-api v1.0.3
21+
sigs.k8s.io/controller-runtime v0.10.3
2222
)
2323

2424
require (

go.sum

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1216,10 +1216,10 @@ rsc.io/binaryregexp v0.2.0/go.mod h1:qTv7/COck+e2FymRvadv62gMdZztPaShugOCi3I+8D8
12161216
rsc.io/quote/v3 v3.1.0/go.mod h1:yEA65RcK8LyAZtP9Kv3t0HmxON59tX3rD+tICJqUlj0=
12171217
rsc.io/sampler v1.3.0/go.mod h1:T1hPZKmBbMNahiBKFy5HrXp6adAjACjK9JXDnKaTXpA=
12181218
sigs.k8s.io/apiserver-network-proxy/konnectivity-client v0.0.22/go.mod h1:LEScyzhFmoF5pso/YSeBstl57mOzx9xlU9n85RGrDQg=
1219-
sigs.k8s.io/cluster-api v1.0.0 h1:GcVA2ObQTXo/+jzSLWPy4Bd3NeiwJyAB8n19kyJIotA=
1220-
sigs.k8s.io/cluster-api v1.0.0/go.mod h1:V230kMSaYENTUcx1QRkoRCklb3vfphQGV3/z4ODNGWo=
1221-
sigs.k8s.io/controller-runtime v0.10.2 h1:jW8qiY+yMnnPx6O9hu63tgcwaKzd1yLYui+mpvClOOc=
1222-
sigs.k8s.io/controller-runtime v0.10.2/go.mod h1:CQp8eyUQZ/Q7PJvnIrB6/hgfTC1kBkGylwsLgOQi1WY=
1219+
sigs.k8s.io/cluster-api v1.0.3 h1:unJqMUG5LJ312kgHSAnBzfYazbGCcGkTcdGD3lF459k=
1220+
sigs.k8s.io/cluster-api v1.0.3/go.mod h1:/LkJXtsvhxTV4U0z1Y2Y1Gr2xebJ0/ce09Ab2M0XU/U=
1221+
sigs.k8s.io/controller-runtime v0.10.3 h1:s5Ttmw/B4AuIbwrXD3sfBkXwnPMMWrqpVj4WRt1dano=
1222+
sigs.k8s.io/controller-runtime v0.10.3/go.mod h1:CQp8eyUQZ/Q7PJvnIrB6/hgfTC1kBkGylwsLgOQi1WY=
12231223
sigs.k8s.io/kustomize/api v0.8.11/go.mod h1:a77Ls36JdfCWojpUqR6m60pdGY1AYFix4AH83nJtY1g=
12241224
sigs.k8s.io/kustomize/cmd/config v0.9.13/go.mod h1:7547FLF8W/lTaDf0BDqFTbZxM9zqwEJqCKN9sSR0xSs=
12251225
sigs.k8s.io/kustomize/kustomize/v4 v4.2.0/go.mod h1:MOkR6fmhwG7hEDRXBYELTi5GSFcLwfqwzTRHW3kv5go=

0 commit comments

Comments
 (0)