Skip to content

Commit 2d5e0e4

Browse files
committed
switch logs to externals
1 parent 179c130 commit 2d5e0e4

File tree

2 files changed

+63
-48
lines changed

2 files changed

+63
-48
lines changed

pkg/oc/cli/logs/logs.go

Lines changed: 57 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,13 @@ import (
1313
"k8s.io/kubernetes/pkg/kubectl/cmd/templates"
1414
kcmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util"
1515
"k8s.io/kubernetes/pkg/kubectl/genericclioptions"
16+
"k8s.io/kubernetes/pkg/kubectl/genericclioptions/resource"
1617

18+
appsapiv1 "github.com/openshift/api/apps/v1"
19+
buildapiv1 "github.com/openshift/api/build/v1"
20+
buildclientv1 "github.com/openshift/client-go/build/clientset/versioned/typed/build/v1"
1721
appsapi "github.com/openshift/origin/pkg/apps/apis/apps"
1822
buildapi "github.com/openshift/origin/pkg/build/apis/build"
19-
buildclientinternal "github.com/openshift/origin/pkg/build/generated/internalclientset"
20-
buildclient "github.com/openshift/origin/pkg/build/generated/internalclientset/typed/build/internalversion"
2123
buildutil "github.com/openshift/origin/pkg/build/util"
2224
"github.com/openshift/origin/pkg/oc/util/ocscheme"
2325
)
@@ -66,11 +68,15 @@ type LogsOptions struct {
6668
KubeLogOptions *kcmd.LogsOptions
6769
// Client enables access to the Build object when processing
6870
// build logs for Jenkins Pipeline Strategy builds
69-
Client buildclient.BuildsGetter
71+
Client buildclientv1.BuildV1Interface
7072
// Namespace is a required parameter when accessing the Build object when processing
7173
// build logs for Jenkins Pipeline Strategy builds
7274
Namespace string
73-
Version int64
75+
76+
Builder func() *resource.Builder
77+
Resources []string
78+
79+
Version int64
7480

7581
genericclioptions.IOStreams
7682
}
@@ -103,9 +109,9 @@ func NewCmdLogs(name, baseName string, f kcmdutil.Factory, streams genericcliopt
103109
return cmd
104110
}
105111

