-
Notifications
You must be signed in to change notification settings - Fork 1.8k
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
feat(ansible): make ansible verbosity configurable #1852
Changes from all commits
42574ed
0d38e6b
bc3a919
fb2c63f
18d1e6b
1ab748e
532ebb7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,30 +19,28 @@ import ( | |
"fmt" | ||
"os" | ||
"runtime" | ||
"strconv" | ||
"strings" | ||
|
||
"github.com/operator-framework/operator-sdk/pkg/ansible/controller" | ||
aoflags "github.com/operator-framework/operator-sdk/pkg/ansible/flags" | ||
proxy "github.com/operator-framework/operator-sdk/pkg/ansible/proxy" | ||
"github.com/operator-framework/operator-sdk/pkg/ansible/proxy/controllermap" | ||
"github.com/operator-framework/operator-sdk/pkg/ansible/runner" | ||
"github.com/operator-framework/operator-sdk/pkg/ansible/watches" | ||
"github.com/operator-framework/operator-sdk/pkg/k8sutil" | ||
kubemetrics "github.com/operator-framework/operator-sdk/pkg/kube-metrics" | ||
"github.com/operator-framework/operator-sdk/pkg/leader" | ||
"github.com/operator-framework/operator-sdk/pkg/metrics" | ||
"github.com/operator-framework/operator-sdk/pkg/restmapper" | ||
sdkVersion "github.com/operator-framework/operator-sdk/version" | ||
v1 "k8s.io/api/core/v1" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/apimachinery/pkg/runtime/schema" | ||
"k8s.io/apimachinery/pkg/util/intstr" | ||
|
||
"sigs.k8s.io/controller-runtime/pkg/client/config" | ||
logf "sigs.k8s.io/controller-runtime/pkg/log" | ||
"sigs.k8s.io/controller-runtime/pkg/manager" | ||
"sigs.k8s.io/controller-runtime/pkg/manager/signals" | ||
|
||
aoflags "github.com/operator-framework/operator-sdk/pkg/ansible/flags" | ||
proxy "github.com/operator-framework/operator-sdk/pkg/ansible/proxy" | ||
kubemetrics "github.com/operator-framework/operator-sdk/pkg/kube-metrics" | ||
sdkVersion "github.com/operator-framework/operator-sdk/version" | ||
v1 "k8s.io/api/core/v1" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
logf "sigs.k8s.io/controller-runtime/pkg/log" | ||
) | ||
|
||
var ( | ||
|
@@ -91,7 +89,7 @@ func Run(flags *aoflags.AnsibleOperatorFlags) error { | |
|
||
var gvks []schema.GroupVersionKind | ||
cMap := controllermap.NewControllerMap() | ||
watches, err := watches.Load(flags.WatchesFile) | ||
watches, err := watches.Load(flags) | ||
if err != nil { | ||
log.Error(err, "Failed to load watches.") | ||
return err | ||
|
@@ -107,7 +105,7 @@ func Run(flags *aoflags.AnsibleOperatorFlags) error { | |
GVK: w.GroupVersionKind, | ||
Runner: runner, | ||
ManageStatus: w.ManageStatus, | ||
MaxWorkers: getMaxWorkers(w.GroupVersionKind, flags.MaxWorkers), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So, currently, we can define the MaxWorkers by CRD. Am I right? Why should we change it? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The two places to set |
||
MaxWorkers: w.MaxWorkers, | ||
ReconcilePeriod: w.ReconcilePeriod, | ||
}) | ||
if ctr == nil { | ||
|
@@ -188,28 +186,3 @@ func Run(flags *aoflags.AnsibleOperatorFlags) error { | |
log.Info("Exiting.") | ||
return nil | ||
} | ||
|
||
// if the WORKER_* environment variable is set, use that value. | ||
// Otherwise, use the value from the CLI. This is definitely | ||
// counter-intuitive but it allows the operator admin adjust the | ||
// number of workers based on their cluster resources. While the | ||
// author may use the CLI option to specify a suggested | ||
// configuration for the operator. | ||
func getMaxWorkers(gvk schema.GroupVersionKind, defValue int) int { | ||
envVar := strings.ToUpper(strings.Replace( | ||
fmt.Sprintf("WORKER_%s_%s", gvk.Kind, gvk.Group), | ||
".", | ||
"_", | ||
-1, | ||
)) | ||
switch maxWorkers, err := strconv.Atoi(os.Getenv(envVar)); { | ||
case maxWorkers <= 1: | ||
return defValue | ||
case err != nil: | ||
// we don't care why we couldn't parse it just use default | ||
log.Info("Failed to parse %v from environment. Using default %v", envVar, defValue) | ||
return defValue | ||
default: | ||
return maxWorkers | ||
} | ||
} |
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -52,56 +52,65 @@ type Runner interface { | |
GetFinalizer() (string, bool) | ||
} | ||
|
||
func ansibleVerbosityString(verbosity int) string { | ||
if verbosity > 0 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would not it can be as v, or vv, or vvv, or vvvv? See https://docs.ansible.com/ansible/latest/cli/ansible-playbook.html#cmdoption-ansible-playbook-v So, wdyt about your solution allow the user to inform it? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
return fmt.Sprintf("-%v", strings.Repeat("v", verbosity)) | ||
} | ||
return "" | ||
} | ||
|
||
type cmdFuncType func(ident, inputDirPath string, maxArtifacts int) *exec.Cmd | ||
|
||
func playbookCmdFunc(verbosity, path string) cmdFuncType { | ||
return func(ident, inputDirPath string, maxArtifacts int) *exec.Cmd { | ||
return exec.Command("ansible-runner", verbosity, "--rotate-artifacts", fmt.Sprintf("%v", maxArtifacts), "-p", path, "-i", ident, "run", inputDirPath) | ||
} | ||
} | ||
|
||
func roleCmdFunc(verbosity, path string) cmdFuncType { | ||
rolePath, roleName := filepath.Split(path) | ||
return func(ident, inputDirPath string, maxArtifacts int) *exec.Cmd { | ||
return exec.Command("ansible-runner", verbosity, "--rotate-artifacts", fmt.Sprintf("%v", maxArtifacts), "--role", roleName, "--roles-path", rolePath, "--hosts", "localhost", "-i", ident, "run", inputDirPath) | ||
} | ||
} | ||
|
||
// New - creates a Runner from a Watch struct | ||
func New(watch watches.Watch) (Runner, error) { | ||
// handle role or playbook | ||
var path string | ||
var cmdFunc func(ident, inputDirPath string, maxArtifacts int) *exec.Cmd | ||
var cmdFunc, finalizerCmdFunc cmdFuncType | ||
|
||
err := watch.Validate() | ||
if err != nil { | ||
log.Error(err, "Failed to validate watch") | ||
return nil, err | ||
} | ||
verbosityString := ansibleVerbosityString(watch.AnsibleVerbosity) | ||
|
||
switch { | ||
case watch.Playbook != "": | ||
path = watch.Playbook | ||
cmdFunc = func(ident, inputDirPath string, maxArtifacts int) *exec.Cmd { | ||
return exec.Command("ansible-runner", "-vv", "--rotate-artifacts", fmt.Sprintf("%v", maxArtifacts), "-p", path, "-i", ident, "run", inputDirPath) | ||
} | ||
cmdFunc = playbookCmdFunc(verbosityString, path) | ||
case watch.Role != "": | ||
path = watch.Role | ||
cmdFunc = func(ident, inputDirPath string, maxArtifacts int) *exec.Cmd { | ||
rolePath, roleName := filepath.Split(path) | ||
return exec.Command("ansible-runner", "-vv", "--rotate-artifacts", fmt.Sprintf("%v", maxArtifacts), "--role", roleName, "--roles-path", rolePath, "--hosts", "localhost", "-i", ident, "run", inputDirPath) | ||
} | ||
default: | ||
return nil, fmt.Errorf("must specify Role or Path") | ||
cmdFunc = roleCmdFunc(verbosityString, path) | ||
} | ||
|
||
// handle finalizer | ||
var finalizer *watches.Finalizer | ||
var finalizerCmdFunc func(ident, inputDirPath string, maxArtifacts int) *exec.Cmd | ||
switch { | ||
case watch.Finalizer == nil: | ||
finalizer = nil | ||
finalizerCmdFunc = nil | ||
case watch.Finalizer.Playbook != "": | ||
finalizer = watch.Finalizer | ||
finalizerCmdFunc = func(ident, inputDirPath string, maxArtifacts int) *exec.Cmd { | ||
return exec.Command("ansible-runner", "-vv", "--rotate-artifacts", fmt.Sprintf("%v", maxArtifacts), "-p", finalizer.Playbook, "-i", ident, "run", inputDirPath) | ||
} | ||
finalizerCmdFunc = playbookCmdFunc(verbosityString, watch.Finalizer.Playbook) | ||
case watch.Finalizer.Role != "": | ||
finalizer = watch.Finalizer | ||
finalizerCmdFunc = func(ident, inputDirPath string, maxArtifacts int) *exec.Cmd { | ||
path := strings.TrimRight(finalizer.Role, "/") | ||
rolePath, roleName := filepath.Split(path) | ||
return exec.Command("ansible-runner", "-vv", "--rotate-artifacts", fmt.Sprintf("%v", maxArtifacts), "--role", roleName, "--roles-path", rolePath, "--hosts", "localhost", "-i", ident, "run", inputDirPath) | ||
} | ||
finalizerCmdFunc = roleCmdFunc(verbosityString, watch.Finalizer.Role) | ||
case len(watch.Finalizer.Vars) != 0: | ||
finalizer = watch.Finalizer | ||
finalizerCmdFunc = cmdFunc | ||
} | ||
|
||
return &runner{ | ||
Path: path, | ||
cmdFunc: cmdFunc, | ||
Finalizer: finalizer, | ||
Finalizer: watch.Finalizer, | ||
finalizerCmdFunc: finalizerCmdFunc, | ||
GVK: watch.GroupVersionKind, | ||
maxRunnerArtifacts: watch.MaxRunnerArtifacts, | ||
|
@@ -119,7 +128,6 @@ type runner struct { | |
} | ||
|
||
func (r *runner) Run(ident string, u *unstructured.Unstructured, kubeconfig string) (RunResult, error) { | ||
|
||
timer := metrics.ReconcileTimer(r.GVK.String()) | ||
defer timer.ObserveDuration() | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is not this change cable of to cause break changes in the current projects?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The proposed changes should not break current projects or require any changes in those projects. If you have a scenario or code path in mind I should certainly fix it.