github.com/deemoprobe/k8s-first-commit@v0.0.0-20230430165612-a541f1982be3/cmd/cloudcfg/cloudcfg.go (about) 1 /* 2 Copyright 2014 Google Inc. All rights reserved. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 package main 17 18 import ( 19 "flag" 20 "fmt" 21 "log" 22 "net/http" 23 "os" 24 "strconv" 25 "time" 26 27 kube_client "github.com/GoogleCloudPlatform/kubernetes/pkg/client" 28 "github.com/GoogleCloudPlatform/kubernetes/pkg/cloudcfg" 29 ) 30 31 const APP_VERSION = "0.1" 32 33 // The flag package provides a default help printer via -h switch 34 var versionFlag *bool = flag.Bool("v", false, "Print the version number.") 35 var httpServer *string = flag.String("h", "", "The host to connect to.") 36 var config *string = flag.String("c", "", "Path to the config file.") 37 var labelQuery *string = flag.String("l", "", "Label query to use for listing") 38 var updatePeriod *time.Duration = flag.Duration("u", 60*time.Second, "Update interarrival in seconds") 39 var portSpec *string = flag.String("p", "", "The port spec, comma-separated list of <external>:<internal>,...") 40 var servicePort *int = flag.Int("s", -1, "If positive, create and run a corresponding service on this port, only used with 'run'") 41 var authConfig *string = flag.String("auth", os.Getenv("HOME")+"/.kubernetes_auth", "Path to the auth info file. If missing, prompt the user") 42 43 func usage() { 44 log.Fatal("Usage: cloudcfg -h <host> [-c config/file.json] [-p <hostPort>:<containerPort>,..., <hostPort-n>:<containerPort-n> <method> <path>") 45 } 46 47 // CloudCfg command line tool. 48 func main() { 49 flag.Parse() // Scan the arguments list 50 51 if *versionFlag { 52 fmt.Println("Version:", APP_VERSION) 53 os.Exit(0) 54 } 55 56 if len(flag.Args()) < 2 { 57 usage() 58 } 59 method := flag.Arg(0) 60 url := *httpServer + "/api/v1beta1" + flag.Arg(1) 61 var request *http.Request 62 var err error 63 64 auth, err := cloudcfg.LoadAuthInfo(*authConfig) 65 if err != nil { 66 log.Fatalf("Error loading auth: %#v", err) 67 } 68 69 if method == "get" || method == "list" { 70 if len(*labelQuery) > 0 && method == "list" { 71 url = url + "?labels=" + *labelQuery 72 } 73 request, err = http.NewRequest("GET", url, nil) 74 } else if method == "delete" { 75 request, err = http.NewRequest("DELETE", url, nil) 76 } else if method == "create" { 77 request, err = cloudcfg.RequestWithBody(*config, url, "POST") 78 } else if method == "update" { 79 request, err = cloudcfg.RequestWithBody(*config, url, "PUT") 80 } else if method == "rollingupdate" { 81 client := &kube_client.Client{ 82 Host: *httpServer, 83 Auth: &auth, 84 } 85 cloudcfg.Update(flag.Arg(1), client, *updatePeriod) 86 } else if method == "run" { 87 args := flag.Args() 88 if len(args) < 4 { 89 log.Fatal("usage: cloudcfg -h <host> run <image> <replicas> <name>") 90 } 91 image := args[1] 92 replicas, err := strconv.Atoi(args[2]) 93 name := args[3] 94 if err != nil { 95 log.Fatalf("Error parsing replicas: %#v", err) 96 } 97 err = cloudcfg.RunController(image, name, replicas, kube_client.Client{Host: *httpServer, Auth: &auth}, *portSpec, *servicePort) 98 if err != nil { 99 log.Fatalf("Error: %#v", err) 100 } 101 return 102 } else if method == "stop" { 103 err = cloudcfg.StopController(flag.Arg(1), kube_client.Client{Host: *httpServer, Auth: &auth}) 104 if err != nil { 105 log.Fatalf("Error: %#v", err) 106 } 107 return 108 } else if method == "rm" { 109 err = cloudcfg.DeleteController(flag.Arg(1), kube_client.Client{Host: *httpServer, Auth: &auth}) 110 if err != nil { 111 log.Fatalf("Error: %#v", err) 112 } 113 return 114 } else { 115 log.Fatalf("Unknown command: %s", method) 116 } 117 if err != nil { 118 log.Fatalf("Error: %#v", err) 119 } 120 var body string 121 body, err = cloudcfg.DoRequest(request, auth.User, auth.Password) 122 if err != nil { 123 log.Fatalf("Error: %#v", err) 124 } 125 fmt.Println(body) 126 }