Skip to content

Commit 66c9d91

Browse files
committed
move builder image code to v1 apis
1 parent b27d4b4 commit 66c9d91

17 files changed

+347
-310
lines changed

hack/import-restrictions.json

+1-2
Original file line numberDiff line numberDiff line change
@@ -115,8 +115,7 @@
115115
"allowedImportPackages": [
116116
"vendor/k8s.io/kubernetes/pkg/apis/core",
117117
"vendor/k8s.io/kubernetes/pkg/apis/core/v1",
118-
"github.com/openshift/origin/pkg/image/apis/image",
119-
"vendor/github.com/golang/glog"
118+
"github.com/openshift/origin/pkg/image/apis/image"
120119
]
121120
},
122121

pkg/build/apis/build/helpers.go

-54
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,7 @@
11
package build
22

33
import (
4-
"time"
5-
6-
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
74
kapi "k8s.io/kubernetes/pkg/apis/core"
8-
9-
"github.com/golang/glog"
105
)
116

127
// BuildToPodLogOptions builds a PodLogOptions object out of a BuildLogOptions.
@@ -81,55 +76,6 @@ func HasTriggerType(triggerType BuildTriggerType, bc *BuildConfig) bool {
8176
return len(matches) > 0
8277
}
8378

84-
// RecordStageAndStepInfo records details about each build stage and step
85-
func RecordStageAndStepInfo(stages []StageInfo, stageName StageName, stepName StepName, startTime metav1.Time, endTime metav1.Time) []StageInfo {
86-
// If the stage already exists in the slice, update the DurationMilliseconds, and append the new step.
87-
for stageKey, stageVal := range stages {
88-
if stageVal.Name == stageName {
89-
for _, step := range stages[stageKey].Steps {
90-
if step.Name == stepName {
91-
glog.V(4).Infof("error recording build timing information, step %v already exists in stage %v", stepName, stageName)
92-
}
93-
}
94-
stages[stageKey].DurationMilliseconds = endTime.Time.Sub(stages[stageKey].StartTime.Time).Nanoseconds() / int64(time.Millisecond)
95-
if len(stages[stageKey].Steps) == 0 {
96-
stages[stageKey].Steps = make([]StepInfo, 0)
97-
}
98-
stages[stageKey].Steps = append(stages[stageKey].Steps, StepInfo{
99-
Name: stepName,
100-
StartTime: startTime,
101-
DurationMilliseconds: endTime.Time.Sub(startTime.Time).Nanoseconds() / int64(time.Millisecond),
102-
})
103-
return stages
104-
}
105-
}
106-
107-
// If the stageName does not exist, add it to the slice along with the new step.
108-
var steps []StepInfo
109-
steps = append(steps, StepInfo{
110-
Name: stepName,
111-
StartTime: startTime,
112-
DurationMilliseconds: endTime.Time.Sub(startTime.Time).Nanoseconds() / int64(time.Millisecond),
113-
})
114-
stages = append(stages, StageInfo{
115-
Name: stageName,
116-
StartTime: startTime,
117-
DurationMilliseconds: endTime.Time.Sub(startTime.Time).Nanoseconds() / int64(time.Millisecond),
118-
Steps: steps,
119-
})
120-
return stages
121-
}
122-
123-
// AppendStageAndStepInfo appends the step info from one stages slice into another.
124-
func AppendStageAndStepInfo(stages []StageInfo, stagesToMerge []StageInfo) []StageInfo {
125-
for _, stage := range stagesToMerge {
126-
for _, step := range stage.Steps {
127-
stages = RecordStageAndStepInfo(stages, stage.Name, step.Name, step.StartTime, metav1.NewTime(step.StartTime.Add(time.Duration(step.DurationMilliseconds)*time.Millisecond)))
128-
}
129-
}
130-
return stages
131-
}
132-
13379
// GetInputReference returns the From ObjectReference associated with the
13480
// BuildStrategy.
13581
func GetInputReference(strategy BuildStrategy) *kapi.ObjectReference {

pkg/build/builder/cmd/builder.go

+26-31
Original file line numberDiff line numberDiff line change
@@ -11,37 +11,34 @@ import (
1111
"github.com/golang/glog"
1212
"k8s.io/kubernetes/pkg/api/legacyscheme"
1313

14-
"k8s.io/apimachinery/pkg/api/errors"
1514
"k8s.io/apimachinery/pkg/runtime"
16-
"k8s.io/apimachinery/pkg/runtime/schema"
1715
restclient "k8s.io/client-go/rest"
1816

1917
s2iapi "github.com/openshift/source-to-image/pkg/api"
2018
s2igit "github.com/openshift/source-to-image/pkg/scm/git"
2119

22-
buildapi "github.com/openshift/origin/pkg/build/apis/build"
23-
"github.com/openshift/origin/pkg/build/apis/build/validation"
20+
buildapiv1 "github.com/openshift/api/build/v1"
21+
buildclientv1 "github.com/openshift/client-go/build/clientset/versioned/typed/build/v1"
2422
bld "github.com/openshift/origin/pkg/build/builder"
2523
"github.com/openshift/origin/pkg/build/builder/cmd/scmauth"
2624
"github.com/openshift/origin/pkg/build/builder/timing"
27-
buildclient "github.com/openshift/origin/pkg/build/generated/internalclientset"
28-
buildinternalversion "github.com/openshift/origin/pkg/build/generated/internalclientset/typed/build/internalversion"
25+
builderutil "github.com/openshift/origin/pkg/build/builder/util"
2926
buildutil "github.com/openshift/origin/pkg/build/util"
3027
"github.com/openshift/origin/pkg/generate/git"
3128
"github.com/openshift/origin/pkg/version"
3229
)
3330

3431
type builder interface {
35-
Build(dockerClient bld.DockerClient, sock string, buildsClient buildinternalversion.BuildResourceInterface, build *buildapi.Build, cgLimits *s2iapi.CGroupLimits) error
32+
Build(dockerClient bld.DockerClient, sock string, buildsClient buildclientv1.BuildInterface, build *buildapiv1.Build, cgLimits *s2iapi.CGroupLimits) error
3633
}
3734

3835
type builderConfig struct {
3936
out io.Writer
40-
build *buildapi.Build
37+
build *buildapiv1.Build
4138
sourceSecretDir string
4239
dockerClient *docker.Client
4340
dockerEndpoint string
44-
buildsClient buildinternalversion.BuildResourceInterface
41+
buildsClient buildclientv1.BuildInterface
4542
}
4643

4744
func newBuilderConfigFromEnvironment(out io.Writer, needsDocker bool) (*builderConfig, error) {
@@ -51,16 +48,17 @@ func newBuilderConfigFromEnvironment(out io.Writer, needsDocker bool) (*builderC
5148
cfg.out = out
5249

5350
buildStr := os.Getenv("BUILD")
54-
cfg.build = &buildapi.Build{}
5551

56-
obj, groupVersionKind, err := legacyscheme.Codecs.UniversalDecoder().Decode([]byte(buildStr), nil, nil)
52+
cfg.build = &buildapiv1.Build{}
53+
54+
obj, groupVersionKind, err := legacyscheme.Codecs.UniversalDecoder().Decode([]byte(buildStr), nil, cfg.build)
5755
if err != nil {
5856
return nil, fmt.Errorf("unable to parse build string: %v", err)
5957
}
6058
ok := false
61-
cfg.build, ok = obj.(*buildapi.Build)
59+
cfg.build, ok = obj.(*buildapiv1.Build)
6260
if !ok {
63-
return nil, fmt.Errorf("build string is not a build: %v", err)
61+
return nil, fmt.Errorf("build string %s is not a build: %#v", buildStr, obj)
6462
}
6563
if glog.V(4) {
6664
redactedBuild := buildutil.SafeForLoggingBuild(cfg.build)
@@ -73,11 +71,8 @@ func newBuilderConfigFromEnvironment(out io.Writer, needsDocker bool) (*builderC
7371
}
7472
glog.V(4).Infof("redacted build: %v", string(bytes))
7573
}
76-
if errs := validation.ValidateBuild(cfg.build); len(errs) > 0 {
77-
return nil, errors.NewInvalid(schema.GroupKind{Kind: "Build"}, cfg.build.Name, errs)
78-
}
7974

80-
masterVersion := os.Getenv(buildapi.OriginVersion)
75+
masterVersion := os.Getenv(builderutil.OriginVersion)
8176
thisVersion := version.Get().String()
8277
if len(masterVersion) != 0 && masterVersion != thisVersion {
8378
glog.V(3).Infof("warning: OpenShift server version %q differs from this image %q\n", masterVersion, thisVersion)
@@ -102,11 +97,11 @@ func newBuilderConfigFromEnvironment(out io.Writer, needsDocker bool) (*builderC
10297
if err != nil {
10398
return nil, fmt.Errorf("cannot connect to the server: %v", err)
10499
}
105-
buildsClient, err := buildclient.NewForConfig(clientConfig)
100+
buildsClient, err := buildclientv1.NewForConfig(clientConfig)
106101
if err != nil {
107102
return nil, fmt.Errorf("failed to get client: %v", err)
108103
}
109-
cfg.buildsClient = buildsClient.Build().Builds(cfg.build.Namespace)
104+
cfg.buildsClient = buildsClient.Builds(cfg.build.Namespace)
110105

111106
return cfg, nil
112107
}
@@ -158,7 +153,7 @@ func (c *builderConfig) setupGitEnvironment() (string, []string, error) {
158153
// clone is responsible for cloning the source referenced in the buildconfig
159154
func (c *builderConfig) clone() error {
160155
ctx := timing.NewContext(context.Background())
161-
var sourceRev *buildapi.SourceRevision
156+
var sourceRev *buildapiv1.SourceRevision
162157
defer func() {
163158
c.build.Status.Stages = timing.GetStages(ctx)
164159
bld.HandleBuildStatusUpdate(c.build, c.buildsClient, sourceRev)
@@ -174,9 +169,9 @@ func (c *builderConfig) clone() error {
174169
buildDir := buildutil.InputContentPath
175170
sourceInfo, err := bld.GitClone(ctx, gitClient, c.build.Spec.Source.Git, c.build.Spec.Revision, buildDir)
176171
if err != nil {
177-
c.build.Status.Phase = buildapi.BuildPhaseFailed
178-
c.build.Status.Reason = buildapi.StatusReasonFetchSourceFailed
179-
c.build.Status.Message = buildapi.StatusMessageFetchSourceFailed
172+
c.build.Status.Phase = buildapiv1.BuildPhaseFailed
173+
c.build.Status.Reason = buildapiv1.StatusReasonFetchSourceFailed
174+
c.build.Status.Message = builderutil.StatusMessageFetchSourceFailed
180175
return err
181176
}
182177

@@ -186,18 +181,18 @@ func (c *builderConfig) clone() error {
186181

187182
err = bld.ExtractInputBinary(os.Stdin, c.build.Spec.Source.Binary, buildDir)
188183
if err != nil {
189-
c.build.Status.Phase = buildapi.BuildPhaseFailed
190-
c.build.Status.Reason = buildapi.StatusReasonFetchSourceFailed
191-
c.build.Status.Message = buildapi.StatusMessageFetchSourceFailed
184+
c.build.Status.Phase = buildapiv1.BuildPhaseFailed
185+
c.build.Status.Reason = buildapiv1.StatusReasonFetchSourceFailed
186+
c.build.Status.Message = builderutil.StatusMessageFetchSourceFailed
192187
return err
193188
}
194189

195190
if len(c.build.Spec.Source.ContextDir) > 0 {
196191
if _, err := os.Stat(filepath.Join(buildDir, c.build.Spec.Source.ContextDir)); os.IsNotExist(err) {
197192
err = fmt.Errorf("provided context directory does not exist: %s", c.build.Spec.Source.ContextDir)
198-
c.build.Status.Phase = buildapi.BuildPhaseFailed
199-
c.build.Status.Reason = buildapi.StatusReasonInvalidContextDirectory
200-
c.build.Status.Message = buildapi.StatusMessageInvalidContextDirectory
193+
c.build.Status.Phase = buildapiv1.BuildPhaseFailed
194+
c.build.Status.Reason = buildapiv1.StatusReasonInvalidContextDirectory
195+
c.build.Status.Message = builderutil.StatusMessageInvalidContextDirectory
201196
return err
202197
}
203198
}
@@ -238,14 +233,14 @@ func (c *builderConfig) execute(b builder) error {
238233
type dockerBuilder struct{}
239234

240235
// Build starts a Docker build.
241-
func (dockerBuilder) Build(dockerClient bld.DockerClient, sock string, buildsClient buildinternalversion.BuildResourceInterface, build *buildapi.Build, cgLimits *s2iapi.CGroupLimits) error {
236+
func (dockerBuilder) Build(dockerClient bld.DockerClient, sock string, buildsClient buildclientv1.BuildInterface, build *buildapiv1.Build, cgLimits *s2iapi.CGroupLimits) error {
242237
return bld.NewDockerBuilder(dockerClient, buildsClient, build, cgLimits).Build()
243238
}
244239

245240
type s2iBuilder struct{}
246241

247242
// Build starts an S2I build.
248-
func (s2iBuilder) Build(dockerClient bld.DockerClient, sock string, buildsClient buildinternalversion.BuildResourceInterface, build *buildapi.Build, cgLimits *s2iapi.CGroupLimits) error {
243+
func (s2iBuilder) Build(dockerClient bld.DockerClient, sock string, buildsClient buildclientv1.BuildInterface, build *buildapiv1.Build, cgLimits *s2iapi.CGroupLimits) error {
249244
return bld.NewS2IBuilder(dockerClient, sock, buildsClient, build, cgLimits).Build()
250245
}
251246

pkg/build/builder/common.go

+18-17
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,16 @@ import (
2121
s2igit "github.com/openshift/source-to-image/pkg/scm/git"
2222
"github.com/openshift/source-to-image/pkg/util"
2323

24-
buildapi "github.com/openshift/origin/pkg/build/apis/build"
24+
buildapiv1 "github.com/openshift/api/build/v1"
2525
"github.com/openshift/origin/pkg/build/builder/timing"
26+
builderutil "github.com/openshift/origin/pkg/build/builder/util"
2627
"github.com/openshift/origin/pkg/build/builder/util/dockerfile"
2728
buildutil "github.com/openshift/origin/pkg/build/util"
2829
"github.com/openshift/origin/pkg/generate/git"
2930
imageapi "github.com/openshift/origin/pkg/image/apis/image"
3031
utilglog "github.com/openshift/origin/pkg/util/glog"
3132

32-
buildinternalversion "github.com/openshift/origin/pkg/build/generated/internalclientset/typed/build/internalversion"
33+
buildclientv1 "github.com/openshift/client-go/build/clientset/versioned/typed/build/v1"
3334
)
3435

3536
// glog is a placeholder until the builders pass an output stream down
@@ -62,7 +63,7 @@ type GitClient interface {
6263

6364
// buildInfo returns a slice of KeyValue pairs with build metadata to be
6465
// inserted into Docker images produced by build.
65-
func buildInfo(build *buildapi.Build, sourceInfo *git.SourceInfo) []KeyValue {
66+
func buildInfo(build *buildapiv1.Build, sourceInfo *git.SourceInfo) []KeyValue {
6667
kv := []KeyValue{
6768
{"OPENSHIFT_BUILD_NAME", build.Name},
6869
{"OPENSHIFT_BUILD_NAMESPACE", build.Namespace},
@@ -118,7 +119,7 @@ func containerName(strategyName, buildName, namespace, containerPurpose string)
118119
// postCommitSpec in a new ephemeral Docker container running the given image.
119120
// It returns an error if the hook cannot be run or returns a non-zero exit
120121
// code.
121-
func execPostCommitHook(ctx context.Context, client DockerClient, postCommitSpec buildapi.BuildPostCommitSpec, image, containerName string) error {
122+
func execPostCommitHook(ctx context.Context, client DockerClient, postCommitSpec buildapiv1.BuildPostCommitSpec, image, containerName string) error {
122123
command := postCommitSpec.Command
123124
args := postCommitSpec.Args
124125
script := postCommitSpec.Script
@@ -174,26 +175,26 @@ func execPostCommitHook(ctx context.Context, client DockerClient, postCommitSpec
174175
Stdout: true,
175176
Stderr: true,
176177
})
177-
timing.RecordNewStep(ctx, buildapi.StagePostCommit, buildapi.StepExecPostCommitHook, startTime, metav1.Now())
178+
timing.RecordNewStep(ctx, buildapiv1.StagePostCommit, buildapiv1.StepExecPostCommitHook, startTime, metav1.Now())
178179

179180
return err
180181
}
181182

182183
// GetSourceRevision returns a SourceRevision object either from the build (if it already had one)
183184
// or by creating one from the sourceInfo object passed in.
184-
func GetSourceRevision(build *buildapi.Build, sourceInfo *git.SourceInfo) *buildapi.SourceRevision {
185+
func GetSourceRevision(build *buildapiv1.Build, sourceInfo *git.SourceInfo) *buildapiv1.SourceRevision {
185186
if build.Spec.Revision != nil {
186187
return build.Spec.Revision
187188
}
188-
return &buildapi.SourceRevision{
189-
Git: &buildapi.GitSourceRevision{
189+
return &buildapiv1.SourceRevision{
190+
Git: &buildapiv1.GitSourceRevision{
190191
Commit: sourceInfo.CommitID,
191192
Message: sourceInfo.Message,
192-
Author: buildapi.SourceControlUser{
193+
Author: buildapiv1.SourceControlUser{
193194
Name: sourceInfo.AuthorName,
194195
Email: sourceInfo.AuthorEmail,
195196
},
196-
Committer: buildapi.SourceControlUser{
197+
Committer: buildapiv1.SourceControlUser{
197198
Name: sourceInfo.CommitterName,
198199
Email: sourceInfo.CommitterEmail,
199200
},
@@ -203,8 +204,8 @@ func GetSourceRevision(build *buildapi.Build, sourceInfo *git.SourceInfo) *build
203204

204205
// HandleBuildStatusUpdate handles updating the build status
205206
// retries occur on update conflict and unreachable api server
206-
func HandleBuildStatusUpdate(build *buildapi.Build, client buildinternalversion.BuildResourceInterface, sourceRev *buildapi.SourceRevision) {
207-
var latestBuild *buildapi.Build
207+
func HandleBuildStatusUpdate(build *buildapiv1.Build, client buildclientv1.BuildInterface, sourceRev *buildapiv1.SourceRevision) {
208+
var latestBuild *buildapiv1.Build
208209
var err error
209210

210211
updateBackoff := wait.Backoff{
@@ -231,7 +232,7 @@ func HandleBuildStatusUpdate(build *buildapi.Build, client buildinternalversion.
231232
latestBuild.Status.Reason = build.Status.Reason
232233
latestBuild.Status.Message = build.Status.Message
233234
latestBuild.Status.Output.To = build.Status.Output.To
234-
latestBuild.Status.Stages = buildapi.AppendStageAndStepInfo(latestBuild.Status.Stages, build.Status.Stages)
235+
latestBuild.Status.Stages = timing.AppendStageAndStepInfo(latestBuild.Status.Stages, build.Status.Stages)
235236

236237
_, err = client.UpdateDetails(latestBuild.Name, latestBuild)
237238

@@ -255,7 +256,7 @@ func HandleBuildStatusUpdate(build *buildapi.Build, client buildinternalversion.
255256

256257
// buildEnv converts the buildInfo output to a format that appendEnv can
257258
// consume.
258-
func buildEnv(build *buildapi.Build, sourceInfo *git.SourceInfo) []dockerfile.KeyValue {
259+
func buildEnv(build *buildapiv1.Build, sourceInfo *git.SourceInfo) []dockerfile.KeyValue {
259260
bi := buildInfo(build, sourceInfo)
260261
kv := make([]dockerfile.KeyValue, len(bi))
261262
for i, item := range bi {
@@ -282,15 +283,15 @@ func toS2ISourceInfo(sourceInfo *git.SourceInfo) *s2igit.SourceInfo {
282283

283284
// buildLabels returns a slice of KeyValue pairs in a format that appendLabel can
284285
// consume.
285-
func buildLabels(build *buildapi.Build, sourceInfo *git.SourceInfo) []dockerfile.KeyValue {
286+
func buildLabels(build *buildapiv1.Build, sourceInfo *git.SourceInfo) []dockerfile.KeyValue {
286287
labels := map[string]string{}
287288
if sourceInfo == nil {
288289
sourceInfo = &git.SourceInfo{}
289290
}
290291
if len(build.Spec.Source.ContextDir) > 0 {
291292
sourceInfo.ContextDir = build.Spec.Source.ContextDir
292293
}
293-
labels = util.GenerateLabelsFromSourceInfo(labels, toS2ISourceInfo(sourceInfo), buildapi.DefaultDockerLabelNamespace)
294+
labels = util.GenerateLabelsFromSourceInfo(labels, toS2ISourceInfo(sourceInfo), builderutil.DefaultDockerLabelNamespace)
294295
addBuildLabels(labels, build)
295296

296297
kv := make([]dockerfile.KeyValue, 0, len(labels)+len(build.Spec.Output.ImageLabels))
@@ -329,7 +330,7 @@ func readSourceInfo() (*git.SourceInfo, error) {
329330
// addBuildParameters checks if a Image is set to replace the default base image.
330331
// If that's the case then change the Dockerfile to make the build with the given image.
331332
// Also append the environment variables and labels in the Dockerfile.
332-
func addBuildParameters(dir string, build *buildapi.Build, sourceInfo *git.SourceInfo) error {
333+
func addBuildParameters(dir string, build *buildapiv1.Build, sourceInfo *git.SourceInfo) error {
333334
dockerfilePath := getDockerfilePath(dir, build)
334335
node, err := parseDockerfile(dockerfilePath)
335336
if err != nil {

0 commit comments

Comments
 (0)