Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expand env variables for cmds/entrypoint with format $(ENV) #19630

Merged
merged 1 commit into from
Aug 23, 2023

Conversation

rhatdan
Copy link
Member

@rhatdan rhatdan commented Aug 15, 2023

Kubernetes supports expanding $(FOOBAR) as environment variables within
the kube.YAML. When using podman kube play, we need to do the same, for
supporting these YAML files.

Fixes: #15983

Does this PR introduce a user-facing change?

Commands and entrypoints can use environment variables.

@openshift-ci openshift-ci bot added release-note approved Indicates a PR has been approved by an approver from all required OWNERS files. labels Aug 15, 2023
@rhatdan
Copy link
Member Author

rhatdan commented Aug 15, 2023

Replaces: #18881
@ygalblum PTAL

Copy link
Contributor

@ygalblum ygalblum left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@umohnani8
Copy link
Member

changes LGTM

inspect.WaitWithDefaultTimeout()
Expect(inspect).Should(Exit(0))
inspectData := inspect.InspectContainerToJSON()
Expect(inspectData).ToNot(BeEmpty())
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggestions:

  • check container logs, confirm that output == "BAR BAR \$\(FOO\) FOOBARBAZ"
  • add more args, like $(BAZ) ${BAZ}, without a corresponding env yaml, but with Putenv() in the test.

@TomSweeneyRedHat
Copy link
Member

nothing over @edsantiago 's comments. Tests are very red.

@rhatdan rhatdan force-pushed the kube branch 2 times, most recently from 775e15f to 22581cc Compare August 18, 2023 09:19
go.mod Outdated Show resolved Hide resolved
Copy link
Collaborator

@flouthoc flouthoc left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

@vrothberg vrothberg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add a commit message.

go.mod Outdated Show resolved Hide resolved
@rhatdan
Copy link
Member Author

rhatdan commented Aug 21, 2023

I agree that adding k8s.io/kubernetes potentially opens us to Bloat, but for now this is only using the expansion subdir, so no bloat.

@vrothberg
Copy link
Member

I think @ygalblum brought up another issue though

BTW if we do add the dependency back, why are we taking such an old version?

Is this package not available in newer K8s versions?

@rhatdan
Copy link
Member Author

rhatdan commented Aug 21, 2023

@vrothberg yup, that is what happens when you grab someone else's original work and don't pay attention to versions.

Copy link
Member

@vrothberg vrothberg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@ygalblum
Copy link
Contributor

LGTM

Expect(inspectData[0].Args[1]).To(BeEquivalentTo("echo BAR"))
Expect(inspectData[0].Args[2]).To(BeEquivalentTo("${FOO}"))
Expect(inspectData[0].Args[3]).To(BeEquivalentTo("$(FOO)"))
Expect(inspectData[0].Args[4]).To(BeEquivalentTo("FOOBARBAZ"))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think something is wrong. I took the liberty of adding the test I requested:

		logs := podmanTest.Podman([]string{"logs", "echo_pod-foobar"})
		logs.WaitWithDefaultTimeout()
		Expect(logs).Should(Exit(0))

		Expect(logs.OutputToString()).To(Equal("BAR BAR BAR FOOBARBAZ"))

...and it's failing:

[FAILED] Expected                                                                                                                                             
      <string>: BAR BAR BAR BAR BAR BAR BAR BAR                                                                                                                 
  to equal                                                                                                                                                      
      <string>: BAR BAR BAR FOOBARBAZ

It's impossible to know which BAR corresponds to what, but I really don't like that failure. So, sorry, placing hold until someone explains it or fixes it.

@edsantiago
Copy link
Member

