/
main.go
133 lines (104 loc) · 2.68 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package main
import (
"fmt"
"io"
"io/ioutil"
"log"
"os"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"github.com/datawire/ambassador/pkg/dtest"
)
// Version holds the version of the code. This is intended to be overridden at build time.
var Version = "(unknown version)"
func main() {
var k3s = &cobra.Command{
Use: "k3sctl",
Short: "k3sctl",
Long: "k3sctl - manage a local docker registry and kubernetes cluster",
SilenceErrors: true,
SilenceUsage: true,
}
var logFile *os.File
k3s.PersistentPreRunE = func(cmd *cobra.Command, args []string) error {
logFile, err := os.OpenFile("/tmp/k3sctl.log", os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
if err != nil {
return err
}
log.SetOutput(logFile)
return nil
}
var up = &cobra.Command{
Use: "up",
Short: "ensure the k3s and registry containers are up",
}
k3s.AddCommand(up)
up.RunE = func(cmd *cobra.Command, args []string) error {
regid := dtest.RegistryUp()
k3sid := dtest.K3sUp()
fmt.Printf("DOCKER_CONTAINER=%q\n", regid)
fmt.Printf("K3S_CONTAINER=%q\n", k3sid)
return nil
}
var down = &cobra.Command{
Use: "down",
Short: "shutdown the k3s and registry containers",
}
k3s.AddCommand(down)
down.RunE = func(cmd *cobra.Command, args []string) error {
k3sid := dtest.K3sDown()
regid := dtest.RegistryDown()
fmt.Printf("Shutdown k3s container: %s\n", k3sid)
fmt.Printf("Shutdown registry container: %s\n", regid)
return nil
}
var registry = &cobra.Command{
Use: "registry",
Short: "print the docker registry url",
}
k3s.AddCommand(registry)
registry.RunE = func(cmd *cobra.Command, args []string) error {
fmt.Println(dtest.DockerRegistry())
return nil
}
var config = &cobra.Command{
Use: "config",
Short: "print the k3s config",
}
output := config.Flags().StringP("output", "o", "", "path for kubeconfig file")
k3s.AddCommand(config)
config.RunE = func(cmd *cobra.Command, args []string) error {
kubeconfig := dtest.GetKubeconfig()
if kubeconfig == "" {
return errors.New("no k3s cluster is running")
}
if *output == "" {
fmt.Print(kubeconfig)
return nil
}
err := ioutil.WriteFile(*output, []byte(kubeconfig), 0644)
if err == io.EOF {
err = nil
}
if err != nil {
return err
}
fmt.Printf("Wrote kubeconfig to %s.\n", *output)
return nil
}
var version = &cobra.Command{
Use: "version",
Short: "print the k3sctl version",
}
k3s.AddCommand(version)
version.RunE = func(cmd *cobra.Command, args []string) error {
fmt.Println("k3sctl", "version", Version)
return nil
}
err := k3s.Execute()
if err != nil {
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
logFile.Close()
os.Exit(1)
}
}