|
| 1 | +// This Source Code Form is subject to the terms of the Mozilla Public |
| 2 | +// License, v. 2.0. If a copy of the MPL was not distributed with this |
| 3 | +// file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 4 | + |
| 5 | +// Package cli provides helper functions for CLI applications using the SSA manager. |
| 6 | +package cli |
| 7 | + |
| 8 | +import ( |
| 9 | + "context" |
| 10 | + |
| 11 | + "github.com/fluxcd/cli-utils/pkg/object" |
| 12 | + |
| 13 | + "github.com/siderolabs/go-kubernetes/kubernetes/ssa" |
| 14 | +) |
| 15 | + |
| 16 | +// LogApplyResults logs the results of an SSA apply operation. |
| 17 | +func LogApplyResults(ctx context.Context, changes []ssa.Change, manager *ssa.Manager, logFunc func(line string, args ...any)) { |
| 18 | + for _, change := range changes { |
| 19 | + switch change.Action { |
| 20 | + case ssa.CreatedAction, ssa.ConfiguredAction, ssa.DeletedAction: |
| 21 | + logFunc(" < %s %s", change.Action, change.Subject) |
| 22 | + logFunc("%s", change.Diff) |
| 23 | + case ssa.SkippedAction, ssa.UnchangedAction: |
| 24 | + logFunc(" > skipped %s: no changes", change.Subject) |
| 25 | + default: |
| 26 | + logFunc(" > processing manifest %s: unknown action %q", change.Subject, change.Action) |
| 27 | + } |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +// Wait waits for the given set of changes to be fully reconciled. |
| 32 | +func Wait(ctx context.Context, changes []ssa.Change, logFunc func(line string, args ...any), manager *ssa.Manager, waitOps ssa.WaitOptions) error { |
| 33 | + waitObjects := make(map[object.ObjMetadata]struct{}, len(changes)) |
| 34 | + |
| 35 | + for _, change := range changes { |
| 36 | + switch change.Action { |
| 37 | + case ssa.CreatedAction, ssa.ConfiguredAction: |
| 38 | + waitObjects[change.ObjMetadata] = struct{}{} |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + logFunc("waiting for kubernetes objects to be fully reconciled") |
| 43 | + |
| 44 | + err := manager.Wait(ctx, |
| 45 | + object.ObjMetadataSetFromMap(waitObjects), |
| 46 | + waitOps, |
| 47 | + ) |
| 48 | + if err != nil { |
| 49 | + return err |
| 50 | + } |
| 51 | + |
| 52 | + logFunc("done") |
| 53 | + |
| 54 | + return nil |
| 55 | +} |
0 commit comments