Skip to content

Commit ac075ae

Browse files
Merge pull request #1943 from awgreene/initialize-deps-with-opcond-name
Bug 1913132: Create CSV Deployments with OpCond EnvVar
2 parents abe648a + 602db92 commit ac075ae

File tree

6 files changed

+49
-28
lines changed

6 files changed

+49
-28
lines changed

pkg/controller/install/deployment.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,13 @@ import (
66

77
log "github.com/sirupsen/logrus"
88
appsv1 "k8s.io/api/apps/v1"
9+
corev1 "k8s.io/api/core/v1"
910
k8serrors "k8s.io/apimachinery/pkg/api/errors"
1011
"k8s.io/apimachinery/pkg/util/rand"
1112

1213
"github.com/operator-framework/api/pkg/operators/v1alpha1"
1314
"github.com/operator-framework/operator-lifecycle-manager/pkg/api/wrappers"
15+
"github.com/operator-framework/operator-lifecycle-manager/pkg/controller/operators/olm/overrides/inject"
1416
hashutil "github.com/operator-framework/operator-lifecycle-manager/pkg/lib/kubernetes/pkg/util/hash"
1517
"github.com/operator-framework/operator-lifecycle-manager/pkg/lib/ownerutil"
1618
)
@@ -150,6 +152,12 @@ func (i *StrategyDeploymentInstaller) deploymentForSpec(name string, spec appsv1
150152
return
151153
}
152154

155+
podSpec := &dep.Spec.Template.Spec
156+
inject.InjectEnvIntoDeployment(podSpec, []corev1.EnvVar{{
157+
Name: "OPERATOR_CONDITION_NAME",
158+
Value: i.owner.GetName(),
159+
}})
160+
153161
// OLM does not support Rollbacks.
154162
// By default, each deployment created by OLM could spawn up to 10 replicaSets.
155163
// By setting the deployments revisionHistoryLimit to 1, OLM will only create up

pkg/controller/operators/olm/operator_test.go

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,10 @@ import (
3131
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
3232
"k8s.io/apimachinery/pkg/labels"
3333
"k8s.io/apimachinery/pkg/runtime"
34+
"k8s.io/apimachinery/pkg/types"
3435
utilclock "k8s.io/apimachinery/pkg/util/clock"
3536
"k8s.io/apimachinery/pkg/util/diff"
3637
utilerrors "k8s.io/apimachinery/pkg/util/errors"
37-
"k8s.io/apimachinery/pkg/types"
3838
"k8s.io/apimachinery/pkg/util/intstr"
3939
"k8s.io/apimachinery/pkg/util/wait"
4040
k8sfake "k8s.io/client-go/kubernetes/fake"
@@ -3152,7 +3152,7 @@ func TestWebhookCABundleRetrieval(t *testing.T) {
31523152
},
31533153
desc: v1alpha1.WebhookDescription{
31543154
GenerateName: "webhook",
3155-
Type: v1alpha1.ValidatingAdmissionWebhook,
3155+
Type: v1alpha1.ValidatingAdmissionWebhook,
31563156
},
31573157
},
31583158
expected: expected{
@@ -3181,8 +3181,8 @@ func TestWebhookCABundleRetrieval(t *testing.T) {
31813181
crdWithConversionWebhook(crd("c1", "v1", "g1"), caBundle),
31823182
},
31833183
desc: v1alpha1.WebhookDescription{
3184-
GenerateName: "webhook",
3185-
Type: v1alpha1.ConversionWebhook,
3184+
GenerateName: "webhook",
3185+
Type: v1alpha1.ConversionWebhook,
31863186
ConversionCRDs: []string{"c1.g1"},
31873187
},
31883188
},
@@ -3212,8 +3212,8 @@ func TestWebhookCABundleRetrieval(t *testing.T) {
32123212
crd("c1", "v1", "g1"),
32133213
},
32143214
desc: v1alpha1.WebhookDescription{
3215-
GenerateName: "webhook",
3216-
Type: v1alpha1.ConversionWebhook,
3215+
GenerateName: "webhook",
3216+
Type: v1alpha1.ConversionWebhook,
32173217
ConversionCRDs: []string{"c1.g1"},
32183218
},
32193219
},
@@ -3263,7 +3263,7 @@ func TestWebhookCABundleRetrieval(t *testing.T) {
32633263
},
32643264
desc: v1alpha1.WebhookDescription{
32653265
GenerateName: "webhook",
3266-
Type: v1alpha1.ValidatingAdmissionWebhook,
3266+
Type: v1alpha1.ValidatingAdmissionWebhook,
32673267
},
32683268
},
32693269
expected: expected{
@@ -3312,7 +3312,7 @@ func TestWebhookCABundleRetrieval(t *testing.T) {
33123312
},
33133313
desc: v1alpha1.WebhookDescription{
33143314
GenerateName: "webhook",
3315-
Type: v1alpha1.MutatingAdmissionWebhook,
3315+
Type: v1alpha1.MutatingAdmissionWebhook,
33163316
},
33173317
},
33183318
expected: expected{
@@ -3679,6 +3679,11 @@ func TestSyncOperatorGroups(t *testing.T) {
36793679
v1alpha1.CSVPhaseNone,
36803680
), labels.Set{resolver.APILabelKeyPrefix + "9f4c46c37bdff8d0": "provided"})
36813681

