Skip to content

Commit

Permalink
Use latest knative/pkg and dynamic informers
Browse files Browse the repository at this point in the history
  • Loading branch information
tcnghia committed Mar 16, 2022
1 parent a60fd71 commit bb02814
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 12 deletions.
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ require (
k8s.io/apimachinery v0.23.3
k8s.io/client-go v0.23.3
k8s.io/utils v0.0.0-20220127004650-9b3446523e65
knative.dev/pkg v0.0.0-20220314165618-a637a96a1bd9
knative.dev/pkg v0.0.0-20220316002959-3a4cc56708b9
sigs.k8s.io/release-utils v0.4.1-0.20220207182343-6dadf2228617
sigs.k8s.io/yaml v1.3.0
)
Expand Down Expand Up @@ -238,7 +238,7 @@ require (
github.com/opentracing/opentracing-go v1.2.0 // indirect
github.com/pelletier/go-toml v1.9.4 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/prometheus/client_golang v1.11.0 // indirect
github.com/prometheus/client_golang v1.11.1 // indirect
github.com/prometheus/client_model v0.2.0 // indirect
github.com/prometheus/common v0.32.1 // indirect
github.com/prometheus/statsd_exporter v0.21.0 // indirect
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1764,6 +1764,8 @@ github.com/prometheus/client_golang v1.7.1/go.mod h1:PY5Wy2awLA44sXw4AOSfFBetzPP
github.com/prometheus/client_golang v1.10.0/go.mod h1:WJM3cc3yu7XKBKa/I8WeZm+V3eltZnBwfENSU7mdogU=
github.com/prometheus/client_golang v1.11.0 h1:HNkLOAEQMIDv/K+04rukrLx6ch7msSRwf3/SASFAGtQ=
github.com/prometheus/client_golang v1.11.0/go.mod h1:Z6t4BnS23TR94PD6BsDNk8yVqroYurpAkEiz0P2BEV0=
github.com/prometheus/client_golang v1.11.1 h1:+4eQaD7vAZ6DsfsxB15hbE0odUjGI5ARs9yskGu1v4s=
github.com/prometheus/client_golang v1.11.1/go.mod h1:Z6t4BnS23TR94PD6BsDNk8yVqroYurpAkEiz0P2BEV0=
github.com/prometheus/client_model v0.0.0-20171117100541-99fa1f4be8e5/go.mod h1:MbSGuTsp3dbXC40dX6PRTWyKYBIrTGTE9sqQNg2J8bo=
github.com/prometheus/client_model v0.0.0-20180712105110-5c3871d89910/go.mod h1:MbSGuTsp3dbXC40dX6PRTWyKYBIrTGTE9sqQNg2J8bo=
github.com/prometheus/client_model v0.0.0-20190115171406-56726106282f/go.mod h1:MbSGuTsp3dbXC40dX6PRTWyKYBIrTGTE9sqQNg2J8bo=
Expand Down Expand Up @@ -3127,6 +3129,8 @@ knative.dev/hack/schema v0.0.0-20220224013837-e1785985d364 h1:iuM9VFi9cjSNuVc3x5
knative.dev/hack/schema v0.0.0-20220224013837-e1785985d364/go.mod h1:ffjwmdcrH5vN3mPhO8RrF2KfNnbHeCE2C60A+2cv3U0=
knative.dev/pkg v0.0.0-20220314165618-a637a96a1bd9 h1:a7VjID6wqPki2oMvXkisNqnhcNJN4H4ARWLa8ULyOng=
knative.dev/pkg v0.0.0-20220314165618-a637a96a1bd9/go.mod h1:SsH9J6Gz+CvrHmoL0TELJXmMmohqKSQ5bpJvCv+1+ZI=
knative.dev/pkg v0.0.0-20220316002959-3a4cc56708b9 h1:+l2JdQ5m9Pz8jVHq/dv74L8YR2nM1vr7CR5BiMP5fEs=
knative.dev/pkg v0.0.0-20220316002959-3a4cc56708b9/go.mod h1:3r6srDeiuiG5DXSGfIe12r2U3Tj5JeeHnQaIXPe/4Zc=
mvdan.cc/gofumpt v0.1.1/go.mod h1:yXG1r1WqZVKWbVRtBWKWX9+CxGYfA51nSomhM0woR48=
mvdan.cc/interfacer v0.0.0-20180901003855-c20040233aed/go.mod h1:Xkxe497xwlCKkIaQYRfC7CSLworTXY9RMqwhhCm+8Nc=
mvdan.cc/lint v0.0.0-20170908181259-adc824a0674b/go.mod h1:2odslEg/xrtNQqCYg2/jCoyKnw3vv5biOc3JnIcYfL4=
Expand Down
17 changes: 9 additions & 8 deletions pkg/reconciler/clusterimagepolicy/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,15 @@ import (
// Use the informer factory that restricts only to our namespace. This way
// we won't have to grant too broad RBAC rights, nor have trouble starting
// up if we don't have them.
nsinformerfactory "knative.dev/pkg/injection/clients/namespacedkube/informers/factory"

pkgreconciler "knative.dev/pkg/reconciler"
"knative.dev/pkg/system"

"github.com/sigstore/cosign/pkg/apis/config"
clusterimagepolicyinformer "github.com/sigstore/cosign/pkg/client/injection/informers/cosigned/v1alpha1/clusterimagepolicy"
clusterimagepolicyreconciler "github.com/sigstore/cosign/pkg/client/injection/reconciler/cosigned/v1alpha1/clusterimagepolicy"
cminformer "knative.dev/pkg/injection/clients/namespacedkube/informers/core/v1/configmap"
secretinformer "knative.dev/pkg/injection/clients/namespacedkube/informers/core/v1/secret"
)

// This is what the default finalizer name is, but make it explicit so we can
Expand All @@ -47,19 +48,19 @@ func NewController(
cmw configmap.Watcher,
) *controller.Impl {
clusterimagepolicyInformer := clusterimagepolicyinformer.Get(ctx)
nsSecretInformer := nsinformerfactory.Get(ctx).Core().V1().Secrets()
nsConfigMapInformer := nsinformerfactory.Get(ctx).Core().V1().ConfigMaps()
secretInformer := secretinformer.Get(ctx)
configMapInformer := cminformer.Get(ctx)

// Start the informers we got from the SharedInformerFactory above because
// injection doesn't do that for us since we're injecting the Factory and
// not the informers.
if err := controller.StartInformers(ctx.Done(), nsSecretInformer.Informer(), nsConfigMapInformer.Informer()); err != nil {
if err := controller.StartInformers(ctx.Done(), secretInformer.Informer(), configMapInformer.Informer()); err != nil {
logging.FromContext(ctx).Fatalf("Failed to start informers: %w", err)
}

r := &Reconciler{
secretlister: nsSecretInformer.Lister(),
configmaplister: nsConfigMapInformer.Lister(),
secretlister: secretInformer.Lister(),
configmaplister: configMapInformer.Lister(),
kubeclient: kubeclient.Get(ctx),
}
impl := clusterimagepolicyreconciler.NewImpl(ctx, r, func(impl *controller.Impl) controller.Options {
Expand All @@ -69,7 +70,7 @@ func NewController(

clusterimagepolicyInformer.Informer().AddEventHandler(controller.HandleAll(impl.Enqueue))

nsSecretInformer.Informer().AddEventHandler(controller.HandleAll(
secretInformer.Informer().AddEventHandler(controller.HandleAll(
// Call the tracker's OnChanged method, but we've seen the objects
// coming through this path missing TypeMeta, so ensure it is properly
// populated.
Expand All @@ -93,7 +94,7 @@ func NewController(
// We could also fetch/construct the store and use CM watcher for it, but
// since we need a lister for it anyways in the reconciler, just set up
// the watch here.
nsConfigMapInformer.Informer().AddEventHandler(cache.FilteringResourceEventHandler{
configMapInformer.Informer().AddEventHandler(cache.FilteringResourceEventHandler{
FilterFunc: pkgreconciler.ChainFilterFuncs(
pkgreconciler.NamespaceFilterFunc(system.Namespace()),
pkgreconciler.NameFilterFunc(config.ImagePoliciesConfigName)),
Expand Down
9 changes: 7 additions & 2 deletions pkg/reconciler/clusterimagepolicy/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,21 @@ import (
"testing"

"knative.dev/pkg/configmap"
. "knative.dev/pkg/reconciler/testing"
rtesting "knative.dev/pkg/reconciler/testing"

// Fake injection informers
_ "github.com/sigstore/cosign/pkg/client/injection/informers/cosigned/v1alpha1/clusterimagepolicy/fake"
_ "knative.dev/pkg/client/injection/kube/informers/core/v1/configmap/fake"
fakecm "knative.dev/pkg/injection/clients/namespacedkube/informers/core/v1/configmap/fake"
fakesecret "knative.dev/pkg/injection/clients/namespacedkube/informers/core/v1/secret/fake"
_ "knative.dev/pkg/injection/clients/namespacedkube/informers/factory/fake"
)

func TestNew(t *testing.T) {
ctx, _ := SetupFakeContext(t)
ctx, _ := rtesting.SetupFakeContext(t)

fakesecret.Get(ctx)
fakecm.Get(ctx)

c := NewController(ctx, &configmap.ManualWatcher{})

Expand Down

0 comments on commit bb02814

Please sign in to comment.