@@ -6,14 +6,17 @@ import (
6
6
"fmt"
7
7
"io/fs"
8
8
"os"
9
+ "strconv"
9
10
"strings"
10
11
11
12
"github.com/cnoe-io/idpbuilder/pkg/runtime"
12
13
"github.com/cnoe-io/idpbuilder/pkg/util"
13
14
"sigs.k8s.io/controller-runtime/pkg/log"
15
+ kindv1alpha4 "sigs.k8s.io/kind/pkg/apis/config/v1alpha4"
14
16
"sigs.k8s.io/kind/pkg/cluster"
15
17
"sigs.k8s.io/kind/pkg/cluster/nodes"
16
18
"sigs.k8s.io/kind/pkg/cluster/nodeutils"
19
+ "sigs.k8s.io/yaml"
17
20
)
18
21
19
22
var (
@@ -66,7 +69,7 @@ func (c *Cluster) getConfig() ([]byte, error) {
66
69
}
67
70
68
71
if err != nil {
69
- return [] byte {}, err
72
+ return nil , fmt . Errorf ( "reading kind config: %w" , err )
70
73
}
71
74
72
75
var portMappingPairs []PortMapping
@@ -82,8 +85,6 @@ func (c *Cluster) getConfig() ([]byte, error) {
82
85
portMappingPairs [i ] = PortMapping {parts [0 ], parts [1 ]}
83
86
}
84
87
}
85
- } else {
86
- portMappingPairs = nil
87
88
}
88
89
89
90
var retBuff []byte
@@ -92,7 +93,20 @@ func (c *Cluster) getConfig() ([]byte, error) {
92
93
KubernetesVersion : c .kubeVersion ,
93
94
ExtraPortsMapping : portMappingPairs ,
94
95
}); 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
96
110
}
97
111
98
112
return retBuff , nil
@@ -214,3 +228,42 @@ func (c *Cluster) Reconcile(ctx context.Context, recreate bool) error {
214
228
func (c * Cluster ) ExportKubeConfig (name string , internal bool ) error {
215
229
return c .provider .ExportKubeConfig (name , c .kubeConfigPath , internal )
216
230
}
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
+ }
0 commit comments