forked from LaconicNetwork/kompose
Support printing to stdout
This commit is contained in:
parent
53802333fe
commit
6ae79d0e70
@ -19,6 +19,7 @@ package app
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"math/rand"
|
"math/rand"
|
||||||
|
"os"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
@ -211,6 +212,7 @@ func ProjectKuberScale(p *project.Project, c *cli.Context) {
|
|||||||
// ProjectKuberConvert tranforms docker compose to k8s objects
|
// ProjectKuberConvert tranforms docker compose to k8s objects
|
||||||
func ProjectKuberConvert(p *project.Project, c *cli.Context) {
|
func ProjectKuberConvert(p *project.Project, c *cli.Context) {
|
||||||
generateYaml := false
|
generateYaml := false
|
||||||
|
toStdout := false
|
||||||
composeFile := c.String("file")
|
composeFile := c.String("file")
|
||||||
|
|
||||||
p = project.NewProject(&project.Context{
|
p = project.NewProject(&project.Context{
|
||||||
@ -226,6 +228,10 @@ func ProjectKuberConvert(p *project.Project, c *cli.Context) {
|
|||||||
generateYaml = true
|
generateYaml = true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if c.BoolT("stdout") {
|
||||||
|
toStdout = true
|
||||||
|
}
|
||||||
|
|
||||||
var mServices map[string]api.Service = make(map[string]api.Service)
|
var mServices map[string]api.Service = make(map[string]api.Service)
|
||||||
var serviceLinks []string
|
var serviceLinks []string
|
||||||
|
|
||||||
@ -624,47 +630,21 @@ func ProjectKuberConvert(p *project.Project, c *cli.Context) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fileRC := fmt.Sprintf("%s-rc.json", name)
|
|
||||||
if generateYaml == true {
|
|
||||||
fileRC = fmt.Sprintf("%s-rc.yaml", name)
|
|
||||||
}
|
|
||||||
if err := ioutil.WriteFile(fileRC, []byte(datarc), 0644); err != nil {
|
|
||||||
logrus.Fatalf("Failed to write replication controller: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Create the deployment container */
|
|
||||||
if c.BoolT("deployment") {
|
if c.BoolT("deployment") {
|
||||||
fileDC := fmt.Sprintf("%s-deployment.json", name)
|
// Create the deployment
|
||||||
if generateYaml == true {
|
print(name, "deployment", datadc, toStdout, generateYaml)
|
||||||
fileDC = fmt.Sprintf("%s-deployment.yaml", name)
|
} else if c.BoolT("daemonset") {
|
||||||
}
|
// Create the daemonset
|
||||||
if err := ioutil.WriteFile(fileDC, []byte(datadc), 0644); err != nil {
|
print(name, "daemonset", datads, toStdout, generateYaml)
|
||||||
logrus.Fatalf("Failed to write deployment container: %v", err)
|
} else if c.BoolT("replicaset") {
|
||||||
}
|
// Create the replicaset container
|
||||||
}
|
print(name, "replicaset", datars, toStdout, generateYaml)
|
||||||
|
} else {
|
||||||
/* Create the daemonset container */
|
// Create the replication controller
|
||||||
if c.BoolT("daemonset") {
|
print(name, "rc", datarc, toStdout, generateYaml)
|
||||||
fileDS := fmt.Sprintf("%s-daemonset.json", name)
|
|
||||||
if generateYaml == true {
|
|
||||||
fileDS = fmt.Sprintf("%s-daemonset.yaml", name)
|
|
||||||
}
|
|
||||||
if err := ioutil.WriteFile(fileDS, []byte(datads), 0644); err != nil {
|
|
||||||
logrus.Fatalf("Failed to write daemonset: %v", err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Create the replicaset container */
|
|
||||||
if c.BoolT("replicaset") {
|
|
||||||
fileRS := fmt.Sprintf("%s-replicaset.json", name)
|
|
||||||
if generateYaml == true {
|
|
||||||
fileRS = fmt.Sprintf("%s-replicaset.yaml", name)
|
|
||||||
}
|
|
||||||
if err := ioutil.WriteFile(fileRS, []byte(datars), 0644); err != nil {
|
|
||||||
logrus.Fatalf("Failed to write replicaset: %v", err)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Create the services
|
||||||
for k, v := range mServices {
|
for k, v := range mServices {
|
||||||
for i := 0; i < len(serviceLinks); i++ {
|
for i := 0; i < len(serviceLinks); i++ {
|
||||||
// convert datasvc to json / yaml
|
// convert datasvc to json / yaml
|
||||||
@ -678,16 +658,10 @@ func ProjectKuberConvert(p *project.Project, c *cli.Context) {
|
|||||||
|
|
||||||
logrus.Debugf("%s\n", datasvc)
|
logrus.Debugf("%s\n", datasvc)
|
||||||
|
|
||||||
fileSVC := fmt.Sprintf("%s-svc.json", k)
|
print(k, "svc", datasvc, toStdout, generateYaml)
|
||||||
if generateYaml == true {
|
|
||||||
fileSVC = fmt.Sprintf("%s-svc.yaml", k)
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := ioutil.WriteFile(fileSVC, []byte(datasvc), 0644); err != nil {
|
|
||||||
logrus.Fatalf("Failed to write service controller: %v", err)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Need to iterate through one more time to ensure we capture all service/rc */
|
/* Need to iterate through one more time to ensure we capture all service/rc */
|
||||||
@ -701,6 +675,25 @@ func ProjectKuberConvert(p *project.Project, c *cli.Context) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func print(name, trailing string, data []byte, toStdout, generateYaml bool) {
|
||||||
|
file := fmt.Sprintf("%s-%s.json", name, trailing)
|
||||||
|
if generateYaml {
|
||||||
|
file = fmt.Sprintf("%s-%s.yaml", name, trailing)
|
||||||
|
}
|
||||||
|
if toStdout {
|
||||||
|
separator := ""
|
||||||
|
if generateYaml {
|
||||||
|
separator = "---"
|
||||||
|
}
|
||||||
|
fmt.Fprintf(os.Stdout, "%s%s\n", string(data), separator)
|
||||||
|
} else {
|
||||||
|
if err := ioutil.WriteFile(file, []byte(data), 0644); err != nil {
|
||||||
|
logrus.Fatalf("Failed to write %s: %v", trailing, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
// ProjectKuberUp brings up rc, svc.
|
// ProjectKuberUp brings up rc, svc.
|
||||||
func ProjectKuberUp(p *project.Project, c *cli.Context) {
|
func ProjectKuberUp(p *project.Project, c *cli.Context) {
|
||||||
factory := cmdutil.NewFactory(nil)
|
factory := cmdutil.NewFactory(nil)
|
||||||
|
|||||||
@ -17,9 +17,9 @@ limitations under the License.
|
|||||||
package command
|
package command
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/urfave/cli"
|
|
||||||
"github.com/docker/libcompose/project"
|
"github.com/docker/libcompose/project"
|
||||||
"github.com/skippbox/kompose2/cli/app"
|
"github.com/skippbox/kompose2/cli/app"
|
||||||
|
"github.com/urfave/cli"
|
||||||
)
|
)
|
||||||
|
|
||||||
// ConvertCommand defines the kompose convert subcommand.
|
// ConvertCommand defines the kompose convert subcommand.
|
||||||
@ -35,6 +35,7 @@ func ConvertCommand(factory app.ProjectFactory) cli.Command {
|
|||||||
Value: "docker-compose.yml",
|
Value: "docker-compose.yml",
|
||||||
EnvVar: "COMPOSE_FILE",
|
EnvVar: "COMPOSE_FILE",
|
||||||
},
|
},
|
||||||
|
// TODO: validate the flags and make sure only one type is specified
|
||||||
cli.BoolFlag{
|
cli.BoolFlag{
|
||||||
Name: "deployment,d",
|
Name: "deployment,d",
|
||||||
Usage: "Generate a deployment resource file",
|
Usage: "Generate a deployment resource file",
|
||||||
@ -55,6 +56,10 @@ func ConvertCommand(factory app.ProjectFactory) cli.Command {
|
|||||||
Name: "yaml, y",
|
Name: "yaml, y",
|
||||||
Usage: "Generate resource file in yaml format",
|
Usage: "Generate resource file in yaml format",
|
||||||
},
|
},
|
||||||
|
cli.BoolFlag{
|
||||||
|
Name: "stdout,out",
|
||||||
|
Usage: "Print Kubernetes objects to stdout",
|
||||||
|
},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user