suggestion (also available as download):

	var testYAMLForEnvExpand = `
apiVersion: v1
kind: Pod
metadata:
  creationTimestamp: "2021-08-05T17:55:51Z"
  labels:
    app: foobar
  name: echo_pod
spec:
  containers:
  - command:
    - /bin/sh
    - -c
    - 'echo paren$(FOO) brace${FOO} dollardollarparen$$(FOO) interp$(FOO)olate'
    env:
    - name: PATH
      value: /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
    - name: TERM
      value: xterm
    - name: container
      value: podman
    - name: FOO
      value: BAR
    image: foobar
    name: foobar
    resources: {}
    securityContext:
      allowPrivilegeEscalation: true
      privileged: false
      readOnlyRootFilesystem: false
      seLinuxOptions: {}
    tty: true
    workingDir: /
  restartPolicy: Never
  dnsConfig: {}
status: {}
`
	It("Check that command is expanded", func() {
		// Setup
		yamlDir := filepath.Join(tempdir, RandomString(12))
		err := os.Mkdir(yamlDir, 0755)
		Expect(err).ToNot(HaveOccurred(), "mkdir "+yamlDir)
		err = writeYaml(testYAMLForEnvExpand, filepath.Join(yamlDir, "echo.yaml"))
		Expect(err).ToNot(HaveOccurred())
		app1Dir := filepath.Join(yamlDir, "foobar")
		err = os.Mkdir(app1Dir, 0755)
		Expect(err).ToNot(HaveOccurred())
		err = writeYaml(playBuildFile + `ENV FOO foo-from-buildfile
COPY FOO /bin/FOO
`, filepath.Join(app1Dir, "Containerfile"))
		Expect(err).ToNot(HaveOccurred())
		// Write a file to be copied
		err = writeYaml(copyFile, filepath.Join(app1Dir, "copyfile"))
		Expect(err).ToNot(HaveOccurred())

		// Shell expansion of $(FOO) in container needs executable FOO
		err = writeYaml(`#!/bin/sh
echo GOT-HERE
`, filepath.Join(app1Dir, "FOO"))
		Expect(err).ToNot(HaveOccurred())
		err = os.Chmod(filepath.Join(app1Dir, "FOO"), 0555)
		Expect(err).ToNot(HaveOccurred(), "chmod FOO")

		os.Setenv("FOO", "make sure we use FOO from kube file, not env")
		defer os.Unsetenv("FOO")

		// Switch to temp dir and restore it afterwards
		cwd, err := os.Getwd()
		Expect(err).ToNot(HaveOccurred())
		Expect(os.Chdir(yamlDir)).To(Succeed())
		defer func() { (Expect(os.Chdir(cwd)).To(BeNil())) }()

		session := podmanTest.Podman([]string{"play", "kube", "echo.yaml"})
		session.WaitWithDefaultTimeout()
		Expect(session).Should(Exit(0))

		logs := podmanTest.Podman([]string{"logs", "echo_pod-foobar"})
		logs.WaitWithDefaultTimeout()
		Expect(logs).Should(Exit(0))
		Expect(logs.OutputToString()).To(Equal("parenBAR braceBAR dollardollarparenGOT-HERE interpBARolate"))

		inspect := podmanTest.Podman([]string{"container", "inspect", "echo_pod-foobar"})
		inspect.WaitWithDefaultTimeout()
		Expect(inspect).Should(Exit(0))
		inspectData := inspect.InspectContainerToJSON()
		Expect(inspectData).ToNot(BeEmpty())

		// dollar-paren are expanded by podman, never seen by container
		Expect(inspectData[0].Args).To(HaveLen(2))
		Expect(inspectData[0].Args[1]).To(Equal("echo parenBAR brace${FOO} dollardollarparen$(FOO) interpBARolate"))
	})

pkg/domain/entities/events.go Outdated Show resolved Hide resolved
Kubernetes supports expanding $(FOOBAR) as environment variables within
the kube.YAML. When using podman kube play, we need to do the same, for
supporting these YAML files.

Fixes: containers#15983

Signed-off-by: Chee Hau Lim <ch33hau@gmail.com>
Signed-off-by: Daniel J Walsh <dwalsh@redhat.com>
@edsantiago
Copy link
Member

Tests LGTM, thank you for accepting my suggestion.

Copy link
Member

@Luap99 Luap99 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@openshift-ci
Copy link
Contributor

openshift-ci bot commented Aug 23, 2023

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: Luap99, rhatdan, vrothberg, ygalblum

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Needs approval from an approver in each of these files:
  • OWNERS [Luap99,rhatdan,vrothberg,ygalblum]

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@Luap99
Copy link
Member

Luap99 commented Aug 23, 2023

/lgtm

@openshift-ci openshift-ci bot added the lgtm Indicates that a PR is ready to be merged. label Aug 23, 2023
@openshift-merge-robot openshift-merge-robot merged commit c07f46e into containers:main Aug 23, 2023
@github-actions github-actions bot added the locked - please file new issue/PR Assist humans wanting to comment on an old issue or PR with locked comments. label Nov 22, 2023
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Nov 22, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
approved Indicates a PR has been approved by an approver from all required OWNERS files. lgtm Indicates that a PR is ready to be merged. locked - please file new issue/PR Assist humans wanting to comment on an old issue or PR with locked comments. release-note
Projects
None yet
Development

Successfully merging this pull request may close these issues.

play kube does not expand env in command
9 participants