Skip to content

Commit

Permalink
ansible output should log with GVK info, namespace as the CR name (op…
Browse files Browse the repository at this point in the history
  • Loading branch information
kandaaaaa authored Feb 9, 2021
1 parent 2eaca78 commit ae4aab9
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 12 deletions.
22 changes: 10 additions & 12 deletions internal/ansible/controller/reconcile.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,12 +178,12 @@ func (r *AnsibleOperatorReconciler) Reconcile(ctx context.Context, request recon
// convert to StatusJobEvent; would love a better way to do this
data, err := json.Marshal(event)
if err != nil {
printEventStats(statusEvent)
printEventStats(statusEvent, u)
return reconcile.Result{}, err
}
err = json.Unmarshal(data, &statusEvent)
if err != nil {
printEventStats(statusEvent)
printEventStats(statusEvent, u)
return reconcile.Result{}, err
}
}
Expand All @@ -210,10 +210,10 @@ func (r *AnsibleOperatorReconciler) Reconcile(ctx context.Context, request recon
}

// To print the stats of the task
printEventStats(statusEvent)
printEventStats(statusEvent, u)

// To print the full ansible result
r.printAnsibleResult(result)
r.printAnsibleResult(result, u)

if statusEvent.Event == "" {
eventErr := errors.New("did not receive playbook_on_stats event")
Expand Down Expand Up @@ -273,20 +273,18 @@ func (r *AnsibleOperatorReconciler) Reconcile(ctx context.Context, request recon
return reconcileResult, nil
}

func printEventStats(statusEvent eventapi.StatusJobEvent) {
func printEventStats(statusEvent eventapi.StatusJobEvent, u *unstructured.Unstructured) {
if len(statusEvent.StdOut) > 0 {
fmt.Printf("\n--------------------------- Ansible Task Status Event StdOut -----------------\n")
fmt.Println(statusEvent.StdOut)
fmt.Printf("\n-------------------------------------------------------------------------------\n")
str := fmt.Sprintf("Ansible Task Status Event StdOut (%s, %s/%s)", u.GroupVersionKind(), u.GetName(), u.GetNamespace())
fmt.Printf("\n----- %70s -----\n\n%s\n\n----------\n", str, statusEvent.StdOut)
}
}

func (r *AnsibleOperatorReconciler) printAnsibleResult(result runner.RunResult) {
func (r *AnsibleOperatorReconciler) printAnsibleResult(result runner.RunResult, u *unstructured.Unstructured) {
if r.AnsibleDebugLogs {
if res, err := result.Stdout(); err == nil && len(res) > 0 {
fmt.Printf("\n--------------------------- Ansible Debug Result -----------------------------\n")
fmt.Println(res)
fmt.Printf("\n-------------------------------------------------------------------------------\n")
str := fmt.Sprintf("Ansible Debug Result (%s, %s/%s)", u.GroupVersionKind(), u.GetName(), u.GetNamespace())
fmt.Printf("\n----- %70s -----\n\n%s\n\n----------\n", str, res)
}
}
}
Expand Down
11 changes: 11 additions & 0 deletions internal/ansible/events/log_events.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package events
import (
"errors"
"fmt"
"sync"

"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
logf "sigs.k8s.io/controller-runtime/pkg/log"
Expand Down Expand Up @@ -45,6 +46,7 @@ type EventHandler interface {

type loggingEventHandler struct {
LogLevel LogLevel
mux *sync.Mutex
}

func (l loggingEventHandler) Handle(ident string, u *unstructured.Unstructured, e eventapi.JobEvent) {
Expand All @@ -67,13 +69,17 @@ func (l loggingEventHandler) Handle(ident string, u *unstructured.Unstructured,
debugAction := e.EventData["task_action"] == eventapi.TaskActionDebug

if e.Event == eventapi.EventPlaybookOnTaskStart && !setFactAction && !debugAction {
l.mux.Lock()
logger.Info("[playbook task]", "EventData.Name", e.EventData["name"])
l.logAnsibleStdOut(e)
l.mux.Unlock()
return
}
if e.Event == eventapi.EventRunnerOnOk && debugAction {
l.mux.Lock()
logger.Info("[playbook debug]", "EventData.TaskArgs", e.EventData["task_args"])
l.logAnsibleStdOut(e)
l.mux.Unlock()
return
}
if e.Event == eventapi.EventRunnerOnFailed {
Expand All @@ -84,16 +90,20 @@ func (l loggingEventHandler) Handle(ident string, u *unstructured.Unstructured,
if taskPath, ok := e.EventData["task_path"]; ok {
errKVs = append(errKVs, "EventData.FailedTaskPath", taskPath)
}
l.mux.Lock()
logger.Error(errors.New("[playbook task failed]"), "", errKVs...)
l.logAnsibleStdOut(e)
l.mux.Unlock()
return
}
}

// log everything else for the 'Everything' LogLevel
if l.LogLevel == Everything {
l.mux.Lock()
logger.Info("", "EventData", e.EventData)
l.logAnsibleStdOut(e)
l.mux.Unlock()
}
}

Expand All @@ -113,5 +123,6 @@ func (l loggingEventHandler) logAnsibleStdOut(e eventapi.JobEvent) {
func NewLoggingEventHandler(l LogLevel) EventHandler {
return loggingEventHandler{
LogLevel: l,
mux: &sync.Mutex{},
}
}

0 comments on commit ae4aab9

Please sign in to comment.