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 all 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",
],
)
1 change: 1 addition & 0 deletions pkg/virtctl/create/clone/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ go_library(
"//vendor/github.com/spf13/cobra:go_default_library",
"//vendor/k8s.io/api/core/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
34 changes: 28 additions & 6 deletions pkg/virtctl/create/clone/clone.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,15 @@ import (
"fmt"
"strings"

"kubevirt.io/kubevirt/pkg/pointer"

"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.

it's not related with this PR but, 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.

I moved this local import to the bottom of the imports on purpose.

Copy link
Contributor

@jcanocan jcanocan Jul 27, 2023

Choose a reason for hiding this comment

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

Sorry, I didn't explain myself properly. I meant between:

"github.com/spf13/cobra"v1

"k8s.io/api/core/v1"

Copy link
Member Author

Choose a reason for hiding this comment

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

Those are all remote imports, so AFAIK we don't separate them further.

v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/util/rand"
"k8s.io/client-go/tools/clientcmd"
clonev1alpha1 "kubevirt.io/api/clone/v1alpha1"
"kubevirt.io/client-go/kubecli"
"sigs.k8s.io/yaml"

"kubevirt.io/kubevirt/pkg/pointer"
)

const (
Expand All @@ -51,6 +52,7 @@ const (
)

type createClone struct {
namespace string
name string
sourceName string
targetName string
Expand All @@ -60,6 +62,8 @@ type createClone struct {
annotationFilters []string
newMacAddresses []string
newSmbiosSerial string

clientConfig clientcmd.ClientConfig
}

type cloneSpec clonev1alpha1.VirtualMachineCloneSpec
Expand All @@ -69,8 +73,10 @@ var optFns = map[string]optionFn{
NewMacAddressesFlag: withNewMacAddresses,
}

func NewCommand() *cobra.Command {
c := createClone{}
func NewCommand(clientConfig clientcmd.ClientConfig) *cobra.Command {
c := createClone{
clientConfig: clientConfig,
}

cmd := &cobra.Command{
Use: Clone,
Expand Down Expand Up @@ -163,6 +169,9 @@ func (c *createClone) usage() string {

func (c *createClone) newClone() (*clonev1alpha1.VirtualMachineClone, error) {
clone := kubecli.NewMinimalClone(c.name)
if c.namespace != "" {
clone.Namespace = c.namespace
}

source, err := c.typeToTypedLocalObjectReference(c.sourceType, c.sourceName, true)
if err != nil {
Expand Down Expand Up @@ -201,7 +210,10 @@ func (c *createClone) applyFlags(cmd *cobra.Command, spec *clonev1alpha1.Virtual
}

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

err := c.validateFlags()
if err != nil {
return err
Expand Down Expand Up @@ -270,7 +282,15 @@ func (c *createClone) typeToTypedLocalObjectReference(sourceOrTargetType, source
}, nil
}

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

if c.name == "" {
c.name = "clone-" + rand.String(5)
}
Expand All @@ -283,6 +303,8 @@ func (c *createClone) setDefaults() {
if c.targetType == "" {
c.targetType = defaultType
}

return nil
}

func (c *createClone) validateFlags() error {
Expand Down
12 changes: 12 additions & 0 deletions pkg/virtctl/create/clone/clone_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,18 @@ var _ = Describe("create clone", func() {
Expect(*cloneObj.Spec.NewSMBiosSerial).To(Equal(newSerial))
})

It("sets the provided namespace", func() {
flags := getSourceNameFlags()

const namespace = "my-namespace"
flags = addFlag(flags, "namespace", namespace)

cloneObj, err := newCommand(flags...)
Expect(err).ToNot(HaveOccurred())

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

})

func addFlag(s []string, flag, value string) []string {
Expand Down
12 changes: 6 additions & 6 deletions pkg/virtctl/create/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ 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"

"kubevirt.io/kubevirt/pkg/virtctl/create/instancetype"
"kubevirt.io/kubevirt/pkg/virtctl/create/preference"
"kubevirt.io/kubevirt/pkg/virtctl/create/vm"
Expand All @@ -34,7 +34,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,10 +43,10 @@ func NewCommand() *cobra.Command {
},
}

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

return cmd
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
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