Skip to content

Commit f83b235

Browse files
Merge pull request #1745 from awgreene/hco-bug
Bug 1874938: Set RevisionHistoryLimit per Deployment
2 parents 8d4d9aa + 724ce6b commit f83b235

File tree

3 files changed

+30
-6
lines changed

3 files changed

+30
-6
lines changed

pkg/controller/install/deployment.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,8 +150,16 @@ func (i *StrategyDeploymentInstaller) deploymentForSpec(name string, spec appsv1
150150
return
151151
}
152152

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

pkg/controller/install/deployment_test.go

Lines changed: 15 additions & 3 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

@@ -299,6 +309,7 @@ func TestInstallStrategyDeploymentCheckInstallErrors(t *testing.T) {
299309
},
300310
}
301311

312+
revisionHistoryLimit := int32(1)
302313
for _, tt := range tests {
303314
t.Run(tt.description, func(t *testing.T) {
304315
fakeClient := new(clientfakes.FakeInstallStrategyDeploymentInterface)
@@ -307,6 +318,7 @@ func TestInstallStrategyDeploymentCheckInstallErrors(t *testing.T) {
307318

308319
dep := testDeployment("olm-dep-1", namespace, &mockOwner)
309320
dep.Spec.Template.SetAnnotations(map[string]string{"test": "annotation"})
321+
dep.Spec.RevisionHistoryLimit = &revisionHistoryLimit
310322
dep.SetLabels(labels.CloneAndAddLabel(dep.ObjectMeta.GetLabels(), DeploymentSpecHashLabelKey, HashDeploymentSpec(dep.Spec)))
311323
fakeClient.FindAnyDeploymentsMatchingLabelsReturns(
312324
[]*appsv1.Deployment{
@@ -323,6 +335,7 @@ func TestInstallStrategyDeploymentCheckInstallErrors(t *testing.T) {
323335

324336
deployment := testDeployment("olm-dep-1", namespace, &mockOwner)
325337
deployment.Spec.Template.SetAnnotations(map[string]string{"test": "annotation"})
338+
deployment.Spec.RevisionHistoryLimit = &revisionHistoryLimit
326339
deployment.SetLabels(labels.CloneAndAddLabel(dep.ObjectMeta.GetLabels(), DeploymentSpecHashLabelKey, HashDeploymentSpec(deployment.Spec)))
327340
fakeClient.CreateOrUpdateDeploymentReturns(&deployment, tt.createDeploymentErr)
328341
defer func() {
@@ -332,7 +345,6 @@ func TestInstallStrategyDeploymentCheckInstallErrors(t *testing.T) {
332345
if tt.createDeploymentErr != nil {
333346
err := installer.Install(strategy)
334347
require.Error(t, err)
335-
return
336348
}
337349
})
338350
}

pkg/controller/operators/olm/operator_test.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"crypto/x509"
99
"crypto/x509/pkix"
1010
"fmt"
11-
opregistry "github.com/operator-framework/operator-registry/pkg/registry"
1211
"math"
1312
"math/big"
1413
"reflect"
@@ -62,6 +61,7 @@ import (
6261
"github.com/operator-framework/operator-lifecycle-manager/pkg/lib/ownerutil"
6362
"github.com/operator-framework/operator-lifecycle-manager/pkg/lib/queueinformer"
6463
"github.com/operator-framework/operator-lifecycle-manager/pkg/lib/scoped"
64+
opregistry "github.com/operator-framework/operator-registry/pkg/registry"
6565
)
6666

6767
type TestStrategy struct{}
@@ -326,7 +326,10 @@ func buildFakeAPIIntersectionReconcilerThatReturns(result resolver.APIReconcilia
326326
}
327327

328328
func deployment(deploymentName, namespace, serviceAccountName string, templateAnnotations map[string]string) *appsv1.Deployment {
329-
var singleInstance = int32(1)
329+
var (
330+
singleInstance = int32(1)
331+
revisionHistoryLimit = int32(1)
332+
)
330333
return &appsv1.Deployment{
331334
ObjectMeta: metav1.ObjectMeta{
332335
Name: deploymentName,
@@ -338,7 +341,8 @@ func deployment(deploymentName, namespace, serviceAccountName string, templateAn
338341
"app": deploymentName,
339342
},
340343
},
341-
Replicas: &singleInstance,
344+
RevisionHistoryLimit: &revisionHistoryLimit,
345+
Replicas: &singleInstance,
342346
Template: corev1.PodTemplateSpec{
343347
ObjectMeta: metav1.ObjectMeta{
344348
Labels: map[string]string{

0 commit comments

Comments
 (0)