From 0cb1ab56c918ae6a5d176ab8c7a521389d0dcd23 Mon Sep 17 00:00:00 2001 From: Emily McMullan Date: Sat, 20 Apr 2024 18:28:42 -0400 Subject: [PATCH] clean resources using routines Signed-off-by: Emily McMullan --- cmd/analyze.go | 92 ++++++++++++++++++++++++++----------------------- cmd/settings.go | 8 +++++ 2 files changed, 56 insertions(+), 44 deletions(-) diff --git a/cmd/analyze.go b/cmd/analyze.go index ef0a620..7918e15 100644 --- a/cmd/analyze.go +++ b/cmd/analyze.go @@ -12,6 +12,7 @@ import ( "os/exec" "path" "runtime" + "sync" "path/filepath" "sort" @@ -48,12 +49,9 @@ var ( ProviderSettingsMountPath = path.Join(ConfigMountPath, "settings.json") ) -// supported provider images const ( - javaExternalProviderImg = "quay.io/konveyor/java-external-provider:latest" - genericProviderImg = "quay.io/konveyor/generic-external-provider:latest" - dotNetProviderImg = "quay.io/konveyor/dotnet-external-provider:latest" - yqProviderImg = "quay.io/konveyor/yq-external-provider:latest" + javaProvider = "java" + goProvider = "go" ) // kantra analyze flags @@ -128,14 +126,19 @@ func NewAnalyzeCmd(log logr.Logger) *cobra.Command { // defer cleaning created resources here instead of PostRun // if Run returns an error, PostRun does not run defer func() { - if err := errors.Join(analyzeCmd.RmAnalysisResources(cmd.Context())); err != nil { - log.Error(err, "failed to remove analysis resource") - } - }() - defer func() { - if err := analyzeCmd.Clean(cmd.Context()); err != nil { - log.Error(err, "failed to clean temporary container resources") - } + var wg sync.WaitGroup + wg.Add(2) + go func() { + if err := analyzeCmd.CleanTempDirs(cmd.Context(), &wg); err != nil { + log.Error(err, "failed to clean temporary directories") + } + }() + go func() { + if err := analyzeCmd.CleanAnalysisResources(cmd.Context(), &wg); err != nil { + log.Error(err, "failed to clean temporary container resources") + } + }() + wg.Wait() }() if analyzeCmd.listSources || analyzeCmd.listTargets { err := analyzeCmd.ListLabels(cmd.Context()) @@ -159,9 +162,10 @@ func NewAnalyzeCmd(log logr.Logger) *cobra.Command { for _, c := range components { log.Info("Got component", "component language", c.Languages, "path", c.Path) for _, l := range c.Languages { - foundProviders = append(foundProviders, l.Name) + foundProviders = append(foundProviders, strings.ToLower(l.Name)) } } + fmt.Printf("FOUND PROVIDERS!! %v \n", foundProviders) containerNetworkName, err := analyzeCmd.createContainerNetwork() if err != nil { log.Error(err, "failed to create container network") @@ -461,10 +465,10 @@ func (a *analyzeCommand) getConfigVolumes(providers []string, ports map[string]i var foundJava bool var foundGolang bool for _, p := range providers { - if p == "Java" { + if p == javaProvider { foundJava = true } - if p == "Go" { + if p == goProvider { foundGolang = true } } @@ -478,14 +482,14 @@ func (a *analyzeCommand) getConfigVolumes(providers []string, ports map[string]i } javaConfig := provider.Config{ - Name: "java", - Address: fmt.Sprintf("0.0.0.0:%v", ports["java"]), + Name: javaProvider, + Address: fmt.Sprintf("0.0.0.0:%v", ports[javaProvider]), InitConfig: []provider.InitConfig{ { Location: SourceMountPath, AnalysisMode: provider.AnalysisMode(a.mode), ProviderSpecificConfig: map[string]interface{}{ - "lspServerName": "java", + "lspServerName": javaProvider, "bundles": JavaBundlesLocation, "depOpenSourceLabelsFile": "/usr/local/etc/maven.default.index", provider.LspServerPathConfigKey: "/jdtls/bin/jdtls", @@ -506,8 +510,8 @@ func (a *analyzeCommand) getConfigVolumes(providers []string, ports map[string]i } goConfig := provider.Config{ - Name: "go", - Address: fmt.Sprintf("0.0.0.0:%v", ports["go"]), + Name: goProvider, + Address: fmt.Sprintf("0.0.0.0:%v", ports[goProvider]), InitConfig: []provider.InitConfig{ { AnalysisMode: provider.FullAnalysisMode, @@ -784,12 +788,12 @@ func (a *analyzeCommand) RunProviders(ctx context.Context, networkName string, v // this will make more sense when we have more than 2 supported providers var providerImage string switch providers[0] { - case "Java": + case javaProvider: providerImage = javaExternalProviderImg - providerPorts["java"] = port - case "Go": + providerPorts[javaProvider] = port + case goProvider: providerImage = genericProviderImg - providerPorts["go"] = port + providerPorts[goProvider] = port default: } args := []string{fmt.Sprintf("--port=%v", port)} @@ -895,7 +899,7 @@ func (a *analyzeCommand) RunAnalysis(ctx context.Context, xmlOutputDir string, v fmt.Sprintf("--context-lines=%d", 100), } // TODO update for running multiple apps - if providers[0] != "Java" { + if providers[0] != javaProvider { a.enableDefaultRulesets = false } if a.enableDefaultRulesets { @@ -1072,20 +1076,6 @@ func (a *analyzeCommand) GenerateStaticReport(ctx context.Context) error { return nil } -func (a *analyzeCommand) Clean(ctx context.Context) error { - if !a.cleanup { - return nil - } - for _, path := range a.tempDirs { - err := os.RemoveAll(path) - if err != nil { - a.log.V(1).Error(err, "failed to delete temporary dir", "dir", path) - continue - } - } - return nil -} - func (a *analyzeCommand) getLabelSelector() string { if a.labelSelector != "" { return a.labelSelector @@ -1243,24 +1233,38 @@ func (a *analyzeCommand) ConvertXML(ctx context.Context) (string, error) { return tempOutputDir, nil } -func (a *analyzeCommand) RmAnalysisResources(ctx context.Context) error { +func (a *analyzeCommand) CleanTempDirs(ctx context.Context, wg *sync.WaitGroup) error { + defer wg.Done() + if !a.cleanup { + return nil + } + a.log.V(1).Info("removing temp dirs") + for _, path := range a.tempDirs { + err := os.RemoveAll(path) + if err != nil { + a.log.V(1).Error(err, "failed to delete temporary dir", "dir", path) + continue + } + } + return nil +} + +func (a *analyzeCommand) CleanAnalysisResources(ctx context.Context, wg *sync.WaitGroup) error { + defer wg.Done() if len(a.providerContainerNames) == 0 { return nil } err := a.RmProviderContainers(ctx) if err != nil { a.log.Error(err, "failed to remove provider container") - return err } err = a.RmNetwork(ctx) if err != nil { a.log.Error(err, "failed to remove network", "network", a.networkName) - return err } err = a.RmVolumes(ctx) if err != nil { a.log.Error(err, "failed to remove volume", "volume", a.volumeName) - return err } return nil } diff --git a/cmd/settings.go b/cmd/settings.go index 5f91e4b..9907bbe 100644 --- a/cmd/settings.go +++ b/cmd/settings.go @@ -10,6 +10,14 @@ import ( var Settings = &Config{} +// supported provider images +const ( + javaExternalProviderImg = "quay.io/konveyor/java-external-provider:latest" + genericProviderImg = "quay.io/konveyor/generic-external-provider:latest" + dotNetProviderImg = "quay.io/konveyor/dotnet-external-provider:latest" + yqProviderImg = "quay.io/konveyor/yq-external-provider:latest" +) + const ( RulesetPath = "/opt/rulesets" OpenRewriteRecipesPath = "/opt/openrewrite"