Skip to content

Commit

Permalink
Add InstanceType to cloudinit metadata
Browse files Browse the repository at this point in the history
Signed-off-by: Ryan Hallisey <[email protected]>
  • Loading branch information
rthallisey committed Jan 12, 2022
1 parent bf73abb commit 6f05a5a
Show file tree
Hide file tree
Showing 9 changed files with 58 additions and 11 deletions.
4 changes: 4 additions & 0 deletions api/openapi-spec/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -15876,6 +15876,10 @@
"description": "EvictionStrategy can be set to \"LiveMigrate\" if the VirtualMachineInstance should be migrated instead of shut-off in case of a node drain.",
"type": "string"
},
"flavor": {
"description": "Flavor references the name of a VirtualMachineFlavor or VirtualMachineClusterFlavor",
"type": "string"
},
"hostname": {
"description": "Specifies the hostname of the vmi If not specified, the hostname will be set to the name of the vmi, if dhcp or cloud-init is configured properly.",
"type": "string"
Expand Down
21 changes: 15 additions & 6 deletions pkg/cloud-init/cloud-init.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,13 @@ type PublicSSHKey struct {
}

type NoCloudMetadata struct {
InstanceType string `json:"instance-type,omitempty"`
InstanceID string `json:"instance-id"`
LocalHostname string `json:"local-hostname,omitempty"`
}

type ConfigDriveMetadata struct {
InstanceType string `json:"instance_type,omitempty"`
InstanceID string `json:"instance_id"`
LocalHostname string `json:"local_hostname,omitempty"`
Hostname string `json:"hostname,omitempty"`
Expand Down Expand Up @@ -120,7 +122,7 @@ func ReadCloudInitVolumeDataSource(vmi *v1.VirtualMachineInstance, secretSourceD
}

cloudInitData, err = readCloudInitNoCloudSource(volume.CloudInitNoCloud)
cloudInitData.NoCloudMetaData = readCloudInitNoCloudMetaData(vmi.Name, hostname, vmi.Namespace)
cloudInitData.NoCloudMetaData = readCloudInitNoCloudMetaData(vmi.Name, hostname, vmi.Namespace, vmi.Spec.Flavor)
cloudInitData.VolumeName = volume.Name
return cloudInitData, err
}
Expand All @@ -132,7 +134,7 @@ func ReadCloudInitVolumeDataSource(vmi *v1.VirtualMachineInstance, secretSourceD
}

cloudInitData, err = readCloudInitConfigDriveSource(volume.CloudInitConfigDrive)
cloudInitData.ConfigDriveMetaData = readCloudInitConfigDriveMetaData(string(vmi.UID), vmi.Name, hostname, vmi.Namespace, keys)
cloudInitData.ConfigDriveMetaData = readCloudInitConfigDriveMetaData(string(vmi.UID), vmi.Name, hostname, vmi.Namespace, keys, vmi.Spec.Flavor)
cloudInitData.VolumeName = volume.Name
return cloudInitData, err
}
Expand Down Expand Up @@ -355,15 +357,17 @@ func readCloudInitConfigDriveSource(source *v1.CloudInitConfigDriveSource) (*Clo
}, nil
}

