Skip to content

Commit

Permalink
merge provider config options with default config options
Browse files Browse the repository at this point in the history
Signed-off-by: Emily McMullan <[email protected]>
  • Loading branch information
eemcmullan committed May 21, 2024
1 parent aa605e9 commit 1154cef
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 39 deletions.
126 changes: 88 additions & 38 deletions cmd/analyze.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ type analyzeCommand struct {
analyzeKnownLibraries bool
jsonOutput bool
overwrite bool
mavenSettingsFile string
sources []string
targets []string
labelSelector string
Expand All @@ -73,9 +72,6 @@ type analyzeCommand struct {
rules []string
jaegerEndpoint string
enableDefaultRulesets bool
httpProxy string
httpsProxy string
noProxy string
contextLines int
incidentSelector string
depFolders []string
Expand Down Expand Up @@ -209,15 +205,11 @@ func NewAnalyzeCmd(log logr.Logger) *cobra.Command {
analyzeCommand.Flags().StringVarP(&analyzeCmd.output, "output", "o", "", "path to the directory for analysis output")
analyzeCommand.Flags().BoolVar(&analyzeCmd.skipStaticReport, "skip-static-report", false, "do not generate static report")
analyzeCommand.Flags().BoolVar(&analyzeCmd.analyzeKnownLibraries, "analyze-known-libraries", false, "analyze known open-source libraries")
analyzeCommand.Flags().StringVar(&analyzeCmd.mavenSettingsFile, "maven-settings", "", "path to a custom maven settings file to use")
analyzeCommand.Flags().StringVarP(&analyzeCmd.mode, "mode", "m", string(provider.FullAnalysisMode), "analysis mode. Must be one of 'full' or 'source-only'")
analyzeCommand.Flags().BoolVar(&analyzeCmd.jsonOutput, "json-output", false, "create analysis and dependency output as json")
analyzeCommand.Flags().BoolVar(&analyzeCmd.overwrite, "overwrite", false, "overwrite output directory")
analyzeCommand.Flags().StringVar(&analyzeCmd.jaegerEndpoint, "jaeger-endpoint", "", "jaeger endpoint to collect traces")
analyzeCommand.Flags().BoolVar(&analyzeCmd.enableDefaultRulesets, "enable-default-rulesets", true, "run default rulesets with analysis")
analyzeCommand.Flags().StringVar(&analyzeCmd.httpProxy, "http-proxy", loadEnvInsensitive("http_proxy"), "HTTP proxy string URL")
analyzeCommand.Flags().StringVar(&analyzeCmd.httpsProxy, "https-proxy", loadEnvInsensitive("https_proxy"), "HTTPS proxy string URL")
analyzeCommand.Flags().StringVar(&analyzeCmd.noProxy, "no-proxy", loadEnvInsensitive("no_proxy"), "proxy excluded URLs (relevant only with proxy)")
analyzeCommand.Flags().IntVar(&analyzeCmd.contextLines, "context-lines", 100, "number of lines of source code to include in the output for each incident")
analyzeCommand.Flags().StringVar(&analyzeCmd.incidentSelector, "incident-selector", "", "an expression to select incidents based on custom variables. ex: (!package=io.konveyor.demo.config-utils)")
analyzeCommand.Flags().StringArrayVarP(&analyzeCmd.depFolders, "dependency-folders", "d", []string{}, "directory for dependencies")
Expand Down Expand Up @@ -288,19 +280,19 @@ func (a *analyzeCommand) Validate() error {
a.mode != string(provider.SourceOnlyAnalysisMode) {
return fmt.Errorf("mode must be one of 'full' or 'source-only'")
}
if _, err := os.Stat(a.mavenSettingsFile); a.mavenSettingsFile != "" && err != nil {
return fmt.Errorf("%w failed to stat maven settings file at path %s", err, a.mavenSettingsFile)
}
// if _, err := os.Stat(a.mavenSettingsFile); a.mavenSettingsFile != "" && err != nil {
// return fmt.Errorf("%w failed to stat maven settings file at path %s", err, a.mavenSettingsFile)
// }
// try to get abs path, if not, continue with relative path
if absPath, err := filepath.Abs(a.output); err == nil {
a.output = absPath
}
if absPath, err := filepath.Abs(a.input); err == nil {
a.input = absPath
}
if absPath, err := filepath.Abs(a.mavenSettingsFile); a.mavenSettingsFile != "" && err == nil {
a.mavenSettingsFile = absPath
}
// if absPath, err := filepath.Abs(a.mavenSettingsFile); a.mavenSettingsFile != "" && err == nil {
// a.mavenSettingsFile = absPath
// }
if !a.enableDefaultRulesets && len(a.rules) == 0 {
return fmt.Errorf("must specify rules if default rulesets are not enabled")
}
Expand Down Expand Up @@ -503,8 +495,6 @@ func (a *analyzeCommand) getConfigVolumes(providers []string, ports map[string]i
default:
return nil, fmt.Errorf("unable to find config for provider %v", providers[0])
}

// TODO (pgaikwad): binaries don't work with alizer right now, we need to revisit this
if !foundJava && a.isFileInput {
foundJava = true
}
Expand Down Expand Up @@ -533,17 +523,6 @@ func (a *analyzeCommand) getConfigVolumes(providers []string, ports map[string]i
},
},
}
if a.mavenSettingsFile != "" {
err := copyFileContents(a.mavenSettingsFile, filepath.Join(tempDir, "settings.xml"))
if err != nil {
a.log.V(1).Error(err, "failed copying maven settings file", "path", a.mavenSettingsFile)
return nil, err
}
javaConfig.InitConfig[0].ProviderSpecificConfig["mavenSettingsFile"] = fmt.Sprintf("%s/%s", ConfigMountPath, "settings.xml")
}
if Settings.JvmMaxMem != "" {
javaConfig.InitConfig[0].ProviderSpecificConfig["jvmMaxMem"] = Settings.JvmMaxMem
}

goConfig := provider.Config{
Name: goProvider,
Expand Down Expand Up @@ -624,19 +603,25 @@ func (a *analyzeCommand) getConfigVolumes(providers []string, ports map[string]i
provConfig = append(provConfig, nodeJSConfig)
}

// Set proxy to providers
if a.httpProxy != "" || a.httpsProxy != "" {
proxy := provider.Proxy{
HTTPProxy: a.httpProxy,
HTTPSProxy: a.httpsProxy,
NoProxy: a.noProxy,
}
for i := range provConfig {
provConfig[i].Proxy = &proxy
}
// TODO change name!!!!!!!!!!!!!!
data, err := os.ReadFile("test.json")
if err != nil {
return nil, err
}
optionsConfig := &[]provider.Config{}
err = yaml.Unmarshal(data, optionsConfig)
if err != nil {
a.log.V(1).Error(err, "failed to unmarshal provider settings options file")
return nil, err
}

jsonData, err := json.MarshalIndent(&provConfig, "", " ")
mergedConfig, err := a.mergeProviderConfig(provConfig, *optionsConfig, tempDir)
if err != nil {
return nil, err
}
fmt.Printf("MERGED CONFIG %v \n", mergedConfig)

jsonData, err := json.MarshalIndent(&mergedConfig, "", " ")
if err != nil {
a.log.V(1).Error(err, "failed to marshal provider config")
return nil, err
Expand Down Expand Up @@ -1325,6 +1310,71 @@ func (a *analyzeCommand) ConvertXML(ctx context.Context) (string, error) {
return tempOutputDir, nil
}

func (a *analyzeCommand) mergeProviderConfig(defaultConf, optionsConf []provider.Config, tempDir string) ([]provider.Config, error) {
merged := []provider.Config{}
seen := map[string]*provider.Config{}

// find options used for supported providers
for idx, conf := range defaultConf {
seen[conf.Name] = &defaultConf[idx]
}

for idx, conf := range optionsConf {
if _, ok := seen[conf.Name]; ok {

// set provider config options
if conf.ContextLines != 0 {
seen[conf.Name].ContextLines = conf.ContextLines
}
if conf.Proxy != nil {
seen[conf.Name].Proxy = conf.Proxy
}
// set init config options
for i, init := range conf.InitConfig {
if len(init.AnalysisMode) != 0 {
seen[conf.Name].InitConfig[i].AnalysisMode = init.AnalysisMode
}
if len(init.ProviderSpecificConfig) != 0 {
provSpecificConf, err := a.mergeProviderSpecificConfig(init.ProviderSpecificConfig, seen[conf.Name].InitConfig[i].ProviderSpecificConfig, tempDir)
if err != nil {
return nil, err
}
seen[conf.Name].InitConfig[i].ProviderSpecificConfig = provSpecificConf
}
}
// custom provider
// we will assume these options will be correct
} else {
seen[conf.Name] = &optionsConf[idx]
}
}
for _, v := range seen {
merged = append(merged, *v)
}

return merged, nil
}

func (a *analyzeCommand) mergeProviderSpecificConfig(optionsConf, seenConf map[string]interface{}, tempDir string) (map[string]interface{}, error) {
for k, v := range optionsConf {
if optionsConf[k] != "" {

// special case for maven settings file to mount correctly
if k == "mavenSettingsFile" {
err := copyFileContents(v.(string), filepath.Join(tempDir, "settings.xml"))
if err != nil {
a.log.V(1).Error(err, "failed copying maven settings file", "path", v)
return nil, err
}
seenConf[k] = fmt.Sprintf("%s/%s", ConfigMountPath, "settings.xml")
continue
}
seenConf[k] = v
}
}
return seenConf, nil
}

func (a *analyzeCommand) CleanAnalysisResources(ctx context.Context) error {
if !a.cleanup {
return nil
Expand Down
1 change: 0 additions & 1 deletion cmd/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ 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"`
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"`
Expand Down
27 changes: 27 additions & 0 deletions provider_settings.json.sample
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
[
{
"name": "go",
"initConfig": [{
"analysisMode": "full",
"providerSpecificConfig": {
"lspServerArgs": [],
"lspServerInitializationOptions": "",

"workspaceFolders": [],
"dependencyFolders": []
}
}]
},
{
"name": "java",
"initConfig": [{
"providerSpecificConfig": {
"bundles": "",
"depOpenSourceLabelsFile": "",
"jvmMaxMem": "",
"mavenSettingsFile": ""
},
"analysisMode": "full"
}]
}
]

0 comments on commit 1154cef

Please sign in to comment.