Skip to content

Commit

Permalink
move local registry creation to func
Browse files Browse the repository at this point in the history
Signed-off-by: Meredith Lancaster <malancas@github.com>
  • Loading branch information
malancas committed Jul 17, 2023
1 parent b6d33cd commit bfd1be4
Showing 1 changed file with 41 additions and 37 deletions.
78 changes: 41 additions & 37 deletions cmd/local-dev/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,43 +116,7 @@ func setup() {
}

if registryURL == localRegistryName {
cli, err := client.NewClientWithOpts(
client.FromEnv,
client.WithAPIVersionNegotiation(),
)
if err != nil {
log.Fatal(err)
}
defer cli.Close()

fmt.Printf("\nStarting local registry %s...\n", localRegistryName)

resp, err := cli.ContainerCreate(context.Background(), &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 {
cli.Close()
log.Fatal(err)
}

if err := cli.ContainerStart(context.Background(), resp.ID, types.ContainerStartOptions{}); err != nil {
cli.Close()
log.Fatal(err)
}

fmt.Println("Connecting network between kind with local registry ...")

if err = cli.NetworkConnect(context.Background(), "kind", localRegistryName, nil); err != nil {
cli.Close()
if err = setupLocalRegistry(); err != nil {
log.Fatal(err)
}
}
Expand Down Expand Up @@ -207,6 +171,46 @@ func setup() {
}
}

func setupLocalRegistry() error {
cli, err := client.NewClientWithOpts(
client.FromEnv,
client.WithAPIVersionNegotiation(),
)
if err != nil {
return nil
}
defer cli.Close()

fmt.Printf("\nStarting local registry %s...\n", localRegistryName)

resp, err := cli.ContainerCreate(context.Background(), &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 := cli.ContainerStart(context.Background(), resp.ID, types.ContainerStartOptions{}); err != nil {
return err
}

fmt.Println("Connecting network between kind with local registry ...")

if err = cli.NetworkConnect(context.Background(), "kind", localRegistryName, nil); err != nil {

Check warning on line 208 in cmd/local-dev/setup.go

View workflow job for this annotation

GitHub Actions / lint

if-return: redundant if ...; err != nil check, just return error instead. (revive)
return err
}
return nil
}

func getKindImage(k8sVersion string) string {
switch k8sVersion {
case "v1.23.x":
Expand Down

0 comments on commit bfd1be4

Please sign in to comment.