Bug 2002502 - []corev1.EnvVar{} can't be appended to container.env
Summary: []corev1.EnvVar{} can't be appended to container.env
Keywords:
Status: CLOSED ERRATA
Alias: None
Product: OpenShift Container Platform
Classification: Red Hat
Component: Operator SDK
Version: 4.9
Hardware: Unspecified
OS: Unspecified
urgent
urgent
Target Milestone: ---
: 4.9.0
Assignee: amacdona@redhat.com
QA Contact: Fan Jia
URL:
Whiteboard:
Depends On:
Blocks:
TreeView+ depends on / blocked
 
Reported: 2021-09-09 02:58 UTC by Fan Jia
Modified: 2021-10-18 17:51 UTC (History)
4 users (show)

Fixed In Version:
Doc Type: If docs needed, set a value
Doc Text:
Clone Of:
Environment:
Last Closed: 2021-10-18 17:51:28 UTC
Target Upstream Version:
Embargoed:


Attachments (Terms of Use)


Links
System ID Private Priority Status Summary Last Updated
Red Hat Product Errata RHSA-2021:3759 0 None None None 2021-10-18 17:51:45 UTC

Description Fan Jia 2021-09-09 02:58:22 UTC
Description of problem:
Pass down proxy env to operands failed for go type operator as the description : https://github.com/operator-framework/enhancements/blob/master/enhancements/proxy-passdown.md. Import the proxy package to the controller.go. But the proxy is not released ,can't be used for controller.go

Version-Release number of selected component (if applicable):
operator-sdk version: "v1.10.1-ocp", commit: "972fd59bb3a4cdb2e5102fc37fc3afa32f6c066f", kubernetes version: "v1.21", go version: "go1.16.5", GOOS: "linux", GOARCH: "amd64"

How reproducible:
always


Steps to Reproduce:
1.generate a go operator as https://docs.openshift.com/container-platform/4.8/operators/operator_sdk/golang/osdk-golang-tutorial.html
import the proxy package to the controller.go.

$cat controllers/memcached_controller.go

package controllers

import (

proxy "github.com/operator-framework/operator-lib/proxy"

)

dep := r.deploymentForMemcached(memcached)
proxyVars := proxy.ReadProxyVarsFromEnv()
for _, container := range dep.Spec.Template.Spec.Containers
{ container.Env = append(container.Env, proxyVars) } 

2.$make docker-build docker-push IMG=quay.io/xxxxx/memcached-operator:v1.9


Actual results:
$ make docker-build docker-push IMG=quay.io/xxxxx/memcached-operator:v1.9
github.com/example-inc/memcached-operator/controllers
controllers/memcached_controller.go:84:25: cannot use proxyVars (type []"k8s.io/api/core/v1".EnvVar) as type "k8s.io/api/core/v1".EnvVar in append
# github.com/example-inc/memcached-operator/controllers
vet: controllers/memcached_controller.go:84:41: cannot use proxyVars (variable of type []"k8s.io/api/core/v1".EnvVar) as "k8s.io/api/core/v1".EnvVar value in argument to append
make: *** [Makefile:88: vet] Error 2


Expected results:
proxyVars can be appended to all the containers.


Additional info:

Comment 1 amacdona@redhat.com 2021-09-09 17:15:45 UTC
To verify the code, please add the ellipsis. 

`{ container.Env = append(container.Env, proxyVars...) }`

Comment 4 amacdona@redhat.com 2021-09-13 16:52:44 UTC
You can use the docs PR to verify this https://github.com/operator-framework/operator-sdk/pull/5204

Comment 5 amacdona@redhat.com 2021-09-13 17:00:09 UTC
Ive also pushed my example if you want to have a look https://github.com/asmacdo/go-proxy-demo

Comment 6 Fan Jia 2021-09-14 03:53:52 UTC
verified.

test env:
1. operator-sdk version: "v1.10.1-ocp", commit: "972fd59bb3a4cdb2e5102fc37fc3afa32f6c066f", kubernetes version: "v1.21", go version: "go1.16.5", GOOS: "linux", GOARCH: "amd64"

2. cv:4.9.0-0.nightly-2021-09-10-170926

3. add the http_proxy message to the go operator
1)controllers/memcached_controller.go
`

import (
......
	proxy "github.com/operator-framework/operator-lib/proxy"
......
)


func (r *MemcachedReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
......
	if err != nil && errors.IsNotFound(err) {
		dep := r.deploymentForMemcached(memcached)
		for i, container := range dep.Spec.Template.Spec.Containers {
			dep.Spec.Template.Spec.Containers[i].Env = append(container.Env, proxy.ReadProxyVarsFromEnv()...)
		}
`

2)Makefile
deploy: manifests kustomize ## Deploy controller to the K8s cluster specified in ~/.kube/config.
	cd config/manager && $(KUSTOMIZE) edit set image controller=${IMG}
	$(KUSTOMIZE) build config/default | HTTP_PROXY=$(shell kubectl get proxies.config.openshift.io cluster  -o json | jq '.spec.httpProxy') envsubst | kubectl apply -f -

3)config/manager/manager.yaml
config/manager/manager.yaml
containers:
 - args:
   - --leader-elect
   - --leader-election-id=helm-proxy-demo
   image: controller:latest
   name: manager
   env:
     - name: "HTTP_PROXY"
       value: $HTTP_PROXY


test result:
CR created success and env is passed to operand
1.$oc create -f config/samples/cache_v1_memcached.yaml 
memcached.cache.example.com/memcached-sample created

2.$oc get deployment memcached-sample -o=jsonpath={.spec.template.spec.containers[0].env}
[map[name:HTTP_PROXY value:http://proxy-user1:JYgU8qRZV4DY4Pxxxxx@ec2-18-188-xxxxxx.us-xxxx-2.compute.xxxxxxxx.com:3128] map[name:http_proxy value:http://proxy-user1:JYgU8qRZV4DY4xxxxxxx@ec2-18-188-xxxxx.us-xxxx-2.compute.xxxxxxxx.com:3128]]

Comment 9 errata-xmlrpc 2021-10-18 17:51:28 UTC
Since the problem described in this bug report should be
resolved in a recent advisory, it has been closed with a
resolution of ERRATA.

For information on the advisory (Moderate: OpenShift Container Platform 4.9.0 bug fix and security update), and where to find the updated
files, follow the link below.

If the solution does not work for you, open a new bug report.

https://access.redhat.com/errata/RHSA-2021:3759


Note You need to log in before you can comment on or make changes to this bug.