Skip to content

Commit 385c649

Browse files
authored
ensure ingress port is available (#368)
Signed-off-by: Manabu McCloskey <[email protected]>
1 parent 78be2c2 commit 385c649

File tree

6 files changed

+161
-4
lines changed

6 files changed

+161
-4
lines changed

pkg/kind/cluster.go

Lines changed: 57 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,17 @@ import (
66
"fmt"
77
"io/fs"
88
"os"
9+
"strconv"
910
"strings"
1011

1112
"github.com/cnoe-io/idpbuilder/pkg/runtime"
1213
"github.com/cnoe-io/idpbuilder/pkg/util"
1314
"sigs.k8s.io/controller-runtime/pkg/log"
15+
kindv1alpha4 "sigs.k8s.io/kind/pkg/apis/config/v1alpha4"
1416
"sigs.k8s.io/kind/pkg/cluster"
1517
"sigs.k8s.io/kind/pkg/cluster/nodes"
1618
"sigs.k8s.io/kind/pkg/cluster/nodeutils"
19+
"sigs.k8s.io/yaml"
1720
)
1821

1922
var (
@@ -66,7 +69,7 @@ func (c *Cluster) getConfig() ([]byte, error) {
6669
}
6770

6871
if err != nil {
69-
return []byte{}, err
72+
return nil, fmt.Errorf("reading kind config: %w", err)
7073
}
7174

7275
var portMappingPairs []PortMapping
@@ -82,8 +85,6 @@ func (c *Cluster) getConfig() ([]byte, error) {
8285
portMappingPairs[i] = PortMapping{parts[0], parts[1]}
8386
}
8487
}
85-
} else {
86-
portMappingPairs = nil
8788
}
8889

8990
var retBuff []byte
@@ -92,7 +93,20 @@ func (c *Cluster) getConfig() ([]byte, error) {
9293
KubernetesVersion: c.kubeVersion,
9394
ExtraPortsMapping: portMappingPairs,
9495
}); err != nil {
95-
return []byte{}, err
96+
return nil, err
97+
}
98+
99+
if c.kindConfigPath != "" {
100+
parsedCluster, err := c.ensureCorrectConfig(retBuff)
101+
if err != nil {
102+
return nil, fmt.Errorf("ensuring custom kind config is correct: %w", err)
103+
}
104+
105+
out, err := yaml.Marshal(parsedCluster)
106+
if err != nil {
107+
return nil, fmt.Errorf("marshaling custom kind cluster config: %w", err)
108+
}
109+
return out, nil
96110
}
97111

