@@ -87,9 +87,135 @@ var _ = Describe("kubebuilder", func() {
8787 GenerateV4 (kbc )
8888 Run (kbc )
8989 })
90+ It ("should generate a runnable project" +
91+ " with the Installer" , func () {
92+ GenerateV4 (kbc )
93+ RunWithInstaller (kbc )
94+ })
9095 })
9196})
9297
98+ // TODO: We need to clean up and refactor these tests.
99+ // Remove duplicates and ensure cohesion and encapsulation
100+ // For Example: Run should not have make generate and manifests it should be part of the build of the project
101+ // Also we can encapsulate the checks such as: checkManager, checkPrometheus, checkCertManager so that we can reuse them
102+ // when or not is required and create many scenarios.
103+
104+ // RunWithInstaller runs a set of e2e tests for a scaffolded project defined by a TestContext.
105+ func RunWithInstaller (kbc * utils.TestContext ) {
106+ var controllerPodName string
107+ var err error
108+
109+ By ("creating manager namespace" )
110+ err = kbc .CreateManagerNamespace ()
111+ ExpectWithOffset (1 , err ).NotTo (HaveOccurred ())
112+
113+ By ("updating the go.mod" )
114+ err = kbc .Tidy ()
115+ ExpectWithOffset (1 , err ).NotTo (HaveOccurred ())
116+
117+ By ("run make manifests" )
118+ err = kbc .Make ("manifests" )
119+ ExpectWithOffset (1 , err ).NotTo (HaveOccurred ())
120+
121+ By ("run make generate" )
122+ err = kbc .Make ("generate" )
123+ ExpectWithOffset (1 , err ).NotTo (HaveOccurred ())
124+
125+ By ("building the controller image" )
126+ err = kbc .Make ("docker-build" , "IMG=" + kbc .ImageName )
127+ ExpectWithOffset (1 , err ).NotTo (HaveOccurred ())
128+
129+ By ("loading the controller docker image into the kind cluster" )
130+ err = kbc .LoadImageToKindCluster ()
131+ ExpectWithOffset (1 , err ).NotTo (HaveOccurred ())
132+
133+ By ("building the installer" )
134+ err = kbc .Make ("build-installer" , "IMG=" + kbc .ImageName )
135+ ExpectWithOffset (1 , err ).NotTo (HaveOccurred ())
136+
137+ // NOTE: If you want to run the test against a GKE cluster, you will need to grant yourself permission.
138+ // Otherwise, you may see "... is forbidden: attempt to grant extra privileges"
139+ // $ kubectl create clusterrolebinding myname-cluster-admin-binding \
140+ // --clusterrole=cluster-admin [email protected] 141+ // https://cloud.google.com/kubernetes-engine/docs/how-to/role-based-access-control
142+ By ("deploying the controller-manager with the installer" )
143+
144+ _ , err = kbc .Kubectl .Apply (true , "-f" , "dist/install.yaml" )
145+ ExpectWithOffset (1 , err ).NotTo (HaveOccurred ())
146+
147+ By ("validating that the controller-manager pod is running as expected" )
148+ verifyControllerUp := func () error {
149+ // Get pod name
150+ podOutput , err := kbc .Kubectl .Get (
151+ true ,
152+ "pods" , "-l" , "control-plane=controller-manager" ,
153+ "-o" , "go-template={{ range .items }}{{ if not .metadata.deletionTimestamp }}{{ .metadata.name }}" +
154+ "{{ \" \\ n\" }}{{ end }}{{ end }}" )
155+ ExpectWithOffset (2 , err ).NotTo (HaveOccurred ())
156+ podNames := util .GetNonEmptyLines (podOutput )
157+ if len (podNames ) != 1 {
158+ return fmt .Errorf ("expect 1 controller pods running, but got %d" , len (podNames ))
159+ }
160+ controllerPodName = podNames [0 ]
161+ ExpectWithOffset (2 , controllerPodName ).Should (ContainSubstring ("controller-manager" ))
162+
163+ // Validate pod status
164+ status , err := kbc .Kubectl .Get (
165+ true ,
166+ "pods" , controllerPodName , "-o" , "jsonpath={.status.phase}" )
167+ ExpectWithOffset (2 , err ).NotTo (HaveOccurred ())
168+ if status != "Running" {
169+ return fmt .Errorf ("controller pod in %s status" , status )
170+ }
171+ return nil
172+ }
173+ defer func () {
174+ out , err := kbc .Kubectl .CommandInNamespace ("describe" , "all" )
175+ ExpectWithOffset (1 , err ).NotTo (HaveOccurred ())
176+ fmt .Fprintln (GinkgoWriter , out )
177+ }()
178+ EventuallyWithOffset (1 , verifyControllerUp , time .Minute , time .Second ).Should (Succeed ())
179+
180+ By ("granting permissions to access the metrics" )
181+ _ , err = kbc .Kubectl .Command (
182+ "create" , "clusterrolebinding" , fmt .Sprintf ("metrics-%s" , kbc .TestSuffix ),
183+ fmt .Sprintf ("--clusterrole=e2e-%s-metrics-reader" , kbc .TestSuffix ),
184+ fmt .Sprintf ("--serviceaccount=%s:%s" , kbc .Kubectl .Namespace , kbc .Kubectl .ServiceAccount ))
185+ ExpectWithOffset (1 , err ).NotTo (HaveOccurred ())
186+
187+ _ = curlMetrics (kbc )
188+
189+ By ("validating that cert-manager has provisioned the certificate Secret" )
190+ EventuallyWithOffset (1 , func () error {
191+ _ , err := kbc .Kubectl .Get (
192+ true ,
193+ "secrets" , "webhook-server-cert" )
194+ return err
195+ }, time .Minute , time .Second ).Should (Succeed ())
196+
197+ By ("validating that the Prometheus manager has provisioned the Service" )
198+ EventuallyWithOffset (1 , func () error {
199+ _ , err := kbc .Kubectl .Get (
200+ false ,
201+ "Service" , "prometheus-operator" )
202+ return err
203+ }, time .Minute , time .Second ).Should (Succeed ())
204+
205+ By ("validating that the ServiceMonitor for Prometheus is applied in the namespace" )
206+ _ , err = kbc .Kubectl .Get (
207+ true ,
208+ "ServiceMonitor" )
209+ ExpectWithOffset (1 , err ).NotTo (HaveOccurred ())
210+
211+ By ("validating that the created resource object gets reconciled in the controller" )
212+ metricsOutput := curlMetrics (kbc )
213+ ExpectWithOffset (1 , metricsOutput ).To (ContainSubstring (fmt .Sprintf (
214+ `controller_runtime_reconcile_total{controller="%s",result="success"} 1` ,
215+ strings .ToLower (kbc .Kind ),
216+ )))
217+ }
218+
93219// Run runs a set of e2e tests for a scaffolded project defined by a TestContext.
94220func Run (kbc * utils.TestContext ) {
95221 var controllerPodName string
0 commit comments