func readCloudInitNoCloudMetaData(name, hostname, namespace string) *NoCloudMetadata {
func readCloudInitNoCloudMetaData(name, hostname, namespace string, instanceType string) *NoCloudMetadata {
return &NoCloudMetadata{
InstanceType: instanceType,
InstanceID: fmt.Sprintf("%s.%s", name, namespace),
LocalHostname: hostname,
}
}

func readCloudInitConfigDriveMetaData(uid, name, hostname, namespace string, keys map[string]string) *ConfigDriveMetadata {
func readCloudInitConfigDriveMetaData(uid, name, hostname, namespace string, keys map[string]string, instanceType string) *ConfigDriveMetadata {
return &ConfigDriveMetadata{
InstanceType: instanceType,
UUID: uid,
InstanceID: fmt.Sprintf("%s.%s", name, namespace),
Hostname: hostname,
Expand Down Expand Up @@ -524,7 +528,7 @@ func GenerateEmptyIso(vmiName string, namespace string, data *CloudInitData, siz
return nil
}

func GenerateLocalData(vmiName string, namespace string, data *CloudInitData) error {
func GenerateLocalData(vmiName string, namespace string, instanceType string, data *CloudInitData) error {
precond.MustNotBeEmpty(vmiName)
precond.MustNotBeNil(data)

Expand All @@ -548,6 +552,9 @@ func GenerateLocalData(vmiName string, namespace string, data *CloudInitData) er
data.NoCloudMetaData = &NoCloudMetadata{
InstanceID: fmt.Sprintf("%s.%s", vmiName, namespace),
}
if instanceType != "" {
data.NoCloudMetaData.InstanceType = instanceType
}
}
metaData, err = json.Marshal(data.NoCloudMetaData)
if err != nil {
Expand All @@ -565,8 +572,10 @@ func GenerateLocalData(vmiName string, namespace string, data *CloudInitData) er
data.ConfigDriveMetaData = &ConfigDriveMetadata{
InstanceID: fmt.Sprintf("%s.%s", vmiName, namespace),
}
if instanceType != "" {
data.ConfigDriveMetaData.InstanceType = instanceType
}
}

data.ConfigDriveMetaData.Devices = data.DevicesData
metaData, err = json.Marshal(data.ConfigDriveMetaData)
if err != nil {
Expand Down
15 changes: 11 additions & 4 deletions pkg/cloud-init/cloud-init_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ var _ = Describe("CloudInit", func() {
Context("verify meta-data model", func() {
It("should match the generated configdrive metadata", func() {
exampleJSONParsed := `{
"instance_type": "fake.fake-flavor",
"instance_id": "fake.fake-namespace",
"local_hostname": "fake",
"uuid": "5d307ca9-b3ef-428c-8861-06e72d69f223",
Expand Down Expand Up @@ -129,6 +130,7 @@ var _ = Describe("CloudInit", func() {
}

metadataStruct := ConfigDriveMetadata{
InstanceType: "fake.fake-flavor",
InstanceID: "fake.fake-namespace",
LocalHostname: "fake",
UUID: "5d307ca9-b3ef-428c-8861-06e72d69f223",
Expand All @@ -141,11 +143,13 @@ var _ = Describe("CloudInit", func() {
})
It("should match the generated nocloud metadata", func() {
exampleJSONParsed := `{
"instance-type": "fake.fake-flavor",
"instance-id": "fake.fake-namespace",
"local-hostname": "fake"
}`

metadataStruct := NoCloudMetadata{
InstanceType: "fake.fake-flavor",
InstanceID: "fake.fake-namespace",
LocalHostname: "fake",
}
Expand Down Expand Up @@ -192,14 +196,15 @@ var _ = Describe("CloudInit", func() {
})

It("should fail local data generation", func() {
flavor := "fake-flavor"
namespace := "fake-namespace"
domain := "fake-domain"
userData := "fake\nuser\ndata\n"
source := &v1.CloudInitNoCloudSource{
UserDataBase64: base64.StdEncoding.EncodeToString([]byte(userData)),
}
cloudInitData, _ := readCloudInitNoCloudSource(source)
err := GenerateLocalData(domain, namespace, cloudInitData)
err := GenerateLocalData(domain, namespace, flavor, cloudInitData)
Expect(err).To(HaveOccurred())
Expect(timedOut).To(BeTrue())
})
Expand All @@ -216,10 +221,11 @@ var _ = Describe("CloudInit", func() {

Describe("A new VirtualMachineInstance definition", func() {
verifyCloudInitData := func(cloudInitData *CloudInitData) {
flavor := "fake-flavor"
namespace := "fake-namespace"
domain := "fake-domain"

err := GenerateLocalData(domain, namespace, cloudInitData)
err := GenerateLocalData(domain, namespace, flavor, cloudInitData)
Expect(err).ToNot(HaveOccurred())

// verify iso is created
Expand Down Expand Up @@ -505,6 +511,7 @@ var _ = Describe("CloudInit", func() {

Describe("GenerateLocalData", func() {
It("should cleanly run twice", func() {
flavor := "fake-flavor"
namespace := "fake-namespace"
domain := "fake-domain"
userData := "fake\nuser\ndata\n"
Expand All @@ -513,9 +520,9 @@ var _ = Describe("CloudInit", func() {
}
cloudInitData, err := readCloudInitNoCloudSource(source)
Expect(err).NotTo(HaveOccurred())
err = GenerateLocalData(domain, namespace, cloudInitData)
err = GenerateLocalData(domain, namespace, flavor, cloudInitData)
Expect(err).NotTo(HaveOccurred())
err = GenerateLocalData(domain, namespace, cloudInitData)
err = GenerateLocalData(domain, namespace, flavor, cloudInitData)
Expect(err).NotTo(HaveOccurred())
})
})
Expand Down
1 change: 1 addition & 0 deletions pkg/flavor/flavor.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ func (m *methods) FindProfile(vm *virtv1.VirtualMachine) (*flavorv1alpha1.Virtua
func (m *methods) ApplyToVmi(field *k8sfield.Path, profile *flavorv1alpha1.VirtualMachineFlavorProfile, vmiSpec *virtv1.VirtualMachineInstanceSpec) Conflicts {
var conflicts Conflicts

vmiSpec.Flavor = profile.Name
conflicts = append(conflicts, applyCpu(field, profile, vmiSpec)...)

return conflicts
Expand Down
2 changes: 1 addition & 1 deletion pkg/virt-launcher/virtwrap/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -432,7 +432,7 @@ func (l *LibvirtDomainManager) generateSomeCloudInitISO(vmi *v1.VirtualMachineIn
if size != 0 {
err = cloudinit.GenerateEmptyIso(vmi.Name, vmi.Namespace, cloudInitDataStore, size)
} else {
err = cloudinit.GenerateLocalData(vmi.Name, vmi.Namespace, cloudInitDataStore)
err = cloudinit.GenerateLocalData(vmi.Name, vmi.Namespace, vmi.Spec.Flavor, cloudInitDataStore)
}
if err != nil {
return fmt.Errorf("generating local cloud-init data failed: %v", err)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4838,6 +4838,10 @@ var CRDsValidation map[string]string = map[string]string{
VirtualMachineInstance should be migrated instead of shut-off
in case of a node drain.
type: string
flavor:
description: Flavor references the name of a VirtualMachineFlavor
or VirtualMachineClusterFlavor
type: string
hostname:
description: Specifies the hostname of the vmi If not specified,
the hostname will be set to the name of the vmi, if dhcp or cloud-init
Expand Down Expand Up @@ -7821,6 +7825,9 @@ var CRDsValidation map[string]string = map[string]string{
description: EvictionStrategy can be set to "LiveMigrate" if the VirtualMachineInstance
should be migrated instead of shut-off in case of a node drain.
type: string
flavor:
description: Flavor references the name of a VirtualMachineFlavor or VirtualMachineClusterFlavor
type: string
hostname:
description: Specifies the hostname of the vmi If not specified, the hostname
will be set to the name of the vmi, if dhcp or cloud-init is configured
Expand Down Expand Up @@ -11779,6 +11786,10 @@ var CRDsValidation map[string]string = map[string]string{
VirtualMachineInstance should be migrated instead of shut-off
in case of a node drain.
type: string
flavor:
description: Flavor references the name of a VirtualMachineFlavor
or VirtualMachineClusterFlavor
type: string
hostname:
description: Specifies the hostname of the vmi If not specified,
the hostname will be set to the name of the vmi, if dhcp or cloud-init
Expand Down Expand Up @@ -18727,6 +18738,10 @@ var CRDsValidation map[string]string = map[string]string{
if the VirtualMachineInstance should be migrated instead
of shut-off in case of a node drain.
type: string
flavor:
description: Flavor references the name of a VirtualMachineFlavor
or VirtualMachineClusterFlavor
type: string
hostname:
description: Specifies the hostname of the vmi If not
specified, the hostname will be set to the name of
Expand Down
3 changes: 3 additions & 0 deletions staging/src/kubevirt.io/api/core/v1/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,9 @@ type VirtualMachineInstanceSpec struct {
// +listType=atomic
// +optional
AccessCredentials []AccessCredential `json:"accessCredentials,omitempty"`

// Flavor references the name of a VirtualMachineFlavor or VirtualMachineClusterFlavor
Flavor string `json:"flavor,omitempty" optional:"true"`
}

func (vmiSpec *VirtualMachineInstanceSpec) UnmarshalJSON(data []byte) error {
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions staging/src/kubevirt.io/client-go/api/openapi_generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 6f05a5a

Please sign in to comment.