From 0d263fca067ec53188ab2ebd5e7a48b25cad9c1a Mon Sep 17 00:00:00 2001 From: juanvallejo Date: Tue, 17 Jul 2018 14:26:40 -0400 Subject: [PATCH] switch logs to externals --- pkg/oc/cli/logs/logs.go | 103 ++++++++++-------- pkg/oc/cli/logs/logs_test.go | 10 +- .../originpolymorphichelpers/logsforobject.go | 27 +++++ 3 files changed, 90 insertions(+), 50 deletions(-) diff --git a/pkg/oc/cli/logs/logs.go b/pkg/oc/cli/logs/logs.go index e1a84994914d..9fb7bc811e02 100644 --- a/pkg/oc/cli/logs/logs.go +++ b/pkg/oc/cli/logs/logs.go @@ -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" ) @@ -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 } @@ -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: @@ -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 { @@ -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, @@ -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, @@ -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 diff --git a/pkg/oc/cli/logs/logs_test.go b/pkg/oc/cli/logs/logs_test.go index 27d384951910..bfe78b3f9f65 100644 --- a/pkg/oc/cli/logs/logs_test.go +++ b/pkg/oc/cli/logs/logs_test.go @@ -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 @@ -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{ @@ -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") { diff --git a/pkg/oc/originpolymorphichelpers/logsforobject.go b/pkg/oc/originpolymorphichelpers/logsforobject.go index 1fc0ac04c2b1..0855f7de929f 100644 --- a/pkg/oc/originpolymorphichelpers/logsforobject.go +++ b/pkg/oc/originpolymorphichelpers/logsforobject.go @@ -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" @@ -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 {