Skip to content

Commit

Permalink
switch logs to externals
Browse files Browse the repository at this point in the history
  • Loading branch information
juanvallejo committed Jul 17, 2018
1 parent 179c130 commit 2d5e0e4
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 48 deletions.
101 changes: 57 additions & 44 deletions pkg/oc/cli/logs/logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,13 @@ import (
"k8s.io/kubernetes/pkg/kubectl/cmd/templates"
kcmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util"
"k8s.io/kubernetes/pkg/kubectl/genericclioptions"
"k8s.io/kubernetes/pkg/kubectl/genericclioptions/resource"

appsapiv1 "github.com/openshift/api/apps/v1"
buildapiv1 "github.com/openshift/api/build/v1"
buildclientv1 "github.com/openshift/client-go/build/clientset/versioned/typed/build/v1"
appsapi "github.com/openshift/origin/pkg/apps/apis/apps"
buildapi "github.com/openshift/origin/pkg/build/apis/build"
buildclientinternal "github.com/openshift/origin/pkg/build/generated/internalclientset"
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/oc/util/ocscheme"
)
Expand Down Expand Up @@ -66,11 +68,15 @@ type LogsOptions struct {
KubeLogOptions *kcmd.LogsOptions
// Client enables access to the Build object when processing
// build logs for Jenkins Pipeline Strategy builds
Client buildclient.BuildsGetter
Client buildclientv1.BuildV1Interface
// Namespace is a required parameter when accessing the Build object when processing
// build logs for Jenkins Pipeline Strategy builds
Namespace string
Version int64

Builder func() *resource.Builder
Resources []string

Version int64

genericclioptions.IOStreams
}
Expand Down Expand Up @@ -103,9 +109,9 @@ func NewCmdLogs(name, baseName string, f kcmdutil.Factory, streams genericcliopt
return cmd
}

