Skip to content

Commit f801fe1

Browse files
authored
pkg/*: use operator-lib handlers (#58)
1 parent 7af2a80 commit f801fe1

File tree

8 files changed

+82
-172
lines changed

8 files changed

+82
-172
lines changed

go.mod

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,22 @@ go 1.13
44

55
require (
66
github.com/go-logr/logr v0.1.0
7-
github.com/onsi/ginkgo v1.12.0
8-
github.com/onsi/gomega v1.9.0
9-
github.com/prometheus/client_golang v1.0.0
7+
github.com/onsi/ginkgo v1.12.1
8+
github.com/onsi/gomega v1.10.1
9+
github.com/operator-framework/operator-lib v0.1.0
1010
github.com/spf13/pflag v1.0.5
1111
github.com/stretchr/testify v1.5.1
1212
go.uber.org/zap v1.13.0
1313
gomodules.xyz/jsonpatch/v2 v2.0.1
1414
helm.sh/helm/v3 v3.2.0
15-
k8s.io/api v0.18.2
16-
k8s.io/apiextensions-apiserver v0.18.2
17-
k8s.io/apimachinery v0.18.2
15+
k8s.io/api v0.18.4
16+
k8s.io/apiextensions-apiserver v0.18.4
17+
k8s.io/apimachinery v0.18.4
1818
k8s.io/cli-runtime v0.18.2
19-
k8s.io/client-go v0.18.2
19+
k8s.io/client-go v0.18.4
2020
k8s.io/klog v1.0.0
2121
k8s.io/kubectl v0.18.2
2222
rsc.io/letsencrypt v0.0.3 // indirect
23-
sigs.k8s.io/controller-runtime v0.6.0
23+
sigs.k8s.io/controller-runtime v0.6.1
2424
sigs.k8s.io/yaml v1.2.0
2525
)

go.sum

Lines changed: 66 additions & 4 deletions
Large diffs are not rendered by default.

pkg/client/actionclient.go

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"errors"
2323
"fmt"
2424

25+
sdkhandler "github.com/operator-framework/operator-lib/handler"
2526
"gomodules.xyz/jsonpatch/v2"
2627
"helm.sh/helm/v3/pkg/action"
2728
"helm.sh/helm/v3/pkg/chart"
@@ -43,7 +44,6 @@ import (
4344
"sigs.k8s.io/yaml"
4445

4546
"github.com/joelanford/helm-operator/pkg/internal/sdk/controllerutil"
46-
"github.com/joelanford/helm-operator/pkg/internal/sdk/handler"
4747
)
4848

4949
type ActionClientGetter interface {
@@ -329,13 +329,9 @@ func (pr *ownerPostRenderer) Run(in *bytes.Buffer) (*bytes.Buffer, error) {
329329
ownerRefs := append(u.GetOwnerReferences(), *ownerRef)
330330
u.SetOwnerReferences(ownerRefs)
331331
} else {
332-
a := u.GetAnnotations()
333-
if a == nil {
334-
a = map[string]string{}
332+
if err := sdkhandler.SetOwnerAnnotations(pr.owner, u); err != nil {
333+
return err
335334
}
336-
a[handler.NamespacedNameAnnotation] = fmt.Sprintf("%s/%s", pr.owner.GetNamespace(), pr.owner.GetName())
337-
a[handler.TypeAnnotation] = pr.owner.GetObjectKind().GroupVersionKind().GroupKind().String()
338-
u.SetAnnotations(a)
339335
}
340336
outData, err := yaml.Marshal(u.Object)
341337
if err != nil {

pkg/internal/sdk/handler/enqueue_annotation.go

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

pkg/reconciler/internal/hook/hook.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"sync"
2121

2222
"github.com/go-logr/logr"
23+
sdkhandler "github.com/operator-framework/operator-lib/handler"
2324
"helm.sh/helm/v3/pkg/release"
2425
"helm.sh/helm/v3/pkg/releaseutil"
2526
"k8s.io/apimachinery/pkg/api/meta"
@@ -32,7 +33,6 @@ import (
3233

3334
"github.com/joelanford/helm-operator/pkg/hook"
3435
"github.com/joelanford/helm-operator/pkg/internal/sdk/controllerutil"
35-
sdkhandler "github.com/joelanford/helm-operator/pkg/internal/sdk/handler"
3636
"github.com/joelanford/helm-operator/pkg/internal/sdk/predicate"
3737
)
3838

@@ -86,7 +86,7 @@ func (d *dependentResourceWatcher) Exec(owner *unstructured.Unstructured, rel re
8686
}
8787
} else {
8888
if err := d.controller.Watch(&source.Kind{Type: &obj}, &sdkhandler.EnqueueRequestForAnnotation{
89-
Type: owner.GetObjectKind().GroupVersionKind().GroupKind().String(),
89+
Type: owner.GetObjectKind().GroupVersionKind().GroupKind(),
9090
}, dependentPredicate); err != nil {
9191
return err
9292
}

pkg/reconciler/internal/hook/hook_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"github.com/go-logr/logr/testing"
2323
. "github.com/onsi/ginkgo"
2424
. "github.com/onsi/gomega"
25+
sdkhandler "github.com/operator-framework/operator-lib/handler"
2526
"helm.sh/helm/v3/pkg/release"
2627
"k8s.io/apimachinery/pkg/api/meta"
2728
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
@@ -30,7 +31,6 @@ import (
3031

3132
"github.com/joelanford/helm-operator/pkg/hook"
3233
"github.com/joelanford/helm-operator/pkg/internal/sdk/fake"
33-
sdkhandler "github.com/joelanford/helm-operator/pkg/internal/sdk/handler"
3434
internalhook "github.com/joelanford/helm-operator/pkg/reconciler/internal/hook"
3535
)
3636

pkg/reconciler/reconciler.go

Lines changed: 2 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import (
2525
"time"
2626

2727
"github.com/go-logr/logr"
28-
"github.com/prometheus/client_golang/prometheus"
28+
sdkhandler "github.com/operator-framework/operator-lib/handler"
2929
"helm.sh/helm/v3/pkg/action"
3030
"helm.sh/helm/v3/pkg/chart"
3131
"helm.sh/helm/v3/pkg/chartutil"
@@ -42,7 +42,6 @@ import (
4242
"sigs.k8s.io/controller-runtime/pkg/client"
4343
"sigs.k8s.io/controller-runtime/pkg/controller"
4444
"sigs.k8s.io/controller-runtime/pkg/handler"
45-
"sigs.k8s.io/controller-runtime/pkg/metrics"
4645
"sigs.k8s.io/controller-runtime/pkg/source"
4746

4847
"github.com/joelanford/helm-operator/pkg/annotation"
@@ -80,8 +79,6 @@ type Reconciler struct {
8079
installAnnotations map[string]annotation.Install
8180
upgradeAnnotations map[string]annotation.Upgrade
8281
uninstallAnnotations map[string]annotation.Uninstall
83-
84-
infoMetric *prometheus.GaugeVec
8582
}
8683

8784
// New creates a new Reconciler that reconciles custom resources that define a
@@ -108,10 +105,6 @@ func New(opts ...Option) (*Reconciler, error) {
108105
return nil, err
109106
}
110107

111-
if err := r.setupMetrics(); err != nil {
112-
return nil, err
113-
}
114-
115108
return r, nil
116109
}
117110

@@ -122,15 +115,6 @@ func (r *Reconciler) setupAnnotationMaps() {
122115
r.uninstallAnnotations = make(map[string]annotation.Uninstall)
123116
}
124117

125-
func (r *Reconciler) setupMetrics() error {
126-
r.infoMetric = prometheus.NewGaugeVec(prometheus.GaugeOpts{
127-
Name: fmt.Sprintf("%s_info", strings.ToLower(r.gvk.Kind)),
128-
Help: fmt.Sprintf("Information about the %s custom resource.", r.gvk.Kind),
129-
}, []string{"namespace", "name"})
130-
131-
return metrics.Registry.Register(r.infoMetric)
132-
}
133-
134118
// SetupWithManager configures a controller for the Reconciler and registers
135119
// watches. It also uses the passed Manager to initialize default values for the
136120
// Reconciler and sets up the manager's scheme with the Reconciler's configured
@@ -524,16 +508,6 @@ func (r *Reconciler) Reconcile(req ctrl.Request) (res ctrl.Result, err error) {
524508
return ctrl.Result{}, fmt.Errorf("unexpected release state: %s", state)
525509
}
526510

527-
labels := map[string]string{
528-
"namespace": obj.GetNamespace(),
529-
"name": obj.GetName(),
530-
}
531-
if m, err := r.infoMetric.GetMetricWith(labels); err != nil {
532-
panic(err)
533-
} else {
534-
m.Set(1.0)
535-
}
536-
537511
for _, h := range r.postHooks {
538512
if err := h.Exec(obj, *rel, log); err != nil {
539513
log.Error(err, "post-release hook failed", "name", rel.Name, "version", rel.Version)
@@ -599,12 +573,6 @@ func (r *Reconciler) handleDeletion(ctx context.Context, actionClient helmclient
599573
return err
600574
}
601575

602-
labels := map[string]string{
603-
"namespace": obj.GetNamespace(),
604-
"name": obj.GetName(),
605-
}
606-
_ = r.infoMetric.Delete(labels)
607-
608576
// Since the client is hitting a cache, waiting for the
609577
// deletion here will guarantee that the next reconciliation
610578
// will see that the CR has been deleted and that there's
@@ -783,7 +751,7 @@ func (r *Reconciler) setupWatches(mgr ctrl.Manager, c controller.Controller) err
783751
obj.SetGroupVersionKind(*r.gvk)
784752
if err := c.Watch(
785753
&source.Kind{Type: obj},
786-
&handler.EnqueueRequestForObject{},
754+
&sdkhandler.InstrumentedEnqueueRequestForObject{},
787755
); err != nil {
788756
return err
789757
}

pkg/reconciler/reconciler_test.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ import (
4646
"sigs.k8s.io/controller-runtime/pkg/client/fake"
4747
"sigs.k8s.io/controller-runtime/pkg/log/zap"
4848
"sigs.k8s.io/controller-runtime/pkg/manager"
49-
"sigs.k8s.io/controller-runtime/pkg/metrics"
5049
"sigs.k8s.io/controller-runtime/pkg/reconcile"
5150
"sigs.k8s.io/yaml"
5251

@@ -428,7 +427,6 @@ var _ = Describe("Reconciler", func() {
428427
}
429428
Expect(err).To(BeNil())
430429
})
431-
metrics.Registry.Unregister(r.infoMetric)
432430
close(done)
433431
})
434432

0 commit comments

Comments
 (0)