Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add vcluster e2e cluster provider #450

Merged
merged 8 commits into from
Nov 14, 2024
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
78 changes: 78 additions & 0 deletions examples/vcluster/vcluster_with_config/main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/*
Copyright 2024 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 vcluster

import (
"context"
"log"
"os"
"testing"

"sigs.k8s.io/e2e-framework/klient/conf"
"sigs.k8s.io/e2e-framework/pkg/env"
"sigs.k8s.io/e2e-framework/pkg/envconf"
"sigs.k8s.io/e2e-framework/pkg/envfuncs"
"sigs.k8s.io/e2e-framework/support"
"sigs.k8s.io/e2e-framework/support/kind"
"sigs.k8s.io/e2e-framework/third_party/vcluster"
)

var testenv env.Environment

func TestMain(m *testing.M) {
opts := []support.ClusterOpts{}
// vcluster requires a "host" cluster to install into, so we should resolve one
if os.Getenv("REAL_CLUSTER") == "true" {
cfg := conf.ResolveKubeConfigFile()
opts = append(opts, vcluster.WithHostKubeConfig(cfg))
} else {
// create a kind cluster to use as the vcluster "host"
cfg, err := kind.NewProvider().WithName("kind-vc-host").Create(context.Background())
if err != nil {
log.Fatal(err)
}
opts = append(opts, vcluster.WithHostKubeConfig(cfg))
}

testenv, _ = env.NewFromFlags()
vclusterName := envconf.RandomName("vcluster-with-config", 16)
namespace := envconf.RandomName("vcluster-ns", 16)
opts = append(opts, vcluster.WithNamespace(namespace))
testenv.Setup(
envfuncs.CreateNamespace(namespace),
envfuncs.CreateClusterWithConfig(vcluster.NewProvider(), vclusterName, "values.yaml", opts...),
)

testenv.Finish(
envfuncs.DestroyCluster(vclusterName),
envfuncs.DeleteNamespace(namespace),
)

if os.Getenv("REAL_CLUSTER") != "true" {
// cleanup the vcluster "host"-kind-cluster
testenv.Finish(
func(ctx context.Context, c *envconf.Config) (context.Context, error) {
if err := kind.NewProvider().WithName("kind-vc-host").Destroy(ctx); err != nil {
return ctx, err
}
return ctx, nil
},
)
}

os.Exit(testenv.Run(m))
}
1 change: 1 addition & 0 deletions examples/vcluster/vcluster_with_config/values.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# TODO: add vcluster configs
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ require (
k8s.io/component-base v0.31.2
k8s.io/klog/v2 v2.130.1
sigs.k8s.io/controller-runtime v0.19.1
sigs.k8s.io/yaml v1.4.0
)

require (
Expand Down Expand Up @@ -61,5 +62,4 @@ require (
k8s.io/utils v0.0.0-20240711033017-18e509b52bc8 // indirect
sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd // indirect
sigs.k8s.io/structured-merge-diff/v4 v4.4.1 // indirect
sigs.k8s.io/yaml v1.4.0 // indirect
)
4 changes: 2 additions & 2 deletions pkg/env/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ func (e *testEnv) processTestFeature(ctx context.Context, t *testing.T, featureN
t.Helper()
skipped, message := e.requireFeatureProcessing(feature)
if skipped {
t.Skipf(message)
t.Skip(message)
}
// execute beforeEachFeature actions
ctx = e.processFeatureActions(ctx, t, feature, e.getBeforeFeatureActions())
Expand Down Expand Up @@ -510,7 +510,7 @@ func (e *testEnv) execFeature(ctx context.Context, t *testing.T, featName string
internalT.Helper()
skipped, message := e.requireAssessmentProcessing(assess, i+1)
if skipped {
internalT.Skipf(message)
internalT.Skip(message)
}
// Set shouldFailNow to true before actually running the assessment, because if the assessment
// calls t.FailNow(), the function will be abruptly stopped in the middle of `e.executeSteps()`.
Expand Down
19 changes: 10 additions & 9 deletions third_party/flux/flux_setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package flux

import (
"context"
"errors"
"fmt"

"sigs.k8s.io/e2e-framework/pkg/env"
Expand All @@ -44,7 +45,7 @@ func InstallFlux(opts ...Option) env.Func {
func CreateGitRepo(gitRepoName, gitRepoURL string, opts ...Option) env.Func {
return func(ctx context.Context, c *envconf.Config) (context.Context, error) {
if manager == nil {
return ctx, fmt.Errorf(NoFluxInstallationFoundMsg)
return ctx, errors.New(NoFluxInstallationFoundMsg)
}
err := manager.createSource(Git, gitRepoName, gitRepoURL, opts...)
if err != nil {
Expand All @@ -58,7 +59,7 @@ func CreateGitRepo(gitRepoName, gitRepoURL string, opts ...Option) env.Func {
func CreateHelmRepository(name, url string, opts ...Option) env.Func {
return func(ctx context.Context, c *envconf.Config) (context.Context, error) {
if manager == nil {
return ctx, fmt.Errorf(NoFluxInstallationFoundMsg)
return ctx, errors.New(NoFluxInstallationFoundMsg)
}
err := manager.createSource(Helm, name, url, opts...)
if err != nil {
Expand All @@ -72,7 +73,7 @@ func CreateHelmRepository(name, url string, opts ...Option) env.Func {
func CreateKustomization(kustomizationName, sourceRef string, opts ...Option) env.Func {
return func(ctx context.Context, c *envconf.Config) (context.Context, error) {
if manager == nil {
return ctx, fmt.Errorf(NoFluxInstallationFoundMsg)
return ctx, errors.New(NoFluxInstallationFoundMsg)
}
err := manager.createKustomization(kustomizationName, sourceRef, opts...)
if err != nil {
Expand All @@ -87,7 +88,7 @@ func CreateKustomization(kustomizationName, sourceRef string, opts ...Option) en
func CreateHelmRelease(name, source, chart string, opts ...Option) env.Func {
return func(ctx context.Context, c *envconf.Config) (context.Context, error) {
if manager == nil {
return ctx, fmt.Errorf(NoFluxInstallationFoundMsg)
return ctx, errors.New(NoFluxInstallationFoundMsg)
}
err := manager.createHelmRelease(name, source, chart, opts...)
if err != nil {
Expand All @@ -101,7 +102,7 @@ func CreateHelmRelease(name, source, chart string, opts ...Option) env.Func {
func UninstallFlux(opts ...Option) env.Func {
return func(ctx context.Context, c *envconf.Config) (context.Context, error) {
if manager == nil {
return ctx, fmt.Errorf(NoFluxInstallationFoundMsg)
return ctx, errors.New(NoFluxInstallationFoundMsg)
}
err := manager.uninstallFlux(opts...)
if err != nil {
Expand All @@ -115,7 +116,7 @@ func UninstallFlux(opts ...Option) env.Func {
func DeleteKustomization(kustomizationName string, opts ...Option) env.Func {
return func(ctx context.Context, c *envconf.Config) (context.Context, error) {
if manager == nil {
return ctx, fmt.Errorf(NoFluxInstallationFoundMsg)
return ctx, errors.New(NoFluxInstallationFoundMsg)
}
err := manager.deleteKustomization(kustomizationName, opts...)
if err != nil {
Expand All @@ -129,7 +130,7 @@ func DeleteKustomization(kustomizationName string, opts ...Option) env.Func {
func DeleteHelmRelease(name string, opts ...Option) env.Func {
return func(ctx context.Context, c *envconf.Config) (context.Context, error) {
if manager == nil {
return ctx, fmt.Errorf(NoFluxInstallationFoundMsg)
return ctx, errors.New(NoFluxInstallationFoundMsg)
}
err := manager.deleteHelmRelease(name, opts...)
if err != nil {
Expand All @@ -143,7 +144,7 @@ func DeleteHelmRelease(name string, opts ...Option) env.Func {
func DeleteGitRepo(gitRepoName string, opts ...Option) env.Func {
return func(ctx context.Context, c *envconf.Config) (context.Context, error) {
if manager == nil {
return ctx, fmt.Errorf(NoFluxInstallationFoundMsg)
return ctx, errors.New(NoFluxInstallationFoundMsg)
}
err := manager.deleteSource(Git, gitRepoName, opts...)
if err != nil {
Expand All @@ -157,7 +158,7 @@ func DeleteGitRepo(gitRepoName string, opts ...Option) env.Func {
func DeleteHelmRepo(name string, opts ...Option) env.Func {
return func(ctx context.Context, c *envconf.Config) (context.Context, error) {
if manager == nil {
return ctx, fmt.Errorf(NoFluxInstallationFoundMsg)
return ctx, errors.New(NoFluxInstallationFoundMsg)
}
err := manager.deleteSource(Helm, name, opts...)
if err != nil {
Expand Down
3 changes: 2 additions & 1 deletion third_party/helm/helm.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package helm

import (
"bytes"
"errors"
"fmt"
"strings"

Expand Down Expand Up @@ -237,7 +238,7 @@ func (m *Manager) run(opts *Opts) (err error) {
}
log.V(4).InfoS("Determining if helm binary is available or not", "executable", m.path)
if m.e.Prog().Avail(m.path) == "" {
err = fmt.Errorf(missingHelm)
err = errors.New(missingHelm)
return
}
command, err := m.getCommand(opts)
Expand Down
3 changes: 2 additions & 1 deletion third_party/ko/ko.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package ko
import (
"bytes"
"context"
"errors"
"fmt"
"strings"

Expand Down Expand Up @@ -159,7 +160,7 @@ func (m *Manager) GetLocalImage(ctx context.Context, packagePath string) (string
func (m *Manager) run(opts *Opts) (out string, err error) {
log.V(4).InfoS("Determining if ko binary is available or not", "executable", m.path)
if m.e.Prog().Avail(m.path) == "" {
return "", fmt.Errorf(missingKo)
return "", errors.New(missingKo)
}

envs := m.getEnvs(opts)
Expand Down
3 changes: 2 additions & 1 deletion third_party/kubetest2/pkg/tester/e2e-tester_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@ limitations under the License.
package tester

import (
"github.com/octago/sflags/gen/gpflag"
"testing"

"github.com/octago/sflags/gen/gpflag"
)

func TestBuildFlags(t *testing.T) {
Expand Down
Loading