Browse code

Inherit LOGLEVEL in Build pods from OpenShift

Michal Fojtik authored on 2015/03/25 01:00:18
Showing 9 changed files
... ...
@@ -13,4 +13,4 @@
13 13
 FROM openshift/origin
14 14
 
15 15
 ENV HOME /root
16
-ENTRYPOINT ["/usr/bin/openshift-docker-build", "--loglevel=5"]
16
+ENTRYPOINT ["/usr/bin/openshift-docker-build"]
... ...
@@ -13,4 +13,4 @@
13 13
 FROM openshift/origin
14 14
 
15 15
 ENV HOME /root
16
-ENTRYPOINT ["/usr/bin/openshift-sti-build", "--loglevel=5"]
16
+ENTRYPOINT ["/usr/bin/openshift-sti-build"]
... ...
@@ -1,10 +1,13 @@
1 1
 package strategy
2 2
 
3 3
 import (
4
+	"fmt"
5
+
4 6
 	kapi "github.com/GoogleCloudPlatform/kubernetes/pkg/api"
5 7
 	"github.com/GoogleCloudPlatform/kubernetes/pkg/runtime"
6 8
 
7 9
 	buildapi "github.com/openshift/origin/pkg/build/api"
10
+	cmdutil "github.com/openshift/origin/pkg/cmd/util"
8 11
 )
9 12
 
10 13
 // DockerBuildStrategy creates a Docker build using a Docker builder image.
... ...
@@ -37,6 +40,7 @@ func (bs *DockerBuildStrategy) CreateBuildPod(build *buildapi.Build) (*kapi.Pod,
37 37
 					Env: []kapi.EnvVar{
38 38
 						{Name: "BUILD", Value: string(data)},
39 39
 					},
40
+					Command: []string{"--loglevel=" + fmt.Sprintf("%d", cmdutil.GetLogLevel())},
40 41
 					// TODO: run unprivileged https://github.com/openshift/origin/issues/662
41 42
 					Privileged: true,
42 43
 				},
... ...
@@ -36,7 +36,7 @@ func TestDockerCreateBuildPod(t *testing.T) {
36 36
 		t.Errorf("Expected never, got %#v", actual.Spec.RestartPolicy)
37 37
 	}
38 38
 	if len(container.Env) != 1 {
39
-		t.Fatalf("Expected 1 elements in Env table, got %d", len(container.Env))
39
+		t.Fatalf("Expected 1 element in Env table, got %d", len(container.Env))
40 40
 	}
41 41
 	buildJSON, _ := v1beta1.Codec.Encode(expected)
