This repository was archived by the owner on Sep 23, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathsetup.go
More file actions
236 lines (203 loc) · 6.77 KB
/
Copy pathsetup.go
File metadata and controls
236 lines (203 loc) · 6.77 KB
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
//
// Copyright 2023 The Sigstore Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"bytes"
"context"
"fmt"
"io/fs"
"log"
"os"
"os/exec"
"path/filepath"
"regexp"
"strconv"
"github.com/docker/docker/api/types/container"
"github.com/docker/docker/client"
"github.com/docker/go-connections/nat"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
const (
localRegistryName = "registry.local"
localRegistryPort = 5001
defaultKindestNodeVersionTag = "v1.27.3"
)
var kindClusterConfig = `
apiVersion: kind.x-k8s.io/v1alpha4
kind: Cluster
name: "%s"
nodes:
- role: control-plane
image: "%s"
# Configure registry for KinD.
containerdConfigPatches:
- |-
[plugins."io.containerd.grpc.v1.cri".registry.mirrors."%s:%d"]
endpoint = ["http://%s:%d"]
`
// check that a supplied image version is in the expected semver format: v<major>.<minor>.<patch>
var semverRegexp = regexp.MustCompile("^v[0-9]+.[0-9]+.[0-9]+$")
// check that registry URLs are in the expected format <url>:<port>
var registryURLRegexp = regexp.MustCompile("^[a-zA-Z0-9]+.[a-z]+:[0-9]+$")
func addSetupFlags(cmd *cobra.Command) {
cmd.Flags().String("cluster-name", "policy-controller-demo", "name of the dev policy controller cluster")
cmd.Flags().String("k8s-version", defaultKindestNodeVersionTag, "name of the Ko Docker repository to use")
cmd.Flags().String("registry-url", "registry.local", "URL and port of the Ko Docker registry to use. Expected format: <url>:<port>. If no registry is provided, the local Kind registry will be used")
}
var setupCmd = &cobra.Command{
Use: "setup",
Short: "setup local k8s cluster for testing policy controller",
Long: "Setup a local k8s cluster for testing policy controller",
PreRunE: func(cmd *cobra.Command, args []string) error {
if err := viper.BindPFlags(cmd.Flags()); err != nil {
log.Fatal("Error initializing cmd line args: ", err)
}
return nil
},
Run: func(cmd *cobra.Command, args []string) {
setup()
},
}
func buildFatalMessage(err error, stderr bytes.Buffer) string {
return fmt.Sprintf("%v: %s", err, stderr.String())
}
func setup() {
var stderr bytes.Buffer
registryURL := viper.GetString("registry-url")
if registryURL == localRegistryName {
fullLocalRegistryURL := fmt.Sprintf("%s:%d/sigstore", localRegistryName, localRegistryPort)
err := os.Setenv("KO_DOCKER_REPO", fullLocalRegistryURL)
if err != nil {
log.Fatal(buildFatalMessage(err, stderr))
}
} else {
if !registryURLRegexp.Match([]byte(registryURL)) {
log.Fatal(fmt.Errorf("provided registry URL is not in the expected format: <url>:<port>"))
}
err := os.Setenv("KO_DOCKER_REPO", registryURL)
if err != nil {
log.Fatal(buildFatalMessage(err, stderr))
}
}
// Create the new Kind cluster
clusterName := viper.GetString("cluster-name")
fmt.Printf("Creating Kind cluster %s...\n", clusterName)
clusterConfig, err := createKindConfig(clusterName, viper.GetString("k8s-version"))
if err != nil {
log.Fatal(err)
}
configBytes := []byte(clusterConfig)
err = os.WriteFile("kind.yaml", configBytes, 0600)
if err != nil {
log.Fatal(err)
}
startKindCluster := exec.Command("kind", "create", "cluster", "--config", "kind.yaml")
startKindCluster.Stderr = &stderr
if err := startKindCluster.Run(); err != nil {
log.Fatal(buildFatalMessage(err, stderr))
}
if registryURL == localRegistryName {
if err = setupLocalRegistry(); err != nil {
log.Fatal(err)
}
}
setGitHash := exec.Command("git", "rev-parse", "HEAD")
setGitHash.Stderr = &stderr
outBytes, err := setGitHash.Output()
if err != nil {
log.Fatal(buildFatalMessage(err, stderr))
}
err = os.Setenv("GIT_HASH", string(outBytes))
if err != nil {
log.Fatal(buildFatalMessage(err, stderr))
}
setGitVersion := exec.Command("git", "describe", "--tags", "--always", "--dirty")
setGitVersion.Stderr = &stderr
outBytes, err = setGitVersion.Output()
if err != nil {
log.Fatal(buildFatalMessage(err, stderr))
}
err = os.Setenv("GIT_VERSION", string(outBytes))
if err != nil {
log.Fatal(buildFatalMessage(err, stderr))
}
var configFiles []string
err = filepath.WalkDir("config", func(path string, d fs.DirEntry, err error) error {
if err != nil {
return err
}
if filepath.Ext(d.Name()) == ".yaml" && d.Name() != "kustomization.yaml" {
configFiles = append(configFiles, path)
}
return nil
})
if err != nil {
log.Fatal(err)
}
fmt.Println("Applying local policy controller manifests...")
for _, configFile := range configFiles {
koApply := exec.Command("ko", "apply", "-f", configFile)
koApply.Stderr = &stderr
_, err = koApply.Output()
if err != nil {
log.Fatal(buildFatalMessage(err, stderr))
}
}
}
func createKindConfig(clusterName, k8sVersion string) (string, error) {
// check that the provided version is in the expected format and use it
if !semverRegexp.Match([]byte(k8sVersion)) {
return "", fmt.Errorf("provided k8s version %s is not in the expected semver format v<major>.<minor>.<patch>", k8sVersion)
}
kindImage := fmt.Sprintf("kindest/node:%s", k8sVersion)
return fmt.Sprintf(kindClusterConfig, clusterName, kindImage, localRegistryName, localRegistryPort, localRegistryName, localRegistryPort), nil
}
func setupLocalRegistry() error {
dockerCLI, err := client.NewClientWithOpts(
client.FromEnv,
client.WithAPIVersionNegotiation(),
)
if err != nil {
return nil
}
defer dockerCLI.Close()
fmt.Printf("\nStarting local registry %s...\n", localRegistryName)
ctx := context.Background()
resp, err := dockerCLI.ContainerCreate(ctx, &container.Config{
Image: "registry:2",
Env: []string{fmt.Sprintf("REGISTRY_HTTP_ADDR=0.0.0.0:%d", localRegistryPort)},
ExposedPorts: nat.PortSet{"5001/tcp": struct{}{}},
}, &container.HostConfig{
RestartPolicy: container.RestartPolicy{Name: "always"},
PortBindings: nat.PortMap{
"5001/tcp": []nat.PortBinding{
{HostIP: "127.0.0.1", HostPort: strconv.Itoa(localRegistryPort)},
},
},
}, nil, nil, localRegistryName)
if err != nil {
return err
}
if err := dockerCLI.ContainerStart(ctx, resp.ID, container.StartOptions{}); err != nil {
return err
}
fmt.Println("Connecting network between kind with local registry ...")
return dockerCLI.NetworkConnect(ctx, "kind", localRegistryName, nil)
}
func init() {
addSetupFlags(setupCmd)
rootCmd.AddCommand(setupCmd)
}