mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-08-19 08:40:42 +00:00
contrib/podex: switch bool flags to strings
This commit is contained in:
parent
f133c118cf
commit
8655d7cd07
@ -28,9 +28,11 @@ limitations under the License.
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
@ -41,13 +43,18 @@ import (
|
|||||||
goyaml "gopkg.in/v2/yaml"
|
goyaml "gopkg.in/v2/yaml"
|
||||||
)
|
)
|
||||||
|
|
||||||
const usage = "usage: podex [-yaml|-json] [-pod|-container] [-id PODNAME] IMAGES..."
|
const usage = "podex [-format=yaml|json] [-type=pod|container] [-id NAME] IMAGES..."
|
||||||
|
|
||||||
var generateYAML = flag.Bool("yaml", true, "generate yaml manifest (default)")
|
var manifestFormat = flag.String("format", "yaml", "manifest format to output, `yaml` or `json`")
|
||||||
var generateJSON = flag.Bool("json", false, "generate json manifest")
|
var manifestType = flag.String("type", "pod", "manifest type to output, `pod` or `container`")
|
||||||
var generatePod = flag.Bool("pod", true, "generate pod manifest (default)")
|
var manifestId = flag.String("id", "", "manifest id, default to image base name")
|
||||||
var generateContainer = flag.Bool("container", false, "generate container manifest")
|
|
||||||
var id = flag.String("id", "", "set id")
|
func init() {
|
||||||
|
flag.Usage = func() {
|
||||||
|
fmt.Fprintf(os.Stderr, "Usage: %s\n", usage)
|
||||||
|
flag.PrintDefaults()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
type image struct {
|
type image struct {
|
||||||
Host string
|
Host string
|
||||||
@ -60,21 +67,15 @@ func main() {
|
|||||||
flag.Parse()
|
flag.Parse()
|
||||||
|
|
||||||
if flag.NArg() < 1 {
|
if flag.NArg() < 1 {
|
||||||
log.Fatal(usage)
|
flag.Usage()
|
||||||
|
log.Fatal("pod: missing image argument")
|
||||||
}
|
}
|
||||||
if *id == "" {
|
if *manifestId == "" {
|
||||||
if flag.NArg() > 1 {
|
if flag.NArg() > 1 {
|
||||||
log.Print(usage)
|
flag.Usage()
|
||||||
log.Fatal("podex: -id arg is required when passing more than one image")
|
log.Fatal("podex: -id arg is required when passing more than one image")
|
||||||
}
|
}
|
||||||
_, _, *id, _ = splitDockerImageName(flag.Arg(0))
|
_, _, *manifestId, _ = splitDockerImageName(flag.Arg(0))
|
||||||
}
|
|
||||||
|
|
||||||
if *generateJSON {
|
|
||||||
*generateYAML = false
|
|
||||||
}
|
|
||||||
if *generateContainer {
|
|
||||||
*generatePod = false
|
|
||||||
}
|
}
|
||||||
|
|
||||||
podContainers := []goyaml.MapSlice{}
|
podContainers := []goyaml.MapSlice{}
|
||||||
@ -113,41 +114,54 @@ func main() {
|
|||||||
|
|
||||||
// TODO(proppy): add flag to handle multiple version
|
// TODO(proppy): add flag to handle multiple version
|
||||||
containerManifest := goyaml.MapSlice{
|
containerManifest := goyaml.MapSlice{
|
||||||
{Key: "version", Value: "v1beta1"},
|
{Key: "version", Value: "v1beta2"},
|
||||||
{Key: "containers", Value: podContainers},
|
{Key: "containers", Value: podContainers},
|
||||||
}
|
}
|
||||||
|
|
||||||
var data interface{}
|
var data interface{}
|
||||||
|
|
||||||
switch {
|
switch *manifestType {
|
||||||
case *generateContainer:
|
case "container":
|
||||||
containerManifest = append(goyaml.MapSlice{
|
containerManifest = append(goyaml.MapSlice{
|
||||||
{Key: "id", Value: *id},
|
{Key: "id", Value: *manifestId},
|
||||||
}, containerManifest...)
|
}, containerManifest...)
|
||||||
data = containerManifest
|
data = containerManifest
|
||||||
case *generatePod:
|
case "pod":
|
||||||
data = goyaml.MapSlice{
|
data = goyaml.MapSlice{
|
||||||
{Key: "id", Value: *id},
|
{Key: "id", Value: *manifestId},
|
||||||
{Key: "kind", Value: "Pod"},
|
{Key: "kind", Value: "Pod"},
|
||||||
{Key: "apiVersion", Value: "v1beta1"},
|
{Key: "apiVersion", Value: "v1beta1"},
|
||||||
{Key: "desiredState", Value: goyaml.MapSlice{
|
{Key: "desiredState", Value: goyaml.MapSlice{
|
||||||
{Key: "manifest", Value: containerManifest},
|
{Key: "manifest", Value: containerManifest},
|
||||||
}},
|
}},
|
||||||
}
|
}
|
||||||
|
default:
|
||||||
|
flag.Usage()
|
||||||
|
log.Fatalf("unsupported manifest type %q", *manifestType)
|
||||||
}
|
}
|
||||||
|
|
||||||
bs, err := goyaml.Marshal(data)
|
yamlBytes, err := goyaml.Marshal(data)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("failed to marshal container manifest: %v", err)
|
log.Fatalf("failed to marshal container manifest: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if *generateJSON {
|
switch *manifestFormat {
|
||||||
bs, err = yaml.YAMLToJSON(bs)
|
case "yaml":
|
||||||
|
os.Stdout.Write(yamlBytes)
|
||||||
|
case "json":
|
||||||
|
jsonBytes, err := yaml.YAMLToJSON(yamlBytes)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("failed to marshal container manifest into JSON: %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)
|
||||||
|
}
|
||||||
|
io.Copy(os.Stdout, &jsonPretty)
|
||||||
|
default:
|
||||||
|
flag.Usage()
|
||||||
|
log.Fatalf("unsupported manifest format %q", *manifestFormat)
|
||||||
}
|
}
|
||||||
os.Stdout.Write(bs)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// splitDockerImageName split a docker image name of the form [HOST/][NAMESPACE/]REPOSITORY[:TAG]
|
// splitDockerImageName split a docker image name of the form [HOST/][NAMESPACE/]REPOSITORY[:TAG]
|
||||||
|
Loading…
Reference in New Issue
Block a user