Skip to content

Commit 81616cc

Browse files
committed
Set RevisionHistoryLimit per Deployment
Problem: OLM does not set the revision history limit for deployments it creates for an operator. This value defaults to 10. If OLM has to update a deployment multiple times when reconciling a CSV, up to 11 replicaSets may exist. Solution: Have OLM set the revision history limit on deployments to 1, keeping only the current and previous replicaSets for a deployment.
1 parent d2dc60a commit 81616cc

File tree

2 files changed

+20
-2
lines changed

2 files changed

+20
-2
lines changed

pkg/controller/install/deployment.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,14 @@ func (i *StrategyDeploymentInstaller) deploymentForSpec(name string, spec appsv1
152152

153153
hash = HashDeploymentSpec(dep.Spec)
154154
dep.Labels[DeploymentSpecHashLabelKey] = hash
155+
156+
// OLM does not support Rollbacks.
157+
// By default, each deployment created by OLM could spawn up to 10 replicaSets.
158+
// By setting the deployments revisionHistoryLimit to 1, OLM will only create up
159+
// to 2 ReplicaSets per deployment it manages, saving memory.
160+
revisionHistoryLimit := int32(1)
161+
dep.Spec.RevisionHistoryLimit = &revisionHistoryLimit
162+
155163
deployment = dep
156164
return
157165
}

pkg/controller/install/deployment_test.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,8 @@ func TestInstallStrategyDeploymentInstallDeployments(t *testing.T) {
101101
Controller: &ownerutil.NotController,
102102
BlockOwnerDeletion: &ownerutil.DontBlockOwnerDeletion,
103103
}}
104+
expectedRevisionHistoryLimit = int32(1)
105+
defaultRevisionHistoryLimit = int32(10)
104106
)
105107

106108
type inputs struct {
@@ -126,11 +128,15 @@ func TestInstallStrategyDeploymentInstallDeployments(t *testing.T) {
126128
strategyDeploymentSpecs: []v1alpha1.StrategyDeploymentSpec{
127129
{
128130
Name: "test-deployment-1",
129-
Spec: appsv1.DeploymentSpec{},
131+
Spec: appsv1.DeploymentSpec{
132+
RevisionHistoryLimit: &defaultRevisionHistoryLimit,
133+
},
130134
},
131135
{
132136
Name: "test-deployment-2",
133-
Spec: appsv1.DeploymentSpec{},
137+
Spec: appsv1.DeploymentSpec{
138+
RevisionHistoryLimit: nil,
139+
},
134140
},
135141
{
136142
Name: "test-deployment-3",
@@ -168,6 +174,7 @@ func TestInstallStrategyDeploymentInstallDeployments(t *testing.T) {
168174
},
169175
},
170176
Spec: appsv1.DeploymentSpec{
177+
RevisionHistoryLimit: &expectedRevisionHistoryLimit,
171178
Template: corev1.PodTemplateSpec{
172179
ObjectMeta: metav1.ObjectMeta{
173180
Annotations: map[string]string{},
@@ -189,6 +196,7 @@ func TestInstallStrategyDeploymentInstallDeployments(t *testing.T) {
189196
},
190197
},
191198
Spec: appsv1.DeploymentSpec{
199+
RevisionHistoryLimit: &expectedRevisionHistoryLimit,
192200
Template: corev1.PodTemplateSpec{
193201
ObjectMeta: metav1.ObjectMeta{
194202
Annotations: map[string]string{},
@@ -210,6 +218,7 @@ func TestInstallStrategyDeploymentInstallDeployments(t *testing.T) {
210218
},
211219
},
212220
Spec: appsv1.DeploymentSpec{
221+
RevisionHistoryLimit: &expectedRevisionHistoryLimit,
213222
Template: corev1.PodTemplateSpec{
214223
ObjectMeta: metav1.ObjectMeta{
215224
Annotations: map[string]string{},
@@ -234,6 +243,7 @@ func TestInstallStrategyDeploymentInstallDeployments(t *testing.T) {
234243
dep := fakeClient.CreateOrUpdateDeploymentArgsForCall(i)
235244
expectedDeployment.Spec.Template.Annotations = map[string]string{}
236245
require.Equal(t, expectedDeployment.OwnerReferences, dep.OwnerReferences)
246+
require.Equal(t, expectedDeployment.Spec.RevisionHistoryLimit, dep.Spec.RevisionHistoryLimit)
237247
}(i, m.expectedDeployment)
238248
}
239249

0 commit comments

Comments
 (0)