Skip to content

Commit 4a943b2

Browse files
Merge pull request #958 from tkashem/follow-up
Bug 1732613: Follow up for pod configuration
2 parents b56e63a + 91fffbf commit 4a943b2

File tree

8 files changed

+95
-64
lines changed

8 files changed

+95
-64
lines changed

pkg/controller/install/deployment.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,9 @@ func (c DeploymentInitializerFuncChain) Apply(deployment *appsv1.Deployment) (er
8080
return
8181
}
8282

83-
type DeploymentInitializerFuncBuilder func(owner ownerutil.Owner) DeploymentInitializerFunc
83+
// DeploymentInitializerBuilderFunc returns a DeploymentInitializerFunc based on
84+
// the given context.
85+
type DeploymentInitializerBuilderFunc func(owner ownerutil.Owner) DeploymentInitializerFunc
8486

8587
func NewStrategyDeploymentInstaller(strategyClient wrappers.InstallStrategyDeploymentInterface, templateAnnotations map[string]string, owner ownerutil.Owner, previousStrategy Strategy, initializers DeploymentInitializerFuncChain) StrategyInstaller {
8688
return &StrategyDeploymentInstaller{

pkg/controller/install/resolver.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ type StrategyResolverInterface interface {
2929
}
3030

3131
type StrategyResolver struct {
32-
ProxyInjectorBuilder DeploymentInitializerFuncBuilder
32+
ProxyInjectorBuilderFunc DeploymentInitializerBuilderFunc
3333
}
3434

3535
func (r *StrategyResolver) UnmarshalStrategy(s v1alpha1.NamedInstallStrategy) (strategy Strategy, err error) {
@@ -51,8 +51,8 @@ func (r *StrategyResolver) InstallerForStrategy(strategyName string, opClient op
5151
strategyClient := wrappers.NewInstallStrategyDeploymentClient(opClient, opLister, owner.GetNamespace())
5252

5353
initializers := []DeploymentInitializerFunc{}
54-
if r.ProxyInjectorBuilder != nil {
55-
initializers = append(initializers, r.ProxyInjectorBuilder(owner))
54+
if r.ProxyInjectorBuilderFunc != nil {
55+
initializers = append(initializers, r.ProxyInjectorBuilderFunc(owner))
5656
}
5757

5858
return NewStrategyDeploymentInstaller(strategyClient, annotations, owner, previousStrategy, initializers)

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

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,12 +56,14 @@ func (d *DeploymentInitializer) initialize(ownerCSV ownerutil.Owner, deployment
5656
err = fmt.Errorf("failed to query cluster proxy configuration - %v", err)
5757
return err
5858
}
59+
60+
proxyEnvVar = dropEmptyProxyEnv(proxyEnvVar)
5961
}
6062

6163
merged = append(podConfigEnvVar, proxyEnvVar...)
6264

6365
if len(merged) == 0 {
64-
d.logger.Debugf("no env var to inject into csv=%s", ownerCSV.GetName())
66+
d.logger.WithField("csv", ownerCSV.GetName()).Debug("no env var to inject into csv")
6567
}
6668

6769
podSpec := deployment.Spec.Template.Spec
@@ -71,3 +73,16 @@ func (d *DeploymentInitializer) initialize(ownerCSV ownerutil.Owner, deployment
7173

7274
return nil
7375
}
76+
77+
func dropEmptyProxyEnv(in []corev1.EnvVar) (out []corev1.EnvVar) {
78+
out = make([]corev1.EnvVar, 0)
79+
for i := range in {
80+
if in[i].Value == "" {
81+
continue
82+
}
83+
84+
out = append(out, in[i])
85+
}
86+
87+
return
88+
}

pkg/controller/operators/olm/envvar/inject.go

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,18 +29,15 @@ func merge(containerEnvVars []corev1.EnvVar, newEnvVars []corev1.EnvVar) (merged
2929

3030
for _, newEnvVar := range newEnvVars {
3131
existing, found := find(containerEnvVars, newEnvVar.Name)
32-
if !found {
33-
if newEnvVar.Value != "" {
34-
merged = append(merged, corev1.EnvVar{
35-
Name: newEnvVar.Name,
36-
Value: newEnvVar.Value,
37-
})
38-
}
39-
32+
if found {
33+
existing.Value = newEnvVar.Value
4034
continue
4135
}
4236

43-
existing.Value = newEnvVar.Value
37+
merged = append(merged, corev1.EnvVar{
38+
Name: newEnvVar.Name,
39+
Value: newEnvVar.Value,
40+
})
4441
}
4542

4643
return

pkg/controller/operators/olm/operator.go

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -407,7 +407,6 @@ func newOperatorWithConfig(ctx context.Context, config *operatorConfig) (*Operat
407407
return nil, err
408408
}
409409

410-
411410
// setup proxy env var injection policies
412411
discovery := config.operatorClient.KubernetesInterface().Discovery()
413412
proxyAPIExists, err := proxy.IsAPIAvailable(discovery)
@@ -416,7 +415,7 @@ func newOperatorWithConfig(ctx context.Context, config *operatorConfig) (*Operat
416415
return nil, err
417416
}
418417

419-
proxyQuerierInUse := proxy.DefaultQuerier()
418+
proxyQuerierInUse := proxy.NoopQuerier()
420419
if proxyAPIExists {
421420
op.logger.Info("OpenShift Proxy API available - setting up watch for Proxy type")
422421

@@ -443,7 +442,7 @@ func newOperatorWithConfig(ctx context.Context, config *operatorConfig) (*Operat
443442

444443
proxyEnvInjector := envvar.NewDeploymentInitializer(op.logger, proxyQuerierInUse, op.lister)
445444
op.resolver = &install.StrategyResolver{
446-
ProxyInjectorBuilder: proxyEnvInjector.GetDeploymentInitializer,
445+
ProxyInjectorBuilderFunc: proxyEnvInjector.GetDeploymentInitializer,
447446
}
448447

449448
return op, nil
@@ -454,12 +453,21 @@ func (a *Operator) now() metav1.Time {
454453
}
455454

456455
func (a *Operator) syncSubscription(obj interface{}) error {
457-
_, ok := obj.(*v1alpha1.Subscription)
456+
sub, ok := obj.(*v1alpha1.Subscription)
458457
if !ok {
459458
a.logger.Debugf("wrong type: %#v\n", obj)
460459
return fmt.Errorf("casting Subscription failed")
461460
}
462461

462+
installedCSV := sub.Status.InstalledCSV
463+
if installedCSV != "" {
464+
a.logger.WithField("csv", installedCSV).Debug("subscription has changed, requeuing installed csv")
465+
if err := a.csvQueueSet.Requeue(sub.GetNamespace(), installedCSV); err != nil {
466+
a.logger.WithField("csv", installedCSV).Debug("failed to requeue installed csv")
467+
return err
468+
}
469+
}
470+
463471
return nil
464472
}
465473

pkg/lib/proxy/querier.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@ import (
44
corev1 "k8s.io/api/core/v1"
55
)
66

7-
// DefaultQuerier does...
8-
func DefaultQuerier() Querier {
9-
return &defaultQuerier{}
7+
// NoopQuerier returns an instance of noopQuerier. It's used for upstream where
8+
// we don't have any cluster proxy configuration.
9+
func NoopQuerier() Querier {
10+
return &noopQuerier{}
1011
}
1112

1213
// Querier is an interface that wraps the QueryProxyConfig method.
@@ -16,11 +17,11 @@ type Querier interface {
1617
QueryProxyConfig() (proxy []corev1.EnvVar, err error)
1718
}
1819

19-
type defaultQuerier struct {
20+
type noopQuerier struct {
2021
}
2122

2223
// QueryProxyConfig returns no env variable(s), err is set to nil to indicate
2324
// that the cluster has no global proxy configuration.
24-
func (*defaultQuerier) QueryProxyConfig() (proxy []corev1.EnvVar, err error) {
25+
func (*noopQuerier) QueryProxyConfig() (proxy []corev1.EnvVar, err error) {
2526
return
2627
}

test/e2e/subscription_e2e_test.go

Lines changed: 37 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -407,7 +407,7 @@ func createSubscription(t *testing.T, crc versioned.Interface, namespace, name,
407407
return buildSubscriptionCleanupFunc(t, crc, subscription)
408408
}
409409

410-
func createSubscriptionWithPodConfig(t *testing.T, crc versioned.Interface, namespace, name, packageName, channel string, approval v1alpha1.Approval, config v1alpha1.SubscriptionConfig) cleanupFunc {
410+
func createSubscriptionForCatalog(t *testing.T, crc versioned.Interface, namespace, name, catalog, packageName, channel, startingCSV string, approval v1alpha1.Approval) cleanupFunc {
411411
subscription := &v1alpha1.Subscription{
412412
TypeMeta: metav1.TypeMeta{
413413
Kind: v1alpha1.SubscriptionKind,
@@ -418,12 +418,12 @@ func createSubscriptionWithPodConfig(t *testing.T, crc versioned.Interface, name
418418
Name: name,
419419
},
420420
Spec: &v1alpha1.SubscriptionSpec{
421-
CatalogSource: catalogSourceName,
421+
CatalogSource: catalog,
422422
CatalogSourceNamespace: namespace,
423423
Package: packageName,
424424
Channel: channel,
425+
StartingCSV: startingCSV,
425426
InstallPlanApproval: approval,
426-
Config: config,
427427
},
428428
}
429429

@@ -432,7 +432,7 @@ func createSubscriptionWithPodConfig(t *testing.T, crc versioned.Interface, name
432432
return buildSubscriptionCleanupFunc(t, crc, subscription)
433433
}
434434

435-
func createSubscriptionForCatalog(t *testing.T, crc versioned.Interface, namespace, name, catalog, packageName, channel, startingCSV string, approval v1alpha1.Approval) cleanupFunc {
435+
func createSubscriptionForCatalogWithSpec(t *testing.T, crc versioned.Interface, namespace, name string, spec *v1alpha1.SubscriptionSpec) cleanupFunc {
436436
subscription := &v1alpha1.Subscription{
437437
TypeMeta: metav1.TypeMeta{
438438
Kind: v1alpha1.SubscriptionKind,
@@ -442,14 +442,7 @@ func createSubscriptionForCatalog(t *testing.T, crc versioned.Interface, namespa
442442
Namespace: namespace,
443443
Name: name,
444444
},
445-
Spec: &v1alpha1.SubscriptionSpec{
446-
CatalogSource: catalog,
447-
CatalogSourceNamespace: namespace,
448-
Package: packageName,
449-
Channel: channel,
450-
StartingCSV: startingCSV,
451-
InstallPlanApproval: approval,
452-
},
445+
Spec: spec,
453446
}
454447

455448
subscription, err := crc.OperatorsV1alpha1().Subscriptions(namespace).Create(subscription)
@@ -1120,11 +1113,9 @@ func TestCreateNewSubscriptionWithPodConfig(t *testing.T) {
11201113

11211114
newConfigClient := func(t *testing.T) configv1client.ConfigV1Interface {
11221115
config, err := clientcmd.BuildConfigFromFlags("", *kubeConfigPath)
1123-
// require.NoErrorf(t, err, "could not create rest config: %s", err.Error())
11241116
require.NoError(t, err,)
11251117

11261118
client, err := configv1client.NewForConfig(config)
1127-
// require.NoErrorf(t, err, "error creating OpenShift Config client: %s", err.Error())
11281119
require.NoError(t, err)
11291120

11301121
return client
@@ -1141,33 +1132,36 @@ func TestCreateNewSubscriptionWithPodConfig(t *testing.T) {
11411132
}
11421133

11431134
require.NotNil(t, proxy)
1144-
proxyEnv := []corev1.EnvVar{
1145-
corev1.EnvVar{
1135+
proxyEnv := []corev1.EnvVar{}
1136+
1137+
if proxy.Status.HTTPProxy !="" {
1138+
proxyEnv = append(proxyEnv, corev1.EnvVar{
11461139
Name: "HTTP_PROXY",
11471140
Value: proxy.Status.HTTPProxy,
1148-
},
1149-
corev1.EnvVar{
1141+
})
1142+
}
1143+
1144+
if proxy.Status.HTTPSProxy !="" {
1145+
proxyEnv = append(proxyEnv, corev1.EnvVar{
11501146
Name: "HTTPS_PROXY",
11511147
Value: proxy.Status.HTTPSProxy,
1152-
},
1153-
corev1.EnvVar{
1148+
})
1149+
}
1150+
1151+
if proxy.Status.NoProxy !="" {
1152+
proxyEnv = append(proxyEnv, corev1.EnvVar{
11541153
Name: "NO_PROXY",
11551154
Value: proxy.Status.NoProxy,
1156-
},
1155+
})
11571156
}
11581157

11591158
return proxyEnv
11601159
}
11611160

1162-
c := newKubeClient(t)
1163-
crc := newCRClient(t)
1161+
kubeClient := newKubeClient(t)
1162+
crClient := newCRClient(t)
11641163
config := newConfigClient(t)
11651164

1166-
defer func() {
1167-
require.NoError(t, crc.OperatorsV1alpha1().Subscriptions(testNamespace).DeleteCollection(&metav1.DeleteOptions{}, metav1.ListOptions{}))
1168-
}()
1169-
require.NoError(t, initCatalog(t, c, crc))
1170-
11711165
podEnv := []corev1.EnvVar{
11721166
corev1.EnvVar{
11731167
Name: "MY_ENV_VARIABLE1",
@@ -1182,22 +1176,31 @@ func TestCreateNewSubscriptionWithPodConfig(t *testing.T) {
11821176
Env: podEnv,
11831177
}
11841178

1185-
subscriptionName := "mysub-podconfig"
1186-
cleanup := createSubscriptionWithPodConfig(t, crc, testNamespace, subscriptionName, testPackageName, betaChannel, v1alpha1.ApprovalAutomatic, podConfig)
1187-
defer cleanup()
1179+
permissions := deploymentPermissions(t)
1180+
catsrc, subSpec, catsrcCleanup := newCatalogSource(t, kubeClient, crClient, "podconfig", testNamespace, permissions)
1181+
defer catsrcCleanup()
1182+
1183+
// Ensure that the catalog source is resolved before we create a subscription.
1184+
_, err := fetchCatalogSource(t, crClient, catsrc.GetName(), testNamespace, catalogSourceRegistryPodSynced)
1185+
require.NoError(t, err)
1186+
1187+
subscriptionName := genName("podconfig-sub-")
1188+
subSpec.Config = podConfig
1189+
cleanupSubscription := createSubscriptionForCatalogWithSpec(t, crClient, testNamespace, subscriptionName, subSpec)
1190+
defer cleanupSubscription()
11881191

1189-
subscription, err := fetchSubscription(t, crc, testNamespace, subscriptionName, subscriptionStateAtLatestChecker)
1192+
subscription, err := fetchSubscription(t, crClient, testNamespace, subscriptionName, subscriptionStateAtLatestChecker)
11901193
require.NoError(t, err)
11911194
require.NotNil(t, subscription)
11921195

1193-
csv, err := fetchCSV(t, crc, subscription.Status.CurrentCSV, testNamespace, buildCSVConditionChecker(v1alpha1.CSVPhaseSucceeded))
1196+
csv, err := fetchCSV(t, crClient, subscription.Status.CurrentCSV, testNamespace, buildCSVConditionChecker(v1alpha1.CSVPhaseSucceeded))
11941197
require.NoError(t, err)
11951198

11961199
proxyEnv := proxyEnvVarFunc(t, config)
11971200
expected := podEnv
11981201
expected = append(expected, proxyEnv...)
11991202

1200-
checkDeploymentWithPodConfiguration(t, c, csv, podConfig.Env)
1203+
checkDeploymentWithPodConfiguration(t, kubeClient, csv, podConfig.Env)
12011204
}
12021205

12031206
func checkDeploymentWithPodConfiguration(t *testing.T, client operatorclient.ClientInterface, csv *v1alpha1.ClusterServiceVersion, envVar []corev1.EnvVar) {

test/e2e/user_defined_sa_test.go

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@ func TestUserDefinedServiceAccountWithNoPermission(t *testing.T) {
4040
_, cleanupOG := newOperatorGroupWithServiceAccount(t, crclient, namespace, ogName, saName)
4141
defer cleanupOG()
4242

43-
catsrc, subSpec, catsrcCleanup := newCatalogSource(t, kubeclient, crclient, namespace)
43+
permissions := deploymentPermissions(t)
44+
catsrc, subSpec, catsrcCleanup := newCatalogSource(t, kubeclient, crclient, "scoped", namespace, permissions)
4445
defer catsrcCleanup()
4546

4647
// Ensure that the catalog source is resolved before we create a subscription.
@@ -96,7 +97,8 @@ func TestUserDefinedServiceAccountWithPermission(t *testing.T) {
9697
_, cleanupOG := newOperatorGroupWithServiceAccount(t, crclient, namespace, ogName, saName)
9798
defer cleanupOG()
9899

99-
catsrc, subSpec, catsrcCleanup := newCatalogSource(t, kubeclient, crclient, namespace)
100+
permissions := deploymentPermissions(t)
101+
catsrc, subSpec, catsrcCleanup := newCatalogSource(t, kubeclient, crclient, "scoped", namespace, permissions)
100102
defer catsrcCleanup()
101103

102104
// Ensure that the catalog source is resolved before we create a subscription.
@@ -194,7 +196,7 @@ func newOperatorGroupWithServiceAccount(t *testing.T, client versioned.Interface
194196
return
195197
}
196198

197-
func newCatalogSource(t *testing.T, kubeclient operatorclient.ClientInterface, crclient versioned.Interface, namespace string) (catsrc *v1alpha1.CatalogSource, subscriptionSpec *v1alpha1.SubscriptionSpec, cleanup cleanupFunc) {
199+
func newCatalogSource(t *testing.T, kubeclient operatorclient.ClientInterface, crclient versioned.Interface, prefix, namespace string, permissions []install.StrategyDeploymentPermissions) (catsrc *v1alpha1.CatalogSource, subscriptionSpec *v1alpha1.SubscriptionSpec, cleanup cleanupFunc) {
198200
crdPlural := genName("ins")
199201
crdName := crdPlural + ".cluster.com"
200202

@@ -215,12 +217,15 @@ func newCatalogSource(t *testing.T, kubeclient operatorclient.ClientInterface, c
215217
},
216218
}
217219

220+
prefixFunc := func(s string) string {
221+
return fmt.Sprintf("%s-%s-", prefix, s)
222+
}
223+
218224
// Create CSV
219-
packageName := genName("nginx-")
225+
packageName := genName(prefixFunc("package"))
220226
stableChannel := "stable"
221227

222-
permissions := deploymentPermissions(t)
223-
namedStrategy := newNginxInstallStrategy(genName("dep-"), permissions, nil)
228+
namedStrategy := newNginxInstallStrategy(genName(prefixFunc("dep")), permissions, nil)
224229
csvA := newCSV("nginx-a", namespace, "", semver.MustParse("0.1.0"), []apiextensions.CustomResourceDefinition{crd}, nil, namedStrategy)
225230
csvB := newCSV("nginx-b", namespace, "nginx-a", semver.MustParse("0.2.0"), []apiextensions.CustomResourceDefinition{crd}, nil, namedStrategy)
226231

@@ -235,7 +240,7 @@ func newCatalogSource(t *testing.T, kubeclient operatorclient.ClientInterface, c
235240
},
236241
}
237242

238-
catalogSourceName := genName("mock-nginx-")
243+
catalogSourceName := genName(prefixFunc("catsrc"))
239244
catsrc, cleanup = createInternalCatalogSource(t, kubeclient, crclient, catalogSourceName, namespace, manifests, []apiextensions.CustomResourceDefinition{crd}, []v1alpha1.ClusterServiceVersion{csvA, csvB})
240245
require.NotNil(t, catsrc)
241246
require.NotNil(t, cleanup)

0 commit comments

Comments
 (0)