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 c4d9d8b
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 12 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
58 changes: 46 additions & 12 deletions pkg/clusters/addons/metallb/metallb.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,33 +58,35 @@ var (
}
)

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

func New() clusters.Addon {
return &addon{}
return &Addon{}
}

// -----------------------------------------------------------------------------
// Metallb Addon - Addon Implementation
// -----------------------------------------------------------------------------

func (a *addon) Name() clusters.AddonName {
func (a *Addon) Name() clusters.AddonName {
return AddonName
}

func (a *addon) Dependencies(_ context.Context, _ clusters.Cluster) []clusters.AddonName {
func (a *Addon) Dependencies(_ context.Context, _ clusters.Cluster) []clusters.AddonName {
return nil
}

func (a *addon) Deploy(ctx context.Context, cluster clusters.Cluster) error {
func (a *Addon) Deploy(ctx context.Context, cluster clusters.Cluster) error {
if cluster.Type() != kind.KindClusterType {
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 {
func (a *Addon) Delete(ctx context.Context, cluster clusters.Cluster) error {
if cluster.Type() != kind.KindClusterType {
return fmt.Errorf("the metallb addon is currently only supported on %s clusters", kind.KindClusterType)
}
Expand Down Expand Up @@ -115,7 +117,7 @@ func (a *addon) Delete(ctx context.Context, cluster clusters.Cluster) error {
return metallbDeleteHack(ctx, kubeconfig)
}

func (a *addon) Ready(ctx context.Context, cluster clusters.Cluster) ([]runtime.Object, bool, error) {
func (a *Addon) Ready(ctx context.Context, cluster clusters.Cluster) ([]runtime.Object, bool, error) {
deployment, err := cluster.Client().AppsV1().Deployments(DefaultNamespace).
Get(ctx, "controller", metav1.GetOptions{})
if err != nil {
Expand All @@ -132,7 +134,7 @@ func (a *addon) Ready(ctx context.Context, cluster clusters.Cluster) ([]runtime.
return nil, true, nil
}

func (a *addon) DumpDiagnostics(context.Context, clusters.Cluster) (map[string][]byte, error) {
func (a *Addon) DumpDiagnostics(context.Context, clusters.Cluster) (map[string][]byte, error) {
diagnostics := make(map[string][]byte)
return diagnostics, nil
}
Expand All @@ -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 {
return &Addon{
disablePoolCreation: b.disablePoolCreation,
}
}

0 comments on commit c4d9d8b

Please sign in to comment.