Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add event stats output to the operator logs for Ansible based-operators. #2580

Merged
merged 1 commit into from
Feb 21, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
- On `generate csv`, populate a CSV manifest’s `spec.icon`, `spec.keywords`, and `spec.mantainers` fields with empty values to better inform users how to add data. ([#2521](https://github.com/operator-framework/operator-sdk/pull/2521))
- Scaffold code in `cmd/manager/main.go` for Go operators and add logic to Ansible/Helm operators to handle [multinamespace caching](https://godoc.org/github.com/kubernetes-sigs/controller-runtime/pkg/cache#MultiNamespacedCacheBuilder) if `WATCH_NAMESPACE` contains multiple namespaces. ([#2522](https://github.com/operator-framework/operator-sdk/pull/2522))
- Add a new flag option (`--skip-cleanup-error`) to the test framework to allow skip the function which will remove all artefacts when an error be faced to perform this operation. ([#2512](https://github.com/operator-framework/operator-sdk/pull/2512))
- Add event stats output to the operator logs for Ansible based-operators. ([2580](https://github.com/operator-framework/operator-sdk/pull/2580))

### Changed
- Ansible scaffolding has been rewritten to be simpler and make use of newer features of Ansible and Molecule.
Expand Down
12 changes: 12 additions & 0 deletions pkg/ansible/controller/reconcile.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,17 +176,23 @@ func (r *AnsibleOperatorReconciler) Reconcile(request reconcile.Request) (reconc
// convert to StatusJobEvent; would love a better way to do this
data, err := json.Marshal(event)
if err != nil {
printEventStats(statusEvent)
return reconcile.Result{}, err
}
err = json.Unmarshal(data, &statusEvent)
if err != nil {
printEventStats(statusEvent)
return reconcile.Result{}, err
}
}
if event.Event == eventapi.EventRunnerOnFailed && !event.IgnoreError() {
failureMessages = append(failureMessages, event.GetFailedPlaybookMessage())
}
}

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

if statusEvent.Event == "" {
eventErr := errors.New("did not receive playbook_on_stats event")
stdout, err := result.Stdout()
Expand Down Expand Up @@ -249,6 +255,12 @@ func (r *AnsibleOperatorReconciler) Reconcile(request reconcile.Request) (reconc
return reconcileResult, nil
}

func printEventStats(statusEvent eventapi.StatusJobEvent) {
fmt.Printf("\n--------------------------- Ansible Task Status Event StdOut -----------------\n")
fmt.Println(statusEvent.StdOut)
fmt.Printf("\n-------------------------------------------------------------------------------\n")
}

Comment on lines +258 to +263
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe I'm missing something but why do we use fmt.Printf here but logger. in other places.

Copy link
Contributor Author

@camilamacedo86 camilamacedo86 Feb 21, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To print the data formatted such as we do here: #2321. It has an image which shows the output as well.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, if you put the ansible stdout through the logger it gets mangled completely since it is JSON serialized

func (r *AnsibleOperatorReconciler) markRunning(u *unstructured.Unstructured,
namespacedName types.NamespacedName) error {
// Get the latest resource to prevent updating a stale status.
Expand Down