Skip to content

Commit

Permalink
Merge pull request operator-framework#569 from njhale/caches-for-ever…
Browse files Browse the repository at this point in the history
…yone

feat(olm): use cache listers in olm-operator
  • Loading branch information
njhale authored Nov 13, 2018
2 parents 3c2d66b + 1028d92 commit 0dd31c7
Show file tree
Hide file tree
Showing 33 changed files with 3,236 additions and 69 deletions.
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,8 @@ container-mockgen:
docker build -t olm:mockgen -f mockgen.Dockerfile .
docker run --name temp-mockgen olm:mockgen /bin/true
docker cp temp-mockgen:/go/src/github.com/operator-framework/operator-lifecycle-manager/pkg/api/wrappers/wrappersfakes/. ./pkg/api/wrappers/wrappersfakes
docker cp temp-mockgen:/go/src/github.com/operator-framework/operator-lifecycle-manager/pkg/fakes/. ./pkg/fakes
docker cp temp-mockgen:/go/src/github.com/operator-framework/operator-lifecycle-manager/pkg/fakes/client-go/listers/. ./pkg/fakes/client-go/listers
docker cp temp-mockgen:/go/src/github.com/operator-framework/operator-lifecycle-manager/pkg/lib/operatorlister/operatorlisterfakes/. ./pkg/lib/operatorlister/operatorlisterfakes
docker cp temp-mockgen:/go/src/github.com/operator-framework/operator-lifecycle-manager/pkg/lib/operatorclient/mock_client.go ./pkg/lib/operatorclient/mock_client.go
docker rm temp-mockgen

Expand Down
3 changes: 3 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -214,12 +214,14 @@ k8s.io/api v0.0.0-20181108095152-eee84a6322ca h1:0gIeW03B5m7yni69Y95oPgDXv7ow7pu
k8s.io/api v0.0.0-20181108095152-eee84a6322ca/go.mod h1:iuAfoD4hCxJ8Onx9kaTIt30j7jUFS00AXQi6QMi99vA=
k8s.io/apiextensions-apiserver v0.0.0-20180905004947-16750353bf97 h1:s4lWWs6JN5kWVzk5bztddkr5kgO/cGIbqTDP+QttUeQ=
k8s.io/apiextensions-apiserver v0.0.0-20180905004947-16750353bf97/go.mod h1:IxkesAMoaCRoLrPJdZNZUQp9NfZnzqaVzLhb2VEQzXE=
k8s.io/apiextensions-apiserver v0.0.0-20181110192823-2c43ee60e25b h1:O3KqnOdhludLAAHs7bvV7UpPYow3gqLqF5Junc5hbw8=
k8s.io/apimachinery v0.0.0-20181026144827-8ee1a638bafa h1:i0EOpPFWExNx7efINILpw8LJeah7gakRl1zjvwVfjiI=
k8s.io/apimachinery v0.0.0-20181026144827-8ee1a638bafa/go.mod h1:ccL7Eh7zubPUSh9A3USN90/OzHNSVN6zxzde07TDCL0=
k8s.io/apiserver v0.0.0-20181026151315-13cfe3978170 h1:CqI85nZvPaV+7JFono0nAOGOx2brocqefcOhDPVhHKI=
k8s.io/apiserver v0.0.0-20181026151315-13cfe3978170/go.mod h1:6bqaTSOSJavUIXUtfaR9Os9JtTCm8ZqH2SUl2S60C4w=
k8s.io/client-go v8.0.0+incompatible h1:tTI4hRmb1DRMl4fG6Vclfdi6nTM82oIrTT7HfitmxC4=
k8s.io/client-go v8.0.0+incompatible/go.mod h1:7vJpHMYJwNQCWgzmNV+VYUl1zCObLyodBc8nIyt8L5s=
k8s.io/client-go v9.0.0+incompatible h1:2kqW3X2xQ9SbFvWZjGEHBLlWc1LG9JIJNXWkuqwdZ3A=
k8s.io/code-generator v0.0.0-20180904193909-8c97d6ab64da h1:L6YB6ObZIbZlYikTQcCjzZGilwS3OVyQBA2esULs8VM=
k8s.io/code-generator v0.0.0-20180904193909-8c97d6ab64da/go.mod h1:MYiN+ZJZ9HkETbgVZdWw2AsuAi9PZ4V80cwfuf2axe8=
k8s.io/gengo v0.0.0-20181106084056-51747d6e00da h1:ZMvcXtMVbhUCtCuiSEzBV+Eur4swzfdxx6ZyX3qT6dk=
Expand All @@ -228,6 +230,7 @@ k8s.io/klog v0.0.0-20181102134211-b9b56d5dfc92 h1:PgoMI/L1Nu5Vmvgm+vGheLuxKST8h6
k8s.io/klog v0.0.0-20181102134211-b9b56d5dfc92/go.mod h1:Gq+BEi5rUBO/HRz0bTSXDUcqjScdoY3a9IHpCEIOOfk=
k8s.io/kube-aggregator v0.0.0-20180905000155-efa32eb095fe h1:LM48rywzVEPRg+Os2oUL9/vsztPQGoxmiD3m5VySchw=
k8s.io/kube-aggregator v0.0.0-20180905000155-efa32eb095fe/go.mod h1:8sbzT4QQKDEmSCIbfqjV0sd97GpUT7A4W626sBiYJmU=
k8s.io/kube-aggregator v0.0.0-20181110192014-6f96af33cb59 h1:8VFjmCurXo3sMW0ASrUvoE4aT8FCzsIz55uic1EyUIc=
k8s.io/kube-openapi v0.0.0-20181031203759-72693cb1fadd h1:ggv/Vfza0i5xuhUZyYyxcc25AmQvHY8Zi1C2m8WgBvA=
k8s.io/kube-openapi v0.0.0-20181031203759-72693cb1fadd/go.mod h1:BXM9ceUBTj2QnfH2MK1odQs778ajze1RxcmP6S8RVVc=
k8s.io/kubernetes v1.11.5-beta.0.0.20181108064615-3290824d1c7b h1:i9PV0cnNZrleFj2YjDYDAUku1qg/SlKfdm4LdTJzAFo=
Expand Down
17 changes: 10 additions & 7 deletions pkg/api/wrappers/deployment_install_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@
package wrappers

