/
main.go
201 lines (193 loc) · 4.74 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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
package main
import (
"fmt"
"io/ioutil"
"net/http"
"os"
"os/exec"
"path/filepath"
"text/template"
"time"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"github.com/datawire/ambassador/pkg/k8s"
"github.com/datawire/ambassador/pkg/kubeapply"
)
var tmpl = template.Must(template.
New("docker-registry.yaml").
Parse(`---
apiVersion: v1
kind: Namespace
metadata:
name: docker-registry
---
apiVersion: v1
kind: Service
metadata:
namespace: docker-registry
name: registry
spec:
type: NodePort
selector:
app: registry
ports:
- port: 5000
nodePort: 31000
---
apiVersion: apps/v1
# XXX: Avoid using a StatefulSet if possible, because kubeapply
# doesn't know how to wait for them.
kind: {{ if eq .Storage "pvc" }}StatefulSet{{ else }}Deployment{{ end }}
metadata:
namespace: docker-registry
name: registry
spec:
replicas: 1
{{ if eq .Storage "pvc" }} # XXX: StatefulSet
serviceName: registry
{{ end }}
selector:
matchLabels:
app: registry
template:
metadata:
name: registry
labels:
app: registry
spec:
containers:
- name: registry
image: docker.io/library/registry:2
ports:
- containerPort: 5000
volumeMounts:
- mountPath: /var/lib/registry
name: registry-data
volumes:
- name: registry-data
{{ if eq .Storage "pvc" | not }}
# On Kubeception clusters, there is only 1 node, so a
# hostPath is fine.
hostPath:
path: /var/lib/registry
{{ else }}
persistentVolumeClaim:
claimName: registry-data
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: registry-data
namespace: docker-registry
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 10Gi
{{ end }}
`))
func main() {
argparser := &cobra.Command{
Use: os.Args[0],
Short: "Manage a private in-cluster registry",
SilenceErrors: true,
SilenceUsage: true,
}
argparser.AddCommand(func() *cobra.Command {
var (
argStorage string
)
subparser := &cobra.Command{
Use: "up",
Short: "Initialize the registry, and create a port-forward to it",
Args: cobra.ExactArgs(0),
RunE: func(_ *cobra.Command, _ []string) error {
var kpfTarget string
switch argStorage {
case "pvc":
kpfTarget = "statefulset/registry"
case "hostPath":
kpfTarget = "deployment/registry"
default:
return errors.Errorf("invalid --storage=%q: must be one of 'pvc' or 'hostPath'", argStorage)
}
kubeinfo := k8s.NewKubeInfo("", "", "")
// Part 1: Apply the YAML
//
// pkg/kubeapply is annoyingly oriented around actual physical files >:(
tmpdir, err := ioutil.TempDir("", filepath.Base(os.Args[0]))
if err != nil {
return err
}
defer os.RemoveAll(tmpdir)
yamlFile, err := os.OpenFile(filepath.Join(tmpdir, "docker-registry.yaml"), os.O_CREATE|os.O_WRONLY, 0600)
if err != nil {
return err
}
err = tmpl.Execute(yamlFile, map[string]interface{}{
"Storage": argStorage,
})
yamlFile.Close()
if err != nil {
return err
}
err = kubeapply.Kubeapply(
kubeinfo, // kubeinfo
time.Minute, // perPhaseTimeout
false, // debug
false, // dryRun
yamlFile.Name(), // files
)
if err != nil {
return err
}
// Part 2: Set up the port-forward
args, err := kubeinfo.GetKubectlArray("port-forward",
"--namespace=docker-registry",
kpfTarget,
"31000:5000")
if err != nil {
return err
}
cmd := exec.Command("kubectl", args...)
cmd.Stdout, err = os.OpenFile(filepath.Join(os.TempDir(), filepath.Base(os.Args[0])+".log"), os.O_CREATE|os.O_WRONLY, 0666)
if err != nil {
return err
}
cmd.Stderr = cmd.Stdout
if err := cmd.Start(); err != nil {
return err
}
for {
_, httpErr := http.Get("http://localhost:31000/")
if httpErr == nil {
fmt.Fprintln(os.Stderr, "port-forward ready")
break
} else {
fmt.Fprintln(os.Stderr, "waiting for port-forward to become ready...")
time.Sleep(time.Second)
}
}
return nil
},
}
subparser.Flags().StringVar(&argStorage, "storage", "", "Which type of storage to use ('pvc' or 'hostPath')")
return subparser
}())
argparser.AddCommand(&cobra.Command{
Use: "down",
Short: "Shut down the port-forward to the registry",
Args: cobra.ExactArgs(0),
RunE: func(_ *cobra.Command, _ []string) error {
cmd := exec.Command("killall", "kubectl") // XXX
cmd.Stdout = os.Stderr
cmd.Stderr = os.Stderr
return cmd.Run()
},
})
if err := argparser.Execute(); err != nil {
fmt.Fprintf(os.Stderr, "%s: error: %v\n", os.Args[0], err)
os.Exit(1)
}
}