-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
Showing
3 changed files
with
129 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |