Skip to content

Commit

Permalink
Merge pull request #86 from ngtuna/kompose-up
Browse files Browse the repository at this point in the history
New behavior of kompose up
  • Loading branch information
ngtuna authored Aug 9, 2016
2 parents aaa94d7 + 8a607c8 commit 3da3fb5
Show file tree
Hide file tree
Showing 613 changed files with 113,273 additions and 119 deletions.
571 changes: 570 additions & 1 deletion Godeps/Godeps.json

Large diffs are not rendered by default.

236 changes: 120 additions & 116 deletions cli/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ import (
"k8s.io/kubernetes/pkg/api"
"k8s.io/kubernetes/pkg/api/unversioned"
"k8s.io/kubernetes/pkg/apis/extensions"
//client "k8s.io/kubernetes/pkg/client/unversioned"
//cmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util"
client "k8s.io/kubernetes/pkg/client/unversioned"
cmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util"
"k8s.io/kubernetes/pkg/runtime"
"k8s.io/kubernetes/pkg/util/intstr"

Expand Down Expand Up @@ -766,7 +766,7 @@ func loadImage(service bundlefile.Service) (string, string) {
}

// Load DAB file into KomposeObject
func loadBundlesFile(file string, opt convertOptions) KomposeObject {
func loadBundlesFile(file string) KomposeObject {
komposeObject := KomposeObject{
ServiceConfigs: make(map[string]ServiceConfig),
}
Expand Down Expand Up @@ -816,7 +816,7 @@ func loadBundlesFile(file string, opt convertOptions) KomposeObject {
}

// Load compose file into KomposeObject
func loadComposeFile(file string, opt convertOptions) KomposeObject {
func loadComposeFile(file string) KomposeObject {
komposeObject := KomposeObject{
ServiceConfigs: make(map[string]ServiceConfig),
}
Expand Down Expand Up @@ -908,17 +908,14 @@ type convertOptions struct {
}

// Convert komposeObject to K8S controllers
func komposeConvert(komposeObject KomposeObject, opt convertOptions) {
func komposeConvert(komposeObject KomposeObject, opt convertOptions) (map[string][]byte, map[string][]byte, map[string][]byte, map[string][]byte, map[string][]byte, []string) {
mServices := make(map[string][]byte)
mReplicationControllers := make(map[string][]byte)
mDeployments := make(map[string][]byte)
mDaemonSets := make(map[string][]byte)
// OpenShift DeploymentConfigs
mDeploymentConfigs := make(map[string][]byte)

f := createOutFile(opt.outFile)
defer f.Close()

var svcnames []string

for name, service := range komposeObject.ServiceConfigs {
Expand Down Expand Up @@ -1041,6 +1038,10 @@ func komposeConvert(komposeObject KomposeObject, opt convertOptions) {
mDeploymentConfigs[name] = dataDeploymentConfig
}

return mServices, mDeployments, mDaemonSets, mReplicationControllers, mDeploymentConfigs, svcnames
}

func printControllers(mServices, mDeployments, mDaemonSets, mReplicationControllers, mDeploymentConfigs map[string][]byte, svcnames []string, opt convertOptions, f *os.File) {
for k, v := range mServices {
if v != nil {
print(k, "svc", v, opt.toStdout, opt.generateYaml, f)
Expand Down Expand Up @@ -1084,48 +1085,28 @@ func komposeConvert(komposeObject KomposeObject, opt convertOptions) {
}
}

// 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 @@ -1135,10 +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
// Convert komposeObject to K8S controllers
if len(dabFile) > 0 {
file = dabFile
}

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

validateFlags(opt, singleOutput, dabFile, inputFile)

komposeObject := KomposeObject{}

if len(dabFile) > 0 {
komposeObject = loadBundlesFile(dabFile, opt)
file = dabFile
komposeObject = loadBundlesFile(dabFile)
} else {
komposeObject = loadComposeFile(inputFile, opt)
komposeObject = loadComposeFile(inputFile)
}

komposeConvert(komposeObject, opt)
// Convert komposeObject to K8S controllers
mServices, mDeployments, mDaemonSets, mReplicationControllers, mDeploymentConfigs, svcnames := komposeConvert(komposeObject, opt)

f := createOutFile(opt.outFile)
defer f.Close()

// Print output
printControllers(mServices, mDeployments, mDaemonSets, mReplicationControllers, mDeploymentConfigs, svcnames, opt, f)
}

func checkUnsupportedKey(service interface{}) {
Expand Down Expand Up @@ -1200,82 +1214,72 @@ func print(name, trailing string, data []byte, toStdout, generateYaml bool, f *o
}
}

// Up brings up rc, svc.
// Up brings up deployment, svc.
func Up(c *cli.Context) {
//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)
//
//files, err := ioutil.ReadDir(".")
//if err != nil {
// logrus.Fatalf("Failed to load rc, svc manifest files: %s\n", err)
//}
//
//// submit svc first
//sc := &api.Service{}
//for _, file := range files {
// if strings.Contains(file.Name(), "svc") {
// datasvc, err := ioutil.ReadFile(file.Name())
//
// if err != nil {
// logrus.Fatalf("Failed to load %s: %s\n", file.Name(), err)
// }
//
// if strings.Contains(file.Name(), "json") {
// err := json.Unmarshal(datasvc, &sc)
// if err != nil {
// logrus.Fatalf("Failed to unmarshal file %s to svc object: %s\n", file.Name(), err)
// }
// }
// if strings.Contains(file.Name(), "yaml") {
// err := yaml.Unmarshal(datasvc, &sc)
// if err != nil {
// logrus.Fatalf("Failed to unmarshal file %s to svc object: %s\n", file.Name(), err)
// }
// }
// // submit sc to k8s
// scCreated, err := client.Services(api.NamespaceDefault).Create(sc)
// if err != nil {
// fmt.Println(err)
// }
// logrus.Debugf("%s\n", scCreated)
// }
//}
//
//// then submit rc
//rc := &api.ReplicationController{}
//for _, file := range files {
// if strings.Contains(file.Name(), "rc") {
// datarc, err := ioutil.ReadFile(file.Name())
//
// if err != nil {
// logrus.Fatalf("Failed to load %s: %s\n", file.Name(), err)
// }
//
// if strings.Contains(file.Name(), "json") {
// err := json.Unmarshal(datarc, &rc)
// if err != nil {
// logrus.Fatalf("Failed to unmarshal file %s to rc object: %s\n", file.Name(), err)
// }
// }
// if strings.Contains(file.Name(), "yaml") {
// err := yaml.Unmarshal(datarc, &rc)
// if err != nil {
// logrus.Fatalf("Failed to unmarshal file %s to rc object: %s\n", file.Name(), err)
// }
// }
// // submit rc to k8s
// rcCreated, err := client.ReplicationControllers(api.NamespaceDefault).Create(rc)
// if err != nil {
// fmt.Println(err)
// }
// logrus.Debugf("%s\n", rcCreated)
// }
//}
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 access the Kubernetes cluster. Make sure you have a Kubernetes running: %v", err)
}
client := client.NewOrDie(clientConfig)

inputFile := c.String("file")
dabFile := c.String("bundle")

komposeObject := KomposeObject{}
opt := convertOptions{
replicas: 1,
}

validateFlags(opt, false, dabFile, inputFile)

if len(dabFile) > 0 {
komposeObject = loadBundlesFile(dabFile)
} else {
komposeObject = loadComposeFile(inputFile)
}

// Convert komposeObject to K8S controllers
mServices, mDeployments, _, _, _, _ := komposeConvert(komposeObject, opt)

// submit svc first
sc := &api.Service{}
for k, v := range mServices {
err := json.Unmarshal(v, &sc)
if err != nil {
logrus.Fatalf("Failed to unmarshal %s to service object: %s\n", k, err)
}
//submit sc to k8s
scCreated, err := client.Services(api.NamespaceDefault).Create(sc)
if err != nil {
logrus.Fatalf("Failed to create service %s: ", k, err)
} else {
fmt.Printf("Service %q has been created.\n", k)
}
logrus.Debugf("%s\n", scCreated)
}

// then submit dc
dc := &extensions.Deployment{}
for k, v := range mDeployments {
err := json.Unmarshal(v, &dc)
if err != nil {
logrus.Fatalf("Failed to unmarshal %s to deployment controller object: %s\n", k, err)
}
//submit sc to k8s
dcCreated, err := client.Deployments(api.NamespaceDefault).Create(dc)
if err != nil {
logrus.Fatalf("Failed to create deployment controller %s: ", k, err)
} else {
fmt.Printf("Deployment %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
15 changes: 14 additions & 1 deletion cli/command/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,23 @@ func ConvertCommand() cli.Command {
func UpCommand() cli.Command {
return cli.Command{
Name: "up",
Usage: "Submit rc, svc objects to kubernetes API endpoint",
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 alternative compose file (default: %s)", app.DefaultComposeFile),
Value: app.DefaultComposeFile,
EnvVar: "COMPOSE_FILE",
},
cli.StringFlag{
Name: "bundle,dab",
Usage: "Specify a Distributed Application Bundle (DAB) file",
EnvVar: "DAB_FILE",
},
},
}
}

Expand Down
2 changes: 1 addition & 1 deletion cli/main/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ func main() {
app.Flags = append(command.CommonFlags())
app.Commands = []cli.Command{
command.ConvertCommand(),
command.UpCommand(),
// TODO: enable these commands and update docs once we fix them
//command.UpCommand(),
//command.PsCommand(),
//command.DeleteCommand(),
//command.ScaleCommand(),
Expand Down
20 changes: 20 additions & 0 deletions vendor/github.com/beorn7/perks/LICENSE

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 3da3fb5

Please sign in to comment.