-
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 3 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 |
---|---|---|
|
@@ -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 | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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" | ||
) | ||
|
@@ -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 | ||
|
@@ -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.", | ||
|
@@ -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 | ||
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. 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 { | ||
|
@@ -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(), | ||
|
@@ -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 { | ||
|
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.
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.
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.