|
| 1 | +package monitor |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "strings" |
| 7 | + "time" |
| 8 | + |
| 9 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 10 | + "k8s.io/apimachinery/pkg/runtime" |
| 11 | + "k8s.io/apimachinery/pkg/watch" |
| 12 | + "k8s.io/client-go/tools/cache" |
| 13 | + |
| 14 | + configv1 "github.com/openshift/api/config/v1" |
| 15 | + configclientset "github.com/openshift/client-go/config/clientset/versioned" |
| 16 | +) |
| 17 | + |
| 18 | +func startClusterOperatorMonitoring(ctx context.Context, m Recorder, client configclientset.Interface) { |
| 19 | + coInformer := cache.NewSharedIndexInformer( |
| 20 | + NewErrorRecordingListWatcher(m, &cache.ListWatch{ |
| 21 | + ListFunc: func(options metav1.ListOptions) (runtime.Object, error) { |
| 22 | + return client.ConfigV1().ClusterOperators().List(options) |
| 23 | + }, |
| 24 | + WatchFunc: func(options metav1.ListOptions) (watch.Interface, error) { |
| 25 | + return client.ConfigV1().ClusterOperators().Watch(options) |
| 26 | + }, |
| 27 | + }), |
| 28 | + &configv1.ClusterOperator{}, |
| 29 | + time.Hour, |
| 30 | + nil, |
| 31 | + ) |
| 32 | + |
| 33 | + coChangeFns := []func(co, oldCO *configv1.ClusterOperator) []Condition{ |
| 34 | + func(co, oldCO *configv1.ClusterOperator) []Condition { |
| 35 | + var conditions []Condition |
| 36 | + for i := range co.Status.Conditions { |
| 37 | + s := &co.Status.Conditions[i] |
| 38 | + previous := findOperatorStatusCondition(oldCO.Status.Conditions, s.Type) |
| 39 | + if previous == nil { |
| 40 | + continue |
| 41 | + } |
| 42 | + if s.Status != previous.Status { |
| 43 | + var msg string |
| 44 | + switch { |
| 45 | + case len(s.Reason) > 0 && len(s.Message) > 0: |
| 46 | + msg = fmt.Sprintf("changed %s to %s: %s: %s", s.Type, s.Status, s.Reason, s.Message) |
| 47 | + case len(s.Message) > 0: |
| 48 | + msg = fmt.Sprintf("changed %s to %s: %s", s.Type, s.Status, s.Message) |
| 49 | + default: |
| 50 | + msg = fmt.Sprintf("changed %s to %s", s.Type, s.Status) |
| 51 | + } |
| 52 | + level := Warning |
| 53 | + if s.Type == configv1.OperatorFailing && s.Status == configv1.ConditionTrue { |
| 54 | + level = Error |
| 55 | + } |
| 56 | + conditions = append(conditions, Condition{ |
| 57 | + Level: level, |
| 58 | + Locator: locateClusterOperator(co), |
| 59 | + Message: msg, |
| 60 | + }) |
| 61 | + } |
| 62 | + if changes := findOperatorVersionChange(oldCO.Status.Versions, co.Status.Versions); len(changes) > 0 { |
| 63 | + conditions = append(conditions, Condition{ |
| 64 | + Level: Info, |
| 65 | + Locator: locateClusterOperator(co), |
| 66 | + Message: fmt.Sprintf("versions: %v", strings.Join(changes, ",")), |
| 67 | + }) |
| 68 | + } |
| 69 | + } |
| 70 | + return conditions |
| 71 | + }, |
| 72 | + } |
| 73 | + |
| 74 | + startTime := time.Now().Add(-time.Minute) |
| 75 | + coInformer.AddEventHandler( |
| 76 | + cache.ResourceEventHandlerFuncs{ |
| 77 | + AddFunc: func(obj interface{}) { |
| 78 | + co, ok := obj.(*configv1.ClusterOperator) |
| 79 | + if !ok { |
| 80 | + return |
| 81 | + } |
| 82 | + // filter out old pods so our monitor doesn't send a big chunk |
| 83 | + // of co creations |
| 84 | + if co.CreationTimestamp.Time.Before(startTime) { |
| 85 | + return |
| 86 | + } |
| 87 | + m.Record(Condition{ |
| 88 | + Level: Info, |
| 89 | + Locator: locateClusterOperator(co), |
| 90 | + Message: "created", |
| 91 | + }) |
| 92 | + }, |
| 93 | + DeleteFunc: func(obj interface{}) { |
| 94 | + co, ok := obj.(*configv1.ClusterOperator) |
| 95 | + if !ok { |
| 96 | + return |
| 97 | + } |
| 98 | + m.Record(Condition{ |
| 99 | + Level: Warning, |
| 100 | + Locator: locateClusterOperator(co), |
| 101 | + Message: "deleted", |
| 102 | + }) |
| 103 | + }, |
| 104 | + UpdateFunc: func(old, obj interface{}) { |
| 105 | + co, ok := obj.(*configv1.ClusterOperator) |
| 106 | + if !ok { |
| 107 | + return |
| 108 | + } |
| 109 | + oldCO, ok := old.(*configv1.ClusterOperator) |
| 110 | + if !ok { |
| 111 | + return |
| 112 | + } |
| 113 | + if co.UID != oldCO.UID { |
| 114 | + return |
| 115 | + } |
| 116 | + for _, fn := range coChangeFns { |
| 117 | + m.Record(fn(co, oldCO)...) |
| 118 | + } |
| 119 | + }, |
| 120 | + }, |
| 121 | + ) |
| 122 | + |
| 123 | + go coInformer.Run(ctx.Done()) |
| 124 | + |
| 125 | + cvInformer := cache.NewSharedIndexInformer( |
| 126 | + NewErrorRecordingListWatcher(m, &cache.ListWatch{ |
| 127 | + ListFunc: func(options metav1.ListOptions) (runtime.Object, error) { |
| 128 | + options.FieldSelector = "metadata.name=version" |
| 129 | + return client.ConfigV1().ClusterVersions().List(options) |
| 130 | + }, |
| 131 | + WatchFunc: func(options metav1.ListOptions) (watch.Interface, error) { |
| 132 | + options.FieldSelector = "metadata.name=version" |
| 133 | + return client.ConfigV1().ClusterVersions().Watch(options) |
| 134 | + }, |
| 135 | + }), |
| 136 | + &configv1.ClusterVersion{}, |
| 137 | + time.Hour, |
| 138 | + nil, |
| 139 | + ) |
| 140 | + |
| 141 | + cvChangeFns := []func(cv, oldCV *configv1.ClusterVersion) []Condition{ |
| 142 | + func(cv, oldCV *configv1.ClusterVersion) []Condition { |
| 143 | + var conditions []Condition |
| 144 | + if len(cv.Status.History) == 0 { |
| 145 | + return nil |
| 146 | + } |
| 147 | + if len(oldCV.Status.History) == 0 { |
| 148 | + conditions = append(conditions, Condition{ |
| 149 | + Level: Warning, |
| 150 | + Locator: locateClusterVersion(cv), |
| 151 | + Message: fmt.Sprintf("cluster converging to %s", cv.Status.History[0].Version), |
| 152 | + }) |
| 153 | + return conditions |
| 154 | + } |
| 155 | + cvNew, cvOld := cv.Status.History[0], oldCV.Status.History[0] |
| 156 | + switch { |
| 157 | + case cvNew.State == configv1.CompletedUpdate && cvOld.State != cvNew.State: |
| 158 | + conditions = append(conditions, Condition{ |
| 159 | + Level: Warning, |
| 160 | + Locator: locateClusterVersion(cv), |
| 161 | + Message: fmt.Sprintf("cluster reached %s", cvNew.Version), |
| 162 | + }) |
| 163 | + case cvNew.State == configv1.PartialUpdate && cvOld.State == cvNew.State && cvOld.Image != cvNew.Image: |
| 164 | + conditions = append(conditions, Condition{ |
| 165 | + Level: Warning, |
| 166 | + Locator: locateClusterVersion(cv), |
| 167 | + Message: fmt.Sprintf("cluster upgrading to %s without completing %s", cvNew.Version, cvOld.Version), |
| 168 | + }) |
| 169 | + } |
| 170 | + return conditions |
| 171 | + }, |
| 172 | + func(cv, oldCV *configv1.ClusterVersion) []Condition { |
| 173 | + var conditions []Condition |
| 174 | + for i := range cv.Status.Conditions { |
| 175 | + s := &cv.Status.Conditions[i] |
| 176 | + previous := findOperatorStatusCondition(oldCV.Status.Conditions, s.Type) |
| 177 | + if previous == nil { |
| 178 | + continue |
| 179 | + } |
| 180 | + if s.Status != previous.Status { |
| 181 | + var msg string |
| 182 | + switch { |
| 183 | + case len(s.Reason) > 0 && len(s.Message) > 0: |
| 184 | + msg = fmt.Sprintf("changed %s to %s: %s: %s", s.Type, s.Status, s.Reason, s.Message) |
| 185 | + case len(s.Message) > 0: |
| 186 | + msg = fmt.Sprintf("changed %s to %s: %s", s.Type, s.Status, s.Message) |
| 187 | + default: |
| 188 | + msg = fmt.Sprintf("changed %s to %s", s.Type, s.Status) |
| 189 | + } |
| 190 | + level := Warning |
| 191 | + if s.Type == configv1.OperatorFailing && s.Status == configv1.ConditionTrue { |
| 192 | + level = Error |
| 193 | + } |
| 194 | + conditions = append(conditions, Condition{ |
| 195 | + Level: level, |
| 196 | + Locator: locateClusterVersion(cv), |
| 197 | + Message: msg, |
| 198 | + }) |
| 199 | + } |
| 200 | + } |
| 201 | + return conditions |
| 202 | + }, |
| 203 | + } |
| 204 | + |
| 205 | + cvInformer.AddEventHandler( |
| 206 | + cache.ResourceEventHandlerFuncs{ |
| 207 | + AddFunc: func(obj interface{}) { |
| 208 | + cv, ok := obj.(*configv1.ClusterVersion) |
| 209 | + if !ok { |
| 210 | + return |
| 211 | + } |
| 212 | + // filter out old pods so our monitor doesn't send a big chunk |
| 213 | + // of co creations |
| 214 | + if cv.CreationTimestamp.Time.Before(startTime) { |
| 215 | + return |
| 216 | + } |
| 217 | + m.Record(Condition{ |
| 218 | + Level: Info, |
| 219 | + Locator: locateClusterVersion(cv), |
| 220 | + Message: "created", |
| 221 | + }) |
| 222 | + }, |
| 223 | + DeleteFunc: func(obj interface{}) { |
| 224 | + cv, ok := obj.(*configv1.ClusterVersion) |
| 225 | + if !ok { |
| 226 | + return |
| 227 | + } |
| 228 | + m.Record(Condition{ |
| 229 | + Level: Warning, |
| 230 | + Locator: locateClusterVersion(cv), |
| 231 | + Message: "deleted", |
| 232 | + }) |
| 233 | + }, |
| 234 | + UpdateFunc: func(old, obj interface{}) { |
| 235 | + cv, ok := obj.(*configv1.ClusterVersion) |
| 236 | + if !ok { |
| 237 | + return |
| 238 | + } |
| 239 | + oldCV, ok := old.(*configv1.ClusterVersion) |
| 240 | + if !ok { |
| 241 | + return |
| 242 | + } |
| 243 | + if cv.UID != oldCV.UID { |
| 244 | + return |
| 245 | + } |
| 246 | + for _, fn := range cvChangeFns { |
| 247 | + m.Record(fn(cv, oldCV)...) |
| 248 | + } |
| 249 | + }, |
| 250 | + }, |
| 251 | + ) |
| 252 | + |
| 253 | + m.AddSampler(func(now time.Time) []*Condition { |
| 254 | + var conditions []*Condition |
| 255 | + for _, obj := range cvInformer.GetStore().List() { |
| 256 | + cv, ok := obj.(*configv1.ClusterVersion) |
| 257 | + if !ok { |
| 258 | + continue |
| 259 | + } |
| 260 | + if len(cv.Status.History) > 0 { |
| 261 | + if cv.Status.History[0].State != configv1.CompletedUpdate { |
| 262 | + conditions = append(conditions, &Condition{ |
| 263 | + Level: Warning, |
| 264 | + Locator: locateClusterVersion(cv), |
| 265 | + Message: fmt.Sprintf("cluster is updating to %s", cv.Status.History[0].Version), |
| 266 | + }) |
| 267 | + } |
| 268 | + } |
| 269 | + } |
| 270 | + return conditions |
| 271 | + }) |
| 272 | + |
| 273 | + go cvInformer.Run(ctx.Done()) |
| 274 | +} |
| 275 | + |
| 276 | +func locateClusterOperator(co *configv1.ClusterOperator) string { |
| 277 | + return fmt.Sprintf("clusteroperator/%s", co.Name) |
| 278 | +} |
| 279 | + |
| 280 | +func locateClusterVersion(cv *configv1.ClusterVersion) string { |
| 281 | + return fmt.Sprintf("clusterversion/%s", cv.Name) |
| 282 | +} |
| 283 | + |
| 284 | +func findOperatorVersionChange(old, new []configv1.OperandVersion) []string { |
| 285 | + var changed []string |
| 286 | + for i := 0; i < len(new); i++ { |
| 287 | + for j := 0; j < len(old); j++ { |
| 288 | + p := (j + i) % len(old) |
| 289 | + if old[p].Name != new[i].Name { |
| 290 | + continue |
| 291 | + } |
| 292 | + if old[p].Version == new[i].Version { |
| 293 | + continue |
| 294 | + } |
| 295 | + changed = append(changed, fmt.Sprintf("%s %s -> %s", new[i].Name, old[p].Version, new[i].Version)) |
| 296 | + } |
| 297 | + } |
| 298 | + return changed |
| 299 | +} |
| 300 | + |
| 301 | +func findOperatorStatusCondition(conditions []configv1.ClusterOperatorStatusCondition, conditionType configv1.ClusterStatusConditionType) *configv1.ClusterOperatorStatusCondition { |
| 302 | + for i := range conditions { |
| 303 | + if conditions[i].Type == conditionType { |
| 304 | + return &conditions[i] |
| 305 | + } |
| 306 | + } |
| 307 | + return nil |
| 308 | +} |
0 commit comments