@@ -3,17 +3,25 @@ package e2e
33import (
44 "context"
55 "fmt"
6+ "io/ioutil"
7+ "os"
8+ "strings"
69 "time"
710
811 . "github.com/onsi/ginkgo/v2"
912 . "github.com/onsi/gomega"
1013 catalogd "github.com/operator-framework/catalogd/pkg/apis/core/v1beta1"
1114 operatorv1alpha1 "github.com/operator-framework/operator-controller/api/v1alpha1"
1215 rukpakv1alpha1 "github.com/operator-framework/rukpak/api/v1alpha1"
16+ "gopkg.in/yaml.v2"
17+ v1 "k8s.io/api/core/v1"
1318 apimeta "k8s.io/apimachinery/pkg/api/meta"
1419 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1520 "k8s.io/apimachinery/pkg/types"
1621 "k8s.io/apimachinery/pkg/util/rand"
22+ kubeclient "k8s.io/client-go/kubernetes"
23+ ctrl "sigs.k8s.io/controller-runtime"
24+ "sigs.k8s.io/controller-runtime/pkg/client"
1725)
1826
1927const (
@@ -99,10 +107,137 @@ var _ = Describe("Operator Install", func() {
99107
100108 })
101109 AfterEach (func () {
110+
111+ // get all the artifacts from the test run and save them to the artifact path
112+ getArtifactsOutput ()
113+
102114 err := c .Delete (ctx , operatorCatalog )
103115 Expect (err ).ToNot (HaveOccurred ())
104116 err = c .Delete (ctx , operator )
105117 Expect (err ).ToNot (HaveOccurred ())
106118 })
107119 })
108120})
121+
122+ // getArtifactsOutput gets all the artifacts from the test run and saves them to the artifact path.
123+ // right now it will save:
124+ // - operators
125+ // - pods logs
126+ // - deployments
127+ // - bundle
128+ // - bundledeployments
129+ // - catalogsources
130+
131+ func getArtifactsOutput () {
132+ ctx := context .Background ()
133+ cfg = ctrl .GetConfigOrDie ()
134+ kubeClient , err := kubeclient .NewForConfig (cfg )
135+ Expect (err ).To (Not (HaveOccurred ()))
136+
137+ artifactPath := "/tmp/artifacts/" + "operator-controller-e2e" + "/" + fmt .Sprint (time .Now ().UnixNano ()) + "/"
138+
139+ // Create the full artifact path
140+ err = os .MkdirAll (artifactPath , 0755 )
141+ Expect (err ).To (Not (HaveOccurred ()))
142+
143+ // Get all namespaces
144+ namespaces , err := kubeClient .CoreV1 ().Namespaces ().List (ctx , metav1.ListOptions {})
145+ Expect (err ).To (Not (HaveOccurred ()))
146+
147+ // get all operators save them to the artifact path.
148+ operators := operatorv1alpha1.OperatorList {}
149+ err = c .List (ctx , & operators , client .InNamespace ("" ))
150+ Expect (err ).To (Not (HaveOccurred ()))
151+ for _ , operator := range operators .Items {
152+ // Save operator to artifact path
153+ operatorYaml , err := yaml .Marshal (operator )
154+ Expect (err ).To (Not (HaveOccurred ()))
155+ err = ioutil .WriteFile (artifactPath + operator .Name + "-operator.yaml" , operatorYaml , 0644 )
156+ Expect (err ).To (Not (HaveOccurred ()))
157+ }
158+
159+ // get all catalogsources save them to the artifact path.
160+ catalogsources := catalogd.CatalogSourceList {}
161+ err = c .List (ctx , & catalogsources , client .InNamespace ("" ))
162+ Expect (err ).To (Not (HaveOccurred ()))
163+ for _ , catalogsource := range catalogsources .Items {
164+ // Save catalogsource to artifact path
165+ catalogsourceYaml , err := yaml .Marshal (catalogsource )
166+ Expect (err ).To (Not (HaveOccurred ()))
167+ err = ioutil .WriteFile (artifactPath + catalogsource .Name + "-catalogsource.yaml" , catalogsourceYaml , 0644 )
168+ Expect (err ).To (Not (HaveOccurred ()))
169+ }
170+
171+ // Get all Bundles in the namespace and save them to the artifact path.
172+ bundles := rukpakv1alpha1.BundleList {}
173+ err = c .List (ctx , & bundles , client .InNamespace ("" ))
174+ Expect (err ).To (Not (HaveOccurred ()))
175+ for _ , bundle := range bundles .Items {
176+ // Save bundle to artifact path
177+ bundleYaml , err := yaml .Marshal (bundle )
178+ Expect (err ).To (Not (HaveOccurred ()))
179+ err = ioutil .WriteFile (artifactPath + bundle .Name + "-bundle.yaml" , bundleYaml , 0644 )
180+ Expect (err ).To (Not (HaveOccurred ()))
181+ }
182+
183+ // Get all BundleDeployments in the namespace and save them to the artifact path.
184+ bundleDeployments := rukpakv1alpha1.BundleDeploymentList {}
185+ err = c .List (ctx , & bundleDeployments , client .InNamespace ("" ))
186+ Expect (err ).To (Not (HaveOccurred ()))
187+ for _ , bundleDeployment := range bundleDeployments .Items {
188+ // Save bundleDeployment to artifact path
189+ bundleDeploymentYaml , err := yaml .Marshal (bundleDeployment )
190+ Expect (err ).To (Not (HaveOccurred ()))
191+ err = ioutil .WriteFile (artifactPath + bundleDeployment .Name + "-bundleDeployment.yaml" , bundleDeploymentYaml , 0644 )
192+ Expect (err ).To (Not (HaveOccurred ()))
193+ }
194+
195+ for _ , namespace := range namespaces .Items {
196+ // let's ignore kube-* namespaces.
197+ if strings .Contains (namespace .Name , "kube-" ) {
198+ continue
199+ }
200+
201+ namespaceArtifactPath := artifactPath + namespace .Name + "/"
202+ err = os .Mkdir (namespaceArtifactPath , 0755 )
203+ Expect (err ).To (Not (HaveOccurred ()))
204+
205+ // get all deployments in the namespace and save them to the artifact path.
206+ deployments , err := kubeClient .AppsV1 ().Deployments (namespace .Name ).List (ctx , metav1.ListOptions {})
207+ Expect (err ).To (Not (HaveOccurred ()))
208+ for _ , deployment := range deployments .Items {
209+ // Save deployment to artifact path
210+ deploymentYaml , err := yaml .Marshal (deployment )
211+ Expect (err ).To (Not (HaveOccurred ()))
212+ err = ioutil .WriteFile (namespaceArtifactPath + deployment .Name + "-deployment.yaml" , deploymentYaml , 0644 )
213+ Expect (err ).To (Not (HaveOccurred ()))
214+ }
215+
216+ // Get logs from all pods in all namespaces
217+ pods , err := kubeClient .CoreV1 ().Pods (namespace .Name ).List (ctx , metav1.ListOptions {})
218+ Expect (err ).To (Not (HaveOccurred ()))
219+ for _ , pod := range pods .Items {
220+ if pod .Status .Phase != v1 .PodRunning {
221+ continue
222+ }
223+ for _ , container := range pod .Spec .Containers {
224+ logs , err := kubeClient .CoreV1 ().Pods (namespace .Name ).GetLogs (pod .Name , & v1.PodLogOptions {Container : container .Name }).Stream (ctx )
225+ Expect (err ).To (Not (HaveOccurred ()))
226+ defer logs .Close ()
227+
228+ // Read logs
229+ buf := make ([]byte , 1024 )
230+ for {
231+ _ , err := logs .Read (buf )
232+ if err != nil {
233+ break
234+ }
235+ }
236+
237+ // Save logs to artifact path
238+ err = ioutil .WriteFile (namespaceArtifactPath + pod .Name + "-" + container .Name + "-logs.txt" , buf , 0644 )
239+ Expect (err ).To (Not (HaveOccurred ()))
240+ }
241+ }
242+ }
243+ }
0 commit comments