Merge pull request #91199 from aubm/refactor-kubectl-create-deploy-isolate-create-logic

Refactor kubectl create deploy: isolate obj construction logic
This commit is contained in:
Kubernetes Prow Robot 2020-05-26 02:27:12 -07:00 committed by GitHub
commit a82d71c376
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -146,32 +146,7 @@ func (o *DeploymentOpts) Complete(f cmdutil.Factory, cmd *cobra.Command, args []
// Run performs the execution of 'create deployment' sub command // Run performs the execution of 'create deployment' sub command
func (o *DeploymentOpts) Run() error { func (o *DeploymentOpts) Run() error {
one := int32(1) deploy := o.createDeployment()
labels := map[string]string{"app": o.Name}
selector := metav1.LabelSelector{MatchLabels: labels}
namespace := ""
if o.EnforceNamespace {
namespace = o.Namespace
}
deploy := &appsv1.Deployment{
TypeMeta: metav1.TypeMeta{APIVersion: appsv1.SchemeGroupVersion.String(), Kind: "Deployment"},
ObjectMeta: metav1.ObjectMeta{
Name: o.Name,
Labels: labels,
Namespace: namespace,
},
Spec: appsv1.DeploymentSpec{
Replicas: &one,
Selector: &selector,
Template: v1.PodTemplateSpec{
ObjectMeta: metav1.ObjectMeta{
Labels: labels,
},
Spec: o.buildPodSpec(),
},
},
}
if o.DryRunStrategy != cmdutil.DryRunClient { if o.DryRunStrategy != cmdutil.DryRunClient {
createOptions := metav1.CreateOptions{} createOptions := metav1.CreateOptions{}
@ -194,6 +169,35 @@ func (o *DeploymentOpts) Run() error {
return o.PrintObj(deploy) return o.PrintObj(deploy)
} }
func (o *DeploymentOpts) createDeployment() *appsv1.Deployment {
one := int32(1)
labels := map[string]string{"app": o.Name}
selector := metav1.LabelSelector{MatchLabels: labels}
namespace := ""
if o.EnforceNamespace {
namespace = o.Namespace
}
return &appsv1.Deployment{
TypeMeta: metav1.TypeMeta{APIVersion: appsv1.SchemeGroupVersion.String(), Kind: "Deployment"},
ObjectMeta: metav1.ObjectMeta{
Name: o.Name,
Labels: labels,
Namespace: namespace,
},
Spec: appsv1.DeploymentSpec{
Replicas: &one,
Selector: &selector,
Template: v1.PodTemplateSpec{
ObjectMeta: metav1.ObjectMeta{
Labels: labels,
},
Spec: o.buildPodSpec(),
},
},
}
}
// buildPodSpec parses the image strings and assemble them into the Containers // buildPodSpec parses the image strings and assemble them into the Containers
// of a PodSpec. This is all you need to create the PodSpec for a deployment. // of a PodSpec. This is all you need to create the PodSpec for a deployment.
func (o *DeploymentOpts) buildPodSpec() v1.PodSpec { func (o *DeploymentOpts) buildPodSpec() v1.PodSpec {