Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ linters:
default: none
enable:
- ineffassign
- unused
- govet
#- errcheck
- staticcheck
exclusions:
generated: lax
paths:
Expand Down
2 changes: 1 addition & 1 deletion pkg/custompluginmonitor/types/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ func (cpc CustomPluginConfig) Validate() error {
}
}
if !defaultConditionExists {
return fmt.Errorf("Permanent problem %s does not have preset default condition.", conditionType)
return fmt.Errorf("permanent problem %s does not have preset default condition", conditionType)
}
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/exporters/k8sexporter/condition/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ func (c *conditionManager) syncLoop(ctx context.Context) {
c.sync(ctx)
}
case <-ctx.Done():
break
return
}
}
}
Expand Down
3 changes: 1 addition & 2 deletions pkg/exporters/k8sexporter/condition/manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,7 @@ func TestNeedUpdates(t *testing.T) {
t.Log(tc.name)
if tc.condition != "" {
// Guarantee that the time advances before creating a new condition.
for now := time.Now(); now == time.Now(); {
}
time.Sleep(time.Nanosecond)
c = newTestCondition(tc.condition)
}
m.UpdateCondition(c)
Expand Down
2 changes: 1 addition & 1 deletion pkg/exporters/k8sexporter/problemclient/configs.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ func getKubeClientConfig(uri *url.URL) (*kube_rest.Config, error) {
kubeConfig.GroupVersion = &schema.GroupVersion{Version: APIVersion}
kubeConfig.Insecure = configOverrides.ClusterInfo.InsecureSkipTLSVerify
if configOverrides.ClusterInfo.InsecureSkipTLSVerify {
kubeConfig.TLSClientConfig.CAFile = ""
kubeConfig.CAFile = ""
}
} else {
authFile := ""
Expand Down
1 change: 0 additions & 1 deletion pkg/exporters/prometheusexporter/prometheus_exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,5 +56,4 @@ func NewExporterOrDie(npdo *options.NodeProblemDetectorOptions) types.Exporter {
// ExportProblems does nothing.
// Prometheus exporter only exports metrics.
func (pe *prometheusExporter) ExportProblems(status *types.Status) {
return
}
12 changes: 8 additions & 4 deletions pkg/exporters/stackdriver/gce/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ limitations under the License.
package gce

import (
"context"

"cloud.google.com/go/compute/metadata"
"k8s.io/klog/v2"
)
Expand All @@ -38,26 +40,28 @@ func (md *Metadata) HasMissingField() bool {
func (md *Metadata) PopulateFromGCE() error {
var err error
klog.Info("Fetching GCE metadata from metadata server")
// Need to be initialized with a context at the main.
ctx := context.TODO()
if md.ProjectID == "" {
md.ProjectID, err = metadata.ProjectID()
md.ProjectID, err = metadata.ProjectIDWithContext(ctx)
if err != nil {
return err
}
}
if md.Zone == "" {
md.Zone, err = metadata.Zone()
md.Zone, err = metadata.ZoneWithContext(ctx)
if err != nil {
return err
}
}
if md.InstanceID == "" {
md.InstanceID, err = metadata.InstanceID()
md.InstanceID, err = metadata.InstanceIDWithContext(ctx)
if err != nil {
return err
}
}
if md.InstanceName == "" {
md.InstanceName, err = metadata.InstanceName()
md.InstanceName, err = metadata.InstanceNameWithContext(ctx)
if err != nil {
return err
}
Expand Down
1 change: 0 additions & 1 deletion pkg/exporters/stackdriver/stackdriver_exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,6 @@ func (se *stackdriverExporter) populateMetadataOrDie() {
// ExportProblems does nothing.
// Stackdriver exporter only exports metrics.
func (se *stackdriverExporter) ExportProblems(status *types.Status) {
return
}

type commandLineOptions struct {
Expand Down
4 changes: 2 additions & 2 deletions pkg/problemmetrics/problem_metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ func NewProblemMetricsManagerOrDie() *ProblemMetricsManager {
// IncrementProblemCounter increments the value of a problem counter.
func (pmm *ProblemMetricsManager) IncrementProblemCounter(reason string, count int64) error {
if pmm.problemCounter == nil {
return errors.New("problem counter is being incremented before initialized.")
return errors.New("problem counter is being incremented before initialized")
}

return pmm.problemCounter.Record(map[string]string{"reason": reason}, count)
Expand All @@ -87,7 +87,7 @@ func (pmm *ProblemMetricsManager) IncrementProblemCounter(reason string, count i
// SetProblemGauge sets the value of a problem gauge.
func (pmm *ProblemMetricsManager) SetProblemGauge(problemType string, reason string, value bool) error {
if pmm.problemGauge == nil {
return errors.New("problem gauge is being set before initialized.")
return errors.New("problem gauge is being set before initialized")
}

pmm.problemTypeToReasonMutex.Lock()
Expand Down
4 changes: 2 additions & 2 deletions pkg/systemlogmonitor/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ func (mc *MonitorConfig) ApplyDefaultConfiguration() {
if mc.EnableMetricsReporting == nil {
mc.EnableMetricsReporting = &defaultEnableMetricsReporting
}
if mc.WatcherConfig.Lookback == "" {
mc.WatcherConfig.Lookback = defaultLookback
if mc.Lookback == "" {
mc.Lookback = defaultLookback
}
}

Expand Down
4 changes: 2 additions & 2 deletions pkg/systemlogmonitor/log_buffer.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ type logBuffer struct {
// lines of patterns we support.
func NewLogBuffer(maxLines int) *logBuffer {
return &logBuffer{
buffer: make([]*types.Log, maxLines, maxLines),
msg: make([]string, maxLines, maxLines),
buffer: make([]*types.Log, maxLines),
msg: make([]string, maxLines),
max: maxLines,
}
}
Expand Down
51 changes: 25 additions & 26 deletions pkg/systemlogmonitor/log_monitor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import (

"k8s.io/node-problem-detector/pkg/problemdaemon"
"k8s.io/node-problem-detector/pkg/problemmetrics"
logtypes "k8s.io/node-problem-detector/pkg/systemlogmonitor/types"
systemlogtypes "k8s.io/node-problem-detector/pkg/systemlogmonitor/types"
"k8s.io/node-problem-detector/pkg/types"
"k8s.io/node-problem-detector/pkg/util"
Expand Down Expand Up @@ -58,7 +57,7 @@ func TestGenerateStatusForConditions(t *testing.T) {
Transition: time.Unix(500, 500),
},
}
logs := []*logtypes.Log{
logs := []*systemlogtypes.Log{
{
Timestamp: time.Unix(1000, 1000),
Message: "test message 1",
Expand All @@ -69,12 +68,12 @@ func TestGenerateStatusForConditions(t *testing.T) {
},
}
for c, test := range []struct {
rule logtypes.Rule
rule systemlogtypes.Rule
expected types.Status
}{
// Do not need Pattern because we don't do pattern match in this test
{
rule: logtypes.Rule{
rule: systemlogtypes.Rule{
Type: types.Perm,
Condition: testConditionA,
Reason: "test reason",
Expand Down Expand Up @@ -102,7 +101,7 @@ func TestGenerateStatusForConditions(t *testing.T) {
},
// Should not update transition time when status and reason are not changed.
{
rule: logtypes.Rule{
rule: systemlogtypes.Rule{
Type: types.Perm,
Condition: testConditionA,
Reason: "initial reason",
Expand All @@ -121,7 +120,7 @@ func TestGenerateStatusForConditions(t *testing.T) {
},
},
{
rule: logtypes.Rule{
rule: systemlogtypes.Rule{
Type: types.Temp,
Reason: "test reason",
},
Expand Down Expand Up @@ -157,19 +156,19 @@ func TestGenerateStatusForMetrics(t *testing.T) {
testCases := []struct {
name string
conditions []types.Condition
triggeredRules []logtypes.Rule
triggeredRules []systemlogtypes.Rule
expectedMetrics []metrics.Int64MetricRepresentation
}{
{
name: "one temporary problem that has not happened",
conditions: []types.Condition{},
triggeredRules: []logtypes.Rule{},
triggeredRules: []systemlogtypes.Rule{},
expectedMetrics: []metrics.Int64MetricRepresentation{},
},
{
name: "one temporary problem happened once",
conditions: []types.Condition{},
triggeredRules: []logtypes.Rule{
triggeredRules: []systemlogtypes.Rule{
{
Type: types.Temp,
Reason: "problem reason foo",
Expand All @@ -186,7 +185,7 @@ func TestGenerateStatusForMetrics(t *testing.T) {
{
name: "one temporary problem happened twice",
conditions: []types.Condition{},
triggeredRules: []logtypes.Rule{
triggeredRules: []systemlogtypes.Rule{
{
Type: types.Temp,
Reason: "problem reason foo",
Expand All @@ -207,7 +206,7 @@ func TestGenerateStatusForMetrics(t *testing.T) {
{
name: "two different temporary problems happened",
conditions: []types.Condition{},
triggeredRules: []logtypes.Rule{
triggeredRules: []systemlogtypes.Rule{
{
Type: types.Temp,
Reason: "problem reason foo",
Expand Down Expand Up @@ -238,7 +237,7 @@ func TestGenerateStatusForMetrics(t *testing.T) {
Status: types.False,
},
},
triggeredRules: []logtypes.Rule{
triggeredRules: []systemlogtypes.Rule{
{
Type: types.Perm,
Condition: "ConditionA",
Expand Down Expand Up @@ -266,7 +265,7 @@ func TestGenerateStatusForMetrics(t *testing.T) {
Status: types.False,
},
},
triggeredRules: []logtypes.Rule{
triggeredRules: []systemlogtypes.Rule{
{
Type: types.Perm,
Condition: "ConditionA",
Expand Down Expand Up @@ -299,7 +298,7 @@ func TestGenerateStatusForMetrics(t *testing.T) {
Status: types.False,
},
},
triggeredRules: []logtypes.Rule{
triggeredRules: []systemlogtypes.Rule{
{
Type: types.Perm,
Condition: "ConditionA",
Expand Down Expand Up @@ -346,7 +345,7 @@ func TestGenerateStatusForMetrics(t *testing.T) {
Status: types.False,
},
},
triggeredRules: []logtypes.Rule{
triggeredRules: []systemlogtypes.Rule{
{
Type: types.Perm,
Condition: "ConditionA",
Expand Down Expand Up @@ -397,7 +396,7 @@ func TestGenerateStatusForMetrics(t *testing.T) {
problemmetrics.GlobalProblemMetricsManager = fakePMM

for _, rule := range test.triggeredRules {
l.generateStatus([]*logtypes.Log{{}}, rule)
l.generateStatus([]*systemlogtypes.Log{{}}, rule)
}

gotMetrics := append(fakeProblemCounter.ListMetrics(), fakeProblemGauge.ListMetrics()...)
Expand All @@ -411,17 +410,17 @@ func TestGenerateStatusForMetrics(t *testing.T) {
func TestInitializeProblemMetricsOrDie(t *testing.T) {
testCases := []struct {
name string
rules []logtypes.Rule
rules []systemlogtypes.Rule
expectedMetrics []metrics.Int64MetricRepresentation
}{
{
name: "no problem type at all",
rules: []logtypes.Rule{},
rules: []systemlogtypes.Rule{},
expectedMetrics: []metrics.Int64MetricRepresentation{},
},
{
name: "one type of temporary problem",
rules: []logtypes.Rule{
rules: []systemlogtypes.Rule{
{
Type: types.Temp,
Reason: "problem reason foo",
Expand All @@ -437,7 +436,7 @@ func TestInitializeProblemMetricsOrDie(t *testing.T) {
},
{
name: "one type of permanent problem",
rules: []logtypes.Rule{
rules: []systemlogtypes.Rule{
{
Type: types.Perm,
Condition: "ConditionA",
Expand All @@ -459,7 +458,7 @@ func TestInitializeProblemMetricsOrDie(t *testing.T) {
},
{
name: "duplicate temporary problem types",
rules: []logtypes.Rule{
rules: []systemlogtypes.Rule{
{
Type: types.Temp,
Reason: "problem reason foo",
Expand All @@ -479,7 +478,7 @@ func TestInitializeProblemMetricsOrDie(t *testing.T) {
},
{
name: "multiple temporary problem types",
rules: []logtypes.Rule{
rules: []systemlogtypes.Rule{
{
Type: types.Temp,
Reason: "problem reason foo",
Expand All @@ -504,7 +503,7 @@ func TestInitializeProblemMetricsOrDie(t *testing.T) {
},
{
name: "multiple permanent problem types with same condition",
rules: []logtypes.Rule{
rules: []systemlogtypes.Rule{
{
Type: types.Perm,
Condition: "ConditionA",
Expand Down Expand Up @@ -541,7 +540,7 @@ func TestInitializeProblemMetricsOrDie(t *testing.T) {
},
{
name: "multiple permanent problem types with different conditions",
rules: []logtypes.Rule{
rules: []systemlogtypes.Rule{
{
Type: types.Perm,
Condition: "ConditionA",
Expand Down Expand Up @@ -578,7 +577,7 @@ func TestInitializeProblemMetricsOrDie(t *testing.T) {
},
{
name: "duplicate permanent problem types",
rules: []logtypes.Rule{
rules: []systemlogtypes.Rule{
{
Type: types.Perm,
Condition: "ConditionA",
Expand All @@ -605,7 +604,7 @@ func TestInitializeProblemMetricsOrDie(t *testing.T) {
},
{
name: "mixture of temporary and permanent problem types",
rules: []logtypes.Rule{
rules: []systemlogtypes.Rule{
{
Type: types.Temp,
Reason: "problem reason foo",
Expand Down
2 changes: 1 addition & 1 deletion pkg/util/helpers_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func getOSVersion(osReleasePath string) (string, error) {
case "flatcar":
return getDebianVersion(osReleaseMap), nil
default:
return "", fmt.Errorf("Unsupported ID in /etc/os-release: %q", osReleaseMap["ID"])
return "", fmt.Errorf("unsupported ID in /etc/os-release: %q", osReleaseMap["ID"])
}
}

Expand Down
7 changes: 4 additions & 3 deletions pkg/util/metrics/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,11 +87,12 @@ func ParsePrometheusMetrics(metricsText string) ([]Float64MetricRepresentation,
}

var value float64
if *metricFamily.Type == pcm.MetricType_COUNTER {
switch *metricFamily.Type {
case pcm.MetricType_COUNTER:
value = *metric.Counter.Value
} else if *metricFamily.Type == pcm.MetricType_GAUGE {
case pcm.MetricType_GAUGE:
value = *metric.Gauge.Value
} else {
default:
return metrics, fmt.Errorf("unexpected MetricType %s for metric %s",
pcm.MetricType_name[int32(*metricFamily.Type)], *metricFamily.Name)
}
Expand Down