Skip to content

Commit 0c3d84d

Browse files
Closes #4446: Logging to Stdout based on verbosity (#4961)
* Print to StdOut * Parsing of verbosity * Refactoring * Handling errors * Changing to assignments * Some changes * New changes Signed-off-by: Venkat Ramaraju <[email protected]>
1 parent bd6fa84 commit 0c3d84d

File tree

1 file changed

+54
-1
lines changed

1 file changed

+54
-1
lines changed

internal/ansible/events/log_events.go

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ package events
1717
import (
1818
"errors"
1919
"fmt"
20+
"os"
21+
"strconv"
2022
"sync"
2123

2224
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
@@ -62,15 +64,23 @@ func (l loggingEventHandler) Handle(ident string, u *unstructured.Unstructured,
6264
"job", ident,
6365
)
6466

67+
verbosity := GetVerbosity(u, e, ident)
68+
6569
// logger only the following for the 'Tasks' LogLevel
6670
t, ok := e.EventData["task"]
6771
if ok {
6872
setFactAction := e.EventData["task_action"] == eventapi.TaskActionSetFact
6973
debugAction := e.EventData["task_action"] == eventapi.TaskActionDebug
7074

75+
if verbosity > 0 {
76+
l.mux.Lock()
77+
fmt.Println(e.StdOut)
78+
l.mux.Unlock()
79+
return
80+
}
7181
if e.Event == eventapi.EventPlaybookOnTaskStart && !setFactAction && !debugAction {
7282
l.mux.Lock()
73-
logger.Info("[playbook task]", "EventData.Name", e.EventData["name"])
83+
logger.Info("[playbook task start]", "EventData.Name", e.EventData["name"])
7484
l.logAnsibleStdOut(e)
7585
l.mux.Unlock()
7686
return
@@ -126,3 +136,46 @@ func NewLoggingEventHandler(l LogLevel) EventHandler {
126136
mux: &sync.Mutex{},
127137
}
128138
}
139+
140+
// GetVerbosity - Parses the verbsoity from CR and environment variables
141+
func GetVerbosity(u *unstructured.Unstructured, e eventapi.JobEvent, ident string) int {
142+
logger := logf.Log.WithName("logging_event_handler").WithValues(
143+
"name", u.GetName(),
144+
"namespace", u.GetNamespace(),
145+
"gvk", u.GroupVersionKind().String(),
146+
"event_type", e.Event,
147+
"job", ident,
148+
)
149+
150+
// Parse verbosity from CR
151+
verbosityAnnotation := 0
152+
if annot, exists := u.UnstructuredContent()["metadata"].(map[string]interface{})["annotations"]; exists {
153+
if verbosityField, present := annot.(map[string]interface{})["ansible.sdk.operatorframework.io/verbosity"]; present {
154+
var err error
155+
verbosityAnnotation, err = strconv.Atoi(verbosityField.(string))
156+
if err != nil {
157+
logger.Error(err, "Unable to parse verbosity value from CR.")
158+
}
159+
}
160+
}
161+
162+
// Parse verbosity from environment variable
163+
verbosityEnvVar := 0
164+
everb := os.Getenv("ANSIBLE_VERBOSITY")
165+
if everb != "" {
166+
var err error
167+
verbosityEnvVar, err = strconv.Atoi(everb)
168+
if err != nil {
169+
logger.Error(err, "Unable to parse verbosity value from environment variable.")
170+
}
171+
}
172+
173+
// Return in order of precedence
174+
if verbosityAnnotation > 0 {
175+
return verbosityAnnotation
176+
} else if verbosityEnvVar > 0 {
177+
return verbosityEnvVar
178+
} else {
179+
return 0 // Default
180+
}
181+
}

0 commit comments

Comments
 (0)