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 18, 2018
1 parent 888df0f commit 0d263fc
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 50 deletions.
103 changes: 57 additions & 46 deletions pkg/oc/cli/logs/logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,12 @@ 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"

appsapi "github.com/openshift/origin/pkg/apps/apis/apps"
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"
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 +67,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 +108,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 +138,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 +192,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,9 +208,9 @@ func (o *LogsOptions) Complete(f kcmdutil.Factory, cmd *cobra.Command, args []st
}
o.Options = bopts

case appsapi.Resource("deploymentconfig"),
appsapi.Resource("deploymentconfigs"):
dopts := &appsapi.DeploymentLogOptions{
case appsapiv1.Resource("deploymentconfig"),
appsapiv1.Resource("deploymentconfigs"):
dopts := &appsapiv1.DeploymentLogOptions{
Container: podLogOptions.Container,
Follow: podLogOptions.Follow,
Previous: podLogOptions.Previous,
Expand All @@ -191,36 +228,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
27 changes: 27 additions & 0 deletions pkg/oc/originpolymorphichelpers/logsforobject.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ import (
"k8s.io/kubernetes/pkg/kubectl/genericclioptions"
"k8s.io/kubernetes/pkg/kubectl/polymorphichelpers"

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"
appsmanualclient "github.com/openshift/origin/pkg/apps/client/internalversion"
appsclientinternal "github.com/openshift/origin/pkg/apps/generated/internalclientset"
Expand Down Expand Up @@ -39,6 +42,30 @@ func NewLogsForObjectFn(delegate polymorphichelpers.LogsForObjectFunc) polymorph
return nil, err
}
return appsmanualclient.NewRolloutLogClient(appsClient.Apps().RESTClient(), t.Namespace).Logs(t.Name, *dopts), nil
case *appsapiv1.DeploymentConfig:
dopts, ok := options.(*appsapiv1.DeploymentLogOptions)
if !ok {
return nil, errors.New("provided options object is not a DeploymentLogOptions")
}
appsClient, err := appsclientinternal.NewForConfig(clientConfig)
if err != nil {
return nil, err
}
return appsmanualclient.NewRolloutLogClient(appsClient.Apps().RESTClient(), t.Namespace).Logs(t.Name, *dopts), nil
case *buildapiv1.Build:
bopts, ok := options.(*buildapiv1.BuildLogOptions)
if !ok {
return nil, errors.New("provided options object is not a v1.BuildLogOptions")
}
if bopts.Version != nil {
return nil, errors.New("cannot specify a version and a build")
}
buildClient, err := buildclientv1.NewForConfig(clientConfig)
if err != nil {
return nil, err
}
return buildmanualclient.NewBuildLogClient(buildClient.RESTClient(), t.Namespace).Logs(t.Name, *bopts), nil

case *buildapi.Build:
bopts, ok := options.(*buildapi.BuildLogOptions)
if !ok {
Expand Down

0 comments on commit 0d263fc

Please sign in to comment.