diff --git a/hack/import-restrictions.json b/hack/import-restrictions.json index a3a22d013337..c873be8feee1 100644 --- a/hack/import-restrictions.json +++ b/hack/import-restrictions.json @@ -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" ] }, @@ -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", diff --git a/pkg/build/apis/build/fields.go b/pkg/build/apis/build/fields.go index 49ab7ba5e449..c74531e9e0f1 100644 --- a/pkg/build/apis/build/fields.go +++ b/pkg/build/apis/build/fields.go @@ -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" ) @@ -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 } diff --git a/pkg/build/apis/build/helpers.go b/pkg/build/apis/build/helpers.go deleted file mode 100644 index 6b07affa7b50..000000000000 --- a/pkg/build/apis/build/helpers.go +++ /dev/null @@ -1,92 +0,0 @@ -package build - -import ( - kapi "k8s.io/kubernetes/pkg/apis/core" -) - -// 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 *BuildLogOptions) *kapi.PodLogOptions { - return &kapi.PodLogOptions{ - Follow: opts.Follow, - SinceSeconds: opts.SinceSeconds, - SinceTime: opts.SinceTime, - Timestamps: opts.Timestamps, - TailLines: opts.TailLines, - LimitBytes: opts.LimitBytes, - } -} - -// PredicateFunc is testing an argument and decides does it meet some criteria or not. -// It can be used for filtering elements based on some conditions. -type PredicateFunc func(interface{}) bool - -// FilterBuilds returns array of builds that satisfies predicate function. -func FilterBuilds(builds []Build, predicate PredicateFunc) []Build { - if len(builds) == 0 { - return builds - } - - result := make([]Build, 0) - for _, build := range builds { - if predicate(build) { - result = append(result, build) - } - } - - return result -} - -// ByBuildConfigPredicate matches all builds that have build config annotation or label with specified value. -func ByBuildConfigPredicate(labelValue string) PredicateFunc { - return func(arg interface{}) bool { - return (hasBuildConfigAnnotation(arg.(Build), BuildConfigAnnotation, labelValue) || - hasBuildConfigLabel(arg.(Build), BuildConfigLabel, labelValue) || - hasBuildConfigLabel(arg.(Build), BuildConfigLabelDeprecated, labelValue)) - } -} - -func hasBuildConfigLabel(build Build, labelName, labelValue string) bool { - value, ok := build.Labels[labelName] - return ok && value == labelValue -} - -func hasBuildConfigAnnotation(build Build, annotationName, annotationValue string) bool { - if build.Annotations == nil { - return false - } - value, ok := build.Annotations[annotationName] - return ok && value == annotationValue -} - -// FindTriggerPolicy retrieves the BuildTrigger(s) of a given type from a build configuration. -// Returns nil if no matches are found. -func FindTriggerPolicy(triggerType BuildTriggerType, config *BuildConfig) (buildTriggers []BuildTriggerPolicy) { - for _, specTrigger := range config.Spec.Triggers { - if specTrigger.Type == triggerType { - buildTriggers = append(buildTriggers, specTrigger) - } - } - return buildTriggers -} - -func HasTriggerType(triggerType BuildTriggerType, bc *BuildConfig) bool { - matches := FindTriggerPolicy(triggerType, bc) - return len(matches) > 0 -} - -// GetInputReference returns the From ObjectReference associated with the -// BuildStrategy. -func GetInputReference(strategy BuildStrategy) *kapi.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 - } -} diff --git a/pkg/build/apis/build/util_test.go b/pkg/build/apis/build/util_test.go deleted file mode 100644 index 312049c30c4b..000000000000 --- a/pkg/build/apis/build/util_test.go +++ /dev/null @@ -1,13 +0,0 @@ -package build - -import ( - "testing" - - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" -) - -func TestGetBuildPodName(t *testing.T) { - if expected, actual := "mybuild-build", GetBuildPodName(&Build{ObjectMeta: metav1.ObjectMeta{Name: "mybuild"}}); expected != actual { - t.Errorf("Expected %s, got %s", expected, actual) - } -} diff --git a/pkg/build/apis/build/v1/conversion.go b/pkg/build/apis/build/v1/conversion.go index 1eb26eb6a66b..0440876f7b9d 100644 --- a/pkg/build/apis/build/v1/conversion.go +++ b/pkg/build/apis/build/v1/conversion.go @@ -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" ) @@ -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 { diff --git a/pkg/build/apis/build/validation/validation.go b/pkg/build/apis/build/validation/validation.go index b7ebee38e9e3..5c707efb9b52 100644 --- a/pkg/build/apis/build/validation/validation.go +++ b/pkg/build/apis/build/validation/validation.go @@ -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" @@ -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 { @@ -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...) } diff --git a/pkg/build/buildapihelpers/helpers.go b/pkg/build/buildapihelpers/helpers.go new file mode 100644 index 000000000000..8ea32aa80f38 --- /dev/null +++ b/pkg/build/buildapihelpers/helpers.go @@ -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 + } +} diff --git a/pkg/build/buildapihelpers/sort.go b/pkg/build/buildapihelpers/sort.go new file mode 100644 index 000000000000..a7f0700c6d76 --- /dev/null +++ b/pkg/build/buildapihelpers/sort.go @@ -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] +} diff --git a/pkg/build/buildapihelpers/sort_test.go b/pkg/build/buildapihelpers/sort_test.go new file mode 100644 index 000000000000..b0da319f6af9 --- /dev/null +++ b/pkg/build/buildapihelpers/sort_test.go @@ -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") + } +} diff --git a/pkg/build/apis/build/util.go b/pkg/build/buildapihelpers/util.go similarity index 54% rename from pkg/build/apis/build/util.go rename to pkg/build/buildapihelpers/util.go index 6e48aa6973bd..aeeb1302f684 100644 --- a/pkg/build/apis/build/util.go +++ b/pkg/build/buildapihelpers/util.go @@ -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" @@ -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. diff --git a/pkg/build/buildapihelpers/util_test.go b/pkg/build/buildapihelpers/util_test.go new file mode 100644 index 000000000000..45d1526958b5 --- /dev/null +++ b/pkg/build/buildapihelpers/util_test.go @@ -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) + } +} diff --git a/pkg/build/controller/build/build_controller.go b/pkg/build/controller/build/build_controller.go index 2d4dbb904f46..663b8b79f0a4 100644 --- a/pkg/build/controller/build/build_controller.go +++ b/pkg/build/controller/build/build_controller.go @@ -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" @@ -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 @@ -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) @@ -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 diff --git a/pkg/build/controller/build/build_controller_test.go b/pkg/build/controller/build/build_controller_test.go index ce3925f7f5b9..ec06d6789042 100644 --- a/pkg/build/controller/build/build_controller_test.go +++ b/pkg/build/controller/build/build_controller_test.go @@ -26,6 +26,7 @@ import ( buildapiv1 "github.com/openshift/api/build/v1" buildapi "github.com/openshift/origin/pkg/build/apis/build" "github.com/openshift/origin/pkg/build/apis/build/validation" + "github.com/openshift/origin/pkg/build/buildapihelpers" builddefaults "github.com/openshift/origin/pkg/build/controller/build/defaults" buildoverrides "github.com/openshift/origin/pkg/build/controller/build/overrides" "github.com/openshift/origin/pkg/build/controller/common" @@ -52,7 +53,7 @@ func TestHandleBuild(t *testing.T) { build := func(phase buildapi.BuildPhase) *buildapi.Build { b := dockerStrategy(mockBuild(phase, buildapi.BuildOutput{})) if phase != buildapi.BuildPhaseNew { - podName := buildapi.GetBuildPodName(b) + podName := buildapihelpers.GetBuildPodName(b) common.SetBuildPodNameAnnotation(b, podName) } return b @@ -433,7 +434,7 @@ func TestCreateBuildPod(t *testing.T) { t.Errorf("unexpected error: %v", err) return } - podName := buildapi.GetBuildPodName(build) + podName := buildapihelpers.GetBuildPodName(build) // Validate update expected := &buildUpdate{} expected.setPodNameAnnotation(podName) @@ -458,7 +459,7 @@ func TestCreateBuildPodWithImageStreamOutput(t *testing.T) { bc := newFakeBuildController(nil, imageClient, nil, nil) defer bc.stop() build := dockerStrategy(mockBuild(buildapi.BuildPhaseNew, buildapi.BuildOutput{To: imageStreamRef, PushSecret: &kapi.LocalObjectReference{}})) - podName := buildapi.GetBuildPodName(build) + podName := buildapihelpers.GetBuildPodName(build) update, err := bc.createBuildPod(build) if err != nil { @@ -570,7 +571,7 @@ func TestCreateBuildPodWithExistingRelatedPod(t *testing.T) { existingPod := &v1.Pod{ ObjectMeta: metav1.ObjectMeta{ - Name: buildapi.GetBuildPodName(build), + Name: buildapihelpers.GetBuildPodName(build), Namespace: build.Namespace, OwnerReferences: []metav1.OwnerReference{ { @@ -602,7 +603,7 @@ func TestCreateBuildPodWithExistingRelatedPod(t *testing.T) { expected.setPhase(buildapi.BuildPhasePending) expected.setReason("") expected.setMessage("") - expected.setPodNameAnnotation(buildapi.GetBuildPodName(build)) + expected.setPodNameAnnotation(buildapihelpers.GetBuildPodName(build)) validateUpdate(t, "create build pod with existing related pod error", expected, update) } @@ -611,7 +612,7 @@ func TestCreateBuildPodWithExistingUnrelatedPod(t *testing.T) { existingPod := &v1.Pod{ ObjectMeta: metav1.ObjectMeta{ - Name: buildapi.GetBuildPodName(build), + Name: buildapihelpers.GetBuildPodName(build), Namespace: build.Namespace, }, } @@ -1289,7 +1290,7 @@ func (f *fakeRunPolicy) Handles(buildapi.BuildRunPolicy) bool { func mockBuildPod(build *buildapi.Build) *v1.Pod { pod := &v1.Pod{} - pod.Name = buildapi.GetBuildPodName(build) + pod.Name = buildapihelpers.GetBuildPodName(build) pod.Namespace = build.Namespace pod.Annotations = map[string]string{} pod.Annotations[buildapi.BuildAnnotation] = build.Name diff --git a/pkg/build/controller/buildconfig/buildconfig_controller.go b/pkg/build/controller/buildconfig/buildconfig_controller.go index 5c5a7ee5545b..4b76ec737e5f 100644 --- a/pkg/build/controller/buildconfig/buildconfig_controller.go +++ b/pkg/build/controller/buildconfig/buildconfig_controller.go @@ -6,6 +6,7 @@ import ( "time" "github.com/golang/glog" + "github.com/openshift/origin/pkg/build/buildapihelpers" "github.com/openshift/origin/pkg/build/buildscheme" clientv1 "k8s.io/api/core/v1" kerrors "k8s.io/apimachinery/pkg/api/errors" @@ -103,7 +104,7 @@ func (c *BuildConfigController) handleBuildConfig(bc *buildapi.BuildConfig) erro utilruntime.HandleError(fmt.Errorf("failed to prune builds for %s/%s: %v", bc.Namespace, bc.Name, err)) } - hasChangeTrigger := buildapi.HasTriggerType(buildapi.ConfigChangeBuildTriggerType, bc) + hasChangeTrigger := buildapihelpers.HasTriggerType(buildapi.ConfigChangeBuildTriggerType, bc) if !hasChangeTrigger { return nil diff --git a/pkg/build/controller/strategy/custom.go b/pkg/build/controller/strategy/custom.go index a95ef9ac9228..a4285df6b8b6 100644 --- a/pkg/build/controller/strategy/custom.go +++ b/pkg/build/controller/strategy/custom.go @@ -17,6 +17,7 @@ import ( "github.com/openshift/origin/pkg/api/legacy" buildapi "github.com/openshift/origin/pkg/build/apis/build" buildv1helpers "github.com/openshift/origin/pkg/build/apis/build/v1" + "github.com/openshift/origin/pkg/build/buildapihelpers" buildutil "github.com/openshift/origin/pkg/build/util" ) @@ -91,7 +92,7 @@ func (bs *CustomBuildStrategy) CreateBuildPod(build *buildapi.Build) (*v1.Pod, e privileged := true pod := &v1.Pod{ ObjectMeta: metav1.ObjectMeta{ - Name: buildapi.GetBuildPodName(build), + Name: buildapihelpers.GetBuildPodName(build), Namespace: build.Namespace, Labels: getPodLabels(build), }, diff --git a/pkg/build/controller/strategy/custom_test.go b/pkg/build/controller/strategy/custom_test.go index 7cbb2c4c0f15..59b1b5d7a20c 100644 --- a/pkg/build/controller/strategy/custom_test.go +++ b/pkg/build/controller/strategy/custom_test.go @@ -18,6 +18,7 @@ import ( buildapiv1 "github.com/openshift/api/build/v1" buildapi "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/util" ) @@ -39,10 +40,10 @@ func TestCustomCreateBuildPod(t *testing.T) { t.Fatalf("Unexpected error: %v", err) } - if expected, actual := buildapi.GetBuildPodName(build), actual.ObjectMeta.Name; expected != actual { + if expected, actual := buildapihelpers.GetBuildPodName(build), actual.ObjectMeta.Name; expected != actual { t.Errorf("Expected %s, but got %s!", expected, actual) } - if !reflect.DeepEqual(map[string]string{buildapi.BuildLabel: buildapi.LabelValue(build.Name)}, actual.Labels) { + if !reflect.DeepEqual(map[string]string{buildapi.BuildLabel: buildapihelpers.LabelValue(build.Name)}, actual.Labels) { t.Errorf("Pod Labels does not match Build Labels!") } if !reflect.DeepEqual(nodeSelector, actual.Spec.NodeSelector) { diff --git a/pkg/build/controller/strategy/docker.go b/pkg/build/controller/strategy/docker.go index c658d45e57b6..a0703b453fc3 100644 --- a/pkg/build/controller/strategy/docker.go +++ b/pkg/build/controller/strategy/docker.go @@ -11,6 +11,7 @@ import ( buildv1 "github.com/openshift/api/build/v1" buildapi "github.com/openshift/origin/pkg/build/apis/build" buildinstall "github.com/openshift/origin/pkg/build/apis/build/install" + "github.com/openshift/origin/pkg/build/buildapihelpers" buildutil "github.com/openshift/origin/pkg/build/util" ) @@ -59,7 +60,7 @@ func (bs *DockerBuildStrategy) CreateBuildPod(build *buildapi.Build) (*v1.Pod, e pod := &v1.Pod{ ObjectMeta: metav1.ObjectMeta{ - Name: buildapi.GetBuildPodName(build), + Name: buildapihelpers.GetBuildPodName(build), Namespace: build.Namespace, Labels: getPodLabels(build), }, diff --git a/pkg/build/controller/strategy/docker_test.go b/pkg/build/controller/strategy/docker_test.go index 82be19b6dca2..412dcdea9b39 100644 --- a/pkg/build/controller/strategy/docker_test.go +++ b/pkg/build/controller/strategy/docker_test.go @@ -15,6 +15,7 @@ import ( buildapi "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/util" buildutil "github.com/openshift/origin/pkg/build/util" ) @@ -30,10 +31,10 @@ func TestDockerCreateBuildPod(t *testing.T) { t.Errorf("Unexpected error: %v", err) } - if expected, actual := buildapi.GetBuildPodName(build), actual.ObjectMeta.Name; expected != actual { + if expected, actual := buildapihelpers.GetBuildPodName(build), actual.ObjectMeta.Name; expected != actual { t.Errorf("Expected %s, but got %s!", expected, actual) } - if !reflect.DeepEqual(map[string]string{buildapi.BuildLabel: buildapi.LabelValue(build.Name)}, actual.Labels) { + if !reflect.DeepEqual(map[string]string{buildapi.BuildLabel: buildapihelpers.LabelValue(build.Name)}, actual.Labels) { t.Errorf("Pod Labels does not match Build Labels!") } if !reflect.DeepEqual(nodeSelector, actual.Spec.NodeSelector) { diff --git a/pkg/build/controller/strategy/sti.go b/pkg/build/controller/strategy/sti.go index 7d1117ea810c..a6ff094adf1a 100644 --- a/pkg/build/controller/strategy/sti.go +++ b/pkg/build/controller/strategy/sti.go @@ -11,6 +11,7 @@ import ( kapi "k8s.io/kubernetes/pkg/apis/core" buildapi "github.com/openshift/origin/pkg/build/apis/build" + "github.com/openshift/origin/pkg/build/buildapihelpers" buildutil "github.com/openshift/origin/pkg/build/util" "github.com/openshift/origin/pkg/security/apis/security" securityinternalversion "github.com/openshift/origin/pkg/security/generated/internalclientset/typed/security/internalversion" @@ -67,7 +68,7 @@ func (bs *SourceBuildStrategy) CreateBuildPod(build *buildapi.Build) (*v1.Pod, e privileged := true pod := &v1.Pod{ ObjectMeta: metav1.ObjectMeta{ - Name: buildapi.GetBuildPodName(build), + Name: buildapihelpers.GetBuildPodName(build), Namespace: build.Namespace, Labels: getPodLabels(build), }, diff --git a/pkg/build/controller/strategy/sti_test.go b/pkg/build/controller/strategy/sti_test.go index fe27b9c6079d..bc6e4cdc0b74 100644 --- a/pkg/build/controller/strategy/sti_test.go +++ b/pkg/build/controller/strategy/sti_test.go @@ -16,6 +16,7 @@ import ( buildapi "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/util" buildutil "github.com/openshift/origin/pkg/build/util" "github.com/openshift/origin/pkg/security/apis/security" @@ -62,10 +63,10 @@ func testSTICreateBuildPod(t *testing.T, rootAllowed bool) { t.Errorf("Unexpected error: %v", err) } - if expected, actual := buildapi.GetBuildPodName(build), actual.ObjectMeta.Name; expected != actual { + if expected, actual := buildapihelpers.GetBuildPodName(build), actual.ObjectMeta.Name; expected != actual { t.Errorf("Expected %s, but got %s!", expected, actual) } - if !reflect.DeepEqual(map[string]string{buildapi.BuildLabel: buildapi.LabelValue(build.Name)}, actual.Labels) { + if !reflect.DeepEqual(map[string]string{buildapi.BuildLabel: buildapihelpers.LabelValue(build.Name)}, actual.Labels) { t.Errorf("Pod Labels does not match Build Labels!") } if !reflect.DeepEqual(nodeSelector, actual.Spec.NodeSelector) { diff --git a/pkg/build/controller/strategy/util.go b/pkg/build/controller/strategy/util.go index 3cbcbbe79151..07638d285ddf 100644 --- a/pkg/build/controller/strategy/util.go +++ b/pkg/build/controller/strategy/util.go @@ -18,6 +18,7 @@ import ( buildapiv1 "github.com/openshift/api/build/v1" "github.com/openshift/origin/pkg/api/apihelpers" buildapi "github.com/openshift/origin/pkg/build/apis/build" + "github.com/openshift/origin/pkg/build/buildapihelpers" "github.com/openshift/origin/pkg/image/apis/image/reference" ) @@ -317,7 +318,7 @@ func setupAdditionalSecrets(pod *v1.Pod, container *v1.Container, secrets []buil // getPodLabels creates labels for the Build Pod func getPodLabels(build *buildapi.Build) map[string]string { - return map[string]string{buildapi.BuildLabel: buildapi.LabelValue(build.Name)} + return map[string]string{buildapi.BuildLabel: buildapihelpers.LabelValue(build.Name)} } func makeOwnerReference(build *buildapi.Build) metav1.OwnerReference { diff --git a/pkg/build/generator/generator.go b/pkg/build/generator/generator.go index da552e395b5f..bffdc35a3b65 100644 --- a/pkg/build/generator/generator.go +++ b/pkg/build/generator/generator.go @@ -23,6 +23,7 @@ import ( buildapiv1 "github.com/openshift/api/build/v1" "github.com/openshift/origin/pkg/api/apihelpers" buildapi "github.com/openshift/origin/pkg/build/apis/build" + "github.com/openshift/origin/pkg/build/buildapihelpers" buildclient "github.com/openshift/origin/pkg/build/generated/internalclientset/typed/build/internalversion" buildutil "github.com/openshift/origin/pkg/build/util" "github.com/openshift/origin/pkg/cmd/server/bootstrappolicy" @@ -153,7 +154,7 @@ func findImageChangeTrigger(bc *buildapi.BuildConfig, ref *kapi.ObjectReference) imageChange := trigger.ImageChange triggerRef := imageChange.From if triggerRef == nil { - triggerRef = buildapi.GetInputReference(bc.Spec.Strategy) + triggerRef = buildapihelpers.GetInputReference(bc.Spec.Strategy) if triggerRef == nil || triggerRef.Kind != "ImageStreamTag" { continue } @@ -355,7 +356,7 @@ func (g *BuildGenerator) updateImageTriggers(ctx context.Context, bc *buildapi.B triggerImageRef := trigger.ImageChange.From if triggerImageRef == nil { - triggerImageRef = buildapi.GetInputReference(bc.Spec.Strategy) + triggerImageRef = buildapihelpers.GetInputReference(bc.Spec.Strategy) } if triggerImageRef == nil { glog.Warningf("Could not get ImageStream reference for default ImageChangeTrigger on BuildConfig %s/%s", bc.Namespace, bc.Name) @@ -540,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 := buildapi.GetInputReference(bcCopy.Spec.Strategy); strategyFrom != nil && + if strategyFrom := buildapihelpers.GetInputReference(bcCopy.Spec.Strategy); strategyFrom != nil && reflect.DeepEqual(sourceImage.From, *strategyFrom) && strategyImageChangeTrigger != nil { sourceImageSpec = strategyImageChangeTrigger.LastTriggeredImageID @@ -912,8 +913,8 @@ func setBuildAnnotationAndLabel(bcCopy *buildapi.BuildConfig, build *buildapi.Bu if build.Labels == nil { build.Labels = make(map[string]string) } - build.Labels[buildapi.BuildConfigLabelDeprecated] = buildapi.LabelValue(bcCopy.Name) - build.Labels[buildapi.BuildConfigLabel] = buildapi.LabelValue(bcCopy.Name) + build.Labels[buildapi.BuildConfigLabelDeprecated] = buildapihelpers.LabelValue(bcCopy.Name) + build.Labels[buildapi.BuildConfigLabel] = buildapihelpers.LabelValue(bcCopy.Name) build.Labels[buildapi.BuildRunPolicyLabel] = string(bcCopy.Spec.RunPolicy) } diff --git a/pkg/build/generator/generator_test.go b/pkg/build/generator/generator_test.go index 4870f660c44b..4f687b503a0c 100644 --- a/pkg/build/generator/generator_test.go +++ b/pkg/build/generator/generator_test.go @@ -20,6 +20,7 @@ import ( "github.com/openshift/api/image" buildapi "github.com/openshift/origin/pkg/build/apis/build" "github.com/openshift/origin/pkg/build/apis/build/validation" + "github.com/openshift/origin/pkg/build/buildapihelpers" mocks "github.com/openshift/origin/pkg/build/generator/test" imageapi "github.com/openshift/origin/pkg/image/apis/image" ) @@ -448,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 = buildapi.GetInputReference(bc.Spec.Strategy) + from = buildapihelpers.GetInputReference(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) diff --git a/pkg/build/metrics/prometheus/metrics.go b/pkg/build/metrics/prometheus/metrics.go index b63da0e53507..022b37f88f38 100644 --- a/pkg/build/metrics/prometheus/metrics.go +++ b/pkg/build/metrics/prometheus/metrics.go @@ -7,6 +7,7 @@ import ( kselector "k8s.io/apimachinery/pkg/labels" buildapi "github.com/openshift/origin/pkg/build/apis/build" + "github.com/openshift/origin/pkg/build/buildapihelpers" internalversion "github.com/openshift/origin/pkg/build/generated/listers/build/internalversion" "github.com/prometheus/client_golang/prometheus" ) @@ -111,7 +112,7 @@ func addTimeGauge(ch chan<- prometheus.Metric, b *buildapi.Build, time *metav1.T func (bc *buildCollector) collectBuild(ch chan<- prometheus.Metric, b *buildapi.Build) (key collectKey) { r := string(b.Status.Reason) - s := buildapi.StrategyType(b.Spec.Strategy) + s := buildapihelpers.StrategyType(b.Spec.Strategy) key = collectKey{reason: r, strategy: s} switch b.Status.Phase { // remember, new and pending builds don't have a start time diff --git a/pkg/build/registry/buildconfiginstantiate/rest.go b/pkg/build/registry/buildconfiginstantiate/rest.go index 4d5eaf9f7dea..a6e5cf8e7faf 100644 --- a/pkg/build/registry/buildconfiginstantiate/rest.go +++ b/pkg/build/registry/buildconfiginstantiate/rest.go @@ -23,6 +23,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" buildstrategy "github.com/openshift/origin/pkg/build/controller/strategy" buildtypedclient "github.com/openshift/origin/pkg/build/generated/internalclientset/typed/build/internalversion" "github.com/openshift/origin/pkg/build/generator" @@ -254,7 +255,7 @@ func (h *binaryInstantiateHandler) handle(r io.Reader) (runtime.Object, error) { return nil, errors.NewBadRequest(fmt.Sprintf("cannot upload file to build %s with status %s", build.Name, latest.Status.Phase)) } - buildPodName := buildapi.GetBuildPodName(build) + buildPodName := buildapihelpers.GetBuildPodName(build) opts := &kapi.PodAttachOptions{ Stdin: true, Container: buildstrategy.GitCloneContainer, diff --git a/pkg/build/registry/buildlog/rest.go b/pkg/build/registry/buildlog/rest.go index aa4bccfc0616..a02578712268 100644 --- a/pkg/build/registry/buildlog/rest.go +++ b/pkg/build/registry/buildlog/rest.go @@ -24,6 +24,7 @@ import ( apiserverrest "github.com/openshift/origin/pkg/apiserver/rest" buildapi "github.com/openshift/origin/pkg/build/apis/build" "github.com/openshift/origin/pkg/build/apis/build/validation" + "github.com/openshift/origin/pkg/build/buildapihelpers" buildstrategy "github.com/openshift/origin/pkg/build/controller/strategy" buildtypedclient "github.com/openshift/origin/pkg/build/generated/internalclientset/typed/build/internalversion" "github.com/openshift/origin/pkg/build/registry" @@ -113,7 +114,7 @@ func (r *REST) Get(ctx context.Context, name string, opts runtime.Object) (runti return nil, errors.NewBadRequest(fmt.Sprintf("build %s is in an error state. %s", build.Name, buildutil.NoBuildLogsMessage)) } // The container should be the default build container, so setting it to blank - buildPodName := buildapi.GetBuildPodName(build) + buildPodName := buildapihelpers.GetBuildPodName(build) // if we can't at least get the build pod, we're not going to get very far, so // error out now. @@ -125,7 +126,7 @@ func (r *REST) Get(ctx context.Context, name string, opts runtime.Object) (runti // check for old style builds with a single container/no initcontainers // and handle them w/ the old logging code. if len(buildPod.Spec.InitContainers) == 0 { - logOpts := buildapi.BuildToPodLogOptions(buildLogOpts) + logOpts := buildapihelpers.BuildToPodLogOptions(buildLogOpts) return r.getSimpleLogsFn(build.Namespace, buildPodName, logOpts) } @@ -216,7 +217,7 @@ func (r *REST) Get(ctx context.Context, name string, opts runtime.Object) (runti } // get the container logstream for this init container - containerLogOpts := buildapi.BuildToPodLogOptions(buildLogOpts) + containerLogOpts := buildapihelpers.BuildToPodLogOptions(buildLogOpts) containerLogOpts.Container = status.Name // never "follow" logs for terminated containers, it causes latency in streaming the result // and there's no point since the log is complete already. @@ -281,7 +282,7 @@ func (r *REST) Get(ctx context.Context, name string, opts runtime.Object) (runti return } - containerLogOpts := buildapi.BuildToPodLogOptions(buildLogOpts) + containerLogOpts := buildapihelpers.BuildToPodLogOptions(buildLogOpts) containerLogOpts.Container = selectBuilderContainer(buildPod.Spec.Containers) if containerLogOpts.Container == "" { glog.Errorf("error: failed to select a container in build pod: %s/%s", build.Namespace, buildPodName) diff --git a/pkg/build/util/util.go b/pkg/build/util/util.go index eeb440d15f3a..caa76b00581b 100644 --- a/pkg/build/util/util.go +++ b/pkg/build/util/util.go @@ -17,6 +17,7 @@ import ( credentialprovidersecrets "k8s.io/kubernetes/pkg/credentialprovider/secrets" 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" ) @@ -63,13 +64,7 @@ func BuildNameForConfigVersion(name string, version int) string { // BuildConfigSelector returns a label Selector which can be used to find all // builds for a BuildConfig. func BuildConfigSelector(name string) labels.Selector { - return labels.Set{buildapi.BuildConfigLabel: buildapi.LabelValue(name)}.AsSelector() -} - -// BuildConfigSelectorDeprecated returns a label Selector which can be used to find -// all builds for a BuildConfig that use the deprecated labels. -func BuildConfigSelectorDeprecated(name string) labels.Selector { - return labels.Set{buildapi.BuildConfigLabelDeprecated: name}.AsSelector() + return labels.Set{buildapi.BuildConfigLabel: buildapihelpers.LabelValue(name)}.AsSelector() } type buildFilter func(*buildapi.Build) bool diff --git a/pkg/build/webhook/bitbucket/bitbucket.go b/pkg/build/webhook/bitbucket/bitbucket.go index 1741de081799..bdb546b40582 100644 --- a/pkg/build/webhook/bitbucket/bitbucket.go +++ b/pkg/build/webhook/bitbucket/bitbucket.go @@ -13,6 +13,7 @@ import ( "github.com/golang/glog" buildapi "github.com/openshift/origin/pkg/build/apis/build" + "github.com/openshift/origin/pkg/build/buildapihelpers" "github.com/openshift/origin/pkg/build/webhook" ) @@ -107,7 +108,7 @@ func (p *WebHookPlugin) Extract(buildCfg *buildapi.BuildConfig, trigger *buildap // GetTriggers retrieves the WebHookTriggers for this webhook type (if any) func (p *WebHookPlugin) GetTriggers(buildConfig *buildapi.BuildConfig) ([]*buildapi.WebHookTrigger, error) { - triggers := buildapi.FindTriggerPolicy(buildapi.BitbucketWebHookBuildTriggerType, buildConfig) + triggers := buildapihelpers.FindTriggerPolicy(buildapi.BitbucketWebHookBuildTriggerType, buildConfig) webhookTriggers := []*buildapi.WebHookTrigger{} for _, trigger := range triggers { if trigger.BitbucketWebHook != nil { diff --git a/pkg/build/webhook/generic/generic.go b/pkg/build/webhook/generic/generic.go index bfe19b73c3a2..0591b3480e5b 100644 --- a/pkg/build/webhook/generic/generic.go +++ b/pkg/build/webhook/generic/generic.go @@ -14,6 +14,7 @@ import ( kapi "k8s.io/kubernetes/pkg/apis/core" buildapi "github.com/openshift/origin/pkg/build/apis/build" + "github.com/openshift/origin/pkg/build/buildapihelpers" "github.com/openshift/origin/pkg/build/webhook" ) @@ -109,7 +110,7 @@ func (p *WebHookPlugin) Extract(buildCfg *buildapi.BuildConfig, trigger *buildap // GetTriggers retrieves the WebHookTriggers for this webhook type (if any) func (p *WebHookPlugin) GetTriggers(buildConfig *buildapi.BuildConfig) ([]*buildapi.WebHookTrigger, error) { - triggers := buildapi.FindTriggerPolicy(buildapi.GenericWebHookBuildTriggerType, buildConfig) + triggers := buildapihelpers.FindTriggerPolicy(buildapi.GenericWebHookBuildTriggerType, buildConfig) webhookTriggers := []*buildapi.WebHookTrigger{} for _, trigger := range triggers { if trigger.GenericWebHook != nil { diff --git a/pkg/build/webhook/github/github.go b/pkg/build/webhook/github/github.go index e55f694aae7b..d820e3c5027b 100644 --- a/pkg/build/webhook/github/github.go +++ b/pkg/build/webhook/github/github.go @@ -12,6 +12,7 @@ import ( "github.com/golang/glog" buildapi "github.com/openshift/origin/pkg/build/apis/build" + "github.com/openshift/origin/pkg/build/buildapihelpers" "github.com/openshift/origin/pkg/build/webhook" ) @@ -75,7 +76,7 @@ func (p *WebHookPlugin) Extract(buildCfg *buildapi.BuildConfig, trigger *buildap // GetTriggers retrieves the WebHookTriggers for this webhook type (if any) func (p *WebHookPlugin) GetTriggers(buildConfig *buildapi.BuildConfig) ([]*buildapi.WebHookTrigger, error) { - triggers := buildapi.FindTriggerPolicy(buildapi.GitHubWebHookBuildTriggerType, buildConfig) + triggers := buildapihelpers.FindTriggerPolicy(buildapi.GitHubWebHookBuildTriggerType, buildConfig) webhookTriggers := []*buildapi.WebHookTrigger{} for _, trigger := range triggers { if trigger.GitHubWebHook != nil { diff --git a/pkg/build/webhook/gitlab/gitlab.go b/pkg/build/webhook/gitlab/gitlab.go index 43d2b7938746..a74e3688e6d8 100644 --- a/pkg/build/webhook/gitlab/gitlab.go +++ b/pkg/build/webhook/gitlab/gitlab.go @@ -12,6 +12,7 @@ import ( "github.com/golang/glog" buildapi "github.com/openshift/origin/pkg/build/apis/build" + "github.com/openshift/origin/pkg/build/buildapihelpers" "github.com/openshift/origin/pkg/build/webhook" ) @@ -76,7 +77,7 @@ func (p *WebHookPlugin) Extract(buildCfg *buildapi.BuildConfig, trigger *buildap // GetTriggers retrieves the WebHookTriggers for this webhook type (if any) func (p *WebHookPlugin) GetTriggers(buildConfig *buildapi.BuildConfig) ([]*buildapi.WebHookTrigger, error) { - triggers := buildapi.FindTriggerPolicy(buildapi.GitLabWebHookBuildTriggerType, buildConfig) + triggers := buildapihelpers.FindTriggerPolicy(buildapi.GitLabWebHookBuildTriggerType, buildConfig) webhookTriggers := []*buildapi.WebHookTrigger{} for _, trigger := range triggers { if trigger.GitLabWebHook != nil { diff --git a/pkg/image/controller/trigger/image_trigger_controller_test.go b/pkg/image/controller/trigger/image_trigger_controller_test.go index 85b02f3d22a7..b03d91f9af61 100644 --- a/pkg/image/controller/trigger/image_trigger_controller_test.go +++ b/pkg/image/controller/trigger/image_trigger_controller_test.go @@ -27,6 +27,7 @@ import ( appsapi "github.com/openshift/origin/pkg/apps/apis/apps" buildapi "github.com/openshift/origin/pkg/build/apis/build" + "github.com/openshift/origin/pkg/build/buildapihelpers" buildgenerator "github.com/openshift/origin/pkg/build/generator" "github.com/openshift/origin/pkg/cmd/server/bootstrappolicy" imageapi "github.com/openshift/origin/pkg/image/apis/image" @@ -946,7 +947,7 @@ func updateBuildConfigImages(bc *buildapi.BuildConfig, tagRetriever trigger.TagR if p.From != nil { from = p.From } else { - from = buildapi.GetInputReference(bc.Spec.Strategy) + from = buildapihelpers.GetInputReference(bc.Spec.Strategy) } namespace := from.Namespace if len(namespace) == 0 { diff --git a/pkg/image/trigger/buildconfigs/buildconfigs.go b/pkg/image/trigger/buildconfigs/buildconfigs.go index 7276544eac10..99d63d66db8e 100644 --- a/pkg/image/trigger/buildconfigs/buildconfigs.go +++ b/pkg/image/trigger/buildconfigs/buildconfigs.go @@ -17,6 +17,7 @@ import ( "github.com/golang/glog" buildapi "github.com/openshift/origin/pkg/build/apis/build" + "github.com/openshift/origin/pkg/build/buildapihelpers" triggerapi "github.com/openshift/origin/pkg/image/apis/image/v1/trigger" "github.com/openshift/origin/pkg/image/trigger" ) @@ -37,7 +38,7 @@ func calculateBuildConfigTriggers(bc *buildapi.BuildConfig) []triggerapi.ObjectF from = t.ImageChange.From fieldPath = "spec.triggers" } else { - from = buildapi.GetInputReference(bc.Spec.Strategy) + from = buildapihelpers.GetInputReference(bc.Spec.Strategy) fieldPath = "spec.strategy.*.from" } if from == nil || from.Kind != "ImageStreamTag" || len(from.Name) == 0 { @@ -151,7 +152,7 @@ func (r *buildConfigReactor) ImageChanged(obj runtime.Object, tagRetriever trigg if p.From != nil { from = p.From } else { - from = buildapi.GetInputReference(bc.Spec.Strategy) + from = buildapihelpers.GetInputReference(bc.Spec.Strategy) } namespace := from.Namespace if len(namespace) == 0 { diff --git a/pkg/oc/cli/admin/prune/builds/resolvers.go b/pkg/oc/cli/admin/prune/builds/resolvers.go index 4b895a969569..a2275d8f40bf 100644 --- a/pkg/oc/cli/admin/prune/builds/resolvers.go +++ b/pkg/oc/cli/admin/prune/builds/resolvers.go @@ -6,6 +6,7 @@ import ( "k8s.io/apimachinery/pkg/util/sets" buildapi "github.com/openshift/origin/pkg/build/apis/build" + "github.com/openshift/origin/pkg/oc/lib/buildapihelpers" ) // Resolver knows how to resolve the set of candidate objects to prune @@ -113,8 +114,8 @@ func (o *perBuildConfigResolver) Resolve() ([]*buildapi.Build, error) { failedBuilds = append(failedBuilds, build) } } - sort.Sort(sort.Reverse(buildapi.BuildPtrSliceByCreationTimestamp(completeBuilds))) - sort.Sort(sort.Reverse(buildapi.BuildPtrSliceByCreationTimestamp(failedBuilds))) + sort.Sort(sort.Reverse(buildapihelpers.BuildPtrSliceByCreationTimestamp(completeBuilds))) + sort.Sort(sort.Reverse(buildapihelpers.BuildPtrSliceByCreationTimestamp(failedBuilds))) if o.keepComplete >= 0 && o.keepComplete < len(completeBuilds) { prunableBuilds = append(prunableBuilds, completeBuilds[o.keepComplete:]...) diff --git a/pkg/oc/cli/admin/prune/builds/resolvers_test.go b/pkg/oc/cli/admin/prune/builds/resolvers_test.go index 46a4b92c2e91..663ded3ae283 100644 --- a/pkg/oc/cli/admin/prune/builds/resolvers_test.go +++ b/pkg/oc/cli/admin/prune/builds/resolvers_test.go @@ -10,6 +10,7 @@ import ( "k8s.io/apimachinery/pkg/util/sets" buildapi "github.com/openshift/origin/pkg/build/apis/build" + "github.com/openshift/origin/pkg/oc/lib/buildapihelpers" ) type mockResolver struct { @@ -153,8 +154,8 @@ func TestPerBuildConfigResolver(t *testing.T) { failedBuilds = append(failedBuilds, build) } } - sort.Sort(sort.Reverse(buildapi.BuildPtrSliceByCreationTimestamp(completeBuilds))) - sort.Sort(sort.Reverse(buildapi.BuildPtrSliceByCreationTimestamp(failedBuilds))) + sort.Sort(sort.Reverse(buildapihelpers.BuildPtrSliceByCreationTimestamp(completeBuilds))) + sort.Sort(sort.Reverse(buildapihelpers.BuildPtrSliceByCreationTimestamp(failedBuilds))) var purgeComplete, purgeFailed []*buildapi.Build if keep >= 0 && keep < len(completeBuilds) { purgeComplete = completeBuilds[keep:] diff --git a/pkg/oc/cli/admin/prune/imageprune/prune.go b/pkg/oc/cli/admin/prune/imageprune/prune.go index 2da374ee32d9..c16bfa2238bb 100644 --- a/pkg/oc/cli/admin/prune/imageprune/prune.go +++ b/pkg/oc/cli/admin/prune/imageprune/prune.go @@ -29,6 +29,7 @@ import ( appsapi "github.com/openshift/origin/pkg/apps/apis/apps" buildapi "github.com/openshift/origin/pkg/build/apis/build" + "github.com/openshift/origin/pkg/build/buildapihelpers" imageapi "github.com/openshift/origin/pkg/image/apis/image" imageclient "github.com/openshift/origin/pkg/image/generated/internalclientset/typed/image/internalversion" appsgraph "github.com/openshift/origin/pkg/oc/lib/graph/appsgraph/nodes" @@ -747,7 +748,7 @@ func (p *pruner) resolveISTagName(g genericgraph.Graph, referrer *kapi.ObjectRef // to the image specified by strategy.from, as long as the image is managed by // OpenShift. func (p *pruner) addBuildStrategyImageReferencesToGraph(referrer *kapi.ObjectReference, strategy buildapi.BuildStrategy, predecessor gonum.Node) []error { - from := buildapi.GetInputReference(strategy) + from := buildapihelpers.GetInputReference(strategy) if from == nil { glog.V(4).Infof("Unable to determine 'from' reference - skipping") return nil diff --git a/pkg/oc/lib/buildapihelpers/helpers.go b/pkg/oc/lib/buildapihelpers/helpers.go new file mode 100644 index 000000000000..2f681dcb0a91 --- /dev/null +++ b/pkg/oc/lib/buildapihelpers/helpers.go @@ -0,0 +1,47 @@ +package buildapihelpers + +import ( + buildinternalapi "github.com/openshift/origin/pkg/build/apis/build" +) + +// PredicateFunc is testing an argument and decides does it meet some criteria or not. +// It can be used for filtering elements based on some conditions. +type PredicateFunc func(interface{}) bool + +// FilterBuilds returns array of builds that satisfies predicate function. +func FilterBuilds(builds []buildinternalapi.Build, predicate PredicateFunc) []buildinternalapi.Build { + if len(builds) == 0 { + return builds + } + + result := make([]buildinternalapi.Build, 0) + for _, build := range builds { + if predicate(build) { + result = append(result, build) + } + } + + return result +} + +// ByBuildConfigPredicate matches all builds that have build config annotation or label with specified value. +func ByBuildConfigPredicate(labelValue string) PredicateFunc { + return func(arg interface{}) bool { + return hasBuildConfigAnnotation(arg.(buildinternalapi.Build), buildinternalapi.BuildConfigAnnotation, labelValue) || + hasBuildConfigLabel(arg.(buildinternalapi.Build), buildinternalapi.BuildConfigLabel, labelValue) || + hasBuildConfigLabel(arg.(buildinternalapi.Build), buildinternalapi.BuildConfigLabelDeprecated, labelValue) + } +} + +func hasBuildConfigLabel(build buildinternalapi.Build, labelName, labelValue string) bool { + value, ok := build.Labels[labelName] + return ok && value == labelValue +} + +func hasBuildConfigAnnotation(build buildinternalapi.Build, annotationName, annotationValue string) bool { + if build.Annotations == nil { + return false + } + value, ok := build.Annotations[annotationName] + return ok && value == annotationValue +} diff --git a/pkg/build/apis/build/helpers_test.go b/pkg/oc/lib/buildapihelpers/helpers_test.go similarity index 74% rename from pkg/build/apis/build/helpers_test.go rename to pkg/oc/lib/buildapihelpers/helpers_test.go index 339d6377017b..b540bf42e259 100644 --- a/pkg/build/apis/build/helpers_test.go +++ b/pkg/oc/lib/buildapihelpers/helpers_test.go @@ -1,21 +1,23 @@ -package build +package buildapihelpers import ( "reflect" "testing" + buildinternalapi "github.com/openshift/origin/pkg/build/apis/build" + "strings" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" ) func TestFilterBuilds_withEmptyArray(t *testing.T) { - actual := FilterBuilds([]Build{}, nil) + actual := FilterBuilds([]buildinternalapi.Build{}, nil) assertThatArrayIsEmpty(t, actual) } func TestFilterBuilds_withAllElementsAccepted(t *testing.T) { - expected := []Build{ + expected := []buildinternalapi.Build{ { ObjectMeta: metav1.ObjectMeta{ Name: "build1-abc", @@ -37,7 +39,7 @@ func TestFilterBuilds_withAllElementsAccepted(t *testing.T) { } func TestFilterBuilds_withFilteredElements(t *testing.T) { - input := []Build{ + input := []buildinternalapi.Build{ { ObjectMeta: metav1.ObjectMeta{ Name: "skip1-abc", @@ -55,10 +57,10 @@ func TestFilterBuilds_withFilteredElements(t *testing.T) { }, } - expected := []Build{input[1]} + expected := []buildinternalapi.Build{input[1]} skipByNamePrefixPredicate := func(arg interface{}) bool { - return !strings.HasPrefix(arg.(Build).Name, "skip") + return !strings.HasPrefix(arg.(buildinternalapi.Build).Name, "skip") } actual := FilterBuilds(input, skipByNamePrefixPredicate) @@ -66,11 +68,11 @@ func TestFilterBuilds_withFilteredElements(t *testing.T) { } func TestByBuildConfigPredicate_withBuildConfigAnnotation(t *testing.T) { - input := []Build{ + input := []buildinternalapi.Build{ { ObjectMeta: metav1.ObjectMeta{ Name: "build1-abc", - Annotations: map[string]string{BuildConfigAnnotation: "foo"}, + Annotations: map[string]string{buildinternalapi.BuildConfigAnnotation: "foo"}, }, }, { @@ -81,7 +83,7 @@ func TestByBuildConfigPredicate_withBuildConfigAnnotation(t *testing.T) { }, } - expected := []Build{input[0]} + expected := []buildinternalapi.Build{input[0]} actual := FilterBuilds(input, ByBuildConfigPredicate("foo")) assertThatArraysAreEquals(t, actual, expected) @@ -91,11 +93,11 @@ func TestByBuildConfigPredicate_withBuildConfigAnnotation(t *testing.T) { } func TestByBuildConfigPredicate_withBuildConfigLabel(t *testing.T) { - input := []Build{ + input := []buildinternalapi.Build{ { ObjectMeta: metav1.ObjectMeta{ Name: "build1-abc", - Labels: map[string]string{BuildConfigLabel: "foo"}, + Labels: map[string]string{buildinternalapi.BuildConfigLabel: "foo"}, }, }, { @@ -106,7 +108,7 @@ func TestByBuildConfigPredicate_withBuildConfigLabel(t *testing.T) { }, } - expected := []Build{input[0]} + expected := []buildinternalapi.Build{input[0]} actual := FilterBuilds(input, ByBuildConfigPredicate("foo")) assertThatArraysAreEquals(t, actual, expected) @@ -116,11 +118,11 @@ func TestByBuildConfigPredicate_withBuildConfigLabel(t *testing.T) { } func TestByBuildConfigPredicate_withBuildConfigLabelDeprecated(t *testing.T) { - input := []Build{ + input := []buildinternalapi.Build{ { ObjectMeta: metav1.ObjectMeta{ Name: "build1-abc", - Labels: map[string]string{BuildConfigLabelDeprecated: "foo"}, + Labels: map[string]string{buildinternalapi.BuildConfigLabelDeprecated: "foo"}, }, }, { @@ -131,7 +133,7 @@ func TestByBuildConfigPredicate_withBuildConfigLabelDeprecated(t *testing.T) { }, } - expected := []Build{input[0]} + expected := []buildinternalapi.Build{input[0]} actual := FilterBuilds(input, ByBuildConfigPredicate("foo")) assertThatArraysAreEquals(t, actual, expected) @@ -141,11 +143,11 @@ func TestByBuildConfigPredicate_withBuildConfigLabelDeprecated(t *testing.T) { } func TestByBuildConfigPredicate_withBothBuildConfigLabels(t *testing.T) { - input := []Build{ + input := []buildinternalapi.Build{ { ObjectMeta: metav1.ObjectMeta{ Name: "build1-abc", - Labels: map[string]string{BuildConfigLabel: "foo"}, + Labels: map[string]string{buildinternalapi.BuildConfigLabel: "foo"}, }, }, { @@ -157,12 +159,12 @@ func TestByBuildConfigPredicate_withBothBuildConfigLabels(t *testing.T) { { ObjectMeta: metav1.ObjectMeta{ Name: "build3-abc", - Labels: map[string]string{BuildConfigLabelDeprecated: "foo"}, + Labels: map[string]string{buildinternalapi.BuildConfigLabelDeprecated: "foo"}, }, }, } - expected := []Build{input[0], input[2]} + expected := []buildinternalapi.Build{input[0], input[2]} actual := FilterBuilds(input, ByBuildConfigPredicate("foo")) assertThatArraysAreEquals(t, actual, expected) @@ -172,7 +174,7 @@ func TestByBuildConfigPredicate_withBothBuildConfigLabels(t *testing.T) { } func TestByBuildConfigPredicate_withoutBuildConfigLabels(t *testing.T) { - input := []Build{ + input := []buildinternalapi.Build{ { ObjectMeta: metav1.ObjectMeta{ Name: "build1-abc", @@ -185,13 +187,13 @@ func TestByBuildConfigPredicate_withoutBuildConfigLabels(t *testing.T) { assertThatArrayIsEmpty(t, actual) } -func assertThatArraysAreEquals(t *testing.T, expected, actual []Build) { +func assertThatArraysAreEquals(t *testing.T, expected, actual []buildinternalapi.Build) { if !reflect.DeepEqual(expected, actual) { t.Errorf("Expected: %v\ngot: %v", expected, actual) } } -func assertThatArrayIsEmpty(t *testing.T, array []Build) { +func assertThatArrayIsEmpty(t *testing.T, array []buildinternalapi.Build) { if len(array) != 0 { t.Errorf("expected empty array, got %v", array) } diff --git a/pkg/build/apis/build/sort.go b/pkg/oc/lib/buildapihelpers/sort.go similarity index 78% rename from pkg/build/apis/build/sort.go rename to pkg/oc/lib/buildapihelpers/sort.go index 1abc4a5a4237..04e3a287781d 100644 --- a/pkg/build/apis/build/sort.go +++ b/pkg/oc/lib/buildapihelpers/sort.go @@ -1,8 +1,10 @@ -package build +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 []Build +type BuildSliceByCreationTimestamp []buildinternalapi.Build func (b BuildSliceByCreationTimestamp) Len() int { return len(b) @@ -18,7 +20,7 @@ func (b BuildSliceByCreationTimestamp) Swap(i, j int) { // BuildPtrSliceByCreationTimestamp implements sort.Interface for []*Build // based on the CreationTimestamp field. -type BuildPtrSliceByCreationTimestamp []*Build +type BuildPtrSliceByCreationTimestamp []*buildinternalapi.Build func (b BuildPtrSliceByCreationTimestamp) Len() int { return len(b) diff --git a/pkg/build/apis/build/sort_test.go b/pkg/oc/lib/buildapihelpers/sort_test.go similarity index 87% rename from pkg/build/apis/build/sort_test.go rename to pkg/oc/lib/buildapihelpers/sort_test.go index 3b454a751578..ca7b3ea29b40 100644 --- a/pkg/build/apis/build/sort_test.go +++ b/pkg/oc/lib/buildapihelpers/sort_test.go @@ -1,17 +1,19 @@ -package build +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 := []Build{ + builds := []buildinternalapi.Build{ { ObjectMeta: metav1.ObjectMeta{ Name: "present", @@ -34,7 +36,7 @@ func TestSortBuildSliceByCreationTimestamp(t *testing.T) { func TestSortBuildPtrSliceByCreationTimestamp(t *testing.T) { present := metav1.Now() past := metav1.NewTime(present.Add(-time.Minute)) - builds := []*Build{ + builds := []*buildinternalapi.Build{ { ObjectMeta: metav1.ObjectMeta{ Name: "present", diff --git a/pkg/oc/lib/describe/describer.go b/pkg/oc/lib/describe/describer.go index 3abcc19f2ad5..e4230273889a 100644 --- a/pkg/oc/lib/describe/describer.go +++ b/pkg/oc/lib/describe/describer.go @@ -43,11 +43,13 @@ import ( authorizationapi "github.com/openshift/origin/pkg/authorization/apis/authorization" oauthorizationclient "github.com/openshift/origin/pkg/authorization/generated/internalclientset/typed/authorization/internalversion" buildapi "github.com/openshift/origin/pkg/build/apis/build" + "github.com/openshift/origin/pkg/build/buildapihelpers" buildclient "github.com/openshift/origin/pkg/build/generated/internalclientset/typed/build/internalversion" imageapi "github.com/openshift/origin/pkg/image/apis/image" imageclient "github.com/openshift/origin/pkg/image/generated/internalclientset/typed/image/internalversion" onetworkclient "github.com/openshift/origin/pkg/network/generated/internalclientset/typed/network/internalversion" oauthclient "github.com/openshift/origin/pkg/oauth/generated/internalclientset/typed/oauth/internalversion" + ocbuildapihelpers "github.com/openshift/origin/pkg/oc/lib/buildapihelpers" projectapi "github.com/openshift/origin/pkg/project/apis/project" projectclient "github.com/openshift/origin/pkg/project/generated/internalclientset/typed/project/internalversion" quotaapi "github.com/openshift/origin/pkg/quota/apis/quota" @@ -185,7 +187,7 @@ func (d *BuildDescriber) Describe(namespace, name string, settings kprinters.Des events = &kapi.EventList{} } // get also pod events and merge it all into one list for describe - if pod, err := d.kubeClient.Core().Pods(namespace).Get(buildapi.GetBuildPodName(build), metav1.GetOptions{}); err == nil { + if pod, err := d.kubeClient.Core().Pods(namespace).Get(buildapihelpers.GetBuildPodName(build), metav1.GetOptions{}); err == nil { if podEvents, _ := d.kubeClient.Core().Events(namespace).Search(legacyscheme.Scheme, pod); podEvents != nil { events.Items = append(events.Items, podEvents.Items...) } @@ -219,7 +221,7 @@ func (d *BuildDescriber) Describe(namespace, name string, settings kprinters.Des if build.Status.Config != nil { formatString(out, "Build Config", build.Status.Config.Name) } - formatString(out, "Build Pod", buildapi.GetBuildPodName(build)) + formatString(out, "Build Pod", buildapihelpers.GetBuildPodName(build)) if build.Status.Output.To != nil && len(build.Status.Output.To.ImageDigest) > 0 { formatString(out, "Image Digest", build.Status.Output.To.ImageDigest) @@ -279,7 +281,7 @@ func nameAndNamespace(ns, name string) string { } func describeCommonSpec(p buildapi.CommonSpec, out *tabwriter.Writer) { - formatString(out, "\nStrategy", buildapi.StrategyType(p.Strategy)) + formatString(out, "\nStrategy", buildapihelpers.StrategyType(p.Strategy)) noneType := true if p.Source.Git != nil { noneType = false @@ -535,7 +537,7 @@ func (d *BuildConfigDescriber) Describe(namespace, name string, settings kprinte if err != nil { return "", err } - buildList.Items = buildapi.FilterBuilds(buildList.Items, buildapi.ByBuildConfigPredicate(name)) + buildList.Items = ocbuildapihelpers.FilterBuilds(buildList.Items, ocbuildapihelpers.ByBuildConfigPredicate(name)) return tabbedString(func(out *tabwriter.Writer) error { formatMeta(out, buildConfig.ObjectMeta) @@ -552,7 +554,7 @@ func (d *BuildConfigDescriber) Describe(namespace, name string, settings kprinte fmt.Fprintf(out, "\nBuild\tStatus\tDuration\tCreation Time\n") builds := buildList.Items - sort.Sort(sort.Reverse(buildapi.BuildSliceByCreationTimestamp(builds))) + sort.Sort(sort.Reverse(buildapihelpers.BuildSliceByCreationTimestamp(builds))) for i, build := range builds { fmt.Fprintf(out, "%s \t%s \t%v \t%v\n", diff --git a/pkg/oc/lib/graph/buildgraph/edges.go b/pkg/oc/lib/graph/buildgraph/edges.go index 9f9174adaac5..8e9bcea16be8 100644 --- a/pkg/oc/lib/graph/buildgraph/edges.go +++ b/pkg/oc/lib/graph/buildgraph/edges.go @@ -5,6 +5,7 @@ import ( kapi "k8s.io/kubernetes/pkg/apis/core" buildapi "github.com/openshift/origin/pkg/build/apis/build" + "github.com/openshift/origin/pkg/build/buildapihelpers" imageapi "github.com/openshift/origin/pkg/image/apis/image" buildgraph "github.com/openshift/origin/pkg/oc/lib/graph/buildgraph/nodes" osgraph "github.com/openshift/origin/pkg/oc/lib/graph/genericgraph" @@ -93,7 +94,7 @@ func AddInputEdges(g osgraph.MutableUniqueGraph, node *buildgraph.BuildConfigNod if in := buildgraph.EnsureSourceRepositoryNode(g, node.BuildConfig.Spec.Source); in != nil { g.AddEdge(in, node, BuildInputEdgeKind) } - inputImage := buildapi.GetInputReference(node.BuildConfig.Spec.Strategy) + inputImage := buildapihelpers.GetInputReference(node.BuildConfig.Spec.Strategy) if input := imageRefNode(g, inputImage, node.BuildConfig); input != nil { g.AddEdge(input, node, BuildInputImageEdgeKind) } @@ -107,7 +108,7 @@ func AddTriggerEdges(g osgraph.MutableUniqueGraph, node *buildgraph.BuildConfigN } from := trigger.ImageChange.From if trigger.ImageChange.From == nil { - from = buildapi.GetInputReference(node.BuildConfig.Spec.Strategy) + from = buildapihelpers.GetInputReference(node.BuildConfig.Spec.Strategy) } triggerNode := imageRefNode(g, from, node.BuildConfig) g.AddEdge(triggerNode, node, BuildTriggerImageEdgeKind) diff --git a/pkg/oc/lib/newapp/cmd/newapp.go b/pkg/oc/lib/newapp/cmd/newapp.go index 08de42ceda66..4c10b8f08dbd 100644 --- a/pkg/oc/lib/newapp/cmd/newapp.go +++ b/pkg/oc/lib/newapp/cmd/newapp.go @@ -27,6 +27,7 @@ import ( ometa "github.com/openshift/origin/pkg/api/meta" authapi "github.com/openshift/origin/pkg/authorization/apis/authorization" buildapi "github.com/openshift/origin/pkg/build/apis/build" + "github.com/openshift/origin/pkg/build/buildapihelpers" imageapi "github.com/openshift/origin/pkg/image/apis/image" imageclient "github.com/openshift/origin/pkg/image/generated/internalclientset/typed/image/internalversion" dockerregistry "github.com/openshift/origin/pkg/image/importer/dockerv1client" @@ -1191,7 +1192,7 @@ func (c *AppConfig) checkCircularReferences(objects app.Objects) error { } if bc, ok := obj.(*buildapi.BuildConfig); ok { - input := buildapi.GetInputReference(bc.Spec.Strategy) + input := buildapihelpers.GetInputReference(bc.Spec.Strategy) output := bc.Spec.Output.To if output == nil || input == nil { diff --git a/pkg/oc/originpolymorphichelpers/logsforobject.go b/pkg/oc/originpolymorphichelpers/logsforobject.go index 1fc0ac04c2b1..a977b6cb5612 100644 --- a/pkg/oc/originpolymorphichelpers/logsforobject.go +++ b/pkg/oc/originpolymorphichelpers/logsforobject.go @@ -19,6 +19,7 @@ import ( buildmanualclient "github.com/openshift/origin/pkg/build/client/internalversion" buildclientinternal "github.com/openshift/origin/pkg/build/generated/internalclientset" buildutil "github.com/openshift/origin/pkg/build/util" + ocbuildapihelpers "github.com/openshift/origin/pkg/oc/lib/buildapihelpers" ) func NewLogsForObjectFn(delegate polymorphichelpers.LogsForObjectFunc) polymorphichelpers.LogsForObjectFunc { @@ -66,7 +67,7 @@ func NewLogsForObjectFn(delegate polymorphichelpers.LogsForObjectFunc) polymorph if err != nil { return nil, err } - builds.Items = buildapi.FilterBuilds(builds.Items, buildapi.ByBuildConfigPredicate(t.Name)) + builds.Items = ocbuildapihelpers.FilterBuilds(builds.Items, ocbuildapihelpers.ByBuildConfigPredicate(t.Name)) if len(builds.Items) == 0 { return nil, fmt.Errorf("no builds found for %q", t.Name) } @@ -75,7 +76,7 @@ func NewLogsForObjectFn(delegate polymorphichelpers.LogsForObjectFunc) polymorph desired := buildutil.BuildNameForConfigVersion(t.Name, int(*bopts.Version)) return logClient.Logs(desired, *bopts), nil } - sort.Sort(sort.Reverse(buildapi.BuildSliceByCreationTimestamp(builds.Items))) + sort.Sort(sort.Reverse(ocbuildapihelpers.BuildSliceByCreationTimestamp(builds.Items))) return logClient.Logs(builds.Items[0].Name, *bopts), nil default: diff --git a/pkg/printers/internalversion/printer.go b/pkg/printers/internalversion/printer.go index 6eedd6d3a4b6..8933fc81f938 100644 --- a/pkg/printers/internalversion/printer.go +++ b/pkg/printers/internalversion/printer.go @@ -20,6 +20,7 @@ import ( appsapi "github.com/openshift/origin/pkg/apps/apis/apps" authorizationapi "github.com/openshift/origin/pkg/authorization/apis/authorization" buildapi "github.com/openshift/origin/pkg/build/apis/build" + "github.com/openshift/origin/pkg/build/buildapihelpers" imageapi "github.com/openshift/origin/pkg/image/apis/image" networkapi "github.com/openshift/origin/pkg/network/apis/network" oauthapi "github.com/openshift/origin/pkg/oauth/apis/oauth" @@ -276,7 +277,7 @@ func printBuild(build *buildapi.Build, w io.Writer, opts kprinters.PrintOptions) if len(build.Status.Reason) > 0 { status = fmt.Sprintf("%s (%s)", status, build.Status.Reason) } - if _, err := fmt.Fprintf(w, "%s\t%s\t%s\t%s\t%s\t%s", name, buildapi.StrategyType(build.Spec.Strategy), from, status, created, duration); err != nil { + if _, err := fmt.Fprintf(w, "%s\t%s\t%s\t%s\t%s\t%s", name, buildapihelpers.StrategyType(build.Spec.Strategy), from, status, created, duration); err != nil { return err } if err := appendItemLabels(build.Labels, w, opts.ColumnLabels, opts.ShowLabels); err != nil { @@ -306,11 +307,31 @@ func describeSourceShort(spec buildapi.CommonSpec) string { from = fmt.Sprintf("%s@%s", from, rev) } default: - from = buildapi.SourceType(source) + from = buildSourceType(source) } return from } +func buildSourceType(source buildapi.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 +} + var nonCommitRev = regexp.MustCompile("[^a-fA-F0-9]") func describeSourceGitRevision(spec buildapi.CommonSpec) string { @@ -330,7 +351,7 @@ func describeSourceGitRevision(spec buildapi.CommonSpec) string { func printBuildList(buildList *buildapi.BuildList, w io.Writer, opts kprinters.PrintOptions) error { builds := buildList.Items - sort.Sort(buildapi.BuildSliceByCreationTimestamp(builds)) + sort.Sort(buildapihelpers.BuildSliceByCreationTimestamp(builds)) for _, build := range builds { if err := printBuild(&build, w, opts); err != nil { return err @@ -349,7 +370,7 @@ func printBuildConfig(bc *buildapi.BuildConfig, w io.Writer, opts kprinters.Prin return err } } - _, err := fmt.Fprintf(w, "%s\t%v\t%s\t%d\n", name, buildapi.StrategyType(bc.Spec.Strategy), bc.Spec.Strategy.CustomStrategy.From.Name, bc.Status.LastVersion) + _, err := fmt.Fprintf(w, "%s\t%v\t%s\t%d\n", name, buildapihelpers.StrategyType(bc.Spec.Strategy), bc.Spec.Strategy.CustomStrategy.From.Name, bc.Status.LastVersion) return err } if opts.WithNamespace { @@ -357,7 +378,7 @@ func printBuildConfig(bc *buildapi.BuildConfig, w io.Writer, opts kprinters.Prin return err } } - if _, err := fmt.Fprintf(w, "%s\t%v\t%s\t%d", name, buildapi.StrategyType(bc.Spec.Strategy), from, bc.Status.LastVersion); err != nil { + if _, err := fmt.Fprintf(w, "%s\t%v\t%s\t%d", name, buildapihelpers.StrategyType(bc.Spec.Strategy), from, bc.Status.LastVersion); err != nil { return err } if err := appendItemLabels(bc.Labels, w, opts.ColumnLabels, opts.ShowLabels); err != nil { diff --git a/test/common/build/controllers.go b/test/common/build/controllers.go index cfaa67ab1bff..55117f452e5e 100644 --- a/test/common/build/controllers.go +++ b/test/common/build/controllers.go @@ -17,6 +17,7 @@ import ( kclientset "k8s.io/kubernetes/pkg/client/clientset_generated/internalclientset" buildapi "github.com/openshift/origin/pkg/build/apis/build" + "github.com/openshift/origin/pkg/build/buildapihelpers" buildtypedclient "github.com/openshift/origin/pkg/build/generated/internalclientset/typed/build/internalversion" imageapi "github.com/openshift/origin/pkg/image/apis/image" imagetypedclient "github.com/openshift/origin/pkg/image/generated/internalclientset/typed/image/internalversion" @@ -129,7 +130,7 @@ func RunBuildControllerTest(t testingT, buildClient buildtypedclient.BuildsGette }() // Watch build pods as they are created - podWatch, err := kClientset.Core().Pods(ns).Watch(metav1.ListOptions{FieldSelector: fields.OneTermEqualSelector("metadata.name", buildapi.GetBuildPodName(b)).String()}) + podWatch, err := kClientset.Core().Pods(ns).Watch(metav1.ListOptions{FieldSelector: fields.OneTermEqualSelector("metadata.name", buildapihelpers.GetBuildPodName(b)).String()}) if err != nil { t.Fatal(err) } @@ -219,7 +220,7 @@ func RunBuildControllerPodSyncTest(t testingT, buildClient buildtypedclient.Buil } // Watch build pod for transition to pending - podWatch, err := kClient.Core().Pods(ns).Watch(metav1.ListOptions{FieldSelector: fields.OneTermEqualSelector("metadata.name", buildapi.GetBuildPodName(b)).String()}) + podWatch, err := kClient.Core().Pods(ns).Watch(metav1.ListOptions{FieldSelector: fields.OneTermEqualSelector("metadata.name", buildapihelpers.GetBuildPodName(b)).String()}) if err != nil { t.Fatal(err) } @@ -579,7 +580,7 @@ func RunBuildDeleteTest(t testingT, clusterAdminClient buildtypedclient.BuildsGe t.Fatalf("expected watch event type %s, got %s", e, a) } pod := event.Object.(*kapi.Pod) - if expected := buildapi.GetBuildPodName(newBuild); pod.Name != expected { + if expected := buildapihelpers.GetBuildPodName(newBuild); pod.Name != expected { t.Fatalf("Expected pod %s to be deleted, but pod %s was deleted", expected, pod.Name) } @@ -661,7 +662,7 @@ func RunBuildRunningPodDeleteTest(t testingT, clusterAdminClient buildtypedclien t.Fatalf("expected build status to be marked pending, but was marked %s", newBuild.Status.Phase) } - clusterAdminKubeClientset.Core().Pods(testutil.Namespace()).Delete(buildapi.GetBuildPodName(newBuild), metav1.NewDeleteOptions(0)) + clusterAdminKubeClientset.Core().Pods(testutil.Namespace()).Delete(buildapihelpers.GetBuildPodName(newBuild), metav1.NewDeleteOptions(0)) event = waitForWatch(t, "build updated to error", buildWatch) if e, a := watchapi.Modified, event.Type; e != a { t.Fatalf("expected watch event type %s, got %s", e, a) @@ -755,7 +756,7 @@ func RunBuildCompletePodDeleteTest(t testingT, clusterAdminClient buildtypedclie t.Fatalf("expected build status to be marked complete, but was marked %s", newBuild.Status.Phase) } - clusterAdminKubeClientset.Core().Pods(testutil.Namespace()).Delete(buildapi.GetBuildPodName(newBuild), metav1.NewDeleteOptions(0)) + clusterAdminKubeClientset.Core().Pods(testutil.Namespace()).Delete(buildapihelpers.GetBuildPodName(newBuild), metav1.NewDeleteOptions(0)) time.Sleep(10 * time.Second) newBuild, err = clusterAdminClient.Builds(testutil.Namespace()).Get(newBuild.Name, metav1.GetOptions{}) if err != nil { diff --git a/test/extended/builds/completiondeadlineseconds.go b/test/extended/builds/completiondeadlineseconds.go index 8983b40550f7..1ed951f4fe74 100644 --- a/test/extended/builds/completiondeadlineseconds.go +++ b/test/extended/builds/completiondeadlineseconds.go @@ -8,6 +8,7 @@ import ( o "github.com/onsi/gomega" buildapi "github.com/openshift/origin/pkg/build/apis/build" + "github.com/openshift/origin/pkg/build/buildapihelpers" exutil "github.com/openshift/origin/test/extended/util" ) @@ -56,7 +57,7 @@ var _ = g.Describe("[Feature:Builds][Slow] builds should have deadlines", func() o.Expect(br.Build.Status.Phase).Should(o.BeEquivalentTo(buildapi.BuildPhaseFailed)) // the build should have failed g.By("verifying the build pod status") - pod, err := oc.KubeClient().Core().Pods(oc.Namespace()).Get(buildapi.GetBuildPodName(br.Build), metav1.GetOptions{}) + pod, err := oc.KubeClient().Core().Pods(oc.Namespace()).Get(buildapihelpers.GetBuildPodName(br.Build), metav1.GetOptions{}) o.Expect(err).NotTo(o.HaveOccurred()) o.Expect(pod.Status.Phase).Should(o.BeEquivalentTo(kapi.PodFailed)) o.Expect(pod.Status.Reason).Should(o.ContainSubstring("DeadlineExceeded")) @@ -80,7 +81,7 @@ var _ = g.Describe("[Feature:Builds][Slow] builds should have deadlines", func() o.Expect(br.Build.Status.Phase).Should(o.BeEquivalentTo(buildapi.BuildPhaseFailed)) // the build should have failed g.By("verifying the build pod status") - pod, err := oc.KubeClient().Core().Pods(oc.Namespace()).Get(buildapi.GetBuildPodName(br.Build), metav1.GetOptions{}) + pod, err := oc.KubeClient().Core().Pods(oc.Namespace()).Get(buildapihelpers.GetBuildPodName(br.Build), metav1.GetOptions{}) o.Expect(err).NotTo(o.HaveOccurred()) o.Expect(pod.Status.Phase).Should(o.BeEquivalentTo(kapi.PodFailed)) o.Expect(pod.Status.Reason).Should(o.ContainSubstring("DeadlineExceeded")) diff --git a/test/integration/buildpod_admission_test.go b/test/integration/buildpod_admission_test.go index c86b115a4f2b..e172d39708c1 100644 --- a/test/integration/buildpod_admission_test.go +++ b/test/integration/buildpod_admission_test.go @@ -16,6 +16,7 @@ import ( buildtestutil "github.com/openshift/origin/pkg/build/admission/testutil" buildapi "github.com/openshift/origin/pkg/build/apis/build" + "github.com/openshift/origin/pkg/build/buildapihelpers" defaultsapi "github.com/openshift/origin/pkg/build/controller/build/apis/defaults" overridesapi "github.com/openshift/origin/pkg/build/controller/build/apis/overrides" buildclient "github.com/openshift/origin/pkg/build/generated/internalclientset" @@ -242,7 +243,7 @@ func runBuildPodAdmissionTest(t *testing.T, client buildclient.Interface, kclien watchOpt := metav1.ListOptions{ FieldSelector: fields.OneTermEqualSelector( "metadata.name", - buildapi.GetBuildPodName(build), + buildapihelpers.GetBuildPodName(build), ).String(), } podWatch, err := kclientset.Core().Pods(ns).Watch(watchOpt)