forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlogs.go
281 lines (244 loc) · 9.35 KB
/
logs.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
package logs
import (
"errors"
"fmt"
"github.com/spf13/cobra"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
kcmd "k8s.io/kubernetes/pkg/kubectl/cmd"
"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"
"k8s.io/kubernetes/pkg/kubectl/scheme"
appsv1 "github.com/openshift/api/apps/v1"
buildv1 "github.com/openshift/api/build/v1"
buildv1client "github.com/openshift/client-go/build/clientset/versioned/typed/build/v1"
buildapi "github.com/openshift/origin/pkg/build/apis/build"
buildutil "github.com/openshift/origin/pkg/build/util"
)
// LogsRecommendedCommandName is the recommended command name
// TODO: Probably move this pattern upstream?
const LogsRecommendedCommandName = "logs"
var (
logsLong = templates.LongDesc(`
Print the logs for a resource
Supported resources are builds, build configs (bc), deployment configs (dc), and pods.
When a pod is specified and has more than one container, the container name should be
specified via -c. When a build config or deployment config is specified, you can view
the logs for a particular version of it via --version.
If your pod is failing to start, you may need to use the --previous option to see the
logs of the last attempt.`)
logsExample = templates.Examples(`
# Start streaming the logs of the most recent build of the openldap build config.
%[1]s %[2]s -f bc/openldap
# Start streaming the logs of the latest deployment of the mysql deployment config.
%[1]s %[2]s -f dc/mysql
# Get the logs of the first deployment for the mysql deployment config. Note that logs
# from older deployments may not exist either because the deployment was successful
# or due to deployment pruning or manual deletion of the deployment.
%[1]s %[2]s --version=1 dc/mysql
# Return a snapshot of ruby-container logs from pod backend.
%[1]s %[2]s backend -c ruby-container
# Start streaming of ruby-container logs from pod backend.
%[1]s %[2]s -f pod/backend -c ruby-container`)
)
// LogsOptions holds all the necessary options for running oc logs.
type LogsOptions struct {
// Options should hold our own *LogOptions objects.
Options runtime.Object
// KubeLogOptions contains all the necessary options for
// running the upstream logs command.
KubeLogOptions *kcmd.LogsOptions
// Client enables access to the Build object when processing
// build logs for Jenkins Pipeline Strategy builds
Client buildv1client.BuildV1Interface
// Namespace is a required parameter when accessing the Build object when processing
// build logs for Jenkins Pipeline Strategy builds
Namespace string
Builder func() *resource.Builder
Resources []string
Version int64
genericclioptions.IOStreams
}
func NewLogsOptions(streams genericclioptions.IOStreams) *LogsOptions {
return &LogsOptions{
KubeLogOptions: kcmd.NewLogsOptions(streams, false),
IOStreams: streams,
}
}
// NewCmdLogs creates a new logs command that supports OpenShift resources.
func NewCmdLogs(name, baseName string, f kcmdutil.Factory, streams genericclioptions.IOStreams) *cobra.Command {
o := NewLogsOptions(streams)
cmd := kcmd.NewCmdLogs(f, streams)
cmd.Short = "Print the logs for a resource"
cmd.Long = logsLong
cmd.Example = fmt.Sprintf(logsExample, baseName, name)
cmd.SuggestFor = []string{"builds", "deployments"}
cmd.Run = func(cmd *cobra.Command, args []string) {
kcmdutil.CheckErr(o.Complete(f, cmd, args))
kcmdutil.CheckErr(o.Validate(args))
kcmdutil.CheckErr(o.RunLog())
}
cmd.Flags().Int64Var(&o.Version, "version", o.Version, "View the logs of a particular build or deployment by version if greater than zero")
return cmd
}
func isPipelineBuild(obj runtime.Object) (bool, *buildv1.BuildConfig, bool, *buildv1.Build, bool) {
bc, isBC := obj.(*buildv1.BuildConfig)
build, isBld := obj.(*buildv1.Build)
isPipeline := false
switch {
case isBC:
isPipeline = bc.Spec.CommonSpec.Strategy.JenkinsPipelineStrategy != nil
case isBld:
isPipeline = build.Spec.CommonSpec.Strategy.JenkinsPipelineStrategy != nil
}
return isPipeline, bc, isBC, build, isBld
}
// Complete calls the upstream Complete for the logs command and then resolves the
// resource a user requested to view its logs and creates the appropriate logOptions
// object for it.
func (o *LogsOptions) Complete(f kcmdutil.Factory, cmd *cobra.Command, args []string) error {
// manually bind all flag values from the upstream command
// TODO: once the upstream command supports binding flags
// by outside callers, this will no longer be needed.
o.KubeLogOptions.AllContainers = kcmdutil.GetFlagBool(cmd, "all-containers")
o.KubeLogOptions.Container = kcmdutil.GetFlagString(cmd, "container")
o.KubeLogOptions.Selector = kcmdutil.GetFlagString(cmd, "selector")
o.KubeLogOptions.Follow = kcmdutil.GetFlagBool(cmd, "follow")
o.KubeLogOptions.Previous = kcmdutil.GetFlagBool(cmd, "previous")
o.KubeLogOptions.Timestamps = kcmdutil.GetFlagBool(cmd, "timestamps")
o.KubeLogOptions.SinceTime = kcmdutil.GetFlagString(cmd, "since-time")
o.KubeLogOptions.LimitBytes = kcmdutil.GetFlagInt64(cmd, "limit-bytes")
o.KubeLogOptions.Tail = kcmdutil.GetFlagInt64(cmd, "tail")
o.KubeLogOptions.SinceSeconds = kcmdutil.GetFlagDuration(cmd, "since")
o.KubeLogOptions.ContainerNameSpecified = cmd.Flag("container").Changed
if err := o.KubeLogOptions.Complete(f, cmd, args); err != nil {
return err
}
var err error
o.KubeLogOptions.GetPodTimeout, err = kcmdutil.GetPodRunningTimeoutFlag(cmd)
if err != nil {
return err
}
o.Namespace, _, err = f.ToRawKubeConfigLoader().Namespace()
if err != nil {
return err
}
clientConfig, err := f.ToRESTConfig()
if err != nil {
return err
}
o.Client, err = buildv1client.NewForConfig(clientConfig)
if err != nil {
return err
}
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(args []string) error {
if err := o.KubeLogOptions.Validate(); err != nil {
return err
}
if o.Options == nil {
return nil
}
switch t := o.Options.(type) {
case *buildv1.BuildLogOptions:
if t.Previous && t.Version != nil {
return errors.New("cannot use both --previous and --version")
}
case *appsv1.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.(*corev1.PodLogOptions)
infos, err := o.Builder().
WithScheme(scheme.Scheme, scheme.Scheme.PrioritizedVersionsAllGroups()...).
NamespaceParam(o.Namespace).DefaultNamespace().
ResourceNames("pods", o.Resources...).
SingleResourceType().RequireObject(false).
Do().Infos()
if err != nil {
return err
}
if len(infos) != 1 {
return errors.New("expected a resource")
}
// TODO: podLogOptions should be included in our own logOptions objects.
switch gr := infos[0].Mapping.Resource.GroupResource(); gr {
case buildv1.Resource("builds"),
buildv1.Resource("buildconfigs"):
bopts := &buildv1.BuildLogOptions{
Follow: podLogOptions.Follow,
Previous: podLogOptions.Previous,
SinceSeconds: podLogOptions.SinceSeconds,
SinceTime: podLogOptions.SinceTime,
Timestamps: podLogOptions.Timestamps,
TailLines: podLogOptions.TailLines,
LimitBytes: podLogOptions.LimitBytes,
}
if o.Version != 0 {
bopts.Version = &o.Version
}
o.Options = bopts
case appsv1.Resource("deploymentconfigs"):
dopts := &appsv1.DeploymentLogOptions{
Container: podLogOptions.Container,
Follow: podLogOptions.Follow,
Previous: podLogOptions.Previous,
SinceSeconds: podLogOptions.SinceSeconds,
SinceTime: podLogOptions.SinceTime,
Timestamps: podLogOptions.Timestamps,
TailLines: podLogOptions.TailLines,
LimitBytes: podLogOptions.LimitBytes,
}
if o.Version != 0 {
dopts.Version = &o.Version
}
o.Options = dopts
default:
o.Options = nil
}
return o.runLogPipeline()
}
func (o *LogsOptions) runLogPipeline() error {
if o.Options != nil {
// Use our own options object.
o.KubeLogOptions.Options = o.Options
}
isPipeline, bc, isBC, build, isBld := isPipelineBuild(o.KubeLogOptions.Object)
if !isPipeline {
return o.KubeLogOptions.RunLogs()
}
switch {
case isBC:
buildName := buildutil.BuildNameForConfigVersion(bc.ObjectMeta.Name, int(bc.Status.LastVersion))
build, _ = o.Client.Builds(o.Namespace).Get(buildName, metav1.GetOptions{})
if build == nil {
return fmt.Errorf("the build %s for build config %s was not found", buildName, bc.Name)
}
fallthrough
case isBld:
urlString, _ := build.Annotations[buildapi.BuildJenkinsBlueOceanLogURLAnnotation]
if len(urlString) == 0 {
return fmt.Errorf("the pipeline strategy build %s does not yet contain the log URL; wait a few moments, then try again", build.Name)
}
fmt.Fprintf(o.Out, "info: logs available at %s\n", urlString)
default:
return fmt.Errorf("a pipeline strategy build log operation peformed against invalid object %#v", o.KubeLogOptions.Object)
}
return nil
}