Skip to content

Commit

Permalink
Merge watches for hybrid and helm
Browse files Browse the repository at this point in the history
This PR does the following:
1. Add labelSelectors to hybrid watches
2. Remove reconcilePeriod and max-concurrent-reconciles from
   watches and instead accept it from flags.
3. Rename ChartPath to ChartDir. ChartPath also accepts directory or a chart file.

Signed-off-by: varshaprasad96 <varshaprasad96@gmail.com>
  • Loading branch information
varshaprasad96 committed Dec 13, 2021
1 parent 1cdcd7f commit b164077
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 95 deletions.
16 changes: 3 additions & 13 deletions internal/cmd/hybrid-operator/run/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,23 +154,13 @@ func run(cmd *cobra.Command, f *flags.Flags) {
}

for _, w := range ws {
reconcilePeriod := f.ReconcilePeriod
if w.ReconcilePeriod != nil {
reconcilePeriod = w.ReconcilePeriod.Duration
}

maxConcurrentReconciles := f.MaxConcurrentReconciles
if w.MaxConcurrentReconciles != nil {
maxConcurrentReconciles = *w.MaxConcurrentReconciles
}

r, err := reconciler.New(
reconciler.WithChart(*w.Chart),
reconciler.WithGroupVersionKind(w.GroupVersionKind),
reconciler.WithOverrideValues(w.OverrideValues),
reconciler.SkipDependentWatches(w.WatchDependentResources != nil && !*w.WatchDependentResources),
reconciler.WithMaxConcurrentReconciles(maxConcurrentReconciles),
reconciler.WithReconcilePeriod(reconcilePeriod),
reconciler.WithMaxConcurrentReconciles(f.MaxConcurrentReconciles),
reconciler.WithReconcilePeriod(f.ReconcilePeriod),
reconciler.WithInstallAnnotations(annotation.DefaultInstallAnnotations...),
reconciler.WithUpgradeAnnotations(annotation.DefaultUpgradeAnnotations...),
reconciler.WithUninstallAnnotations(annotation.DefaultUninstallAnnotations...),
Expand All @@ -184,7 +174,7 @@ func run(cmd *cobra.Command, f *flags.Flags) {
log.Error(err, "unable to create controller", "controller", "Helm")
os.Exit(1)
}
log.Info("configured watch", "gvk", w.GroupVersionKind, "chartPath", w.ChartPath, "maxConcurrentReconciles", f.MaxConcurrentReconciles, "reconcilePeriod", f.ReconcilePeriod)
log.Info("configured watch", "gvk", w.GroupVersionKind, "chartDir", w.ChartDir, "maxConcurrentReconciles", f.MaxConcurrentReconciles, "reconcilePeriod", f.ReconcilePeriod)
}

log.Info("starting manager")
Expand Down
17 changes: 5 additions & 12 deletions pkg/plugins/hybrid/v1alpha/scaffolds/internal/templates/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,8 +212,6 @@ import (
var (
scheme = ctrlruntime.NewScheme()
setupLog = ctrl.Log.WithName("setup")
defaultMaxConcurrentReconciles = runtime.NumCPU()
defaultReconcilePeriod = time.Minute
)
func init() {
Expand All @@ -229,13 +227,17 @@ func main() {
leaderElectionID string
watchesPath string
probeAddr string
maxConcurrentReconciles int
reconcilePeriod time.Duration
enableLeaderElection bool
)
flag.StringVar(&metricsAddr, "metrics-bind-address", ":8080", "The address the metric endpoint binds to.")
flag.StringVar(&probeAddr, "health-probe-bind-address", ":8081", "The address the probe endpoint binds to.")
flag.StringVar(&watchesPath, "watches-file", "watches.yaml", "path to watches file")
flag.StringVar(&leaderElectionID, "leader-election-id", "{{ hashFNV .Repo }}.{{ .Domain }}", "provide leader election")
flagSet.IntVar(maxConcurrentReconciles,"max-concurrent-reconciles", runtime.NumCPU(), "Maximum number of concurrent reconciles for controllers.")
flagSet.DurationVar(reconcilePeriod, "reconcile-period", time.Minute, "Default reconcile period for controllers")
flag.BoolVar(&enableLeaderElection, "leader-elect", false,
"Enable leader election for controller manager. " +
"Enabling this will ensure there is only one active controller manager.")
Expand Down Expand Up @@ -300,15 +302,6 @@ func main() {
for _, w := range ws {
// Register controller with the factory
reconcilePeriod := defaultReconcilePeriod
if w.ReconcilePeriod != nil {
reconcilePeriod = w.ReconcilePeriod.Duration
}
maxConcurrentReconciles := defaultMaxConcurrentReconciles
if w.MaxConcurrentReconciles != nil {
maxConcurrentReconciles = *w.MaxConcurrentReconciles
}
r, err := reconciler.New(
reconciler.WithChart(*w.Chart),
Expand All @@ -329,7 +322,7 @@ func main() {
setupLog.Error(err, "unable to create controller", "controller", "Helm")
os.Exit(1)
}
setupLog.Info("configured watch", "gvk", w.GroupVersionKind, "chartPath", w.ChartPath, "maxConcurrentReconciles", maxConcurrentReconciles, "reconcilePeriod", reconcilePeriod)
setupLog.Info("configured watch", "gvk", w.GroupVersionKind, "chartDir", w.ChartDir, "maxConcurrentReconciles", maxConcurrentReconciles, "reconcilePeriod", reconcilePeriod)
}
setupLog.Info("starting manager")
Expand Down
16 changes: 8 additions & 8 deletions pkg/watches/watches.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,13 @@ import (

type Watch struct {
schema.GroupVersionKind `json:",inline"`
ChartPath string `json:"chart"`
// Though the name is ChartDir, this accepts chart directory or a chart path.
ChartDir string `json:"chart"`

WatchDependentResources *bool `json:"watchDependentResources,omitempty"`
OverrideValues map[string]string `json:"overrideValues,omitempty"`
ReconcilePeriod *metav1.Duration `json:"reconcilePeriod,omitempty"`
MaxConcurrentReconciles *int `json:"maxConcurrentReconciles,omitempty"`
Chart *chart.Chart `json:"-"`
WatchDependentResources *bool `json:"watchDependentResources,omitempty"`
OverrideValues map[string]string `json:"overrideValues,omitempty"`
Chart *chart.Chart `json:"-"`
Selector metav1.LabelSelector `json:"selector"`
}

// Load loads a slice of Watches from the watch file at `path`. For each entry
Expand Down Expand Up @@ -81,9 +81,9 @@ func LoadReader(reader io.Reader) ([]Watch, error) {
return nil, fmt.Errorf("invalid GVK: %s: %w", gvk, err)
}

cl, err := loader.Load(w.ChartPath)
cl, err := loader.Load(w.ChartDir)
if err != nil {
return nil, fmt.Errorf("invalid chart %s: %w", w.ChartPath, err)
return nil, fmt.Errorf("invalid chart %s: %w", w.ChartDir, err)
}
w.Chart = cl

Expand Down
61 changes: 19 additions & 42 deletions pkg/watches/watches_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,26 @@ var _ = Describe("LoadReader", func() {
kind: MyKind
chart: ../../pkg/internal/testdata/test-chart
watchDependentResources: false
selector:
matchExpressions:
- {key: testLabel, operator: Exists, values: []}
overrideValues:
key: value
`
expectedWatches = []Watch{
{
GroupVersionKind: schema.GroupVersionKind{Group: "mygroup", Version: "v1alpha1", Kind: "MyKind"},
ChartPath: "../../pkg/internal/testdata/test-chart",
ChartDir: "../../pkg/internal/testdata/test-chart",
WatchDependentResources: &falseVal,
OverrideValues: map[string]string{"key": "value"},
Selector: v1.LabelSelector{
MatchLabels: nil,
MatchExpressions: []v1.LabelSelectorRequirement{{
Key: "testLabel",
Operator: v1.LabelSelectorOpExists,
Values: []string{},
}},
},
},
}

Expand All @@ -69,42 +80,9 @@ var _ = Describe("LoadReader", func() {
expectedWatches = []Watch{
{
GroupVersionKind: schema.GroupVersionKind{Group: "mygroup", Version: "v1alpha1", Kind: "MyKind"},
ChartPath: "../../pkg/internal/testdata/test-chart",
WatchDependentResources: &falseVal,
OverrideValues: map[string]string{"key": "value"},
},
}

err := os.Setenv("MY_VALUE", "value")
Expect(err).NotTo(HaveOccurred())

watchesData := bytes.NewBufferString(data)
watches, err := LoadReader(watchesData)
Expect(err).NotTo(HaveOccurred())
verifyEqualWatches(expectedWatches, watches)
})

It("should create valid watches with MaxConcurrentReconciles and ReconcilePeriod", func() {
concurrentReconciles := 2
data = `---
- group: mygroup
version: v1alpha1
kind: MyKind
chart: ../../pkg/internal/testdata/test-chart
watchDependentResources: false
reconcilePeriod: 1s
maxConcurrentReconciles: 2
overrideValues:
key: $MY_VALUE
`
expectedWatches = []Watch{
{
GroupVersionKind: schema.GroupVersionKind{Group: "mygroup", Version: "v1alpha1", Kind: "MyKind"},
ChartPath: "../../pkg/internal/testdata/test-chart",
ChartDir: "../../pkg/internal/testdata/test-chart",
WatchDependentResources: &falseVal,
OverrideValues: map[string]string{"key": "value"},
MaxConcurrentReconciles: &concurrentReconciles,
ReconcilePeriod: &v1.Duration{Duration: 1000000000},
},
}

Expand All @@ -131,7 +109,7 @@ var _ = Describe("LoadReader", func() {
expectedWatches = []Watch{
{
GroupVersionKind: schema.GroupVersionKind{Group: "mygroup", Version: "v1alpha1", Kind: "MyKind"},
ChartPath: "../../pkg/internal/testdata/test-chart",
ChartDir: "../../pkg/internal/testdata/test-chart",
WatchDependentResources: &falseVal,
OverrideValues: map[string]string{
"repo": "quay.io/operator-framework/helm-operator",
Expand Down Expand Up @@ -182,12 +160,12 @@ var _ = Describe("LoadReader", func() {
expectedWatches = []Watch{
{
GroupVersionKind: schema.GroupVersionKind{Group: "mygroup", Version: "v1alpha1", Kind: "MyFirstKind"},
ChartPath: "../../pkg/internal/testdata/test-chart",
ChartDir: "../../pkg/internal/testdata/test-chart",
WatchDependentResources: &trueVal,
},
{
GroupVersionKind: schema.GroupVersionKind{Group: "mygroup", Version: "v1alpha1", Kind: "MySecondKind"},
ChartPath: "../../pkg/internal/testdata/test-chart",
ChartDir: "../../pkg/internal/testdata/test-chart",
WatchDependentResources: &trueVal,
},
}
Expand Down Expand Up @@ -299,7 +277,7 @@ var _ = Describe("Load", func() {
expectedWatches = []Watch{
{
GroupVersionKind: schema.GroupVersionKind{Group: "mygroup", Version: "v1alpha1", Kind: "MyKind"},
ChartPath: "../../pkg/internal/testdata/test-chart",
ChartDir: "../../pkg/internal/testdata/test-chart",
WatchDependentResources: &falseVal,
OverrideValues: map[string]string{"key": "value"},
},
Expand All @@ -324,11 +302,10 @@ func verifyEqualWatches(expectedWatch, obtainedWatch []Watch) {
Expect(expectedWatch[i]).NotTo(BeNil())
Expect(obtainedWatch[i]).NotTo(BeNil())
Expect(expectedWatch[i].GroupVersionKind).To(BeEquivalentTo(obtainedWatch[i].GroupVersionKind))
Expect(expectedWatch[i].ChartPath).To(BeEquivalentTo(obtainedWatch[i].ChartPath))
Expect(expectedWatch[i].ChartDir).To(BeEquivalentTo(obtainedWatch[i].ChartDir))
Expect(expectedWatch[i].WatchDependentResources).To(BeEquivalentTo(obtainedWatch[i].WatchDependentResources))
Expect(expectedWatch[i].OverrideValues).To(BeEquivalentTo(obtainedWatch[i].OverrideValues))
Expect(expectedWatch[i].MaxConcurrentReconciles).To(BeEquivalentTo(obtainedWatch[i].MaxConcurrentReconciles))
Expect(expectedWatch[i].ReconcilePeriod).To(BeEquivalentTo(obtainedWatch[i].ReconcilePeriod))
Expect(expectedWatch[i].Selector).To(BeEquivalentTo(obtainedWatch[i].Selector))
}
}

Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

31 changes: 12 additions & 19 deletions testdata/hybrid/memcached-operator/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,8 @@ import (
)

var (
scheme = ctrlruntime.NewScheme()
setupLog = ctrl.Log.WithName("setup")
defaultMaxConcurrentReconciles = runtime.NumCPU()
defaultReconcilePeriod = time.Minute
scheme = ctrlruntime.NewScheme()
setupLog = ctrl.Log.WithName("setup")
)

func init() {
Expand All @@ -56,17 +54,21 @@ func init() {

func main() {
var (
metricsAddr string
leaderElectionID string
watchesPath string
probeAddr string
enableLeaderElection bool
metricsAddr string
leaderElectionID string
watchesPath string
probeAddr string
maxConcurrentReconciles int
reconcilePeriod time.Duration
enableLeaderElection bool
)

flag.StringVar(&metricsAddr, "metrics-bind-address", ":8080", "The address the metric endpoint binds to.")
flag.StringVar(&probeAddr, "health-probe-bind-address", ":8081", "The address the probe endpoint binds to.")
flag.StringVar(&watchesPath, "watches-file", "watches.yaml", "path to watches file")
flag.StringVar(&leaderElectionID, "leader-election-id", "86f835c3.my.domain", "provide leader election")
flagSet.IntVar(maxConcurrentReconciles, "max-concurrent-reconciles", runtime.NumCPU(), "Maximum number of concurrent reconciles for controllers.")
flagSet.DurationVar(reconcilePeriod, "reconcile-period", time.Minute, "Default reconcile period for controllers")
flag.BoolVar(&enableLeaderElection, "leader-elect", false,
"Enable leader election for controller manager. "+
"Enabling this will ensure there is only one active controller manager.")
Expand Down Expand Up @@ -117,15 +119,6 @@ func main() {

for _, w := range ws {
// Register controller with the factory
reconcilePeriod := defaultReconcilePeriod
if w.ReconcilePeriod != nil {
reconcilePeriod = w.ReconcilePeriod.Duration
}

maxConcurrentReconciles := defaultMaxConcurrentReconciles
if w.MaxConcurrentReconciles != nil {
maxConcurrentReconciles = *w.MaxConcurrentReconciles
}

r, err := reconciler.New(
reconciler.WithChart(*w.Chart),
Expand All @@ -146,7 +139,7 @@ func main() {
setupLog.Error(err, "unable to create controller", "controller", "Helm")
os.Exit(1)
}
setupLog.Info("configured watch", "gvk", w.GroupVersionKind, "chartPath", w.ChartPath, "maxConcurrentReconciles", maxConcurrentReconciles, "reconcilePeriod", reconcilePeriod)
setupLog.Info("configured watch", "gvk", w.GroupVersionKind, "chartDir", w.ChartDir, "maxConcurrentReconciles", maxConcurrentReconciles, "reconcilePeriod", reconcilePeriod)
}

setupLog.Info("starting manager")
Expand Down

0 comments on commit b164077

Please sign in to comment.