kompose/pkg/app/app.go
Charlie Drage 1b9228e696 Switch to spf13/cobra from urfave/cli
There's A LOT happening in this commit, so here's an outline:

First off, urfave/cli has been removed in favour of spf13/cobra. With
this, comes changes to the formatting as well as the help page for
Kompose.

Upon converting, I noticed a CLI flag was NOT appearing for OpenShift.
Specifically, --deploymentconfig. This has been added with a note
that says it is OpenShift only.

Exit codes have been fixed. If the conversion / down / up fails for
any reason, Kompose will exit with Code 1.

--verbose as well as --suppress-warnings can now be set at the
same time.

app_test.go in the cli directory has been moved to pkg/transformer
to better reflect the testing coverage.

version.go has been removed and converted to it's own CLI command in
conjuction with (most) Go software. A new CLI command has been
created. kompose version

--dab isn't a conventional way for short-form CLI paramters. This
has been shortened to -b for bundle.

CLI flags consisting of only two/three letters have been removed due to
it being unconventional for CLI. For example, --dc was removed in preference
for --deploymentconfig

--replicas has been added as an option when using kompose down or
kompose up. This has been added as previously in app.go the
replica amount was hard-coded as 1.

Differentiating names have been used for flags. For example,
persistent flags use the name Global (ex. GlobalOut). Command-specific
flags have their own names (ex. UpOpt).

Closes #239 #253
2016-12-22 08:15:51 -05:00

274 lines
7.2 KiB
Go

