Skip to content

Commit

Permalink
Support configuring ExtraPortMappings for the kind cluster
Browse files Browse the repository at this point in the history
  • Loading branch information
klzsysy committed Jan 24, 2024
1 parent 28f0335 commit fe4a08b
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 6 deletions.
25 changes: 19 additions & 6 deletions test/framework/bootstrap/kind_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,13 @@ func WithDualStackFamily() KindClusterOption {
})
}

// WithExtraPortMappings implements a New Option that instruct the kindClusterProvider to set extra port forward mappings.
func WithExtraPortMappings(mappings []kindv1.PortMapping) KindClusterOption {
return kindClusterOptionAdapter(func(k *KindClusterProvider) {
k.extraPortMappings = mappings
})
}

// LogFolder implements a New Option that instruct the kindClusterProvider to dump bootstrap logs in a folder in case of errors.
func LogFolder(path string) KindClusterOption {
return kindClusterOptionAdapter(func(k *KindClusterProvider) {
Expand All @@ -104,12 +111,13 @@ func NewKindClusterProvider(name string, options ...KindClusterOption) *KindClus

// KindClusterProvider implements a ClusterProvider that can create a kind cluster.
type KindClusterProvider struct {
name string
withDockerSock bool
kubeconfigPath string
nodeImage string
ipFamily clusterv1.ClusterIPFamily
logFolder string
name string
withDockerSock bool
kubeconfigPath string
nodeImage string
ipFamily clusterv1.ClusterIPFamily
logFolder string
extraPortMappings []kindv1.PortMapping
}

// Create a Kubernetes cluster using kind.
Expand Down Expand Up @@ -139,6 +147,11 @@ func (k *KindClusterProvider) createKindCluster() {
APIVersion: "kind.x-k8s.io/v1alpha4",
Kind: "Cluster",
},
Nodes: []kindv1.Node{
{
ExtraPortMappings: k.extraPortMappings,
},
},
}

if k.ipFamily == clusterv1.IPv6IPFamily {
Expand Down
5 changes: 5 additions & 0 deletions test/framework/bootstrap/kind_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (

. "github.com/onsi/gomega"
"github.com/pkg/errors"
kindv1 "sigs.k8s.io/kind/pkg/apis/config/v1alpha4"
kind "sigs.k8s.io/kind/pkg/cluster"
kindnodes "sigs.k8s.io/kind/pkg/cluster/nodes"
kindnodesutils "sigs.k8s.io/kind/pkg/cluster/nodeutils"
Expand Down Expand Up @@ -52,6 +53,9 @@ type CreateKindBootstrapClusterAndLoadImagesInput struct {

// LogFolder where to dump logs in case of errors
LogFolder string

// ExtraPortMappings specifies the port forward configuration of the kind node.
ExtraPortMappings []kindv1.PortMapping
}

// CreateKindBootstrapClusterAndLoadImages returns a new Kubernetes cluster with pre-loaded images.
Expand All @@ -77,6 +81,7 @@ func CreateKindBootstrapClusterAndLoadImages(ctx context.Context, input CreateKi
if input.LogFolder != "" {
options = append(options, LogFolder(input.LogFolder))
}
options = append(options, WithExtraPortMappings(input.ExtraPortMappings))

clusterProvider := NewKindClusterProvider(input.Name, options...)
Expect(clusterProvider).ToNot(BeNil(), "Failed to create a kind cluster")
Expand Down

0 comments on commit fe4a08b

Please sign in to comment.