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

Support valueFrom syntax for build env vars #14143

Merged
merged 1 commit into from
Jun 17, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 5 additions & 5 deletions api/swagger-spec/oapi-v1.json
Original file line number Diff line number Diff line change
Expand Up @@ -23726,7 +23726,7 @@
"items": {
"$ref": "v1.EnvVar"
},
"description": "env contains additional environment variables you want to pass into a builder container. ValueFrom is not supported."
"description": "env contains additional environment variables you want to pass into a builder container."
},
"forcePull": {
"type": "boolean",
Expand Down Expand Up @@ -23896,7 +23896,7 @@
"items": {
"$ref": "v1.EnvVar"
},
"description": "env contains additional environment variables you want to pass into a builder container. ValueFrom is not supported."
"description": "env contains additional environment variables you want to pass into a builder container."
},
"scripts": {
"type": "string",
Expand Down Expand Up @@ -23943,7 +23943,7 @@
"items": {
"$ref": "v1.EnvVar"
},
"description": "env contains additional environment variables you want to pass into a builder container. ValueFrom is not supported."
"description": "env contains additional environment variables you want to pass into a builder container."
},
"exposeDockerSocket": {
"type": "boolean",
Expand Down Expand Up @@ -24001,7 +24001,7 @@
"items": {
"$ref": "v1.EnvVar"
},
"description": "env contains additional environment variables you want to pass into a build pipeline. ValueFrom is not supported."
"description": "env contains additional environment variables you want to pass into a build pipeline."
}
}
},
Expand Down Expand Up @@ -24296,7 +24296,7 @@
"items": {
"$ref": "v1.EnvVar"
},
"description": "env contains additional environment variables you want to pass into a builder container. ValueFrom is not supported."
"description": "env contains additional environment variables you want to pass into a builder container."
},
"triggeredBy": {
"type": "array",
Expand Down
10 changes: 5 additions & 5 deletions api/swagger-spec/openshift-openapi-spec.json
Original file line number Diff line number Diff line change
Expand Up @@ -72897,7 +72897,7 @@
"$ref": "#/definitions/v1.DockerStrategyOptions"
},
"env": {
"description": "env contains additional environment variables you want to pass into a builder container. ValueFrom is not supported.",
"description": "env contains additional environment variables you want to pass into a builder container.",
"type": "array",
"items": {
"$ref": "#/definitions/v1.EnvVar"
Expand Down Expand Up @@ -74251,7 +74251,7 @@
"type": "string"
},
"env": {
"description": "env contains additional environment variables you want to pass into a builder container. ValueFrom is not supported.",
"description": "env contains additional environment variables you want to pass into a builder container.",
"type": "array",
"items": {
"$ref": "#/definitions/v1.EnvVar"
Expand Down Expand Up @@ -74859,7 +74859,7 @@
"type": "string"
},
"env": {
"description": "env contains additional environment variables you want to pass into a builder container. ValueFrom is not supported.",
"description": "env contains additional environment variables you want to pass into a builder container.",
"type": "array",
"items": {
"$ref": "#/definitions/v1.EnvVar"
Expand Down Expand Up @@ -76722,7 +76722,7 @@
"description": "JenkinsPipelineBuildStrategy holds parameters specific to a Jenkins Pipeline build. This strategy is in tech preview.",
"properties": {
"env": {
"description": "env contains additional environment variables you want to pass into a build pipeline. ValueFrom is not supported.",
"description": "env contains additional environment variables you want to pass into a build pipeline.",
"type": "array",
"items": {
"$ref": "#/definitions/v1.EnvVar"
Expand Down Expand Up @@ -81535,7 +81535,7 @@
],
"properties": {
"env": {
"description": "env contains additional environment variables you want to pass into a builder container. ValueFrom is not supported.",
"description": "env contains additional environment variables you want to pass into a builder container.",
"type": "array",
"items": {
"$ref": "#/definitions/v1.EnvVar"
Expand Down
29 changes: 8 additions & 21 deletions pkg/build/admission/defaults/admission.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/openshift/origin/pkg/build/admission/defaults/api/validation"
buildapi "github.com/openshift/origin/pkg/build/api"
"github.com/openshift/origin/pkg/build/util"
buildutil "github.com/openshift/origin/pkg/build/util"
configapi "github.com/openshift/origin/pkg/cmd/server/api"
)

Expand Down Expand Up @@ -103,10 +104,9 @@ func (b BuildDefaults) applyPodDefaults(pod *kapi.Pod) {

func (b BuildDefaults) applyBuildDefaults(build *buildapi.Build) {
// Apply default env
buildEnv := getBuildEnv(build)
for _, envVar := range b.config.Env {
glog.V(5).Infof("Adding default environment variable %s=%s to build %s/%s", envVar.Name, envVar.Value, build.Namespace, build.Name)
addDefaultEnvVar(envVar, buildEnv)
addDefaultEnvVar(build, envVar)
}

// Apply default labels
Expand Down Expand Up @@ -174,29 +174,16 @@ func (b BuildDefaults) applyBuildDefaults(build *buildapi.Build) {
}
}

func getBuildEnv(build *buildapi.Build) *[]kapi.EnvVar {
switch {
case build.Spec.Strategy.DockerStrategy != nil:
return &build.Spec.Strategy.DockerStrategy.Env
case build.Spec.Strategy.SourceStrategy != nil:
return &build.Spec.Strategy.SourceStrategy.Env
case build.Spec.Strategy.CustomStrategy != nil:
return &build.Spec.Strategy.CustomStrategy.Env
}
return nil
}

func addDefaultEnvVar(v kapi.EnvVar, envVars *[]kapi.EnvVar) {
if envVars == nil {
return
}
func addDefaultEnvVar(build *buildapi.Build, v kapi.EnvVar) {
envVars := buildutil.GetBuildEnv(build)

for i := range *envVars {
if (*envVars)[i].Name == v.Name {
for i := range envVars {
if envVars[i].Name == v.Name {
return
}
}
*envVars = append(*envVars, v)
envVars = append(envVars, v)
buildutil.SetBuildEnv(build, envVars)
}

func addDefaultLabel(defaultLabel buildapi.ImageLabel, buildLabels *[]buildapi.ImageLabel) {
Expand Down
5 changes: 3 additions & 2 deletions pkg/build/admission/defaults/admission_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
defaultsapi "github.com/openshift/origin/pkg/build/admission/defaults/api"
u "github.com/openshift/origin/pkg/build/admission/testutil"
buildapi "github.com/openshift/origin/pkg/build/api"
buildutil "github.com/openshift/origin/pkg/build/util"

_ "github.com/openshift/origin/pkg/api/install"
)
Expand Down Expand Up @@ -73,9 +74,9 @@ func TestEnvDefaults(t *testing.T) {
t.Fatalf("unexpected error: %v", err)
}

env := getBuildEnv(build)
env := buildutil.GetBuildEnv(build)
var1found, var2found := false, false
for _, ev := range *env {
for _, ev := range env {
if ev.Name == "VAR1" {
if ev.Value != "VALUE1" {
t.Errorf("unexpected value %s", ev.Value)
Expand Down
14 changes: 9 additions & 5 deletions pkg/build/admission/defaults/api/validation/validation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,19 +83,23 @@ func TestValidateBuildDefaultsConfig(t *testing.T) {
errField: "env[0].name",
errType: field.ErrorTypeInvalid,
},
// 5: valueFrom present in env var
// 5: ResourceFieldRef present in env var
{
config: &defaultsapi.BuildDefaultsConfig{
Env: []kapi.EnvVar{
{
Name: "name",
Value: "test",
ValueFrom: &kapi.EnvVarSource{},
Name: "name",
ValueFrom: &kapi.EnvVarSource{
ResourceFieldRef: &kapi.ResourceFieldSelector{
ContainerName: "name",
Resource: "resource",
},
},
},
},
},
errExpected: true,
errField: "env[0].valueFrom",
errField: "env[0].valueFrom.ResourceFieldRef",
errType: field.ErrorTypeInvalid,
},
// 6: label: empty name
Expand Down
46 changes: 23 additions & 23 deletions pkg/build/api/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -483,31 +483,36 @@ const (
// one container with a non-zero exit status.
StatusReasonFailedContainer StatusReason = "FailedContainer"

// StatusReasonUnresolvableEnvironmentVariable indicates that an error occurred processing
// the supplied options for environment variables in the build strategy environment
StatusReasonUnresolvableEnvironmentVariable StatusReason = "UnresolvableEnvironmentVariable"

// StatusReasonGenericBuildFailed is the reason associated with a broad
// range of build failures.
StatusReasonGenericBuildFailed StatusReason = "GenericBuildFailed"
)

// NOTE: These messages might change.
const (
StatusMessageCannotCreateBuildPodSpec = "Failed to create pod spec."
StatusMessageCannotCreateBuildPod = "Failed creating build pod."
StatusMessageInvalidOutputRef = "Output image could not be resolved."
StatusMessageCancelBuildFailed = "Failed to cancel build."
StatusMessageBuildPodDeleted = "The pod for this build was deleted before the build completed."
StatusMessageExceededRetryTimeout = "Build did not complete and retrying timed out."
StatusMessageMissingPushSecret = "Missing push secret."
StatusMessagePostCommitHookFailed = "Build failed because of post commit hook."
StatusMessagePushImageToRegistryFailed = "Failed to push the image to the registry."
StatusMessagePullBuilderImageFailed = "Failed pulling builder image."
StatusMessageFetchSourceFailed = "Failed to fetch the input source."
StatusMessageInvalidContextDirectory = "The supplied context directory does not exist."
StatusMessageCancelledBuild = "The build was cancelled by the user."
StatusMessageDockerBuildFailed = "Docker build strategy has failed."
StatusMessageBuildPodExists = "The pod for this build already exists and is older than the build."
StatusMessageNoBuildContainerStatus = "The pod for this build has no container statuses indicating success or failure."
StatusMessageFailedContainer = "The pod for this build has at least one container with a non-zero exit status."
StatusMessageGenericBuildFailed = "Generic Build failure - check logs for details."
StatusMessageCannotCreateBuildPodSpec = "Failed to create pod spec."
StatusMessageCannotCreateBuildPod = "Failed creating build pod."
StatusMessageInvalidOutputRef = "Output image could not be resolved."
StatusMessageCancelBuildFailed = "Failed to cancel build."
StatusMessageBuildPodDeleted = "The pod for this build was deleted before the build completed."
StatusMessageExceededRetryTimeout = "Build did not complete and retrying timed out."
StatusMessageMissingPushSecret = "Missing push secret."
StatusMessagePostCommitHookFailed = "Build failed because of post commit hook."
StatusMessagePushImageToRegistryFailed = "Failed to push the image to the registry."
StatusMessagePullBuilderImageFailed = "Failed pulling builder image."
StatusMessageFetchSourceFailed = "Failed to fetch the input source."
StatusMessageInvalidContextDirectory = "The supplied context directory does not exist."
StatusMessageCancelledBuild = "The build was cancelled by the user."
StatusMessageDockerBuildFailed = "Docker build strategy has failed."
StatusMessageBuildPodExists = "The pod for this build already exists and is older than the build."
StatusMessageNoBuildContainerStatus = "The pod for this build has no container statuses indicating success or failure."
StatusMessageFailedContainer = "The pod for this build has at least one container with a non-zero exit status."
StatusMessageGenericBuildFailed = "Generic Build failure - check logs for details."
StatusMessageUnresolvableEnvironmentVariable = "Unable to resolve build environment variable reference."
)

// BuildStatusOutput contains the status of the built image.
Expand Down Expand Up @@ -720,7 +725,6 @@ type CustomBuildStrategy struct {
PullSecret *kapi.LocalObjectReference

// Env contains additional environment variables you want to pass into a builder container.
// ValueFrom is not supported.
Copy link
Contributor

Choose a reason for hiding this comment

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

Please also update docs in v1/types.go

Copy link
Member Author

Choose a reason for hiding this comment

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

fixed in next push

Env []kapi.EnvVar

// ExposeDockerSocket will allow running Docker commands (and build Docker images) from
Expand Down Expand Up @@ -777,7 +781,6 @@ type DockerBuildStrategy struct {
NoCache bool

// Env contains additional environment variables you want to pass into a builder container.
// ValueFrom is not supported.
Env []kapi.EnvVar

// Args contains any build arguments that are to be passed to Docker. See
Expand Down Expand Up @@ -813,7 +816,6 @@ type SourceBuildStrategy struct {
PullSecret *kapi.LocalObjectReference

// Env contains additional environment variables you want to pass into a builder container.
// ValueFrom is not supported.
Env []kapi.EnvVar

// Scripts is the location of Source scripts
Expand Down Expand Up @@ -852,7 +854,6 @@ type JenkinsPipelineBuildStrategy struct {
Jenkinsfile string

// Env contains additional environment variables you want to pass into a build pipeline.
// ValueFrom is not supported.
Copy link
Contributor

Choose a reason for hiding this comment

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

since based on other discussion i think we're supporting ValueFrom in BuildRequest.Env now too, please update the doc there too (And make sure our validation allows it).

Copy link
Member Author

Choose a reason for hiding this comment

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

updated in next push

Env []kapi.EnvVar
}

Expand Down Expand Up @@ -1209,7 +1210,6 @@ type BuildRequest struct {
LastVersion *int64

// Env contains additional environment variables you want to pass into a builder container.
// ValueFrom is not supported.
Env []kapi.EnvVar

// TriggeredBy describes which triggers started the most recent update to the
Expand Down
5 changes: 0 additions & 5 deletions pkg/build/api/v1/generated.proto

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading