Skip to content

Commit a82d71c

Browse files
authored
Merge pull request kubernetes#91199 from aubm/refactor-kubectl-create-deploy-isolate-create-logic
Refactor kubectl create deploy: isolate obj construction logic
2 parents b07d99d + c43f62c commit a82d71c

File tree

1 file changed

+25
-21
lines changed

1 file changed

+25
-21
lines changed

staging/src/k8s.io/kubectl/pkg/cmd/create/create_deployment.go

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,30 @@ func (o *DeploymentOpts) Complete(f cmdutil.Factory, cmd *cobra.Command, args []
146146

147147
// Run performs the execution of 'create deployment' sub command
148148
func (o *DeploymentOpts) Run() error {
149+
deploy := o.createDeployment()
150+
151+
if o.DryRunStrategy != cmdutil.DryRunClient {
152+
createOptions := metav1.CreateOptions{}
153+
if o.FieldManager != "" {
154+
createOptions.FieldManager = o.FieldManager
155+
}
156+
if o.DryRunStrategy == cmdutil.DryRunServer {
157+
if err := o.DryRunVerifier.HasSupport(deploy.GroupVersionKind()); err != nil {
158+
return err
159+
}
160+
createOptions.DryRun = []string{metav1.DryRunAll}
161+
}
162+
var err error
163+
deploy, err = o.Client.Deployments(o.Namespace).Create(context.TODO(), deploy, createOptions)
164+
if err != nil {
165+
return fmt.Errorf("failed to create deployment: %v", err)
166+
}
167+
}
168+
169+
return o.PrintObj(deploy)
170+
}
171+
172+
func (o *DeploymentOpts) createDeployment() *appsv1.Deployment {
149173
one := int32(1)
150174
labels := map[string]string{"app": o.Name}
151175
selector := metav1.LabelSelector{MatchLabels: labels}
@@ -154,7 +178,7 @@ func (o *DeploymentOpts) Run() error {
154178
namespace = o.Namespace
155179
}
156180

157-
deploy := &appsv1.Deployment{
181+
return &appsv1.Deployment{
158182
TypeMeta: metav1.TypeMeta{APIVersion: appsv1.SchemeGroupVersion.String(), Kind: "Deployment"},
159183
ObjectMeta: metav1.ObjectMeta{
160184
Name: o.Name,
@@ -172,26 +196,6 @@ func (o *DeploymentOpts) Run() error {
172196
},
173197
},
174198
}
175-
176-
if o.DryRunStrategy != cmdutil.DryRunClient {
177-
createOptions := metav1.CreateOptions{}
178-
if o.FieldManager != "" {
179-
createOptions.FieldManager = o.FieldManager
180-
}
181-
if o.DryRunStrategy == cmdutil.DryRunServer {
182-
if err := o.DryRunVerifier.HasSupport(deploy.GroupVersionKind()); err != nil {
183-
return err
184-
}
185-
createOptions.DryRun = []string{metav1.DryRunAll}
186-
}
187-
var err error
188-
deploy, err = o.Client.Deployments(o.Namespace).Create(context.TODO(), deploy, createOptions)
189-
if err != nil {
190-
return fmt.Errorf("failed to create deployment: %v", err)
191-
}
192-
}
193-
194-
return o.PrintObj(deploy)
195199
}
196200

197201
// buildPodSpec parses the image strings and assemble them into the Containers

0 commit comments

Comments
 (0)