Merge pull request #91562 from zhouya0/support_kubectl_create_deployments_replicas

Support kubectl create deployment with replicas
This commit is contained in:
Kubernetes Prow Robot 2020-06-04 13:51:58 -07:00 committed by GitHub
commit 80f1d65f47
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 38 additions and 2 deletions

View File

@ -48,6 +48,9 @@ var (
# Create a deployment with command
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.
kubectl create deployment my-dep --image=busybox --port=5701`))
)
@ -61,6 +64,7 @@ type CreateDeploymentOptions struct {
Name string
Images []string
Port int32
Replicas int32
Command []string
Namespace string
EnforceNamespace bool
@ -76,6 +80,7 @@ type CreateDeploymentOptions struct {
func NewCreateCreateDeploymentOptions(ioStreams genericclioptions.IOStreams) *CreateDeploymentOptions {
return &CreateDeploymentOptions{
Port: -1,
Replicas: 1,
PrintFlags: genericclioptions.NewPrintFlags("created").WithTypeSetter(scheme.Scheme),
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.MarkFlagRequired("image")
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")
return cmd
@ -196,7 +202,6 @@ func (o *CreateDeploymentOptions) Run() error {
}
func (o *CreateDeploymentOptions) createDeployment() *appsv1.Deployment {
one := int32(1)
labels := map[string]string{"app": o.Name}
selector := metav1.LabelSelector{MatchLabels: labels}
namespace := ""
@ -212,7 +217,7 @@ func (o *CreateDeploymentOptions) createDeployment() *appsv1.Deployment {
Namespace: namespace,
},
Spec: appsv1.DeploymentSpec{
Replicas: &one,
Replicas: &o.Replicas,
Selector: &selector,
Template: corev1.PodTemplateSpec{
ObjectMeta: metav1.ObjectMeta{

View File

@ -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) {
depName := "jonny-dep"
tf := cmdtesting.NewTestFactory().WithNamespace("test")