diff --git a/controllers/componentdetectionquery_controller.go b/controllers/componentdetectionquery_controller.go index 603bdc98e..4fd5b010f 100644 --- a/controllers/componentdetectionquery_controller.go +++ b/controllers/componentdetectionquery_controller.go @@ -26,6 +26,7 @@ import ( "time" "github.com/go-logr/logr" + "github.com/hashicorp/go-multierror" "github.com/prometheus/client_golang/prometheus" appstudiov1alpha1 "github.com/redhat-appstudio/application-api/api/v1alpha1" cdqanalysis "github.com/redhat-appstudio/application-service/cdq-analysis/pkg" @@ -172,6 +173,8 @@ func (r *ComponentDetectionQueryReconciler) Reconcile(ctx context.Context, req c var componentPortsMapReturned map[string][]int revision := source.Revision + // with annotation runCDQAnalysisLocal = true, would allow the CDQ controller to run the cdq-analysis go modoule + // it is being used for CDQ controller tests to test both k8s job and go module if r.RunKubernetesJob && !(componentDetectionQuery.Annotations["runCDQAnalysisLocal"] == "true") { // perfume cdq job that requires repo cloning and azlier analysis clientset, err := kubernetes.NewForConfig(r.Config) @@ -256,13 +259,41 @@ func (r *ComponentDetectionQueryReconciler) Reconcile(ctx context.Context, req c return ctrl.Result{}, nil } var errMapReturned map[string]string - json.Unmarshal(cm.BinaryData["devfilesMap"], &devfilesMapReturned) - json.Unmarshal(cm.BinaryData["dockerfileContextMap"], &dockerfileContextMapReturned) - json.Unmarshal(cm.BinaryData["devfilesURLMap"], &devfilesURLMapReturned) - json.Unmarshal(cm.BinaryData["componentPortsMap"], &componentPortsMapReturned) - json.Unmarshal(cm.BinaryData["revision"], &revision) - json.Unmarshal(cm.BinaryData["errorMap"], &errMapReturned) + var unmarshalErr error + err = json.Unmarshal(cm.BinaryData["devfilesMap"], &devfilesMapReturned) + if err != nil { + unmarshalErr = multierror.Append(unmarshalErr, fmt.Errorf("unmarshal devfilesMap: %v", err)) + } + err = json.Unmarshal(cm.BinaryData["dockerfileContextMap"], &dockerfileContextMapReturned) + if err != nil { + unmarshalErr = multierror.Append(unmarshalErr, fmt.Errorf("unmarshal dockerfileContextMap: %v", err)) + } + err = json.Unmarshal(cm.BinaryData["devfilesURLMap"], &devfilesURLMapReturned) + if err != nil { + unmarshalErr = multierror.Append(unmarshalErr, fmt.Errorf("unmarshal devfilesURLMap: %v", err)) + } + err = json.Unmarshal(cm.BinaryData["componentPortsMap"], &componentPortsMapReturned) + if err != nil { + unmarshalErr = multierror.Append(unmarshalErr, fmt.Errorf("unmarshal componentPortsMap: %v", err)) + } + err = json.Unmarshal(cm.BinaryData["revision"], &revision) + if err != nil { + unmarshalErr = multierror.Append(unmarshalErr, fmt.Errorf("unmarshal revision: %v", err)) + } + err = json.Unmarshal(cm.BinaryData["errorMap"], &errMapReturned) + if err != nil { + unmarshalErr = multierror.Append(unmarshalErr, fmt.Errorf("unmarshal errorMap: %v", err)) + } cleanupK8sResources(log, clientset, ctx, fmt.Sprintf("%s-job", req.Name), req.Name, req.Namespace) + + if unmarshalErr != nil { + // if a direct devfileURL is provided and errors out, we dont do an alizer detection + log.Error(unmarshalErr, fmt.Sprintf("Failed to unmarshal the returned result from CDQ configmap... %v", req.NamespacedName)) + err := fmt.Errorf("Failed to unmarshal the returned result from CDQ configmap... ") + r.SetCompleteConditionAndUpdateCR(ctx, req, &componentDetectionQuery, copiedCDQ, err) + return ctrl.Result{}, nil + } + if errMapReturned != nil && !reflect.DeepEqual(errMapReturned, map[string]string{}) { var retErr error // only 1 index in the error map diff --git a/controllers/start_test_env.go b/controllers/start_test_env.go index cbfe67bd5..995198ed6 100644 --- a/controllers/start_test_env.go +++ b/controllers/start_test_env.go @@ -18,6 +18,7 @@ package controllers import ( "context" "go/build" + "os" "path/filepath" spiapi "github.com/redhat-appstudio/service-provider-integration-operator/api/v1beta1" @@ -94,6 +95,12 @@ func SetupTestEnv() (client.Client, *envtest.Environment, context.Context, conte mockGhTokenClient := github.MockGitHubTokenClient{} + // Retrieve the option to specify a cdq-analysis image + cdqAnalysisImage := os.Getenv("CDQ_ANALYSIS_IMAGE") + if cdqAnalysisImage == "" { + cdqAnalysisImage = "quay.io/redhat-appstudio/cdq-analysis:next" + } + // To Do: Set up reconcilers for the other controllers err = (&ApplicationReconciler{ Client: k8sManager.GetClient(), @@ -124,7 +131,7 @@ func SetupTestEnv() (client.Client, *envtest.Environment, context.Context, conte AppFS: ioutils.NewMemoryFilesystem(), Config: cfg, RunKubernetesJob: true, - CdqAnalysisImage: "quay.io/redhat-appstudio/cdq-analysis:next", + CdqAnalysisImage: cdqAnalysisImage, }).SetupWithManager(ctx, k8sManager) gomega.Expect(err).ToNot(gomega.HaveOccurred())