diff --git a/cmd/harvest/harvest.go b/cmd/harvest/harvest.go index 7f582f56e..bab546a04 100644 --- a/cmd/harvest/harvest.go +++ b/cmd/harvest/harvest.go @@ -504,7 +504,7 @@ func getPollerPrometheusPort(pollerName string, opts *options) int { return opts.promPort } - if promPort, err = conf.GetPrometheusExporterPorts(pollerName); err != nil { + if promPort, err = conf.GetPrometheusExporterPorts(pollerName, false); err != nil { fmt.Println(err) return 0 } diff --git a/cmd/tools/generate/generate.go b/cmd/tools/generate/generate.go index 1cec382dd..d417be399 100644 --- a/cmd/tools/generate/generate.go +++ b/cmd/tools/generate/generate.go @@ -162,10 +162,9 @@ func generateDocker(path string, kind int) { if err != nil { panic(err) } - conf.ValidatePortInUse = true var filesd []string for _, v := range conf.Config.PollersOrdered { - port, _ := conf.GetPrometheusExporterPorts(v) + port, _ := conf.GetPrometheusExporterPorts(v, true) pollerInfo := PollerInfo{ ServiceName: normalizeContainerNames(v), PollerName: v, diff --git a/pkg/conf/conf.go b/pkg/conf/conf.go index c36e97a06..1a262f5b8 100644 --- a/pkg/conf/conf.go +++ b/pkg/conf/conf.go @@ -21,7 +21,6 @@ import ( var Config = HarvestConfig{} var configRead = false -var ValidatePortInUse = false const ( DefaultAPIVersion = "1.3" @@ -32,6 +31,7 @@ const ( // TestLoadHarvestConfig is used by testing code to reload a new config func TestLoadHarvestConfig(configPath string) { configRead = false + promPortRangeMapping = make(map[string]PortMap) err := LoadHarvestConfig(configPath) if err != nil { log.Fatalf("Failed to load config at=[%s] err=%+v\n", configPath, err) @@ -179,12 +179,12 @@ func GetHarvestLogPath() string { } // GetPrometheusExporterPorts returns the Prometheus port for the given poller -func GetPrometheusExporterPorts(pollerName string) (int, error) { +func GetPrometheusExporterPorts(pollerName string, validatePortInUse bool) (int, error) { var port int var isPrometheusExporterConfigured bool if len(promPortRangeMapping) == 0 { - loadPrometheusExporterPortRangeMapping() + loadPrometheusExporterPortRangeMapping(validatePortInUse) } poller := Config.Pollers[pollerName] if poller == nil { @@ -229,18 +229,18 @@ type PortMap struct { freePorts map[int]struct{} } -func PortMapFromRange(address string, portRange *IntRange) PortMap { +func PortMapFromRange(address string, portRange *IntRange, validatePortInUse bool) PortMap { portMap := PortMap{} portMap.freePorts = make(map[int]struct{}) start := portRange.Min end := portRange.Max for i := start; i <= end; i++ { portMap.portSet = append(portMap.portSet, i) - if ValidatePortInUse { + if validatePortInUse { portMap.freePorts[i] = struct{}{} } } - if !ValidatePortInUse { + if !validatePortInUse { portMap.freePorts = util.CheckFreePorts(address, portMap.portSet) } return portMap @@ -248,12 +248,12 @@ func PortMapFromRange(address string, portRange *IntRange) PortMap { var promPortRangeMapping = make(map[string]PortMap) -func loadPrometheusExporterPortRangeMapping() { +func loadPrometheusExporterPortRangeMapping(validatePortInUse bool) { for k, v := range Config.Exporters { if v.Type == "Prometheus" { if v.PortRange != nil { // we only care about free ports on the localhost - promPortRangeMapping[k] = PortMapFromRange("localhost", v.PortRange) + promPortRangeMapping[k] = PortMapFromRange("localhost", v.PortRange, validatePortInUse) } } } diff --git a/pkg/conf/conf_test.go b/pkg/conf/conf_test.go index c461dc667..a003a1b66 100644 --- a/pkg/conf/conf_test.go +++ b/pkg/conf/conf_test.go @@ -12,8 +12,6 @@ var testYml = "../../cmd/tools/doctor/testdata/testConfig.yml" func TestGetPrometheusExporterPorts(t *testing.T) { TestLoadHarvestConfig(testYml) - // Test without checking - ValidatePortInUse = true type args struct { pollerNames []string } @@ -29,7 +27,7 @@ func TestGetPrometheusExporterPorts(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { for i, v := range tt.args.pollerNames { - got, err := GetPrometheusExporterPorts(v) + got, err := GetPrometheusExporterPorts(v, true) if (err != nil) != tt.wantErr[i] { t.Errorf("GetPrometheusExporterPorts() error = %v, wantErr %v", err, tt.wantErr) return @@ -44,8 +42,8 @@ func TestGetPrometheusExporterPorts(t *testing.T) { func TestGetPrometheusExporterPortsIssue284(t *testing.T) { TestLoadHarvestConfig("../../cmd/tools/doctor/testdata/issue-284.yml") - loadPrometheusExporterPortRangeMapping() - got, _ := GetPrometheusExporterPorts("issue-284") + loadPrometheusExporterPortRangeMapping(false) + got, _ := GetPrometheusExporterPorts("issue-284", false) if got != 0 { t.Fatalf("expected port to be 0 but was %d", got) }