Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support disabling generated IPAddressPool #835

Merged
merged 2 commits into from
Oct 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
31 changes: 31 additions & 0 deletions pkg/clusters/addons/metallb/builder.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package metallb

// -----------------------------------------------------------------------------
// 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
}

// WithIPAddressPoolDisabled instructs the builder to create addons with pool creation disabled.
func (b *Builder) WithIPAddressPoolDisabled() *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,
}
}
28 changes: 16 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
Loading