This repository has been archived by the owner on Feb 16, 2024. It is now read-only.
forked from kudobuilder/kuttl
-
Notifications
You must be signed in to change notification settings - Fork 5
/
kind.go
111 lines (90 loc) · 2.67 KB
/
kind.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package test
import (
"context"
"testing"
"k8s.io/apimachinery/pkg/version"
"sigs.k8s.io/kind/pkg/apis/config/v1alpha4"
"sigs.k8s.io/kind/pkg/cluster"
"sigs.k8s.io/kind/pkg/cluster/nodes"
"sigs.k8s.io/kind/pkg/cluster/nodeutils"
testutils "github.com/kyverno/kuttl/pkg/test/utils"
)
// kind provides a thin abstraction layer for a KIND cluster.
type kind struct {
Provider *cluster.Provider
context string
explicitPath string
}
func newKind(kindContext string, explicitPath string, logger testutils.Logger) kind {
provider := cluster.NewProvider(cluster.ProviderWithLogger(&kindLogger{logger}))
return kind{
Provider: provider,
context: kindContext,
explicitPath: explicitPath,
}
}
// Run starts a KIND cluster from a given configuration.
func (k *kind) Run(config *v1alpha4.Cluster) error {
return k.Provider.Create(
k.context,
cluster.CreateWithV1Alpha4Config(config),
cluster.CreateWithKubeconfigPath(k.explicitPath),
cluster.CreateWithRetain(true),
)
}
// IsRunning checks if a KIND cluster is already running for the current context.
func (k *kind) IsRunning() bool {
contexts, err := k.Provider.List()
if err != nil {
panic(err)
}
for _, context := range contexts {
if context == k.context {
return true
}
}
return false
}
// AddContainers loads the named Docker containers into a KIND cluster.
// The cluster must be running for this to work.
func (k *kind) AddContainers(docker testutils.DockerClient, containers []string, t *testing.T) error {
if !k.IsRunning() {
panic("KIND cluster isn't running")
}
t.Logf("Adding Containers to KIND...\n")
nodes, err := k.Provider.ListNodes(k.context)
if err != nil {
return err
}
for _, node := range nodes {
for _, container := range containers {
t.Logf("Add image %s to node %s\n", container, node.String())
if err := loadContainer(docker, node, container); err != nil {
return err
}
}
}
return nil
}
// CollectLogs saves the cluster logs to a directory.
func (k *kind) CollectLogs(dir string) error {
return k.Provider.CollectLogs(k.context, dir)
}
// Stop stops the KIND cluster.
func (k *kind) Stop() error {
return k.Provider.Delete(k.context, k.explicitPath)
}
func loadContainer(docker testutils.DockerClient, node nodes.Node, container string) error {
image, err := docker.ImageSave(context.TODO(), []string{container})
if err != nil {
return err
}
defer image.Close()
return nodeutils.LoadImageArchive(node, image)
}
// IsMinVersion checks if pass ver is the min required kind version
func IsMinVersion(ver string) bool {
minVersion := "kind.sigs.k8s.io/v1alpha4"
comp := version.CompareKubeAwareVersionStrings(minVersion, ver)
return comp != -1
}