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]>

update settings with provider images

Signed-off-by: Emily McMullan <[email protected]>
  • Loading branch information
eemcmullan committed Apr 22, 2024
1 parent 37b895e commit 316e44e
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 95 deletions.
6 changes: 0 additions & 6 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@ RUN microdnf -y install git &&\

FROM quay.io/konveyor/static-report:latest as static-report

FROM quay.io/konveyor/java-external-provider:latest as java-provider
FROM quay.io/konveyor/generic-external-provider:latest as generic-provider

# Build the manager binary
FROM golang:1.21 as builder

Expand Down Expand Up @@ -47,9 +44,6 @@ COPY --from=rulesets /rulesets/default/generated /opt/rulesets
COPY --from=rulesets /windup-rulesets/rules/rules-reviewed/openrewrite /opt/openrewrite
COPY --from=static-report /usr/bin/js-bundle-generator /usr/local/bin
COPY --from=static-report /usr/local/static-report /usr/local/static-report
COPY --from=java-provider /usr/local/bin/java-external-provider /usr/local/bin/java-external-provider
COPY --from=generic-provider /usr/local/bin/go-dependency-provider /usr/local/bin/go-dependency-provider
COPY --from=generic-provider /usr/local/bin/generic-external-provider /usr/local/bin/generic-external-provider
COPY --chmod=755 entrypoint.sh /usr/bin/entrypoint.sh
COPY --chmod=755 openrewrite_entrypoint.sh /usr/bin/openrewrite_entrypoint.sh

Expand Down
116 changes: 32 additions & 84 deletions cmd/analyze.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,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,13 +125,8 @@ 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")
if err := analyzeCmd.CleanAnalysisResources(cmd.Context()); err != nil {
log.Error(err, "failed to clean temporary directories")
}
}()
if analyzeCmd.listSources || analyzeCmd.listTargets {
Expand All @@ -159,7 +151,7 @@ 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))
}
}
containerNetworkName, err := analyzeCmd.createContainerNetwork()
Expand Down Expand Up @@ -461,10 +453,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 +470,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 +498,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,13 +776,14 @@ 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":
providerImage = javaExternalProviderImg
providerPorts["java"] = port
case "Go":
providerImage = genericProviderImg
providerPorts["go"] = port
case javaProvider:
providerImage = Settings.JavaProviderImage
providerPorts[javaProvider] = port
case goProvider:
providerImage = Settings.GenericProviderImage
providerPorts[goProvider] = port
default:
return nil, fmt.Errorf("unable to run unsupported provider %v", providers[0])
}
args := []string{fmt.Sprintf("--port=%v", port)}
a.log.Info("starting provider", "provider", providers[0])
Expand All @@ -812,45 +805,6 @@ func (a *analyzeCommand) RunProviders(ctx context.Context, networkName string, v
}
a.providerContainerNames = append(a.providerContainerNames, con.Name)

// we need to start each provider container separately from the first one so that
// we can join the first container's network
// this is so not for the provider containers to share the same network but
// because the network needs to be shared with the analyzer engine
// if len(providers) > 1 {
// for _, prov := range providers[1:] {
// // this will make more sense when we have more than 2 supported providers
// var provImage string
// switch prov {
// case "Java":
// provImage = javaExternalProviderImg
// providerPorts["java"] = port
// case "Go":
// provImage = golangDepProviderImg
// providerPorts["go"] = port
// default:
// }

// a.log.Info("starting provider", "provider", prov)
// c := container.NewContainer()
// err := con.Run(
// ctx,
// container.WithImage(provImage),
// container.WithLog(a.log.V(1)),
// container.WithVolumes(volumes),
// container.WithContainerToolBin(Settings.PodmanBinary),
// container.WithEntrypointArgs(args...),
// container.WithDetachedMode(true),
// container.WithCleanup(a.cleanup),
// container.WithNetwork(fmt.Sprintf("container:%v", providers[0])),
// )
// if err != nil {
// return err
// }
// // for cleanup
// a.providerContainerNames = append(a.providerContainerNames, c.Name)
// }
// }

return providerPorts, nil
}

Expand Down Expand Up @@ -895,7 +849,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 +1026,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 +1183,32 @@ func (a *analyzeCommand) ConvertXML(ctx context.Context) (string, error) {
return tempOutputDir, nil
}

func (a *analyzeCommand) RmAnalysisResources(ctx context.Context) error {
func (a *analyzeCommand) CleanAnalysisResources(ctx context.Context) error {
if len(a.providerContainerNames) == 0 {
return nil
}
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
}
}
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
14 changes: 9 additions & 5 deletions cmd/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,15 @@ const (
)

type Config struct {
RootCommandName string `env:"CMD_NAME" default:"kantra"`
PodmanBinary string `env:"PODMAN_BIN" default:"/usr/bin/podman"`
RunnerImage string `env:"RUNNER_IMG" default:"quay.io/konveyor/kantra"`
JvmMaxMem string `env:"JVM_MAX_MEM" default:""`
RunLocal bool `env:"RUN_LOCAL"`
RootCommandName string `env:"CMD_NAME" default:"kantra"`
PodmanBinary string `env:"PODMAN_BIN" default:"/usr/bin/podman"`
RunnerImage string `env:"RUNNER_IMG" default:"quay.io/konveyor/kantra"`
JvmMaxMem string `env:"JVM_MAX_MEM" default:""`
RunLocal bool `env:"RUN_LOCAL"`
JavaProviderImage string `env:"JAVA_PROVIDER_IMG" default:"quay.io/konveyor/java-external-provider:latest"`
GenericProviderImage string `env:"GENERIC_PROVIDER_IMG" default:"quay.io/konveyor/generic-external-provider:latest"`
DotNetProviderImage string `env:"DOTNET_PROVIDER_IMG" default:"quay.io/konveyor/dotnet-external-provider:latest"`
YQProviderImage string `env:"YQ_PROVIDER_IMG" default:"quay.io/konveyor/yq-external-provider:latest"`
}

func (c *Config) Load() error {
Expand Down

0 comments on commit 316e44e

Please sign in to comment.