98112
return retBuff, nil
@@ -214,3 +228,42 @@ func (c *Cluster) Reconcile(ctx context.Context, recreate bool) error {
214228
func (c *Cluster) ExportKubeConfig(name string, internal bool) error {
215229
return c.provider.ExportKubeConfig(name, c.kubeConfigPath, internal)
216230
}
231+
232+
func (c *Cluster) ensureCorrectConfig(in []byte) (kindv1alpha4.Cluster, error) {
233+
// see pkg/kind/resources/kind.yaml.tmpl and pkg/controllers/localbuild/resources/nginx/k8s/ingress-nginx.yaml
234+
// defines which container port we should be looking for.
235+
containerPort := "443"
236+
if c.cfg.Protocol == "http" {
237+
containerPort = "80"
238+
}
239+
parsedCluster := kindv1alpha4.Cluster{}
240+
err := yaml.Unmarshal(in, &parsedCluster)
241+
if err != nil {
242+
return kindv1alpha4.Cluster{}, fmt.Errorf("parsing kind config: %w", err)
243+
}
244+
245+
appendNecessaryPort := true
246+
nodes:
247+
for i := range parsedCluster.Nodes {
248+
node := parsedCluster.Nodes[i]
249+
for _, pm := range node.ExtraPortMappings {
250+
if strconv.Itoa(int(pm.HostPort)) == c.cfg.Port {
251+
appendNecessaryPort = false
252+
break nodes
253+
}
254+
}
255+
}
256+
257+
if appendNecessaryPort && len(parsedCluster.Nodes) != 0 {
258+
hp, err := strconv.Atoi(c.cfg.Port)
259+
if err != nil {
260+
return kindv1alpha4.Cluster{}, fmt.Errorf("converting port, %s, to int: %w", c.cfg.Port, err)
261+
}
262+
// either "80" or "443". No need to check for err
263+
cp, _ := strconv.Atoi(containerPort)
264+
265+
parsedCluster.Nodes[0].ExtraPortMappings = append(parsedCluster.Nodes[0].ExtraPortMappings, kindv1alpha4.PortMapping{ContainerPort: int32(cp), HostPort: int32(hp)})
266+
}
267+
268+
return parsedCluster, nil
269+
}

pkg/kind/cluster_test.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package kind
33
import (
44
"context"
55
"io"
6+
"os"
67
"testing"
78

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

103+
func TestGetConfigCustom(t *testing.T) {
104+
105+
type testCase struct {
106+
inputPath string
107+
outputPath string
108+
hostPort string
109+
Protocol string
110+
}
111+
112+
cases := []testCase{
113+
{
114+
inputPath: "testdata/no-necessary-port.yaml",
115+
outputPath: "testdata/expected/no-necessary-port.yaml",
116+
hostPort: "8443",
117+
Protocol: "https",
118+
},
119+
{
120+
inputPath: "testdata/necessary-port-present.yaml",
121+
outputPath: "testdata/expected/necessary-port-present.yaml",
122+
hostPort: "80",
123+
Protocol: "http",
124+
},
125+
}
126+
127+
for _, v := range cases {
128+
c, _ := NewCluster("testcase", "v1.26.3", "", v.inputPath, "", util.CorePackageTemplateConfig{
129+
Host: "cnoe.localtest.me",
130+
Port: v.hostPort,
131+
Protocol: v.Protocol,
132+
})
133+
134+
b, err := c.getConfig()
135+
assert.NoError(t, err)
136+
expected, _ := os.ReadFile(v.outputPath)
137+
assert.YAMLEq(t, string(expected), string(b))
138+
}
139+
140+
}
141+
102142
// Mock provider for testing
103143
type mockProvider struct {
104144
mock.Mock
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
kind: Cluster
2+
apiVersion: kind.x-k8s.io/v1alpha4
3+
networking: {}
4+
nodes:
5+
- role: control-plane
6+
extraMounts:
7+
- containerPath: /var/lib/kubelet/config.json
8+
hostPath: ~/.docker/config.json
9+
extraPortMappings:
10+
- containerPort: 31337
11+
hostPort: 31337
12+
- containerPort: 31340
13+
hostPort: 31340
14+
- containerPort: 31333
15+
hostPort: 31333
16+
- containerPort: 80
17+
hostPort: 80
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
kind: Cluster
2+
apiVersion: kind.x-k8s.io/v1alpha4
3+
networking: {}
4+
nodes:
5+
- role: control-plane
6+
extraMounts:
7+
- containerPath: /var/lib/kubelet/config.json
8+
hostPath: ~/.docker/config.json
9+
extraPortMappings:
10+
- containerPort: 31337
11+
hostPort: 31337
12+
- containerPort: 31340
13+
hostPort: 31340
14+
- containerPort: 31333
15+
hostPort: 31333
16+
- containerPort: 443
17+
hostPort: 8443
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
kind: Cluster
2+
apiVersion: kind.x-k8s.io/v1alpha4
3+
nodes:
4+
- role: control-plane
5+
extraMounts:
6+
- containerPath: /var/lib/kubelet/config.json
7+
hostPath: ~/.docker/config.json
8+
extraPortMappings:
9+
- containerPort: 31337
10+
hostPort: 31337
11+
- containerPort: 31340
12+
hostPort: 31340
13+
- containerPort: 31333
14+
hostPort: 31333
15+
- containerPort: 80
16+
hostPort: 80
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
kind: Cluster
2+
apiVersion: kind.x-k8s.io/v1alpha4
3+
nodes:
4+
- role: control-plane
5+
extraMounts:
6+
- containerPath: /var/lib/kubelet/config.json
7+
hostPath: ~/.docker/config.json
8+
extraPortMappings:
9+
- containerPort: 31337
10+
hostPort: 31337
11+
- containerPort: 31340
12+
hostPort: 31340
13+
- containerPort: 31333
14+
hostPort: 31333

0 commit comments

Comments
 (0)