Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

virtctl: Apply --namespace to created manifests #10167

Merged
merged 4 commits into from
Sep 6, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions pkg/virtctl/create/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@ go_library(
"//pkg/virtctl/create/vm:go_default_library",
"//pkg/virtctl/templates:go_default_library",
"//vendor/github.com/spf13/cobra:go_default_library",
"//vendor/k8s.io/client-go/tools/clientcmd:go_default_library",
],
)
7 changes: 4 additions & 3 deletions pkg/virtctl/create/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ package create

import (
"github.com/spf13/cobra"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

newline?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Basically the same as the other comment: First stdlib imports, then remote packages, then local packages. I did this on purpose while touching the imports anyways.

"k8s.io/client-go/tools/clientcmd"

"kubevirt.io/kubevirt/pkg/virtctl/create/clone"

Expand All @@ -34,7 +35,7 @@ const (
CREATE = "create"
)

func NewCommand() *cobra.Command {
func NewCommand(clientConfig clientcmd.ClientConfig) *cobra.Command {
cmd := &cobra.Command{
Use: CREATE,
Short: "Create a manifest for the specified Kind.",
Expand All @@ -43,8 +44,8 @@ func NewCommand() *cobra.Command {
},
}

cmd.AddCommand(vm.NewCommand())
cmd.AddCommand(preference.NewCommand())
cmd.AddCommand(vm.NewCommand(clientConfig))
cmd.AddCommand(preference.NewCommand(clientConfig))
cmd.AddCommand(instancetype.NewCommand())
cmd.AddCommand(clone.NewCommand())
cmd.SetUsageTemplate(templates.UsageTemplate())
Expand Down
1 change: 1 addition & 0 deletions pkg/virtctl/create/preference/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ go_library(
"//vendor/github.com/spf13/cobra:go_default_library",
"//vendor/k8s.io/apimachinery/pkg/apis/meta/v1:go_default_library",
"//vendor/k8s.io/apimachinery/pkg/util/rand:go_default_library",
"//vendor/k8s.io/client-go/tools/clientcmd:go_default_library",
"//vendor/sigs.k8s.io/yaml:go_default_library",
],
)
Expand Down
38 changes: 30 additions & 8 deletions pkg/virtctl/create/preference/preference.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ package preference

import (
"github.com/spf13/cobra"
"k8s.io/apimachinery/pkg/util/rand"
"sigs.k8s.io/yaml"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/rand"
"k8s.io/client-go/tools/clientcmd"
instancetypev1beta1 "kubevirt.io/api/instancetype/v1beta1"
"sigs.k8s.io/yaml"

"kubevirt.io/kubevirt/pkg/virtctl/create/params"
)
Expand All @@ -46,11 +46,14 @@ const (
)

type createPreference struct {
namespace string
name string
namespaced bool
CPUTopology string
machineType string
preferredStorageClass string

clientConfig clientcmd.ClientConfig
}

type optionFn func(*createPreference, *instancetypev1beta1.VirtualMachinePreferenceSpec) error
Expand All @@ -61,8 +64,10 @@ var optFns = map[string]optionFn{
CPUTopologyFlag: withCPUTopology,
}

func NewCommand() *cobra.Command {
c := createPreference{}
func NewCommand(clientConfig clientcmd.ClientConfig) *cobra.Command {
c := createPreference{
clientConfig: clientConfig,
}
cmd := &cobra.Command{
Use: Preference,
Short: "Create a VirtualMachinePreference or VirtualMachineClusterPreference manifest.",
Expand All @@ -80,16 +85,27 @@ func NewCommand() *cobra.Command {
return cmd
}

func (c *createPreference) setDefaults(cmd *cobra.Command) {
func (c *createPreference) setDefaults(cmd *cobra.Command) error {
namespace, overridden, err := c.clientConfig.Namespace()
if err != nil {
return err
}
if overridden {
c.namespace = namespace
c.namespaced = true
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same question like for VirtualMachineInstancetype.

}

if cmd.Flags().Changed(NameFlag) {
return
return nil
}

if c.namespaced {
c.name = "preference-" + rand.String(5)
} else {
c.name = "clusterpreference-" + rand.String(5)
}

return nil
}

func withVolumeStorageClass(c *createPreference, preferenceSpec *instancetypev1beta1.VirtualMachinePreferenceSpec) error {
Expand Down Expand Up @@ -147,7 +163,7 @@ func (c *createPreference) newClusterPreference() *instancetypev1beta1.VirtualMa
}

func (c *createPreference) newPreference() *instancetypev1beta1.VirtualMachinePreference {
return &instancetypev1beta1.VirtualMachinePreference{
preference := &instancetypev1beta1.VirtualMachinePreference{
TypeMeta: metav1.TypeMeta{
Kind: "VirtualMachinePreference",
APIVersion: instancetypev1beta1.SchemeGroupVersion.String(),
Expand All @@ -156,6 +172,12 @@ func (c *createPreference) newPreference() *instancetypev1beta1.VirtualMachinePr
Name: c.name,
},
}

if c.namespace != "" {
preference.Namespace = c.namespace
}

return preference
}

func (c *createPreference) applyFlags(cmd *cobra.Command, preferenceSpec *instancetypev1beta1.VirtualMachinePreferenceSpec) error {
Expand Down
15 changes: 15 additions & 0 deletions pkg/virtctl/create/preference/preference_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,21 @@ var _ = Describe("create", func() {
Entry("VirtualMachineClusterPreference", "", "preferThreads", false),
)

It("should create namespaced object and apply namespace when namespace is specified", func() {
const namespace = "my-namespace"
bytes, err := clientcmd.NewRepeatableVirtctlCommandWithOut(create, Preference,
setFlag("namespace", namespace),
)()
Expect(err).ToNot(HaveOccurred())

decodedObj, err := runtime.Decode(generatedscheme.Codecs.UniversalDeserializer(), bytes)
Expect(err).ToNot(HaveOccurred())

preference, ok := decodedObj.(*instancetypev1beta1.VirtualMachinePreference)
Expect(ok).To(BeTrue())
Expect(preference.Namespace).To(Equal(namespace))
})

DescribeTable("should fail with invalid CPU topology values", func(namespacedFlag, CPUTopology string, namespaced bool) {
err := clientcmd.NewRepeatableVirtctlCommand(create, Preference, namespacedFlag,
setFlag(CPUTopologyFlag, CPUTopology),
Expand Down
1 change: 1 addition & 0 deletions pkg/virtctl/create/vm/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ go_library(
"//vendor/k8s.io/apimachinery/pkg/api/resource:go_default_library",
"//vendor/k8s.io/apimachinery/pkg/apis/meta/v1:go_default_library",
"//vendor/k8s.io/apimachinery/pkg/util/rand:go_default_library",
"//vendor/k8s.io/client-go/tools/clientcmd:go_default_library",
"//vendor/kubevirt.io/containerized-data-importer-api/pkg/apis/core/v1beta1:go_default_library",
"//vendor/sigs.k8s.io/yaml:go_default_library",
],
Expand Down
39 changes: 31 additions & 8 deletions pkg/virtctl/create/vm/vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/rand"
"k8s.io/client-go/tools/clientcmd"
v1 "kubevirt.io/api/core/v1"
"kubevirt.io/api/instancetype"
cdiv1 "kubevirt.io/containerized-data-importer-api/pkg/apis/core/v1beta1"
Expand Down Expand Up @@ -71,6 +72,7 @@ const (
)

type createVM struct {
namespace string
name string
terminationGracePeriod int64
runStrategy string
Expand All @@ -88,7 +90,8 @@ type createVM struct {
inferPreference string
volumeImport []string

bootOrders map[uint]string
clientConfig clientcmd.ClientConfig
bootOrders map[uint]string
}

type cloneVolume struct {
Expand Down Expand Up @@ -247,8 +250,8 @@ var runStrategies = []string{
string(v1.RunStrategyRerunOnFailure),
}

func NewCommand() *cobra.Command {
c := defaultCreateVM()
func NewCommand(clientConfig clientcmd.ClientConfig) *cobra.Command {
c := defaultCreateVM(clientConfig)
cmd := &cobra.Command{
Use: VM,
Short: "Create a VirtualMachine manifest.",
Expand Down Expand Up @@ -301,11 +304,12 @@ func NewCommand() *cobra.Command {
return cmd
}

func defaultCreateVM() createVM {
func defaultCreateVM(clientConfig clientcmd.ClientConfig) createVM {
return createVM{
terminationGracePeriod: 180,
runStrategy: string(v1.RunStrategyAlways),
memory: "512Mi",
clientConfig: clientConfig,
bootOrders: map[uint]string{},
}
}
Expand Down Expand Up @@ -338,7 +342,10 @@ func volumeShouldNotExist(flag string, vm *v1.VirtualMachine, name string) error
}

func (c *createVM) run(cmd *cobra.Command) error {
c.setDefaults()
if err := c.setDefaults(); err != nil {
return err
}

vm, err := c.newVM()
if err != nil {
return err
Expand All @@ -361,10 +368,20 @@ func (c *createVM) run(cmd *cobra.Command) error {
return nil
}

func (c *createVM) setDefaults() {
func (c *createVM) setDefaults() error {
namespace, overridden, err := c.clientConfig.Namespace()
if err != nil {
return err
}
if overridden {
c.namespace = namespace
}

if c.name == "" {
c.name = "vm-" + rand.String(5)
}

return nil
}

func (c *createVM) usage() string {
Expand Down Expand Up @@ -431,7 +448,7 @@ func (c *createVM) newVM() (*v1.VirtualMachine, error) {

}

return &v1.VirtualMachine{
vm := &v1.VirtualMachine{
TypeMeta: metav1.TypeMeta{
Kind: v1.VirtualMachineGroupVersionKind.Kind,
APIVersion: v1.VirtualMachineGroupVersionKind.GroupVersion().String(),
Expand All @@ -452,7 +469,13 @@ func (c *createVM) newVM() (*v1.VirtualMachine, error) {
},
},
},
}, nil
}

if c.namespace != "" {
vm.Namespace = c.namespace
}

return vm, nil
}

func (c *createVM) addDiskWithBootOrder(flag string, vm *v1.VirtualMachine, name string, bootOrder *uint) error {
Expand Down
10 changes: 9 additions & 1 deletion pkg/virtctl/create/vm/vm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,15 @@ var _ = Describe("create vm", func() {
_ = unmarshalVM(out)
})

It("VM with specified namespace", func() {
const namespace = "my-namespace"
out, err := runCmd(setFlag("namespace", namespace))
Expect(err).ToNot(HaveOccurred())
vm := unmarshalVM(out)

Expect(vm.Namespace).To(Equal(namespace))
})

It("VM with specified name", func() {
const name = "my-vm"
out, err := runCmd(setFlag(NameFlag, name))
Expand Down Expand Up @@ -470,7 +479,6 @@ var _ = Describe("create vm", func() {
const terminationGracePeriod int64 = 123
const instancetypeKind = "virtualmachineinstancetype"
const instancetypeName = "my-instancetype"
const preferenceName = "my-preference"
const dsNamespace = "my-ns"
const dsName = "my-ds"
const dvtSize = "10Gi"
Expand Down
2 changes: 1 addition & 1 deletion pkg/virtctl/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ func NewVirtctlCommand() (*cobra.Command, clientcmd.ClientConfig) {
imageupload.NewImageUploadCommand(clientConfig),
guestfs.NewGuestfsShellCommand(clientConfig),
vmexport.NewVirtualMachineExportCommand(clientConfig),
create.NewCommand(),
create.NewCommand(clientConfig),
credentials.NewCommand(clientConfig),
optionsCmd,
)
Expand Down