Skip to content

Commit

Permalink
feat: support disabling generated IPAddressPool
Browse files Browse the repository at this point in the history
  • Loading branch information
rainest committed Oct 10, 2023
1 parent 05a2a38 commit 0d3b0ca
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 5 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## v0.40.0

- Metallb addon now has a builder. The builder can disable IPAddressPool
creation.
[#835](https://github.com/Kong/kubernetes-testing-framework/pull/835)

## v0.39.1

- Removed a module exclude that made `go install` unhappy.
Expand Down
44 changes: 39 additions & 5 deletions pkg/clusters/addons/metallb/metallb.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,9 @@ var (
}
)

type addon struct{}
type addon struct {
disablePoolCreation bool
}

func New() clusters.Addon {
return &addon{}
Expand All @@ -81,7 +83,7 @@ func (a *addon) Deploy(ctx context.Context, cluster clusters.Cluster) error {
return fmt.Errorf("the metallb addon is currently only supported on %s clusters", kind.KindClusterType)
}

return deployMetallbForKindCluster(ctx, cluster, kind.DefaultKindDockerNetwork)
return a.deployMetallbForKindCluster(ctx, cluster, kind.DefaultKindDockerNetwork)
}

func (a *addon) Delete(ctx context.Context, cluster clusters.Cluster) error {
Expand Down Expand Up @@ -151,7 +153,7 @@ var (
// -----------------------------------------------------------------------------

// deployMetallbForKindCluster deploys Metallb to the given Kind cluster using the Docker network provided for LoadBalancer IPs.
func deployMetallbForKindCluster(ctx context.Context, cluster clusters.Cluster, dockerNetwork string) error {
func (a *addon) deployMetallbForKindCluster(ctx context.Context, cluster clusters.Cluster, dockerNetwork string) error {
// ensure the namespace for metallb is created
ns := corev1.Namespace{ObjectMeta: metav1.ObjectMeta{Name: DefaultNamespace}}
if _, err := cluster.Client().CoreV1().Namespaces().Create(ctx, &ns, metav1.CreateOptions{}); err != nil {
Expand All @@ -167,8 +169,10 @@ func deployMetallbForKindCluster(ctx context.Context, cluster clusters.Cluster,
}

// create an ip address pool
if err := createIPAddressPool(ctx, cluster, dockerNetwork); err != nil {
return err
if !a.disablePoolCreation {
if err := createIPAddressPool(ctx, cluster, dockerNetwork); err != nil {
return err
}
}

if err := createL2Advertisement(ctx, cluster); err != nil {
Expand Down Expand Up @@ -400,3 +404,33 @@ func metallbDeleteHack(ctx context.Context, kubeconfig *os.File) error {
WithStdout(io.Discard).
Do(ctx)
}

// -----------------------------------------------------------------------------
// Metallb Builder
// -----------------------------------------------------------------------------

// Builder is a configuration tool for metallb cluster.Addons.
type Builder struct {
disablePoolCreation bool
}

// NewBuilder provides a new Builder object with default addon settings.
func NewBuilder() *Builder {
builder := &Builder{
disablePoolCreation: false,
}
return builder
}

// WithPoolDisabled instructs the builder to create addons with pool creation disabled.
func (b *Builder) WithPoolDisabled() *Builder {
b.disablePoolCreation = true
return b
}

// Build generates an addon with the builder's configuration.
func (b *Builder) Build() *addon {

Check warning on line 432 in pkg/clusters/addons/metallb/metallb.go

View workflow job for this annotation

GitHub Actions / golangci-lint

unexported-return: exported method Build returns unexported type *metallb.addon, which can be annoying to use (revive)
return &addon{
disablePoolCreation: b.disablePoolCreation,
}
}

0 comments on commit 0d3b0ca

Please sign in to comment.