|
| 1 | +/* |
| 2 | +Copyright 2017 The Kubernetes Authors All rights reserved. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package custompluginmonitor |
| 18 | + |
| 19 | +import ( |
| 20 | + "encoding/json" |
| 21 | + "io/ioutil" |
| 22 | + "time" |
| 23 | + |
| 24 | + "github.com/golang/glog" |
| 25 | + |
| 26 | + "k8s.io/node-problem-detector/pkg/custompluginmonitor/plugin" |
| 27 | + cpmtypes "k8s.io/node-problem-detector/pkg/custompluginmonitor/types" |
| 28 | + "k8s.io/node-problem-detector/pkg/types" |
| 29 | + "k8s.io/node-problem-detector/pkg/util/tomb" |
| 30 | +) |
| 31 | + |
| 32 | +type customPluginMonitor struct { |
| 33 | + config cpmtypes.CustomPluginConfig |
| 34 | + conditions []types.Condition |
| 35 | + plugin *plugin.Plugin |
| 36 | + resultChan <-chan cpmtypes.Result |
| 37 | + statusChan chan *types.Status |
| 38 | + tomb *tomb.Tomb |
| 39 | +} |
| 40 | + |
| 41 | +// NewCustomPluginMonitorOrDie create a new customPluginMonitor, panic if error occurs. |
| 42 | +func NewCustomPluginMonitorOrDie(configPath string) types.Monitor { |
| 43 | + c := &customPluginMonitor{ |
| 44 | + tomb: tomb.NewTomb(), |
| 45 | + } |
| 46 | + f, err := ioutil.ReadFile(configPath) |
| 47 | + if err != nil { |
| 48 | + glog.Fatalf("Failed to read configuration file %q: %v", configPath, err) |
| 49 | + } |
| 50 | + err = json.Unmarshal(f, &c.config) |
| 51 | + if err != nil { |
| 52 | + glog.Fatalf("Failed to unmarshal configuration file %q: %v", configPath, err) |
| 53 | + } |
| 54 | + // Apply configurations |
| 55 | + err = (&c.config).ApplyConfiguration() |
| 56 | + if err != nil { |
| 57 | + glog.Fatalf("Failed to apply configuration for %q: %v", configPath, err) |
| 58 | + } |
| 59 | + |
| 60 | + // Validate configurations |
| 61 | + err = c.config.Validate() |
| 62 | + if err != nil { |
| 63 | + glog.Fatalf("Failed to validate custom plugin config %+v: %v", c.config, err) |
| 64 | + } |
| 65 | + |
| 66 | + glog.Infof("Finish parsing custom plugin monitor config file: %+v", c.config) |
| 67 | + |
| 68 | + c.plugin = plugin.NewPlugin(c.config) |
| 69 | + // A 1000 size channel should be big enough. |
| 70 | + c.statusChan = make(chan *types.Status, 1000) |
| 71 | + return c |
| 72 | +} |
| 73 | + |
| 74 | +func (c *customPluginMonitor) Start() (<-chan *types.Status, error) { |
| 75 | + glog.Info("Start custom plugin monitor") |
| 76 | + go c.plugin.Run() |
| 77 | + go c.monitorLoop() |
| 78 | + return c.statusChan, nil |
| 79 | +} |
| 80 | + |
| 81 | +func (c *customPluginMonitor) Stop() { |
| 82 | + glog.Info("Stop custom plugin monitor") |
| 83 | + c.tomb.Stop() |
| 84 | +} |
| 85 | + |
| 86 | +// monitorLoop is the main loop of log monitor. |
| 87 | +func (c *customPluginMonitor) monitorLoop() { |
| 88 | + c.initializeStatus() |
| 89 | + |
| 90 | + resultChan := c.plugin.GetResultChan() |
| 91 | + |
| 92 | + for { |
| 93 | + select { |
| 94 | + case result := <-resultChan: |
| 95 | + glog.V(3).Infof("Receive new plugin result: %+v", result) |
| 96 | + status := c.generateStatus(result) |
| 97 | + glog.Infof("New status generated: %+v", status) |
| 98 | + c.statusChan <- status |
| 99 | + case <-c.tomb.Stopping(): |
| 100 | + c.plugin.Stop() |
| 101 | + glog.Infof("Custom plugin monitor stopped") |
| 102 | + c.tomb.Done() |
| 103 | + break |
| 104 | + } |
| 105 | + } |
| 106 | +} |
| 107 | + |
| 108 | +// generateStatus generates status from the plugin check result. |
| 109 | +func (c *customPluginMonitor) generateStatus(result cpmtypes.Result) *types.Status { |
| 110 | + timestamp := time.Now() |
| 111 | + var events []types.Event |
| 112 | + if result.Rule.Type == types.Temp { |
| 113 | + // For temporary error only generate event when exit status is above warning |
| 114 | + if result.ExitStatus >= cpmtypes.NonOK { |
| 115 | + events = append(events, types.Event{ |
| 116 | + Severity: types.Warn, |
| 117 | + Timestamp: timestamp, |
| 118 | + Reason: result.Rule.Reason, |
| 119 | + Message: result.Message, |
| 120 | + }) |
| 121 | + } |
| 122 | + } else { |
| 123 | + // For permanent error changes the condition |
| 124 | + for i := range c.conditions { |
| 125 | + condition := &c.conditions[i] |
| 126 | + if condition.Type == result.Rule.Condition { |
| 127 | + status := result.ExitStatus >= cpmtypes.NonOK |
| 128 | + if condition.Status != status || condition.Reason != result.Rule.Reason { |
| 129 | + condition.Transition = timestamp |
| 130 | + condition.Message = result.Message |
| 131 | + } |
| 132 | + condition.Status = status |
| 133 | + condition.Reason = result.Rule.Reason |
| 134 | + break |
| 135 | + } |
| 136 | + } |
| 137 | + } |
| 138 | + return &types.Status{ |
| 139 | + Source: c.config.Source, |
| 140 | + // TODO(random-liu): Aggregate events and conditions and then do periodically report. |
| 141 | + Events: events, |
| 142 | + Conditions: c.conditions, |
| 143 | + } |
| 144 | +} |
| 145 | + |
| 146 | +// initializeStatus initializes the internal condition and also reports it to the node problem detector. |
| 147 | +func (c *customPluginMonitor) initializeStatus() { |
| 148 | + // Initialize the default node conditions |
| 149 | + c.conditions = initialConditions(c.config.DefaultConditions) |
| 150 | + glog.Infof("Initialize condition generated: %+v", c.conditions) |
| 151 | + // Update the initial status |
| 152 | + c.statusChan <- &types.Status{ |
| 153 | + Source: c.config.Source, |
| 154 | + Conditions: c.conditions, |
| 155 | + } |
| 156 | +} |
| 157 | + |
| 158 | +func initialConditions(defaults []types.Condition) []types.Condition { |
| 159 | + conditions := make([]types.Condition, len(defaults)) |
| 160 | + copy(conditions, defaults) |
| 161 | + for i := range conditions { |
| 162 | + // TODO(random-liu): Validate default conditions |
| 163 | + conditions[i].Status = false |
| 164 | + conditions[i].Transition = time.Now() |
| 165 | + } |
| 166 | + return conditions |
| 167 | +} |
0 commit comments