42 42
 	errorCases := map[int][]string{
... ...
@@ -1,12 +1,14 @@
1 1
 package strategy
2 2
 
3 3
 import (
4
+	"fmt"
4 5
 	"io/ioutil"
5 6
 
6 7
 	kapi "github.com/GoogleCloudPlatform/kubernetes/pkg/api"
7 8
 	"github.com/GoogleCloudPlatform/kubernetes/pkg/runtime"
8 9
 
9 10
 	buildapi "github.com/openshift/origin/pkg/build/api"
11
+	cmdutil "github.com/openshift/origin/pkg/cmd/util"
10 12
 )
11 13
 
12 14
 // STIBuildStrategy creates STI(source to image) builds
... ...
@@ -42,11 +44,12 @@ func (bs *STIBuildStrategy) CreateBuildPod(build *buildapi.Build) (*kapi.Pod, er
42 42
 	containerEnv := []kapi.EnvVar{
43 43
 		{Name: "BUILD", Value: string(data)},
44 44
 		{Name: "SOURCE_REPOSITORY", Value: build.Parameters.Source.Git.URI},
45
+		{Name: "BUILD_LOGLEVEL", Value: fmt.Sprintf("%d", cmdutil.GetLogLevel())},
45 46
 	}
46 47
 
47 48
 	strategy := build.Parameters.Strategy.STIStrategy
48 49
 	if len(strategy.Env) > 0 {
49
-		containerEnv = append(containerEnv, strategy.Env...)
50
+		mergeEnvWithoutDuplicates(strategy.Env, &containerEnv)
50 51
 	}
51 52
 
52 53
 	pod := &kapi.Pod{
... ...
@@ -62,6 +65,7 @@ func (bs *STIBuildStrategy) CreateBuildPod(build *buildapi.Build) (*kapi.Pod, er
62 62
 					Env:   containerEnv,
63 63
 					// TODO: run unprivileged https://github.com/openshift/origin/issues/662
64 64
 					Privileged: true,
65
+					Command:    []string{"--loglevel=" + getContainerVerbosity(containerEnv)},
65 66
 				},
66 67
 			},
67 68
 			RestartPolicy: kapi.RestartPolicyNever,
... ...
@@ -41,8 +41,8 @@ func TestSTICreateBuildPod(t *testing.T) {
41 41
 	if actual.Spec.RestartPolicy != kapi.RestartPolicyNever {
42 42
 		t.Errorf("Expected never, got %#v", actual.Spec.RestartPolicy)
43 43
 	}
44
-	if len(container.Env) != 3 {
45
-		t.Fatalf("Expected 3 elements in Env table, got %d", len(container.Env))
44
+	if len(container.Env) != 4 {
45
+		t.Fatalf("Expected 4 elements in Env table, got %d", len(container.Env))
46 46
 	}
47 47
 	found := false
48 48
 	for _, v := range container.Env {
... ...
@@ -107,3 +107,38 @@ func setupDockerSecrets(pod *kapi.Pod, pushSecret string) {
107 107
 		{Name: "PULL_DOCKERCFG_PATH", Value: filepath.Join(volumeMount.MountPath, pushSecret)},
108 108
 	}...)
109 109
 }
110
+
111
+// mergeEnvWithoutDuplicates merges two environment lists without having
112
+// duplicate items in the output list.
113
+func mergeEnvWithoutDuplicates(source []kapi.EnvVar, output *[]kapi.EnvVar) {
114
+	type sourceMapItem struct {
115
+		index int
116
+		value string
117
+	}
118
+	// Convert source to Map for faster access
119
+	sourceMap := make(map[string]sourceMapItem)
120
+	for i, env := range source {
121
+		sourceMap[env.Name] = sourceMapItem{i, env.Value}
122
+	}
123
+	result := *output
124
+	for i, env := range result {
125
+		// If the value exists in output, override it and remove it
126
+		// from the source list
127
+		if v, found := sourceMap[env.Name]; found {
128
+			result[i].Value = v.value
129
+			source = append(source[:v.index], source[v.index+1:]...)
130
+		}
131
+	}
132
+	*output = append(result, source...)
133
+}
134
+
135
+// getContainerVerbosity returns the defined BUILD_LOGLEVEL value
136
+func getContainerVerbosity(containerEnv []kapi.EnvVar) (verbosity string) {
137
+	for _, env := range containerEnv {
138
+		if env.Name == "BUILD_LOGLEVEL" {
139
+			verbosity = env.Value
140
+			break
141
+		}
142
+	}
143
+	return
144
+}
... ...
@@ -99,3 +99,26 @@ func TestSetupBuildEnvFails(t *testing.T) {
99 99
 		t.Errorf("unexpected non-error: %v", err)
100 100
 	}
101 101
 }
102
+
103
+func TestMergeEnvWithoutDuplicates(t *testing.T) {
104
+	input := []kapi.EnvVar{
105
+		{Name: "foo", Value: "bar"},
106
+		{Name: "input", Value: "inputVal"},
107
+	}
108
+	output := []kapi.EnvVar{
109
+		{Name: "foo", Value: "test"},
110
+	}
111
+
112
+	mergeEnvWithoutDuplicates(input, &output)
113
+
114
+	if len(output) != 2 {
115
+		t.Errorf("Expected output to contain input items len!=2 (%d)", len(output))
116
+	}
117
+
118
+	if output[0].Name != "foo" {
119
+		t.Errorf("Expected output to have env 'foo', got %+v", output[0])
120
+	}
121
+	if output[0].Value != "bar" {
122
+		t.Errorf("Expected output env 'foo' to have value 'bar', got %+v", output[0])
123
+	}
124
+}
102 125
new file mode 100644
... ...
@@ -0,0 +1,13 @@
0
+package util
1
+
2
+import "github.com/golang/glog"
3
+
4
+// GetLogLevel returns the current glog log level
5
+func GetLogLevel() (level int) {
6
+	for level = 5; level >= 0; level-- {
7
+		if glog.V(glog.Level(level)) == true {
8
+			break
9
+		}
10
+	}
11
+	return
12
+}