Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

create kind network #67

Merged
merged 5 commits into from
Oct 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 65 additions & 6 deletions pkg/controllers/gitserver/image_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,16 @@ package gitserver
import (
"context"
"fmt"
"github.com/cnoe-io/idpbuilder/pkg/kind"
"github.com/docker/docker/api/types/container"
"github.com/docker/go-connections/nat"
"io"
"net"
"os"
"strconv"
"strings"
"testing"
"time"

"github.com/cnoe-io/idpbuilder/api/v1alpha1"
"github.com/cnoe-io/idpbuilder/pkg/apps"
Expand Down Expand Up @@ -39,23 +47,74 @@ func TestReconcileGitServerImage(t *testing.T) {
},
}

_, err = r.reconcileGitServerImage(ctx, controllerruntime.Request{}, &resource)
dockerClient, err := docker.GetDockerClient()
if err != nil {
t.Errorf("Getting docker client: %v", err)
}
defer dockerClient.Close()
reader, err := dockerClient.ImagePull(ctx, "docker.io/library/registry:2", types.ImagePullOptions{})
defer reader.Close()
// blocks until pull is completed
io.Copy(os.Stdout, reader)
if err != nil {
t.Errorf("reconcile error: %v", err)
t.Fatalf("failed pulilng registry image: %v", err)
}

if !strings.HasPrefix(resource.Status.ImageID, "sha256") {
t.Errorf("Invalid or no Image ID in status: %q", resource.Status.ImageID)
waitTimeout := time.Second * 90
waitInterval := time.Second * 3
// very crude. no guarantee that the port will be available by the time request is sent to docker
endTime := time.Now().Add(waitTimeout)
for {
if time.Now().After(endTime) {
t.Fatalf("Timed out waiting for port %d to be available", kind.ExposedRegistryPort)
}
conn, cErr := net.DialTimeout("tcp", net.JoinHostPort("0.0.0.0", strconv.Itoa(int(kind.ExposedRegistryPort))), time.Second*3)
if cErr != nil {
break
}
conn.Close()
time.Sleep(waitInterval)
Comment on lines +67 to +76
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cosmetics here but pushing the check to a go func would be nicer

Suggested change
for {
if time.Now().After(endTime) {
t.Fatalf("Timed out waiting for port %d to be available", kind.ExposedRegistryPort)
}
conn, cErr := net.DialTimeout("tcp", net.JoinHostPort("0.0.0.0", strconv.Itoa(int(kind.ExposedRegistryPort))), time.Second*3)
if cErr != nil {
break
}
conn.Close()
time.Sleep(waitInterval)
ready := make(chan bool)
endTime := time.After(waitTimeout)
go func() {
for {
....
if cErr == nil {
close(ready)
}
}
}()
select {
case <-timeout:
err := errors.New("Timeout")
log.Error(err, "Didn't reconcile Gitea on time.")
return ctrl.Result{}, err
case <-ready:
log.Info("Gitea is ready!")
resource.Status.GiteaAvailable = true
}

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm using the same format that was used here:

endTime := time.Now().Add(timeout)

If we are using timeout, we should use the same logic everywhere or make it reusable. not worth the time effort imo.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah not a big deal, lets get it over the line.

}

dockerClient, err := docker.GetDockerClient()
resp, err := dockerClient.ContainerCreate(ctx, &container.Config{
Image: "docker.io/library/registry:2",
Tty: false,
ExposedPorts: nat.PortSet{
nat.Port(fmt.Sprintf("%d/tcp", kind.InternalRegistryPort)): struct{}{},
},
}, &container.HostConfig{
PortBindings: nat.PortMap{
nat.Port(fmt.Sprintf("%d/tcp", kind.InternalRegistryPort)): []nat.PortBinding{
{
HostIP: "0.0.0.0",
HostPort: fmt.Sprintf("%d", kind.ExposedRegistryPort),
},
},
},
}, nil, nil, "testcase-registry")
if err != nil {
t.Errorf("Getting docker client: %v", err)
t.Fatalf("failed creating registry container %v", err)
}

defer dockerClient.ContainerRemove(ctx, resp.ID, types.ContainerRemoveOptions{Force: true})

err = dockerClient.ContainerStart(ctx, resp.ID, types.ContainerStartOptions{})
if err != nil {
t.Fatalf("failed starting container %v", err)
}

_, err = r.reconcileGitServerImage(ctx, controllerruntime.Request{}, &resource)
if err != nil {
t.Fatalf("reconcile error: %v", err)
}

if !strings.HasPrefix(resource.Status.ImageID, "sha256") {
t.Fatalf("Invalid or no Image ID in status: %q", resource.Status.ImageID)
}
imageNameID := fmt.Sprintf("%s@%s", GetImageTag(&resource), resource.Status.ImageID)
_, err = dockerClient.ImageRemove(ctx, imageNameID, types.ImageRemoveOptions{})
if err != nil {
t.Errorf("Removing docker image: %v", err)
}
}

34 changes: 26 additions & 8 deletions pkg/kind/registry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package kind
import (
"context"
"testing"
"time"

"github.com/cnoe-io/idpbuilder/pkg/docker"
"github.com/docker/docker/api/types"
Expand All @@ -19,23 +20,44 @@ func TestReconcileRegistry(t *testing.T) {
}
defer dockerCli.Close()

kindNetwork, err := dockerCli.NetworkCreate(ctx, "kind", types.NetworkCreate{})
if err != nil {
t.Fatalf("Failed creaking kind network: %v", err)
}
defer dockerCli.NetworkRemove(ctx, kindNetwork.ID)

// Create cluster
cluster, err := NewCluster("testcase", "v1.26.3", "", "", "")
if err != nil {
t.Fatalf("Initializing cluster resource: %v", err)
}

// Create registry
err = cluster.ReconcileRegistry(ctx)
if err != nil {
t.Fatalf("Error reconciling registry: %v", err)
defer dockerCli.ContainerRemove(ctx, cluster.getRegistryContainerName(), types.ContainerRemoveOptions{Force: true})
waitTimeout := time.Second * 90
waitInterval := time.Second * 3
endTime := time.Now().Add(waitTimeout)

for {
if time.Now().After(endTime) {
t.Fatalf("Timed out waiting for registry. recent error: %v", err)
Comment on lines +39 to +43
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same as above

}
err = cluster.ReconcileRegistry(ctx)
if err == nil {
break
}
t.Logf("Failed to reconcile: %v", err)
dockerCli.ContainerRemove(ctx, cluster.getRegistryContainerName(), types.ContainerRemoveOptions{Force: true})
time.Sleep(waitInterval)
}

// Get resulting container
container, err := cluster.getRegistryContainer(ctx, dockerCli)
if err != nil {
t.Fatalf("Error getting registry container after reconcile: %v", err)
}
defer dockerCli.ImageRemove(ctx, container.ImageID, types.ImageRemoveOptions{})

if container == nil {
t.Fatal("Expected registry container after reconcile but got nil")
}
Expand All @@ -45,9 +67,5 @@ func TestReconcileRegistry(t *testing.T) {
if err != nil {
t.Fatalf("Error reconciling registry: %v", err)
}

// Cleanup
if err = dockerCli.ContainerRemove(ctx, container.ID, types.ContainerRemoveOptions{Force: true}); err != nil {
t.Fatalf("Error removing registry docker container after reconcile: %v", err)
}
}