Fix golangci-lint errors (#10196)

* Fix golangci-lint errors

Signed-off-by: z1cheng <imchench@gmail.com>

* Fix dupl errors

Signed-off-by: z1cheng <imchench@gmail.com>

* Fix comments

Signed-off-by: z1cheng <imchench@gmail.com>

* Fix errcheck lint errors

Signed-off-by: z1cheng <imchench@gmail.com>

* Fix assert in e2e test

Signed-off-by: z1cheng <imchench@gmail.com>

* Not interrupt the waitForPodsReady

Signed-off-by: z1cheng <imchench@gmail.com>

* Replace string with constant

Signed-off-by: z1cheng <imchench@gmail.com>

* Fix comments

Signed-off-by: z1cheng <imchench@gmail.com>

* Revert write file permision

Signed-off-by: z1cheng <imchench@gmail.com>

---------

Signed-off-by: z1cheng <imchench@gmail.com>
This commit is contained in:
Chen Chen 2023-08-31 15:36:48 +08:00 committed by GitHub
parent 46d87d3462
commit b3060bfbd0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
253 changed files with 2434 additions and 2113 deletions

View file

@ -104,7 +104,7 @@ func NewAdmissionCollector(pod, namespace, class string) *AdmissionCollector {
}
// Describe implements prometheus.Collector
func (am AdmissionCollector) Describe(ch chan<- *prometheus.Desc) {
func (am *AdmissionCollector) Describe(ch chan<- *prometheus.Desc) {
am.testedIngressLength.Describe(ch)
am.testedIngressTime.Describe(ch)
am.renderingIngressLength.Describe(ch)
@ -114,7 +114,7 @@ func (am AdmissionCollector) Describe(ch chan<- *prometheus.Desc) {
}
// Collect implements the prometheus.Collector interface.
func (am AdmissionCollector) Collect(ch chan<- prometheus.Metric) {
func (am *AdmissionCollector) Collect(ch chan<- prometheus.Metric) {
am.testedIngressLength.Collect(ch)
am.testedIngressTime.Collect(ch)
am.renderingIngressLength.Collect(ch)
@ -139,7 +139,7 @@ func ByteFormat(bytes int64) string {
}
// SetAdmissionMetrics sets the values for AdmissionMetrics that can be called externally
func (am *AdmissionCollector) SetAdmissionMetrics(testedIngressLength float64, testedIngressTime float64, renderingIngressLength float64, renderingIngressTime float64, testedConfigurationSize float64, admissionTime float64) {
func (am *AdmissionCollector) SetAdmissionMetrics(testedIngressLength, testedIngressTime, renderingIngressLength, renderingIngressTime, testedConfigurationSize, admissionTime float64) {
am.testedIngressLength.Set(testedIngressLength)
am.testedIngressTime.Set(testedIngressTime)
am.renderingIngressLength.Set(renderingIngressLength)

View file

@ -226,7 +226,7 @@ func (cm *Controller) IncCheckErrorCount(namespace, name string) {
}
// IncOrphanIngress sets the the orphaned ingress gauge to one
func (cm *Controller) IncOrphanIngress(namespace string, name string, orphanityType string) {
func (cm *Controller) IncOrphanIngress(namespace, name, orphanityType string) {
labels := prometheus.Labels{
"namespace": namespace,
"ingress": name,
@ -236,7 +236,7 @@ func (cm *Controller) IncOrphanIngress(namespace string, name string, orphanityT
}
// DecOrphanIngress sets the the orphaned ingress gauge to zero (all services has their endpoints)
func (cm *Controller) DecOrphanIngress(namespace string, name string, orphanityType string) {
func (cm *Controller) DecOrphanIngress(namespace, name, orphanityType string) {
labels := prometheus.Labels{
"namespace": namespace,
"ingress": name,
@ -261,7 +261,7 @@ func (cm *Controller) ConfigSuccess(hash uint64, success bool) {
}
// Describe implements prometheus.Collector
func (cm Controller) Describe(ch chan<- *prometheus.Desc) {
func (cm *Controller) Describe(ch chan<- *prometheus.Desc) {
cm.configHash.Describe(ch)
cm.configSuccess.Describe(ch)
cm.configSuccessTime.Describe(ch)
@ -277,7 +277,7 @@ func (cm Controller) Describe(ch chan<- *prometheus.Desc) {
}
// Collect implements the prometheus.Collector interface.
func (cm Controller) Collect(ch chan<- prometheus.Metric) {
func (cm *Controller) Collect(ch chan<- prometheus.Metric) {
cm.configHash.Collect(ch)
cm.configSuccess.Collect(ch)
cm.configSuccessTime.Collect(ch)
@ -295,41 +295,45 @@ func (cm Controller) Collect(ch chan<- prometheus.Metric) {
// SetSSLExpireTime sets the expiration time of SSL Certificates
func (cm *Controller) SetSSLExpireTime(servers []*ingress.Server) {
for _, s := range servers {
if s.Hostname != "" && s.SSLCert != nil && s.SSLCert.ExpireTime.Unix() > 0 {
labels := make(prometheus.Labels, len(cm.labels)+1)
for k, v := range cm.labels {
labels[k] = v
}
labels["host"] = s.Hostname
labels["secret_name"] = s.SSLCert.Name
cm.sslExpireTime.With(labels).Set(float64(s.SSLCert.ExpireTime.Unix()))
if !(s.Hostname != "" && s.SSLCert != nil && s.SSLCert.ExpireTime.Unix() > 0) {
continue
}
labels := make(prometheus.Labels, len(cm.labels)+1)
for k, v := range cm.labels {
labels[k] = v
}
labels["host"] = s.Hostname
labels["secret_name"] = s.SSLCert.Name
cm.sslExpireTime.With(labels).Set(float64(s.SSLCert.ExpireTime.Unix()))
}
}
// SetSSLInfo creates a metric with all certificates informations
func (cm *Controller) SetSSLInfo(servers []*ingress.Server) {
for _, s := range servers {
if s.SSLCert != nil && s.SSLCert.Certificate != nil && s.SSLCert.Certificate.SerialNumber != nil {
labels := make(prometheus.Labels, len(cm.labels)+1)
for k, v := range cm.labels {
labels[k] = v
}
labels["identifier"] = s.SSLCert.Identifier()
labels["host"] = s.Hostname
labels["secret_name"] = s.SSLCert.Name
labels["namespace"] = s.SSLCert.Namespace
labels["issuer_common_name"] = s.SSLCert.Certificate.Issuer.CommonName
labels["issuer_organization"] = ""
if len(s.SSLCert.Certificate.Issuer.Organization) > 0 {
labels["issuer_organization"] = s.SSLCert.Certificate.Issuer.Organization[0]
}
labels["serial_number"] = s.SSLCert.Certificate.SerialNumber.String()
labels["public_key_algorithm"] = s.SSLCert.Certificate.PublicKeyAlgorithm.String()
cm.sslInfo.With(labels).Set(1)
if s.SSLCert == nil || s.SSLCert.Certificate == nil || s.SSLCert.Certificate.SerialNumber == nil {
continue
}
labels := make(prometheus.Labels, len(cm.labels)+1)
for k, v := range cm.labels {
labels[k] = v
}
labels["identifier"] = s.SSLCert.Identifier()
labels["host"] = s.Hostname
labels["secret_name"] = s.SSLCert.Name
labels["namespace"] = s.SSLCert.Namespace
labels["issuer_common_name"] = s.SSLCert.Certificate.Issuer.CommonName
labels["issuer_organization"] = ""
if len(s.SSLCert.Certificate.Issuer.Organization) > 0 {
labels["issuer_organization"] = s.SSLCert.Certificate.Issuer.Organization[0]
}
labels["serial_number"] = s.SSLCert.Certificate.SerialNumber.String()
labels["public_key_algorithm"] = s.SSLCert.Certificate.PublicKeyAlgorithm.String()
cm.sslInfo.With(labels).Set(1)
}
}

View file

@ -76,9 +76,12 @@ func TestControllerCounters(t *testing.T) {
{
name: "should set SSL certificates metrics",
test: func(cm *Controller) {
t1, _ := time.Parse(
t1, err := time.Parse(
time.RFC3339,
"2012-11-01T22:08:41+00:00")
if err != nil {
t.Errorf("unexpected error: %v", err)
}
servers := []*ingress.Server{
{
@ -106,7 +109,6 @@ func TestControllerCounters(t *testing.T) {
{
name: "should set SSL certificates infos metrics",
test: func(cm *Controller) {
servers := []*ingress.Server{
{
Hostname: "demo",
@ -143,7 +145,6 @@ func TestControllerCounters(t *testing.T) {
{
name: "should ignore certificates without serial number",
test: func(cm *Controller) {
servers := []*ingress.Server{
{
Hostname: "demo",
@ -168,7 +169,6 @@ func TestControllerCounters(t *testing.T) {
{
name: "should ignore certificates with nil x509 pointer",
test: func(cm *Controller) {
servers := []*ingress.Server{
{
Hostname: "demo",
@ -193,7 +193,6 @@ func TestControllerCounters(t *testing.T) {
{
name: "should ignore servers without certificates",
test: func(cm *Controller) {
servers := []*ingress.Server{
{
Hostname: "demo",
@ -232,9 +231,12 @@ func TestRemoveMetrics(t *testing.T) {
t.Errorf("registering collector failed: %s", err)
}
t1, _ := time.Parse(
t1, err := time.Parse(
time.RFC3339,
"2012-11-01T22:08:41+00:00")
if err != nil {
t.Errorf("Unexpected error: %v", err)
}
servers := []*ingress.Server{
{
@ -279,10 +281,12 @@ func TestRemoveAllSSLMetrics(t *testing.T) {
t.Errorf("registering collector failed: %s", err)
}
t1, _ := time.Parse(
t1, err := time.Parse(
time.RFC3339,
"2012-11-01T22:08:41+00:00")
if err != nil {
t.Errorf("unexpected error: %v", err)
}
servers := []*ingress.Server{
{
Hostname: "demo",

View file

@ -75,7 +75,6 @@ type NGINXStatusCollector interface {
// NewNGINXStatus returns a new prometheus collector the default nginx status module
func NewNGINXStatus(podName, namespace, ingressClass string) (NGINXStatusCollector, error) {
p := nginxStatusCollector{
scrapeChan: make(chan scrapeRequest),
}

View file

@ -106,7 +106,7 @@ func TestStatusCollector(t *testing.T) {
server := &httptest.Server{
Listener: listener,
Config: &http.Server{Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
Config: &http.Server{Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { //nolint:gosec // Ignore the gosec error in testing
w.WriteHeader(http.StatusOK)
if r.URL.Path == "/nginx_status" {

View file

@ -53,6 +53,8 @@ type BinaryNameMatcher struct {
// MatchAndName returns false if the match failed, otherwise
// true and the resulting name.
//
//nolint:gocritic // nacl param cannot be a pointer since it's to implement common.MatchNamer interface
func (em BinaryNameMatcher) MatchAndName(nacl common.ProcAttributes) (bool, string) {
if len(nacl.Cmdline) == 0 {
return false, ""
@ -94,8 +96,10 @@ type NGINXProcessCollector interface {
Stop()
}
var name = "nginx"
var binary = "/usr/bin/nginx"
var (
name = "nginx"
binary = "/usr/bin/nginx"
)
// NewNGINXProcess returns a new prometheus collector for the nginx process
func NewNGINXProcess(pod, namespace, ingressClass string) (NGINXProcessCollector, error) {
@ -106,7 +110,7 @@ func NewNGINXProcess(pod, namespace, ingressClass string) (NGINXProcessCollector
nm := newBinaryNameMatcher(name, binary)
p := namedProcess{
p := &namedProcess{
scrapeChan: make(chan scrapeRequest),
Grouper: proc.NewGrouper(nm, true, false, false, false),
fs: fs,
@ -164,7 +168,7 @@ func NewNGINXProcess(pod, namespace, ingressClass string) (NGINXProcessCollector
}
// Describe implements prometheus.Collector.
func (p namedProcess) Describe(ch chan<- *prometheus.Desc) {
func (p *namedProcess) Describe(ch chan<- *prometheus.Desc) {
ch <- p.data.cpuSecs
ch <- p.data.numProcs
ch <- p.data.readBytes
@ -175,13 +179,13 @@ func (p namedProcess) Describe(ch chan<- *prometheus.Desc) {
}
// Collect implements prometheus.Collector.
func (p namedProcess) Collect(ch chan<- prometheus.Metric) {
func (p *namedProcess) Collect(ch chan<- prometheus.Metric) {
req := scrapeRequest{results: ch, done: make(chan struct{})}
p.scrapeChan <- req
<-req.done
}
func (p namedProcess) Start() {
func (p *namedProcess) Start() {
for req := range p.scrapeChan {
ch := req.results
p.scrape(ch)
@ -189,18 +193,19 @@ func (p namedProcess) Start() {
}
}
func (p namedProcess) Stop() {
func (p *namedProcess) Stop() {
close(p.scrapeChan)
}
func (p namedProcess) scrape(ch chan<- prometheus.Metric) {
func (p *namedProcess) scrape(ch chan<- prometheus.Metric) {
_, groups, err := p.Update(p.fs.AllProcs())
if err != nil {
klog.Warningf("unexpected error obtaining nginx process info: %v", err)
return
}
for _, gcounts := range groups {
for i := range groups {
gcounts := groups[i]
ch <- prometheus.MustNewConstMetric(p.data.numProcs,
prometheus.GaugeValue, float64(gcounts.Procs))
ch <- prometheus.MustNewConstMetric(p.data.memResidentbytes,

View file

@ -48,8 +48,11 @@ func TestProcessCollector(t *testing.T) {
done := make(chan struct{})
go func() {
cmd.Wait() //nolint:errcheck
status := cmd.ProcessState.Sys().(syscall.WaitStatus)
cmd.Wait() //nolint:errcheck // Ignore the error
status, ok := cmd.ProcessState.Sys().(syscall.WaitStatus)
if !ok {
t.Errorf("unexpected type: %T", cmd.ProcessState.Sys())
}
if status.Signaled() {
t.Logf("Signal: %v", status.Signal())
} else {
@ -69,7 +72,7 @@ func TestProcessCollector(t *testing.T) {
defer func() {
cm.Stop()
cmd.Process.Kill() //nolint:errcheck
cmd.Process.Kill() //nolint:errcheck // Ignore the error
<-done
close(done)
}()

View file

@ -44,14 +44,11 @@ type socketData struct {
Latency float64 `json:"upstreamLatency"`
HeaderTime float64 `json:"upstreamHeaderTime"`
ResponseTime float64 `json:"upstreamResponseTime"`
//ResponseLength float64 `json:"upstreamResponseLength"`
//Status string `json:"upstreamStatus"`
Namespace string `json:"namespace"`
Ingress string `json:"ingress"`
Service string `json:"service"`
Canary string `json:"canary"`
Path string `json:"path"`
Namespace string `json:"namespace"`
Ingress string `json:"ingress"`
Service string `json:"service"`
Canary string `json:"canary"`
Path string `json:"path"`
}
// HistogramBuckets allow customizing prometheus histogram buckets values
@ -89,19 +86,17 @@ type SocketCollector struct {
reportStatusClasses bool
}
var (
requestTags = []string{
"status",
var requestTags = []string{
"status",
"method",
"path",
"method",
"path",
"namespace",
"ingress",
"service",
"canary",
}
)
"namespace",
"ingress",
"service",
"canary",
}
// DefObjectives was removed in https://github.com/prometheus/client_golang/pull/262
// updating the library to latest version changed the output of the metrics
@ -112,6 +107,7 @@ var defObjectives = map[float64]float64{0.5: 0.05, 0.9: 0.01, 0.99: 0.001}
func NewSocketCollector(pod, namespace, class string, metricsPerHost, reportStatusClasses bool, buckets HistogramBuckets, excludeMetrics []string) (*SocketCollector, error) {
socket := "/tmp/nginx/prometheus-nginx.socket"
// unix sockets must be unlink()ed before being used
//nolint:errcheck // Ignore unlink error
_ = syscall.Unlink(socket)
listener, err := net.Listen("unix", socket)
@ -119,7 +115,7 @@ func NewSocketCollector(pod, namespace, class string, metricsPerHost, reportStat
return nil, err
}
err = os.Chmod(socket, 0777) // #nosec
err = os.Chmod(socket, 0o777) // #nosec
if err != nil {
return nil, err
}
@ -152,7 +148,7 @@ func NewSocketCollector(pod, namespace, class string, metricsPerHost, reportStat
reportStatusClasses: reportStatusClasses,
connectTime: histogramMetric(
prometheus.HistogramOpts{
&prometheus.HistogramOpts{
Name: "connect_duration_seconds",
Help: "The time spent on establishing a connection with the upstream server",
Namespace: PrometheusNamespace,
@ -165,7 +161,7 @@ func NewSocketCollector(pod, namespace, class string, metricsPerHost, reportStat
),
headerTime: histogramMetric(
prometheus.HistogramOpts{
&prometheus.HistogramOpts{
Name: "header_duration_seconds",
Help: "The time spent on receiving first header from the upstream server",
Namespace: PrometheusNamespace,
@ -177,7 +173,7 @@ func NewSocketCollector(pod, namespace, class string, metricsPerHost, reportStat
mm,
),
responseTime: histogramMetric(
prometheus.HistogramOpts{
&prometheus.HistogramOpts{
Name: "response_duration_seconds",
Help: "The time spent on receiving the response from the upstream server",
Namespace: PrometheusNamespace,
@ -190,7 +186,7 @@ func NewSocketCollector(pod, namespace, class string, metricsPerHost, reportStat
),
requestTime: histogramMetric(
prometheus.HistogramOpts{
&prometheus.HistogramOpts{
Name: "request_duration_seconds",
Help: "The request processing time in milliseconds",
Namespace: PrometheusNamespace,
@ -203,7 +199,7 @@ func NewSocketCollector(pod, namespace, class string, metricsPerHost, reportStat
),
responseLength: histogramMetric(
prometheus.HistogramOpts{
&prometheus.HistogramOpts{
Name: "response_size",
Help: "The response length (including request line, header, and request body)",
Namespace: PrometheusNamespace,
@ -216,7 +212,7 @@ func NewSocketCollector(pod, namespace, class string, metricsPerHost, reportStat
),
requestLength: histogramMetric(
prometheus.HistogramOpts{
&prometheus.HistogramOpts{
Name: "request_size",
Help: "The request length (including request line, header, and request body)",
Namespace: PrometheusNamespace,
@ -229,7 +225,7 @@ func NewSocketCollector(pod, namespace, class string, metricsPerHost, reportStat
),
requests: counterMetric(
prometheus.CounterOpts{
&prometheus.CounterOpts{
Name: "requests",
Help: "The total number of client requests",
Namespace: PrometheusNamespace,
@ -241,7 +237,7 @@ func NewSocketCollector(pod, namespace, class string, metricsPerHost, reportStat
),
bytesSent: histogramMetric(
prometheus.HistogramOpts{
&prometheus.HistogramOpts{
Name: "bytes_sent",
Help: "DEPRECATED The number of bytes sent to a client",
Namespace: PrometheusNamespace,
@ -254,7 +250,7 @@ func NewSocketCollector(pod, namespace, class string, metricsPerHost, reportStat
),
upstreamLatency: summaryMetric(
prometheus.SummaryOpts{
&prometheus.SummaryOpts{
Name: "ingress_upstream_latency_seconds",
Help: "DEPRECATED Upstream service latency per Ingress",
Namespace: PrometheusNamespace,
@ -279,36 +275,36 @@ func containsMetric(excludeMetrics map[string]struct{}, name string) bool {
return false
}
func summaryMetric(opts prometheus.SummaryOpts, requestTags []string, excludeMetrics map[string]struct{}, metricMapping metricMapping) *prometheus.SummaryVec {
func summaryMetric(opts *prometheus.SummaryOpts, requestTags []string, excludeMetrics map[string]struct{}, metricMapping metricMapping) *prometheus.SummaryVec {
if containsMetric(excludeMetrics, opts.Name) {
return nil
}
m := prometheus.NewSummaryVec(
opts,
*opts,
requestTags,
)
metricMapping[prometheus.BuildFQName(PrometheusNamespace, "", opts.Name)] = m
return m
}
func counterMetric(opts prometheus.CounterOpts, requestTags []string, excludeMetrics map[string]struct{}, metricMapping metricMapping) *prometheus.CounterVec {
func counterMetric(opts *prometheus.CounterOpts, requestTags []string, excludeMetrics map[string]struct{}, metricMapping metricMapping) *prometheus.CounterVec {
if containsMetric(excludeMetrics, opts.Name) {
return nil
}
m := prometheus.NewCounterVec(
opts,
*opts,
requestTags,
)
metricMapping[prometheus.BuildFQName(PrometheusNamespace, "", opts.Name)] = m
return m
}
func histogramMetric(opts prometheus.HistogramOpts, requestTags []string, excludeMetrics map[string]struct{}, metricMapping metricMapping) *prometheus.HistogramVec {
func histogramMetric(opts *prometheus.HistogramOpts, requestTags []string, excludeMetrics map[string]struct{}, metricMapping metricMapping) *prometheus.HistogramVec {
if containsMetric(excludeMetrics, opts.Name) {
return nil
}
m := prometheus.NewHistogramVec(
opts,
*opts,
requestTags,
)
metricMapping[prometheus.BuildFQName(PrometheusNamespace, "", opts.Name)] = m
@ -326,7 +322,8 @@ func (sc *SocketCollector) handleMessage(msg []byte) {
return
}
for _, stats := range statsBatch {
for i := range statsBatch {
stats := &statsBatch[i]
if sc.metricsPerHost && !sc.hosts.Has(stats.Host) {
klog.V(3).InfoS("Skipping metric for host not being served", "host", stats.Host)
continue
@ -543,14 +540,14 @@ func (sc *SocketCollector) RemoveMetrics(ingresses []string, registry prometheus
}
// Describe implements prometheus.Collector
func (sc SocketCollector) Describe(ch chan<- *prometheus.Desc) {
func (sc *SocketCollector) Describe(ch chan<- *prometheus.Desc) {
for _, metric := range sc.metricMapping {
metric.Describe(ch)
}
}
// Collect implements the prometheus.Collector interface.
func (sc SocketCollector) Collect(ch chan<- prometheus.Metric) {
func (sc *SocketCollector) Collect(ch chan<- prometheus.Metric) {
for _, metric := range sc.metricMapping {
metric.Collect(ch)
}

View file

@ -30,6 +30,7 @@ import (
func TestNewUDPLogListener(t *testing.T) {
var count uint64
//nolint:unparam // Unused `message` param is required by the handleMessages function
fn := func(message []byte) {
atomic.AddUint64(&count, 1)
}
@ -57,7 +58,10 @@ func TestNewUDPLogListener(t *testing.T) {
}
}()
conn, _ := net.Dial("unix", tmpFile)
conn, err := net.Dial("unix", tmpFile)
if err != nil {
t.Errorf("unexpected error connecting to unix socket: %v", err)
}
if _, err := conn.Write([]byte("message")); err != nil {
t.Errorf("unexpected error writing to unix socket: %v", err)
}
@ -70,7 +74,6 @@ func TestNewUDPLogListener(t *testing.T) {
}
func TestCollector(t *testing.T) {
buckets := struct {
TimeBuckets []float64
LengthBuckets []float64

View file

@ -31,7 +31,7 @@ import (
// GatherAndCompare retrieves all metrics exposed by a collector and compares it
// to an expected output in the Prometheus text exposition format.
// metricNames allows only comparing the given metrics. All are compared if it's nil.
func GatherAndCompare(c prometheus.Collector, expected string, metricNames []string, reg prometheus.Gatherer) error {
func GatherAndCompare(_ prometheus.Collector, expected string, metricNames []string, reg prometheus.Gatherer) error {
expected = removeUnusedWhitespace(expected)
metrics, err := reg.Gather()
@ -77,9 +77,7 @@ metric output does not match expectation; want:
got:
'%s'
`, buf2.String(), buf1.String())
'%s'`, buf2.String(), buf1.String())
}
return nil
}