3682+
operatorCSV.Spec.InstallStrategy.StrategySpec.DeploymentSpecs[0].Spec.Template.Spec.Containers[0].Env = []corev1.EnvVar{corev1.EnvVar{
3683+
Name: "OPERATOR_CONDITION_NAME",
3684+
Value: operatorCSV.GetName(),
3685+
}}
3686+
36823687
serverVersion := version.Get().String()
36833688
// after state transitions from operatorgroups, this is the operator csv we expect
36843689
operatorCSVFinal := operatorCSV.DeepCopy()
@@ -3778,6 +3783,7 @@ func TestSyncOperatorGroups(t *testing.T) {
37783783
ownerutil.AddNonBlockingOwner(serviceAccount, operatorCSV)
37793784

37803785
ownedDeployment := deployment(deploymentName, operatorNamespace, serviceAccount.GetName(), nil)
3786+
ownedDeployment.Spec.Template.Spec.Containers[0].Env = []corev1.EnvVar{{Name: "OPERATOR_CONDITION_NAME", Value: "csv1"}}
37813787
ownerutil.AddNonBlockingOwner(ownedDeployment, operatorCSV)
37823788
deploymentSpec := installStrategy(deploymentName, permissions, nil).StrategySpec.DeploymentSpecs[0].Spec
37833789
ownedDeployment.SetLabels(map[string]string{

pkg/controller/operators/olm/overrides/initializer.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"fmt"
55

66
"github.com/operator-framework/operator-lifecycle-manager/pkg/controller/install"
7+
"github.com/operator-framework/operator-lifecycle-manager/pkg/controller/operators/olm/overrides/inject"
78
"github.com/operator-framework/operator-lifecycle-manager/pkg/lib/operatorlister"
89
"github.com/operator-framework/operator-lifecycle-manager/pkg/lib/ownerutil"
910
"github.com/operator-framework/operator-lifecycle-manager/pkg/lib/proxy"
@@ -67,27 +68,27 @@ func (d *DeploymentInitializer) initialize(ownerCSV ownerutil.Owner, deployment
6768
}
6869

6970
podSpec := &deployment.Spec.Template.Spec
70-
if err := InjectEnvIntoDeployment(podSpec, merged); err != nil {
71+
if err := inject.InjectEnvIntoDeployment(podSpec, merged); err != nil {
7172
return fmt.Errorf("failed to inject proxy env variable(s) into deployment spec name=%s - %v", deployment.Name, err)
7273
}
7374

74-
if err = InjectVolumesIntoDeployment(podSpec, volumeOverrides); err != nil {
75+
if err = inject.InjectVolumesIntoDeployment(podSpec, volumeOverrides); err != nil {
7576
return fmt.Errorf("failed to inject volume(s) into deployment spec name=%s - %v", deployment.Name, err)
7677
}
7778

78-
if err = InjectVolumeMountsIntoDeployment(podSpec, volumeMountOverrides); err != nil {
79+
if err = inject.InjectVolumeMountsIntoDeployment(podSpec, volumeMountOverrides); err != nil {
7980
return fmt.Errorf("failed to inject volumeMounts(s) into deployment spec name=%s - %v", deployment.Name, err)
8081
}
8182

82-
if err = InjectTolerationsIntoDeployment(podSpec, tolerationOverrides); err != nil {
83+
if err = inject.InjectTolerationsIntoDeployment(podSpec, tolerationOverrides); err != nil {
8384
return fmt.Errorf("failed to inject toleration(s) into deployment spec name=%s - %v", deployment.Name, err)
8485
}
8586

86-
if err = InjectResourcesIntoDeployment(podSpec, resourcesOverride); err != nil {
87+
if err = inject.InjectResourcesIntoDeployment(podSpec, resourcesOverride); err != nil {
8788
return fmt.Errorf("failed to inject resources into deployment spec name=%s - %v", deployment.Name, err)
8889
}
8990

90-
if err = InjectNodeSelectorIntoDeployment(podSpec, nodeSelectorOverride); err != nil {
91+
if err = inject.InjectNodeSelectorIntoDeployment(podSpec, nodeSelectorOverride); err != nil {
9192
return fmt.Errorf("failed to inject nodeSelector into deployment spec name=%s - %v", deployment.Name, err)
9293
}
9394

pkg/controller/operators/olm/overrides/inject.go renamed to pkg/controller/operators/olm/overrides/inject/inject.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package overrides
1+
package inject
22

33
import (
44
"errors"

pkg/controller/operators/olm/overrides/inject_test.go renamed to pkg/controller/operators/olm/overrides/inject/inject_test.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
package overrides_test
1+
package inject_test
22

33
import (
44
"testing"
55

6-
"github.com/operator-framework/operator-lifecycle-manager/pkg/controller/operators/olm/overrides"
6+
"github.com/operator-framework/operator-lifecycle-manager/pkg/controller/operators/olm/overrides/inject"
77
"github.com/stretchr/testify/assert"
88
corev1 "k8s.io/api/core/v1"
99
"k8s.io/apimachinery/pkg/api/resource"
@@ -196,7 +196,7 @@ func TestInjectVolumeMountIntoDeployment(t *testing.T) {
196196

197197
for _, tt := range tests {
198198
t.Run(tt.name, func(t *testing.T) {
199-
overrides.InjectVolumeMountsIntoDeployment(tt.podSpec, tt.volumeMounts)
199+
inject.InjectVolumeMountsIntoDeployment(tt.podSpec, tt.volumeMounts)
200200

201201
podSpecWant := tt.expected
202202
podSpecGot := tt.podSpec
@@ -298,7 +298,7 @@ func TestInjectVolumeIntoDeployment(t *testing.T) {
298298

299299
for _, tt := range tests {
300300
t.Run(tt.name, func(t *testing.T) {
301-
overrides.InjectVolumesIntoDeployment(tt.podSpec, tt.volumes)
301+
inject.InjectVolumesIntoDeployment(tt.podSpec, tt.volumes)
302302

303303
podSpecWant := tt.expected
304304
podSpecGot := tt.podSpec
@@ -511,7 +511,7 @@ func TestInjectEnvIntoDeployment(t *testing.T) {
511511

512512
for _, tt := range tests {
513513
t.Run(tt.name, func(t *testing.T) {
514-
overrides.InjectEnvIntoDeployment(tt.podSpec, tt.envVar)
514+
inject.InjectEnvIntoDeployment(tt.podSpec, tt.envVar)
515515

516516
podSpecWant := tt.expected
517517
podSpecGot := tt.podSpec
@@ -617,7 +617,7 @@ func TestInjectTolerationsIntoDeployment(t *testing.T) {
617617

618618
for _, tt := range tests {
619619
t.Run(tt.name, func(t *testing.T) {
620-
overrides.InjectTolerationsIntoDeployment(tt.podSpec, tt.tolerations)
620+
inject.InjectTolerationsIntoDeployment(tt.podSpec, tt.tolerations)
621621

622622
podSpecWant := tt.expected
623623
podSpecGot := tt.podSpec
@@ -699,7 +699,7 @@ func TestInjectResourcesIntoDeployment(t *testing.T) {
699699

700700
for _, tt := range tests {
701701
t.Run(tt.name, func(t *testing.T) {
702-
overrides.InjectResourcesIntoDeployment(tt.podSpec, tt.resources)
702+
inject.InjectResourcesIntoDeployment(tt.podSpec, tt.resources)
703703

704704
podSpecWant := tt.expected
705705
podSpecGot := tt.podSpec
@@ -774,7 +774,7 @@ func TestInjectNodeSelectorIntoDeployment(t *testing.T) {
774774

775775
for _, tt := range tests {
776776
t.Run(tt.name, func(t *testing.T) {
777-
overrides.InjectNodeSelectorIntoDeployment(tt.podSpec, tt.nodeSelector)
777+
inject.InjectNodeSelectorIntoDeployment(tt.podSpec, tt.nodeSelector)
778778

779779
podSpecWant := tt.expected
780780
podSpecGot := tt.podSpec

test/e2e/installplan_e2e_test.go

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1650,7 +1650,7 @@ var _ = Describe("Install Plan", func() {
16501650
csv, err := awaitCSV(crc, testNamespace, mainCSV.GetName(), csvSucceededChecker)
16511651
require.NoError(GinkgoT(), err)
16521652

1653-
modifiedEnv := []corev1.EnvVar{{Name: "EXAMPLE", Value: "value"}}
1653+
addedEnvVar := corev1.EnvVar{Name: "EXAMPLE", Value: "value"}
16541654
modifiedDetails := operatorsv1alpha1.StrategyDetailsDeployment{
16551655
DeploymentSpecs: []operatorsv1alpha1.StrategyDeploymentSpec{
16561656
{
@@ -1670,7 +1670,7 @@ var _ = Describe("Install Plan", func() {
16701670
Image: *dummyImage,
16711671
Ports: []corev1.ContainerPort{{ContainerPort: 80}},
16721672
ImagePullPolicy: corev1.PullIfNotPresent,
1673-
Env: modifiedEnv,
1673+
Env: []corev1.EnvVar{addedEnvVar},
16741674
},
16751675
}},
16761676
},
@@ -1700,7 +1700,12 @@ var _ = Describe("Install Plan", func() {
17001700
if len(dep.Spec.Template.Spec.Containers[0].Env) == 0 {
17011701
return false, nil
17021702
}
1703-
return modifiedEnv[0] == dep.Spec.Template.Spec.Containers[0].Env[0], nil
1703+
for _, envVar := range dep.Spec.Template.Spec.Containers[0].Env {
1704+
if envVar == addedEnvVar {
1705+
return true, nil
1706+
}
1707+
}
1708+
return false, nil
17041709
})
17051710
require.NoError(GinkgoT(), err)
17061711

@@ -1740,8 +1745,9 @@ var _ = Describe("Install Plan", func() {
17401745
require.NotNil(GinkgoT(), updatedDep)
17411746

17421747
// Should have the updated env var
1743-
var emptyEnv []corev1.EnvVar = nil
1744-
require.Equal(GinkgoT(), emptyEnv, updatedDep.Spec.Template.Spec.Containers[0].Env)
1748+
for _, envVar := range updatedDep.Spec.Template.Spec.Containers[0].Env {
1749+
require.False(GinkgoT(), envVar == addedEnvVar)
1750+
}
17451751
})
17461752
It("UpdateSingleExistingCRDOwner", func() {
17471753

0 commit comments

Comments
 (0)