@@ -27,11 +27,6 @@ import (
27
27
. "github.com/onsi/ginkgo" //nolint:golint
28
28
)
29
29
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
-
35
30
// TestContext specified to run e2e tests
36
31
type TestContext struct {
37
32
* CmdContext
@@ -42,8 +37,9 @@ type TestContext struct {
42
37
Kind string
43
38
Resources string
44
39
ImageName string
45
- Kubectl * Kubectl
46
40
BinaryName string
41
+ Kubectl * Kubectl
42
+ K8sVersion * KubernetesVersion
47
43
}
48
44
49
45
// NewTestContext init with a random suffix for test TestContext stuff,
@@ -54,30 +50,36 @@ func NewTestContext(binaryName string, env ...string) (*TestContext, error) {
54
50
return nil , err
55
51
}
56
52
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 ()
59
63
if err != nil {
60
64
return nil , err
61
65
}
62
66
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
66
70
}
67
71
68
72
return & TestContext {
69
73
TestSuffix : testSuffix ,
70
74
Domain : "example.com" + testSuffix ,
71
- Group : testGroup ,
75
+ Group : "bar" + testSuffix ,
72
76
Version : "v1alpha1" ,
73
77
Kind : "Foo" + testSuffix ,
74
78
Resources : "foo" + testSuffix + "s" ,
75
79
ImageName : "e2e-test/controller-manager:" + testSuffix ,
76
80
CmdContext : cc ,
77
- Kubectl : & Kubectl {
78
- Namespace : fmt .Sprintf ("e2e-%s-system" , testSuffix ),
79
- CmdContext : cc ,
80
- },
81
+ Kubectl : kubectl ,
82
+ K8sVersion : & k8sVersion ,
81
83
BinaryName : binaryName ,
82
84
}, nil
83
85
}
@@ -98,12 +100,33 @@ func (t *TestContext) Prepare() error {
98
100
return os .MkdirAll (t .Dir , 0755 )
99
101
}
100
102
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 )
105
122
}
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 )
107
130
if _ , err := t .Kubectl .Apply (false , "-f" , url , "--validate=false" ); err != nil {
108
131
return err
109
132
}
@@ -117,6 +140,24 @@ func (t *TestContext) InstallCertManager() error {
117
140
return err
118
141
}
119
142
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
+
120
161
// InstallPrometheusOperManager installs the prometheus manager bundle.
121
162
func (t * TestContext ) InstallPrometheusOperManager () error {
122
163
url := fmt .Sprintf (prometheusOperatorURL , prometheusOperatorVersion )
@@ -132,18 +173,6 @@ func (t *TestContext) UninstallPrometheusOperManager() {
132
173
}
133
174
}
134
175
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
-
147
176
// CleanupManifests is a helper func to run kustomize build and pipe the output to kubectl delete -f -
148
177
func (t * TestContext ) CleanupManifests (dir string ) {
149
178
cmd := exec .Command ("kustomize" , "build" , dir )
@@ -231,7 +260,7 @@ func (cc *CmdContext) Run(cmd *exec.Cmd) ([]byte, error) {
231
260
fmt .Fprintf (GinkgoWriter , "running: %s\n " , command )
232
261
output , err := cmd .CombinedOutput ()
233
262
if 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 ))
235
264
}
236
265
237
266
return output , nil
0 commit comments