-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
Changes from all commits
2fee65d
c22bd54
3908e9f
92a1c8d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,9 +21,9 @@ package create | |
|
||
import ( | ||
"github.com/spf13/cobra" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. newline? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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" | ||
|
@@ -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.", | ||
|
@@ -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 | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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" | ||
|
@@ -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 { | ||
|
@@ -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.", | ||
|
@@ -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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we still need the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd keep the |
||
} | ||
|
||
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 { | ||
|
@@ -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(), | ||
|
@@ -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 { | ||
|
@@ -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 | ||
|
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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:
There was a problem hiding this comment.
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.