Skip to content

Commit

Permalink
fix gitea registry for path based routing (#411)
Browse files Browse the repository at this point in the history
Signed-off-by: Manabu McCloskey <[email protected]>
  • Loading branch information
nabuskey authored Oct 17, 2024
1 parent 1baf788 commit d966372
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 14 deletions.
65 changes: 52 additions & 13 deletions pkg/kind/cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,20 @@ import (
)

func TestGetConfig(t *testing.T) {
cluster, err := NewCluster("testcase", "v1.26.3", "", "", "", util.CorePackageTemplateConfig{
Host: "cnoe.localtest.me",
Port: "8443",
})
if err != nil {
t.Fatalf("Initializing cluster resource: %v", err)
}

cfg, err := cluster.getConfig()
if err != nil {
t.Errorf("Error getting kind config: %v", err)
type tc struct {
host string
port string
usePathRouting bool
expectConfig string
}

expectConfig := `# Kind kubernetes release images https://github.com/kubernetes-sigs/kind/releases
tcs := []tc{
{
host: "cnoe.localtest.me",
port: "8443",
usePathRouting: false,
expectConfig: `
kind: Cluster
apiVersion: kind.x-k8s.io/v1alpha4
nodes:
Expand All @@ -48,8 +48,47 @@ containerdConfigPatches:
[plugins."io.containerd.grpc.v1.cri".registry.mirrors."gitea.cnoe.localtest.me:8443"]
endpoint = ["https://gitea.cnoe.localtest.me"]
[plugins."io.containerd.grpc.v1.cri".registry.configs."gitea.cnoe.localtest.me".tls]
insecure_skip_verify = true`
assert.YAMLEq(t, expectConfig, string(cfg))
insecure_skip_verify = true`,
},
{
host: "cnoe.localtest.me",
port: "8443",
usePathRouting: true,
expectConfig: `
kind: Cluster
apiVersion: kind.x-k8s.io/v1alpha4
nodes:
- role: control-plane
image: "kindest/node:v1.26.3"
labels:
ingress-ready: "true"
extraPortMappings:
- containerPort: 443
hostPort: 8443
protocol: TCP
containerdConfigPatches:
- |-
[plugins."io.containerd.grpc.v1.cri".registry.mirrors."cnoe.localtest.me:8443"]
endpoint = ["https://cnoe.localtest.me"]
[plugins."io.containerd.grpc.v1.cri".registry.configs."cnoe.localtest.me".tls]
insecure_skip_verify = true`,
},
}

for i := range tcs {
c := tcs[i]
cluster, err := NewCluster("testcase", "v1.26.3", "", "", "", util.CorePackageTemplateConfig{
Host: c.host,
Port: c.port,
UsePathRouting: c.usePathRouting,
})
assert.NoError(t, err)

cfg, err := cluster.getConfig()
assert.NoError(t, err)
assert.YAMLEq(t, c.expectConfig, string(cfg))
}
}

