From 42e4a59b4a275215e3edea65eb3331fbc85048de Mon Sep 17 00:00:00 2001 From: Emily McMullan Date: Mon, 20 May 2024 21:11:23 -0400 Subject: [PATCH] merge provider config options with default config options Signed-off-by: Emily McMullan --- cmd/analyze.go | 125 ++++++++++++++++++++++++---------- cmd/settings.go | 1 - provider_settings.json.sample | 27 ++++++++ 3 files changed, 115 insertions(+), 38 deletions(-) create mode 100644 provider_settings.json.sample diff --git a/cmd/analyze.go b/cmd/analyze.go index 1ab1874..bd33cef 100644 --- a/cmd/analyze.go +++ b/cmd/analyze.go @@ -61,7 +61,6 @@ type analyzeCommand struct { analyzeKnownLibraries bool jsonOutput bool overwrite bool - mavenSettingsFile string sources []string targets []string labelSelector string @@ -71,9 +70,6 @@ type analyzeCommand struct { rules []string jaegerEndpoint string enableDefaultRulesets bool - httpProxy string - httpsProxy string - noProxy string contextLines int incidentSelector string @@ -206,15 +202,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)") @@ -273,9 +265,9 @@ 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 @@ -283,9 +275,9 @@ func (a *analyzeCommand) Validate() error { 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") } @@ -467,7 +459,6 @@ func (a *analyzeCommand) getConfigVolumes(providers []string, ports map[string]i foundGolang = true } } - // TODO (pgaikwad): binaries don't work with alizer right now, we need to revisit this if !foundJava && a.isFileInput { foundJava = true } @@ -496,17 +487,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, @@ -542,19 +522,25 @@ func (a *analyzeCommand) getConfigVolumes(providers []string, ports map[string]i provConfig = append(provConfig, goConfig) } - // 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 + } + + mergedConfig, err := a.mergeProviderConfig(provConfig, *optionsConfig, tempDir) + if err != nil { + return nil, err + } + fmt.Printf("MERGED CONFIG %v \n", mergedConfig) - jsonData, err := json.MarshalIndent(&provConfig, "", " ") + jsonData, err := json.MarshalIndent(&mergedConfig, "", " ") if err != nil { a.log.V(1).Error(err, "failed to marshal provider config") return nil, err @@ -1221,6 +1207,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 diff --git a/cmd/settings.go b/cmd/settings.go index 2c7828a..9fcf0bd 100644 --- a/cmd/settings.go +++ b/cmd/settings.go @@ -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"` diff --git a/provider_settings.json.sample b/provider_settings.json.sample new file mode 100644 index 0000000..311b663 --- /dev/null +++ b/provider_settings.json.sample @@ -0,0 +1,27 @@ +[ + { + "name": "go", + "initConfig": [{ + "analysisMode": "full", + "providerSpecificConfig": { + "lspServerArgs": [], + "lspServerInitializationOptions": "", + + "workspaceFolders": [], + "dependencyFolders": [] + } + }] + }, + { + "name": "java", + "initConfig": [{ + "providerSpecificConfig": { + "bundles": "", + "depOpenSourceLabelsFile": "", + "jvmMaxMem": "", + "mavenSettingsFile": "" + }, + "analysisMode": "full" + }] + } +]