Skip to content
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
30 changes: 15 additions & 15 deletions action.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ func (a *Action) get(
arg := a.Get.Arg
argRep := gdtcontext.ReplaceVariables(ctx, arg)
if arg != argRep {
debug.Println(
debug.Printf(
ctx,
"kube.get: replaced arg: %s -> %s",
arg, argRep,
Expand All @@ -145,7 +145,7 @@ func (a *Action) get(
} else {
nameRep := gdtcontext.ReplaceVariables(ctx, name)
if name != nameRep {
debug.Println(
debug.Printf(
ctx,
"kube.get: replaced name: %s -> %s",
name, nameRep,
Expand Down Expand Up @@ -177,15 +177,15 @@ func (a *Action) doList(
opts.LabelSelector = labelsStr
}
if c.resourceNamespaced(res) {
debug.Println(
debug.Printf(
ctx, "kube.get: %s%s (ns: %s)",
resName, labelSelString, ns,
)
return c.client.Resource(res).Namespace(ns).List(
ctx, opts,
)
}
debug.Println(
debug.Printf(
ctx, "kube.get: %s%s (non-namespaced resource)",
resName, labelSelString,
)
Expand All @@ -204,7 +204,7 @@ func (a *Action) doGet(
) (*unstructured.Unstructured, error) {
resName := res.Resource
if c.resourceNamespaced(res) {
debug.Println(
debug.Printf(
ctx, "kube.get: %s/%s (ns: %s)",
resName, name, ns,
)
Expand All @@ -214,7 +214,7 @@ func (a *Action) doGet(
metav1.GetOptions{},
)
}
debug.Println(
debug.Printf(
ctx, "kube.get: %s/%s (non-namespaced resource)",
resName, name,
)
Expand Down Expand Up @@ -275,14 +275,14 @@ func (a *Action) create(
if ons == "" {
ons = ns
}
debug.Println(ctx, "kube.create: %s (ns: %s)", resName, ons)
debug.Printf(ctx, "kube.create: %s (ns: %s)", resName, ons)
obj, err = c.client.Resource(res).Namespace(ons).Create(
ctx,
obj,
metav1.CreateOptions{},
)
} else {
debug.Println(ctx, "kube.create: %s (non-namespaced resource)", resName)
debug.Printf(ctx, "kube.create: %s (non-namespaced resource)", resName)
obj, err = c.client.Resource(res).Create(
ctx,
obj,
Expand Down Expand Up @@ -348,7 +348,7 @@ func (a *Action) apply(
if ons == "" {
ons = ns
}
debug.Println(ctx, "kube.apply: %s (ns: %s)", resName, ons)
debug.Printf(ctx, "kube.apply: %s (ns: %s)", resName, ons)
obj, err = c.client.Resource(res).Namespace(ns).Apply(
ctx,
// NOTE(jaypipes): Not sure why a separate name argument is
Expand All @@ -362,7 +362,7 @@ func (a *Action) apply(
metav1.ApplyOptions{FieldManager: fieldManagerName, Force: true},
)
} else {
debug.Println(ctx, "kube.apply: %s (non-namespaced resource)", resName)
debug.Printf(ctx, "kube.apply: %s (non-namespaced resource)", resName)
obj, err = c.client.Resource(res).Apply(
ctx,
// NOTE(jaypipes): Not sure why a separate name argument is
Expand Down Expand Up @@ -446,15 +446,15 @@ func (a *Action) doDelete(
) error {
nameRep := gdtcontext.ReplaceVariables(ctx, name)
if name != nameRep {
debug.Println(
debug.Printf(
ctx,
"kube.delete: replaced name: %s -> %s",
name, nameRep,
)
}
resName := res.Resource
if c.resourceNamespaced(res) {
debug.Println(
debug.Printf(
ctx, "kube.delete: %s/%s (ns: %s)",
resName, nameRep, ns,
)
Expand All @@ -464,7 +464,7 @@ func (a *Action) doDelete(
metav1.DeleteOptions{},
)
}
debug.Println(
debug.Printf(
ctx, "kube.delete: %s/%s (non-namespaced resource)",
resName, nameRep,
)
Expand Down Expand Up @@ -494,7 +494,7 @@ func (a *Action) doDeleteCollection(
}
resName := res.Resource
if c.resourceNamespaced(res) {
debug.Println(
debug.Printf(
ctx, "kube.delete: %s%s (ns: %s)",
resName, labelSelString, ns,
)
Expand All @@ -504,7 +504,7 @@ func (a *Action) doDeleteCollection(
opts,
)
}
debug.Println(
debug.Printf(
ctx, "kube.delete: %s%s (non-namespaced resource)",
resName, labelSelString,
)
Expand Down
4 changes: 2 additions & 2 deletions compare.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ func replaceVariablesInMapEntry(
) (string, any) {
kRep := gdtcontext.ReplaceVariables(ctx, k)
if k != kRep {
debug.Println(
debug.Printf(
ctx,
"kube.assert: replaced match key: %s -> %s",
k, kRep,
Expand All @@ -188,7 +188,7 @@ func replaceVariablesInMapEntry(
case string:
entryRep := gdtcontext.ReplaceVariables(ctx, entry)
if entry != entryRep {
debug.Println(
debug.Printf(
ctx,
"kube.assert: replaced match key %s value: %s -> %s",
k, entry, entryRep,
Expand Down
54 changes: 53 additions & 1 deletion eval.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"context"

"github.com/gdt-dev/core/api"
gdtcontext "github.com/gdt-dev/core/context"
"github.com/gdt-dev/core/debug"
kubeerrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand All @@ -29,7 +30,7 @@ func (s *Spec) Eval(ctx context.Context) (*api.Result, error) {
return nil, err
}
if nsCreated {
debug.Println(ctx, "auto-created namespace: %s", ns)
debug.Printf(ctx, "auto-created namespace: %s", ns)
}

var out any
Expand All @@ -45,6 +46,9 @@ func (s *Spec) Eval(ctx context.Context) (*api.Result, error) {
a := newAssertions(c, s.Assert, err, out)
if a.OK(ctx) {
res := api.NewResult()
if nsCreated {
res.AddCleanup(cleanupAutoNamespace(ctx, c, ns))
}
if err := saveVars(ctx, s.Var, out, res); err != nil {
return nil, err
}
Expand All @@ -53,6 +57,54 @@ func (s *Spec) Eval(ctx context.Context) (*api.Result, error) {
return api.NewResult(api.WithFailures(a.Failures()...)), nil
}

// cleanupAutoNamespace returns a cleanup function that deletes the
// auto-created namespace.
func cleanupAutoNamespace(
ctx context.Context,
c *connection,
ns string,
) func() {
debug.Printf(
ctx, "registered cleanup for auto-created namespace: %s", ns,
)
// NOTE(jaypipes): We need to create a new context that will be used to
// execute the cleanup because the context supplied is for the spec and
// that context has its own lifecycle (and gets a cancel/timeout that will
// be called before the cleanup function runs...
cleanupCtx := context.Background()
tu := gdtcontext.TestUnit(ctx)
if tu != nil {
cleanupCtx = gdtcontext.SetTestUnit(cleanupCtx, tu)
}
debuggers := gdtcontext.Debug(ctx)
if len(debuggers) > 0 {
cleanupCtx = gdtcontext.SetDebug(cleanupCtx, debuggers...)
}
trace := gdtcontext.Trace(ctx)
cleanupCtx = gdtcontext.SetTrace(cleanupCtx, trace)
return func() {
res, err := c.gvrFromArg("namespaces")
if err != nil {
debug.Printf(cleanupCtx, "failed to get GVR for namespace")
return
}

err = c.client.Resource(res).Delete(
cleanupCtx,
ns,
metav1.DeleteOptions{},
)
if err != nil {
debug.Printf(
cleanupCtx, "failed to delete auto-created namespace %s: %s",
ns, err,
)
return
}
debug.Printf(cleanupCtx, "deleted auto-created namespace: %s", ns)
}
}

// ensureNamespace automatically creates a supplied Kubernetes Namespace if it
// does not already exist, returning whether the namespace was created.
func ensureNamespace(
Expand Down
Loading
Loading