@@ -22,8 +22,9 @@ import (
22
22
"strings"
23
23
24
24
"github.com/spf13/cobra"
25
+
25
26
appsv1 "k8s.io/api/apps/v1"
26
- v1 "k8s.io/api/core/v1"
27
+ corev1 "k8s.io/api/core/v1"
27
28
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
28
29
"k8s.io/apimachinery/pkg/runtime"
29
30
utilrand "k8s.io/apimachinery/pkg/util/rand"
@@ -41,17 +42,26 @@ var (
41
42
Create a deployment with the specified name.` ))
42
43
43
44
deploymentExample = templates .Examples (i18n .T (`
44
- # Create a new deployment named my-dep that runs the busybox image.
45
- kubectl create deployment my-dep --image=busybox` ))
45
+ # Create a deployment named my-dep that runs the busybox image.
46
+ kubectl create deployment my-dep --image=busybox
47
+
48
+ # Create a deployment with command
49
+ kubectl create deployment my-dep --image=busybox -- date
50
+
51
+ # Create a deployment named my-dep that runs the busybox image and expose port 5701.
52
+ kubectl create deployment my-dep --image=busybox --port=5701` ))
46
53
)
47
54
48
- // DeploymentOpts is returned by NewCmdCreateDeployment
49
- type DeploymentOpts struct {
55
+ // CreateDeploymentOptions is returned by NewCmdCreateDeployment
56
+ type CreateDeploymentOptions struct {
50
57
PrintFlags * genericclioptions.PrintFlags
51
- PrintObj func (obj runtime.Object ) error
58
+
59
+ PrintObj func (obj runtime.Object ) error
52
60
53
61
Name string
54
62
Images []string
63
+ Port int32
64
+ Command []string
55
65
Namespace string
56
66
EnforceNamespace bool
57
67
FieldManager string
@@ -63,46 +73,55 @@ type DeploymentOpts struct {
63
73
genericclioptions.IOStreams
64
74
}
65
75
66
- // NewCmdCreateDeployment is a macro command to create a new deployment.
67
- // This command is better known to users as `kubectl create deployment`.
68
- func NewCmdCreateDeployment (f cmdutil.Factory , ioStreams genericclioptions.IOStreams ) * cobra.Command {
69
- options := & DeploymentOpts {
76
+ func NewCreateCreateDeploymentOptions (ioStreams genericclioptions.IOStreams ) * CreateDeploymentOptions {
77
+ return & CreateDeploymentOptions {
78
+ Port : - 1 ,
70
79
PrintFlags : genericclioptions .NewPrintFlags ("created" ).WithTypeSetter (scheme .Scheme ),
71
80
IOStreams : ioStreams ,
72
81
}
82
+ }
73
83
84
+ // NewCmdCreateDeployment is a macro command to create a new deployment.
85
+ // This command is better known to users as `kubectl create deployment`.
86
+ func NewCmdCreateDeployment (f cmdutil.Factory , ioStreams genericclioptions.IOStreams ) * cobra.Command {
87
+ o := NewCreateCreateDeploymentOptions (ioStreams )
74
88
cmd := & cobra.Command {
75
- Use : "deployment NAME --image=image [--dry-run=server|client|none ]" ,
89
+ Use : "deployment NAME --image=image -- [COMMAND] [args... ]" ,
76
90
DisableFlagsInUseLine : true ,
77
91
Aliases : []string {"deploy" },
78
92
Short : deploymentLong ,
79
93
Long : deploymentLong ,
80
94
Example : deploymentExample ,
81
95
Run : func (cmd * cobra.Command , args []string ) {
82
- cmdutil .CheckErr (options .Complete (f , cmd , args ))
83
- cmdutil .CheckErr (options .Run ())
96
+ cmdutil .CheckErr (o .Complete (f , cmd , args ))
97
+ cmdutil .CheckErr (o .Validate ())
98
+ cmdutil .CheckErr (o .Run ())
84
99
},
85
100
}
86
101
87
- options .PrintFlags .AddFlags (cmd )
102
+ o .PrintFlags .AddFlags (cmd )
88
103
89
104
cmdutil .AddApplyAnnotationFlags (cmd )
90
105
cmdutil .AddValidateFlags (cmd )
91
106
cmdutil .AddGeneratorFlags (cmd , "" )
92
- cmd .Flags ().StringSliceVar (& options .Images , "image" , []string {}, "Image name to run." )
93
- _ = cmd .MarkFlagRequired ("image" )
94
- cmdutil .AddFieldManagerFlagVar (cmd , & options .FieldManager , "kubectl-create" )
107
+ cmd .Flags ().StringSliceVar (& o .Images , "image" , o .Images , "Image names to run." )
108
+ cmd .MarkFlagRequired ("image" )
109
+ cmd .Flags ().Int32Var (& o .Port , "port" , o .Port , "The port that this container exposes." )
110
+ cmdutil .AddFieldManagerFlagVar (cmd , & o .FieldManager , "kubectl-create" )
95
111
96
112
return cmd
97
113
}
98
114
99
115
// Complete completes all the options
100
- func (o * DeploymentOpts ) Complete (f cmdutil.Factory , cmd * cobra.Command , args []string ) error {
116
+ func (o * CreateDeploymentOptions ) Complete (f cmdutil.Factory , cmd * cobra.Command , args []string ) error {
101
117
name , err := NameFromCommandArgs (cmd , args )
102
118
if err != nil {
103
119
return err
104
120
}
105
121
o .Name = name
122
+ if len (args ) > 1 {
123
+ o .Command = args [1 :]
124
+ }
106
125
107
126
clientConfig , err := f .ToRESTConfig ()
108
127
if err != nil {
@@ -144,8 +163,15 @@ func (o *DeploymentOpts) Complete(f cmdutil.Factory, cmd *cobra.Command, args []
144
163
return nil
145
164
}
146
165
166
+ func (o * CreateDeploymentOptions ) Validate () error {
167
+ if len (o .Images ) > 1 && len (o .Command ) > 0 {
168
+ return fmt .Errorf ("cannot specify multiple --image options and command" )
169
+ }
170
+ return nil
171
+ }
172
+
147
173
// Run performs the execution of 'create deployment' sub command
148
- func (o * DeploymentOpts ) Run () error {
174
+ func (o * CreateDeploymentOptions ) Run () error {
149
175
deploy := o .createDeployment ()
150
176
151
177
if o .DryRunStrategy != cmdutil .DryRunClient {
@@ -169,7 +195,7 @@ func (o *DeploymentOpts) Run() error {
169
195
return o .PrintObj (deploy )
170
196
}
171
197
172
- func (o * DeploymentOpts ) createDeployment () * appsv1.Deployment {
198
+ func (o * CreateDeploymentOptions ) createDeployment () * appsv1.Deployment {
173
199
one := int32 (1 )
174
200
labels := map [string ]string {"app" : o .Name }
175
201
selector := metav1.LabelSelector {MatchLabels : labels }
@@ -188,7 +214,7 @@ func (o *DeploymentOpts) createDeployment() *appsv1.Deployment {
188
214
Spec : appsv1.DeploymentSpec {
189
215
Replicas : & one ,
190
216
Selector : & selector ,
191
- Template : v1 .PodTemplateSpec {
217
+ Template : corev1 .PodTemplateSpec {
192
218
ObjectMeta : metav1.ObjectMeta {
193
219
Labels : labels ,
194
220
},
@@ -200,8 +226,8 @@ func (o *DeploymentOpts) createDeployment() *appsv1.Deployment {
200
226
201
227
// buildPodSpec parses the image strings and assemble them into the Containers
202
228
// of a PodSpec. This is all you need to create the PodSpec for a deployment.
203
- func (o * DeploymentOpts ) buildPodSpec () v1 .PodSpec {
204
- podSpec := v1 .PodSpec {Containers : []v1 .Container {}}
229
+ func (o * CreateDeploymentOptions ) buildPodSpec () corev1 .PodSpec {
230
+ podSpec := corev1 .PodSpec {Containers : []corev1 .Container {}}
205
231
for _ , imageString := range o .Images {
206
232
// Retain just the image name
207
233
imageSplit := strings .Split (imageString , "/" )
@@ -214,7 +240,11 @@ func (o *DeploymentOpts) buildPodSpec() v1.PodSpec {
214
240
name = strings .Split (name , "@" )[0 ]
215
241
}
216
242
name = sanitizeAndUniquify (name )
217
- podSpec .Containers = append (podSpec .Containers , v1.Container {Name : name , Image : imageString })
243
+ podSpec .Containers = append (podSpec .Containers , corev1.Container {
244
+ Name : name ,
245
+ Image : imageString ,
246
+ Command : o .Command ,
247
+ })
218
248
}
219
249
return podSpec
220
250
}
0 commit comments