diff --git a/internal/operator-controller/rukpak/render/registryv1/generators/generators.go b/internal/operator-controller/rukpak/render/registryv1/generators/generators.go index cf17142fa..9f4c9d651 100644 --- a/internal/operator-controller/rukpak/render/registryv1/generators/generators.go +++ b/internal/operator-controller/rukpak/render/registryv1/generators/generators.go @@ -69,11 +69,20 @@ func BundleCSVDeploymentGenerator(rv1 *bundle.RegistryV1, opts render.Options) ( // See https://github.com/operator-framework/operator-lifecycle-manager/blob/dfd0b2bea85038d3c0d65348bc812d297f16b8d2/pkg/controller/install/deployment.go#L177-L180 depSpec.Spec.RevisionHistoryLimit = ptr.To(int32(1)) + deploymentOpts := []ResourceCreatorOption{ + WithDeploymentSpec(depSpec.Spec), + WithLabels(depSpec.Label), + } + if opts.Proxy != nil { + deploymentOpts = append( + deploymentOpts, + WithProxy(opts.Proxy.HTTPProxy, opts.Proxy.HTTPSProxy, opts.Proxy.NoProxy), + ) + } deploymentResource := CreateDeploymentResource( depSpec.Name, opts.InstallNamespace, - WithDeploymentSpec(depSpec.Spec), - WithLabels(depSpec.Label), + deploymentOpts..., ) secretInfo := render.CertProvisionerFor(depSpec.Name, opts).GetCertSecretInfo() diff --git a/internal/operator-controller/rukpak/render/registryv1/generators/resources.go b/internal/operator-controller/rukpak/render/registryv1/generators/resources.go index ed1cf6552..0b5f9c435 100644 --- a/internal/operator-controller/rukpak/render/registryv1/generators/resources.go +++ b/internal/operator-controller/rukpak/render/registryv1/generators/resources.go @@ -115,6 +115,40 @@ func WithMutatingWebhooks(webhooks ...admissionregistrationv1.MutatingWebhook) f } } +// With +func WithProxy(httpProxy, httpsProxy, noProxy string) func(client.Object) { + return func(obj client.Object) { + switch o := obj.(type) { + case *appsv1.Deployment: + addProxyEnvVars(httpProxy, httpsProxy, noProxy, o.Spec.Template.Spec.Containers) + } + } +} + +func addProxyEnvVars(httpProxy, httpsProxy, noProxy string, containers []corev1.Container) { + cs := containers + for i := range cs { + if len(httpProxy) > 0 { + cs[i].Env = append(cs[i].Env, corev1.EnvVar{ + Name: "HTTP_PROXY", + Value: httpProxy, + }) + } + if len(httpsProxy) > 0 { + cs[i].Env = append(cs[i].Env, corev1.EnvVar{ + Name: "HTTPS_PROXY", + Value: httpsProxy, + }) + } + if len(noProxy) > 0 { + cs[i].Env = append(cs[i].Env, corev1.EnvVar{ + Name: "NO_PROXY", + Value: noProxy, + }) + } + } +} + // CreateServiceAccountResource creates a ServiceAccount resource with name 'name', namespace 'namespace', and applying // any ServiceAccount related options in opts func CreateServiceAccountResource(name string, namespace string, opts ...ResourceCreatorOption) *corev1.ServiceAccount { diff --git a/internal/operator-controller/rukpak/render/registryv1/generators/resources_test.go b/internal/operator-controller/rukpak/render/registryv1/generators/resources_test.go index aa3227987..5c29a02a9 100644 --- a/internal/operator-controller/rukpak/render/registryv1/generators/resources_test.go +++ b/internal/operator-controller/rukpak/render/registryv1/generators/resources_test.go @@ -6,8 +6,10 @@ import ( "strings" "testing" + "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" admissionregistrationv1 "k8s.io/api/admissionregistration/v1" + appsv1 "k8s.io/api/apps/v1" corev1 "k8s.io/api/core/v1" rbacv1 "k8s.io/api/rbac/v1" "sigs.k8s.io/controller-runtime/pkg/client" @@ -276,3 +278,60 @@ func Test_WithMutatingWebhook(t *testing.T) { {Name: "wh-two"}, }, wh.Webhooks) } + +func Test_WithProxy(t *testing.T) { + depSpec := appsv1.DeploymentSpec{ + Template: corev1.PodTemplateSpec{ + Spec: corev1.PodSpec{ + Containers: []corev1.Container{ + { + Name: "c1", + Env: []corev1.EnvVar{ + { + Name: "TEST", + Value: "xxx", + }, + }, + }, + { + Name: "c2", + Env: []corev1.EnvVar{ + { + Name: "TEST", + Value: "xxx", + }, + }, + }, + }, + }, + }, + } + + depl := generators.CreateDeploymentResource( + "test", + "test-ns", + generators.WithDeploymentSpec(depSpec), + generators.WithProxy("http,prox", "https,prox", "no,prox"), + ) + + expected := []corev1.EnvVar{ + { + Name: "TEST", + Value: "xxx", + }, + { + Name: "HTTP_PROXY", + Value: "http,prox", + }, + { + Name: "HTTPS_PROXY", + Value: "https,prox", + }, + { + Name: "NO_PROXY", + Value: "no,prox", + }, + } + assert.Equal(t, expected, depl.Spec.Template.Spec.Containers[0].Env) + assert.Equal(t, expected, depl.Spec.Template.Spec.Containers[1].Env) +} diff --git a/internal/operator-controller/rukpak/render/render.go b/internal/operator-controller/rukpak/render/render.go index 70063f1d4..3b10406a8 100644 --- a/internal/operator-controller/rukpak/render/render.go +++ b/internal/operator-controller/rukpak/render/render.go @@ -56,11 +56,16 @@ func (r ResourceGenerators) ResourceGenerator() ResourceGenerator { type UniqueNameGenerator func(string, interface{}) (string, error) +type Proxy struct { + HTTPProxy, HTTPSProxy, NoProxy string +} + type Options struct { InstallNamespace string TargetNamespaces []string UniqueNameGenerator UniqueNameGenerator CertificateProvider CertificateProvider + Proxy *Proxy } func (o *Options) apply(opts ...Option) *Options { @@ -106,6 +111,12 @@ func WithCertificateProvider(provider CertificateProvider) Option { } } +func WithProxy(proxy Proxy) Option { + return func(o *Options) { + o.Proxy = &proxy + } +} + type BundleRenderer struct { BundleValidator BundleValidator ResourceGenerators []ResourceGenerator