Skip to content

Commit 2b8846c

Browse files
djzagerfabianvf
authored andcommitted
refactor(ansible): store relevant data on watch (#2067)
* refactor(ansible): store relevant data on watch Bring all of the logic for how values are reconciled, and where important values are stored, into the watches package. This allows us to handle command line flags, environment variables, defaults, and values from the watches file in one place with sane defaults. - Add new functions to watches package `New()` and `Validate()` - Move `getMaxWorkers()` to watches package (from runner) - Handle command line arguments in watches package * Don't pass flags to watches.Load() Instead of giving all of the flags to `watches.Load()`, we should only pass down the values that are needed. Also update `New()` so that it's possible to create a valid watch. * Log invalid value for maxWorkers and use default
1 parent 1018749 commit 2b8846c

File tree

9 files changed

+386
-254
lines changed

9 files changed

+386
-254
lines changed

pkg/ansible/run.go

Lines changed: 10 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -19,30 +19,28 @@ import (
1919
"fmt"
2020
"os"
2121
"runtime"
22-
"strconv"
23-
"strings"
2422

2523
"github.com/operator-framework/operator-sdk/pkg/ansible/controller"
26-
aoflags "github.com/operator-framework/operator-sdk/pkg/ansible/flags"
27-
proxy "github.com/operator-framework/operator-sdk/pkg/ansible/proxy"
2824
"github.com/operator-framework/operator-sdk/pkg/ansible/proxy/controllermap"
2925
"github.com/operator-framework/operator-sdk/pkg/ansible/runner"
3026
"github.com/operator-framework/operator-sdk/pkg/ansible/watches"
3127
"github.com/operator-framework/operator-sdk/pkg/k8sutil"
32-
kubemetrics "github.com/operator-framework/operator-sdk/pkg/kube-metrics"
3328
"github.com/operator-framework/operator-sdk/pkg/leader"
3429
"github.com/operator-framework/operator-sdk/pkg/metrics"
3530
"github.com/operator-framework/operator-sdk/pkg/restmapper"
36-
sdkVersion "github.com/operator-framework/operator-sdk/version"
37-
v1 "k8s.io/api/core/v1"
38-
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
3931
"k8s.io/apimachinery/pkg/runtime/schema"
4032
"k8s.io/apimachinery/pkg/util/intstr"
41-
4233
"sigs.k8s.io/controller-runtime/pkg/client/config"
43-
logf "sigs.k8s.io/controller-runtime/pkg/log"
4434
"sigs.k8s.io/controller-runtime/pkg/manager"
4535
"sigs.k8s.io/controller-runtime/pkg/manager/signals"
36+
37+
aoflags "github.com/operator-framework/operator-sdk/pkg/ansible/flags"
38+
proxy "github.com/operator-framework/operator-sdk/pkg/ansible/proxy"
39+
kubemetrics "github.com/operator-framework/operator-sdk/pkg/kube-metrics"
40+
sdkVersion "github.com/operator-framework/operator-sdk/version"
41+
v1 "k8s.io/api/core/v1"
42+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
43+
logf "sigs.k8s.io/controller-runtime/pkg/log"
4644
)
4745

4846
var (
@@ -91,7 +89,7 @@ func Run(flags *aoflags.AnsibleOperatorFlags) error {
9189

9290
var gvks []schema.GroupVersionKind
9391
cMap := controllermap.NewControllerMap()
94-
watches, err := watches.Load(flags.WatchesFile)
92+
watches, err := watches.Load(flags.WatchesFile, flags.MaxWorkers)
9593
if err != nil {
9694
log.Error(err, "Failed to load watches.")
9795
return err
@@ -107,7 +105,7 @@ func Run(flags *aoflags.AnsibleOperatorFlags) error {
107105
GVK: w.GroupVersionKind,
108106
Runner: runner,
109107
ManageStatus: w.ManageStatus,
110-
MaxWorkers: getMaxWorkers(w.GroupVersionKind, flags.MaxWorkers),
108+
MaxWorkers: w.MaxWorkers,
111109
ReconcilePeriod: w.ReconcilePeriod,
112110
})
113111
if ctr == nil {
@@ -188,28 +186,3 @@ func Run(flags *aoflags.AnsibleOperatorFlags) error {
188186
log.Info("Exiting.")
189187
return nil
190188
}
191-
192-
// if the WORKER_* environment variable is set, use that value.
193-
// Otherwise, use the value from the CLI. This is definitely
194-
// counter-intuitive but it allows the operator admin adjust the
195-
// number of workers based on their cluster resources. While the
196-
// author may use the CLI option to specify a suggested
197-
// configuration for the operator.
198-
func getMaxWorkers(gvk schema.GroupVersionKind, defValue int) int {
199-
envVar := strings.ToUpper(strings.Replace(
200-
fmt.Sprintf("WORKER_%s_%s", gvk.Kind, gvk.Group),
201-
".",
202-
"_",
203-
-1,
204-
))
205-
switch maxWorkers, err := strconv.Atoi(os.Getenv(envVar)); {
206-
case maxWorkers <= 1:
207-
return defValue
208-
case err != nil:
209-
// we don't care why we couldn't parse it just use default
210-
log.Info("Failed to parse %v from environment. Using default %v", envVar, defValue)
211-
return defValue
212-
default:
213-
return maxWorkers
214-
}
215-
}

pkg/ansible/run_test.go

Lines changed: 0 additions & 73 deletions
This file was deleted.

pkg/ansible/runner/runner.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,12 @@ func New(watch watches.Watch) (Runner, error) {
5858
var path string
5959
var cmdFunc func(ident, inputDirPath string, maxArtifacts int) *exec.Cmd
6060

61+
err := watch.Validate()
62+
if err != nil {
63+
log.Error(err, "Failed to validate watch")
64+
return nil, err
65+
}
66+
6167
switch {
6268
case watch.Playbook != "":
6369
path = watch.Playbook
@@ -119,7 +125,6 @@ type runner struct {
119125
}
120126

121127
func (r *runner) Run(ident string, u *unstructured.Unstructured, kubeconfig string) (RunResult, error) {
122-
123128
timer := metrics.ReconcileTimer(r.GVK.String())
124129
defer timer.ObserveDuration()
125130

0 commit comments

Comments
 (0)