106-
func isPipelineBuild(obj runtime.Object) (bool, *buildapi.BuildConfig, bool, *buildapi.Build, bool) {
107-
bc, isBC := obj.(*buildapi.BuildConfig)
108-
build, isBld := obj.(*buildapi.Build)
112+
func isPipelineBuild(obj runtime.Object) (bool, *buildapiv1.BuildConfig, bool, *buildapiv1.Build, bool) {
113+
bc, isBC := obj.(*buildapiv1.BuildConfig)
114+
build, isBld := obj.(*buildapiv1.Build)
109115
isPipeline := false
110116
switch {
111117
case isBC:
@@ -133,17 +139,49 @@ func (o *LogsOptions) Complete(f kcmdutil.Factory, cmd *cobra.Command, args []st
133139
if err != nil {
134140
return err
135141
}
136-
client, err := buildclientinternal.NewForConfig(clientConfig)
142+
o.Client, err = buildclientv1.NewForConfig(clientConfig)
137143
if err != nil {
138144
return err
139145
}
140-
o.Client = client.Build()
141146

147+
o.Builder = f.NewBuilder
148+
o.Resources = args
149+
150+
return nil
151+
}
152+
153+
// Validate runs the upstream validation for the logs command and then it
154+
// will validate any OpenShift-specific log options.
155+
func (o *LogsOptions) Validate() error {
156+
if err := o.KubeLogOptions.Validate(); err != nil {
157+
return err
158+
}
159+
if o.Options == nil {
160+
return nil
161+
}
162+
switch t := o.Options.(type) {
163+
case *buildapiv1.BuildLogOptions:
164+
if t.Previous && t.Version != nil {
165+
return errors.New("cannot use both --previous and --version")
166+
}
167+
case *appsapiv1.DeploymentLogOptions:
168+
if t.Previous && t.Version != nil {
169+
return errors.New("cannot use both --previous and --version")
170+
}
171+
default:
172+
return errors.New("invalid log options object provided")
173+
}
174+
return nil
175+
}
176+
177+
// RunLog will run the upstream logs command and may use an OpenShift
178+
// logOptions object.
179+
func (o *LogsOptions) RunLog() error {
142180
podLogOptions := o.KubeLogOptions.Options.(*kapi.PodLogOptions)
143-
infos, err := f.NewBuilder().
181+
infos, err := o.Builder().
144182
WithScheme(ocscheme.ReadingInternalScheme, ocscheme.ReadingInternalScheme.PrioritizedVersionsAllGroups()...).
145183
NamespaceParam(o.Namespace).DefaultNamespace().
146-
ResourceNames("pods", args...).
184+
ResourceNames("pods", o.Resources...).
147185
SingleResourceType().RequireObject(false).
148186
Do().Infos()
149187
if err != nil {
@@ -155,9 +193,9 @@ func (o *LogsOptions) Complete(f kcmdutil.Factory, cmd *cobra.Command, args []st
155193

156194
// TODO: podLogOptions should be included in our own logOptions objects.
157195
switch gr := infos[0].Mapping.Resource.GroupResource(); gr {
158-
case buildapi.Resource("builds"),
159-
buildapi.Resource("buildconfigs"):
160-
bopts := &buildapi.BuildLogOptions{
196+
case buildapiv1.Resource("builds"),
197+
buildapiv1.Resource("buildconfigs"):
198+
bopts := &buildapiv1.BuildLogOptions{
161199
Follow: podLogOptions.Follow,
162200
Previous: podLogOptions.Previous,
163201
SinceSeconds: podLogOptions.SinceSeconds,
@@ -171,8 +209,9 @@ func (o *LogsOptions) Complete(f kcmdutil.Factory, cmd *cobra.Command, args []st
171209
}
172210
o.Options = bopts
173211

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

194-
return nil
195-
}
196-
197-
// Validate runs the upstream validation for the logs command and then it
198-
// will validate any OpenShift-specific log options.
199-
func (o *LogsOptions) Validate() error {
200-
if err := o.KubeLogOptions.Validate(); err != nil {
201-
return err
202-
}
203-
if o.Options == nil {
204-
return nil
205-
}
206-
switch t := o.Options.(type) {
207-
case *buildapi.BuildLogOptions:
208-
if t.Previous && t.Version != nil {
209-
return errors.New("cannot use both --previous and --version")
210-
}
211-
case *appsapi.DeploymentLogOptions:
212-
if t.Previous && t.Version != nil {
213-
return errors.New("cannot use both --previous and --version")
214-
}
215-
default:
216-
return errors.New("invalid log options object provided")
217-
}
218-
return nil
233+
return o.runLogPipeline()
219234
}
220235

221-
// RunLog will run the upstream logs command and may use an OpenShift
222-
// logOptions object.
223-
func (o *LogsOptions) RunLog() error {
236+
func (o *LogsOptions) runLogPipeline() error {
224237
if o.Options != nil {
225238
// Use our own options object.
226239
o.KubeLogOptions.Options = o.Options

pkg/oc/cli/logs/logs_test.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,10 @@ import (
1212
kcmd "k8s.io/kubernetes/pkg/kubectl/cmd"
1313
"k8s.io/kubernetes/pkg/kubectl/genericclioptions"
1414

15+
buildapi "github.com/openshift/api/build/v1"
16+
buildfake "github.com/openshift/client-go/build/clientset/versioned/fake"
1517
appsapi "github.com/openshift/origin/pkg/apps/apis/apps"
16-
buildapi "github.com/openshift/origin/pkg/build/apis/build"
17-
buildfake "github.com/openshift/origin/pkg/build/generated/internalclientset/fake"
18+
internalbuildapi "github.com/openshift/origin/pkg/build/apis/build"
1819
)
1920

2021
// TestLogsFlagParity makes sure that our copied flags don't slip during rebases
@@ -50,7 +51,7 @@ func TestRunLogForPipelineStrategy(t *testing.T) {
5051
ObjectMeta: metav1.ObjectMeta{
5152
Name: "foo-0",
5253
Namespace: "foo",
53-
Annotations: map[string]string{buildapi.BuildJenkinsBlueOceanLogURLAnnotation: "https://foo"},
54+
Annotations: map[string]string{internalbuildapi.BuildJenkinsBlueOceanLogURLAnnotation: "https://foo"},
5455
},
5556
Spec: buildapi.BuildSpec{
5657
CommonSpec: buildapi.CommonSpec{
@@ -91,12 +92,13 @@ func TestRunLogForPipelineStrategy(t *testing.T) {
9192
o := &LogsOptions{
9293
IOStreams: streams,
9394
KubeLogOptions: &kcmd.LogsOptions{
95+
IOStreams: streams,
9496
Object: tc.o,
9597
Namespace: "foo",
9698
},
9799
Client: fakebc.Build(),
98100
}
99-
if err := o.RunLog(); err != nil {
101+
if err := o.runLogPipeline(); err != nil {
100102
t.Errorf("%#v: RunLog error %v", tc.o, err)
101103
}
102104
if !strings.Contains(out.String(), "https://foo") {

0 commit comments

Comments
 (0)