Skip to content

Commit

Permalink
Update context
Browse files Browse the repository at this point in the history
  • Loading branch information
ciarams87 committed May 23, 2023
1 parent 344f343 commit 752cebd
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 47 deletions.
56 changes: 56 additions & 0 deletions internal/certmanager/test_files/context.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
Copyright 2020 The cert-manager Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package test

import (
"context"

kubeinformers "k8s.io/client-go/informers"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/record"
"k8s.io/utils/clock"

clientset "github.com/cert-manager/cert-manager/pkg/client/clientset/versioned"
informers "github.com/cert-manager/cert-manager/pkg/client/informers/externalversions"
)

// Context contains various types that are used by controller implementations.
type Context struct {
// RootContext is the root context for the controller
RootContext context.Context

StopCh <-chan struct{}
// RESTConfig is the loaded Kubernetes apiserver rest client configuration
RESTConfig *rest.Config
Client kubernetes.Interface
CMClient clientset.Interface

Recorder record.EventRecorder

KubeSharedInformerFactory kubeinformers.SharedInformerFactory
SharedInformerFactory informers.SharedInformerFactory

ContextOptions
}

// ContextOptions are static Controller Context options.
type ContextOptions struct {
Kubeconfig string
Namespace string
Clock clock.Clock
}
51 changes: 4 additions & 47 deletions internal/certmanager/test_files/context_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,26 +24,20 @@ import (
"testing"
"time"

networkingv1 "k8s.io/api/networking/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
utilerrors "k8s.io/apimachinery/pkg/util/errors"
kubeinformers "k8s.io/client-go/informers"
kubefake "k8s.io/client-go/kubernetes/fake"
"k8s.io/client-go/rest"
coretesting "k8s.io/client-go/testing"
"k8s.io/client-go/tools/cache"
"k8s.io/utils/clock"
fakeclock "k8s.io/utils/clock/testing"

apiutil "github.com/cert-manager/cert-manager/pkg/api/util"
cmfake "github.com/cert-manager/cert-manager/pkg/client/clientset/versioned/fake"
informers "github.com/cert-manager/cert-manager/pkg/client/informers/externalversions"
"github.com/cert-manager/cert-manager/pkg/controller"
"github.com/cert-manager/cert-manager/pkg/logs"
"github.com/cert-manager/cert-manager/pkg/metrics"
"github.com/cert-manager/cert-manager/pkg/util"
discoveryfake "github.com/cert-manager/cert-manager/test/unit/discovery"
k8s_nginx "github.com/nginxinc/kubernetes-ingress/pkg/client/clientset/versioned"
vsfake "github.com/nginxinc/kubernetes-ingress/pkg/client/clientset/versioned/fake"
vsinformers "github.com/nginxinc/kubernetes-ingress/pkg/client/informers/externalversions"
Expand Down Expand Up @@ -77,11 +71,10 @@ type Builder struct {
// test).
CheckFn func(*Builder, ...interface{})

stopCh chan struct{}
requiredReactors map[string]bool
additionalSyncFuncs []cache.InformerSynced
stopCh chan struct{}
requiredReactors map[string]bool

*controller.Context
*Context
}

func (b *Builder) generateNameReactor(action coretesting.Action) (handled bool, ret runtime.Object, err error) {
Expand All @@ -100,7 +93,7 @@ const informerResyncPeriod = time.Millisecond * 10
// for any unset fields.
func (b *Builder) Init() {
if b.Context == nil {
b.Context = &controller.Context{
b.Context = &Context{
RootContext: context.Background(),
}
}
Expand All @@ -111,29 +104,6 @@ func (b *Builder) Init() {
b.Client = kubefake.NewSimpleClientset(b.KubeObjects...)
b.CMClient = cmfake.NewSimpleClientset(b.CertManagerObjects...)
b.VSClient = vsfake.NewSimpleClientset(b.VSObjects...)
b.DiscoveryClient = discoveryfake.NewDiscovery().WithServerResourcesForGroupVersion(func(groupVersion string) (*metav1.APIResourceList, error) {
if groupVersion == networkingv1.SchemeGroupVersion.String() {
return &metav1.APIResourceList{
TypeMeta: metav1.TypeMeta{},
GroupVersion: networkingv1.SchemeGroupVersion.String(),
APIResources: []metav1.APIResource{
{
Name: "ingresses",
SingularName: "Ingress",
Namespaced: true,
Group: networkingv1.GroupName,
Version: networkingv1.SchemeGroupVersion.Version,
Kind: networkingv1.SchemeGroupVersion.WithKind("Ingress").Kind,
Verbs: metav1.Verbs{"get", "list", "watch", "create", "update", "patch", "delete", "deletecollection"},
ShortNames: []string{"ing"},
Categories: []string{"all"},
StorageVersionHash: "testing",
},
},
}, nil
}
return &metav1.APIResourceList{}, nil
})
b.Recorder = new(FakeRecorder)
b.FakeKubeClient().PrependReactor("create", "*", b.generateNameReactor)
b.FakeCMClient().PrependReactor("create", "*", b.generateNameReactor)
Expand All @@ -142,7 +112,6 @@ func (b *Builder) Init() {
b.SharedInformerFactory = informers.NewSharedInformerFactory(b.CMClient, informerResyncPeriod)
b.VsSharedInformerFactory = vsinformers.NewSharedInformerFactory(b.VSClient, informerResyncPeriod)
b.stopCh = make(chan struct{})
b.Metrics = metrics.New(logs.Log, clock.RealClock{})

// set the Clock on the context
if b.Clock == nil {
Expand Down Expand Up @@ -328,21 +297,9 @@ func (b *Builder) Sync() {
if err := mustAllSync(b.VsSharedInformerFactory.WaitForCacheSync(b.stopCh)); err != nil {
panic("Error waiting for VSShared to sync: " + err.Error())
}
if b.additionalSyncFuncs != nil {
cache.WaitForCacheSync(b.stopCh, b.additionalSyncFuncs...)
}
time.Sleep(informerResyncPeriod * 3)
}

// RegisterAdditionalSyncFuncs registers an additional InformerSynced function
// with the builder.
// When the Sync method is called, the builder will also wait for the given
// listers to be synced as well as the listers that were registered with the
// informer factories that the builder provides.
func (b *Builder) RegisterAdditionalSyncFuncs(fns ...cache.InformerSynced) {
b.additionalSyncFuncs = append(b.additionalSyncFuncs, fns...)
}

func (b *Builder) Events() []string {
if e, ok := b.Recorder.(*FakeRecorder); ok {
return e.Events
Expand Down

0 comments on commit 752cebd

Please sign in to comment.