Skip to content

Commit

Permalink
fixing typoes
Browse files Browse the repository at this point in the history
  • Loading branch information
ngtuna committed Aug 6, 2016
1 parent 278d1db commit 82495e8
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 44 deletions.
105 changes: 63 additions & 42 deletions cli/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -1085,48 +1085,28 @@ func printControllers(mServices, mDeployments, mDaemonSets, mReplicationControll
}
}

// Convert tranforms docker compose or dab file to k8s objects
func Convert(c *cli.Context) {
inputFile := c.String("file")
dabFile := c.String("bundle")
outFile := c.String("out")
generateYaml := c.BoolT("yaml")
toStdout := c.BoolT("stdout")
createD := c.BoolT("deployment")
createDS := c.BoolT("daemonset")
createRC := c.BoolT("replicationcontroller")
createChart := c.BoolT("chart")
replicas := c.Int("replicas")
singleOutput := len(outFile) != 0 || toStdout
createDeploymentConfig := c.BoolT("deploymentconfig")

// Create Deployment by default if no controller has be set
if !createD && !createDS && !createRC && !createDeploymentConfig {
createD = true
}

// Validate the flags
if len(outFile) != 0 && toStdout {
func validateFlags(opt convertOptions, singleOutput bool, dabFile, inputFile string) {
if len(opt.outFile) != 0 && opt.toStdout {
logrus.Fatalf("Error: --out and --stdout can't be set at the same time")
}
if createChart && toStdout {
if opt.createChart && opt.toStdout {
logrus.Fatalf("Error: chart cannot be generated when --stdout is specified")
}
if replicas < 0 {
if opt.replicas < 0 {
logrus.Fatalf("Error: --replicas cannot be negative")
}
if singleOutput {
count := 0
if createD {
if opt.createD {
count++
}
if createDS {
if opt.createDS {
count++
}
if createRC {
if opt.createRC {
count++
}
if createDeploymentConfig {
if opt.createDeploymentConfig {
count++
}
if count > 1 {
Expand All @@ -1136,9 +1116,33 @@ func Convert(c *cli.Context) {
if len(dabFile) > 0 && len(inputFile) > 0 && inputFile != DefaultComposeFile {
logrus.Fatalf("Error: compose file and dab file cannot be specified at the same time")
}
}

// Convert tranforms docker compose or dab file to k8s objects
func Convert(c *cli.Context) {
inputFile := c.String("file")
dabFile := c.String("bundle")
outFile := c.String("out")
generateYaml := c.BoolT("yaml")
toStdout := c.BoolT("stdout")
createD := c.BoolT("deployment")
createDS := c.BoolT("daemonset")
createRC := c.BoolT("replicationcontroller")
createChart := c.BoolT("chart")
replicas := c.Int("replicas")
singleOutput := len(outFile) != 0 || toStdout
createDeploymentConfig := c.BoolT("deploymentconfig")

// Create Deployment by default if no controller has be set
if !createD && !createDS && !createRC && !createDeploymentConfig {
createD = true
}

komposeObject := KomposeObject{}
file := inputFile
if len(dabFile) > 0 {
file = dabFile
}

opt := convertOptions{
toStdout: toStdout,
createD: createD,
Expand All @@ -1152,9 +1156,12 @@ func Convert(c *cli.Context) {
outFile: outFile,
}

validateFlags(opt, singleOutput, dabFile, inputFile)

komposeObject := KomposeObject{}

if len(dabFile) > 0 {
komposeObject = loadBundlesFile(dabFile)
file = dabFile
} else {
komposeObject = loadComposeFile(inputFile)
}
Expand Down Expand Up @@ -1207,10 +1214,27 @@ func print(name, trailing string, data []byte, toStdout, generateYaml bool, f *o
}
}

// Up brings up rc, svc.
func validateK8S(client *client.Client) (error){
_, err := client.Services(api.NamespaceSystem).List(api.ListOptions{})
return err
}

// Up brings up deployment, svc.
func Up(c *cli.Context) {
fmt.Println("We are going to create deployment controller and service for your dockerized application. \n" +
"If you need more kind of controllers, consider to use kompose convert and kubectl. \n")
fmt.Println("We are going to create Kubernetes deployment and service for your dockerized application. \n" +
"If you need more kind of controllers, use 'kompose convert' and 'kubectl create -f' instead. \n")

factory := cmdutil.NewFactory(nil)
clientConfig, err := factory.ClientConfig()
if err != nil {
logrus.Fatalf("Failed to get config for accessing the Kubernetes cluster: %v", err)
}
client := client.NewOrDie(clientConfig)

//FIXME: try to list out system services in order to check whether k8s is running or not
if err := validateK8S(client); err != nil {
logrus.Fatalf("Kubernetes cluster is not running.")
}

inputFile := c.String("file")
dabFile := c.String("bundle")
Expand All @@ -1220,6 +1244,8 @@ func Up(c *cli.Context) {
replicas: 1,
}

validateFlags(opt, false, dabFile, inputFile)

if len(dabFile) > 0 {
komposeObject = loadBundlesFile(dabFile)
} else {
Expand All @@ -1229,13 +1255,6 @@ func Up(c *cli.Context) {
// Convert komposeObject to K8S controllers
mServices, mDeployments, _, _, _, _ := komposeConvert(komposeObject, opt)

factory := cmdutil.NewFactory(nil)
clientConfig, err := factory.ClientConfig()
if err != nil {
logrus.Fatalf("Failed to get Kubernetes client config: %v", err)
}
client := client.NewOrDie(clientConfig)

// submit svc first
sc := &api.Service{}
for k, v := range mServices {
Expand All @@ -1248,7 +1267,7 @@ func Up(c *cli.Context) {
if err != nil {
logrus.Fatalf("Failed to create service %s: ", k, err)
} else {
fmt.Println("Service " + k + " has been created.")
fmt.Printf("Service %q has been created.\n", k)
}
logrus.Debugf("%s\n", scCreated)
}
Expand All @@ -1265,10 +1284,12 @@ func Up(c *cli.Context) {
if err != nil {
logrus.Fatalf("Failed to create deployment controller %s: ", k, err)
} else {
fmt.Println("Deployment controller " + k + " has been created.")
fmt.Printf("Deployment controller %q has been created.\n", k)
}
logrus.Debugf("%s\n", dcCreated)
}

fmt.Println("\nApplication has been deployed to Kubernetes. You can run 'kubectl get deployment, svc' for details.")
}

// updateController updates the given object with the given pod template update function and ObjectMeta update function
Expand Down
4 changes: 2 additions & 2 deletions cli/command/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,14 +89,14 @@ func ConvertCommand() cli.Command {
func UpCommand() cli.Command {
return cli.Command{
Name: "up",
Usage: "Deploy your Dockerized application to Kubernetes (by default create dc and svc)",
Usage: "Deploy your Dockerized application to Kubernetes (default: creating Kubernetes deployment and service)",
Action: func(c *cli.Context) {
app.Up(c)
},
Flags: []cli.Flag{
cli.StringFlag{
Name: "file,f",
Usage: fmt.Sprintf("Specify an alternate compose file (default: %s)", app.DefaultComposeFile),
Usage: fmt.Sprintf("Specify an alternative compose file (default: %s)", app.DefaultComposeFile),
Value: app.DefaultComposeFile,
EnvVar: "COMPOSE_FILE",
},
Expand Down

0 comments on commit 82495e8

Please sign in to comment.