import (
"github.com/operator-framework/operator-lifecycle-manager/pkg/lib/operatorclient"
"github.com/operator-framework/operator-lifecycle-manager/pkg/lib/ownerutil"
"github.com/pkg/errors"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
rbacv1 "k8s.io/api/rbac/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

"github.com/operator-framework/operator-lifecycle-manager/pkg/lib/operatorclient"
"github.com/operator-framework/operator-lifecycle-manager/pkg/lib/operatorlister"
"github.com/operator-framework/operator-lifecycle-manager/pkg/lib/ownerutil"
)

var ErrNilObject = errors.New("Bad object supplied: <nil>")
Expand All @@ -27,14 +29,16 @@ type InstallStrategyDeploymentInterface interface {

type InstallStrategyDeploymentClientForNamespace struct {
opClient operatorclient.ClientInterface
opLister operatorlister.OperatorLister
Namespace string
}

var _ InstallStrategyDeploymentInterface = &InstallStrategyDeploymentClientForNamespace{}

func NewInstallStrategyDeploymentClient(opClient operatorclient.ClientInterface, namespace string) InstallStrategyDeploymentInterface {
func NewInstallStrategyDeploymentClient(opClient operatorclient.ClientInterface, opLister operatorlister.OperatorLister, namespace string) InstallStrategyDeploymentInterface {
return &InstallStrategyDeploymentClientForNamespace{
opClient: opClient,
opLister: opLister,
Namespace: namespace,
}
}
Expand All @@ -52,7 +56,7 @@ func (c *InstallStrategyDeploymentClientForNamespace) EnsureServiceAccount(servi
return nil, ErrNilObject
}

foundAccount, err := c.opClient.GetServiceAccount(c.Namespace, serviceAccount.Name)
foundAccount, err := c.opLister.CoreV1().ServiceAccountLister().ServiceAccounts(c.Namespace).Get(serviceAccount.Name)
if err != nil && !apierrors.IsNotFound(err) {
return nil, errors.Wrap(err, "checking for existing serviceacccount failed")
}
Expand Down Expand Up @@ -96,14 +100,13 @@ func (c *InstallStrategyDeploymentClientForNamespace) CreateOrUpdateDeployment(d
}

func (c *InstallStrategyDeploymentClientForNamespace) GetServiceAccountByName(serviceAccountName string) (*corev1.ServiceAccount, error) {
return c.opClient.KubernetesInterface().CoreV1().ServiceAccounts(c.Namespace).Get(serviceAccountName, metav1.GetOptions{})
return c.opLister.CoreV1().ServiceAccountLister().ServiceAccounts(c.Namespace).Get(serviceAccountName)
}

func (c *InstallStrategyDeploymentClientForNamespace) FindAnyDeploymentsMatchingNames(depNames []string) ([]*appsv1.Deployment, error) {
var deployments []*appsv1.Deployment
for _, depName := range depNames {
fetchedDep, err := c.opClient.GetDeployment(c.Namespace, depName)

fetchedDep, err := c.opLister.AppsV1().DeploymentLister().Deployments(c.Namespace).Get(depName)
if err == nil {
deployments = append(deployments, fetchedDep)
} else {
Expand Down
24 changes: 17 additions & 7 deletions pkg/api/wrappers/deployment_install_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,27 @@ package wrappers
import (
"fmt"
"testing"
"time"

"github.com/golang/mock/gomock"
"github.com/operator-framework/operator-lifecycle-manager/pkg/api/apis/operators/v1alpha1"
"github.com/operator-framework/operator-lifecycle-manager/pkg/lib/operatorclient"
"github.com/pkg/errors"
"github.com/stretchr/testify/require"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/equality"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/diff"

listerfakes "github.com/operator-framework/operator-lifecycle-manager/pkg/fakes/client-go/listers"
"github.com/operator-framework/operator-lifecycle-manager/pkg/api/apis/operators/v1alpha1"
"github.com/operator-framework/operator-lifecycle-manager/pkg/lib/operatorclient"
"github.com/operator-framework/operator-lifecycle-manager/pkg/lib/operatorlister/operatorlisterfakes"
)

var (
Controller = false
BlockOwnerDeletion = false
WakeupInterval = 5 * time.Second
)

func ownerReferenceFromCSV(csv *v1alpha1.ClusterServiceVersion) metav1.OwnerReference {
Expand Down Expand Up @@ -405,13 +410,18 @@ func TestEnsureServiceAccount(t *testing.T) {
t.Run(testName, func(t *testing.T) {
ctrl := gomock.NewController(t)
mockOpClient := operatorclient.NewMockClientInterface(ctrl)
client := NewInstallStrategyDeploymentClient(mockOpClient, tt.state.namespace)
fakeLister := &operatorlisterfakes.FakeOperatorLister{}
fakeCoreV1Lister := &operatorlisterfakes.FakeCoreV1Lister{}
fakeServiceAccountLister := &listerfakes.FakeServiceAccountLister{}
fakeServiceAccountNamespacedLister := &listerfakes.FakeServiceAccountNamespaceLister{}
fakeServiceAccountNamespacedLister.GetReturns(tt.state.existingServiceAccount, tt.state.getServiceAccountError)
fakeServiceAccountLister.ServiceAccountsReturns(fakeServiceAccountNamespacedLister)
fakeCoreV1Lister.ServiceAccountListerReturns(fakeServiceAccountLister)
fakeLister.CoreV1Returns(fakeCoreV1Lister)

mockOpClient.EXPECT().
GetServiceAccount(tt.state.namespace, tt.input.serviceAccountName).
Return(tt.state.existingServiceAccount, tt.state.getServiceAccountError).
AnyTimes()
client := NewInstallStrategyDeploymentClient(mockOpClient, fakeLister, tt.state.namespace)


mockOpClient.EXPECT().
CreateServiceAccount(tt.input.serviceAccount).
Return(tt.state.createServiceAccountResult, tt.state.createServiceAccountError).
Expand Down
7 changes: 4 additions & 3 deletions pkg/controller/install/resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/operator-framework/operator-lifecycle-manager/pkg/api/apis/operators/v1alpha1"
"github.com/operator-framework/operator-lifecycle-manager/pkg/api/wrappers"
"github.com/operator-framework/operator-lifecycle-manager/pkg/lib/operatorclient"
"github.com/operator-framework/operator-lifecycle-manager/pkg/lib/operatorlister"
"github.com/operator-framework/operator-lifecycle-manager/pkg/lib/ownerutil"
)

Expand All @@ -24,7 +25,7 @@ type StrategyInstaller interface {

type StrategyResolverInterface interface {
UnmarshalStrategy(s v1alpha1.NamedInstallStrategy) (strategy Strategy, err error)
InstallerForStrategy(strategyName string, opClient operatorclient.ClientInterface, owner ownerutil.Owner, previousStrategy Strategy) StrategyInstaller
InstallerForStrategy(strategyName string, opClient operatorclient.ClientInterface, opLister operatorlister.OperatorLister, owner ownerutil.Owner, previousStrategy Strategy) StrategyInstaller
}

type StrategyResolver struct{}
Expand All @@ -42,10 +43,10 @@ func (r *StrategyResolver) UnmarshalStrategy(s v1alpha1.NamedInstallStrategy) (s
return
}

func (r *StrategyResolver) InstallerForStrategy(strategyName string, opClient operatorclient.ClientInterface, owner ownerutil.Owner, previousStrategy Strategy) StrategyInstaller {
func (r *StrategyResolver) InstallerForStrategy(strategyName string, opClient operatorclient.ClientInterface, opLister operatorlister.OperatorLister, owner ownerutil.Owner, previousStrategy Strategy) StrategyInstaller {
switch strategyName {
case InstallStrategyNameDeployment:
strategyClient := wrappers.NewInstallStrategyDeploymentClient(opClient, owner.GetNamespace())
strategyClient := wrappers.NewInstallStrategyDeploymentClient(opClient, opLister, owner.GetNamespace())
return NewStrategyDeploymentInstaller(strategyClient, owner, previousStrategy)
}

Expand Down
24 changes: 20 additions & 4 deletions pkg/controller/operators/olm/operator.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"k8s.io/client-go/tools/record"
"k8s.io/client-go/util/workqueue"
kagg "k8s.io/kube-aggregator/pkg/client/informers/externalversions"
aextv1beta1 "k8s.io/apiextensions-apiserver/pkg/client/informers/externalversions"

"github.com/operator-framework/operator-lifecycle-manager/pkg/api/apis/operators/v1alpha1"
"github.com/operator-framework/operator-lifecycle-manager/pkg/api/client/clientset/versioned"
Expand Down Expand Up @@ -145,7 +146,7 @@ func NewOperator(crClient versioned.Interface, opClient operatorclient.ClientInt
op.RegisterQueueInformer(namespaceQueueInformer)
op.lister.CoreV1().RegisterNamespaceLister(namespaceInformer.Lister())

// Register APIService QueueInformers
// Register APIService QueueInformer
apiServiceInformer := kagg.NewSharedInformerFactory(opClient.ApiregistrationV1Interface(), wakeupInterval).Apiregistration().V1().APIServices()
op.RegisterQueueInformer(queueinformer.NewInformer(
workqueue.NewNamedRateLimitingQueue(workqueue.DefaultControllerRateLimiter(), "apiservices"),
Expand All @@ -158,6 +159,20 @@ func NewOperator(crClient versioned.Interface, opClient operatorclient.ClientInt
metrics.NewMetricsNil(),
))
op.lister.APIRegistrationV1().RegisterAPIServiceLister(apiServiceInformer.Lister())

// Register CustomResourceDefinition QueueInformer
customResourceDefinitionInformer := aextv1beta1.NewSharedInformerFactory(opClient.ApiextensionsV1beta1Interface(), wakeupInterval).Apiextensions().V1beta1().CustomResourceDefinitions()
op.RegisterQueueInformer(queueinformer.NewInformer(
workqueue.NewNamedRateLimitingQueue(workqueue.DefaultControllerRateLimiter(), "customresourcedefinitions"),
customResourceDefinitionInformer.Informer(),
op.syncObject,
&cache.ResourceEventHandlerFuncs{
DeleteFunc: op.handleDeletion,
},
"customresourcedefinitions",
metrics.NewMetricsNil(),
))
op.lister.APIExtensionsV1beta1().RegisterCustomResourceDefinitionLister(customResourceDefinitionInformer.Lister())

// Register Secret QueueInformer
secretInformer := informers.NewSharedInformerFactory(opClient.KubernetesInterface(), wakeupInterval).Core().V1().Secrets()
Expand Down Expand Up @@ -214,7 +229,7 @@ func NewOperator(crClient versioned.Interface, opClient operatorclient.ClientInt
// Register queue and QueueInformer
queueName := fmt.Sprintf("%s/clusterserviceversions", namespace)
csvQueue := workqueue.NewNamedRateLimitingQueue(workqueue.DefaultControllerRateLimiter(), queueName)
csvQueueInformer := queueinformer.NewInformer(csvQueue, csvInformer.Informer(), op.syncClusterServiceVersion, csvHandlers, queueName, metrics.NewMetricsCSV(op.client))
csvQueueInformer := queueinformer.NewInformer(csvQueue, csvInformer.Informer(), op.syncClusterServiceVersion, csvHandlers, queueName, metrics.NewMetricsCSV(op.lister.OperatorsV1alpha1().ClusterServiceVersionLister()))
op.RegisterQueueInformer(csvQueueInformer)
op.csvQueues[namespace] = csvQueue
}
Expand Down Expand Up @@ -592,6 +607,7 @@ func (a *Operator) transitionCSVState(in v1alpha1.ClusterServiceVersion) (out *v
for _, csv := range a.findIntermediatesForDeletion(out) {
// we only mark them in this step, in case some get deleted but others fail and break the replacement chain
csv.SetPhaseWithEvent(v1alpha1.CSVPhaseDeleting, v1alpha1.CSVReasonReplaced, "has been replaced by a newer ClusterServiceVersion that has successfully installed.", a.recorder)

// ignore errors and success here; this step is just an optimization to speed up GC
a.client.OperatorsV1alpha1().ClusterServiceVersions(csv.GetNamespace()).UpdateStatus(csv)
a.requeueCSV(csv.GetName(), csv.GetNamespace())
Expand Down Expand Up @@ -726,7 +742,7 @@ func (a *Operator) parseStrategiesAndUpdateStatus(csv *v1alpha1.ClusterServiceVe
}

strName := strategy.GetStrategyName()
installer := a.resolver.InstallerForStrategy(strName, a.OpClient, csv, previousStrategy)
installer := a.resolver.InstallerForStrategy(strName, a.OpClient, a.lister, csv, previousStrategy)
return installer, strategy, previousStrategy
}

Expand Down Expand Up @@ -790,7 +806,7 @@ func (a *Operator) isReplacing(in *v1alpha1.ClusterServiceVersion) *v1alpha1.Clu
if in.Spec.Replaces == "" {
return nil
}
previous, err := a.client.OperatorsV1alpha1().ClusterServiceVersions(in.GetNamespace()).Get(in.Spec.Replaces, metav1.GetOptions{})
previous, err := a.lister.OperatorsV1alpha1().ClusterServiceVersionLister().ClusterServiceVersions(in.GetNamespace()).Get(in.Spec.Replaces)
if err != nil {
log.Debugf("unable to get previous csv: %s", err.Error())
return nil
Expand Down
Loading

0 comments on commit 0dd31c7

Please sign in to comment.