forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate.go
82 lines (76 loc) · 2.92 KB
/
generate.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package util
import (
"github.com/golang/glog"
kapi "github.com/GoogleCloudPlatform/kubernetes/pkg/api"
"github.com/openshift/origin/pkg/build/api"
)
// GenerateBuildFromConfig creates a new build based on a given BuildConfig. Optionally a SourceRevision for the new
// build can be specified
func GenerateBuildFromConfig(bc *api.BuildConfig, r *api.SourceRevision) (build *api.Build) {
// Need to copy the buildConfig here so that it doesn't share pointers with
// the build object which could be (will be) modified later.
obj, _ := kapi.Scheme.Copy(bc)
bcCopy := obj.(*api.BuildConfig)
return &api.Build{
Parameters: api.BuildParameters{
Source: bcCopy.Parameters.Source,
Strategy: bcCopy.Parameters.Strategy,
Output: bcCopy.Parameters.Output,
Revision: r,
},
ObjectMeta: kapi.ObjectMeta{
Labels: map[string]string{api.BuildConfigLabel: bcCopy.Name},
},
}
}
// GenerateBuildFromBuild creates a new build based on a given Build.
func GenerateBuildFromBuild(build *api.Build) *api.Build {
obj, _ := kapi.Scheme.Copy(build)
buildCopy := obj.(*api.Build)
return &api.Build{
Parameters: buildCopy.Parameters,
ObjectMeta: kapi.ObjectMeta{
Labels: buildCopy.ObjectMeta.Labels,
},
}
}
// SubstituteImageReferences replaces references to an image with a new value
func SubstituteImageReferences(build *api.Build, oldImage string, newImage string) {
switch {
case build.Parameters.Strategy.Type == api.DockerBuildStrategyType &&
build.Parameters.Strategy.DockerStrategy != nil &&
build.Parameters.Strategy.DockerStrategy.BaseImage == oldImage:
build.Parameters.Strategy.DockerStrategy.BaseImage = newImage
case build.Parameters.Strategy.Type == api.STIBuildStrategyType &&
build.Parameters.Strategy.STIStrategy != nil &&
build.Parameters.Strategy.STIStrategy.Image == oldImage:
build.Parameters.Strategy.STIStrategy.Image = newImage
case build.Parameters.Strategy.Type == api.CustomBuildStrategyType:
// update env variable references to the old image with the new image
strategy := build.Parameters.Strategy.CustomStrategy
if strategy.Env == nil {
strategy.Env = make([]kapi.EnvVar, 1)
strategy.Env[0] = kapi.EnvVar{Name: api.CustomBuildStrategyBaseImageKey, Value: newImage}
} else {
found := false
for i := range strategy.Env {
glog.V(4).Infof("Checking env variable %s %s", strategy.Env[i].Name, strategy.Env[i].Value)
if strategy.Env[i].Name == api.CustomBuildStrategyBaseImageKey {
found = true
if strategy.Env[i].Value == oldImage {
strategy.Env[i].Value = newImage
glog.V(4).Infof("Updated env variable %s %s", strategy.Env[i].Name, strategy.Env[i].Value)
break
}
}
}
if !found {
strategy.Env = append(strategy.Env, kapi.EnvVar{Name: api.CustomBuildStrategyBaseImageKey, Value: newImage})
}
}
// update the actual custom build image with the new image, if applicable
if strategy.Image == oldImage {
strategy.Image = newImage
}
}
}