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 1 commit
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
2 changes: 1 addition & 1 deletion pkg/virtctl/create/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func NewCommand(clientConfig clientcmd.ClientConfig) *cobra.Command {

cmd.AddCommand(vm.NewCommand(clientConfig))
cmd.AddCommand(preference.NewCommand(clientConfig))
cmd.AddCommand(instancetype.NewCommand())
cmd.AddCommand(instancetype.NewCommand(clientConfig))
cmd.AddCommand(clone.NewCommand())
cmd.SetUsageTemplate(templates.UsageTemplate())

Expand Down
1 change: 1 addition & 0 deletions pkg/virtctl/create/instancetype/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,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/sigs.k8s.io/yaml:go_default_library",
],
)
Expand Down
37 changes: 31 additions & 6 deletions pkg/virtctl/create/instancetype/instancetype.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,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"
instancetypev1beta1 "kubevirt.io/api/instancetype/v1beta1"
"sigs.k8s.io/yaml"
Expand All @@ -51,13 +52,16 @@ const (
)

type createInstancetype struct {
namespace string
name string
cpu uint32
memory string
gpus []string
hostDevices []string
ioThreadsPolicy string
namespaced bool

clientConfig clientcmd.ClientConfig
}

type GPU struct {
Expand All @@ -78,8 +82,10 @@ var optFns = map[string]optionFn{
IOThreadsPolicyFlag: withIOThreadsPolicy,
}

func NewCommand() *cobra.Command {
c := createInstancetype{}
func NewCommand(clientConfig clientcmd.ClientConfig) *cobra.Command {
c := createInstancetype{
clientConfig: clientConfig,
}
cmd := &cobra.Command{
Use: Instancetype,
Short: "Create VirtualMachineInstancetype or VirtualMachineClusterInstancetype manifest.",
Expand Down Expand Up @@ -107,16 +113,27 @@ func NewCommand() *cobra.Command {
return cmd
}

func (c *createInstancetype) setDefaults(cmd *cobra.Command) {
func (c *createInstancetype) 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.

Do we still need the namespaced flag? We could by default create VirtualMachineClusterInstancetype and based on namespace flag decide if we want the VirtualMachineInstancetype instead. What do you think about that?

Copy link
Member Author

Choose a reason for hiding this comment

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

I'd keep the namespaced flag so it is still possible to create a VirtualMachineInstancetype that does not have the namespace set.

}

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

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

return nil
}

func withGPUs(c *createInstancetype, instancetypeSpec *instancetypev1beta1.VirtualMachineInstancetypeSpec) error {
Expand Down Expand Up @@ -197,7 +214,7 @@ func (c *createInstancetype) usage() string {
}

func (c *createInstancetype) newInstancetype(_ *cobra.Command) *instancetypev1beta1.VirtualMachineInstancetype {
return &instancetypev1beta1.VirtualMachineInstancetype{
instancetype := &instancetypev1beta1.VirtualMachineInstancetype{
TypeMeta: metav1.TypeMeta{
Kind: "VirtualMachineInstancetype",
APIVersion: instancetypev1beta1.SchemeGroupVersion.String(),
Expand All @@ -214,6 +231,12 @@ func (c *createInstancetype) newInstancetype(_ *cobra.Command) *instancetypev1be
},
},
}

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

return instancetype
}

func (c *createInstancetype) newClusterInstancetype(_ *cobra.Command) *instancetypev1beta1.VirtualMachineClusterInstancetype {
Expand Down Expand Up @@ -264,7 +287,9 @@ func (c *createInstancetype) run(cmd *cobra.Command) error {
var out []byte
var err error

c.setDefaults(cmd)
if err := c.setDefaults(cmd); err != nil {
return err
}

if err := c.validateFlags(); err != nil {
return err
Expand Down
17 changes: 17 additions & 0 deletions pkg/virtctl/create/instancetype/instancetype_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,23 @@ var _ = Describe("create", func() {
Entry("VirtualMachineClusterInstacetype set to shared", "", "shared", false, v1.IOThreadsPolicyShared),
)

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

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

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

DescribeTable("invalid cpu and memory", func(cpu, memory, errMsg string) {
err := clientcmd.NewRepeatableVirtctlCommand(create, Instancetype, namespaced,
setFlag(CPUFlag, cpu),
Expand Down