github.com/redhat-appstudio/e2e-tests@v0.0.0-20240520140907-9709f6f59323/pkg/clients/has/componentdetectionqueries.go (about) 1 package has 2 3 import ( 4 "context" 5 "fmt" 6 "time" 7 8 appservice "github.com/redhat-appstudio/application-api/api/v1alpha1" 9 "github.com/redhat-appstudio/e2e-tests/pkg/logs" 10 "github.com/redhat-appstudio/e2e-tests/pkg/utils" 11 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 12 "k8s.io/apimachinery/pkg/types" 13 rclient "sigs.k8s.io/controller-runtime/pkg/client" 14 ) 15 16 // GetComponentDetectionQuery return the status from the ComponentDetectionQuery Custom Resource object 17 func (h *HasController) GetComponentDetectionQuery(name, namespace string) (*appservice.ComponentDetectionQuery, error) { 18 componentDetectionQuery := &appservice.ComponentDetectionQuery{ 19 Spec: appservice.ComponentDetectionQuerySpec{}, 20 } 21 22 if err := h.KubeRest().Get(context.Background(), types.NamespacedName{Name: name, Namespace: namespace}, componentDetectionQuery); err != nil { 23 return nil, err 24 } 25 26 return componentDetectionQuery, nil 27 } 28 29 // CreateComponentDetectionQuery create a has componentdetectionquery from a given name, namespace, and git source 30 func (h *HasController) CreateComponentDetectionQuery(name string, namespace string, gitSourceURL string, gitSourceRevision string, gitSourceContext string, secret string, isMultiComponent bool) (*appservice.ComponentDetectionQuery, error) { 31 return h.CreateComponentDetectionQueryWithTimeout(name, namespace, gitSourceURL, gitSourceRevision, gitSourceContext, secret, isMultiComponent, 6*time.Minute) 32 } 33 34 // CreateComponentDetectionQueryWithTimeout create a has componentdetectionquery from a given name, namespace, and git source and waits for it to be read 35 func (h *HasController) CreateComponentDetectionQueryWithTimeout(name string, namespace string, gitSourceURL string, gitSourceRevision string, gitSourceContext string, secret string, isMultiComponent bool, timeout time.Duration) (*appservice.ComponentDetectionQuery, error) { 36 componentDetectionQuery := &appservice.ComponentDetectionQuery{ 37 ObjectMeta: metav1.ObjectMeta{ 38 Name: name, 39 Namespace: namespace, 40 }, 41 Spec: appservice.ComponentDetectionQuerySpec{ 42 GitSource: appservice.GitSource{ 43 URL: gitSourceURL, 44 Revision: gitSourceRevision, 45 Context: gitSourceContext, 46 }, 47 Secret: secret, 48 GenerateComponentName: true, 49 }, 50 } 51 52 ctx, cancel := context.WithTimeout(context.Background(), time.Minute*1) 53 defer cancel() 54 if err := h.KubeRest().Create(ctx, componentDetectionQuery); err != nil { 55 return nil, err 56 } 57 58 err := utils.WaitUntil(func() (done bool, err error) { 59 componentDetectionQuery, err = h.GetComponentDetectionQuery(componentDetectionQuery.Name, componentDetectionQuery.Namespace) 60 if err != nil { 61 return false, err 62 } 63 for _, condition := range componentDetectionQuery.Status.Conditions { 64 if condition.Type == "Completed" && len(componentDetectionQuery.Status.ComponentDetected) > 0 { 65 return true, nil 66 } 67 } 68 return false, nil 69 }, timeout) 70 71 if err != nil { 72 return nil, fmt.Errorf("error waiting for cdq to be ready: %v", err) 73 } 74 75 return componentDetectionQuery, nil 76 } 77 78 // DeleteAllComponentDetectionQueriesInASpecificNamespace removes all CDQs CRs from a specific namespace. Useful when creating a lot of resources and want to remove all of them 79 func (h *HasController) DeleteAllComponentDetectionQueriesInASpecificNamespace(namespace string, timeout time.Duration) error { 80 if err := h.KubeRest().DeleteAllOf(context.Background(), &appservice.ComponentDetectionQuery{}, rclient.InNamespace(namespace)); err != nil { 81 return fmt.Errorf("error deleting component detection queries from the namespace %s: %+v", namespace, err) 82 } 83 84 return utils.WaitUntil(func() (done bool, err error) { 85 componentDetectionQueriesList, err := h.ListAllComponentDetectionQueries(namespace) 86 if err != nil { 87 return false, nil 88 } 89 return len(componentDetectionQueriesList.Items) == 0, nil 90 }, timeout) 91 } 92 93 // ListAllComponentDetectionQueries returns a list of all ComponentDetectionQueries in a given namespace. 94 func (h *HasController) ListAllComponentDetectionQueries(namespace string) (*appservice.ComponentDetectionQueryList, error) { 95 componentDetectionQueryList := &appservice.ComponentDetectionQueryList{} 96 err := h.KubeRest().List(context.Background(), componentDetectionQueryList, &rclient.ListOptions{Namespace: namespace}) 97 return componentDetectionQueryList, err 98 } 99 100 // StoreComponentDetectionQuery stores a given ComponentDetectionQuery as an artifact. 101 func (h *HasController) StoreComponentDetectionQuery(ComponentDetectionQuery *appservice.ComponentDetectionQuery) error { 102 return logs.StoreResourceYaml(ComponentDetectionQuery, "componentDetectionQuery-"+ComponentDetectionQuery.Name) 103 } 104 105 // StoreAllComponentDetectionQueries stores all ComponentDetectionQueries in a given namespace. 106 func (h *HasController) StoreAllComponentDetectionQueries(namespace string) error { 107 componentDetectionQueryList, err := h.ListAllComponentDetectionQueries(namespace) 108 if err != nil { 109 return err 110 } 111 112 for _, componentDetectionQuery := range componentDetectionQueryList.Items { 113 if err := h.StoreComponentDetectionQuery(&componentDetectionQuery); err != nil { 114 return err 115 } 116 } 117 return nil 118 } 119 120 // UpdateComponent updates a component 121 func (h *HasController) UpdateComponent(component *appservice.Component) error { 122 err := h.KubeRest().Update(context.Background(), component, &rclient.UpdateOptions{}) 123 124 if err != nil { 125 return err 126 } 127 return nil 128 }