Skip to content

Commit

Permalink
Make external builder helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
soltysh committed Aug 16, 2018
1 parent b33814d commit 7240c76
Show file tree
Hide file tree
Showing 16 changed files with 69 additions and 42 deletions.
2 changes: 1 addition & 1 deletion pkg/build/apis/build/v1/conversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func Convert_v1_BuildConfig_To_build_BuildConfig(in *v1.BuildConfig, out *newer.
// Strip off any default imagechange triggers where the buildconfig's
// "from" is not an ImageStreamTag, because those triggers
// will never be invoked.
imageRef := buildapihelpers.GetInputReference(out.Spec.Strategy)
imageRef := buildapihelpers.GetInputReferenceInternal(out.Spec.Strategy)
hasIST := imageRef != nil && imageRef.Kind == "ImageStreamTag"
for _, trigger := range out.Spec.Triggers {
if trigger.Type != newer.ImageChangeBuildTriggerType {
Expand Down
4 changes: 2 additions & 2 deletions pkg/build/apis/build/validation/validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func ValidateBuildUpdate(build *buildapi.Build, older *buildapi.Build) field.Err
allErrs = append(allErrs, validation.ValidateObjectMetaUpdate(&build.ObjectMeta, &older.ObjectMeta, field.NewPath("metadata"))...)
allErrs = append(allErrs, validation.ValidateObjectMeta(&build.ObjectMeta, true, validation.NameIsDNSSubdomain, field.NewPath("metadata"))...)

if buildutil.IsBuildComplete(older) && older.Status.Phase != build.Status.Phase {
if buildutil.IsBuildCompleteInternal(older) && older.Status.Phase != build.Status.Phase {
allErrs = append(allErrs, field.Invalid(field.NewPath("status", "phase"), build.Status.Phase, "phase cannot be updated from a terminal state"))
}

Expand Down Expand Up @@ -85,7 +85,7 @@ func ValidateBuildConfig(config *buildapi.BuildConfig) field.ErrorList {
fromRefs := map[string]struct{}{}
specPath := field.NewPath("spec")
triggersPath := specPath.Child("triggers")
buildFrom := buildapihelpers.GetInputReference(config.Spec.Strategy)
buildFrom := buildapihelpers.GetInputReferenceInternal(config.Spec.Strategy)
for i, trg := range config.Spec.Triggers {
allErrs = append(allErrs, validateTrigger(&trg, buildFrom, triggersPath.Index(i))...)
if trg.Type != buildapi.ImageChangeBuildTriggerType || trg.ImageChange == nil {
Expand Down
2 changes: 1 addition & 1 deletion pkg/build/apiserver/registry/build/strategy.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ func (detailsStrategy) PrepareForUpdate(ctx context.Context, obj, old runtime.Ob
// a completed phase.
phase := oldBuild.Status.Phase
stages := newBuild.Status.Stages
if buildutil.IsBuildComplete(newBuild) {
if buildutil.IsBuildCompleteInternal(newBuild) {
phase = newBuild.Status.Phase
}
revision := newBuild.Spec.Revision
Expand Down
19 changes: 18 additions & 1 deletion pkg/build/buildapihelpers/helpers.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package buildapihelpers

import (
corev1 "k8s.io/api/core/v1"
coreinternalapi "k8s.io/kubernetes/pkg/apis/core"

buildv1 "github.com/openshift/api/build/v1"
buildinternalapi "github.com/openshift/origin/pkg/build/apis/build"
)

Expand Down Expand Up @@ -36,9 +38,24 @@ func HasTriggerType(triggerType buildinternalapi.BuildTriggerType, bc *buildinte
return len(matches) > 0
}

// GetInputReferenceInternal returns the From ObjectReference associated with the
// BuildStrategy.
func GetInputReferenceInternal(strategy buildinternalapi.BuildStrategy) *coreinternalapi.ObjectReference {
switch {
case strategy.SourceStrategy != nil:
return &strategy.SourceStrategy.From
case strategy.DockerStrategy != nil:
return strategy.DockerStrategy.From
case strategy.CustomStrategy != nil:
return &strategy.CustomStrategy.From
default:
return nil
}
}

// GetInputReference returns the From ObjectReference associated with the
// BuildStrategy.
func GetInputReference(strategy buildinternalapi.BuildStrategy) *coreinternalapi.ObjectReference {
func GetInputReference(strategy buildv1.BuildStrategy) *corev1.ObjectReference {
switch {
case strategy.SourceStrategy != nil:
return &strategy.SourceStrategy.From
Expand Down
16 changes: 8 additions & 8 deletions pkg/build/controller/build/build_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,7 @@ func (bc *BuildController) handleBuild(build *buildapi.Build) error {

// If pipeline build, handle pruning.
if build.Spec.Strategy.JenkinsPipelineStrategy != nil {
if buildutil.IsBuildComplete(build) {
if buildutil.IsBuildCompleteInternal(build) {
if err := common.HandleBuildPruning(buildutil.ConfigNameForBuild(build), build.Namespace, bc.buildLister, bc.buildConfigGetter, bc.buildDeleter); err != nil {
utilruntime.HandleError(fmt.Errorf("failed to prune builds for %s/%s: %v", build.Namespace, build.Name, err))
}
Expand Down Expand Up @@ -363,7 +363,7 @@ func (bc *BuildController) handleBuild(build *buildapi.Build) error {
case build.Status.Phase == buildapi.BuildPhasePending,
build.Status.Phase == buildapi.BuildPhaseRunning:
update, err = bc.handleActiveBuild(build, pod)
case buildutil.IsBuildComplete(build):
case buildutil.IsBuildCompleteInternal(build):
update, err = bc.handleCompletedBuild(build, pod)
}
if update != nil && !update.isEmpty() {
Expand Down Expand Up @@ -395,7 +395,7 @@ func shouldIgnore(build *buildapi.Build) bool {
// state and its completion time or logsnippet is not set, then we should at least attempt to set its
// completion time and logsnippet if possible because the build pod may have put the build in
// this state and it would have not set the completion timestamp or logsnippet data.
if buildutil.IsBuildComplete(build) {
if buildutil.IsBuildCompleteInternal(build) {
switch build.Status.Phase {
case buildapi.BuildPhaseComplete:
if build.Status.CompletionTimestamp == nil {
Expand All @@ -415,7 +415,7 @@ func shouldIgnore(build *buildapi.Build) bool {

// shouldCancel returns true if a build is active and its cancellation flag is set
func shouldCancel(build *buildapi.Build) bool {
return !buildutil.IsBuildComplete(build) && build.Status.Cancelled
return !buildutil.IsBuildCompleteInternal(build) && build.Status.Cancelled
}

// cancelBuild deletes a build pod and returns an update to mark the build as cancelled
Expand Down Expand Up @@ -1085,7 +1085,7 @@ func (bc *BuildController) updateBuild(build *buildapi.Build, update *buildUpdat
}

// Update build completion timestamp if transitioning to a terminal phase
if buildutil.IsTerminalPhase(*update.phase) {
if buildutil.IsTerminalPhaseInternal(*update.phase) {
setBuildCompletionData(build, pod, update)
}
glog.V(4).Infof("Updating build %s -> %s%s", buildDesc(build), *update.phase, reasonText)
Expand Down Expand Up @@ -1114,7 +1114,7 @@ func (bc *BuildController) updateBuild(build *buildapi.Build, update *buildUpdat
buildapi.BuildPhaseFailed:
bc.recorder.Eventf(patchedBuild, kapi.EventTypeNormal, buildapi.BuildFailedEventReason, fmt.Sprintf(buildapi.BuildFailedEventMessage, patchedBuild.Namespace, patchedBuild.Name))
}
if buildutil.IsTerminalPhase(*update.phase) {
if buildutil.IsTerminalPhaseInternal(*update.phase) {
bc.handleBuildCompletion(patchedBuild)
}
}
Expand Down Expand Up @@ -1270,7 +1270,7 @@ func (bc *BuildController) buildDeleted(obj interface{}) {
}
}
// If the build was not in a complete state, poke the buildconfig to run the next build
if !buildutil.IsBuildComplete(build) {
if !buildutil.IsBuildCompleteInternal(build) {
bcName := buildutil.ConfigNameForBuild(build)
bc.enqueueBuildConfig(build.Namespace, bcName)
}
Expand Down Expand Up @@ -1375,7 +1375,7 @@ func isValidTransition(from, to buildapi.BuildPhase) bool {
}

switch {
case buildutil.IsTerminalPhase(from):
case buildutil.IsTerminalPhaseInternal(from):
return false
case from == buildapi.BuildPhasePending:
switch to {
Expand Down
4 changes: 2 additions & 2 deletions pkg/build/controller/build/build_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -758,7 +758,7 @@ func TestIsValidTransition(t *testing.T) {
}
for _, fromPhase := range phases {
for _, toPhase := range phases {
if buildutil.IsTerminalPhase(fromPhase) && fromPhase != toPhase {
if buildutil.IsTerminalPhaseInternal(fromPhase) && fromPhase != toPhase {
if isValidTransition(fromPhase, toPhase) {
t.Errorf("transition %v -> %v should be invalid", fromPhase, toPhase)
}
Expand Down Expand Up @@ -795,7 +795,7 @@ func TestIsTerminal(t *testing.T) {
buildapi.BuildPhaseCancelled: true,
}
for phase, expected := range tests {
if actual := buildutil.IsTerminalPhase(phase); actual != expected {
if actual := buildutil.IsTerminalPhaseInternal(phase); actual != expected {
t.Errorf("unexpected response for %s: %v", phase, actual)
}
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/build/controller/policy/serial_latest_only.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func (s *SerialLatestOnlyPolicy) cancelPreviousBuilds(build *buildapi.Build) []e
builds, err := buildutil.BuildConfigBuilds(s.BuildLister, build.Namespace, bcName, func(b *buildapi.Build) bool {
// Do not cancel the complete builds, builds that were already cancelled, or
// running builds.
if buildutil.IsBuildComplete(b) || b.Status.Phase == buildapi.BuildPhaseRunning {
if buildutil.IsBuildCompleteInternal(b) || b.Status.Phase == buildapi.BuildPhaseRunning {
return false
}

Expand Down
6 changes: 3 additions & 3 deletions pkg/build/generator/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ func findImageChangeTrigger(bc *buildapi.BuildConfig, ref *kapi.ObjectReference)
imageChange := trigger.ImageChange
triggerRef := imageChange.From
if triggerRef == nil {
triggerRef = buildapihelpers.GetInputReference(bc.Spec.Strategy)
triggerRef = buildapihelpers.GetInputReferenceInternal(bc.Spec.Strategy)
if triggerRef == nil || triggerRef.Kind != "ImageStreamTag" {
continue
}
Expand Down Expand Up @@ -356,7 +356,7 @@ func (g *BuildGenerator) updateImageTriggers(ctx context.Context, bc *buildapi.B

triggerImageRef := trigger.ImageChange.From
if triggerImageRef == nil {
triggerImageRef = buildapihelpers.GetInputReference(bc.Spec.Strategy)
triggerImageRef = buildapihelpers.GetInputReferenceInternal(bc.Spec.Strategy)
}
if triggerImageRef == nil {
glog.Warningf("Could not get ImageStream reference for default ImageChangeTrigger on BuildConfig %s/%s", bc.Namespace, bc.Name)
Expand Down Expand Up @@ -541,7 +541,7 @@ func (g *BuildGenerator) setBuildSourceImage(ctx context.Context, builderSecrets
var sourceImageSpec string
// if the imagesource matches the strategy from, and we have a trigger for the strategy from,
// use the imageid from the trigger rather than resolving it.
if strategyFrom := buildapihelpers.GetInputReference(bcCopy.Spec.Strategy); strategyFrom != nil &&
if strategyFrom := buildapihelpers.GetInputReferenceInternal(bcCopy.Spec.Strategy); strategyFrom != nil &&
reflect.DeepEqual(sourceImage.From, *strategyFrom) &&
strategyImageChangeTrigger != nil {
sourceImageSpec = strategyImageChangeTrigger.LastTriggeredImageID
Expand Down
2 changes: 1 addition & 1 deletion pkg/build/generator/generator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -449,7 +449,7 @@ func TestInstantiateWithImageTrigger(t *testing.T) {
if bc.Spec.Triggers[i].Type == buildapi.ImageChangeBuildTriggerType {
from := bc.Spec.Triggers[i].ImageChange.From
if from == nil {
from = buildapihelpers.GetInputReference(bc.Spec.Strategy)
from = buildapihelpers.GetInputReferenceInternal(bc.Spec.Strategy)
}
if bc.Spec.Triggers[i].ImageChange.LastTriggeredImageID != ("ref/" + from.Name) {
t.Errorf("%s: expected LastTriggeredImageID for trigger at %d (%+v) to be %s. Got: %s", tc.name, i, bc.Spec.Triggers[i].ImageChange.From, "ref/"+from.Name, bc.Spec.Triggers[i].ImageChange.LastTriggeredImageID)
Expand Down
25 changes: 21 additions & 4 deletions pkg/build/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"k8s.io/kubernetes/pkg/credentialprovider"
credentialprovidersecrets "k8s.io/kubernetes/pkg/credentialprovider/secrets"

buildv1 "github.com/openshift/api/build/v1"
buildapi "github.com/openshift/origin/pkg/build/apis/build"
"github.com/openshift/origin/pkg/build/buildapihelpers"
buildlister "github.com/openshift/origin/pkg/build/generated/listers/build/internalversion"
Expand All @@ -39,13 +40,13 @@ var (
proxyRegex = regexp.MustCompile("(?i)proxy")
)

// IsBuildComplete returns whether the provided build is complete or not
func IsBuildComplete(build *buildapi.Build) bool {
return IsTerminalPhase(build.Status.Phase)
// IsBuildCompleteInternal returns whether the provided build is complete or not
func IsBuildCompleteInternal(build *buildapi.Build) bool {
return IsTerminalPhaseInternal(build.Status.Phase)
}

// IsTerminalPhase returns true if the provided phase is terminal
func IsTerminalPhase(phase buildapi.BuildPhase) bool {
func IsTerminalPhaseInternal(phase buildapi.BuildPhase) bool {
switch phase {
case buildapi.BuildPhaseNew,
buildapi.BuildPhasePending,
Expand All @@ -55,6 +56,22 @@ func IsTerminalPhase(phase buildapi.BuildPhase) bool {
return true
}

// IsBuildComplete returns whether the provided build is complete or not
func IsBuildComplete(build *buildv1.Build) bool {
return IsTerminalPhase(build.Status.Phase)
}

// IsTerminalPhase returns true if the provided phase is terminal
func IsTerminalPhase(phase buildv1.BuildPhase) bool {
switch phase {
case buildv1.BuildPhaseNew,
buildv1.BuildPhasePending,
buildv1.BuildPhaseRunning:
return false
}
return true
}

// BuildNameForConfigVersion returns the name of the version-th build
// for the config that has the provided name.
func BuildNameForConfigVersion(name string, version int) string {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -948,7 +948,7 @@ func updateBuildConfigImages(bc *buildapi.BuildConfig, tagRetriever trigger.TagR
if p.From != nil {
from = p.From
} else {
from = buildapihelpers.GetInputReference(bc.Spec.Strategy)
from = buildapihelpers.GetInputReferenceInternal(bc.Spec.Strategy)
}
namespace := from.Namespace
if len(namespace) == 0 {
Expand Down
4 changes: 2 additions & 2 deletions pkg/image/trigger/buildconfigs/buildconfigs.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func calculateBuildConfigTriggers(bc *buildapi.BuildConfig) []triggerapi.ObjectF
from = t.ImageChange.From
fieldPath = "spec.triggers"
} else {
from = buildapihelpers.GetInputReference(bc.Spec.Strategy)
from = buildapihelpers.GetInputReferenceInternal(bc.Spec.Strategy)
fieldPath = "spec.strategy.*.from"
}
if from == nil || from.Kind != "ImageStreamTag" || len(from.Name) == 0 {
Expand Down Expand Up @@ -152,7 +152,7 @@ func (r *buildConfigReactor) ImageChanged(obj runtime.Object, tagRetriever trigg
if p.From != nil {
from = p.From
} else {
from = buildapihelpers.GetInputReference(bc.Spec.Strategy)
from = buildapihelpers.GetInputReferenceInternal(bc.Spec.Strategy)
}
namespace := from.Namespace
if len(namespace) == 0 {
Expand Down
9 changes: 1 addition & 8 deletions pkg/oc/cli/cancelbuild/cancelbuild.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ import (
"github.com/openshift/api/build"
buildv1 "github.com/openshift/api/build/v1"
buildtv1client "github.com/openshift/client-go/build/clientset/versioned/typed/build/v1"
buildapi "github.com/openshift/origin/pkg/build/apis/build"
buildapiv1 "github.com/openshift/origin/pkg/build/apis/build/v1"
buildclientinternal "github.com/openshift/origin/pkg/build/client"
buildclientv1 "github.com/openshift/origin/pkg/build/client/v1"
buildinternalclient "github.com/openshift/origin/pkg/build/generated/internalclientset"
Expand Down Expand Up @@ -212,12 +210,7 @@ func (o *CancelBuildOptions) RunCancelBuild() error {
}
}

internalBuildStatus := &buildapi.BuildStatus{}
if err := buildapiv1.Convert_v1_BuildStatus_To_build_BuildStatus(&build.Status, internalBuildStatus, nil); err != nil {
return err
}

if stateMatch && !buildutil.IsTerminalPhase(internalBuildStatus.Phase) {
if stateMatch && !buildutil.IsTerminalPhase(build.Phase) {
builds = append(builds, build)
}
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/oc/lib/newapp/cmd/newapp.go
Original file line number Diff line number Diff line change
Expand Up @@ -1192,7 +1192,7 @@ func (c *AppConfig) checkCircularReferences(objects app.Objects) error {
}

if bc, ok := obj.(*buildapi.BuildConfig); ok {
input := buildapihelpers.GetInputReference(bc.Spec.Strategy)
input := buildapihelpers.GetInputReferenceInternal(bc.Spec.Strategy)
output := bc.Spec.Output.To

if output == nil || input == nil {
Expand Down
4 changes: 2 additions & 2 deletions pkg/template/controller/readiness.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ import (
func checkBuildReadiness(obj runtime.Object) (bool, bool, error) {
b := obj.(*buildapi.Build)

ready := buildutil.IsTerminalPhase(b.Status.Phase) &&
ready := buildutil.IsTerminalPhaseInternal(b.Status.Phase) &&
b.Status.Phase == buildapi.BuildPhaseComplete

failed := buildutil.IsTerminalPhase(b.Status.Phase) &&
failed := buildutil.IsTerminalPhaseInternal(b.Status.Phase) &&
b.Status.Phase != buildapi.BuildPhaseComplete

return ready, failed, nil
Expand Down
8 changes: 4 additions & 4 deletions test/extended/builds/run_policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ var _ = g.Describe("[Feature:Builds][Slow] using build configuration runPolicy",
for {
event := <-buildWatch.ResultChan()
build := event.Object.(*buildapi.Build)
o.Expect(buildutil.IsBuildComplete(build)).Should(o.BeFalse())
o.Expect(buildutil.IsBuildCompleteInternal(build)).Should(o.BeFalse())
if build.Name == startedBuilds[0] && build.Status.Phase == buildapi.BuildPhaseRunning {
break
}
Expand All @@ -92,7 +92,7 @@ var _ = g.Describe("[Feature:Builds][Slow] using build configuration runPolicy",
event := <-buildWatch.ResultChan()
build := event.Object.(*buildapi.Build)
if build.Name == startedBuilds[0] {
if buildutil.IsBuildComplete(build) {
if buildutil.IsBuildCompleteInternal(build) {
break
}
continue
Expand All @@ -117,7 +117,7 @@ var _ = g.Describe("[Feature:Builds][Slow] using build configuration runPolicy",
counter++
}
// When the build failed or completed prematurely, fail the test
o.Expect(buildutil.IsBuildComplete(build)).Should(o.BeFalse())
o.Expect(buildutil.IsBuildCompleteInternal(build)).Should(o.BeFalse())
if counter == 2 {
break
}
Expand Down Expand Up @@ -403,7 +403,7 @@ var _ = g.Describe("[Feature:Builds][Slow] using build configuration runPolicy",
expectedRunningBuild = 2
break
}
o.Expect(buildutil.IsBuildComplete(build)).Should(o.BeFalse())
o.Expect(buildutil.IsBuildCompleteInternal(build)).Should(o.BeFalse())
}
}

Expand Down

0 comments on commit 7240c76

Please sign in to comment.