Skip to content

Commit

Permalink
remove build helpers from api packages
Browse files Browse the repository at this point in the history
  • Loading branch information
deads2k committed Jul 24, 2018
1 parent 6893918 commit 39c9dd8
Show file tree
Hide file tree
Showing 48 changed files with 340 additions and 238 deletions.
2 changes: 2 additions & 0 deletions hack/import-restrictions.json
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@
"allowedImportPackages": [
"vendor/k8s.io/kubernetes/pkg/apis/core",
"vendor/k8s.io/kubernetes/pkg/apis/core/v1",
"github.com/openshift/origin/pkg/build/buildapihelpers",
"github.com/openshift/origin/pkg/image/apis/image"
]
},
Expand Down Expand Up @@ -480,6 +481,7 @@
"github.com/openshift/origin/pkg/authorization/util",
"github.com/openshift/origin/pkg/build/apis/build",
"github.com/openshift/origin/pkg/build/apis/build/install",
"github.com/openshift/origin/pkg/build/buildapihelpers",
"github.com/openshift/origin/pkg/build/client",
"github.com/openshift/origin/pkg/build/client/internalversion",
"github.com/openshift/origin/pkg/build/util",
Expand Down
3 changes: 2 additions & 1 deletion pkg/build/apis/build/fields.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package build
import (
"fmt"

"github.com/openshift/origin/pkg/api/apihelpers"
"k8s.io/apimachinery/pkg/fields"
runtime "k8s.io/apimachinery/pkg/runtime"
)
Expand All @@ -13,7 +14,7 @@ func BuildFieldSelector(obj runtime.Object, fieldSet fields.Set) error {
return fmt.Errorf("%T not a Build", obj)
}
fieldSet["status"] = string(build.Status.Phase)
fieldSet["podName"] = GetBuildPodName(build)
fieldSet["podName"] = apihelpers.GetPodName(build.Name, "build")

return nil
}
92 changes: 0 additions & 92 deletions pkg/build/apis/build/helpers.go

This file was deleted.

13 changes: 0 additions & 13 deletions pkg/build/apis/build/util_test.go

This file was deleted.

3 changes: 2 additions & 1 deletion pkg/build/apis/build/v1/conversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

"github.com/openshift/api/build/v1"
newer "github.com/openshift/origin/pkg/build/apis/build"
"github.com/openshift/origin/pkg/build/buildapihelpers"
imageapi "github.com/openshift/origin/pkg/image/apis/image"
)

