@@ -27,11 +27,6 @@ import (
2727. "github.com/onsi/ginkgo" //nolint:golint
2828)
2929
30- const certmanagerVersion = "v0.11.0"
31- const prometheusOperatorVersion = "0.33"
32- const certmanagerURL = "https://github.com/jetstack/cert-manager/releases/download/%s/cert-manager.yaml"
33- const prometheusOperatorURL = "https://raw.githubusercontent.com/coreos/prometheus-operator/release-%s/bundle.yaml"
34-
3530// TestContext specified to run e2e tests
3631type TestContext struct {
3732* CmdContext
@@ -42,8 +37,9 @@ type TestContext struct {
4237Kind string
4338Resources string
4439ImageName string
45- Kubectl * Kubectl
4640BinaryName string
41+ Kubectl * Kubectl
42+ K8sVersion * KubernetesVersion
4743}
4844
4945// NewTestContext init with a random suffix for test TestContext stuff,
@@ -54,30 +50,36 @@ func NewTestContext(binaryName string, env ...string) (*TestContext, error) {
5450return nil , err
5551}
5652
57- testGroup := "bar" + testSuffix
58- path , err := filepath .Abs ("e2e-" + testSuffix )
53+ cc := & CmdContext {
54+ Env : env ,
55+ }
56+
57+ // Use kubectl to get Kubernetes client and cluster version.
58+ kubectl := & Kubectl {
59+ Namespace : fmt .Sprintf ("e2e-%s-system" , testSuffix ),
60+ CmdContext : cc ,
61+ }
62+ k8sVersion , err := kubectl .Version ()
5963if err != nil {
6064return nil , err
6165}
6266
63- cc := & CmdContext {
64- Env : env ,
65- Dir : path ,
67+ // Set CmdContext.Dir after running Kubectl.Version() because dir does not exist yet.
68+ if cc . Dir , err = filepath . Abs ( "e2e-" + testSuffix ); err != nil {
69+ return nil , err
6670}
6771
6872return & TestContext {
6973TestSuffix : testSuffix ,
7074Domain : "example.com" + testSuffix ,
71- Group : testGroup ,
75+ Group : "bar" + testSuffix ,
7276Version : "v1alpha1" ,
7377Kind : "Foo" + testSuffix ,
7478Resources : "foo" + testSuffix + "s" ,
7579ImageName : "e2e-test/controller-manager:" + testSuffix ,
7680CmdContext : cc ,
77- Kubectl : & Kubectl {
78- Namespace : fmt .Sprintf ("e2e-%s-system" , testSuffix ),
79- CmdContext : cc ,
80- },
81+ Kubectl : kubectl ,
82+ K8sVersion : & k8sVersion ,
8183BinaryName : binaryName ,
8284}, nil
8385}
@@ -98,12 +100,33 @@ func (t *TestContext) Prepare() error {
98100return os .MkdirAll (t .Dir , 0755 )
99101}
100102
101- // InstallCertManager installs the cert manager bundle.
102- func (t * TestContext ) InstallCertManager () error {
103- if _ , err := t .Kubectl .Command ("create" , "namespace" , "cert-manager" ); err != nil {
104- return err
103+ const (
104+ certmanagerVersionWithv1beta2CRs = "v0.11.0"
105+ certmanagerVersion = "v1.0.4"
106+
107+ certmanagerURLTmplLegacy = "https://github.com/jetstack/cert-manager/releases/download/%s/cert-manager-legacy.yaml"
108+ certmanagerURLTmpl = "https://github.com/jetstack/cert-manager/releases/download/%s/cert-manager.yaml"
109+ )
110+
111+ // makeCertManagerURL returns a kubectl-able URL for the cert-manager bundle.
112+ func (t * TestContext ) makeCertManagerURL (hasv1beta1CRs bool ) string {
113+ // Return a URL for the manifest bundle with v1beta1 CRs.
114+ if hasv1beta1CRs {
115+ return fmt .Sprintf (certmanagerURLTmpl , certmanagerVersionWithv1beta2CRs )
116+ }
117+
118+ // Determine which URL to use for a manifest bundle with v1 CRs.
119+ // The most up-to-date bundle uses v1 CRDs, which were introduced in k8s v1.16.
120+ if ver := t .K8sVersion .ServerVersion ; ver .GetMajorInt () <= 1 && ver .GetMinorInt () < 16 {
121+ return fmt .Sprintf (certmanagerURLTmplLegacy , certmanagerVersion )
105122}
106- url := fmt .Sprintf (certmanagerURL , certmanagerVersion )
123+ return fmt .Sprintf (certmanagerURLTmpl , certmanagerVersion )
124+ }
125+
126+ // InstallCertManager installs the cert manager bundle. If hasv1beta1CRs is true,
127+ // the legacy version (which uses v1alpha2 CRs) is installed.
128+ func (t * TestContext ) InstallCertManager (hasv1beta1CRs bool ) error {
129+ url := t .makeCertManagerURL (hasv1beta1CRs )
107130if _ , err := t .Kubectl .Apply (false , "-f" , url , "--validate=false" ); err != nil {
108131return err
109132}
@@ -117,6 +140,24 @@ func (t *TestContext) InstallCertManager() error {
117140return err
118141}
119142
143+ // UninstallCertManager uninstalls the cert manager bundle. If hasv1beta1CRs is true,
144+ // the legacy version (which uses v1alpha2 CRs) is installed.
145+ func (t * TestContext ) UninstallCertManager (hasv1beta1CRs bool ) {
146+ url := t .makeCertManagerURL (hasv1beta1CRs )
147+ if _ , err := t .Kubectl .Delete (false , "-f" , url ); err != nil {
148+ fmt .Fprintf (GinkgoWriter ,
149+ "warning: error when running kubectl delete during cleaning up cert manager: %v\n " , err )
150+ }
151+ if _ , err := t .Kubectl .Delete (false , "namespace" , "cert-manager" ); err != nil {
152+ fmt .Fprintf (GinkgoWriter , "warning: error when cleaning up the cert manager namespace: %v\n " , err )
153+ }
154+ }
155+
156+ const (
157+ prometheusOperatorVersion = "0.33"
158+ prometheusOperatorURL = "https://raw.githubusercontent.com/coreos/prometheus-operator/release-%s/bundle.yaml"
159+ )
160+
120161// InstallPrometheusOperManager installs the prometheus manager bundle.
121162func (t * TestContext ) InstallPrometheusOperManager () error {
122163url := fmt .Sprintf (prometheusOperatorURL , prometheusOperatorVersion )
@@ -132,18 +173,6 @@ func (t *TestContext) UninstallPrometheusOperManager() {
132173}
133174}
134175
135- // UninstallCertManager uninstalls the cert manager bundle.
136- func (t * TestContext ) UninstallCertManager () {
137- url := fmt .Sprintf (certmanagerURL , certmanagerVersion )
138- if _ , err := t .Kubectl .Delete (false , "-f" , url ); err != nil {
139- fmt .Fprintf (GinkgoWriter ,
140- "warning: error when running kubectl delete during cleaning up cert manager: %v\n " , err )
141- }
142- if _ , err := t .Kubectl .Delete (false , "namespace" , "cert-manager" ); err != nil {
143- fmt .Fprintf (GinkgoWriter , "warning: error when cleaning up the cert manager namespace: %v\n " , err )
144- }
145- }
146-
147176// CleanupManifests is a helper func to run kustomize build and pipe the output to kubectl delete -f -
148177func (t * TestContext ) CleanupManifests (dir string ) {
149178cmd := exec .Command ("kustomize" , "build" , dir )
@@ -231,7 +260,7 @@ func (cc *CmdContext) Run(cmd *exec.Cmd) ([]byte, error) {
231260fmt .Fprintf (GinkgoWriter , "running: %s\n " , command )
232261output , err := cmd .CombinedOutput ()
233262if err != nil {
234- return output , fmt .Errorf ("%s failed with error: % s" , command , string (output ))
263+ return output , fmt .Errorf ("%s failed with error: (%v) % s" , command , err , string (output ))
235264}
236265
237266return output , nil
0 commit comments