Skip to content

Commit

Permalink
Merge pull request #71006 from liggitt/kubectl-run-apps-v1
Browse files Browse the repository at this point in the history
update kubectl run to generate apps/v1 deployments
  • Loading branch information
k8s-ci-robot authored Nov 14, 2018
2 parents 89698c5 + 03f26e4 commit d00cb23
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 5 deletions.
2 changes: 1 addition & 1 deletion pkg/kubectl/cmd/run/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@ func (o *RunOptions) Run(f cmdutil.Factory, cmd *cobra.Command, args []string) e
if len(generatorName) == 0 {
switch restartPolicy {
case corev1.RestartPolicyAlways:
generatorName = generateversioned.DeploymentAppsV1Beta1GeneratorName
generatorName = generateversioned.DeploymentAppsV1GeneratorName
case corev1.RestartPolicyOnFailure:
generatorName = generateversioned.JobV1GeneratorName
case corev1.RestartPolicyNever:
Expand Down
10 changes: 10 additions & 0 deletions pkg/kubectl/generate/versioned/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ const (
HorizontalPodAutoscalerV1GeneratorName = "horizontalpodautoscaler/v1"
DeploymentV1Beta1GeneratorName = "deployment/v1beta1"
DeploymentAppsV1Beta1GeneratorName = "deployment/apps.v1beta1"
DeploymentAppsV1GeneratorName = "deployment/apps.v1"
DeploymentBasicV1Beta1GeneratorName = "deployment-basic/v1beta1"
DeploymentBasicAppsV1Beta1GeneratorName = "deployment-basic/apps.v1beta1"
DeploymentBasicAppsV1GeneratorName = "deployment-basic/apps.v1"
Expand Down Expand Up @@ -105,6 +106,7 @@ func DefaultGenerators(cmdName string) map[string]generate.Generator {
RunPodV1GeneratorName: BasicPod{},
DeploymentV1Beta1GeneratorName: DeploymentV1Beta1{},
DeploymentAppsV1Beta1GeneratorName: DeploymentAppsV1Beta1{},
DeploymentAppsV1GeneratorName: DeploymentAppsV1{},
JobV1GeneratorName: JobV1{},
CronJobV2Alpha1GeneratorName: CronJobV2Alpha1{},
CronJobV1Beta1GeneratorName: CronJobV1Beta1{},
Expand Down Expand Up @@ -146,6 +148,14 @@ func FallbackGeneratorNameIfNecessary(
cmdErr io.Writer,
) (string, error) {
switch generatorName {
case DeploymentAppsV1GeneratorName:
hasResource, err := HasResource(discoveryClient, appsv1.SchemeGroupVersion.WithResource("deployments"))
if err != nil {
return "", err
}
if !hasResource {
return FallbackGeneratorNameIfNecessary(DeploymentAppsV1Beta1GeneratorName, discoveryClient, cmdErr)
}
case DeploymentAppsV1Beta1GeneratorName:
hasResource, err := HasResource(discoveryClient, appsv1beta1.SchemeGroupVersion.WithResource("deployments"))
if err != nil {
Expand Down
89 changes: 89 additions & 0 deletions pkg/kubectl/generate/versioned/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"strconv"
"strings"

appsv1 "k8s.io/api/apps/v1"
appsv1beta1 "k8s.io/api/apps/v1beta1"
batchv1 "k8s.io/api/batch/v1"
batchv1beta1 "k8s.io/api/batch/v1beta1"
Expand Down Expand Up @@ -210,6 +211,94 @@ func (DeploymentAppsV1Beta1) Generate(genericParams map[string]interface{}) (run
return &deployment, nil
}

type DeploymentAppsV1 struct{}

func (DeploymentAppsV1) ParamNames() []generate.GeneratorParam {
return []generate.GeneratorParam{
{Name: "labels", Required: false},
{Name: "default-name", Required: false},
{Name: "name", Required: true},
{Name: "replicas", Required: true},
{Name: "image", Required: true},
{Name: "image-pull-policy", Required: false},
{Name: "port", Required: false},
{Name: "hostport", Required: false},
{Name: "stdin", Required: false},
{Name: "tty", Required: false},
{Name: "command", Required: false},
{Name: "args", Required: false},
{Name: "env", Required: false},
{Name: "requests", Required: false},
{Name: "limits", Required: false},
{Name: "serviceaccount", Required: false},
}
}

func (DeploymentAppsV1) Generate(genericParams map[string]interface{}) (runtime.Object, error) {
args, err := getArgs(genericParams)
if err != nil {
return nil, err
}

envs, err := getEnvs(genericParams)
if err != nil {
return nil, err
}

params, err := getParams(genericParams)
if err != nil {
return nil, err
}

name, err := getName(params)
if err != nil {
return nil, err
}

labels, err := getLabels(params, name)
if err != nil {
return nil, err
}

count, err := strconv.Atoi(params["replicas"])
if err != nil {
return nil, err
}

podSpec, err := makePodSpec(params, name)
if err != nil {
return nil, err
}

imagePullPolicy := v1.PullPolicy(params["image-pull-policy"])
if err = updatePodContainers(params, args, envs, imagePullPolicy, podSpec); err != nil {
return nil, err
}

if err := updatePodPorts(params, podSpec); err != nil {
return nil, err
}

count32 := int32(count)
deployment := appsv1.Deployment{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Labels: labels,
},
Spec: appsv1.DeploymentSpec{
Replicas: &count32,
Selector: &metav1.LabelSelector{MatchLabels: labels},
Template: v1.PodTemplateSpec{
ObjectMeta: metav1.ObjectMeta{
Labels: labels,
},
Spec: *podSpec,
},
},
}
return &deployment, nil
}

// getLabels returns map of labels.
func getLabels(params map[string]string, name string) (map[string]string, error) {
labelString, found := params["labels"]
Expand Down
8 changes: 4 additions & 4 deletions test/cmd/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -45,17 +45,17 @@ run_kubectl_run_tests() {
# Post-Condition: Deployment "nginx" is created
kube::test::get_object_assert deployment.extensions "{{range.items}}{{$id_field}}:{{end}}" 'nginx-extensions:'
# new generator was used
output_message=$(kubectl get deployment.extensions/nginx-extensions -o jsonpath='{.spec.revisionHistoryLimit}')
kube::test::if_has_string "${output_message}" '2'
output_message=$(kubectl get deployment.apps/nginx-extensions -o jsonpath='{.spec.revisionHistoryLimit}')
kube::test::if_has_string "${output_message}" '10'
# Clean up
kubectl delete deployment nginx-extensions "${kube_flags[@]}"
# Command
kubectl run nginx-apps "--image=$IMAGE_NGINX" --generator=deployment/apps.v1beta1 "${kube_flags[@]}"
kubectl run nginx-apps "--image=$IMAGE_NGINX" --generator=deployment/apps.v1 "${kube_flags[@]}"
# Post-Condition: Deployment "nginx" is created
kube::test::get_object_assert deployment.apps "{{range.items}}{{$id_field}}:{{end}}" 'nginx-apps:'
# and new generator was used, iow. new defaults are applied
output_message=$(kubectl get deployment/nginx-apps -o jsonpath='{.spec.revisionHistoryLimit}')
kube::test::if_has_string "${output_message}" '2'
kube::test::if_has_string "${output_message}" '10'
# Clean up
kubectl delete deployment nginx-apps "${kube_flags[@]}"

Expand Down

0 comments on commit d00cb23

Please sign in to comment.