Expand All @@ -18,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 := newer.GetInputReference(out.Spec.Strategy)
imageRef := buildapihelpers.GetInputReference(out.Spec.Strategy)
hasIST := imageRef != nil && imageRef.Kind == "ImageStreamTag"
for _, trigger := range out.Spec.Triggers {
if trigger.Type != newer.ImageChangeBuildTriggerType {
Expand Down
5 changes: 3 additions & 2 deletions pkg/build/apis/build/validation/validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (

buildapiv1 "github.com/openshift/api/build/v1"
buildapi "github.com/openshift/origin/pkg/build/apis/build"
"github.com/openshift/origin/pkg/build/buildapihelpers"
"github.com/openshift/origin/pkg/build/buildscheme"
buildutil "github.com/openshift/origin/pkg/build/util"
imageapi "github.com/openshift/origin/pkg/image/apis/image"
Expand Down Expand Up @@ -84,7 +85,7 @@ func ValidateBuildConfig(config *buildapi.BuildConfig) field.ErrorList {
fromRefs := map[string]struct{}{}
specPath := field.NewPath("spec")
triggersPath := specPath.Child("triggers")
buildFrom := buildapi.GetInputReference(config.Spec.Strategy)
buildFrom := buildapihelpers.GetInputReference(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 Expand Up @@ -664,7 +665,7 @@ func ValidateBuildLogOptions(opts *buildapi.BuildLogOptions) field.ErrorList {
allErrs := field.ErrorList{}

// TODO: Replace by validating PodLogOptions via BuildLogOptions once it's bundled in
popts := buildapi.BuildToPodLogOptions(opts)
popts := buildapihelpers.BuildToPodLogOptions(opts)
if errs := validation.ValidatePodLogOptions(popts); len(errs) > 0 {
allErrs = append(allErrs, errs...)
}
Expand Down
52 changes: 52 additions & 0 deletions pkg/build/buildapihelpers/helpers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package buildapihelpers

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

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

// BuildToPodLogOptions builds a PodLogOptions object out of a BuildLogOptions.
// Currently BuildLogOptions.Container and BuildLogOptions.Previous aren't used
// so they won't be copied to PodLogOptions.
func BuildToPodLogOptions(opts *buildinternalapi.BuildLogOptions) *coreinternalapi.PodLogOptions {
return &coreinternalapi.PodLogOptions{
Follow: opts.Follow,
SinceSeconds: opts.SinceSeconds,
SinceTime: opts.SinceTime,
Timestamps: opts.Timestamps,
TailLines: opts.TailLines,
LimitBytes: opts.LimitBytes,
}
}

// FindTriggerPolicy retrieves the BuildTrigger(s) of a given type from a build configuration.
// Returns nil if no matches are found.
func FindTriggerPolicy(triggerType buildinternalapi.BuildTriggerType, config *buildinternalapi.BuildConfig) (buildTriggers []buildinternalapi.BuildTriggerPolicy) {
for _, specTrigger := range config.Spec.Triggers {
if specTrigger.Type == triggerType {
buildTriggers = append(buildTriggers, specTrigger)
}
}
return buildTriggers
}

func HasTriggerType(triggerType buildinternalapi.BuildTriggerType, bc *buildinternalapi.BuildConfig) bool {
matches := FindTriggerPolicy(triggerType, bc)
return len(matches) > 0
}

// GetInputReference returns the From ObjectReference associated with the
// BuildStrategy.
func GetInputReference(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
}
}
19 changes: 19 additions & 0 deletions pkg/build/buildapihelpers/sort.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package buildapihelpers

import buildinternalapi "github.com/openshift/origin/pkg/build/apis/build"

// BuildSliceByCreationTimestamp implements sort.Interface for []Build
// based on the CreationTimestamp field.
type BuildSliceByCreationTimestamp []buildinternalapi.Build

func (b BuildSliceByCreationTimestamp) Len() int {
return len(b)
}

func (b BuildSliceByCreationTimestamp) Less(i, j int) bool {
return b[i].CreationTimestamp.Before(&b[j].CreationTimestamp)
}

func (b BuildSliceByCreationTimestamp) Swap(i, j int) {
b[i], b[j] = b[j], b[i]
}
34 changes: 34 additions & 0 deletions pkg/build/buildapihelpers/sort_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package buildapihelpers

import (
"sort"
"testing"
"time"

buildinternalapi "github.com/openshift/origin/pkg/build/apis/build"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

func TestSortBuildSliceByCreationTimestamp(t *testing.T) {
present := metav1.Now()
past := metav1.NewTime(present.Add(-time.Minute))
builds := []buildinternalapi.Build{
{
ObjectMeta: metav1.ObjectMeta{
Name: "present",
CreationTimestamp: present,
},
},
{
ObjectMeta: metav1.ObjectMeta{
Name: "past",
CreationTimestamp: past,
},
},
}
sort.Sort(BuildSliceByCreationTimestamp(builds))
if [2]string{builds[0].Name, builds[1].Name} != [2]string{"past", "present"} {
t.Errorf("Unexpected sort order")
}
}
33 changes: 7 additions & 26 deletions pkg/build/apis/build/util.go → pkg/build/buildapihelpers/util.go
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
package build
package buildapihelpers

import (
"github.com/openshift/origin/pkg/api/apihelpers"
buildinternalapi "github.com/openshift/origin/pkg/build/apis/build"
"k8s.io/apimachinery/pkg/util/validation"
)

const (
// BuildPodSuffix is the suffix used to append to a build pod name given a build name
BuildPodSuffix = "build"
// buildPodSuffix is the suffix used to append to a build pod name given a build name
buildPodSuffix = "build"
)

// GetBuildPodName returns name of the build pod.
func GetBuildPodName(build *Build) string {
return apihelpers.GetPodName(build.Name, BuildPodSuffix)
func GetBuildPodName(build *buildinternalapi.Build) string {
return apihelpers.GetPodName(build.Name, buildPodSuffix)
}

func StrategyType(strategy BuildStrategy) string {
func StrategyType(strategy buildinternalapi.BuildStrategy) string {
switch {
case strategy.DockerStrategy != nil:
return "Docker"
Expand All @@ -29,26 +30,6 @@ func StrategyType(strategy BuildStrategy) string {
return ""
}

func SourceType(source BuildSource) string {
var sourceType string
if source.Git != nil {
sourceType = "Git"
}
if source.Dockerfile != nil {
if len(sourceType) != 0 {
sourceType = sourceType + ","
}
sourceType = sourceType + "Dockerfile"
}
if source.Binary != nil {
if len(sourceType) != 0 {
sourceType = sourceType + ","
}
sourceType = sourceType + "Binary"
}
return sourceType
}

// LabelValue returns a string to use as a value for the Build
// label in a pod. If the length of the string parameter exceeds
// the maximum label length, the value will be truncated.
Expand Down
15 changes: 15 additions & 0 deletions pkg/build/buildapihelpers/util_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package buildapihelpers

import (
"testing"

buildinternalapi "github.com/openshift/origin/pkg/build/apis/build"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

func TestGetBuildPodName(t *testing.T) {
if expected, actual := "mybuild-build", GetBuildPodName(&buildinternalapi.Build{ObjectMeta: metav1.ObjectMeta{Name: "mybuild"}}); expected != actual {
t.Errorf("Expected %s, got %s", expected, actual)
}
}
7 changes: 4 additions & 3 deletions pkg/build/controller/build/build_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"time"

"github.com/golang/glog"
"github.com/openshift/origin/pkg/build/buildapihelpers"
metrics "github.com/openshift/origin/pkg/build/metrics/prometheus"
"k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
Expand Down Expand Up @@ -342,7 +343,7 @@ func (bc *BuildController) handleBuild(build *buildapi.Build) error {

glog.V(4).Infof("Handling build %s", buildDesc(build))

pod, podErr := bc.podStore.Pods(build.Namespace).Get(buildapi.GetBuildPodName(build))
pod, podErr := bc.podStore.Pods(build.Namespace).Get(buildapihelpers.GetBuildPodName(build))

// Technically the only error that is returned from retrieving the pod is the
// NotFound error so this check should not be needed, but leaving here in case
Expand Down Expand Up @@ -421,7 +422,7 @@ func shouldCancel(build *buildapi.Build) bool {
func (bc *BuildController) cancelBuild(build *buildapi.Build) (*buildUpdate, error) {
glog.V(4).Infof("Cancelling build %s", buildDesc(build))

podName := buildapi.GetBuildPodName(build)
podName := buildapihelpers.GetBuildPodName(build)
err := bc.podClient.Pods(build.Namespace).Delete(podName, &metav1.DeleteOptions{})
if err != nil && !errors.IsNotFound(err) {
return nil, fmt.Errorf("could not delete build pod %s/%s to cancel build %s: %v", build.Namespace, podName, buildDesc(build), err)
Expand Down Expand Up @@ -1175,7 +1176,7 @@ func (bc *BuildController) patchBuild(build *buildapi.Build, update *buildUpdate
// It is called when a corresponding pod for a build is not found in the cache.
func (bc *BuildController) findMissingPod(build *buildapi.Build) *v1.Pod {
// Make one last attempt to fetch the pod using the REST client
pod, err := bc.podClient.Pods(build.Namespace).Get(buildapi.GetBuildPodName(build), metav1.GetOptions{})
pod, err := bc.podClient.Pods(build.Namespace).Get(buildapihelpers.GetBuildPodName(build), metav1.GetOptions{})
if err == nil {
glog.V(2).Infof("Found missing pod for build %s by using direct client.", buildDesc(build))
return pod
Expand Down
Loading

0 comments on commit 39c9dd8

Please sign in to comment.