Skip to content

Commit 99d63be

Browse files
authored
Merge pull request #122 from DirectXMan12/feature/external-cluster-envtest
External clusters in envtest
2 parents 9fb6e8b + 748f55d commit 99d63be

File tree

4 files changed

+98
-65
lines changed

4 files changed

+98
-65
lines changed

pkg/envtest/ginkgo.go

Lines changed: 3 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,11 @@
1-
/*
2-
Copyright 2016 The Kubernetes Authors.
3-
4-
Licensed under the Apache License, Version 2.0 (the "License");
5-
you may not use this file except in compliance with the License.
6-
You may obtain a copy of the License at
7-
8-
http://www.apache.org/licenses/LICENSE-2.0
9-
10-
Unless required by applicable law or agreed to in writing, software
11-
distributed under the License is distributed on an "AS IS" BASIS,
12-
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13-
See the License for the specific language governing permissions and
14-
limitations under the License.
15-
*/
16-
171
package envtest
182

193
import (
20-
"fmt"
21-
22-
"github.com/onsi/ginkgo"
23-
"github.com/onsi/ginkgo/config"
24-
"github.com/onsi/ginkgo/types"
4+
"sigs.k8s.io/controller-runtime/pkg/envtest/printer"
255
)
266

27-
var _ ginkgo.Reporter = NewlineReporter{}
28-
297
// NewlineReporter is Reporter that Prints a newline after the default Reporter output so that the results
308
// are correctly parsed by test automation.
319
// See issue https://github.com/jstemmer/go-junit-report/issues/31
32-
type NewlineReporter struct{}
33-
34-
// SpecSuiteWillBegin implements ginkgo.Reporter
35-
func (NewlineReporter) SpecSuiteWillBegin(config config.GinkgoConfigType, summary *types.SuiteSummary) {
36-
}
37-
38-
// BeforeSuiteDidRun implements ginkgo.Reporter
39-
func (NewlineReporter) BeforeSuiteDidRun(setupSummary *types.SetupSummary) {}
40-
41-
// AfterSuiteDidRun implements ginkgo.Reporter
42-
func (NewlineReporter) AfterSuiteDidRun(setupSummary *types.SetupSummary) {}
43-
44-
// SpecWillRun implements ginkgo.Reporter
45-
func (NewlineReporter) SpecWillRun(specSummary *types.SpecSummary) {}
46-
47-
// SpecDidComplete implements ginkgo.Reporter
48-
func (NewlineReporter) SpecDidComplete(specSummary *types.SpecSummary) {}
49-
50-
// SpecSuiteDidEnd Prints a newline between "35 Passed | 0 Failed | 0 Pending | 0 Skipped" and "--- PASS:"
51-
func (NewlineReporter) SpecSuiteDidEnd(summary *types.SuiteSummary) { fmt.Printf("\n") }
10+
// It's re-exported here to avoid compatibility breakage/mass rewrites.
11+
type NewlineReporter = printer.NewlineReporter

pkg/envtest/printer/ginkgo.go

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
Copyright 2016 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package printer
18+
19+
import (
20+
"fmt"
21+
22+
"github.com/onsi/ginkgo"
23+
"github.com/onsi/ginkgo/config"
24+
"github.com/onsi/ginkgo/types"
25+
)
26+
27+
var _ ginkgo.Reporter = NewlineReporter{}
28+
29+
// NewlineReporter is Reporter that Prints a newline after the default Reporter output so that the results
30+
// are correctly parsed by test automation.
31+
// See issue https://github.com/jstemmer/go-junit-report/issues/31
32+
type NewlineReporter struct{}
33+
34+
// SpecSuiteWillBegin implements ginkgo.Reporter
35+
func (NewlineReporter) SpecSuiteWillBegin(config config.GinkgoConfigType, summary *types.SuiteSummary) {
36+
}
37+
38+
// BeforeSuiteDidRun implements ginkgo.Reporter
39+
func (NewlineReporter) BeforeSuiteDidRun(setupSummary *types.SetupSummary) {}
40+
41+
// AfterSuiteDidRun implements ginkgo.Reporter
42+
func (NewlineReporter) AfterSuiteDidRun(setupSummary *types.SetupSummary) {}
43+
44+
// SpecWillRun implements ginkgo.Reporter
45+
func (NewlineReporter) SpecWillRun(specSummary *types.SpecSummary) {}
46+
47+
// SpecDidComplete implements ginkgo.Reporter
48+
func (NewlineReporter) SpecDidComplete(specSummary *types.SpecSummary) {}
49+
50+
// SpecSuiteDidEnd Prints a newline between "35 Passed | 0 Failed | 0 Pending | 0 Skipped" and "--- PASS:"
51+
func (NewlineReporter) SpecSuiteDidEnd(summary *types.SuiteSummary) { fmt.Printf("\n") }

