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
Showing
6 changed files
with
253 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
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,141 @@ | ||
package create | ||
|
||
import ( | ||
"fmt" | ||
"io" | ||
|
||
"github.com/spf13/cobra" | ||
|
||
kapi "k8s.io/kubernetes/pkg/api" | ||
"k8s.io/kubernetes/pkg/api/meta" | ||
cmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util" | ||
"k8s.io/kubernetes/pkg/runtime" | ||
|
||
"github.com/openshift/origin/pkg/client" | ||
"github.com/openshift/origin/pkg/cmd/util/clientcmd" | ||
deployapi "github.com/openshift/origin/pkg/deploy/api" | ||
) | ||
|
||
const ( | ||
DeploymentConfigRecommendedName = "deploymentconfig" | ||
|
||
deploymentConfigLong = ` | ||
Create a deployment config that uses a given image.` | ||
|
||
deploymentConfigExample = ` # Create an nginx deployment config named my-nginx | ||
$ %[1]s my-nginx --image=nginx` | ||
) | ||
|
||
type CreateDeploymentConfigOptions struct { | ||
DC *deployapi.DeploymentConfig | ||
Client client.DeploymentConfigsNamespacer | ||
|
||
Mapper meta.RESTMapper | ||
OutputFormat string | ||
Out io.Writer | ||
Printer ObjectPrinter | ||
} | ||
|
||
// NewCmdCreateServiceAccount is a macro command to create a new service account | ||
func NewCmdCreateDeploymentConfig(name, fullName string, f *clientcmd.Factory, out io.Writer) *cobra.Command { | ||
o := &CreateDeploymentConfigOptions{Out: out} | ||
|
||
cmd := &cobra.Command{ | ||
Use: name + " NAME --image=IMAGE -- [COMMAND] [args...]", | ||
Short: "Create deployment config with default options that uses a given image.", | ||
Long: deploymentConfigLong, | ||
Example: fmt.Sprintf(deploymentConfigExample, fullName), | ||
Run: func(cmd *cobra.Command, args []string) { | ||
cmdutil.CheckErr(o.Complete(cmd, f, args)) | ||
cmdutil.CheckErr(o.Validate()) | ||
cmdutil.CheckErr(o.Run()) | ||
}, | ||
Aliases: []string{"dc"}, | ||
} | ||
|
||
cmd.Flags().String("image", "", "The image for the container to run.") | ||
cmd.MarkFlagRequired("image") | ||
cmdutil.AddOutputFlagsForMutation(cmd) | ||
return cmd | ||
} | ||
|
||
func (o *CreateDeploymentConfigOptions) Complete(cmd *cobra.Command, f *clientcmd.Factory, args []string) error { | ||
if len(args) == 0 { | ||
return fmt.Errorf("at least one argument is required") | ||
} | ||
labels := map[string]string{"deployment-config.name": args[0]} | ||
|
||
o.DC = &deployapi.DeploymentConfig{ | ||
ObjectMeta: kapi.ObjectMeta{Name: args[0]}, | ||
Spec: deployapi.DeploymentConfigSpec{ | ||
Selector: labels, | ||
Replicas: 1, | ||
Template: &kapi.PodTemplateSpec{ | ||
ObjectMeta: kapi.ObjectMeta{Labels: labels}, | ||
Spec: kapi.PodSpec{ | ||
Containers: []kapi.Container{ | ||
{ | ||
Name: "default-container", | ||
Image: cmdutil.GetFlagString(cmd, "image"), | ||
Command: args[1:], | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
var err error | ||
o.DC.Namespace, _, err = f.DefaultNamespace() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
o.Client, _, err = f.Clients() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
o.Mapper, _ = f.Object(false) | ||
o.OutputFormat = cmdutil.GetFlagString(cmd, "output") | ||
|
||
o.Printer = func(obj runtime.Object, out io.Writer) error { | ||
return f.PrintObject(cmd, o.Mapper, obj, out) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (o *CreateDeploymentConfigOptions) Validate() error { | ||
if o.DC == nil { | ||
return fmt.Errorf("DC is required") | ||
} | ||
if o.Client == nil { | ||
return fmt.Errorf("Client is required") | ||
} | ||
if o.Mapper == nil { | ||
return fmt.Errorf("Mapper is required") | ||
} | ||
if o.Out == nil { | ||
return fmt.Errorf("Out is required") | ||
} | ||
if o.Printer == nil { | ||
return fmt.Errorf("Printer is required") | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (o *CreateDeploymentConfigOptions) Run() error { | ||
actualObj, err := o.Client.DeploymentConfigs(o.DC.Namespace).Create(o.DC) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if useShortOutput := o.OutputFormat == "name"; useShortOutput || len(o.OutputFormat) == 0 { | ||
cmdutil.PrintSuccess(o.Mapper, useShortOutput, o.Out, "deploymentconfig", actualObj.Name, "created") | ||
return nil | ||
} | ||
|
||
return o.Printer(actualObj, o.Out) | ||
} |
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