Skip to content

Commit

Permalink
feat: add load image addon (#151)
Browse files Browse the repository at this point in the history
Add an addon to load images from the local Docker environment into the 
test cluster. Currently only supports KIND.
  • Loading branch information
Travis Raines authored Nov 18, 2021
1 parent 67eb46f commit 4c5baa4
Show file tree
Hide file tree
Showing 3 changed files with 129 additions and 0 deletions.
68 changes: 68 additions & 0 deletions pkg/clusters/addons/loadimage/addon.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package loadimage

import (
"context"
"fmt"

"k8s.io/apimachinery/pkg/runtime"

"github.com/kong/kubernetes-testing-framework/pkg/clusters"
"github.com/kong/kubernetes-testing-framework/pkg/clusters/types/kind"
)

// -----------------------------------------------------------------------------
// CertManager Addon
// -----------------------------------------------------------------------------

const (
// AddonName indicates the unique name of this addon.
AddonName clusters.AddonName = "loadimage"
)

type Addon struct {
image string
loaded bool
}

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

// -----------------------------------------------------------------------------
// CertManager Addon - Addon Implementation
// -----------------------------------------------------------------------------

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

func (a *Addon) Deploy(ctx context.Context, cluster clusters.Cluster) error {
switch ctype := cluster.Type(); ctype {
case kind.KindClusterType:
if err := a.loadIntoKind(cluster); err != nil {
return err
}
default:
return fmt.Errorf("loadimage addon is not supported by cluster type '%v'", cluster.Type())
}

return nil
}

func (a *Addon) Delete(ctx context.Context, cluster clusters.Cluster) error {
switch ctype := cluster.Type(); ctype {
case kind.KindClusterType:
// per https://github.com/kubernetes-sigs/kind/issues/658 this is basically impossible
// we lie here, because we want to mask this error. not deleting an image from KIND is benign:
// you either don't use it after (in which case you shouldn't care that it's still present) or
// load another image with the same name (in which case the name will point to the new image)
return nil
default:
return fmt.Errorf("loadimage addon is not supported by cluster type '%v'", cluster.Type())
}
}

func (a *Addon) Ready(ctx context.Context, cluster clusters.Cluster) ([]runtime.Object, bool, error) {
// no way to verify this, we rely on Deploy's cmd.Run() not failing
return nil, a.loaded, nil
}
32 changes: 32 additions & 0 deletions pkg/clusters/addons/loadimage/builder.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package loadimage

import (
"errors"
)

// -----------------------------------------------------------------------------
// CertManager Addon - Builder
// -----------------------------------------------------------------------------

type Builder struct {
image string
}

func NewBuilder() *Builder {
return &Builder{}
}

func (b *Builder) WithImage(image string) (*Builder, error) {
if len(image) == 0 {
return nil, errors.New("no image provided")
}
b.image = image
return b, nil
}

func (b *Builder) Build() *Addon {
return &Addon{
image: b.image,
loaded: false,
}
}
29 changes: 29 additions & 0 deletions pkg/clusters/addons/loadimage/cluster_implementations.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package loadimage

import (
"bytes"
"fmt"
"io"
"os/exec"

"github.com/kong/kubernetes-testing-framework/pkg/clusters"
)

func (a *Addon) loadIntoKind(cluster clusters.Cluster) error {
deployArgs := []string{
"load", "docker-image",
a.image,
"--name", cluster.Name(),
}

stderr := new(bytes.Buffer)
cmd := exec.Command("kind", deployArgs...)
cmd.Stdout = io.Discard
cmd.Stderr = stderr

if err := cmd.Run(); err != nil {
return fmt.Errorf("%s: %w", stderr.String(), err)
}
a.loaded = true
return nil
}

0 comments on commit 4c5baa4

Please sign in to comment.