func isPipelineBuild(obj runtime.Object) (bool, *buildapi.BuildConfig, bool, *buildapi.Build, bool) {
bc, isBC := obj.(*buildapi.BuildConfig)
build, isBld := obj.(*buildapi.Build)
func isPipelineBuild(obj runtime.Object) (bool, *buildapiv1.BuildConfig, bool, *buildapiv1.Build, bool) {
bc, isBC := obj.(*buildapiv1.BuildConfig)
build, isBld := obj.(*buildapiv1.Build)
isPipeline := false
switch {
case isBC:
Expand Down Expand Up @@ -133,17 +139,49 @@ func (o *LogsOptions) Complete(f kcmdutil.Factory, cmd *cobra.Command, args []st
if err != nil {
return err
}
client, err := buildclientinternal.NewForConfig(clientConfig)
o.Client, err = buildclientv1.NewForConfig(clientConfig)
if err != nil {
return err
}
o.Client = client.Build()

o.Builder = f.NewBuilder
o.Resources = args

return nil
}

// Validate runs the upstream validation for the logs command and then it
// will validate any OpenShift-specific log options.
func (o *LogsOptions) Validate() error {
if err := o.KubeLogOptions.Validate(); err != nil {
return err
}
if o.Options == nil {
return nil
}
switch t := o.Options.(type) {
case *buildapiv1.BuildLogOptions:
if t.Previous && t.Version != nil {
return errors.New("cannot use both --previous and --version")
}
case *appsapiv1.DeploymentLogOptions:
if t.Previous && t.Version != nil {
return errors.New("cannot use both --previous and --version")
}
default:
return errors.New("invalid log options object provided")
}
return nil
}

// RunLog will run the upstream logs command and may use an OpenShift
// logOptions object.
func (o *LogsOptions) RunLog() error {
podLogOptions := o.KubeLogOptions.Options.(*kapi.PodLogOptions)
infos, err := f.NewBuilder().
infos, err := o.Builder().
WithScheme(ocscheme.ReadingInternalScheme, ocscheme.ReadingInternalScheme.PrioritizedVersionsAllGroups()...).
NamespaceParam(o.Namespace).DefaultNamespace().
ResourceNames("pods", args...).
ResourceNames("pods", o.Resources...).
SingleResourceType().RequireObject(false).
Do().Infos()
if err != nil {
Expand All @@ -155,9 +193,9 @@ func (o *LogsOptions) Complete(f kcmdutil.Factory, cmd *cobra.Command, args []st

// TODO: podLogOptions should be included in our own logOptions objects.
switch gr := infos[0].Mapping.Resource.GroupResource(); gr {
case buildapi.Resource("builds"),
buildapi.Resource("buildconfigs"):
bopts := &buildapi.BuildLogOptions{
case buildapiv1.Resource("builds"),
buildapiv1.Resource("buildconfigs"):
bopts := &buildapiv1.BuildLogOptions{
Follow: podLogOptions.Follow,
Previous: podLogOptions.Previous,
SinceSeconds: podLogOptions.SinceSeconds,
Expand All @@ -171,8 +209,9 @@ func (o *LogsOptions) Complete(f kcmdutil.Factory, cmd *cobra.Command, args []st
}
o.Options = bopts

case appsapi.Resource("deploymentconfig"),
appsapi.Resource("deploymentconfigs"):
case appsapiv1.Resource("deploymentconfig"),
appsapiv1.Resource("deploymentconfigs"):
// TODO: switch to using appsapiv1.DeploymentLogOptions once originpolymorphichelpers.LogsForObjectFn supports appsv1
dopts := &appsapi.DeploymentLogOptions{
Container: podLogOptions.Container,
Follow: podLogOptions.Follow,
Expand All @@ -191,36 +230,10 @@ func (o *LogsOptions) Complete(f kcmdutil.Factory, cmd *cobra.Command, args []st
o.Options = nil
}

return nil
}

// Validate runs the upstream validation for the logs command and then it
// will validate any OpenShift-specific log options.
func (o *LogsOptions) Validate() error {
if err := o.KubeLogOptions.Validate(); err != nil {
return err
}
if o.Options == nil {
return nil
}
switch t := o.Options.(type) {
case *buildapi.BuildLogOptions:
if t.Previous && t.Version != nil {
return errors.New("cannot use both --previous and --version")
}
case *appsapi.DeploymentLogOptions:
if t.Previous && t.Version != nil {
return errors.New("cannot use both --previous and --version")
}
default:
return errors.New("invalid log options object provided")
}
return nil
return o.runLogPipeline()
}

// RunLog will run the upstream logs command and may use an OpenShift
// logOptions object.
func (o *LogsOptions) RunLog() error {
func (o *LogsOptions) runLogPipeline() error {
if o.Options != nil {
// Use our own options object.
o.KubeLogOptions.Options = o.Options
Expand Down
10 changes: 6 additions & 4 deletions pkg/oc/cli/logs/logs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@ import (
kcmd "k8s.io/kubernetes/pkg/kubectl/cmd"
"k8s.io/kubernetes/pkg/kubectl/genericclioptions"

buildapi "github.com/openshift/api/build/v1"
buildfake "github.com/openshift/client-go/build/clientset/versioned/fake"
appsapi "github.com/openshift/origin/pkg/apps/apis/apps"
buildapi "github.com/openshift/origin/pkg/build/apis/build"
buildfake "github.com/openshift/origin/pkg/build/generated/internalclientset/fake"
internalbuildapi "github.com/openshift/origin/pkg/build/apis/build"
)

// TestLogsFlagParity makes sure that our copied flags don't slip during rebases
Expand Down Expand Up @@ -50,7 +51,7 @@ func TestRunLogForPipelineStrategy(t *testing.T) {
ObjectMeta: metav1.ObjectMeta{
Name: "foo-0",
Namespace: "foo",
Annotations: map[string]string{buildapi.BuildJenkinsBlueOceanLogURLAnnotation: "https://foo"},
Annotations: map[string]string{internalbuildapi.BuildJenkinsBlueOceanLogURLAnnotation: "https://foo"},
},
Spec: buildapi.BuildSpec{
CommonSpec: buildapi.CommonSpec{
Expand Down Expand Up @@ -91,12 +92,13 @@ func TestRunLogForPipelineStrategy(t *testing.T) {
o := &LogsOptions{
IOStreams: streams,
KubeLogOptions: &kcmd.LogsOptions{
IOStreams: streams,
Object: tc.o,
Namespace: "foo",
},
Client: fakebc.Build(),
}
if err := o.RunLog(); err != nil {
if err := o.runLogPipeline(); err != nil {
t.Errorf("%#v: RunLog error %v", tc.o, err)
}
if !strings.Contains(out.String(), "https://foo") {
Expand Down

0 comments on commit 2d5e0e4

Please sign in to comment.