Skip to content

Commit f8217b7

Browse files
Merge pull request #2274 from dinhxuanvu/cm-reduce
Add managed label to configmap to reduce cache size
2 parents 3f4f6a9 + 9d7732c commit f8217b7

File tree

4 files changed

+46
-6
lines changed

4 files changed

+46
-6
lines changed

pkg/controller/bundle/bundle_unpacker.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626
"github.com/operator-framework/api/pkg/operators/reference"
2727
operatorsv1alpha1 "github.com/operator-framework/api/pkg/operators/v1alpha1"
2828
listersoperatorsv1alpha1 "github.com/operator-framework/operator-lifecycle-manager/pkg/api/client/listers/operators/v1alpha1"
29+
"github.com/operator-framework/operator-lifecycle-manager/pkg/controller/install"
2930
"github.com/operator-framework/operator-lifecycle-manager/pkg/controller/registry/resolver/projection"
3031
)
3132

@@ -560,10 +561,25 @@ func (c *ConfigMapUnpacker) ensureConfigmap(csRef *corev1.ObjectReference, name
560561
fresh.SetNamespace(csRef.Namespace)
561562
fresh.SetName(name)
562563
fresh.SetOwnerReferences([]metav1.OwnerReference{ownerRef(csRef)})
564+
fresh.SetLabels(map[string]string{install.OLMManagedLabelKey: install.OLMManagedLabelValue})
563565

564566
cm, err = c.cmLister.ConfigMaps(fresh.GetNamespace()).Get(fresh.GetName())
565567
if apierrors.IsNotFound(err) {
566568
cm, err = c.client.CoreV1().ConfigMaps(fresh.GetNamespace()).Create(context.TODO(), fresh, metav1.CreateOptions{})
569+
// CM already exists in cluster but not in cache, then add the label
570+
if err != nil && apierrors.IsAlreadyExists(err) {
571+
cm, err = c.client.CoreV1().ConfigMaps(fresh.GetNamespace()).Get(context.TODO(), fresh.GetName(), metav1.GetOptions{})
572+
if err != nil {
573+
return nil, fmt.Errorf("Failed to retrieve configmap %s: %v", fresh.GetName(), err)
574+
}
575+
cm.SetLabels(map[string]string{
576+
install.OLMManagedLabelKey: install.OLMManagedLabelValue,
577+
})
578+
cm, err = c.client.CoreV1().ConfigMaps(cm.GetNamespace()).Update(context.TODO(), cm, metav1.UpdateOptions{})
579+
if err != nil {
580+
return nil, fmt.Errorf("Failed to update configmap %s: %v", cm.GetName(), err)
581+
}
582+
}
567583
}
568584

569585
return

pkg/controller/bundle/bundle_unpacker_test.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
operatorsv1alpha1 "github.com/operator-framework/api/pkg/operators/v1alpha1"
2020
crfake "github.com/operator-framework/operator-lifecycle-manager/pkg/api/client/clientset/versioned/fake"
2121
crinformers "github.com/operator-framework/operator-lifecycle-manager/pkg/api/client/informers/externalversions"
22+
"github.com/operator-framework/operator-lifecycle-manager/pkg/controller/install"
2223
"github.com/operator-framework/operator-registry/pkg/api"
2324
"github.com/operator-framework/operator-registry/pkg/configmap"
2425
)
@@ -180,6 +181,7 @@ func TestConfigMapUnpacker(t *testing.T) {
180181
ObjectMeta: metav1.ObjectMeta{
181182
Name: pathHash,
182183
Namespace: "ns-a",
184+
Labels: map[string]string{install.OLMManagedLabelKey: install.OLMManagedLabelValue},
183185
OwnerReferences: []metav1.OwnerReference{
184186
{
185187
APIVersion: "operators.coreos.com/v1alpha1",
@@ -570,6 +572,7 @@ func TestConfigMapUnpacker(t *testing.T) {
570572
ObjectMeta: metav1.ObjectMeta{
571573
Name: pathHash,
572574
Namespace: "ns-a",
575+
Labels: map[string]string{install.OLMManagedLabelKey: install.OLMManagedLabelValue},
573576
OwnerReferences: []metav1.OwnerReference{
574577
{
575578
APIVersion: "operators.coreos.com/v1alpha1",
@@ -1305,7 +1308,10 @@ func TestConfigMapUnpacker(t *testing.T) {
13051308

13061309
period := 5 * time.Minute
13071310
factory := informers.NewSharedInformerFactory(client, period)
1308-
cmLister := factory.Core().V1().ConfigMaps().Lister()
1311+
configMapInformer := informers.NewSharedInformerFactoryWithOptions(client, period, informers.WithTweakListOptions(func(options *metav1.ListOptions) {
1312+
options.LabelSelector = install.OLMManagedLabelKey
1313+
})).Core().V1().ConfigMaps()
1314+
cmLister := configMapInformer.Lister()
13091315
jobLister := factory.Batch().V1().Jobs().Lister()
13101316
podLister := factory.Core().V1().Pods().Lister()
13111317
roleLister := factory.Rbac().V1().Roles().Lister()

pkg/controller/operators/catalog/operator.go

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@ import (
1111
"sync"
1212
"time"
1313

14-
k8serrors "k8s.io/apimachinery/pkg/api/errors"
15-
1614
errorwrap "github.com/pkg/errors"
1715
"github.com/sirupsen/logrus"
1816
"google.golang.org/grpc/connectivity"
@@ -23,6 +21,7 @@ import (
2321
apiextensionsv1beta1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1beta1"
2422
"k8s.io/apiextensions-apiserver/pkg/apiserver/validation"
2523
extinf "k8s.io/apiextensions-apiserver/pkg/client/informers/externalversions"
24+
k8serrors "k8s.io/apimachinery/pkg/api/errors"
2625
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2726
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
2827
"k8s.io/apimachinery/pkg/labels"
@@ -48,6 +47,7 @@ import (
4847
"github.com/operator-framework/operator-lifecycle-manager/pkg/api/client/informers/externalversions"
4948
"github.com/operator-framework/operator-lifecycle-manager/pkg/controller/bundle"
5049
olmerrors "github.com/operator-framework/operator-lifecycle-manager/pkg/controller/errors"
50+
"github.com/operator-framework/operator-lifecycle-manager/pkg/controller/install"
5151
"github.com/operator-framework/operator-lifecycle-manager/pkg/controller/operators/catalog/subscription"
5252
"github.com/operator-framework/operator-lifecycle-manager/pkg/controller/registry"
5353
"github.com/operator-framework/operator-lifecycle-manager/pkg/controller/registry/grpc"
@@ -341,7 +341,9 @@ func NewOperator(ctx context.Context, kubeconfigPath string, clock utilclock.Clo
341341
sharedIndexInformers = append(sharedIndexInformers, buPodInformer.Informer())
342342

343343
// Wire ConfigMaps
344-
configMapInformer := k8sInformerFactory.Core().V1().ConfigMaps()
344+
configMapInformer := informers.NewSharedInformerFactoryWithOptions(op.opClient.KubernetesInterface(), resyncPeriod(), informers.WithTweakListOptions(func(options *metav1.ListOptions) {
345+
options.LabelSelector = install.OLMManagedLabelKey
346+
})).Core().V1().ConfigMaps()
345347
op.lister.CoreV1().RegisterConfigMapLister(metav1.NamespaceAll, configMapInformer.Lister())
346348
sharedIndexInformers = append(sharedIndexInformers, configMapInformer.Informer())
347349

@@ -595,15 +597,31 @@ func (o *Operator) syncConfigMap(logger *logrus.Entry, in *v1alpha1.CatalogSourc
595597

596598
logger.Debug("checking catsrc configmap state")
597599

600+
var updateLabel bool
598601
// Get the catalog source's config map
599602
configMap, err := o.lister.CoreV1().ConfigMapLister().ConfigMaps(in.GetNamespace()).Get(in.Spec.ConfigMap)
603+
// Attempt to look up the CM via api call if there is a cache miss
604+
if k8serrors.IsNotFound(err) {
605+
configMap, err = o.opClient.KubernetesInterface().CoreV1().ConfigMaps(in.GetNamespace()).Get(context.TODO(), in.Spec.ConfigMap, metav1.GetOptions{})
606+
// Found cm in the cluster, add managed label to configmap
607+
if err == nil {
608+
labels := configMap.GetLabels()
609+
if labels == nil {
610+
labels = make(map[string]string)
611+
}
612+
613+
labels[install.OLMManagedLabelKey] = "false"
614+
configMap.SetLabels(labels)
615+
updateLabel = true
616+
}
617+
}
600618
if err != nil {
601619
syncError = fmt.Errorf("failed to get catalog config map %s: %s", in.Spec.ConfigMap, err)
602620
out.SetError(v1alpha1.CatalogSourceConfigMapError, syncError)
603621
return
604622
}
605623

606-
if wasOwned := ownerutil.EnsureOwner(configMap, in); !wasOwned {
624+
if wasOwned := ownerutil.EnsureOwner(configMap, in); !wasOwned || updateLabel {
607625
configMap, err = o.opClient.KubernetesInterface().CoreV1().ConfigMaps(configMap.GetNamespace()).Update(context.TODO(), configMap, metav1.UpdateOptions{})
608626
if err != nil {
609627
syncError = fmt.Errorf("unable to write owner onto catalog source configmap - %v", err)

pkg/controller/operators/catalog/operator_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -902,7 +902,7 @@ func TestSyncCatalogSources(t *testing.T) {
902902
&corev1.ConfigMap{},
903903
},
904904
expectedStatus: nil,
905-
expectedError: errors.New("failed to get catalog config map cool-configmap: configmap \"cool-configmap\" not found"),
905+
expectedError: errors.New("failed to get catalog config map cool-configmap: configmaps \"cool-configmap\" not found"),
906906
},
907907
{
908908
testName: "CatalogSourceWithGrpcImage",

0 commit comments

Comments
 (0)