From 9896d841734efb18495b90a54bc2c410aaf3d690 Mon Sep 17 00:00:00 2001 From: Stuart Douglas Date: Thu, 15 Jun 2023 11:59:24 +1000 Subject: [PATCH] bug: component detection with context Fixes RHTAPBUGS-418 --- controllers/componentdetectionquery_controller.go | 2 +- pkg/devfile/detect.go | 9 +++++---- pkg/devfile/devfile.go | 12 +++++++++++- 3 files changed, 17 insertions(+), 6 deletions(-) diff --git a/controllers/componentdetectionquery_controller.go b/controllers/componentdetectionquery_controller.go index 725b6b4b2..5b8ad8d94 100644 --- a/controllers/componentdetectionquery_controller.go +++ b/controllers/componentdetectionquery_controller.go @@ -284,7 +284,7 @@ func (r *ComponentDetectionQueryReconciler) Reconcile(ctx context.Context, req c if isMultiComponent { log.Info(fmt.Sprintf("Since this is a multi-component, attempt will be made to read only level 1 dir for devfiles... %v", req.NamespacedName)) - devfilesMap, devfilesURLMap, dockerfileContextMap, componentPortsMap, err = devfile.ScanRepo(log, r.AlizerClient, componentPath, r.DevfileRegistryURL, source) + devfilesMap, devfilesURLMap, dockerfileContextMap, componentPortsMap, err = devfile.ScanRepo(log, r.AlizerClient, source.Context, componentPath, r.DevfileRegistryURL, source) if err != nil { if _, ok := err.(*devfile.NoDevfileFound); !ok { log.Error(err, fmt.Sprintf("Unable to find devfile(s) in repo %s due to an error %s, exiting reconcile loop %v", source.URL, err.Error(), req.NamespacedName)) diff --git a/pkg/devfile/detect.go b/pkg/devfile/detect.go index d665d5318..598ec47ad 100644 --- a/pkg/devfile/detect.go +++ b/pkg/devfile/detect.go @@ -46,7 +46,7 @@ type AlizerClient struct { // Map 2 returns a context to the matched devfileURL from the github repository. If no devfile was present, then a link to a matching devfile in the devfile registry will be used instead. // Map 3 returns a context to the Dockerfile uri or a matched DockerfileURL from the devfile registry if no Dockerfile is present in the context // Map 4 returns a context to the list of ports that were detected by alizer in the source code, at that given context -func search(log logr.Logger, a Alizer, localpath string, devfileRegistryURL string, source appstudiov1alpha1.GitSource) (map[string][]byte, map[string]string, map[string]string, map[string][]int, error) { +func search(log logr.Logger, a Alizer, originalContext string, localpath string, devfileRegistryURL string, source appstudiov1alpha1.GitSource) (map[string][]byte, map[string]string, map[string]string, map[string][]int, error) { devfileMapFromRepo := make(map[string][]byte) devfilesURLMapFromRepo := make(map[string]string) @@ -63,7 +63,8 @@ func search(log logr.Logger, a Alizer, localpath string, devfileRegistryURL stri isDevfilePresent := false isDockerfilePresent := false curPath := path.Join(localpath, f.Name()) - context := f.Name() + dirName := f.Name() + context := originalContext + f.Name() files, err := ioutil.ReadDir(curPath) if err != nil { return nil, nil, nil, nil, err @@ -74,7 +75,7 @@ func search(log logr.Logger, a Alizer, localpath string, devfileRegistryURL stri /* #nosec G304 -- false positive, filename is not based on user input*/ devfilePath := path.Join(curPath, f.Name()) // Set the proper devfile URL for the detected devfile - updatedLink, err := UpdateGitLink(source.URL, source.Revision, path.Join(source.Context, path.Join(context, f.Name()))) + updatedLink, err := UpdateGitLink(source.URL, source.Revision, path.Join(source.Context, path.Join(dirName, f.Name()))) if err != nil { return nil, nil, nil, nil, err } @@ -105,7 +106,7 @@ func search(log logr.Logger, a Alizer, localpath string, devfileRegistryURL stri devfilePath := path.Join(hiddenDirPath, f.Name()) // Set the proper devfile URL for the detected devfile - updatedLink, err := UpdateGitLink(source.URL, source.Revision, path.Join(source.Context, path.Join(context, HiddenDevfileDir, f.Name()))) + updatedLink, err := UpdateGitLink(source.URL, source.Revision, path.Join(source.Context, path.Join(dirName, HiddenDevfileDir, f.Name()))) if err != nil { return nil, nil, nil, nil, err } diff --git a/pkg/devfile/devfile.go b/pkg/devfile/devfile.go index 8e56e8160..6166bdf8a 100644 --- a/pkg/devfile/devfile.go +++ b/pkg/devfile/devfile.go @@ -843,6 +843,8 @@ func DownloadDevfileAndDockerfile(url string) ([]byte, string, []byte, string) { } // ScanRepo attempts to read and return devfiles and Dockerfiles/Containerfiles from the local path upto the specified depth +// it also takes an originalContext parameter that represents the context path in the git repo +// this will be appended to the keys of all the returned maps // Iterate through each sub-folder under first level, and scan for component. (devfile, Dockerfile/Containerfile, then Alizer) // If no devfile(s) or Dockerfile(s)/Containerfile(s) are found in sub-folders of the root directory, then the Alizer tool is used to detect and match a devfile/Dockerfile from the devfile registry // ScanRepo returns 3 maps and an error: @@ -851,7 +853,15 @@ func DownloadDevfileAndDockerfile(url string) ([]byte, string, []byte, string) { // Map 3 returns a context to the Dockerfile uri or a matched DockerfileURL from the devfile registry if no Dockerfile/Containerfile is present in the context // Map 4 returns a context to the list of ports that were detected by alizer in the source code, at that given context func ScanRepo(log logr.Logger, a Alizer, localpath string, devfileRegistryURL string, source appstudiov1alpha1.GitSource) (map[string][]byte, map[string]string, map[string]string, map[string][]int, error) { - return search(log, a, localpath, devfileRegistryURL, source) + originalContext := source.Context + if originalContext != "" { + if originalContext == "./" { + originalContext = "" + } else if !strings.HasSuffix(originalContext, "/") { + originalContext += "/" + } + } + return search(log, a, originalContext, localpath, devfileRegistryURL, source) } // UpdateLocalDockerfileURItoAbsolute takes in a Devfile, and a DockefileURL, and returns back a Devfile with any local URIs to the Dockerfile updates to be absolute