forked from kubernetes/kubernetes
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Angus Salkeld
committed
Aug 8, 2016
1 parent
42ca885
commit bd0b752
Showing
9 changed files
with
421 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
This file is autogenerated, but we've stopped checking such files into the | ||
repository to reduce the need for rebases. Please run hack/generate-docs.sh to | ||
populate this file. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<!-- BEGIN MUNGE: UNVERSIONED_WARNING --> | ||
|
||
<!-- BEGIN STRIP_FOR_RELEASE --> | ||
|
||
<img src="http://kubernetes.io/kubernetes/img/warning.png" alt="WARNING" | ||
width="25" height="25"> | ||
<img src="http://kubernetes.io/kubernetes/img/warning.png" alt="WARNING" | ||
width="25" height="25"> | ||
<img src="http://kubernetes.io/kubernetes/img/warning.png" alt="WARNING" | ||
width="25" height="25"> | ||
<img src="http://kubernetes.io/kubernetes/img/warning.png" alt="WARNING" | ||
width="25" height="25"> | ||
<img src="http://kubernetes.io/kubernetes/img/warning.png" alt="WARNING" | ||
width="25" height="25"> | ||
|
||
<h2>PLEASE NOTE: This document applies to the HEAD of the source tree</h2> | ||
|
||
If you are using a released version of Kubernetes, you should | ||
refer to the docs that go with that version. | ||
|
||
Documentation for other releases can be found at | ||
[releases.k8s.io](http://releases.k8s.io). | ||
</strong> | ||
-- | ||
|
||
<!-- END STRIP_FOR_RELEASE --> | ||
|
||
<!-- END MUNGE: UNVERSIONED_WARNING --> | ||
|
||
This file is autogenerated, but we've stopped checking such files into the | ||
repository to reduce the need for rebases. Please run hack/generate-docs.sh to | ||
populate this file. | ||
|
||
<!-- BEGIN MUNGE: GENERATED_ANALYTICS --> | ||
[![Analytics](https://kubernetes-site.appspot.com/UA-36037335-10/GitHub/docs/user-guide/kubectl/kubectl_create_deployment.md?pixel)]() | ||
<!-- END MUNGE: GENERATED_ANALYTICS --> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
/* | ||
Copyright 2016 The Kubernetes Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package cmd | ||
|
||
import ( | ||
"fmt" | ||
"io" | ||
|
||
"github.com/renstrom/dedent" | ||
"github.com/spf13/cobra" | ||
|
||
"k8s.io/kubernetes/pkg/kubectl" | ||
cmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util" | ||
) | ||
|
||
var ( | ||
deploymentLong = dedent.Dedent(` | ||
Create a deployment with the specified name.`) | ||
|
||
deploymentExample = dedent.Dedent(` | ||
# Create a new deployment named my-dep that runs the busybox image. | ||
kubectl create deployment my-dep --image=busybox`) | ||
) | ||
|
||
// NewCmdCreateDeployment is a macro command to create a new deployment | ||
func NewCmdCreateDeployment(f *cmdutil.Factory, cmdOut io.Writer) *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "deployment NAME --image=image [--dry-run]", | ||
Aliases: []string{"dep"}, | ||
Short: "Create a deployment with the specified name.", | ||
Long: deploymentLong, | ||
Example: deploymentExample, | ||
Run: func(cmd *cobra.Command, args []string) { | ||
err := CreateDeployment(f, cmdOut, cmd, args) | ||
cmdutil.CheckErr(err) | ||
}, | ||
} | ||
cmdutil.AddApplyAnnotationFlags(cmd) | ||
cmdutil.AddValidateFlags(cmd) | ||
cmdutil.AddPrinterFlags(cmd) | ||
cmdutil.AddGeneratorFlags(cmd, cmdutil.DeploymentBasicV1Beta1GeneratorName) | ||
cmd.Flags().StringSlice("image", []string{}, "Image name to run.") | ||
cmd.MarkFlagRequired("image") | ||
return cmd | ||
} | ||
|
||
// CreateDeployment implements the behavior to run the create deployment command | ||
func CreateDeployment(f *cmdutil.Factory, cmdOut io.Writer, cmd *cobra.Command, args []string) error { | ||
name, err := NameFromCommandArgs(cmd, args) | ||
if err != nil { | ||
return err | ||
} | ||
var generator kubectl.StructuredGenerator | ||
switch generatorName := cmdutil.GetFlagString(cmd, "generator"); generatorName { | ||
case cmdutil.DeploymentBasicV1Beta1GeneratorName: | ||
generator = &kubectl.DeploymentBasicGeneratorV1{Name: name, Images: cmdutil.GetFlagStringSlice(cmd, "image")} | ||
default: | ||
return cmdutil.UsageError(cmd, fmt.Sprintf("Generator: %s not supported.", generatorName)) | ||
} | ||
return RunCreateSubcommand(f, cmd, cmdOut, &CreateSubcommandOptions{ | ||
Name: name, | ||
StructuredGenerator: generator, | ||
DryRun: cmdutil.GetDryRunFlag(cmd), | ||
OutputFormat: cmdutil.GetFlagString(cmd, "output"), | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
/* | ||
Copyright 2016 The Kubernetes Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package cmd | ||
|
||
import ( | ||
"bytes" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestCreateDeployment(t *testing.T) { | ||
depName := "jonny-dep" | ||
f, tf, _, _ := NewAPIFactory() | ||
tf.Printer = &testPrinter{} | ||
tf.Namespace = "test" | ||
buf := bytes.NewBuffer([]byte{}) | ||
cmd := NewCmdCreateDeployment(f, buf) | ||
cmd.Flags().Set("dry-run", "true") | ||
cmd.Flags().Set("output", "name") | ||
cmd.Flags().Set("image", "hollywood/jonny.depp:v2") | ||
cmd.Run(cmd, []string{depName}) | ||
expectedOutput := "deployment/" + depName + "\n" | ||
if buf.String() != expectedOutput { | ||
t.Errorf("expected output: %s, but got: %s", expectedOutput, buf.String()) | ||
} | ||
} | ||
|
||
func TestCreateDeploymentNoImage(t *testing.T) { | ||
depName := "jonny-dep" | ||
f, tf, _, _ := NewAPIFactory() | ||
tf.Printer = &testPrinter{} | ||
tf.Namespace = "test" | ||
buf := bytes.NewBuffer([]byte{}) | ||
cmd := NewCmdCreateDeployment(f, buf) | ||
cmd.Flags().Set("dry-run", "true") | ||
cmd.Flags().Set("output", "name") | ||
err := CreateDeployment(f, buf, cmd, []string{depName}) | ||
assert.Error(t, err, "at least one image must be specified") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
/* | ||
Copyright 2016 The Kubernetes Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package kubectl | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
|
||
"k8s.io/kubernetes/pkg/api" | ||
"k8s.io/kubernetes/pkg/api/unversioned" | ||
"k8s.io/kubernetes/pkg/apis/extensions" | ||
"k8s.io/kubernetes/pkg/runtime" | ||
) | ||
|
||
// DeploymentGeneratorV1 supports stable generation of a deployment | ||
type DeploymentBasicGeneratorV1 struct { | ||
Name string | ||
Images []string | ||
} | ||
|
||
// Ensure it supports the generator pattern that uses parameters specified during construction | ||
var _ StructuredGenerator = &DeploymentBasicGeneratorV1{} | ||
|
||
func (DeploymentBasicGeneratorV1) ParamNames() []GeneratorParam { | ||
return []GeneratorParam{ | ||
{"name", true}, | ||
{"image", true}, | ||
} | ||
} | ||
|
||
func (s DeploymentBasicGeneratorV1) Generate(params map[string]interface{}) (runtime.Object, error) { | ||
err := ValidateParams(s.ParamNames(), params) | ||
if err != nil { | ||
return nil, err | ||
} | ||
name, isString := params["name"].(string) | ||
if !isString { | ||
return nil, fmt.Errorf("expected string, saw %v for 'name'", name) | ||
} | ||
imageStrings, isArray := params["image"].([]string) | ||
if !isArray { | ||
return nil, fmt.Errorf("expected []string, found :%v", imageStrings) | ||
} | ||
delegate := &DeploymentBasicGeneratorV1{Name: name, Images: imageStrings} | ||
return delegate.StructuredGenerate() | ||
} | ||
|
||
// StructuredGenerate outputs a deployment object using the configured fields | ||
func (s *DeploymentBasicGeneratorV1) StructuredGenerate() (runtime.Object, error) { | ||
if err := s.validate(); err != nil { | ||
return nil, err | ||
} | ||
|
||
podSpec := api.PodSpec{Containers: []api.Container{}} | ||
for _, imageString := range s.Images { | ||
imageSplit := strings.Split(imageString, "/") | ||
name := imageSplit[len(imageSplit)-1] | ||
podSpec.Containers = append(podSpec.Containers, api.Container{Name: name, Image: imageString}) | ||
} | ||
|
||
// setup default label and selector | ||
labels := map[string]string{} | ||
labels["app"] = s.Name | ||
selector := unversioned.LabelSelector{MatchLabels: labels} | ||
deployment := extensions.Deployment{ | ||
ObjectMeta: api.ObjectMeta{ | ||
Name: s.Name, | ||
Labels: labels, | ||
}, | ||
Spec: extensions.DeploymentSpec{ | ||
Replicas: 1, | ||
Selector: &selector, | ||
Template: api.PodTemplateSpec{ | ||
ObjectMeta: api.ObjectMeta{ | ||
Labels: labels, | ||
}, | ||
Spec: podSpec, | ||
}, | ||
}, | ||
} | ||
return &deployment, nil | ||
} | ||
|
||
// validate validates required fields are set to support structured generation | ||
func (s *DeploymentBasicGeneratorV1) validate() error { | ||
if len(s.Name) == 0 { | ||
return fmt.Errorf("name must be specified") | ||
} | ||
if len(s.Images) == 0 { | ||
return fmt.Errorf("at least one image must be specified") | ||
} | ||
return nil | ||
} |
Oops, something went wrong.