Skip to content

Commit

Permalink
make startbuild cmd external
Browse files Browse the repository at this point in the history
  • Loading branch information
juanvallejo committed Jul 23, 2018
1 parent efb331e commit 3ccb02a
Showing 1 changed file with 38 additions and 40 deletions.
78 changes: 38 additions & 40 deletions pkg/oc/cli/startbuild/startbuild.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"github.com/openshift/source-to-image/pkg/tar"
s2ifs "github.com/openshift/source-to-image/pkg/util/fs"

corev1 "k8s.io/api/core/v1"
kerrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand All @@ -30,17 +31,15 @@ import (
"k8s.io/apimachinery/third_party/forked/golang/netutil"
restclient "k8s.io/client-go/rest"
"k8s.io/kubernetes/pkg/api/legacyscheme"
kapi "k8s.io/kubernetes/pkg/apis/core"
"k8s.io/kubernetes/pkg/kubectl/cmd/templates"
kcmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util"
"k8s.io/kubernetes/pkg/kubectl/genericclioptions"

"github.com/openshift/api/build"
buildapiv1 "github.com/openshift/api/build/v1"
buildclientv1 "github.com/openshift/client-go/build/clientset/versioned/typed/build/v1"
buildapi "github.com/openshift/origin/pkg/build/apis/build"
buildclientinternalmanual "github.com/openshift/origin/pkg/build/client/internalversion"
buildclientinternal "github.com/openshift/origin/pkg/build/generated/internalclientset"
buildclient "github.com/openshift/origin/pkg/build/generated/internalclientset/typed/build/internalversion"
buildclientmanual "github.com/openshift/origin/pkg/build/client/v1"
cmdutil "github.com/openshift/origin/pkg/cmd/util"
"github.com/openshift/origin/pkg/git"
ocerrors "github.com/openshift/origin/pkg/oc/lib/errors"
Expand Down Expand Up @@ -115,14 +114,14 @@ type StartBuildOptions struct {
GitPostReceive string

Mapper meta.RESTMapper
BuildClient buildclient.BuildInterface
BuildLogClient buildclientinternalmanual.BuildLogInterface
BuildClient buildclientv1.BuildV1Interface
BuildLogClient buildclientmanual.BuildLogInterface
ClientConfig *restclient.Config

AsBinary bool
ShortOutput bool
EnvVar []kapi.EnvVar
BuildArgs []kapi.EnvVar
EnvVar []corev1.EnvVar
BuildArgs []corev1.EnvVar
Name string
Namespace string

Expand Down Expand Up @@ -237,20 +236,20 @@ func (o *StartBuildOptions) Complete(f kcmdutil.Factory, cmd *cobra.Command, cmd
return kcmdutil.UsageErrorf(cmd, "Must pass a name of a build config or specify build name with '--from-build' flag.\nUse \"%s get bc\" to list all available build configs.", cmdFullName)
}

namespace, _, err := f.ToRawKubeConfigLoader().Namespace()
clientConfig, err := f.ToRESTConfig()
if err != nil {
return err
}

clientConfig, err := f.ToRESTConfig()
o.Namespace, _, err = f.ToRawKubeConfigLoader().Namespace()
if err != nil {
return err
}
c, err := buildclientinternal.NewForConfig(clientConfig)

o.BuildClient, err = buildclientv1.NewForConfig(clientConfig)
if err != nil {
return err
}
o.BuildClient = c.Build()

var (
name = buildName
Expand Down Expand Up @@ -280,7 +279,7 @@ func (o *StartBuildOptions) Complete(f kcmdutil.Factory, cmd *cobra.Command, cmd

// when listing webhooks, allow --from-build to lookup a build config
if build.Resource("builds") == resource && len(o.ListWebhooks) > 0 {
build, err := o.BuildClient.Builds(namespace).Get(name, metav1.GetOptions{})
build, err := o.BuildClient.Builds(o.Namespace).Get(name, metav1.GetOptions{})
if err != nil {
return err
}
Expand All @@ -289,15 +288,14 @@ func (o *StartBuildOptions) Complete(f kcmdutil.Factory, cmd *cobra.Command, cmd
return fmt.Errorf("the provided Build %q was not created from a BuildConfig and cannot have webhooks", name)
}
if len(ref.Namespace) > 0 {
namespace = ref.Namespace
o.Namespace = ref.Namespace
}
name = ref.Name
}

o.Namespace = namespace
o.Name = name

o.BuildLogClient = buildclientinternalmanual.NewBuildLogClient(o.BuildClient.RESTClient(), o.Namespace)
o.BuildLogClient = buildclientmanual.NewBuildLogClient(o.BuildClient.RESTClient(), o.Namespace)

// Handle environment variables
cmdutil.WarnAboutCommaSeparation(o.ErrOut, o.Env, "--env")
Expand All @@ -306,7 +304,7 @@ func (o *StartBuildOptions) Complete(f kcmdutil.Factory, cmd *cobra.Command, cmd
return err
}
if len(buildLogLevel) > 0 {
env = append(env, kapi.EnvVar{Name: "BUILD_LOGLEVEL", Value: buildLogLevel})
env = append(env, corev1.EnvVar{Name: "BUILD_LOGLEVEL", Value: buildLogLevel})
}
o.EnvVar = env

Expand Down Expand Up @@ -357,17 +355,17 @@ func (o *StartBuildOptions) Run() error {
return o.RunListBuildWebHooks()
}

buildRequestCauses := []buildapi.BuildTriggerCause{}
request := &buildapi.BuildRequest{
buildRequestCauses := []buildapiv1.BuildTriggerCause{}
request := &buildapiv1.BuildRequest{
TriggeredBy: append(buildRequestCauses,
buildapi.BuildTriggerCause{
buildapiv1.BuildTriggerCause{
Message: buildapi.BuildTriggerCauseManualMsg,
},
),
ObjectMeta: metav1.ObjectMeta{Name: o.Name},
}

request.SourceStrategyOptions = &buildapi.SourceStrategyOptions{}
request.SourceStrategyOptions = &buildapiv1.SourceStrategyOptions{}
if o.IncrementalOverride {
request.SourceStrategyOptions.Incremental = &o.Incremental
}
Expand All @@ -376,7 +374,7 @@ func (o *StartBuildOptions) Run() error {
request.Env = o.EnvVar
}

request.DockerStrategyOptions = &buildapi.DockerStrategyOptions{}
request.DockerStrategyOptions = &buildapiv1.DockerStrategyOptions{}
if len(o.BuildArgs) > 0 {
request.DockerStrategyOptions.BuildArgs = o.BuildArgs
}
Expand All @@ -386,18 +384,18 @@ func (o *StartBuildOptions) Run() error {
}

if len(o.Commit) > 0 {
request.Revision = &buildapi.SourceRevision{
Git: &buildapi.GitSourceRevision{
request.Revision = &buildapiv1.SourceRevision{
Git: &buildapiv1.GitSourceRevision{
Commit: o.Commit,
},
}
}

var err error
var newBuild *buildapi.Build
var newBuild *buildapiv1.Build
switch {
case o.AsBinary:
request := &buildapi.BinaryBuildRequestOptions{
request := &buildapiv1.BinaryBuildRequestOptions{
ObjectMeta: metav1.ObjectMeta{
Name: o.Name,
Namespace: o.Namespace,
Expand All @@ -410,7 +408,7 @@ func (o *StartBuildOptions) Run() error {
if len(o.BuildArgs) > 0 {
fmt.Fprintf(o.ErrOut, "WARNING: Specifying build arguments with binary builds is not supported.\n")
}
instantiateClient := buildclientinternalmanual.NewBuildInstantiateBinaryClient(o.BuildClient.RESTClient(), o.Namespace)
instantiateClient := buildclientmanual.NewBuildInstantiateBinaryClient(o.BuildClient.RESTClient(), o.Namespace)
if newBuild, err = streamPathToBuild(o.Git, o.In, o.ErrOut, instantiateClient, o.FromDir, o.FromFile, o.FromRepo, request); err != nil {
if kerrors.IsAlreadyExists(err) {
return transformIsAlreadyExistsError(err, o.Name)
Expand Down Expand Up @@ -456,8 +454,8 @@ func (o *StartBuildOptions) Run() error {
return nil
}

func (o *StartBuildOptions) streamBuildLogs(build *buildapi.Build) error {
opts := buildapi.BuildLogOptions{
func (o *StartBuildOptions) streamBuildLogs(build *buildapiv1.Build) error {
opts := buildapiv1.BuildLogOptions{
Follow: true,
NoWait: false,
}
Expand Down Expand Up @@ -503,7 +501,7 @@ func (o *StartBuildOptions) RunListBuildWebHooks() error {
return err
}

webhookClient := buildclientinternalmanual.NewWebhookURLClient(o.BuildClient.RESTClient(), o.Namespace)
webhookClient := buildclientmanual.NewWebhookURLClient(o.BuildClient.RESTClient(), o.Namespace)
for _, t := range config.Spec.Triggers {
hookType := ""
switch {
Expand All @@ -520,7 +518,7 @@ func (o *StartBuildOptions) RunListBuildWebHooks() error {
}
u, err := webhookClient.WebHookURL(o.Name, &t)
if err != nil {
if err != buildclientinternalmanual.ErrTriggerIsNotAWebHook {
if err != buildclientmanual.ErrTriggerIsNotAWebHook {
fmt.Fprintf(o.ErrOut, "error: unable to get webhook for %s: %v", o.Name, err)
}
continue
Expand All @@ -531,7 +529,7 @@ func (o *StartBuildOptions) RunListBuildWebHooks() error {
return nil
}

func streamPathToBuild(repo git.Repository, in io.Reader, out io.Writer, client buildclientinternalmanual.BuildInstantiateBinaryInterface, fromDir, fromFile, fromRepo string, options *buildapi.BinaryBuildRequestOptions) (*buildapi.Build, error) {
func streamPathToBuild(repo git.Repository, in io.Reader, out io.Writer, client buildclientmanual.BuildInstantiateBinaryInterface, fromDir, fromFile, fromRepo string, options *buildapiv1.BinaryBuildRequestOptions) (*buildapiv1.Build, error) {
asDir, asFile, asRepo := len(fromDir) > 0, len(fromFile) > 0, len(fromRepo) > 0

if asRepo && !git.IsGitInstalled() {
Expand Down Expand Up @@ -890,14 +888,14 @@ func gitRefInfo(repo git.Repository, dir, ref string) (buildapi.GitRefInfo, erro
}

// WaitForBuildComplete waits for a build identified by the name to complete
func WaitForBuildComplete(c buildclient.BuildResourceInterface, name string) error {
isOK := func(b *buildapi.Build) bool {
return b.Status.Phase == buildapi.BuildPhaseComplete
func WaitForBuildComplete(c buildclientv1.BuildInterface, name string) error {
isOK := func(b *buildapiv1.Build) bool {
return b.Status.Phase == buildapiv1.BuildPhaseComplete
}
isFailed := func(b *buildapi.Build) bool {
return b.Status.Phase == buildapi.BuildPhaseFailed ||
b.Status.Phase == buildapi.BuildPhaseCancelled ||
b.Status.Phase == buildapi.BuildPhaseError
isFailed := func(b *buildapiv1.Build) bool {
return b.Status.Phase == buildapiv1.BuildPhaseFailed ||
b.Status.Phase == buildapiv1.BuildPhaseCancelled ||
b.Status.Phase == buildapiv1.BuildPhaseError
}
for {
list, err := c.List(metav1.ListOptions{FieldSelector: fields.Set{"metadata.name": name}.AsSelector().String()})
Expand Down Expand Up @@ -926,7 +924,7 @@ func WaitForBuildComplete(c buildclient.BuildResourceInterface, name string) err
// reget and re-watch
break
}
if e, ok := val.Object.(*buildapi.Build); ok {
if e, ok := val.Object.(*buildapiv1.Build); ok {
if name == e.Name && isOK(e) {
return nil
}
Expand Down

0 comments on commit 3ccb02a

Please sign in to comment.