Skip to content

Commit 01c0785

Browse files
committed
Add operating system and architecture information to VSphereMachineTemplate status
1 parent b4c6778 commit 01c0785

13 files changed

+826
-14
lines changed

apis/vmware/v1beta1/conversion.go

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

1919
import (
20+
apimachineryconversion "k8s.io/apimachinery/pkg/conversion"
2021
"sigs.k8s.io/controller-runtime/pkg/conversion"
2122

2223
vmwarev1 "sigs.k8s.io/cluster-api-provider-vsphere/apis/vmware/v1beta2"
@@ -111,3 +112,12 @@ func (dst *ProviderServiceAccount) ConvertFrom(srcRaw conversion.Hub) error {
111112

112113
return nil
113114
}
115+
116+
// Convert_v1beta2_VSphereMachineTemplateStatus_To_v1beta1_VSphereMachineTemplateStatus converts v1beta2 VSphereMachineTemplateStatus to v1beta1.
117+
// This is a manual conversion function that handles the NodeInfo field which doesn't exist in v1beta1.
118+
// The NodeInfo field is intentionally dropped during this conversion as v1beta1 doesn't support it.
119+
func Convert_v1beta2_VSphereMachineTemplateStatus_To_v1beta1_VSphereMachineTemplateStatus(in *vmwarev1.VSphereMachineTemplateStatus, out *VSphereMachineTemplateStatus, s apimachineryconversion.Scope) error {
120+
// Call the auto-generated conversion function which handles the Capacity field
121+
// Note: The NodeInfo field from v1beta2 is intentionally dropped as it doesn't exist in v1beta1
122+
return autoConvert_v1beta2_VSphereMachineTemplateStatus_To_v1beta1_VSphereMachineTemplateStatus(in, out, s)
123+
}

apis/vmware/v1beta1/conversion_test.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
"k8s.io/apimachinery/pkg/runtime"
2525
runtimeserializer "k8s.io/apimachinery/pkg/runtime/serializer"
2626
utilconversion "sigs.k8s.io/cluster-api/util/conversion"
27+
"sigs.k8s.io/randfill"
2728

2829
vmwarev1 "sigs.k8s.io/cluster-api-provider-vsphere/apis/vmware/v1beta2"
2930
)
@@ -91,7 +92,16 @@ func VSphereMachineFuzzFuncs(_ runtimeserializer.CodecFactory) []interface{} {
9192
}
9293

9394
func VSphereMachineTemplateFuzzFuncs(_ runtimeserializer.CodecFactory) []interface{} {
94-
return []interface{}{}
95+
return []interface{}{
96+
hubVSphereMachineTemplateStatus,
97+
}
98+
}
99+
100+
func hubVSphereMachineTemplateStatus(in *vmwarev1.VSphereMachineTemplate, c randfill.Continue) {
101+
c.FillNoCustom(in)
102+
// NodeInfo doesn't exist in v1beta1, so it will be lost during hub-spoke-hub conversion.
103+
// Clear it in the hub object before comparison.
104+
in.Status.NodeInfo = vmwarev1.NodeInfo{}
95105
}
96106

