5
5
package kind
6
6
7
7
import (
8
+ "context"
8
9
"strings"
9
10
11
+ gdtcontext "github.com/gdt-dev/gdt/context"
12
+ "github.com/gdt-dev/gdt/debug"
10
13
gdttypes "github.com/gdt-dev/gdt/types"
11
14
"github.com/samber/lo"
12
15
"sigs.k8s.io/kind/pkg/cluster"
@@ -20,9 +23,29 @@ import (
20
23
type KindFixture struct {
21
24
// provider is the KinD cluster provider
22
25
provider * cluster.Provider
23
- // cfgStr contains the stringified KUBECONFIG that KinD returns in its
24
- // Provider.KubeConfig() call
25
- cfgStr string
26
+ // deleteOnStop indicates that the KinD cluster should be deleted when
27
+ // the fixture is stopped. Fixtures are stopped when test scenarios
28
+ // utilizing the fixture have executed all their test steps.
29
+ //
30
+ // By default, KinD clusters that were already running when the fixture was
31
+ // started are not deleted. This is to prevent the deletion of KinD
32
+ // clusters that were in use outside of a gdt-kube execution. To override
33
+ // this behaviour and always delete the KinD cluster on stop, use the
34
+ // WithDeleteOnStop() modifier.
35
+ deleteOnStop bool
36
+ // retainOnStop indicates that the KinD cluster should *not* be deleted
37
+ // when the fixture is stopped. Fixtures are stopped when test scenarios
38
+ // utilizing the fixture have executed all their test steps.
39
+ //
40
+ // By default, KinD clusters that were *not* already running when the fixture was
41
+ // started are deleted when the fixture stops. This is to clean up KinD
42
+ // clusters that were created and used by the gdt-kube execution. To override
43
+ // this behaviour and always retain the KinD cluster on stop, use the
44
+ // WithRetainOnStop() modifier.
45
+ retainOnStop bool
46
+ // runningBeforeStart is true when the KinD cluster was already running
47
+ // when the fixture was started.
48
+ runningBeforeStart bool
26
49
// ClusterName is the name of the KinD cluster. If not specified, gdt will
27
50
// use the default cluster name that KinD uses, which is just "kind"
28
51
ClusterName string
@@ -34,20 +57,35 @@ type KindFixture struct {
34
57
ConfigPath string
35
58
}
36
59
37
- func (f * KindFixture ) Start () {
60
+ func (f * KindFixture ) Start (ctx context.Context ) {
61
+ ctx = gdtcontext .PushTrace (ctx , "fixtures.kind.start" )
62
+ defer func () {
63
+ ctx = gdtcontext .PopTrace (ctx )
64
+ }()
38
65
if f .ClusterName == "" {
39
66
f .ClusterName = kindconst .DefaultClusterName
40
67
}
41
68
if f .isRunning () {
69
+ debug .Println (ctx , "cluster %s already running" , f .ClusterName )
70
+ f .runningBeforeStart = true
42
71
return
43
72
}
44
73
opts := []cluster.CreateOption {}
45
74
if f .ConfigPath != "" {
75
+ debug .Println (
76
+ ctx , "using custom kind config %s for cluster %s" ,
77
+ f .ConfigPath , f .ClusterName ,
78
+ )
46
79
opts = append (opts , cluster .CreateWithConfigFile (f .ConfigPath ))
47
80
}
48
81
if err := f .provider .Create (f .ClusterName , opts ... ); err != nil {
49
82
panic (err )
50
83
}
84
+ debug .Println (ctx , "cluster %s successfully created" , f .ClusterName )
85
+ if ! f .retainOnStop {
86
+ f .deleteOnStop = true
87
+ debug .Println (ctx , "cluster %s will be deleted on stop" , f .ClusterName )
88
+ }
51
89
}
52
90
53
91
func (f * KindFixture ) isRunning () bool {
@@ -61,7 +99,26 @@ func (f *KindFixture) isRunning() bool {
61
99
return lo .Contains (clusterNames , f .ClusterName )
62
100
}
63
101
64
- func (f * KindFixture ) Stop () {}
102
+ func (f * KindFixture ) Stop (ctx context.Context ) {
103
+ ctx = gdtcontext .PushTrace (ctx , "fixtures.kind.stop" )
104
+ defer func () {
105
+ ctx = gdtcontext .PopTrace (ctx )
106
+ }()
107
+ if ! f .isRunning () {
108
+ debug .Println (ctx , "cluster %s not running" , f .ClusterName )
109
+ return
110
+ }
111
+ if f .runningBeforeStart && ! f .deleteOnStop {
112
+ debug .Println (ctx , "cluster %s was running before start and deleteOnStop=false so not deleting" , f .ClusterName )
113
+ return
114
+ }
115
+ if f .deleteOnStop {
116
+ if err := f .provider .Delete (f .ClusterName , "" ); err != nil {
117
+ panic (err )
118
+ }
119
+ debug .Println (ctx , "cluster %s successfully deleted" , f .ClusterName )
120
+ }
121
+ }
65
122
66
123
func (f * KindFixture ) HasState (key string ) bool {
67
124
lkey := strings .ToLower (key )
@@ -119,6 +176,36 @@ func WithConfigPath(path string) KindFixtureModifier {
119
176
}
120
177
}
121
178
179
+ // WithDeleteOnStop instructs gdt-kube to always delete the KinD cluster when
180
+ // the fixture stops. Fixtures are stopped when test scenarios utilizing the
181
+ // fixture have executed all their test steps.
182
+ //
183
+ // By default, KinD clusters that were already running when the fixture was
184
+ // started are not deleted. This is to prevent the deletion of KinD
185
+ // clusters that were in use outside of a gdt-kube execution. To override
186
+ // this behaviour and always delete the KinD cluster on stop, use the
187
+ // WithDeleteOnStop() modifier.
188
+ func WithDeleteOnStop () KindFixtureModifier {
189
+ return func (f * KindFixture ) {
190
+ f .deleteOnStop = true
191
+ }
192
+ }
193
+
194
+ // WithRetainOnStop instructs gdt-kube that the KinD cluster should *not* be
195
+ // deleted when the fixture is stopped. Fixtures are stopped when test
196
+ // scenarios utilizing the fixture have executed all their test steps.
197
+ //
198
+ // By default, KinD clusters that were *not* already running when the fixture
199
+ // was started are deleted when the fixture stops. This is to clean up KinD
200
+ // clusters that were created and used by the gdt-kube execution. To override
201
+ // this behaviour and always retain the KinD cluster on stop, use the
202
+ // WithRetainOnStop() modifier.
203
+ func WithRetainOnStop () KindFixtureModifier {
204
+ return func (f * KindFixture ) {
205
+ f .retainOnStop = true
206
+ }
207
+ }
208
+
122
209
// New returns a fixture that exposes Kubernetes configuration/context
123
210
// information about a KinD cluster. If no such KinD cluster exists, one will
124
211
// be created. If the KinD cluster is created, it is destroyed at the end of
0 commit comments