Skip to content

Commit fca7783

Browse files
committed
Switch to systemd describe() for operating system description
1 parent 8697ad4 commit fca7783

File tree

9 files changed

+286
-26
lines changed

9 files changed

+286
-26
lines changed

api/v1alpha1/hypervisor_types.go

Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,15 +66,50 @@ type HyperVisorUpdateStatus struct {
6666
Retry int `json:"retry"`
6767
}
6868

69-
// HypervisorStatus defines the observed state of Hypervisor
70-
type HypervisorStatus struct {
69+
type OperatingSystemStatus struct {
7170
// Represents the Operating System version.
72-
Version string `json:"version"`
71+
Version string `json:"version,omitempty"`
72+
73+
// PrettyVersion
74+
PrettyVersion string `json:"prettyVersion,omitempty"`
75+
76+
// KernelName
77+
KernelName string `json:"kernelName,omitempty"`
78+
79+
// KernelRelease
80+
KernelRelease string `json:"kernelRelease,omitempty"`
81+
82+
// KernelVersion
83+
KernelVersion string `json:"kernelVersion,omitempty"`
84+
85+
// HardwareVendor
86+
HardwareVendor string `json:"hardwareVendor,omitempty"`
87+
88+
// HardwareModel
89+
HardwareModel string `json:"hardwareModel,omitempty"`
7390

91+
// HardwareSerial
92+
HardwareSerial string `json:"hardwareSerial,omitempty"`
93+
94+
// FirmwareVersion
95+
FirmwareVersion string `json:"firmwareVersion,omitempty"`
96+
97+
// FirmwareVendor
98+
FirmwareVendor string `json:"firmwareVendor,omitempty"`
99+
100+
// FirmwareDate
101+
FirmwareDate metav1.Time `json:"firmwareDate,omitempty"`
102+
}
103+
104+
// HypervisorStatus defines the observed state of Hypervisor
105+
type HypervisorStatus struct {
74106
// +kubebuilder:default:=unknown
75107
// Represents the LibVirt version.
76108
LibVirtVersion string `json:"libVirtVersion"`
77109

110+
// Represents the Operating System status.
111+
OperatingSystem OperatingSystemStatus `json:"operatingSystem"`
112+
78113
// Represents the Hypervisor update status.
79114
Update HyperVisorUpdateStatus `json:"updateStatus"`
80115

@@ -96,8 +131,10 @@ type HypervisorStatus struct {
96131

97132
// +kubebuilder:object:root=true
98133
// +kubebuilder:subresource:status
99-
// +kubebuilder:printcolumn:JSONPath=".status.version",name="Version",type="string"
134+
// +kubebuilder:printcolumn:JSONPath=".status.operatingSystem.prettyVersion",name="Version",type="string"
100135
// +kubebuilder:printcolumn:JSONPath=".status.numInstances",name="Instances",type="integer"
136+
// +kubebuilder:printcolumn:JSONPath=".status.operatingSystem.hardwareModel",name="Hardware",type="string"
137+
// +kubebuilder:printcolumn:JSONPath=".status.operatingSystem.kernelRelease",name="Kernel",type="string"
101138
// +kubebuilder:printcolumn:JSONPath=".metadata.creationTimestamp",name="Age",type="date"
102139

103140
// Hypervisor is the Schema for the hypervisors API

api/v1alpha1/zz_generated.deepcopy.go

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

charts/kvm-node-agent/crds/hypervisor-crd.yaml

Lines changed: 46 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,18 @@ spec:
1414
scope: Namespaced
1515
versions:
1616
- additionalPrinterColumns:
17-
- jsonPath: .status.version
17+
- jsonPath: .status.operatingSystem.prettyVersion
1818
name: Version
1919
type: string
2020
- jsonPath: .status.numInstances
2121
name: Instances
2222
type: integer
23+
- jsonPath: .status.operatingSystem.hardwareModel
24+
name: Hardware
25+
type: string
26+
- jsonPath: .status.operatingSystem.kernelRelease
27+
name: Kernel
28+
type: string
2329
- jsonPath: .metadata.creationTimestamp
2430
name: Age
2531
type: date
@@ -169,6 +175,44 @@ spec:
169175
default: 0
170176
description: Represent the num of instances
171177
type: integer
178+
operatingSystem:
179+
description: Represents the Operating System status.
180+
properties:
181+
firmwareDate:
182+
description: FirmwareDate
183+
format: date-time
184+
type: string
185+
firmwareVendor:
186+
description: FirmwareVendor
187+
type: string
188+
firmwareVersion:
189+
description: FirmwareVersion
190+
type: string
191+
hardwareModel:
192+
description: HardwareModel
193+
type: string
194+
hardwareSerial:
195+
description: HardwareSerial
196+
type: string
197+
hardwareVendor:
198+
description: HardwareVendor
199+
type: string
200+
kernelName:
201+
description: KernelName
202+
type: string
203+
kernelRelease:
204+
description: KernelRelease
205+
type: string
206+
kernelVersion:
207+
description: KernelVersion
208+
type: string
209+
prettyVersion:
210+
description: PrettyVersion
211+
type: string
212+
version:
213+
description: Represents the Operating System version.
214+
type: string
215+
type: object
172216
specHash:
173217
type: string
174218
updateStatus:
@@ -191,15 +235,12 @@ spec:
191235
- inProgress
192236
- retry
193237
type: object
194-
version:
195-
description: Represents the Operating System version.
196-
type: string
197238
required:
198239
- libVirtVersion
199240
- node
200241
- numInstances
242+
- operatingSystem
201243
- updateStatus
202-
- version
203244
type: object
204245
type: object
205246
served: true

config/crd/bases/kvm.cloud.sap_hypervisors.yaml

Lines changed: 46 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,18 @@ spec:
1515
scope: Namespaced
1616
versions:
1717
- additionalPrinterColumns:
18-
- jsonPath: .status.version
18+
- jsonPath: .status.operatingSystem.prettyVersion
1919
name: Version
2020
type: string
2121
- jsonPath: .status.numInstances
2222
name: Instances
2323
type: integer
24+
- jsonPath: .status.operatingSystem.hardwareModel
25+
name: Hardware
26+
type: string
27+
- jsonPath: .status.operatingSystem.kernelRelease
28+
name: Kernel
29+
type: string
2430
- jsonPath: .metadata.creationTimestamp
2531
name: Age
2632
type: date
@@ -170,6 +176,44 @@ spec:
170176
default: 0
171177
description: Represent the num of instances
172178
type: integer
179+
operatingSystem:
180+
description: Represents the Operating System status.
181+
properties:
182+
firmwareDate:
183+
description: FirmwareDate
184+
format: date-time
185+
type: string
186+
firmwareVendor:
187+
description: FirmwareVendor
188+
type: string
189+
firmwareVersion:
190+
description: FirmwareVersion
191+
type: string
192+
hardwareModel:
193+
description: HardwareModel
194+
type: string
195+
hardwareSerial:
196+
description: HardwareSerial
197+
type: string
198+
hardwareVendor:
199+
description: HardwareVendor
200+
type: string
201+
kernelName:
202+
description: KernelName
203+
type: string
204+
kernelRelease:
205+
description: KernelRelease
206+
type: string
207+
kernelVersion:
208+
description: KernelVersion
209+
type: string
210+
prettyVersion:
211+
description: PrettyVersion
212+
type: string
213+
version:
214+
description: Represents the Operating System version.
215+
type: string
216+
type: object
173217
specHash:
174218
type: string
175219
updateStatus:
@@ -192,15 +236,12 @@ spec:
192236
- inProgress
193237
- retry
194238
type: object
195-
version:
196-
description: Represents the Operating System version.
197-
type: string
198239
required:
199240
- libVirtVersion
200241
- node
201242
- numInstances
243+
- operatingSystem
202244
- updateStatus
203-
- version
204245
type: object
205246
type: object
206247
served: true

internal/controller/hypervisor_controller.go

Lines changed: 33 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"errors"
2323
"fmt"
2424
"os"
25+
"strings"
2526
"time"
2627

2728
"k8s.io/apimachinery/pkg/api/meta"
@@ -43,13 +44,13 @@ import (
4344
// HypervisorReconciler reconciles a Hypervisor object
4445
type HypervisorReconciler struct {
4546
client.Client
46-
Scheme *runtime.Scheme
47-
libvirt libvirt.Interface
48-
systemd systemd.Interface
49-
libvirtVersion string
50-
OperatingSystemVersion string
51-
evacuateOnReboot bool
52-
migrationJobs map[string]context.Context
47+
Scheme *runtime.Scheme
48+
libvirt libvirt.Interface
49+
systemd systemd.Interface
50+
libvirtVersion string
51+
osDescriptor *systemd.Descriptor
52+
evacuateOnReboot bool
53+
migrationJobs map[string]context.Context
5354
}
5455

5556
const (
@@ -227,16 +228,32 @@ func (r *HypervisorReconciler) Reconcile(ctx context.Context, req ctrl.Request)
227228
Message: fmt.Sprintf("%s: %s, %s", unit.Name, unit.ActiveState, unit.LoadState),
228229
})
229230
}
230-
}
231231

232-
if hypervisor.Status.Version != r.OperatingSystemVersion {
233-
hypervisor.Status.Version = r.OperatingSystemVersion
232+
if r.osDescriptor != nil && hypervisor.Status.OperatingSystem.Version == "" {
233+
for _, line := range r.osDescriptor.OperatingSystemReleaseData {
234+
switch strings.Split(line, "=")[0] {
235+
case "PRETTY_NAME":
236+
hypervisor.Status.OperatingSystem.PrettyVersion = strings.Split(line, "=")[1]
237+
case "GARDENLINUX_VERSION":
238+
hypervisor.Status.OperatingSystem.Version = strings.Split(line, "=")[1]
239+
}
240+
}
241+
hypervisor.Status.OperatingSystem.KernelVersion = r.osDescriptor.KernelVersion
242+
hypervisor.Status.OperatingSystem.KernelRelease = r.osDescriptor.KernelRelease
243+
hypervisor.Status.OperatingSystem.KernelName = r.osDescriptor.KernelName
244+
hypervisor.Status.OperatingSystem.HardwareVendor = r.osDescriptor.HardwareVendor
245+
hypervisor.Status.OperatingSystem.HardwareModel = r.osDescriptor.HardwareModel
246+
hypervisor.Status.OperatingSystem.HardwareSerial = r.osDescriptor.HardwareSerial
247+
hypervisor.Status.OperatingSystem.FirmwareVersion = r.osDescriptor.FirmwareVersion
248+
hypervisor.Status.OperatingSystem.FirmwareVendor = r.osDescriptor.FirmwareVendor
249+
hypervisor.Status.OperatingSystem.FirmwareDate = metav1.NewTime(time.UnixMicro(r.osDescriptor.FirmwareDate))
250+
}
234251
}
235252

236253
// Reconcile operating system update
237254
if hypervisor.Spec.OperatingSystemVersion != "" &&
238255
// only update if the version is different to current running version
239-
hypervisor.Spec.OperatingSystemVersion != hypervisor.Status.Version &&
256+
hypervisor.Spec.OperatingSystemVersion != hypervisor.Status.OperatingSystem.Version &&
240257
// only update if the version is different to the installed version
241258
hypervisor.Spec.OperatingSystemVersion != hypervisor.Status.Update.Installed {
242259

@@ -322,7 +339,6 @@ func (r *HypervisorReconciler) Reconcile(ctx context.Context, req ctrl.Request)
322339
func (r *HypervisorReconciler) SetupWithManager(mgr ctrl.Manager) error {
323340
ctx := context.Background()
324341
emulate := os.Getenv("EMULATE")
325-
r.OperatingSystemVersion = sys.GetOSVersion(ctx)
326342
r.migrationJobs = make(map[string]context.Context)
327343

328344
var err error
@@ -337,6 +353,11 @@ func (r *HypervisorReconciler) SetupWithManager(mgr ctrl.Manager) error {
337353
}
338354
}
339355

356+
r.osDescriptor, err = r.systemd.Describe(ctx)
357+
if err != nil {
358+
return fmt.Errorf("unable to get systemd hostname describe(): %w", err)
359+
}
360+
340361
return ctrl.NewControllerManagedBy(mgr).
341362
For(&kvmv1alpha1.Hypervisor{}).
342363
Complete(r)

internal/controller/hypervisor_controller_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,9 @@ var _ = Describe("Hypervisor Controller", func() {
120120
},
121121
}, nil
122122
},
123+
DescribeFunc: func(ctx context.Context) (*systemd.Descriptor, error) {
124+
return nil, nil
125+
},
123126
},
124127
}
125128

internal/systemd/interface.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,36 @@ import (
2727
"github.com/cobaltcode-dev/kvm-node-agent/api/v1alpha1"
2828
)
2929

30+
type Descriptor struct {
31+
Hostname string `json:"Hostname,omitempty"`
32+
StaticHostname string `json:"StaticHostname,omitempty"`
33+
PrettyHostname interface{} `json:"PrettyHostname,omitempty"`
34+
DefaultHostname string `json:"DefaultHostname,omitempty"`
35+
HostnameSource string `json:"HostnameSource,omitempty"`
36+
IconName string `json:"IconName,omitempty"`
37+
Chassis string `json:"Chassis,omitempty"`
38+
Deployment interface{} `json:"Deployment,omitempty"`
39+
Location interface{} `json:"Location,omitempty"`
40+
KernelName string `json:"KernelName,omitempty"`
41+
KernelRelease string `json:"KernelRelease,omitempty"`
42+
KernelVersion string `json:"KernelVersion,omitempty"`
43+
OperatingSystemPrettyName string `json:"PrettyName,omitempty"`
44+
OperatingSystemCPEName interface{} `json:"OperatingSystemCPEName,omitempty"`
45+
OperatingSystemHomeURL string `json:"OperatingSystemHomeURL,omitempty"`
46+
OperatingSystemReleaseData []string `json:"OperatingSystemReleaseData,omitempty"`
47+
MachineInformationData []interface{} `json:"MachineInformationData,omitempty"`
48+
HardwareVendor string `json:"HardwareVendor,omitempty"`
49+
HardwareModel string `json:"HardwareModel,omitempty"`
50+
HardwareSerial string `json:"HardwareSerial,omitempty"`
51+
FirmwareVersion string `json:"FirmwareVersion,omitempty"`
52+
FirmwareVendor string `json:"FirmwareVendor,omitempty"`
53+
FirmwareDate int64 `json:"FirmwareDate,omitempty"`
54+
MachineID string `json:"MachineID,omitempty"`
55+
BootID string `json:"BootID,omitempty"`
56+
ProductUUID string `json:"ProductUUID,omitempty"`
57+
VSockCID interface{} `json:"VSockCID,omitempty"`
58+
}
59+
3060
type Interface interface {
3161
// Close closes the connection to the systemd D-Bus API.
3262
Close()
@@ -51,4 +81,7 @@ type Interface interface {
5181

5282
// DisableShutdownInhibit disables the shutdown inhibition
5383
DisableShutdownInhibit() error
84+
85+
// Describe returns hostname and related machine metadata
86+
Describe(ctx context.Context) (*Descriptor, error)
5487
}

0 commit comments

Comments
 (0)