97107
func ProviderServiceAccountFuzzFuncs(_ runtimeserializer.CodecFactory) []interface{} {

apis/vmware/v1beta1/zz_generated.conversion.go

Lines changed: 28 additions & 12 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

apis/vmware/v1beta2/vspheremachinetemplate_types.go

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,45 @@ const (
3030
VSphereResourceMemory corev1.ResourceName = "memory"
3131
)
3232

33+
// Architecture represents the CPU architecture of the node.
34+
// Its underlying type is a string and its value can be any of amd64, arm64, s390x, ppc64le.
35+
// +kubebuilder:validation:Enum=amd64;arm64;s390x;ppc64le
36+
// +enum
37+
type Architecture string
38+
39+
const (
40+
// ArchitectureAmd64 is the AMD64 architecture.
41+
ArchitectureAmd64 Architecture = "amd64"
42+
// ArchitectureArm64 is the ARM64 architecture.
43+
ArchitectureArm64 Architecture = "arm64"
44+
// ArchitectureS390x is the S390X architecture.
45+
ArchitectureS390x Architecture = "s390x"
46+
// ArchitecturePpc64le is the PPC64LE architecture.
47+
ArchitecturePpc64le Architecture = "ppc64le"
48+
)
49+
50+
const (
51+
// VMwareSystemOSArchPropertyKey is the key for the architecture property in the
52+
// ClusterVirtualMachineImage's vmwareSystemProperties. This is defined by VM Operator.
53+
VMwareSystemOSArchPropertyKey = "vmware-system.tkr.os-arch"
54+
// VMwareSystemOSTypePropertyKey is the key for the operating system type property in the
55+
// ClusterVirtualMachineImage's vmwareSystemProperties. This is defined by VM Operator.
56+
VMwareSystemOSTypePropertyKey = "vmware-system.tkr.os-type"
57+
)
58+
59+
// OperatingSystem represents the operating system of the node.
60+
// Its underlying type is a string and its value can be any of linux, windows.
61+
// +kubebuilder:validation:Enum=linux;windows
62+
// +enum
63+
type OperatingSystem string
64+
65+
const (
66+
// OperatingSystemLinux is the Linux operating system.
67+
OperatingSystemLinux OperatingSystem = "linux"
68+
// OperatingSystemWindows is the Windows operating system.
69+
OperatingSystemWindows OperatingSystem = "windows"
70+
)
71+
3372
// VSphereMachineTemplateSpec defines the desired state of VSphereMachineTemplate.
3473
type VSphereMachineTemplateSpec struct {
3574
// template defines the desired state of VSphereMachineTemplate.
@@ -43,6 +82,11 @@ type VSphereMachineTemplateStatus struct {
4382
// https://github.com/kubernetes-sigs/cluster-api/blob/main/docs/proposals/20210310-opt-in-autoscaling-from-zero.md
4483
// +optional
4584
Capacity corev1.ResourceList `json:"capacity,omitempty"`
85+
// nodeInfo defines the node's architecture and operating system.
86+
// This value is used for autoscaling from zero operations as defined in:
87+
// https://github.com/kubernetes-sigs/cluster-api/blob/main/docs/proposals/20210310-opt-in-autoscaling-from-zero.md#implementation-detailsnotesconstraints
88+
// +optional
89+
NodeInfo NodeInfo `json:"nodeInfo,omitempty,omitzero"`
4690
}
4791

4892
// +kubebuilder:object:root=true
@@ -65,6 +109,19 @@ type VSphereMachineTemplate struct {
65109
Status VSphereMachineTemplateStatus `json:"status,omitempty"`
66110
}
67111

112+
// NodeInfo contains information about the node's architecture and operating system.
113+
// +kubebuilder:validation:MinProperties=1
114+
type NodeInfo struct {
115+
// architecture is the CPU architecture of the node.
116+
// Its underlying type is a string and its value can be any of amd64, arm64, s390x, ppc64le.
117+
// +optional
118+
Architecture Architecture `json:"architecture,omitempty"`
119+
// operatingSystem is a string representing the operating system of the node.
120+
// This may be a string like 'linux' or 'windows'.
121+
// +optional
122+
OperatingSystem OperatingSystem `json:"operatingSystem,omitempty"`
123+
}
124+
68125
// +kubebuilder:object:root=true
69126

70127
// VSphereMachineTemplateList contains a list of VSphereMachineTemplate.

apis/vmware/v1beta2/zz_generated.deepcopy.go

Lines changed: 16 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

config/rbac/role.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,7 @@ rules:
241241
- apiGroups:
242242
- vmoperator.vmware.com
243243
resources:
244+
- clustervirtualmachineimages
244245
- virtualmachineclasses
245246
verbs:
246247
- get

config/supervisor/crd/bases/vmware.infrastructure.cluster.x-k8s.io_vspheremachinetemplates.yaml

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -745,6 +745,32 @@ spec:
745745
This value is used for autoscaling from zero operations as defined in:
746746
https://github.com/kubernetes-sigs/cluster-api/blob/main/docs/proposals/20210310-opt-in-autoscaling-from-zero.md
747747
type: object
748+
nodeInfo:
749+
description: |-
750+
nodeInfo defines the node's architecture and operating system.
751+
This value is used for autoscaling from zero operations as defined in:
752+
https://github.com/kubernetes-sigs/cluster-api/blob/main/docs/proposals/20210310-opt-in-autoscaling-from-zero.md#implementation-detailsnotesconstraints
753+
minProperties: 1
754+
properties:
755+
architecture:
756+
description: |-
757+
architecture is the CPU architecture of the node.
758+
Its underlying type is a string and its value can be any of amd64, arm64, s390x, ppc64le.
759+
enum:
760+
- amd64
761+
- arm64
762+
- s390x
763+
- ppc64le
764+
type: string
765+
operatingSystem:
766+
description: |-
767+
operatingSystem is a string representing the operating system of the node.
768+
This may be a string like 'linux' or 'windows'.
769+
enum:
770+
- linux
771+
- windows
772+
type: string
773+
type: object
748774
type: object
749775
type: object
750776
served: true

controllers/vmware/vspheremachinetemplate_controller.go

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package vmware
1818

1919
import (
2020
"context"
21+
"fmt"
2122

2223
"github.com/pkg/errors"
2324
corev1 "k8s.io/api/core/v1"
@@ -41,6 +42,7 @@ import (
4142
// +kubebuilder:rbac:groups=vmware.infrastructure.cluster.x-k8s.io,resources=vspheremachinetemplates,verbs=get;list;watch;create;update;patch;delete
4243
// +kubebuilder:rbac:groups=vmware.infrastructure.cluster.x-k8s.io,resources=vspheremachinetemplates/status,verbs=get;update;patch
4344
// +kubebuilder:rbac:groups=vmoperator.vmware.com,resources=virtualmachineclasses,verbs=get;list;watch
45+
// +kubebuilder:rbac:groups=vmoperator.vmware.com,resources=clustervirtualmachineimages,verbs=get;list;watch
4446

4547
// AddVSphereMachineTemplateControllerToManager adds the machine template controller to the provided
4648
// manager.
@@ -107,9 +109,79 @@ func (r *vSphereMachineTemplateReconciler) Reconcile(ctx context.Context, req ct
107109
vSphereMachineTemplate.Status.Capacity[vmwarev1.VSphereResourceMemory] = vmClass.Spec.Hardware.Memory
108110
}
109111

112+
// retrieve the os and arch info from the ClusterVirtualMachineImage
113+
os, arch := getOSAndArchFromClusterVirtualMachineImage(ctx, r.Client, vSphereMachineTemplate.Spec.Template.Spec.ImageName)
114+
115+
if validOS := normalizeOperatingSystem(os); validOS != "" {
116+
vSphereMachineTemplate.Status.NodeInfo.OperatingSystem = validOS
117+
}
118+
if validArch := normalizeArchitecture(arch); validArch != "" {
119+
vSphereMachineTemplate.Status.NodeInfo.Architecture = validArch
120+
}
121+
fmt.Println("vSphereMachineTemplate.Status.NodeInfo.OperatingSystem", vSphereMachineTemplate.Status.NodeInfo.OperatingSystem)
122+
fmt.Println("vSphereMachineTemplate.Status.NodeInfo.Architecture", vSphereMachineTemplate.Status.NodeInfo.Architecture)
123+
110124
return reconcile.Result{}, patchHelper.Patch(ctx, vSphereMachineTemplate)
111125
}
112126

127+
// normalizeOperatingSystem converts the OS string from CVMI to a valid OperatingSystem constant.
128+
// Returns empty string if the value is not recognized.
129+
func normalizeOperatingSystem(os string) vmwarev1.OperatingSystem {
130+
switch os {
131+
case "linux":
132+
return vmwarev1.OperatingSystemLinux
133+
case "windows":
134+
return vmwarev1.OperatingSystemWindows
135+
default:
136+
return ""
137+
}
138+
}
139+
140+
// normalizeArchitecture converts the architecture string from CVMI to a valid Architecture constant.
141+
// Returns empty string if the value is not recognized.
142+
func normalizeArchitecture(arch string) vmwarev1.Architecture {
143+
switch arch {
144+
case "amd64":
145+
return vmwarev1.ArchitectureAmd64
146+
case "arm64":
147+
return vmwarev1.ArchitectureArm64
148+
case "s390x":
149+
return vmwarev1.ArchitectureS390x
150+
case "ppc64le":
151+
return vmwarev1.ArchitecturePpc64le
152+
default:
153+
return ""
154+
}
155+
}
156+
157+
func getOSAndArchFromClusterVirtualMachineImage(ctx context.Context, c client.Client, imageName string) (string, string) {
158+
fmt.Println("run here", "11111")
159+
if imageName == "" {
160+
return "", ""
161+
}
162+
fmt.Println("run here", "2222")
163+
// Try to fetch the ClusterVirtualMachineImage with the given name
164+
cvmi := &vmoprvhub.ClusterVirtualMachineImage{}
165+
if err := c.Get(ctx, client.ObjectKey{Name: imageName}, cvmi); err != nil {
166+
fmt.Println("run here", "3333", "error:", err)
167+
return "", ""
168+
}
169+
170+
fmt.Println("cvmi.Status.VMwareSystemProperties", cvmi.Status.VMwareSystemProperties)
171+
// Extract OS type and architecture from vmwareSystemProperties
172+
var osType, osArch string
173+
for _, prop := range cvmi.Status.VMwareSystemProperties {
174+
switch prop.Key {
175+
case vmwarev1.VMwareSystemOSTypePropertyKey:
176+
osType = prop.Value
177+
case vmwarev1.VMwareSystemOSArchPropertyKey:
178+
osArch = prop.Value
179+
}
180+
}
181+
182+
return osType, osArch
183+
}
184+
113185
// enqueueVirtualMachineClassToVSphereMachineTemplateRequests returns a list of VSphereMachineTemplate reconcile requests
114186
// that use a specific VirtualMachineClass.
115187
func (r *vSphereMachineTemplateReconciler) enqueueVirtualMachineClassToVSphereMachineTemplateRequests(ctx context.Context, virtualMachineClass client.Object) []reconcile.Request {

0 commit comments

Comments
 (0)