Skip to content

Commit

Permalink
clean resources using routines
Browse files Browse the repository at this point in the history
Signed-off-by: Emily McMullan <[email protected]>
  • Loading branch information
eemcmullan committed Apr 20, 2024
1 parent 37b895e commit 0cb1ab5
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 44 deletions.
92 changes: 48 additions & 44 deletions cmd/analyze.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"os/exec"
"path"
"runtime"
"sync"

"path/filepath"
"sort"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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())
Expand All @@ -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")
Expand Down Expand Up @@ -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
}
}
Expand All @@ -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",
Expand All @@ -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,
Expand Down Expand Up @@ -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)}
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
}
Expand Down
8 changes: 8 additions & 0 deletions cmd/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit 0cb1ab5

Please sign in to comment.