From 1f0d0cdc31dd9152353cbf8927d219238168fef4 Mon Sep 17 00:00:00 2001 From: Johan Euphrosine Date: Fri, 9 Jan 2015 14:54:54 +0000 Subject: [PATCH] contrib/podex: switch to go-yaml to manage ordering --- contrib/podex/podex.go | 85 ++++++++++++++++++++++++++---------------- 1 file changed, 53 insertions(+), 32 deletions(-) diff --git a/contrib/podex/podex.go b/contrib/podex/podex.go index ab69f7708f..2039370a26 100644 --- a/contrib/podex/podex.go +++ b/contrib/podex/podex.go @@ -37,15 +37,17 @@ import ( "strconv" "strings" - "github.com/GoogleCloudPlatform/kubernetes/pkg/api/v1beta1" "github.com/ghodss/yaml" + goyaml "gopkg.in/v2/yaml" ) -const usage = "usage: podex [-json|-yaml] [-id PODNAME] IMAGES" +const usage = "usage: podex [-yaml|-json] [-pod|-container] [-id PODNAME] IMAGES..." -var generateJSON = flag.Bool("json", false, "generate json manifest") var generateYAML = flag.Bool("yaml", false, "generate yaml manifest") -var podName = flag.String("id", "", "set pod name") +var generateJSON = flag.Bool("json", false, "generate json manifest") +var pod = flag.Bool("pod", false, "generate pod manifest") +var container = flag.Bool("container", false, "generate container manifest") +var id = flag.String("id", "", "set id") type image struct { Host string @@ -60,26 +62,26 @@ func main() { if flag.NArg() < 1 { log.Fatal(usage) } - if *podName == "" { + if *id == "" { if flag.NArg() > 1 { log.Print(usage) log.Fatal("podex: -id arg is required when passing more than one image") } - _, _, *podName, _ = splitDockerImageName(flag.Arg(0)) + _, _, *id, _ = splitDockerImageName(flag.Arg(0)) } - if (!*generateJSON && !*generateYAML) || (*generateJSON && *generateYAML) { + if (!*generateYAML && !*generateJSON) || (*generateYAML && *generateJSON) || (!*pod && !*container) || (*pod && *container) { log.Fatal(usage) } - podContainers := []v1beta1.Container{} + podContainers := []goyaml.MapSlice{} for _, imageName := range flag.Args() { host, namespace, repo, tag := splitDockerImageName(imageName) - container := v1beta1.Container{ - Name: repo, - Image: imageName, + container := goyaml.MapSlice{ + {Key: "name", Value: repo}, + {Key: "image", Value: imageName}, } img, err := getImageMetadata(host, namespace, repo, tag) @@ -87,44 +89,63 @@ func main() { if err != nil { log.Fatalf("failed to get image metadata %q: %v", imageName, err) } + portSlice := []goyaml.MapSlice{} for p := range img.ContainerConfig.ExposedPorts { port, err := strconv.Atoi(p.Port()) if err != nil { log.Fatalf("failed to parse port %q: %v", p.Port(), err) } - container.Ports = append(container.Ports, v1beta1.Port{ - Name: strings.Join([]string{repo, p.Proto(), p.Port()}, "-"), - ContainerPort: port, - Protocol: v1beta1.Protocol(strings.ToUpper(p.Proto())), - }) + portEntry := goyaml.MapSlice{{ + Key: "name", Value: strings.Join([]string{repo, p.Proto(), p.Port()}, "-")}, {Key: "containerPort", Value: port}} + portSlice = append(portSlice, portEntry) + if p.Proto() != "tcp" { + portEntry = append(portEntry, goyaml.MapItem{Key: "protocol", Value: strings.ToUpper(p.Proto())}) + } + } + if len(img.ContainerConfig.ExposedPorts) > 0 { + container = append(container, goyaml.MapItem{Key: "ports", Value: portSlice}) } podContainers = append(podContainers, container) } // TODO(proppy): add flag to handle multiple version - manifest := v1beta1.ContainerManifest{ - Version: "v1beta1", - ID: *podName + "-pod", - Containers: podContainers, - RestartPolicy: v1beta1.RestartPolicy{ - Always: &v1beta1.RestartPolicyAlways{}, - }, + containerManifest := goyaml.MapSlice{ + {Key: "version", Value: "v1beta1"}, + {Key: "containers", Value: podContainers}, } - if *generateJSON { - bs, err := json.MarshalIndent(manifest, "", " ") - if err != nil { - log.Fatalf("failed to render JSON container manifest: %v", err) + var data interface{} + + switch { + case *container: + containerManifest = append(goyaml.MapSlice{ + {Key: "id", Value: *id}, + }, containerManifest...) + data = containerManifest + case *pod: + data = goyaml.MapSlice{ + {Key: "id", Value: *id}, + {Key: "kind", Value: "Pod"}, + {Key: "apiVersion", Value: "v1beta1"}, + {Key: "desiredState", Value: goyaml.MapSlice{ + {Key: "manifest", Value: containerManifest}, + }}, } - os.Stdout.Write(bs) } - if *generateYAML { - bs, err := yaml.Marshal(manifest) + + bs, err := goyaml.Marshal(data) + if err != nil { + log.Fatalf("failed to marshal container manifest: %v", err) + } + + switch { + case *generateJSON: + bs, err = yaml.YAMLToJSON(bs) if err != nil { - log.Fatalf("failed to render YAML container manifest: %v", err) + log.Fatalf("failed to marshal container manifest into JSON: %v", err) } - os.Stdout.Write(bs) } + os.Stdout.Write(bs) } // splitDockerImageName split a docker image name of the form [HOST/][NAMESPACE/]REPOSITORY[:TAG]