Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 6459805

Browse files
committedAug 2, 2018
React to LogsForObjectFn signature change
1 parent b4bd6a8 commit 6459805

File tree

4 files changed

+77
-63
lines changed

4 files changed

+77
-63
lines changed
 

‎pkg/oc/cli/admin/diagnostics/diagnostics/client/pod/run_diagnostics_pod.go

+21-19
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ func (d *DiagnosticPod) processDiagnosticPodResults(protoPod *kapi.Pod, imageNam
148148
Follow: true,
149149
LimitBytes: &bytelim,
150150
}
151-
req, err := polymorphichelpers.LogsForObjectFn(d.Factory, pod, podLogsOpts, 1*time.Minute)
151+
requests, err := polymorphichelpers.LogsForObjectFn(d.Factory, pod, podLogsOpts, 1*time.Minute, false)
152152
if err != nil {
153153
r.Error("DCli2005", err, fmt.Sprintf("The request for diagnostic pod logs failed unexpectedly. Error: (%T) %[1]v", err))
154154
return
@@ -157,28 +157,30 @@ func (d *DiagnosticPod) processDiagnosticPodResults(protoPod *kapi.Pod, imageNam
157157
// wait for pod to be started and logs available
158158
var scanner *bufio.Scanner
159159
var lastError error
160+
outerLoop:
160161
for times := 1; true; times++ {
161162
if times <= 50 {
162-
readCloser, err := req.Stream()
163-
if err != nil {
164-
lastError = err
165-
r.Debug("DCli2010", fmt.Sprintf("Could not get diagnostic pod logs (loop %d): (%T[2]) %[2]v", times, err))
163+
for _, req := range requests {
164+
readCloser, err := req.Stream()
165+
if err != nil {
166+
lastError = err
167+
r.Debug("DCli2010", fmt.Sprintf("Could not get diagnostic pod logs (loop %d): (%T[2]) %[2]v", times, err))
168+
time.Sleep(time.Duration(times*100) * time.Millisecond)
169+
continue outerLoop
170+
}
171+
// make sure we can actually get something from the stream before going on.
172+
// it seems the creation of docker logs can trail the container start a bit.
173+
lineScanner := bufio.NewScanner(readCloser)
174+
if lineScanner.Scan() {
175+
scanner = lineScanner
176+
break outerLoop // success - drop down to reading the logs.
177+
}
178+
// no luck - try, try again
179+
lastError = fmt.Errorf("Diagnostics pod is ready but not its logs (loop %d). Retry.", times)
180+
r.Debug("DCli2010", lastError.Error())
166181
time.Sleep(time.Duration(times*100) * time.Millisecond)
167-
continue
182+
continue outerLoop
168183
}
169-
defer readCloser.Close()
170-
// make sure we can actually get something from the stream before going on.
171-
// it seems the creation of docker logs can trail the container start a bit.
172-
lineScanner := bufio.NewScanner(readCloser)
173-
if lineScanner.Scan() {
174-
scanner = lineScanner
175-
break // success - drop down to reading the logs.
176-
}
177-
// no luck - try, try again
178-
lastError = fmt.Errorf("Diagnostics pod is ready but not its logs (loop %d). Retry.", times)
179-
r.Debug("DCli2010", lastError.Error())
180-
time.Sleep(time.Duration(times*100) * time.Millisecond)
181-
continue
182184
}
183185
// tries exhausted
184186
r.Warn("DCli2006", err, fmt.Sprintf("Timed out preparing diagnostic pod logs for streaming, so this diagnostic cannot run.\nIt is likely that the image '%s' was not pulled and running yet.\nLast error: (%T[2]) %[2]v", pod.Spec.Containers[0].Image, lastError))

‎pkg/oc/cli/admin/diagnostics/diagnostics/cluster/network/results.go

+30-28
Original file line numberDiff line numberDiff line change
@@ -106,40 +106,42 @@ func (d *NetworkDiagnostic) getNetworkPodLogs(pod *kapi.Pod) error {
106106
LimitBytes: &bytelim,
107107
}
108108

109-
req, err := polymorphichelpers.LogsForObjectFn(d.Factory, pod, opts, 1*time.Minute)
109+
requests, err := polymorphichelpers.LogsForObjectFn(d.Factory, pod, opts, 1*time.Minute, false)
110110
if err != nil {
111111
return fmt.Errorf("Request for network diagnostic pod on node %q failed unexpectedly: %v", pod.Spec.NodeName, err)
112112
}
113-
readCloser, err := req.Stream()
114-
if err != nil {
115-
return fmt.Errorf("Logs for network diagnostic pod on node %q failed: %v", pod.Spec.NodeName, err)
116-
}
117-
defer readCloser.Close()
118-
119-
scanner := bufio.NewScanner(readCloser)
120-
podLogs, nwarnings, nerrors := "", 0, 0
121-
errorRegex := regexp.MustCompile(`^\[Note\]\s+Errors\s+seen:\s+(\d+)`)
122-
warnRegex := regexp.MustCompile(`^\[Note\]\s+Warnings\s+seen:\s+(\d+)`)
123-
124-
for scanner.Scan() {
125-
line := scanner.Text()
126-
podLogs += line + "\n"
127-
if matches := errorRegex.FindStringSubmatch(line); matches != nil {
128-
nerrors, _ = strconv.Atoi(matches[1])
129-
} else if matches := warnRegex.FindStringSubmatch(line); matches != nil {
130-
nwarnings, _ = strconv.Atoi(matches[1])
113+
for _, req := range requests {
114+
readCloser, err := req.Stream()
115+
if err != nil {
116+
return fmt.Errorf("Logs for network diagnostic pod on node %q failed: %v", pod.Spec.NodeName, err)
117+
}
118+
defer readCloser.Close()
119+
120+
scanner := bufio.NewScanner(readCloser)
121+
podLogs, nwarnings, nerrors := "", 0, 0
122+
errorRegex := regexp.MustCompile(`^\[Note\]\s+Errors\s+seen:\s+(\d+)`)
123+
warnRegex := regexp.MustCompile(`^\[Note\]\s+Warnings\s+seen:\s+(\d+)`)
124+
125+
for scanner.Scan() {
126+
line := scanner.Text()
127+
podLogs += line + "\n"
128+
if matches := errorRegex.FindStringSubmatch(line); matches != nil {
129+
nerrors, _ = strconv.Atoi(matches[1])
130+
} else if matches := warnRegex.FindStringSubmatch(line); matches != nil {
131+
nwarnings, _ = strconv.Atoi(matches[1])
132+
}
131133
}
132-
}
133134

134-
if err := scanner.Err(); err != nil { // Scan terminated abnormally
135-
return fmt.Errorf("Unexpected error reading network diagnostic pod on node %q: (%T) %[1]v\nLogs are:\n%[3]s", pod.Spec.NodeName, err, podLogs)
136-
} else {
137-
if nerrors > 0 {
138-
return fmt.Errorf("See the errors below in the output from the network diagnostic pod on node %q:\n%s", pod.Spec.NodeName, podLogs)
139-
} else if nwarnings > 0 {
140-
d.res.Warn("DNet4002", nil, fmt.Sprintf("See the warnings below in the output from the network diagnostic pod on node %q:\n%s", pod.Spec.NodeName, podLogs))
135+
if err := scanner.Err(); err != nil { // Scan terminated abnormally
136+
return fmt.Errorf("Unexpected error reading network diagnostic pod on node %q: (%T) %[1]v\nLogs are:\n%[3]s", pod.Spec.NodeName, err, podLogs)
141137
} else {
142-
d.res.Info("DNet4003", fmt.Sprintf("Output from the network diagnostic pod on node %q:\n%s", pod.Spec.NodeName, podLogs))
138+
if nerrors > 0 {
139+
return fmt.Errorf("See the errors below in the output from the network diagnostic pod on node %q:\n%s", pod.Spec.NodeName, podLogs)
140+
} else if nwarnings > 0 {
141+
d.res.Warn("DNet4002", nil, fmt.Sprintf("See the warnings below in the output from the network diagnostic pod on node %q:\n%s", pod.Spec.NodeName, podLogs))
142+
} else {
143+
d.res.Info("DNet4003", fmt.Sprintf("Output from the network diagnostic pod on node %q:\n%s", pod.Spec.NodeName, podLogs))
144+
}
143145
}
144146
}
145147
return nil

‎pkg/oc/cli/admin/diagnostics/diagnostics/cluster/network/setup.go

+8-6
Original file line numberDiff line numberDiff line change
@@ -194,17 +194,19 @@ func (d *NetworkDiagnostic) getPodLogs(nsList []string) (string, error) {
194194
LimitBytes: &limit,
195195
}
196196

197-
req, err := polymorphichelpers.LogsForObjectFn(d.Factory, &pod, opts, 10*time.Second)
197+
requests, err := polymorphichelpers.LogsForObjectFn(d.Factory, &pod, opts, 10*time.Second, false)
198198
if err != nil {
199199
errList = append(errList, err)
200200
continue
201201
}
202-
data, err := req.DoRaw()
203-
if err != nil {
204-
errList = append(errList, err)
205-
continue
202+
for _, req := range requests {
203+
data, err := req.DoRaw()
204+
if err != nil {
205+
errList = append(errList, err)
206+
continue
207+
}
208+
logData.Insert(string(data[:]))
206209
}
207-
logData.Insert(string(data[:]))
208210
}
209211
}
210212
return strings.Join(logData.List(), ", "), kerrors.NewAggregate(errList)

‎pkg/oc/originpolymorphichelpers/logsforobject.go

+18-10
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ import (
2929
)
3030

3131
func NewLogsForObjectFn(delegate polymorphichelpers.LogsForObjectFunc) polymorphichelpers.LogsForObjectFunc {
32-
return func(restClientGetter genericclioptions.RESTClientGetter, object, options runtime.Object, timeout time.Duration) (*rest.Request, error) {
32+
return func(restClientGetter genericclioptions.RESTClientGetter, object, options runtime.Object, timeout time.Duration, allContainers bool) ([]*rest.Request, error) {
3333
clientConfig, err := restClientGetter.ToRESTConfig()
3434
if err != nil {
3535
return nil, err
@@ -45,7 +45,8 @@ func NewLogsForObjectFn(delegate polymorphichelpers.LogsForObjectFunc) polymorph
4545
if err != nil {
4646
return nil, err
4747
}
48-
return appsmanualclient.NewRolloutLogClient(appsClient.Apps().RESTClient(), t.Namespace).Logs(t.Name, *dopts), nil
48+
// TODO: support allContainers flag
49+
return []*rest.Request{appsmanualclient.NewRolloutLogClient(appsClient.Apps().RESTClient(), t.Namespace).Logs(t.Name, *dopts)}, nil
4950
case *appsv1.DeploymentConfig:
5051
dopts, ok := options.(*appsv1.DeploymentLogOptions)
5152
if !ok {
@@ -55,7 +56,8 @@ func NewLogsForObjectFn(delegate polymorphichelpers.LogsForObjectFunc) polymorph
5556
if err != nil {
5657
return nil, err
5758
}
58-
return appsmanualclientv1.NewRolloutLogClient(appsClient.RESTClient(), t.Namespace).Logs(t.Name, *dopts), nil
59+
// TODO: support allContainers flag
60+
return []*rest.Request{appsmanualclientv1.NewRolloutLogClient(appsClient.RESTClient(), t.Namespace).Logs(t.Name, *dopts)}, nil
5961
case *buildv1.Build:
6062
bopts, ok := options.(*buildv1.BuildLogOptions)
6163
if !ok {
@@ -68,7 +70,8 @@ func NewLogsForObjectFn(delegate polymorphichelpers.LogsForObjectFunc) polymorph
6870
if err != nil {
6971
return nil, err
7072
}
71-
return buildmanualclientv1.NewBuildLogClient(buildClient.RESTClient(), t.Namespace).Logs(t.Name, *bopts), nil
73+
// TODO: support allContainers flag
74+
return []*rest.Request{buildmanualclientv1.NewBuildLogClient(buildClient.RESTClient(), t.Namespace).Logs(t.Name, *bopts)}, nil
7275
case *buildv1.BuildConfig:
7376
bopts, ok := options.(*buildv1.BuildLogOptions)
7477
if !ok {
@@ -93,10 +96,12 @@ func NewLogsForObjectFn(delegate polymorphichelpers.LogsForObjectFunc) polymorph
9396
if bopts.Version != nil {
9497
// If a version has been specified, try to get the logs from that build.
9598
desired := buildutil.BuildNameForConfigVersion(t.Name, int(*bopts.Version))
96-
return logClient.Logs(desired, *bopts), nil
99+
// TODO: support allContainers flag
100+
return []*rest.Request{logClient.Logs(desired, *bopts)}, nil
97101
}
98102
sort.Sort(sort.Reverse(ocbuildapihelpers.BuildSliceByCreationTimestamp(filteredInternalBuildItems)))
99-
return logClient.Logs(filteredInternalBuildItems[0].Name, *bopts), nil
103+
// TODO: support allContainers flag
104+
return []*rest.Request{logClient.Logs(filteredInternalBuildItems[0].Name, *bopts)}, nil
100105
case *buildapi.Build:
101106
bopts, ok := options.(*buildapi.BuildLogOptions)
102107
if !ok {
@@ -109,7 +114,8 @@ func NewLogsForObjectFn(delegate polymorphichelpers.LogsForObjectFunc) polymorph
109114
if err != nil {
110115
return nil, err
111116
}
112-
return buildmanualclient.NewBuildLogClient(buildClient.Build().RESTClient(), t.Namespace).Logs(t.Name, *bopts), nil
117+
// TODO: support allContainers flag
118+
return []*rest.Request{buildmanualclient.NewBuildLogClient(buildClient.Build().RESTClient(), t.Namespace).Logs(t.Name, *bopts)}, nil
113119
case *buildapi.BuildConfig:
114120
bopts, ok := options.(*buildapi.BuildLogOptions)
115121
if !ok {
@@ -131,13 +137,15 @@ func NewLogsForObjectFn(delegate polymorphichelpers.LogsForObjectFunc) polymorph
131137
if bopts.Version != nil {
132138
// If a version has been specified, try to get the logs from that build.
133139
desired := buildutil.BuildNameForConfigVersion(t.Name, int(*bopts.Version))
134-
return logClient.Logs(desired, *bopts), nil
140+
// TODO: support allContainers flag
141+
return []*rest.Request{logClient.Logs(desired, *bopts)}, nil
135142
}
136143
sort.Sort(sort.Reverse(ocbuildapihelpers.BuildSliceByCreationTimestampInternal(builds.Items)))
137-
return logClient.Logs(builds.Items[0].Name, *bopts), nil
144+
// TODO: support allContainers flag
145+
return []*rest.Request{logClient.Logs(builds.Items[0].Name, *bopts)}, nil
138146

139147
default:
140-
return delegate(restClientGetter, object, options, timeout)
148+
return delegate(restClientGetter, object, options, timeout, allContainers)
141149
}
142150
}
143151
}

0 commit comments

Comments
 (0)
Please sign in to comment.