|
| 1 | +// Copyright 2015 Google Inc. All Rights Reserved. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package main |
| 16 | + |
| 17 | +import ( |
| 18 | + "k8s.io/kubernetes/pkg/api" |
| 19 | + client "k8s.io/kubernetes/pkg/client/unversioned" |
| 20 | + "log" |
| 21 | + "strings" |
| 22 | +) |
| 23 | + |
| 24 | +// Partial string to correctly filter warning events. |
| 25 | +// Has to be lower case for correct case insensitive comparison. |
| 26 | +const FAILED_REASON_PARTIAL = "failed" |
| 27 | + |
| 28 | +// Contains basic information about event related to a pod |
| 29 | +type PodEvent struct { |
| 30 | + // Short, machine understandable string that gives the reason |
| 31 | + // for this event being generated. |
| 32 | + Reason string `json:"reason"` |
| 33 | + |
| 34 | + // A human-readable description of the status of related pod. |
| 35 | + Message string `json:"message"` |
| 36 | +} |
| 37 | + |
| 38 | +// Returns warning pod events based on given list of pods. |
| 39 | +// TODO(floreks) : Import and use Set instead of custom function to get rid of duplicates |
| 40 | +func GetPodsEventWarnings(client client.Interface, pods []api.Pod) (result []PodEvent, err error) { |
| 41 | + for _, pod := range pods { |
| 42 | + if !isRunningOrSucceeded(pod) { |
| 43 | + log.Printf("Getting warning events from pod: %s", pod.Name) |
| 44 | + events, err := GetPodEvents(client, pod) |
| 45 | + |
| 46 | + if err != nil { |
| 47 | + return nil, err |
| 48 | + } |
| 49 | + |
| 50 | + result = getPodsEventWarnings(events) |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + return removeDuplicates(result), nil |
| 55 | +} |
| 56 | + |
| 57 | +// Returns list of Pod Event model objects based on kubernetes API event list object |
| 58 | +// Event list object is filtered to get only warning events. |
| 59 | +func getPodsEventWarnings(eventList *api.EventList) []PodEvent { |
| 60 | + result := make([]PodEvent, 0) |
| 61 | + |
| 62 | + var events []api.Event |
| 63 | + if isTypeFilled(eventList.Items) { |
| 64 | + events = filterEventsByType(eventList.Items, api.EventTypeWarning) |
| 65 | + } else { |
| 66 | + events = filterEventsByReason(eventList.Items, FAILED_REASON_PARTIAL) |
| 67 | + } |
| 68 | + |
| 69 | + for _, event := range events { |
| 70 | + result = append(result, PodEvent{ |
| 71 | + Message: event.Message, |
| 72 | + Reason: event.Reason, |
| 73 | + }) |
| 74 | + } |
| 75 | + |
| 76 | + return result |
| 77 | +} |
| 78 | + |
| 79 | +// Filters kubernetes API event objects based on event type. |
| 80 | +// Empty string will return all events. |
| 81 | +func filterEventsByType(events []api.Event, eventType string) []api.Event { |
| 82 | + if len(eventType) == 0 || len(events) == 0 { |
| 83 | + return events |
| 84 | + } |
| 85 | + |
| 86 | + result := make([]api.Event, 0) |
| 87 | + for _, event := range events { |
| 88 | + if event.Type == eventType { |
| 89 | + result = append(result, event) |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + return result |
| 94 | +} |
| 95 | + |
| 96 | +// Filters kubernetes API event objects based on reason property. |
| 97 | +// Empty string will return all events. |
| 98 | +func filterEventsByReason(events []api.Event, partial string) []api.Event { |
| 99 | + if len(partial) == 0 || len(events) == 0 { |
| 100 | + return events |
| 101 | + } |
| 102 | + |
| 103 | + result := make([]api.Event, 0) |
| 104 | + for _, event := range events { |
| 105 | + if strings.Contains(strings.ToLower(event.Reason), partial) { |
| 106 | + result = append(result, event) |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + return result |
| 111 | +} |
| 112 | + |
| 113 | +// Returns true if all given events type is filled, false otherwise. |
| 114 | +// This is needed as some older versions of kubernetes do not have Type property filled. |
| 115 | +func isTypeFilled(events []api.Event) bool { |
| 116 | + if len(events) == 0 { |
| 117 | + return false |
| 118 | + } |
| 119 | + |
| 120 | + for _, event := range events { |
| 121 | + if len(event.Type) == 0 { |
| 122 | + return false |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + return true |
| 127 | +} |
| 128 | + |
| 129 | +// Removes duplicate strings from the slice |
| 130 | +func removeDuplicates(slice []PodEvent) []PodEvent { |
| 131 | + visited := make(map[string]bool, 0) |
| 132 | + result := make([]PodEvent, 0) |
| 133 | + |
| 134 | + for _, elem := range slice { |
| 135 | + if !visited[elem.Reason] { |
| 136 | + visited[elem.Reason] = true |
| 137 | + result = append(result, elem) |
| 138 | + } |
| 139 | + } |
| 140 | + |
| 141 | + return result |
| 142 | +} |
| 143 | + |
| 144 | +// Returns true if given pod is in state running or succeeded, false otherwise |
| 145 | +func isRunningOrSucceeded(pod api.Pod) bool { |
| 146 | + switch pod.Status.Phase { |
| 147 | + case api.PodRunning, api.PodSucceeded: |
| 148 | + return true |
| 149 | + } |
| 150 | + |
| 151 | + return false |
| 152 | +} |
0 commit comments