/*
Copyright 2016 The Kubernetes Authors All rights reserved.
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 app
import (
"fmt"
"strings"
"github.com/Sirupsen/logrus"
"github.com/spf13/cobra"
// install kubernetes api
_ "k8s.io/kubernetes/pkg/api/install"
_ "k8s.io/kubernetes/pkg/apis/extensions/install"
// install OpenShift api
_ "github.com/openshift/origin/pkg/deploy/api/install"
_ "github.com/openshift/origin/pkg/image/api/install"
_ "github.com/openshift/origin/pkg/route/api/install"
"github.com/kubernetes-incubator/kompose/pkg/kobject"
"github.com/kubernetes-incubator/kompose/pkg/loader"
"github.com/kubernetes-incubator/kompose/pkg/transformer"
"github.com/kubernetes-incubator/kompose/pkg/transformer/kubernetes"
"github.com/kubernetes-incubator/kompose/pkg/transformer/openshift"
)
const (
DefaultComposeFile = "docker-compose.yml"
DefaultProvider = "kubernetes"
)
var inputFormat = "compose"
func ValidateFlags(bundle string, args []string, cmd *cobra.Command, opt *kobject.ConvertOptions) {
// Check to see if the "file" has changed from the default flag value
isFileSet := cmd.Flags().Lookup("file").Changed
if opt.OutFile == "-" {
opt.ToStdout = true
opt.OutFile = ""
}
// Get the provider
provider := cmd.Flags().Lookup("provider").Value.String()
logrus.Debug("Checking validation of provider %s", provider)
// OpenShift specific flags
deploymentConfig := cmd.Flags().Lookup("deployment-config").Changed
// Kubernetes specific flags
chart := cmd.Flags().Lookup("chart").Changed
daemonSet := cmd.Flags().Lookup("daemon-set").Changed
replicationController := cmd.Flags().Lookup("replication-controller").Changed
deployment := cmd.Flags().Lookup("deployment").Changed
// Check validations against provider flags
switch {
case provider == "openshift":
if chart {
logrus.Fatalf("--chart, -c is a Kubernetes only flag")
}
if daemonSet {
logrus.Fatalf("--daemon-set is a Kubernetes only flag")
}
if replicationController {
logrus.Fatalf("--replication-controller is a Kubernetes only flag")
}
if deployment {
logrus.Fatalf("--deployment, -d is a Kubernetes only flag")
}
case provider == "kubernetes":
if deploymentConfig {
logrus.Fatalf("--deployment-config is an OpenShift only flag")
}
}
// Standard checks regardless of provider
if len(opt.OutFile) != 0 && opt.ToStdout {
logrus.Fatalf("Error: --out and --stdout can't be set at the same time")
}
if opt.CreateChart && opt.ToStdout {
logrus.Fatalf("Error: chart cannot be generated when --stdout is specified")
}
if opt.Replicas < 0 {
logrus.Fatalf("Error: --replicas cannot be negative")
}
if len(bundle) > 0 {
inputFormat = "bundle"
opt.InputFile = bundle
}
if len(bundle) > 0 && isFileSet {
logrus.Fatalf("Error: 'compose' file and 'dab' file cannot be specified at the same time")
}
if len(args) != 0 {
logrus.Fatal("Unknown Argument(s): ", strings.Join(args, ","))
}
}
func validateControllers(opt *kobject.ConvertOptions) {
singleOutput := len(opt.OutFile) != 0 || opt.OutFile == "-" || opt.ToStdout
if opt.Provider == "kubernetes" {
// create deployment by default if no controller has been set
if !opt.CreateD && !opt.CreateDS && !opt.CreateRC {
opt.CreateD = true
}
if singleOutput {
count := 0
if opt.CreateD {
count++
}
if opt.CreateDS {
count++
}
if opt.CreateRC {
count++
}
if count > 1 {
logrus.Fatalf("Error: only one kind of Kubernetes resource can be generated when --out or --stdout is specified")
}
}
} else if opt.Provider == "openshift" {
// create deploymentconfig by default if no controller has been set
if !opt.CreateDeploymentConfig {
opt.CreateDeploymentConfig = true
}
if singleOutput {
count := 0
if opt.CreateDeploymentConfig {
count++
}
// Add more controllers here once they are available in OpenShift
// if opt.foo {count++}
if count > 1 {
logrus.Fatalf("Error: only one kind of OpenShift resource can be generated when --out or --stdout is specified")
}
}
}
}
// Convert transforms docker compose or dab file to k8s objects
func Convert(opt kobject.ConvertOptions) {
validateControllers(&opt)
// loader parses input from file into komposeObject.
l, err := loader.GetLoader(inputFormat)
if err != nil {
logrus.Fatal(err)
}
komposeObject := kobject.KomposeObject{
ServiceConfigs: make(map[string]kobject.ServiceConfig),
}
komposeObject = l.LoadFile(opt.InputFile)
// Get a transformer that maps komposeObject to provider's primitives
t := getTransformer(opt)
// Do the transformation
objects := t.Transform(komposeObject, opt)
// Print output
kubernetes.PrintList(objects, opt)
}
// Up brings up deployment, svc.
func Up(opt kobject.ConvertOptions) {
validateControllers(&opt)
// loader parses input from file into komposeObject.
l, err := loader.GetLoader(inputFormat)
if err != nil {
logrus.Fatal(err)
}
komposeObject := kobject.KomposeObject{
ServiceConfigs: make(map[string]kobject.ServiceConfig),
}
komposeObject = l.LoadFile(opt.InputFile)
// Get the transformer
t := getTransformer(opt)
//Submit objects to provider
errDeploy := t.Deploy(komposeObject, opt)
if errDeploy != nil {
logrus.Fatalf("Error while deploying application: %s", errDeploy)
}
}
// Down deletes all deployment, svc.
func Down(opt kobject.ConvertOptions) {
validateControllers(&opt)
// loader parses input from file into komposeObject.
l, err := loader.GetLoader(inputFormat)
if err != nil {
logrus.Fatal(err)
}
komposeObject := kobject.KomposeObject{
ServiceConfigs: make(map[string]kobject.ServiceConfig),
}
komposeObject = l.LoadFile(opt.InputFile)
// Get the transformer
t := getTransformer(opt)
//Remove deployed application
errUndeploy := t.Undeploy(komposeObject, opt)
if errUndeploy != nil {
logrus.Fatalf("Error while deleting application: %s", errUndeploy)
}
}
// Convenience method to return the appropriate Transformer based on
// what provider we are using.
func getTransformer(opt kobject.ConvertOptions) transformer.Transformer {
var t transformer.Transformer
if opt.Provider == "kubernetes" {
// Create/Init new Kubernetes object with CLI opts
t = &kubernetes.Kubernetes{Opt: opt}
} else {
// Create/Init new OpenShift object that is initialized with a newly
// created Kubernetes object. Openshift inherits from Kubernetes
t = &openshift.OpenShift{Kubernetes: kubernetes.Kubernetes{Opt: opt}}
}
return t
}
func askForConfirmation() bool {
var response string
_, err := fmt.Scanln(&response)
if err != nil {
logrus.Fatal(err)
}
if response == "yes" {
return true
} else if response == "no" {
return false
} else {
fmt.Println("Please type yes or no and then press enter:")
return askForConfirmation()
}
}