Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

contrib/podex: fix ordering and manifest generation #3357

Merged
merged 5 commits into from
Jan 30, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
123 changes: 80 additions & 43 deletions contrib/podex/podex.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,22 +32,29 @@ import (
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"io"
"log"
"net/http"
"os"
"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 = "podex [-format=yaml|json] [-type=pod|container] [-id NAME] 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 manifestFormat = flag.String("format", "yaml", "manifest format to output, `yaml` or `json`")
var manifestType = flag.String("type", "pod", "manifest type to output, `pod` or `container`")
var manifestName = flag.String("name", "", "manifest name, default to image base name")

func init() {
flag.Usage = func() {
fmt.Fprintf(os.Stderr, "Usage: %s\n", usage)
flag.PrintDefaults()
}
}

type image struct {
Host string
Expand All @@ -60,72 +67,105 @@ func main() {
flag.Parse()

if flag.NArg() < 1 {
log.Fatal(usage)
flag.Usage()
log.Fatal("pod: missing image argument")
}
if *podName == "" {
if *manifestName == "" {
if flag.NArg() > 1 {
log.Print(usage)
flag.Usage()
log.Fatal("podex: -id arg is required when passing more than one image")
}
_, _, *podName, _ = splitDockerImageName(flag.Arg(0))
_, _, *manifestName, _ = splitDockerImageName(flag.Arg(0))
}

if (!*generateJSON && !*generateYAML) || (*generateJSON && *generateYAML) {
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)

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: "v1beta2"},
{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 *manifestType {
case "container":
containerManifest = append(goyaml.MapSlice{
{Key: "id", Value: *manifestName},
}, containerManifest...)
data = containerManifest
case "pod":
data = goyaml.MapSlice{
{Key: "id", Value: *manifestName},
{Key: "kind", Value: "Pod"},
{Key: "apiVersion", Value: "v1beta1"},
{Key: "desiredState", Value: goyaml.MapSlice{
{Key: "manifest", Value: containerManifest},
}},
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add error condition?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

os.Stdout.Write(bs)
default:
flag.Usage()
log.Fatalf("unsupported manifest type %q", *manifestType)
}
if *generateYAML {
bs, err := yaml.Marshal(manifest)

yamlBytes, err := goyaml.Marshal(data)
if err != nil {
log.Fatalf("failed to marshal container manifest: %v", err)
}

switch *manifestFormat {
case "yaml":
os.Stdout.Write(yamlBytes)
case "json":
jsonBytes, err := yaml.YAMLToJSON(yamlBytes)
if err != nil {
log.Fatalf("failed to render YAML container manifest: %v", err)
log.Fatalf("failed to marshal container manifest into JSON: %v", err)
}
var jsonPretty bytes.Buffer
if err := json.Indent(&jsonPretty, jsonBytes, "", " "); err != nil {
log.Fatalf("failed to indent json %q: %v", string(jsonBytes), err)
}
os.Stdout.Write(bs)
io.Copy(os.Stdout, &jsonPretty)
default:
flag.Usage()
log.Fatalf("unsupported manifest format %q", *manifestFormat)
}
}

Expand Down Expand Up @@ -215,11 +255,8 @@ func getImageMetadata(host, namespace, repo, tag string) (*imageMetadata, error)
if err != nil {
return nil, fmt.Errorf("error getting json for image %q: %v", imageID, err)
}
data, _ := ioutil.ReadAll(resp.Body)
buf := bytes.NewBuffer(data)
log.Print(string(data))
var image imageMetadata
if err := json.NewDecoder(buf).Decode(&image); err != nil {
if err := json.NewDecoder(resp.Body).Decode(&image); err != nil {
return nil, fmt.Errorf("error decoding image %q metadata: %v", imageID, err)
}
return &image, nil
Expand Down
1 change: 1 addition & 0 deletions hack/test-go.sh
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ kube::test::find_dirs() {
-o -wholename './target' \
-o -wholename '*/third_party/*' \
-o -wholename '*/Godeps/*' \
-o -wholename '*/contrib/podex/*' \
\) -prune \
\) -name '*_test.go' -print0 | xargs -0n1 dirname | sed 's|^\./||' | sort -u
)
Expand Down