github.com/lablabs/operator-sdk@v0.8.2/pkg/ansible/events/log_events.go (about) 1 // Copyright 2018 The Operator-SDK Authors 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 events 16 17 import ( 18 "errors" 19 20 "github.com/operator-framework/operator-sdk/pkg/ansible/runner/eventapi" 21 22 "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" 23 logf "sigs.k8s.io/controller-runtime/pkg/runtime/log" 24 ) 25 26 // LogLevel - Levelt for the logging to take place. 27 type LogLevel int 28 29 const ( 30 // Tasks - only log the high level tasks. 31 Tasks LogLevel = iota 32 33 // Everything - log every event. 34 Everything 35 36 // Nothing - this will log nothing. 37 Nothing 38 ) 39 40 // EventHandler - knows how to handle job events. 41 type EventHandler interface { 42 Handle(string, *unstructured.Unstructured, eventapi.JobEvent) 43 } 44 45 type loggingEventHandler struct { 46 LogLevel LogLevel 47 } 48 49 func (l loggingEventHandler) Handle(ident string, u *unstructured.Unstructured, e eventapi.JobEvent) { 50 if l.LogLevel == Nothing { 51 return 52 } 53 54 logger := logf.Log.WithName("logging_event_handler").WithValues( 55 "name", u.GetName(), 56 "namespace", u.GetNamespace(), 57 "gvk", u.GroupVersionKind().String(), 58 "event_type", e.Event, 59 "job", ident, 60 ) 61 62 // logger only the following for the 'Tasks' LogLevel 63 t, ok := e.EventData["task"] 64 if ok { 65 setFactAction := e.EventData["task_action"] == eventapi.TaskActionSetFact 66 debugAction := e.EventData["task_action"] == eventapi.TaskActionDebug 67 68 if e.Event == eventapi.EventPlaybookOnTaskStart && !setFactAction && !debugAction { 69 logger.Info("[playbook task]", "EventData.Name", e.EventData["name"]) 70 return 71 } 72 if e.Event == eventapi.EventRunnerOnOk && debugAction { 73 logger.Info("[playbook debug]", "EventData.TaskArgs", e.EventData["task_args"]) 74 return 75 } 76 if e.Event == eventapi.EventRunnerOnFailed { 77 errKVs := []interface{}{ 78 "EventData.Task", t, 79 "EventData.TaskArgs", e.EventData["task_args"], 80 } 81 if taskPath, ok := e.EventData["task_path"]; ok { 82 errKVs = append(errKVs, "EventData.FailedTaskPath", taskPath) 83 } 84 logger.Error(errors.New("[playbook task failed]"), "", errKVs...) 85 return 86 } 87 } 88 89 // log everything else for the 'Everything' LogLevel 90 if l.LogLevel == Everything { 91 logger.Info("", "EventData", e.EventData) 92 } 93 } 94 95 // NewLoggingEventHandler - Creates a Logging Event Handler to log events. 96 func NewLoggingEventHandler(l LogLevel) EventHandler { 97 return loggingEventHandler{ 98 LogLevel: l, 99 } 100 }