Skip to content

Commit

Permalink
ensure ingress port is available (#368)
Browse files Browse the repository at this point in the history
Signed-off-by: Manabu McCloskey <[email protected]>
  • Loading branch information
nabuskey authored Aug 27, 2024
1 parent 78be2c2 commit 385c649
Show file tree
Hide file tree
Showing 6 changed files with 161 additions and 4 deletions.
61 changes: 57 additions & 4 deletions pkg/kind/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,17 @@ import (
"fmt"
"io/fs"
"os"
"strconv"
"strings"

"github.com/cnoe-io/idpbuilder/pkg/runtime"
"github.com/cnoe-io/idpbuilder/pkg/util"
"sigs.k8s.io/controller-runtime/pkg/log"
kindv1alpha4 "sigs.k8s.io/kind/pkg/apis/config/v1alpha4"
"sigs.k8s.io/kind/pkg/cluster"
"sigs.k8s.io/kind/pkg/cluster/nodes"
"sigs.k8s.io/kind/pkg/cluster/nodeutils"
"sigs.k8s.io/yaml"
)

var (
Expand Down Expand Up @@ -66,7 +69,7 @@ func (c *Cluster) getConfig() ([]byte, error) {
}

if err != nil {
return []byte{}, err
return nil, fmt.Errorf("reading kind config: %w", err)
}

var portMappingPairs []PortMapping
Expand All @@ -82,8 +85,6 @@ func (c *Cluster) getConfig() ([]byte, error) {
portMappingPairs[i] = PortMapping{parts[0], parts[1]}
}
}
} else {
portMappingPairs = nil
}

var retBuff []byte
Expand All @@ -92,7 +93,20 @@ func (c *Cluster) getConfig() ([]byte, error) {
KubernetesVersion: c.kubeVersion,
ExtraPortsMapping: portMappingPairs,
}); err != nil {
return []byte{}, err
return nil, err
}

if c.kindConfigPath != "" {
parsedCluster, err := c.ensureCorrectConfig(retBuff)
if err != nil {
return nil, fmt.Errorf("ensuring custom kind config is correct: %w", err)
}

out, err := yaml.Marshal(parsedCluster)
if err != nil {
return nil, fmt.Errorf("marshaling custom kind cluster config: %w", err)
}
return out, nil
}

return retBuff, nil
Expand Down Expand Up @@ -214,3 +228,42 @@ func (c *Cluster) Reconcile(ctx context.Context, recreate bool) error {
func (c *Cluster) ExportKubeConfig(name string, internal bool) error {
return c.provider.ExportKubeConfig(name, c.kubeConfigPath, internal)
}

func (c *Cluster) ensureCorrectConfig(in []byte) (kindv1alpha4.Cluster, error) {
// see pkg/kind/resources/kind.yaml.tmpl and pkg/controllers/localbuild/resources/nginx/k8s/ingress-nginx.yaml
// defines which container port we should be looking for.
containerPort := "443"
if c.cfg.Protocol == "http" {
containerPort = "80"
}
parsedCluster := kindv1alpha4.Cluster{}
err := yaml.Unmarshal(in, &parsedCluster)
if err != nil {
return kindv1alpha4.Cluster{}, fmt.Errorf("parsing kind config: %w", err)
}

appendNecessaryPort := true
nodes:
for i := range parsedCluster.Nodes {
node := parsedCluster.Nodes[i]
for _, pm := range node.ExtraPortMappings {
if strconv.Itoa(int(pm.HostPort)) == c.cfg.Port {
appendNecessaryPort = false
break nodes
}
}
}

if appendNecessaryPort && len(parsedCluster.Nodes) != 0 {
hp, err := strconv.Atoi(c.cfg.Port)
if err != nil {
return kindv1alpha4.Cluster{}, fmt.Errorf("converting port, %s, to int: %w", c.cfg.Port, err)
}
// either "80" or "443". No need to check for err
cp, _ := strconv.Atoi(containerPort)

parsedCluster.Nodes[0].ExtraPortMappings = append(parsedCluster.Nodes[0].ExtraPortMappings, kindv1alpha4.PortMapping{ContainerPort: int32(cp), HostPort: int32(hp)})
}

return parsedCluster, nil
}
40 changes: 40 additions & 0 deletions pkg/kind/cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package kind
import (
"context"
"io"
"os"
"testing"

runtime "github.com/cnoe-io/idpbuilder/pkg/runtime"
Expand Down Expand Up @@ -99,6 +100,45 @@ containerdConfigPatches:
assert.YAMLEq(t, expectConfig, string(cfg))
}

func TestGetConfigCustom(t *testing.T) {

type testCase struct {
inputPath string
outputPath string
hostPort string
Protocol string
}

cases := []testCase{
{
inputPath: "testdata/no-necessary-port.yaml",
outputPath: "testdata/expected/no-necessary-port.yaml",
hostPort: "8443",
Protocol: "https",
},
{
inputPath: "testdata/necessary-port-present.yaml",
outputPath: "testdata/expected/necessary-port-present.yaml",
hostPort: "80",
Protocol: "http",
},
}

for _, v := range cases {
c, _ := NewCluster("testcase", "v1.26.3", "", v.inputPath, "", util.CorePackageTemplateConfig{
Host: "cnoe.localtest.me",
Port: v.hostPort,
Protocol: v.Protocol,
})

b, err := c.getConfig()
assert.NoError(t, err)
expected, _ := os.ReadFile(v.outputPath)
assert.YAMLEq(t, string(expected), string(b))
}

}

// Mock provider for testing
type mockProvider struct {
mock.Mock
Expand Down
17 changes: 17 additions & 0 deletions pkg/kind/testdata/expected/necessary-port-present.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
kind: Cluster
apiVersion: kind.x-k8s.io/v1alpha4
networking: {}
nodes:
- role: control-plane
extraMounts:
- containerPath: /var/lib/kubelet/config.json
hostPath: ~/.docker/config.json
extraPortMappings:
- containerPort: 31337
hostPort: 31337
- containerPort: 31340
hostPort: 31340
- containerPort: 31333
hostPort: 31333
- containerPort: 80
hostPort: 80
17 changes: 17 additions & 0 deletions pkg/kind/testdata/expected/no-necessary-port.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
kind: Cluster
apiVersion: kind.x-k8s.io/v1alpha4
networking: {}
nodes:
- role: control-plane
extraMounts:
- containerPath: /var/lib/kubelet/config.json
hostPath: ~/.docker/config.json
extraPortMappings:
- containerPort: 31337
hostPort: 31337
- containerPort: 31340
hostPort: 31340
- containerPort: 31333
hostPort: 31333
- containerPort: 443
hostPort: 8443
16 changes: 16 additions & 0 deletions pkg/kind/testdata/necessary-port-present.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
kind: Cluster
apiVersion: kind.x-k8s.io/v1alpha4
nodes:
- role: control-plane
extraMounts:
- containerPath: /var/lib/kubelet/config.json
hostPath: ~/.docker/config.json
extraPortMappings:
- containerPort: 31337
hostPort: 31337
- containerPort: 31340
hostPort: 31340
- containerPort: 31333
hostPort: 31333
- containerPort: 80
hostPort: 80
14 changes: 14 additions & 0 deletions pkg/kind/testdata/no-necessary-port.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
kind: Cluster
apiVersion: kind.x-k8s.io/v1alpha4
nodes:
- role: control-plane
extraMounts:
- containerPath: /var/lib/kubelet/config.json
hostPath: ~/.docker/config.json
extraPortMappings:
- containerPort: 31337
hostPort: 31337
- containerPort: 31340
hostPort: 31340
- containerPort: 31333
hostPort: 31333

0 comments on commit 385c649

Please sign in to comment.