pkg/envtest/server.go

Lines changed: 42 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222

2323
apiextensionsv1beta1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1beta1"
2424
"k8s.io/client-go/rest"
25+
"sigs.k8s.io/controller-runtime/pkg/client/config"
2526
"sigs.k8s.io/testing_frameworks/integration"
2627
)
2728

@@ -70,38 +71,59 @@ type Environment struct {
7071

7172
// CRDDirectoryPaths is a list of paths containing CRD yaml or json configs.
7273
CRDDirectoryPaths []string
74+
75+
// UseExisting indicates that this environments should use an
76+
// existing kubeconfig, instead of trying to stand up a new control plane.
77+
// This is useful in cases that need aggregated API servers and the like.
78+
UseExistingCluster bool
7379
}
7480

7581
// Stop stops a running server
7682
func (te *Environment) Stop() error {
83+
if te.UseExistingCluster {
84+
return nil
85+
}
7786
return te.ControlPlane.Stop()
7887
}
7988

8089
// Start starts a local Kubernetes server and updates te.ApiserverPort with the port it is listening on
8190
func (te *Environment) Start() (*rest.Config, error) {
82-
te.ControlPlane = integration.ControlPlane{}
83-
te.ControlPlane.APIServer = &integration.APIServer{Args: defaultKubeAPIServerFlags}
84-
if os.Getenv(envKubeAPIServerBin) == "" {
85-
te.ControlPlane.APIServer.Path = defaultAssetPath("kube-apiserver")
86-
}
87-
if os.Getenv(envEtcdBin) == "" {
88-
te.ControlPlane.Etcd = &integration.Etcd{Path: defaultAssetPath("etcd")}
89-
}
90-
if os.Getenv(envKubectlBin) == "" {
91-
// we can't just set the path manually (it's behind a function), so set the environment variable instead
92-
if err := os.Setenv(envKubectlBin, defaultAssetPath("kubectl")); err != nil {
93-
return nil, err
91+
if te.UseExistingCluster {
92+
if te.Config == nil {
93+
// we want to allow people to pass in their own config, so
94+
// only load a config if it hasn't already been set.
95+
96+
var err error
97+
te.Config, err = config.GetConfig()
98+
if err != nil {
99+
return nil, err
100+
}
101+
}
102+
} else {
103+
te.ControlPlane = integration.ControlPlane{}
104+
te.ControlPlane.APIServer = &integration.APIServer{Args: defaultKubeAPIServerFlags}
105+
if os.Getenv(envKubeAPIServerBin) == "" {
106+
te.ControlPlane.APIServer.Path = defaultAssetPath("kube-apiserver")
107+
}
108+
if os.Getenv(envEtcdBin) == "" {
109+
te.ControlPlane.Etcd = &integration.Etcd{Path: defaultAssetPath("etcd")}
110+
}
111+
if os.Getenv(envKubectlBin) == "" {
112+
// we can't just set the path manually (it's behind a function), so set the environment variable instead
113+
if err := os.Setenv(envKubectlBin, defaultAssetPath("kubectl")); err != nil {
114+
return nil, err
115+
}
94116
}
95-
}
96117

97-
// Start the control plane - retry if it fails
98-
if err := te.ControlPlane.Start(); err != nil {
99-
return nil, err
100-
}
118+
// Start the control plane - retry if it fails
119+
if err := te.ControlPlane.Start(); err != nil {
120+
return nil, err
121+
}
101122

102-
// Create the *rest.Config for creating new clients
103-
te.Config = &rest.Config{
104-
Host: te.ControlPlane.APIURL().Host,
123+
// Create the *rest.Config for creating new clients
124+
te.Config = &rest.Config{
125+
Host: te.ControlPlane.APIURL().Host,
126+
}
105127
}
106128

107129
_, err := InstallCRDs(te.Config, CRDInstallOptions{

pkg/runtime/log/log_suite_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,10 @@ import (
2121

2222
. "github.com/onsi/ginkgo"
2323
. "github.com/onsi/gomega"
24-
"sigs.k8s.io/controller-runtime/pkg/envtest"
24+
"sigs.k8s.io/controller-runtime/pkg/envtest/printer"
2525
)
2626

2727
func TestSource(t *testing.T) {
2828
RegisterFailHandler(Fail)
29-
RunSpecsWithDefaultAndCustomReporters(t, "Runtime Log Suite", []Reporter{envtest.NewlineReporter{}})
29+
RunSpecsWithDefaultAndCustomReporters(t, "Runtime Log Suite", []Reporter{printer.NewlineReporter{}})
3030
}

0 commit comments

Comments
 (0)