mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-25 04:33:26 +00:00
Merge pull request #91562 from zhouya0/support_kubectl_create_deployments_replicas
Support kubectl create deployment with replicas
This commit is contained in:
commit
80f1d65f47
@ -48,6 +48,9 @@ var (
|
|||||||
# Create a deployment with command
|
# Create a deployment with command
|
||||||
kubectl create deployment my-dep --image=busybox -- date
|
kubectl create deployment my-dep --image=busybox -- date
|
||||||
|
|
||||||
|
# Create a deployment named my-dep that runs the nginx image with 3 replicas.
|
||||||
|
kubectl create deployment my-dep --image=nginx --replicas=3
|
||||||
|
|
||||||
# Create a deployment named my-dep that runs the busybox image and expose port 5701.
|
# Create a deployment named my-dep that runs the busybox image and expose port 5701.
|
||||||
kubectl create deployment my-dep --image=busybox --port=5701`))
|
kubectl create deployment my-dep --image=busybox --port=5701`))
|
||||||
)
|
)
|
||||||
@ -61,6 +64,7 @@ type CreateDeploymentOptions struct {
|
|||||||
Name string
|
Name string
|
||||||
Images []string
|
Images []string
|
||||||
Port int32
|
Port int32
|
||||||
|
Replicas int32
|
||||||
Command []string
|
Command []string
|
||||||
Namespace string
|
Namespace string
|
||||||
EnforceNamespace bool
|
EnforceNamespace bool
|
||||||
@ -76,6 +80,7 @@ type CreateDeploymentOptions struct {
|
|||||||
func NewCreateCreateDeploymentOptions(ioStreams genericclioptions.IOStreams) *CreateDeploymentOptions {
|
func NewCreateCreateDeploymentOptions(ioStreams genericclioptions.IOStreams) *CreateDeploymentOptions {
|
||||||
return &CreateDeploymentOptions{
|
return &CreateDeploymentOptions{
|
||||||
Port: -1,
|
Port: -1,
|
||||||
|
Replicas: 1,
|
||||||
PrintFlags: genericclioptions.NewPrintFlags("created").WithTypeSetter(scheme.Scheme),
|
PrintFlags: genericclioptions.NewPrintFlags("created").WithTypeSetter(scheme.Scheme),
|
||||||
IOStreams: ioStreams,
|
IOStreams: ioStreams,
|
||||||
}
|
}
|
||||||
@ -107,6 +112,7 @@ func NewCmdCreateDeployment(f cmdutil.Factory, ioStreams genericclioptions.IOStr
|
|||||||
cmd.Flags().StringSliceVar(&o.Images, "image", o.Images, "Image names to run.")
|
cmd.Flags().StringSliceVar(&o.Images, "image", o.Images, "Image names to run.")
|
||||||
cmd.MarkFlagRequired("image")
|
cmd.MarkFlagRequired("image")
|
||||||
cmd.Flags().Int32Var(&o.Port, "port", o.Port, "The port that this container exposes.")
|
cmd.Flags().Int32Var(&o.Port, "port", o.Port, "The port that this container exposes.")
|
||||||
|
cmd.Flags().Int32VarP(&o.Replicas, "replicas", "r", o.Replicas, "Number of replicas to create. Default is 1.")
|
||||||
cmdutil.AddFieldManagerFlagVar(cmd, &o.FieldManager, "kubectl-create")
|
cmdutil.AddFieldManagerFlagVar(cmd, &o.FieldManager, "kubectl-create")
|
||||||
|
|
||||||
return cmd
|
return cmd
|
||||||
@ -196,7 +202,6 @@ func (o *CreateDeploymentOptions) Run() error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (o *CreateDeploymentOptions) createDeployment() *appsv1.Deployment {
|
func (o *CreateDeploymentOptions) createDeployment() *appsv1.Deployment {
|
||||||
one := int32(1)
|
|
||||||
labels := map[string]string{"app": o.Name}
|
labels := map[string]string{"app": o.Name}
|
||||||
selector := metav1.LabelSelector{MatchLabels: labels}
|
selector := metav1.LabelSelector{MatchLabels: labels}
|
||||||
namespace := ""
|
namespace := ""
|
||||||
@ -212,7 +217,7 @@ func (o *CreateDeploymentOptions) createDeployment() *appsv1.Deployment {
|
|||||||
Namespace: namespace,
|
Namespace: namespace,
|
||||||
},
|
},
|
||||||
Spec: appsv1.DeploymentSpec{
|
Spec: appsv1.DeploymentSpec{
|
||||||
Replicas: &one,
|
Replicas: &o.Replicas,
|
||||||
Selector: &selector,
|
Selector: &selector,
|
||||||
Template: corev1.PodTemplateSpec{
|
Template: corev1.PodTemplateSpec{
|
||||||
ObjectMeta: metav1.ObjectMeta{
|
ObjectMeta: metav1.ObjectMeta{
|
||||||
|
@ -94,6 +94,37 @@ func TestCreateDeploymentWithPort(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestCreateDeploymentWithReplicas(t *testing.T) {
|
||||||
|
depName := "jonny-dep"
|
||||||
|
replicas := "3"
|
||||||
|
tf := cmdtesting.NewTestFactory().WithNamespace("test")
|
||||||
|
defer tf.Cleanup()
|
||||||
|
|
||||||
|
ns := scheme.Codecs.WithoutConversion()
|
||||||
|
fakeDiscovery := "{\"kind\":\"APIResourceList\",\"apiVersion\":\"v1\",\"groupVersion\":\"apps/v1\",\"resources\":[{\"name\":\"deployments\",\"singularName\":\"\",\"namespaced\":true,\"kind\":\"Deployment\",\"verbs\":[\"create\",\"delete\",\"deletecollection\",\"get\",\"list\",\"patch\",\"update\",\"watch\"],\"shortNames\":[\"deploy\"],\"categories\":[\"all\"]}]}"
|
||||||
|
tf.Client = &fake.RESTClient{
|
||||||
|
NegotiatedSerializer: ns,
|
||||||
|
Client: fake.CreateHTTPClient(func(req *http.Request) (*http.Response, error) {
|
||||||
|
return &http.Response{
|
||||||
|
StatusCode: http.StatusOK,
|
||||||
|
Body: ioutil.NopCloser(bytes.NewBuffer([]byte(fakeDiscovery))),
|
||||||
|
}, nil
|
||||||
|
}),
|
||||||
|
}
|
||||||
|
tf.ClientConfigVal = &restclient.Config{}
|
||||||
|
|
||||||
|
ioStreams, _, buf, _ := genericclioptions.NewTestIOStreams()
|
||||||
|
cmd := NewCmdCreateDeployment(tf, ioStreams)
|
||||||
|
cmd.Flags().Set("dry-run", "client")
|
||||||
|
cmd.Flags().Set("output", "jsonpath={.spec.replicas}")
|
||||||
|
cmd.Flags().Set("replicas", replicas)
|
||||||
|
cmd.Flags().Set("image", "hollywood/jonny.depp:v2")
|
||||||
|
cmd.Run(cmd, []string{depName})
|
||||||
|
if buf.String() != replicas {
|
||||||
|
t.Errorf("expected output: %s, but got: %s", replicas, buf.String())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestCreateDeploymentNoImage(t *testing.T) {
|
func TestCreateDeploymentNoImage(t *testing.T) {
|
||||||
depName := "jonny-dep"
|
depName := "jonny-dep"
|
||||||
tf := cmdtesting.NewTestFactory().WithNamespace("test")
|
tf := cmdtesting.NewTestFactory().WithNamespace("test")
|
||||||
|
Loading…
Reference in New Issue
Block a user