From 20133af02152f6c4300497f7a4cae3edb298fd80 Mon Sep 17 00:00:00 2001 From: Miltiadis Alexis Date: Thu, 2 Jan 2025 17:21:02 +0100 Subject: [PATCH 1/3] feat: allow diskSetup to include partition layout Signed-off-by: Miltiadis Alexis --- .../api/v1beta1/kubeadmconfig_types.go | 25 +++- .../api/v1beta1/zz_generated.deepcopy.go | 48 +++++++ ...strap.cluster.x-k8s.io_kubeadmconfigs.yaml | 26 +++- ...uster.x-k8s.io_kubeadmconfigtemplates.yaml | 26 +++- .../internal/cloudinit/cloudinit_test.go | 133 +++++++++++------- .../kubeadm/internal/cloudinit/disk_setup.go | 11 ++ ...cluster.x-k8s.io_kubeadmcontrolplanes.yaml | 26 +++- ...x-k8s.io_kubeadmcontrolplanetemplates.yaml | 26 +++- .../bootstrap/kubeadm/v1alpha3/conversion.go | 12 ++ .../v1alpha3/zz_generated.conversion.go | 60 ++++++-- .../bootstrap/kubeadm/v1alpha4/conversion.go | 11 ++ .../v1alpha4/zz_generated.conversion.go | 60 ++++++-- 12 files changed, 381 insertions(+), 83 deletions(-) diff --git a/bootstrap/kubeadm/api/v1beta1/kubeadmconfig_types.go b/bootstrap/kubeadm/api/v1beta1/kubeadmconfig_types.go index ea2956d30857..bab106e52817 100644 --- a/bootstrap/kubeadm/api/v1beta1/kubeadmconfig_types.go +++ b/bootstrap/kubeadm/api/v1beta1/kubeadmconfig_types.go @@ -675,19 +675,42 @@ type DiskSetup struct { Filesystems []Filesystem `json:"filesystems,omitempty"` } +// DiskLayout represents an array of partition specifications +type DiskLayout []PartitionSpec + +// PartitionSpec defines the size and optional type for a partition +type PartitionSpec struct { + // Percentage of disk that partition will take (1-100) + // +kubebuilder:validation:Minimum=1 + // +kubebuilder:validation:Maximum=100 + Percentage int32 `json:"percentage"` + + // PartitionType is the numerical value of the partition type (optional) + // +optional + PartitionType *int32 `json:"partitionType,omitempty"` +} + // Partition defines how to create and layout a partition. type Partition struct { // device is the name of the device. Device string `json:"device"` + // layout specifies the device layout. // If it is true, a single partition will be created for the entire device. // When layout is false, it means don't partition or ignore existing partitioning. Layout bool `json:"layout"` + + // diskLayout specifies the percentage of disk space and partition types. + // If specified, this will override the Layout field. + // +optional + DiskLayout DiskLayout `json:"diskLayout,omitempty"` + // overwrite describes whether to skip checks and create the partition if a partition or filesystem is found on the device. // Use with caution. Default is 'false'. // +optional Overwrite *bool `json:"overwrite,omitempty"` - // tableType specifies the tupe of partition table. The following are supported: + + // tableType specifies the type of partition table. The following are supported: // 'mbr': default and setups a MS-DOS partition table // 'gpt': setups a GPT partition table // +optional diff --git a/bootstrap/kubeadm/api/v1beta1/zz_generated.deepcopy.go b/bootstrap/kubeadm/api/v1beta1/zz_generated.deepcopy.go index 3f508065e68c..b225e06904a9 100644 --- a/bootstrap/kubeadm/api/v1beta1/zz_generated.deepcopy.go +++ b/bootstrap/kubeadm/api/v1beta1/zz_generated.deepcopy.go @@ -305,6 +305,27 @@ func (in *Discovery) DeepCopy() *Discovery { return out } +// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. +func (in DiskLayout) DeepCopyInto(out *DiskLayout) { + { + in := &in + *out = make(DiskLayout, len(*in)) + for i := range *in { + (*in)[i].DeepCopyInto(&(*out)[i]) + } + } +} + +// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new DiskLayout. +func (in DiskLayout) DeepCopy() DiskLayout { + if in == nil { + return nil + } + out := new(DiskLayout) + in.DeepCopyInto(out) + return *out +} + // DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. func (in *DiskSetup) DeepCopyInto(out *DiskSetup) { *out = *in @@ -1194,6 +1215,13 @@ func (in *NodeRegistrationOptions) DeepCopy() *NodeRegistrationOptions { // DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. func (in *Partition) DeepCopyInto(out *Partition) { *out = *in + if in.DiskLayout != nil { + in, out := &in.DiskLayout, &out.DiskLayout + *out = make(DiskLayout, len(*in)) + for i := range *in { + (*in)[i].DeepCopyInto(&(*out)[i]) + } + } if in.Overwrite != nil { in, out := &in.Overwrite, &out.Overwrite *out = new(bool) @@ -1216,6 +1244,26 @@ func (in *Partition) DeepCopy() *Partition { return out } +// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. +func (in *PartitionSpec) DeepCopyInto(out *PartitionSpec) { + *out = *in + if in.PartitionType != nil { + in, out := &in.PartitionType, &out.PartitionType + *out = new(int32) + **out = **in + } +} + +// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new PartitionSpec. +func (in *PartitionSpec) DeepCopy() *PartitionSpec { + if in == nil { + return nil + } + out := new(PartitionSpec) + in.DeepCopyInto(out) + return out +} + // DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. func (in *PasswdSource) DeepCopyInto(out *PasswdSource) { *out = *in diff --git a/bootstrap/kubeadm/config/crd/bases/bootstrap.cluster.x-k8s.io_kubeadmconfigs.yaml b/bootstrap/kubeadm/config/crd/bases/bootstrap.cluster.x-k8s.io_kubeadmconfigs.yaml index 3d29c546acc4..1da391e33bde 100644 --- a/bootstrap/kubeadm/config/crd/bases/bootstrap.cluster.x-k8s.io_kubeadmconfigs.yaml +++ b/bootstrap/kubeadm/config/crd/bases/bootstrap.cluster.x-k8s.io_kubeadmconfigs.yaml @@ -2914,6 +2914,30 @@ spec: device: description: device is the name of the device. type: string + diskLayout: + description: |- + diskLayout specifies the percentage of disk space and partition types. + If specified, this will override the Layout field. + items: + description: PartitionSpec defines the size and optional + type for a partition + properties: + partitionType: + description: PartitionType is the numerical value + of the partition type (optional) + format: int32 + type: integer + percentage: + description: Percentage of disk that partition will + take (1-100) + format: int32 + maximum: 100 + minimum: 1 + type: integer + required: + - percentage + type: object + type: array layout: description: |- layout specifies the device layout. @@ -2927,7 +2951,7 @@ spec: type: boolean tableType: description: |- - tableType specifies the tupe of partition table. The following are supported: + tableType specifies the type of partition table. The following are supported: 'mbr': default and setups a MS-DOS partition table 'gpt': setups a GPT partition table type: string diff --git a/bootstrap/kubeadm/config/crd/bases/bootstrap.cluster.x-k8s.io_kubeadmconfigtemplates.yaml b/bootstrap/kubeadm/config/crd/bases/bootstrap.cluster.x-k8s.io_kubeadmconfigtemplates.yaml index 23cfa5bbd1f0..8b1597e9519b 100644 --- a/bootstrap/kubeadm/config/crd/bases/bootstrap.cluster.x-k8s.io_kubeadmconfigtemplates.yaml +++ b/bootstrap/kubeadm/config/crd/bases/bootstrap.cluster.x-k8s.io_kubeadmconfigtemplates.yaml @@ -2890,6 +2890,30 @@ spec: device: description: device is the name of the device. type: string + diskLayout: + description: |- + diskLayout specifies the percentage of disk space and partition types. + If specified, this will override the Layout field. + items: + description: PartitionSpec defines the size and + optional type for a partition + properties: + partitionType: + description: PartitionType is the numerical + value of the partition type (optional) + format: int32 + type: integer + percentage: + description: Percentage of disk that partition + will take (1-100) + format: int32 + maximum: 100 + minimum: 1 + type: integer + required: + - percentage + type: object + type: array layout: description: |- layout specifies the device layout. @@ -2903,7 +2927,7 @@ spec: type: boolean tableType: description: |- - tableType specifies the tupe of partition table. The following are supported: + tableType specifies the type of partition table. The following are supported: 'mbr': default and setups a MS-DOS partition table 'gpt': setups a GPT partition table type: string diff --git a/bootstrap/kubeadm/internal/cloudinit/cloudinit_test.go b/bootstrap/kubeadm/internal/cloudinit/cloudinit_test.go index 789e9b7e8bcd..75a478b60cd9 100644 --- a/bootstrap/kubeadm/internal/cloudinit/cloudinit_test.go +++ b/bootstrap/kubeadm/internal/cloudinit/cloudinit_test.go @@ -127,66 +127,99 @@ func TestNewInitControlPlaneCommands(t *testing.T) { } func TestNewInitControlPlaneDiskMounts(t *testing.T) { - g := NewWithT(t) - - cpinput := &ControlPlaneInput{ - BaseUserData: BaseUserData{ - Header: "test", - PreKubeadmCommands: nil, - PostKubeadmCommands: nil, - WriteFiles: nil, - Users: nil, - NTP: nil, - DiskSetup: &bootstrapv1.DiskSetup{ - Partitions: []bootstrapv1.Partition{ - { - Device: "test-device", - Layout: true, - Overwrite: ptr.To(false), - TableType: ptr.To("gpt"), + tests := []struct { + name string + input *ControlPlaneInput + expected []string + }{ + { + name: "simple layout", + input: &ControlPlaneInput{ + BaseUserData: BaseUserData{ + Header: "test", + DiskSetup: &bootstrapv1.DiskSetup{ + Partitions: []bootstrapv1.Partition{ + { + Device: "test-device", + Layout: true, + Overwrite: ptr.To(false), + TableType: ptr.To("gpt"), + }, + }, + Filesystems: []bootstrapv1.Filesystem{ + { + Device: "test-device", + Filesystem: "ext4", + Label: "test_disk", + ExtraOpts: []string{"-F", "-E", "lazy_itable_init=1,lazy_journal_init=1"}, + }, + }, + }, + Mounts: []bootstrapv1.MountPoints{ + {"test_disk", "/var/lib/testdir"}, }, }, - Filesystems: []bootstrapv1.Filesystem{ - { - Device: "test-device", - Filesystem: "ext4", - Label: "test_disk", - ExtraOpts: []string{"-F", "-E", "lazy_itable_init=1,lazy_journal_init=1"}, + Certificates: secret.Certificates{}, + ClusterConfiguration: "my-cluster-config", + InitConfiguration: "my-init-config", + }, + expected: []string{ + `disk_setup: + test-device: + table_type: gpt + layout: true + overwrite: false`, + }, + }, + { + name: "detailed disk layout", + input: &ControlPlaneInput{ + BaseUserData: BaseUserData{ + Header: "test", + DiskSetup: &bootstrapv1.DiskSetup{ + Partitions: []bootstrapv1.Partition{ + { + Device: "test-device", + DiskLayout: bootstrapv1.DiskLayout{ + {Percentage: 33}, + {Percentage: 33, PartitionType: ptr.To[int32](82)}, + {Percentage: 33}, + }, + Overwrite: ptr.To(true), + TableType: ptr.To("gpt"), + }, + }, }, }, + Certificates: secret.Certificates{}, + ClusterConfiguration: "my-cluster-config", + InitConfiguration: "my-init-config", }, - Mounts: []bootstrapv1.MountPoints{ - {"test_disk", "/var/lib/testdir"}, + expected: []string{ + `disk_setup: + test-device: + table_type: gpt + layout: + - 33 + - [33, 82] + - 33 + overwrite: true`, }, }, - Certificates: secret.Certificates{}, - ClusterConfiguration: "my-cluster-config", - InitConfiguration: "my-init-config", } - out, err := NewInitControlPlane(cpinput) - g.Expect(err).ToNot(HaveOccurred()) + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + g := NewWithT(t) - expectedDiskSetup := `disk_setup: - test-device: - table_type: gpt - layout: true - overwrite: false` - expectedFSSetup := `fs_setup: - - label: test_disk - filesystem: ext4 - device: test-device - extra_opts: - - -F - - -E - - lazy_itable_init=1,lazy_journal_init=1` - expectedMounts := `mounts: - - - test_disk - - /var/lib/testdir` - - g.Expect(string(out)).To(ContainSubstring(expectedDiskSetup)) - g.Expect(string(out)).To(ContainSubstring(expectedFSSetup)) - g.Expect(string(out)).To(ContainSubstring(expectedMounts)) + out, err := NewInitControlPlane(tt.input) + g.Expect(err).ToNot(HaveOccurred()) + + for _, expectedString := range tt.expected { + g.Expect(string(out)).To(ContainSubstring(expectedString)) + } + }) + } } func TestNewJoinControlPlaneAdditionalFileEncodings(t *testing.T) { diff --git a/bootstrap/kubeadm/internal/cloudinit/disk_setup.go b/bootstrap/kubeadm/internal/cloudinit/disk_setup.go index dfb169dd0543..d85caeb78c68 100644 --- a/bootstrap/kubeadm/internal/cloudinit/disk_setup.go +++ b/bootstrap/kubeadm/internal/cloudinit/disk_setup.go @@ -24,7 +24,18 @@ disk_setup:{{ range .Partitions }} {{- if .TableType }} table_type: {{ .TableType }} {{- end }} + {{- if .DiskLayout }} + layout: + {{- range .DiskLayout }} + {{- if .PartitionType }} + - [{{ .Percentage }}, {{ .PartitionType }}] + {{- else }} + - {{ .Percentage }} + {{- end }} + {{- end }} + {{- else }} layout: {{ .Layout }} + {{- end }} {{- if .Overwrite }} overwrite: {{ .Overwrite }} {{- end -}} diff --git a/controlplane/kubeadm/config/crd/bases/controlplane.cluster.x-k8s.io_kubeadmcontrolplanes.yaml b/controlplane/kubeadm/config/crd/bases/controlplane.cluster.x-k8s.io_kubeadmcontrolplanes.yaml index 26232ab849a5..73ec09f2e909 100644 --- a/controlplane/kubeadm/config/crd/bases/controlplane.cluster.x-k8s.io_kubeadmcontrolplanes.yaml +++ b/controlplane/kubeadm/config/crd/bases/controlplane.cluster.x-k8s.io_kubeadmcontrolplanes.yaml @@ -3391,6 +3391,30 @@ spec: device: description: device is the name of the device. type: string + diskLayout: + description: |- + diskLayout specifies the percentage of disk space and partition types. + If specified, this will override the Layout field. + items: + description: PartitionSpec defines the size and optional + type for a partition + properties: + partitionType: + description: PartitionType is the numerical value + of the partition type (optional) + format: int32 + type: integer + percentage: + description: Percentage of disk that partition + will take (1-100) + format: int32 + maximum: 100 + minimum: 1 + type: integer + required: + - percentage + type: object + type: array layout: description: |- layout specifies the device layout. @@ -3404,7 +3428,7 @@ spec: type: boolean tableType: description: |- - tableType specifies the tupe of partition table. The following are supported: + tableType specifies the type of partition table. The following are supported: 'mbr': default and setups a MS-DOS partition table 'gpt': setups a GPT partition table type: string diff --git a/controlplane/kubeadm/config/crd/bases/controlplane.cluster.x-k8s.io_kubeadmcontrolplanetemplates.yaml b/controlplane/kubeadm/config/crd/bases/controlplane.cluster.x-k8s.io_kubeadmcontrolplanetemplates.yaml index 332634f0bfa1..2f2ed90b6231 100644 --- a/controlplane/kubeadm/config/crd/bases/controlplane.cluster.x-k8s.io_kubeadmcontrolplanetemplates.yaml +++ b/controlplane/kubeadm/config/crd/bases/controlplane.cluster.x-k8s.io_kubeadmcontrolplanetemplates.yaml @@ -2119,6 +2119,30 @@ spec: device: description: device is the name of the device. type: string + diskLayout: + description: |- + diskLayout specifies the percentage of disk space and partition types. + If specified, this will override the Layout field. + items: + description: PartitionSpec defines the size + and optional type for a partition + properties: + partitionType: + description: PartitionType is the numerical + value of the partition type (optional) + format: int32 + type: integer + percentage: + description: Percentage of disk that partition + will take (1-100) + format: int32 + maximum: 100 + minimum: 1 + type: integer + required: + - percentage + type: object + type: array layout: description: |- layout specifies the device layout. @@ -2132,7 +2156,7 @@ spec: type: boolean tableType: description: |- - tableType specifies the tupe of partition table. The following are supported: + tableType specifies the type of partition table. The following are supported: 'mbr': default and setups a MS-DOS partition table 'gpt': setups a GPT partition table type: string diff --git a/internal/apis/bootstrap/kubeadm/v1alpha3/conversion.go b/internal/apis/bootstrap/kubeadm/v1alpha3/conversion.go index 4d02942935a3..cfb6116f4f60 100644 --- a/internal/apis/bootstrap/kubeadm/v1alpha3/conversion.go +++ b/internal/apis/bootstrap/kubeadm/v1alpha3/conversion.go @@ -109,6 +109,13 @@ func MergeRestoredKubeadmConfigSpec(dst *bootstrapv1.KubeadmConfigSpec, restored dst.JoinConfiguration.NodeRegistration.ImagePullPolicy = restored.JoinConfiguration.NodeRegistration.ImagePullPolicy dst.JoinConfiguration.NodeRegistration.ImagePullSerial = restored.JoinConfiguration.NodeRegistration.ImagePullSerial } + + if restored.DiskSetup != nil { + if dst.DiskSetup == nil { + dst.DiskSetup = &bootstrapv1.DiskSetup{} + } + dst.DiskSetup = restored.DiskSetup + } } func (dst *KubeadmConfig) ConvertFrom(srcRaw conversion.Hub) error { @@ -249,3 +256,8 @@ func Convert_v1beta1_KubeadmConfigStatus_To_v1alpha3_KubeadmConfigStatus(in *boo // V1Beta2 was added in v1beta1. return autoConvert_v1beta1_KubeadmConfigStatus_To_v1alpha3_KubeadmConfigStatus(in, out, s) } + +func Convert_v1beta1_Partition_To_v1alpha3_Partition(in *bootstrapv1.Partition, out *Partition, s apiconversion.Scope) error { + // Disk Lawas added in v1beta1. + return autoConvert_v1beta1_Partition_To_v1alpha3_Partition(in, out, s) +} diff --git a/internal/apis/bootstrap/kubeadm/v1alpha3/zz_generated.conversion.go b/internal/apis/bootstrap/kubeadm/v1alpha3/zz_generated.conversion.go index 8134a0dd27bf..c28a6b580978 100644 --- a/internal/apis/bootstrap/kubeadm/v1alpha3/zz_generated.conversion.go +++ b/internal/apis/bootstrap/kubeadm/v1alpha3/zz_generated.conversion.go @@ -149,11 +149,6 @@ func RegisterConversions(s *runtime.Scheme) error { }); err != nil { return err } - if err := s.AddGeneratedConversionFunc((*v1beta1.Partition)(nil), (*Partition)(nil), func(a, b interface{}, scope conversion.Scope) error { - return Convert_v1beta1_Partition_To_v1alpha3_Partition(a.(*v1beta1.Partition), b.(*Partition), scope) - }); err != nil { - return err - } if err := s.AddGeneratedConversionFunc((*SecretFileSource)(nil), (*v1beta1.SecretFileSource)(nil), func(a, b interface{}, scope conversion.Scope) error { return Convert_v1alpha3_SecretFileSource_To_v1beta1_SecretFileSource(a.(*SecretFileSource), b.(*v1beta1.SecretFileSource), scope) }); err != nil { @@ -224,6 +219,11 @@ func RegisterConversions(s *runtime.Scheme) error { }); err != nil { return err } + if err := s.AddConversionFunc((*v1beta1.Partition)(nil), (*Partition)(nil), func(a, b interface{}, scope conversion.Scope) error { + return Convert_v1beta1_Partition_To_v1alpha3_Partition(a.(*v1beta1.Partition), b.(*Partition), scope) + }); err != nil { + return err + } if err := s.AddConversionFunc((*v1beta1.User)(nil), (*User)(nil), func(a, b interface{}, scope conversion.Scope) error { return Convert_v1beta1_User_To_v1alpha3_User(a.(*v1beta1.User), b.(*User), scope) }); err != nil { @@ -233,7 +233,17 @@ func RegisterConversions(s *runtime.Scheme) error { } func autoConvert_v1alpha3_DiskSetup_To_v1beta1_DiskSetup(in *DiskSetup, out *v1beta1.DiskSetup, s conversion.Scope) error { - out.Partitions = *(*[]v1beta1.Partition)(unsafe.Pointer(&in.Partitions)) + if in.Partitions != nil { + in, out := &in.Partitions, &out.Partitions + *out = make([]v1beta1.Partition, len(*in)) + for i := range *in { + if err := Convert_v1alpha3_Partition_To_v1beta1_Partition(&(*in)[i], &(*out)[i], s); err != nil { + return err + } + } + } else { + out.Partitions = nil + } out.Filesystems = *(*[]v1beta1.Filesystem)(unsafe.Pointer(&in.Filesystems)) return nil } @@ -244,7 +254,17 @@ func Convert_v1alpha3_DiskSetup_To_v1beta1_DiskSetup(in *DiskSetup, out *v1beta1 } func autoConvert_v1beta1_DiskSetup_To_v1alpha3_DiskSetup(in *v1beta1.DiskSetup, out *DiskSetup, s conversion.Scope) error { - out.Partitions = *(*[]Partition)(unsafe.Pointer(&in.Partitions)) + if in.Partitions != nil { + in, out := &in.Partitions, &out.Partitions + *out = make([]Partition, len(*in)) + for i := range *in { + if err := Convert_v1beta1_Partition_To_v1alpha3_Partition(&(*in)[i], &(*out)[i], s); err != nil { + return err + } + } + } else { + out.Partitions = nil + } out.Filesystems = *(*[]Filesystem)(unsafe.Pointer(&in.Filesystems)) return nil } @@ -449,7 +469,15 @@ func autoConvert_v1alpha3_KubeadmConfigSpec_To_v1beta1_KubeadmConfigSpec(in *Kub } else { out.Files = nil } - out.DiskSetup = (*v1beta1.DiskSetup)(unsafe.Pointer(in.DiskSetup)) + if in.DiskSetup != nil { + in, out := &in.DiskSetup, &out.DiskSetup + *out = new(v1beta1.DiskSetup) + if err := Convert_v1alpha3_DiskSetup_To_v1beta1_DiskSetup(*in, *out, s); err != nil { + return err + } + } else { + out.DiskSetup = nil + } out.Mounts = *(*[]v1beta1.MountPoints)(unsafe.Pointer(&in.Mounts)) out.PreKubeadmCommands = *(*[]string)(unsafe.Pointer(&in.PreKubeadmCommands)) out.PostKubeadmCommands = *(*[]string)(unsafe.Pointer(&in.PostKubeadmCommands)) @@ -515,7 +543,15 @@ func autoConvert_v1beta1_KubeadmConfigSpec_To_v1alpha3_KubeadmConfigSpec(in *v1b } else { out.Files = nil } - out.DiskSetup = (*DiskSetup)(unsafe.Pointer(in.DiskSetup)) + if in.DiskSetup != nil { + in, out := &in.DiskSetup, &out.DiskSetup + *out = new(DiskSetup) + if err := Convert_v1beta1_DiskSetup_To_v1alpha3_DiskSetup(*in, *out, s); err != nil { + return err + } + } else { + out.DiskSetup = nil + } out.Mounts = *(*[]MountPoints)(unsafe.Pointer(&in.Mounts)) out.PreKubeadmCommands = *(*[]string)(unsafe.Pointer(&in.PreKubeadmCommands)) out.PostKubeadmCommands = *(*[]string)(unsafe.Pointer(&in.PostKubeadmCommands)) @@ -710,16 +746,12 @@ func Convert_v1alpha3_Partition_To_v1beta1_Partition(in *Partition, out *v1beta1 func autoConvert_v1beta1_Partition_To_v1alpha3_Partition(in *v1beta1.Partition, out *Partition, s conversion.Scope) error { out.Device = in.Device out.Layout = in.Layout + // WARNING: in.DiskLayout requires manual conversion: does not exist in peer-type out.Overwrite = (*bool)(unsafe.Pointer(in.Overwrite)) out.TableType = (*string)(unsafe.Pointer(in.TableType)) return nil } -// Convert_v1beta1_Partition_To_v1alpha3_Partition is an autogenerated conversion function. -func Convert_v1beta1_Partition_To_v1alpha3_Partition(in *v1beta1.Partition, out *Partition, s conversion.Scope) error { - return autoConvert_v1beta1_Partition_To_v1alpha3_Partition(in, out, s) -} - func autoConvert_v1alpha3_SecretFileSource_To_v1beta1_SecretFileSource(in *SecretFileSource, out *v1beta1.SecretFileSource, s conversion.Scope) error { out.Name = in.Name out.Key = in.Key diff --git a/internal/apis/bootstrap/kubeadm/v1alpha4/conversion.go b/internal/apis/bootstrap/kubeadm/v1alpha4/conversion.go index 3f9597aa6784..7aa9cdf0424d 100644 --- a/internal/apis/bootstrap/kubeadm/v1alpha4/conversion.go +++ b/internal/apis/bootstrap/kubeadm/v1alpha4/conversion.go @@ -106,6 +106,12 @@ func MergeRestoredKubeadmConfigSpec(dst *bootstrapv1.KubeadmConfigSpec, restored dst.JoinConfiguration.NodeRegistration.ImagePullPolicy = restored.JoinConfiguration.NodeRegistration.ImagePullPolicy dst.JoinConfiguration.NodeRegistration.ImagePullSerial = restored.JoinConfiguration.NodeRegistration.ImagePullSerial } + if restored.DiskSetup != nil { + if dst.DiskSetup == nil { + dst.DiskSetup = &bootstrapv1.DiskSetup{} + } + dst.DiskSetup = restored.DiskSetup + } } func (dst *KubeadmConfig) ConvertFrom(srcRaw conversion.Hub) error { @@ -228,3 +234,8 @@ func Convert_v1beta1_KubeadmConfigStatus_To_v1alpha4_KubeadmConfigStatus(in *boo // V1Beta2 was added in v1beta1. return autoConvert_v1beta1_KubeadmConfigStatus_To_v1alpha4_KubeadmConfigStatus(in, out, s) } + +func Convert_v1beta1_Partition_To_v1alpha4_Partition(in *bootstrapv1.Partition, out *Partition, s apiconversion.Scope) error { + // Disk Lawas added in v1beta1. + return autoConvert_v1beta1_Partition_To_v1alpha4_Partition(in, out, s) +} diff --git a/internal/apis/bootstrap/kubeadm/v1alpha4/zz_generated.conversion.go b/internal/apis/bootstrap/kubeadm/v1alpha4/zz_generated.conversion.go index 0e1ff11cb46e..cc53005b1476 100644 --- a/internal/apis/bootstrap/kubeadm/v1alpha4/zz_generated.conversion.go +++ b/internal/apis/bootstrap/kubeadm/v1alpha4/zz_generated.conversion.go @@ -335,11 +335,6 @@ func RegisterConversions(s *runtime.Scheme) error { }); err != nil { return err } - if err := s.AddGeneratedConversionFunc((*v1beta1.Partition)(nil), (*Partition)(nil), func(a, b interface{}, scope conversion.Scope) error { - return Convert_v1beta1_Partition_To_v1alpha4_Partition(a.(*v1beta1.Partition), b.(*Partition), scope) - }); err != nil { - return err - } if err := s.AddGeneratedConversionFunc((*SecretFileSource)(nil), (*v1beta1.SecretFileSource)(nil), func(a, b interface{}, scope conversion.Scope) error { return Convert_v1alpha4_SecretFileSource_To_v1beta1_SecretFileSource(a.(*SecretFileSource), b.(*v1beta1.SecretFileSource), scope) }); err != nil { @@ -405,6 +400,11 @@ func RegisterConversions(s *runtime.Scheme) error { }); err != nil { return err } + if err := s.AddConversionFunc((*v1beta1.Partition)(nil), (*Partition)(nil), func(a, b interface{}, scope conversion.Scope) error { + return Convert_v1beta1_Partition_To_v1alpha4_Partition(a.(*v1beta1.Partition), b.(*Partition), scope) + }); err != nil { + return err + } if err := s.AddConversionFunc((*v1beta1.User)(nil), (*User)(nil), func(a, b interface{}, scope conversion.Scope) error { return Convert_v1beta1_User_To_v1alpha4_User(a.(*v1beta1.User), b.(*User), scope) }); err != nil { @@ -712,7 +712,17 @@ func Convert_v1beta1_Discovery_To_v1alpha4_Discovery(in *v1beta1.Discovery, out } func autoConvert_v1alpha4_DiskSetup_To_v1beta1_DiskSetup(in *DiskSetup, out *v1beta1.DiskSetup, s conversion.Scope) error { - out.Partitions = *(*[]v1beta1.Partition)(unsafe.Pointer(&in.Partitions)) + if in.Partitions != nil { + in, out := &in.Partitions, &out.Partitions + *out = make([]v1beta1.Partition, len(*in)) + for i := range *in { + if err := Convert_v1alpha4_Partition_To_v1beta1_Partition(&(*in)[i], &(*out)[i], s); err != nil { + return err + } + } + } else { + out.Partitions = nil + } out.Filesystems = *(*[]v1beta1.Filesystem)(unsafe.Pointer(&in.Filesystems)) return nil } @@ -723,7 +733,17 @@ func Convert_v1alpha4_DiskSetup_To_v1beta1_DiskSetup(in *DiskSetup, out *v1beta1 } func autoConvert_v1beta1_DiskSetup_To_v1alpha4_DiskSetup(in *v1beta1.DiskSetup, out *DiskSetup, s conversion.Scope) error { - out.Partitions = *(*[]Partition)(unsafe.Pointer(&in.Partitions)) + if in.Partitions != nil { + in, out := &in.Partitions, &out.Partitions + *out = make([]Partition, len(*in)) + for i := range *in { + if err := Convert_v1beta1_Partition_To_v1alpha4_Partition(&(*in)[i], &(*out)[i], s); err != nil { + return err + } + } + } else { + out.Partitions = nil + } out.Filesystems = *(*[]Filesystem)(unsafe.Pointer(&in.Filesystems)) return nil } @@ -1142,7 +1162,15 @@ func autoConvert_v1alpha4_KubeadmConfigSpec_To_v1beta1_KubeadmConfigSpec(in *Kub } else { out.Files = nil } - out.DiskSetup = (*v1beta1.DiskSetup)(unsafe.Pointer(in.DiskSetup)) + if in.DiskSetup != nil { + in, out := &in.DiskSetup, &out.DiskSetup + *out = new(v1beta1.DiskSetup) + if err := Convert_v1alpha4_DiskSetup_To_v1beta1_DiskSetup(*in, *out, s); err != nil { + return err + } + } else { + out.DiskSetup = nil + } out.Mounts = *(*[]v1beta1.MountPoints)(unsafe.Pointer(&in.Mounts)) out.PreKubeadmCommands = *(*[]string)(unsafe.Pointer(&in.PreKubeadmCommands)) out.PostKubeadmCommands = *(*[]string)(unsafe.Pointer(&in.PostKubeadmCommands)) @@ -1208,7 +1236,15 @@ func autoConvert_v1beta1_KubeadmConfigSpec_To_v1alpha4_KubeadmConfigSpec(in *v1b } else { out.Files = nil } - out.DiskSetup = (*DiskSetup)(unsafe.Pointer(in.DiskSetup)) + if in.DiskSetup != nil { + in, out := &in.DiskSetup, &out.DiskSetup + *out = new(DiskSetup) + if err := Convert_v1beta1_DiskSetup_To_v1alpha4_DiskSetup(*in, *out, s); err != nil { + return err + } + } else { + out.DiskSetup = nil + } out.Mounts = *(*[]MountPoints)(unsafe.Pointer(&in.Mounts)) out.PreKubeadmCommands = *(*[]string)(unsafe.Pointer(&in.PreKubeadmCommands)) out.PostKubeadmCommands = *(*[]string)(unsafe.Pointer(&in.PostKubeadmCommands)) @@ -1484,16 +1520,12 @@ func Convert_v1alpha4_Partition_To_v1beta1_Partition(in *Partition, out *v1beta1 func autoConvert_v1beta1_Partition_To_v1alpha4_Partition(in *v1beta1.Partition, out *Partition, s conversion.Scope) error { out.Device = in.Device out.Layout = in.Layout + // WARNING: in.DiskLayout requires manual conversion: does not exist in peer-type out.Overwrite = (*bool)(unsafe.Pointer(in.Overwrite)) out.TableType = (*string)(unsafe.Pointer(in.TableType)) return nil } -// Convert_v1beta1_Partition_To_v1alpha4_Partition is an autogenerated conversion function. -func Convert_v1beta1_Partition_To_v1alpha4_Partition(in *v1beta1.Partition, out *Partition, s conversion.Scope) error { - return autoConvert_v1beta1_Partition_To_v1alpha4_Partition(in, out, s) -} - func autoConvert_v1alpha4_SecretFileSource_To_v1beta1_SecretFileSource(in *SecretFileSource, out *v1beta1.SecretFileSource, s conversion.Scope) error { out.Name = in.Name out.Key = in.Key From 56bc6a0e0eb6981c69f611d365fd969ebc8c2140 Mon Sep 17 00:00:00 2001 From: Miltiadis Alexis Date: Thu, 2 Jan 2025 17:45:49 +0100 Subject: [PATCH 2/3] fixup! feat: allow diskSetup to include partition layout Signed-off-by: Miltiadis Alexis --- internal/apis/bootstrap/kubeadm/v1alpha3/conversion.go | 2 +- internal/apis/bootstrap/kubeadm/v1alpha4/conversion.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/internal/apis/bootstrap/kubeadm/v1alpha3/conversion.go b/internal/apis/bootstrap/kubeadm/v1alpha3/conversion.go index cfb6116f4f60..05f0ba135d9f 100644 --- a/internal/apis/bootstrap/kubeadm/v1alpha3/conversion.go +++ b/internal/apis/bootstrap/kubeadm/v1alpha3/conversion.go @@ -258,6 +258,6 @@ func Convert_v1beta1_KubeadmConfigStatus_To_v1alpha3_KubeadmConfigStatus(in *boo } func Convert_v1beta1_Partition_To_v1alpha3_Partition(in *bootstrapv1.Partition, out *Partition, s apiconversion.Scope) error { - // Disk Lawas added in v1beta1. + // DiskLayout was added in v1beta1. return autoConvert_v1beta1_Partition_To_v1alpha3_Partition(in, out, s) } diff --git a/internal/apis/bootstrap/kubeadm/v1alpha4/conversion.go b/internal/apis/bootstrap/kubeadm/v1alpha4/conversion.go index 7aa9cdf0424d..70a35e346200 100644 --- a/internal/apis/bootstrap/kubeadm/v1alpha4/conversion.go +++ b/internal/apis/bootstrap/kubeadm/v1alpha4/conversion.go @@ -236,6 +236,6 @@ func Convert_v1beta1_KubeadmConfigStatus_To_v1alpha4_KubeadmConfigStatus(in *boo } func Convert_v1beta1_Partition_To_v1alpha4_Partition(in *bootstrapv1.Partition, out *Partition, s apiconversion.Scope) error { - // Disk Lawas added in v1beta1. + // Disk Layout was added in v1beta1. return autoConvert_v1beta1_Partition_To_v1alpha4_Partition(in, out, s) } From 356052457dc7047cad16469bd91258aebab7d79e Mon Sep 17 00:00:00 2001 From: Miltiadis Alexis Date: Thu, 2 Jan 2025 18:31:26 +0100 Subject: [PATCH 3/3] fixup! feat: allow diskSetup to include partition layout Signed-off-by: Miltiadis Alexis --- bootstrap/kubeadm/api/v1beta1/kubeadmconfig_types.go | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/bootstrap/kubeadm/api/v1beta1/kubeadmconfig_types.go b/bootstrap/kubeadm/api/v1beta1/kubeadmconfig_types.go index bab106e52817..ed4e7d2a00dc 100644 --- a/bootstrap/kubeadm/api/v1beta1/kubeadmconfig_types.go +++ b/bootstrap/kubeadm/api/v1beta1/kubeadmconfig_types.go @@ -675,17 +675,15 @@ type DiskSetup struct { Filesystems []Filesystem `json:"filesystems,omitempty"` } -// DiskLayout represents an array of partition specifications -type DiskLayout []PartitionSpec - // PartitionSpec defines the size and optional type for a partition type PartitionSpec struct { - // Percentage of disk that partition will take (1-100) + // percentage of disk that partition will take (1-100) // +kubebuilder:validation:Minimum=1 // +kubebuilder:validation:Maximum=100 + // +required Percentage int32 `json:"percentage"` - // PartitionType is the numerical value of the partition type (optional) + // partitionType is the numerical value of the partition type (optional) // +optional PartitionType *int32 `json:"partitionType,omitempty"` } @@ -703,7 +701,7 @@ type Partition struct { // diskLayout specifies the percentage of disk space and partition types. // If specified, this will override the Layout field. // +optional - DiskLayout DiskLayout `json:"diskLayout,omitempty"` + DiskLayout []PartitionSpec `json:"diskLayout,omitempty"` // overwrite describes whether to skip checks and create the partition if a partition or filesystem is found on the device. // Use with caution. Default is 'false'.