Use Golangci-lint

This commit is contained in:
Olivier Lemasle 2022-11-09 21:52:24 +01:00
parent fdfecc8d7f
commit 0ea1c1b8d3
23 changed files with 177 additions and 174 deletions

View file

@ -21,7 +21,6 @@ import (
"crypto/x509"
"flag"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"os"
@ -93,7 +92,7 @@ func (cmd *PrometheusAdapter) makePromClient() (prom.Client, error) {
}
if cmd.PrometheusVerb != http.MethodGet && cmd.PrometheusVerb != http.MethodPost {
return nil, fmt.Errorf("unsupported Prometheus HTTP verb %q. use \"GET\" or \"POST\" instead.", cmd.PrometheusVerb)
return nil, fmt.Errorf("unsupported Prometheus HTTP verb %q; supported verbs: \"GET\" and \"POST\"", cmd.PrometheusVerb)
}
var httpClient *http.Client
@ -115,7 +114,7 @@ func (cmd *PrometheusAdapter) makePromClient() (prom.Client, error) {
}
if cmd.PrometheusTokenFile != "" {
data, err := ioutil.ReadFile(cmd.PrometheusTokenFile)
data, err := os.ReadFile(cmd.PrometheusTokenFile)
if err != nil {
return nil, fmt.Errorf("failed to read prometheus-token-file: %v", err)
}
@ -392,7 +391,7 @@ func makeKubeconfigHTTPClient(inClusterAuth bool, kubeConfigPath string) (*http.
}
func makePrometheusCAClient(caFilePath string, tlsCertFilePath string, tlsKeyFilePath string) (*http.Client, error) {
data, err := ioutil.ReadFile(caFilePath)
data, err := os.ReadFile(caFilePath)
if err != nil {
return nil, fmt.Errorf("failed to read prometheus-ca-file: %v", err)
}
@ -409,6 +408,7 @@ func makePrometheusCAClient(caFilePath string, tlsCertFilePath string, tlsKeyFil
}
return &http.Client{
Transport: &http.Transport{
//nolint:gosec
TLSClientConfig: &tls.Config{
RootCAs: pool,
Certificates: []tls.Certificate{tlsClientCerts},
@ -419,6 +419,7 @@ func makePrometheusCAClient(caFilePath string, tlsCertFilePath string, tlsKeyFil
return &http.Client{
Transport: &http.Transport{
//nolint:gosec
TLSClientConfig: &tls.Config{
RootCAs: pool,
},

View file

@ -27,7 +27,6 @@ import (
const certsDir = "testdata"
func TestMakeKubeconfigHTTPClient(t *testing.T) {
tests := []struct {
kubeconfigPath string
inClusterAuth bool
@ -71,16 +70,13 @@ func TestMakeKubeconfigHTTPClient(t *testing.T) {
t.Error("HTTP client Transport is nil, expected http.RoundTripper")
}
}
} else {
if err == nil {
t.Errorf("Error is nil, expected %v", err)
}
} else if err == nil {
t.Errorf("Error is nil, expected %v", err)
}
}
}
func TestMakePrometheusCAClient(t *testing.T) {
tests := []struct {
caFilePath string
tlsCertFilePath string
@ -140,16 +136,13 @@ func TestMakePrometheusCAClient(t *testing.T) {
t.Errorf("TLS certificates is %+v, expected nil", prometheusCAClient.Transport.(*http.Transport).TLSClientConfig.Certificates)
}
}
} else {
if err == nil {
t.Errorf("Error is nil, expected %v", err)
}
} else if err == nil {
t.Errorf("Error is nil, expected %v", err)
}
}
}
func TestParseHeaderArgs(t *testing.T) {
tests := []struct {
args []string
headers http.Header

View file

@ -7,7 +7,7 @@ import (
pmodel "github.com/prometheus/common/model"
prom "sigs.k8s.io/prometheus-adapter/pkg/client"
. "sigs.k8s.io/prometheus-adapter/pkg/config"
"sigs.k8s.io/prometheus-adapter/pkg/config"
)
// DefaultConfig returns a configuration equivalent to the former
@ -15,55 +15,55 @@ import (
// will be of the form `<prefix><<.Resource>>`, cadvisor series will be
// of the form `container_`, and have the label `pod`. Any series ending
// in total will be treated as a rate metric.
func DefaultConfig(rateInterval time.Duration, labelPrefix string) *MetricsDiscoveryConfig {
return &MetricsDiscoveryConfig{
Rules: []DiscoveryRule{
func DefaultConfig(rateInterval time.Duration, labelPrefix string) *config.MetricsDiscoveryConfig {
return &config.MetricsDiscoveryConfig{
Rules: []config.DiscoveryRule{
// container seconds rate metrics
{
SeriesQuery: string(prom.MatchSeries("", prom.NameMatches("^container_.*"), prom.LabelNeq("container", "POD"), prom.LabelNeq("namespace", ""), prom.LabelNeq("pod", ""))),
Resources: ResourceMapping{
Overrides: map[string]GroupResource{
Resources: config.ResourceMapping{
Overrides: map[string]config.GroupResource{
"namespace": {Resource: "namespace"},
"pod": {Resource: "pod"},
},
},
Name: NameMapping{Matches: "^container_(.*)_seconds_total$"},
Name: config.NameMapping{Matches: "^container_(.*)_seconds_total$"},
MetricsQuery: fmt.Sprintf(`sum(rate(<<.Series>>{<<.LabelMatchers>>,container!="POD"}[%s])) by (<<.GroupBy>>)`, pmodel.Duration(rateInterval).String()),
},
// container rate metrics
{
SeriesQuery: string(prom.MatchSeries("", prom.NameMatches("^container_.*"), prom.LabelNeq("container", "POD"), prom.LabelNeq("namespace", ""), prom.LabelNeq("pod", ""))),
SeriesFilters: []RegexFilter{{IsNot: "^container_.*_seconds_total$"}},
Resources: ResourceMapping{
Overrides: map[string]GroupResource{
SeriesFilters: []config.RegexFilter{{IsNot: "^container_.*_seconds_total$"}},
Resources: config.ResourceMapping{
Overrides: map[string]config.GroupResource{
"namespace": {Resource: "namespace"},
"pod": {Resource: "pod"},
},
},
Name: NameMapping{Matches: "^container_(.*)_total$"},
Name: config.NameMapping{Matches: "^container_(.*)_total$"},
MetricsQuery: fmt.Sprintf(`sum(rate(<<.Series>>{<<.LabelMatchers>>,container!="POD"}[%s])) by (<<.GroupBy>>)`, pmodel.Duration(rateInterval).String()),
},
// container non-cumulative metrics
{
SeriesQuery: string(prom.MatchSeries("", prom.NameMatches("^container_.*"), prom.LabelNeq("container", "POD"), prom.LabelNeq("namespace", ""), prom.LabelNeq("pod", ""))),
SeriesFilters: []RegexFilter{{IsNot: "^container_.*_total$"}},
Resources: ResourceMapping{
Overrides: map[string]GroupResource{
SeriesFilters: []config.RegexFilter{{IsNot: "^container_.*_total$"}},
Resources: config.ResourceMapping{
Overrides: map[string]config.GroupResource{
"namespace": {Resource: "namespace"},
"pod": {Resource: "pod"},
},
},
Name: NameMapping{Matches: "^container_(.*)$"},
Name: config.NameMapping{Matches: "^container_(.*)$"},
MetricsQuery: `sum(<<.Series>>{<<.LabelMatchers>>,container!="POD"}) by (<<.GroupBy>>)`,
},
// normal non-cumulative metrics
{
SeriesQuery: string(prom.MatchSeries("", prom.LabelNeq(fmt.Sprintf("%snamespace", labelPrefix), ""), prom.NameNotMatches("^container_.*"))),
SeriesFilters: []RegexFilter{{IsNot: ".*_total$"}},
Resources: ResourceMapping{
SeriesFilters: []config.RegexFilter{{IsNot: ".*_total$"}},
Resources: config.ResourceMapping{
Template: fmt.Sprintf("%s<<.Resource>>", labelPrefix),
},
MetricsQuery: "sum(<<.Series>>{<<.LabelMatchers>>}) by (<<.GroupBy>>)",
@ -72,9 +72,9 @@ func DefaultConfig(rateInterval time.Duration, labelPrefix string) *MetricsDisco
// normal rate metrics
{
SeriesQuery: string(prom.MatchSeries("", prom.LabelNeq(fmt.Sprintf("%snamespace", labelPrefix), ""), prom.NameNotMatches("^container_.*"))),
SeriesFilters: []RegexFilter{{IsNot: ".*_seconds_total"}},
Name: NameMapping{Matches: "^(.*)_total$"},
Resources: ResourceMapping{
SeriesFilters: []config.RegexFilter{{IsNot: ".*_seconds_total"}},
Name: config.NameMapping{Matches: "^(.*)_total$"},
Resources: config.ResourceMapping{
Template: fmt.Sprintf("%s<<.Resource>>", labelPrefix),
},
MetricsQuery: fmt.Sprintf("sum(rate(<<.Series>>{<<.LabelMatchers>>}[%s])) by (<<.GroupBy>>)", pmodel.Duration(rateInterval).String()),
@ -83,20 +83,20 @@ func DefaultConfig(rateInterval time.Duration, labelPrefix string) *MetricsDisco
// seconds rate metrics
{
SeriesQuery: string(prom.MatchSeries("", prom.LabelNeq(fmt.Sprintf("%snamespace", labelPrefix), ""), prom.NameNotMatches("^container_.*"))),
Name: NameMapping{Matches: "^(.*)_seconds_total$"},
Resources: ResourceMapping{
Name: config.NameMapping{Matches: "^(.*)_seconds_total$"},
Resources: config.ResourceMapping{
Template: fmt.Sprintf("%s<<.Resource>>", labelPrefix),
},
MetricsQuery: fmt.Sprintf("sum(rate(<<.Series>>{<<.LabelMatchers>>}[%s])) by (<<.GroupBy>>)", pmodel.Duration(rateInterval).String()),
},
},
ResourceRules: &ResourceRules{
CPU: ResourceRule{
ResourceRules: &config.ResourceRules{
CPU: config.ResourceRule{
ContainerQuery: fmt.Sprintf("sum(rate(container_cpu_usage_seconds_total{<<.LabelMatchers>>}[%s])) by (<<.GroupBy>>)", pmodel.Duration(rateInterval).String()),
NodeQuery: fmt.Sprintf("sum(rate(container_cpu_usage_seconds_total{<<.LabelMatchers>>, id='/'}[%s])) by (<<.GroupBy>>)", pmodel.Duration(rateInterval).String()),
Resources: ResourceMapping{
Overrides: map[string]GroupResource{
Resources: config.ResourceMapping{
Overrides: map[string]config.GroupResource{
"namespace": {Resource: "namespace"},
"pod": {Resource: "pod"},
"instance": {Resource: "node"},
@ -104,11 +104,11 @@ func DefaultConfig(rateInterval time.Duration, labelPrefix string) *MetricsDisco
},
ContainerLabel: fmt.Sprintf("%scontainer", labelPrefix),
},
Memory: ResourceRule{
Memory: config.ResourceRule{
ContainerQuery: "sum(container_memory_working_set_bytes{<<.LabelMatchers>>}) by (<<.GroupBy>>)",
NodeQuery: "sum(container_memory_working_set_bytes{<<.LabelMatchers>>,id='/'}) by (<<.GroupBy>>)",
Resources: ResourceMapping{
Overrides: map[string]GroupResource{
Resources: config.ResourceMapping{
Overrides: map[string]config.GroupResource{
"namespace": {Resource: "namespace"},
"pod": {Resource: "pod"},
"instance": {Resource: "node"},