Skip to content

Support global environment variables defined by a component-level attribute #538

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

Merged
merged 3 commits into from
Aug 6, 2021

Conversation

amisevsk
Copy link
Collaborator

@amisevsk amisevsk commented Jul 30, 2021

This PR is a slightly cleaned up iteration of #529

What does this PR do?

Add support for the devfile 1.0 feature workspaceEnv (env vars supplied by a component that get added to all containers in the workspace). This is defined using an attribute on components named workspaceEnv formatted as []EnvVar, i.e.:

attributes:
  workspaceEnv: 
    - name: MY_ENV_1
      value: value
    - name: MY_ENV_2
      value: value

Note: it's necessary to define the attribute on components for now, because merging plugin components with top-level attributes is unsuitable to some use cases:

  1. If the base DevWorkspace defines a workspaceEnv attribute, any plugin that also defines a top-level workspaceEnv attribute will fail to merge during flattening
  2. If two plugins define workspaceEnv attributes at the top level, the second plugin will overwrite the value from the first, i.e. it's only possible to define workspaceEnv in one place in this way.

An alternative approach that would allow using top-level attributes would be some sort of namespacing mechanism, e.g. using workspace-env/my-component as the attribute, but this would be harder to process and write.

What issues does this PR fix or reference?

Closes #530

Is it tested? How?

Test cases are included; can be tested directly using the DevWorkspace

kind: DevWorkspace
apiVersion: workspace.devfile.io/v1alpha2
metadata:
  name: theia-next
spec:
  started: true
  template:
    components:
      - name: tooling
        attributes:
          workspaceEnv: 
            - name: TEST_ENV_1
              value: TEST_VAL_1
            - name: TEST_ENV_2
              value: TEST_VAL_2
        container:
          image: quay.io/wto/web-terminal-tooling
          args: ["tail", "-f", "/dev/null"]
      - name: theia
        plugin:
          uri: https://che-plugin-registry-main.surge.sh/v3/plugins/eclipse/che-theia/next/devfile.yaml

and checking that all containers in the resulting pod have the environment variables listed.

PR Checklist

  • E2E tests pass (when PR is ready, comment /test v7-devworkspaces-operator-e2e, v7-devworkspace-happy-path to trigger)
    • v7-devworkspaces-operator-e2e: DevWorkspace e2e test
    • v7-devworkspace-happy-path: DevWorkspace e2e test

Copy link
Member

@sleshchenko sleshchenko left a comment

Choose a reason for hiding this comment

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

LGTM

Just a few minor comments. Good job!

@@ -64,3 +65,14 @@ func formatImportCycle(end *resolutionContextTree) string {
}
return cycle
}

func getOriginalNameForComponent(component dw.Component) string {
Copy link
Member

Choose a reason for hiding this comment

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

Well, we don't modify components names, it returns exactly what is written in body: SourceComponent. Maybe then rename it to getSourceForComponent

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

We don't modify component names per se, but we delete plugin components and add e.g. container components:

components:
  - name: my-plugin
    plugin: [...]

becomes

components:
  - name: plugin-container
    container: [...]

But I see your point -- I'll update.

var componentEnv []dw.EnvVar
err := component.Attributes.GetInto(WorkspaceEnvAttribute, &componentEnv)
if err != nil {
return nil, fmt.Errorf("failed to read attribute %s on component %s: %w", WorkspaceEnvAttribute, getOriginalNameForComponent(component), err)
Copy link
Member

@sleshchenko sleshchenko Aug 2, 2021

Choose a reason for hiding this comment

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

validate the format of attributes can be +1 things we can validate with validate webhooks.

@@ -0,0 +1,75 @@
name: "Adds workspace environment variables from plugin"
Copy link
Member

Choose a reason for hiding this comment

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

please allign test files names, now some are with _, some only -, some with CamelCase for workspaceEnv, some has workspace-env.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Fixed for consistency -- note the format I've used thus far for _ vs - is that dashes replace spaces in the sentence describing the test, and error_ is the prefix that specifies that a test case is expected to return an error (it's mostly so that an alphabetical sort puts all error testcases together). If I've missed a place in that framework, I'll fix it.

@@ -0,0 +1,84 @@
name: "Adds JSON workspace environment variables from plugin "
Copy link
Member

Choose a reason for hiding this comment

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

File name and case name does not seem to be really the same.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Copy-paste error :)

@@ -0,0 +1,84 @@
name: "Adds workspace environment variables from plugin"
Copy link
Member

Choose a reason for hiding this comment

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

This suite case appear the second time

Copy link
Contributor

@JPinkney JPinkney left a comment

Choose a reason for hiding this comment

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

Other then Serhii's comments it looks good to me

@openshift-ci openshift-ci bot removed the lgtm label Aug 4, 2021
@openshift-ci openshift-ci bot added the lgtm label Aug 4, 2021
@openshift-ci
Copy link

openshift-ci bot commented Aug 4, 2021

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: amisevsk, JPinkney, sleshchenko

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 [JPinkney,amisevsk,sleshchenko]

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

@sleshchenko
Copy link
Member

/test v7-devworkspaces-operator-e2e, v7-devworkspace-happy-path

@sleshchenko
Copy link
Member

@amisevsk feel free to merge with squash or rebase to get rid of fixups and rerun tests.

Add support for workspaceEnv (i.e. environment variables provided by a
component that get added to all other components) though setting an
attribute 'workspaceEnv' in a component. If that component is added to
the workspace (either directly or through a plugin) the environment
variables defined within are added to all containers and init containers
in the workspace.

Example: a component

  components:
  - name: my-component
    attributes:
      workspaceEnv:
        - name: MY_ENV
          value: my-value
        - name: MY_ENV_2
          value: my-value-2

will add the MY_ENV and MY_ENV_2 environment variables to all containers
in the DevWorkspace.

Note we use component attributes rather than top-level attributes for
workspaceEnv, since top-level attributes pose some problems for
flattening:
- If the base DevWorkspace defines workspaceEnv in top-level attributes
  and a plugin does the same, flattening fails
- If two plugins define top-level workspaceEnv attributes, the
  workspaceEnv of the first if overwritten by the second

Signed-off-by: Angel Misevski <[email protected]>
Extract common setup for tests to a separate function

Signed-off-by: Angel Misevski <[email protected]>
@amisevsk amisevsk force-pushed the support-workspace-env branch from 6a5d12f to a3b6122 Compare August 4, 2021 14:29
@openshift-ci openshift-ci bot removed the lgtm label Aug 4, 2021
@openshift-ci
Copy link

openshift-ci bot commented Aug 4, 2021

New changes are detected. LGTM label has been removed.

@amisevsk
Copy link
Collaborator Author

amisevsk commented Aug 4, 2021

/test v7-devworkspaces-operator-e2e, v7-devworkspace-happy-path

@amisevsk amisevsk merged commit 0f248ed into devfile:main Aug 6, 2021
@amisevsk amisevsk deleted the support-workspace-env branch August 6, 2021 18:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add support for defining environment variables for all containers in a component
3 participants