@@ -17,6 +17,8 @@ package events
17
17
import (
18
18
"errors"
19
19
"fmt"
20
+ "os"
21
+ "strconv"
20
22
"sync"
21
23
22
24
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
@@ -62,15 +64,23 @@ func (l loggingEventHandler) Handle(ident string, u *unstructured.Unstructured,
62
64
"job" , ident ,
63
65
)
64
66
67
+ verbosity := GetVerbosity (u , e , ident )
68
+
65
69
// logger only the following for the 'Tasks' LogLevel
66
70
t , ok := e .EventData ["task" ]
67
71
if ok {
68
72
setFactAction := e .EventData ["task_action" ] == eventapi .TaskActionSetFact
69
73
debugAction := e .EventData ["task_action" ] == eventapi .TaskActionDebug
70
74
75
+ if verbosity > 0 {
76
+ l .mux .Lock ()
77
+ fmt .Println (e .StdOut )
78
+ l .mux .Unlock ()
79
+ return
80
+ }
71
81
if e .Event == eventapi .EventPlaybookOnTaskStart && ! setFactAction && ! debugAction {
72
82
l .mux .Lock ()
73
- logger .Info ("[playbook task]" , "EventData.Name" , e .EventData ["name" ])
83
+ logger .Info ("[playbook task start ]" , "EventData.Name" , e .EventData ["name" ])
74
84
l .logAnsibleStdOut (e )
75
85
l .mux .Unlock ()
76
86
return
@@ -126,3 +136,46 @@ func NewLoggingEventHandler(l LogLevel) EventHandler {
126
136
mux : & sync.Mutex {},
127
137
}
128
138
}
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