Skip to content

Commit

Permalink
Change VMSpec to VMSpec + metadata in templates
Browse files Browse the repository at this point in the history
  • Loading branch information
gitlawr authored and guangbochen committed Aug 17, 2021
1 parent 47dc99b commit 0cdebf9
Show file tree
Hide file tree
Showing 5 changed files with 221 additions and 186 deletions.
15 changes: 9 additions & 6 deletions pkg/api/vm/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -406,7 +406,7 @@ func (h *vmActionHandler) createTemplate(namespace, name string, input CreateTem
Spec: harvesterv1.VirtualMachineTemplateVersionSpec{
TemplateID: vmID,
Description: fmt.Sprintf("Template drived from virtual machine [%s]", vmID),
VM: removeMacAddresses(vm.Spec),
VM: removeMacAddresses(vm),
KeyPairIDs: keyPairIDs,
},
})
Expand All @@ -415,12 +415,15 @@ func (h *vmActionHandler) createTemplate(namespace, name string, input CreateTem

// removeMacAddresses replaces the mac address of each device interface with an empty string.
// This is because macAddresses are unique, and should not reuse the original's.
func removeMacAddresses(vmSpec kv1.VirtualMachineSpec) kv1.VirtualMachineSpec {
censoredSpec := vmSpec
for index := range censoredSpec.Template.Spec.Domain.Devices.Interfaces {
censoredSpec.Template.Spec.Domain.Devices.Interfaces[index].MacAddress = ""
func removeMacAddresses(vm *kv1.VirtualMachine) harvesterv1.VirtualMachineSourceSpec {
sanitizedVm := vm.DeepCopy()
for index := range sanitizedVm.Spec.Template.Spec.Domain.Devices.Interfaces {
sanitizedVm.Spec.Template.Spec.Domain.Devices.Interfaces[index].MacAddress = ""
}
return harvesterv1.VirtualMachineSourceSpec{
ObjectMeta: sanitizedVm.ObjectMeta,
Spec: sanitizedVm.Spec,
}
return censoredSpec
}

// getSSHKeysFromVMITemplateSpec first checks the given VirtualMachineInstanceTemplateSpec
Expand Down
10 changes: 9 additions & 1 deletion pkg/apis/harvesterhci.io/v1beta1/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,15 @@ type VirtualMachineTemplateVersionSpec struct {
KeyPairIDs []string `json:"keyPairIds,omitempty"`

// +optional
VM kv1.VirtualMachineSpec `json:"vm,omitempty"`
VM VirtualMachineSourceSpec `json:"vm,omitempty"`
}

type VirtualMachineSourceSpec struct {
// +kubebuilder:pruning:PreserveUnknownFields
// +optional
ObjectMeta metav1.ObjectMeta `json:"metadata,omitempty"`

Spec kv1.VirtualMachineSpec `json:"spec,omitempty"`
}

type VirtualMachineTemplateVersionStatus struct {
Expand Down
7 changes: 3 additions & 4 deletions pkg/controller/master/template/template_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/watch"
kv1 "kubevirt.io/client-go/api/v1"

harvesterv1 "github.com/harvester/harvester/pkg/apis/harvesterhci.io/v1beta1"
"github.com/harvester/harvester/pkg/generated/clientset/versioned/fake"
Expand Down Expand Up @@ -119,7 +118,7 @@ func TestTemplateHandler_OnChanged(t *testing.T) {
Description: "fake_description",
TemplateID: "default/test",
ImageID: "fake_image_id",
VM: kv1.VirtualMachineSpec{},
VM: harvesterv1.VirtualMachineSourceSpec{},
},
Status: harvesterv1.VirtualMachineTemplateVersionStatus{
Version: 1,
Expand Down Expand Up @@ -162,7 +161,7 @@ func TestTemplateHandler_OnChanged(t *testing.T) {
Description: "fake_description",
TemplateID: "fake_template_id",
ImageID: "fake_image_id",
VM: kv1.VirtualMachineSpec{},
VM: harvesterv1.VirtualMachineSourceSpec{},
},
Status: harvesterv1.VirtualMachineTemplateVersionStatus{
Version: 1,
Expand Down Expand Up @@ -217,7 +216,7 @@ func TestTemplateHandler_OnChanged(t *testing.T) {
Description: "fake_description",
TemplateID: "default/test",
ImageID: "fake_image_id",
VM: kv1.VirtualMachineSpec{},
VM: harvesterv1.VirtualMachineSourceSpec{},
},
Status: harvesterv1.VirtualMachineTemplateVersionStatus{
Version: 2,
Expand Down
Loading

0 comments on commit 0cdebf9

Please sign in to comment.