Skip to content

Commit 7183b67

Browse files
committed
Add Proxy support to rukpak generator
1 parent 71108b2 commit 7183b67

File tree

4 files changed

+115
-2
lines changed

4 files changed

+115
-2
lines changed

internal/operator-controller/rukpak/render/registryv1/generators/generators.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,11 +69,20 @@ func BundleCSVDeploymentGenerator(rv1 *bundle.RegistryV1, opts render.Options) (
6969
// See https://github.com/operator-framework/operator-lifecycle-manager/blob/dfd0b2bea85038d3c0d65348bc812d297f16b8d2/pkg/controller/install/deployment.go#L177-L180
7070
depSpec.Spec.RevisionHistoryLimit = ptr.To(int32(1))
7171

72+
deploymentOpts := []ResourceCreatorOption{
73+
WithDeploymentSpec(depSpec.Spec),
74+
WithLabels(depSpec.Label),
75+
}
76+
if opts.Proxy != nil {
77+
deploymentOpts = append(
78+
deploymentOpts,
79+
WithProxy(opts.Proxy.HttpProxy, opts.Proxy.HttpsProxy, opts.Proxy.NoProxy),
80+
)
81+
}
7282
deploymentResource := CreateDeploymentResource(
7383
depSpec.Name,
7484
opts.InstallNamespace,
75-
WithDeploymentSpec(depSpec.Spec),
76-
WithLabels(depSpec.Label),
85+
deploymentOpts...,
7786
)
7887

7988
secretInfo := render.CertProvisionerFor(depSpec.Name, opts).GetCertSecretInfo()

internal/operator-controller/rukpak/render/registryv1/generators/resources.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,40 @@ func WithMutatingWebhooks(webhooks ...admissionregistrationv1.MutatingWebhook) f
115115
}
116116
}
117117

118+
// With
119+
func WithProxy(httpProxy, httpsProxy, noProxy string) func(client.Object) {
120+
return func(obj client.Object) {
121+
switch o := obj.(type) {
122+
case *appsv1.Deployment:
123+
addProxyEnvVars(httpProxy, httpsProxy, noProxy, o.Spec.Template.Spec.Containers)
124+
}
125+
}
126+
}
127+
128+
func addProxyEnvVars(httpProxy, httpsProxy, noProxy string, containers []corev1.Container) {
129+
cs := containers
130+
for i := range cs {
131+
if len(httpProxy) > 0 {
132+
cs[i].Env = append(cs[i].Env, corev1.EnvVar{
133+
Name: "HTTP_PROXY",
134+
Value: httpProxy,
135+
})
136+
}
137+
if len(httpsProxy) > 0 {
138+
cs[i].Env = append(cs[i].Env, corev1.EnvVar{
139+
Name: "HTTPS_PROXY",
140+
Value: httpsProxy,
141+
})
142+
}
143+
if len(noProxy) > 0 {
144+
cs[i].Env = append(cs[i].Env, corev1.EnvVar{
145+
Name: "NO_PROXY",
146+
Value: noProxy,
147+
})
148+
}
149+
}
150+
}
151+
118152
// CreateServiceAccountResource creates a ServiceAccount resource with name 'name', namespace 'namespace', and applying
119153
// any ServiceAccount related options in opts
120154
func CreateServiceAccountResource(name string, namespace string, opts ...ResourceCreatorOption) *corev1.ServiceAccount {

internal/operator-controller/rukpak/render/registryv1/generators/resources_test.go

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@ import (
66
"strings"
77
"testing"
88

9+
"github.com/stretchr/testify/assert"
910
"github.com/stretchr/testify/require"
1011
admissionregistrationv1 "k8s.io/api/admissionregistration/v1"
12+
appsv1 "k8s.io/api/apps/v1"
1113
corev1 "k8s.io/api/core/v1"
1214
rbacv1 "k8s.io/api/rbac/v1"
1315
"sigs.k8s.io/controller-runtime/pkg/client"
@@ -276,3 +278,60 @@ func Test_WithMutatingWebhook(t *testing.T) {
276278
{Name: "wh-two"},
277279
}, wh.Webhooks)
278280
}
281+
282+
func Test_WithProxy(t *testing.T) {
283+
depSpec := appsv1.DeploymentSpec{
284+
Template: corev1.PodTemplateSpec{
285+
Spec: corev1.PodSpec{
286+
Containers: []corev1.Container{
287+
{
288+
Name: "c1",
289+
Env: []corev1.EnvVar{
290+
{
291+
Name: "TEST",
292+
Value: "xxx",
293+
},
294+
},
295+
},
296+
{
297+
Name: "c2",
298+
Env: []corev1.EnvVar{
299+
{
300+
Name: "TEST",
301+
Value: "xxx",
302+
},
303+
},
304+
},
305+
},
306+
},
307+
},
308+
}
309+
310+
depl := generators.CreateDeploymentResource(
311+
"test",
312+
"test-ns",
313+
generators.WithDeploymentSpec(depSpec),
314+
generators.WithProxy("http,prox", "https,prox", "no,prox"),
315+
)
316+
317+
expected := []corev1.EnvVar{
318+
{
319+
Name: "TEST",
320+
Value: "xxx",
321+
},
322+
{
323+
Name: "HTTP_PROXY",
324+
Value: "http,prox",
325+
},
326+
{
327+
Name: "HTTPS_PROXY",
328+
Value: "https,prox",
329+
},
330+
{
331+
Name: "NO_PROXY",
332+
Value: "no,prox",
333+
},
334+
}
335+
assert.Equal(t, expected, depl.Spec.Template.Spec.Containers[0].Env)
336+
assert.Equal(t, expected, depl.Spec.Template.Spec.Containers[1].Env)
337+
}

internal/operator-controller/rukpak/render/render.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,16 @@ func (r ResourceGenerators) ResourceGenerator() ResourceGenerator {
5656

5757
type UniqueNameGenerator func(string, interface{}) (string, error)
5858

59+
type Proxy struct {
60+
HttpProxy, HttpsProxy, NoProxy string
61+
}
62+
5963
type Options struct {
6064
InstallNamespace string
6165
TargetNamespaces []string
6266
UniqueNameGenerator UniqueNameGenerator
6367
CertificateProvider CertificateProvider
68+
Proxy *Proxy
6469
}
6570

6671
func (o *Options) apply(opts ...Option) *Options {
@@ -106,6 +111,12 @@ func WithCertificateProvider(provider CertificateProvider) Option {
106111
}
107112
}
108113

114+
func WithProxy(proxy Proxy) Option {
115+
return func(o *Options) {
116+
o.Proxy = &proxy
117+
}
118+
}
119+
109120
type BundleRenderer struct {
110121
BundleValidator BundleValidator
111122
ResourceGenerators []ResourceGenerator

0 commit comments

Comments
 (0)