Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(internal): Only query status before stopping #2118

Draft
wants to merge 2 commits into
base: staging
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ require (
k8s.io/apimachinery v0.32.1
k8s.io/apiserver v0.32.0
oras.land/oras-go/v2 v2.5.0
sdk.kraft.cloud v0.5.10-0.20250121130825-58dca316e23e
sdk.kraft.cloud v0.5.10-0.20250130161247-eb43ebd7d4ef
sigs.k8s.io/kustomize/kyaml v0.19.0
)

Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1784,6 +1784,8 @@ rsc.io/quote/v3 v3.1.0/go.mod h1:yEA65RcK8LyAZtP9Kv3t0HmxON59tX3rD+tICJqUlj0=
rsc.io/sampler v1.3.0/go.mod h1:T1hPZKmBbMNahiBKFy5HrXp6adAjACjK9JXDnKaTXpA=
sdk.kraft.cloud v0.5.10-0.20250121130825-58dca316e23e h1:RoMoO6NlSFl1f/Aosmiz69j7enPaK8/8CTbYhchcmuY=
sdk.kraft.cloud v0.5.10-0.20250121130825-58dca316e23e/go.mod h1:rQfcPNvAFbki+VZtN8RGowzskriGRQY9qEo8ZabmJSc=
sdk.kraft.cloud v0.5.10-0.20250130161247-eb43ebd7d4ef h1:T3j3R648kYC1ZPwvt0zBoUOlBAcgYZWqeBSMqypxGXc=
sdk.kraft.cloud v0.5.10-0.20250130161247-eb43ebd7d4ef/go.mod h1:MQJQxCYgtG+KNXXxkxJeJxEw0n3VFsRdcK+7oBuTrS8=
sigs.k8s.io/apiserver-network-proxy/konnectivity-client v0.0.14/go.mod h1:LEScyzhFmoF5pso/YSeBstl57mOzx9xlU9n85RGrDQg=
sigs.k8s.io/apiserver-network-proxy/konnectivity-client v0.0.15/go.mod h1:LEScyzhFmoF5pso/YSeBstl57mOzx9xlU9n85RGrDQg=
sigs.k8s.io/apiserver-network-proxy/konnectivity-client v0.31.0 h1:CPT0ExVicCzcpeN4baWEV2ko2Z/AsiZgEdwgcfwLgMo=
Expand Down
75 changes: 30 additions & 45 deletions internal/cli/kraft/cloud/instance/logs/logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import (
"github.com/spf13/cobra"

kraftcloud "sdk.kraft.cloud"
kcinstances "sdk.kraft.cloud/instances"

"kraftkit.sh/cmdfactory"
"kraftkit.sh/config"
Expand Down Expand Up @@ -133,23 +132,6 @@ func Logs(ctx context.Context, opts *LogOptions, args ...string) error {
for _, instance := range args {
instance := instance

// Start by fetching the instance.
resp, err := opts.Client.Instances().WithMetro(opts.Metro).Get(ctx, instance)
if err != nil {
// Likely there was an issue performing the request; so we'll just
// skip and attempt to retrieve more logs.
if !errors.Is(err, io.EOF) {
log.G(ctx).Error(err)
}

continue
}

inst, err := resp.FirstOrErr()
if err != nil {
errGroup = append(errGroup, err)
}

prefix := ""
if !opts.NoPrefix {
prefix = instance + strings.Repeat(" ", longestName-len(instance))
Expand All @@ -167,31 +149,6 @@ func Logs(ctx context.Context, opts *LogOptions, args ...string) error {

observations.Add(instance)

// Continuously check the state in a separate thread every 1 second.
go func() {
for {
resp, err := opts.Client.Instances().WithMetro(opts.Metro).Get(ctx, instance)
if err != nil {
// Likely there was an issue performing the request; so we'll just
// skip and attempt to retrieve more logs.
if !errors.Is(err, io.EOF) {
log.G(ctx).Error(err)
}

continue
}

inst, err = resp.FirstOrErr()
if err != nil {
errGroup = append(errGroup, err)
}

if len(observations.Items()) == 0 {
return
}
}
}()

go func() {
defer observations.Done(instance)

Expand All @@ -202,7 +159,21 @@ func Logs(ctx context.Context, opts *LogOptions, args ...string) error {
case err := <-errChan:
if err != nil {
if errors.Is(err, io.EOF) {
if inst != nil && inst.State == kcinstances.InstanceStateStopped {
if opts.Tail < 1 {
resp, err := opts.Client.Instances().WithMetro(opts.Metro).Get(ctx, instance)
if err != nil {
if !errors.Is(err, io.EOF) {
log.G(ctx).Error(err)
}

continue
}

inst, err := resp.FirstOrErr()
if err != nil {
errGroup = append(errGroup, err)
}

consumer.Consume(
"",
fmt.Sprintf("The instance has exited (%s).", inst.DescribeStopReason()),
Expand All @@ -226,7 +197,21 @@ func Logs(ctx context.Context, opts *LogOptions, args ...string) error {
if ok {
consumer.Consume(line)
} else {
if inst != nil && inst.State == kcinstances.InstanceStateStopped {
if opts.Tail < 1 {
resp, err := opts.Client.Instances().WithMetro(opts.Metro).Get(ctx, instance)
if err != nil {
if !errors.Is(err, io.EOF) {
log.G(ctx).Error(err)
}

continue
}

inst, err := resp.FirstOrErr()
if err != nil {
errGroup = append(errGroup, err)
}

consumer.Consume(
"",
fmt.Sprintf("The instance has exited (%s).", inst.DescribeStopReason()),
Expand Down
Loading