generated from kubernetes/kubernetes-template-project
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding env support functions and tests
- Loading branch information
1 parent
55eec9d
commit c1def61
Showing
4 changed files
with
99 additions
and
132 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
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
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
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,95 @@ | ||
/* | ||
Copyright 2021 The Kubernetes Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package kind | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"time" | ||
|
||
corev1 "k8s.io/api/core/v1" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"sigs.k8s.io/e2e-framework/pkg/env" | ||
"sigs.k8s.io/e2e-framework/pkg/envconf" | ||
) | ||
|
||
type contextKey string | ||
|
||
var ( | ||
ContextKeyCluster contextKey = "kindcluster" | ||
ContextKeyNamespace contextKey = "namespace" | ||
) | ||
|
||
// FuncCreate returns a pkg/env/Func that is used to | ||
// create a kind cluster and then generate a new cluster namespace (if autoNS=true). | ||
// The kind cluster instance is then injected in the context key value ContextKeyCluster. | ||
func FuncCreate(name string, autoNS bool) env.Func { | ||
return func(ctx context.Context, cfg *envconf.Config) (context.Context, error) { | ||
kind := NewCluster(name) | ||
kubecfg, err := kind.Create() | ||
if err != nil { | ||
return ctx, fmt.Errorf("kind setup: %w", err) | ||
} | ||
|
||
// stall, give pods time | ||
time.Sleep(7 * time.Second) | ||
|
||
// update envconfig with kubeconfig | ||
if _, err := cfg.WithKubeconfigFile(kubecfg); err != nil { | ||
return ctx, fmt.Errorf("kind setup: envconfig: %w", err) | ||
} | ||
|
||
// create a namespace | ||
if autoNS { | ||
name := cfg.WithRandomNamespace().Namespace() | ||
namespace := corev1.Namespace{ObjectMeta: metav1.ObjectMeta{Name: name}} | ||
if err := cfg.Client().Resources().Create(ctx, &namespace); err != nil { | ||
return ctx, fmt.Errorf("kind setup: auto-create namespace: %w", err) | ||
} | ||
// save namespace for cleanup | ||
ctx = context.WithValue(ctx, ContextKeyNamespace, &namespace) | ||
} | ||
|
||
// forward cluster name and kubecfg file name (via context) | ||
// for cleanup later | ||
return context.WithValue(ctx, ContextKeyCluster, kind), nil | ||
} | ||
} | ||
|
||
// FuncDestroy returns an EnvFunc that | ||
// retrieves the kind cluster instance from the forwarded context | ||
// then deletes the cluster. | ||
func FuncDestroy() env.Func { | ||
return func(ctx context.Context, cfg *envconf.Config) (context.Context, error) { | ||
kind, ok := ctx.Value(ContextKeyCluster).(*Cluster) | ||
if !ok { | ||
return ctx, fmt.Errorf("kind destroy: unexpected type for cluster value") | ||
} | ||
|
||
// delete namespace if found | ||
if ns, ok := ctx.Value(ContextKeyCluster).(*corev1.Namespace); ok{ | ||
if err := cfg.Client().Resources().Delete(ctx, ns); err != nil { | ||
return ctx, fmt.Errorf("kind destroy: auto-create namespace: %w", err) | ||
} | ||
} | ||
|
||
if err := kind.Destroy(); err != nil { | ||
return ctx, fmt.Errorf("kind destroy: %w", err) | ||
} | ||
return ctx, nil | ||
} | ||
} |