func TestExtraPortMappings(t *testing.T) {
Expand Down
8 changes: 7 additions & 1 deletion pkg/kind/resources/kind.yaml.tmpl
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# Kind kubernetes release images https://github.com/kubernetes-sigs/kind/releases
kind: Cluster
apiVersion: kind.x-k8s.io/v1alpha4
nodes:
Expand All @@ -17,7 +16,14 @@ nodes:
{{ end }}
containerdConfigPatches:
- |-
{{ if .UsePathRouting -}}
[plugins."io.containerd.grpc.v1.cri".registry.mirrors."{{ .Host }}:{{ .Port }}"]
endpoint = ["https://{{ .Host }}"]
[plugins."io.containerd.grpc.v1.cri".registry.configs."{{ .Host }}".tls]
insecure_skip_verify = true
{{- else -}}
[plugins."io.containerd.grpc.v1.cri".registry.mirrors."gitea.{{ .Host }}:{{ .Port }}"]
endpoint = ["https://gitea.{{ .Host }}"]
[plugins."io.containerd.grpc.v1.cri".registry.configs."gitea.{{ .Host }}".tls]
insecure_skip_verify = true
{{- end -}}
4 changes: 4 additions & 0 deletions tests/e2e/docker/docker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ func testCreate(t *testing.T) {
argoBaseUrl := fmt.Sprintf("https://argocd.%s:%s", e2e.DefaultBaseDomain, e2e.DefaultPort)
giteaBaseUrl := fmt.Sprintf("https://gitea.%s:%s", e2e.DefaultBaseDomain, e2e.DefaultPort)
e2e.TestCoreEndpoints(ctx, t, argoBaseUrl, giteaBaseUrl)

e2e.TestGiteaRegistry(ctx, t, "docker", fmt.Sprintf("gitea.%s", e2e.DefaultBaseDomain), e2e.DefaultPort)
}

// test idpbuilder create --use-path-routing
Expand All @@ -90,6 +92,8 @@ func testCreatePath(t *testing.T) {
argoBaseUrl := fmt.Sprintf("https://%s:%s/argocd", e2e.DefaultBaseDomain, e2e.DefaultPort)
giteaBaseUrl := fmt.Sprintf("https://%s:%s/gitea", e2e.DefaultBaseDomain, e2e.DefaultPort)
e2e.TestCoreEndpoints(ctx, t, argoBaseUrl, giteaBaseUrl)

e2e.TestGiteaRegistry(ctx, t, "docker", e2e.DefaultBaseDomain, e2e.DefaultPort)
}

func testCreatePort(t *testing.T) {
Expand Down
2 changes: 2 additions & 0 deletions tests/e2e/docker/test-dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
FROM scratch
COPY test-dockerfile .
30 changes: 30 additions & 0 deletions tests/e2e/e2e.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"github.com/cnoe-io/idpbuilder/pkg/cmd/get"
"github.com/cnoe-io/idpbuilder/pkg/k8s"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"k8s.io/client-go/tools/clientcmd"
"k8s.io/client-go/util/homedir"
"sigs.k8s.io/controller-runtime/pkg/client"
Expand Down Expand Up @@ -363,3 +364,32 @@ func GetKubeClient() (client.Client, error) {
}
return client.New(conf, client.Options{Scheme: k8s.GetScheme()})
}

// login, build a test image, push, then pull.
func TestGiteaRegistry(ctx context.Context, t *testing.T, cmd, giteaHost, giteaPort string) {
t.Log("testing gitea container registry")
b, err := RunCommand(ctx, fmt.Sprintf("%s get secrets -o json -p gitea", IdpbuilderBinaryLocation), 10*time.Second)
assert.NoError(t, err)

secs := make([]get.TemplateData, 2)
err = json.Unmarshal(b, &secs)
assert.NoError(t, err)

sec := secs[0]
user := sec.Data["username"]
pass := sec.Data["password"]

login, err := RunCommand(ctx, fmt.Sprintf("%s login %s:%s -u %s -p %s", cmd, giteaHost, giteaPort, user, pass), 10*time.Second)
require.NoErrorf(t, err, "%s login err: %s", cmd, login)

tag := fmt.Sprintf("%s:%s/giteaadmin/test:latest", giteaHost, giteaPort)

build, err := RunCommand(ctx, fmt.Sprintf("%s build -f test-dockerfile -t %s .", cmd, tag), 10*time.Second)
require.NoErrorf(t, err, "%s build err: %s", cmd, build)

push, err := RunCommand(ctx, fmt.Sprintf("%s push %s", cmd, tag), 10*time.Second)
require.NoErrorf(t, err, "%s push err: %s", cmd, push)

pull, err := RunCommand(ctx, fmt.Sprintf("%s pull %s", cmd, tag), 10*time.Second)
require.NoErrorf(t, err, "%s pull err: %s", cmd, pull)
}

0 comments on commit d966372

Please sign in to comment.