-
Notifications
You must be signed in to change notification settings - Fork 606
/
config.go
169 lines (143 loc) · 6.05 KB
/
config.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/*
Copyright 2017 The Kubernetes Authors All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package types
import (
"fmt"
"os"
"time"
"k8s.io/node-problem-detector/pkg/types"
)
var (
defaultGlobalTimeout = 5 * time.Second
defaultGlobalTimeoutString = defaultGlobalTimeout.String()
defaultInvokeInterval = 30 * time.Second
defaultInvokeIntervalString = defaultInvokeInterval.String()
defaultMaxOutputLength = 80
defaultConcurrency = 3
defaultMessageChangeBasedConditionUpdate = false
defaultEnableMetricsReporting = true
defaultSkipInitialStatus = false
customPluginName = "custom"
)
type pluginGlobalConfig struct {
// InvokeIntervalString is the interval string at which plugins will be invoked.
InvokeIntervalString *string `json:"invoke_interval,omitempty"`
// TimeoutString is the global plugin execution timeout string.
TimeoutString *string `json:"timeout,omitempty"`
// InvokeInterval is the interval at which plugins will be invoked.
InvokeInterval *time.Duration `json:"-"`
// Timeout is the global plugin execution timeout.
Timeout *time.Duration `json:"-"`
// MaxOutputLength is the maximum plugin output message length.
MaxOutputLength *int `json:"max_output_length,omitempty"`
// Concurrency is the number of concurrent running plugins.
Concurrency *int `json:"concurrency,omitempty"`
// EnableMessageChangeBasedConditionUpdate indicates whether NPD should enable message change based condition update.
EnableMessageChangeBasedConditionUpdate *bool `json:"enable_message_change_based_condition_update,omitempty"`
// SkipInitialStatus prevents the first status update with default conditions
SkipInitialStatus *bool `json:"skip_initial_status,omitempty"`
}
// Custom plugin config is the configuration of custom plugin monitor.
type CustomPluginConfig struct {
// Plugin is the name of plugin which is currently used.
// Currently supported: custom.
Plugin string `json:"plugin,omitempty"`
// PluginConfig is global plugin configuration.
PluginGlobalConfig pluginGlobalConfig `json:"pluginConfig,omitempty"`
// Source is the source name of the custom plugin monitor
Source string `json:"source"`
// DefaultConditions are the default states of all the conditions custom plugin monitor should handle.
DefaultConditions []types.Condition `json:"conditions"`
// Rules are the rules custom plugin monitor will follow to parse and invoke plugins.
Rules []*CustomRule `json:"rules"`
// EnableMetricsReporting describes whether to report problems as metrics or not.
EnableMetricsReporting *bool `json:"metricsReporting,omitempty"`
}
// ApplyConfiguration applies default configurations.
func (cpc *CustomPluginConfig) ApplyConfiguration() error {
if cpc.PluginGlobalConfig.TimeoutString == nil {
cpc.PluginGlobalConfig.TimeoutString = &defaultGlobalTimeoutString
}
timeout, err := time.ParseDuration(*cpc.PluginGlobalConfig.TimeoutString)
if err != nil {
return fmt.Errorf("error in parsing global timeout %q: %v", *cpc.PluginGlobalConfig.TimeoutString, err)
}
cpc.PluginGlobalConfig.Timeout = &timeout
if cpc.PluginGlobalConfig.InvokeIntervalString == nil {
cpc.PluginGlobalConfig.InvokeIntervalString = &defaultInvokeIntervalString
}
invokeInterval, err := time.ParseDuration(*cpc.PluginGlobalConfig.InvokeIntervalString)
if err != nil {
return fmt.Errorf("error in parsing invoke interval %q: %v", *cpc.PluginGlobalConfig.InvokeIntervalString, err)
}
cpc.PluginGlobalConfig.InvokeInterval = &invokeInterval
if cpc.PluginGlobalConfig.MaxOutputLength == nil {
cpc.PluginGlobalConfig.MaxOutputLength = &defaultMaxOutputLength
}
if cpc.PluginGlobalConfig.Concurrency == nil {
cpc.PluginGlobalConfig.Concurrency = &defaultConcurrency
}
if cpc.PluginGlobalConfig.EnableMessageChangeBasedConditionUpdate == nil {
cpc.PluginGlobalConfig.EnableMessageChangeBasedConditionUpdate = &defaultMessageChangeBasedConditionUpdate
}
if cpc.PluginGlobalConfig.SkipInitialStatus == nil {
cpc.PluginGlobalConfig.SkipInitialStatus = &defaultSkipInitialStatus
}
for _, rule := range cpc.Rules {
if rule.TimeoutString != nil {
timeout, err := time.ParseDuration(*rule.TimeoutString)
if err != nil {
return fmt.Errorf("error in parsing rule timeout %+v: %v", rule, err)
}
rule.Timeout = &timeout
}
}
if cpc.EnableMetricsReporting == nil {
cpc.EnableMetricsReporting = &defaultEnableMetricsReporting
}
return nil
}
// Validate verifies whether the settings in CustomPluginConfig are valid.
func (cpc CustomPluginConfig) Validate() error {
if cpc.Plugin != customPluginName {
return fmt.Errorf("NPD does not support %q plugin for now. Only support \"custom\"", cpc.Plugin)
}
for _, rule := range cpc.Rules {
if rule.Timeout != nil && *rule.Timeout > *cpc.PluginGlobalConfig.Timeout {
return fmt.Errorf("plugin timeout is greater than global timeout. "+
"Rule: %+v. Global timeout: %v", rule, cpc.PluginGlobalConfig.Timeout)
}
}
for _, rule := range cpc.Rules {
if _, err := os.Stat(rule.Path); os.IsNotExist(err) {
return fmt.Errorf("rule path %q does not exist. Rule: %+v", rule.Path, rule)
}
}
for _, rule := range cpc.Rules {
if rule.Type != types.Perm {
continue
}
conditionType := rule.Condition
defaultConditionExists := false
for _, cond := range cpc.DefaultConditions {
if conditionType == cond.Type {
defaultConditionExists = true
break
}
}
if !defaultConditionExists {
return fmt.Errorf("Permanent problem %s does not have preset default condition.", conditionType)
}
}
return nil
}