Skip to content

Commit ee4826d

Browse files
authored
Merge pull request #52 from wrongerror/release-0.6
bugfixed in logs subcommand
2 parents b0ebb89 + 47792ed commit ee4826d

File tree

1 file changed

+35
-32
lines changed

1 file changed

+35
-32
lines changed

pkg/cmd/subcommand/logs.go

Lines changed: 35 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424

2525
"github.com/OpenFunction/cli/pkg/cmd/util"
2626
cc "github.com/OpenFunction/cli/pkg/cmd/util/client"
27+
openfunction "github.com/openfunction/apis/core/v1beta1"
2728
client "github.com/openfunction/pkg/client/clientset/versioned"
2829
swclient "github.com/shipwright-io/build/pkg/client/clientset/versioned"
2930
"github.com/spf13/cobra"
@@ -121,48 +122,48 @@ func (l *Logs) run() error {
121122
}
122123

123124
// Build stage
124-
if f.Status.Build != nil {
125+
if f.Status.Build != nil && f.Status.Build.State != openfunction.Skipped {
125126
builderRef := f.Status.Build.ResourceRef
126-
127-
// get openfunction builder ref
128-
builder, err := l.functionClient.CoreV1beta1().Builders(f.Namespace).Get(ctx, builderRef, metav1.GetOptions{})
129-
if err != nil {
130-
statusError, ok := err.(*k8serrors.StatusError)
131-
// builder has been cleaned up
132-
if !ok || statusError.Status().Code != 404 {
133-
return err
134-
}
135-
}
136-
137-
// get shipwright builder-buildrun
138-
swbuildrunRef := builder.Status.ResourceRef["shipwright.io/buildRun"]
139-
if swbuildrunRef != "" {
140-
swBuildRun, err := l.swClient.ShipwrightV1alpha1().BuildRuns(f.Namespace).Get(ctx, swbuildrunRef, metav1.GetOptions{})
127+
if builderRef != "" {
128+
// get openfunction builder ref
129+
builder, err := l.functionClient.CoreV1beta1().Builders(f.Namespace).Get(ctx, builderRef, metav1.GetOptions{})
141130
if err != nil {
142131
statusError, ok := err.(*k8serrors.StatusError)
143-
// buildrun has been cleaned up
132+
// builder has been cleaned up
144133
if !ok || statusError.Status().Code != 404 {
145134
return err
146135
}
147136
}
148-
err = l.logsForPods(ctx, metav1.ListOptions{LabelSelector: fmt.Sprintf("buildrun.shipwright.io/name=%s", swBuildRun.Name)})
149-
if err != nil {
150-
return err
137+
// get shipwright builder-buildrun
138+
swbuildrunRef := builder.Status.ResourceRef["shipwright.io/buildRun"]
139+
if swbuildrunRef != "" {
140+
swBuildRun, err := l.swClient.ShipwrightV1alpha1().BuildRuns(f.Namespace).Get(ctx, swbuildrunRef, metav1.GetOptions{})
141+
if err != nil {
142+
statusError, ok := err.(*k8serrors.StatusError)
143+
// buildrun has been cleaned up
144+
if !ok || statusError.Status().Code != 404 {
145+
return err
146+
}
147+
}
148+
err = l.logsForPods(ctx, metav1.ListOptions{LabelSelector: fmt.Sprintf("buildrun.shipwright.io/name=%s", swBuildRun.Name)})
149+
if err != nil {
150+
return err
151+
}
151152
}
152153
}
153-
154154
}
155155

156156
// Serving stage
157-
if f.Status.Serving != nil {
158-
serving := f.Status.Serving.ResourceRef
159-
160-
if l.containerName == "" {
161-
l.containerName = "function"
162-
}
163-
err := l.logsForPods(ctx, metav1.ListOptions{LabelSelector: fmt.Sprintf("openfunction.io/serving=%s", serving)})
164-
if err != nil {
165-
return err
157+
if f.Status.Serving != nil && f.Status.Serving.State != openfunction.Skipped {
158+
servingRef := f.Status.Serving.ResourceRef
159+
if servingRef != "" {
160+
if l.containerName == "" {
161+
l.containerName = "function"
162+
}
163+
err := l.logsForPods(ctx, metav1.ListOptions{LabelSelector: fmt.Sprintf("openfunction.io/serving=%s", servingRef)})
164+
if err != nil {
165+
return err
166+
}
166167
}
167168
}
168169

@@ -183,15 +184,17 @@ func (l *Logs) logsForPods(ctx context.Context, listOpt metav1.ListOptions) erro
183184
var logReader io.ReadCloser
184185
if l.containerName != "" {
185186
logReader, err = podInterface.GetLogs(pod.Name, &corev1.PodLogOptions{Follow: l.Follow, Container: l.containerName}).Stream(ctx)
186-
if err != nil {
187+
// the pod may be deleted due to scale, so we should exclude NotFound errors
188+
if err != nil && !k8serrors.IsNotFound(err) {
187189
return err
188190
}
189191
defer logReader.Close()
190192
readerList = append(readerList, logReader)
191193
} else {
192194
for _, container := range pod.Spec.Containers {
193195
logReader, err = podInterface.GetLogs(pod.Name, &corev1.PodLogOptions{Follow: l.Follow, Container: container.Name}).Stream(ctx)
194-
if err != nil {
196+
// the pod may be deleted due to scale, so we should exclude NotFound errors
197+
if err != nil && !k8serrors.IsNotFound(err) {
195198
return err
196199
}
197200

0 commit comments

Comments
 (0)