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

fix(tailscale): remove unused version checks #299

Merged
merged 1 commit into from
Jan 21, 2025
Merged
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
58 changes: 6 additions & 52 deletions components/tailscale/component.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,79 +3,36 @@ package tailscale

import (
"context"
"fmt"
"time"

"github.com/leptonai/gpud/components"
"github.com/leptonai/gpud/components/query"
tailscale_id "github.com/leptonai/gpud/components/tailscale/id"
"github.com/leptonai/gpud/log"
)

func New(ctx context.Context, cfg Config) components.Component {
cfg.Query.SetDefaultsIfNotSet()
setDefaultPoller(cfg)

cctx, ccancel := context.WithCancel(ctx)
getDefaultPoller().Start(cctx, cfg.Query, tailscale_id.Name)

return &component{
rootCtx: ctx,
cancel: ccancel,
poller: getDefaultPoller(),
}
}

var _ components.Component = (*component)(nil)

type component struct {
rootCtx context.Context
cancel context.CancelFunc
poller query.Poller
}

func (c *component) Name() string { return tailscale_id.Name }

func (c *component) States(ctx context.Context) ([]components.State, error) {
last, err := c.poller.Last()
if err == query.ErrNoData { // no data
log.Logger.Debugw("nothing found in last state (no data collected yet)", "component", tailscale_id.Name)
return []components.State{
{
Name: tailscale_id.Name,
Healthy: true,
Reason: query.ErrNoData.Error(),
},
}, nil
}
if err != nil {
return nil, err
}
if last.Error != nil {
return []components.State{
{
Name: tailscale_id.Name,
Healthy: false,
Error: last.Error.Error(),
Reason: "last query failed",
},
}, nil
}
if last.Output == nil {
return []components.State{
{
Name: tailscale_id.Name,
Healthy: true,
Reason: "no output",
},
}, nil
}

output, ok := last.Output.(*Output)
if !ok {
return nil, fmt.Errorf("invalid output type: %T", last.Output)
}
return output.States()
return []components.State{
{
Name: tailscale_id.Name,
Healthy: true,
},
}, nil
}

func (c *component) Events(ctx context.Context, since time.Time) ([]components.Event, error) {
Expand All @@ -91,8 +48,5 @@ func (c *component) Metrics(ctx context.Context, since time.Time) ([]components.
func (c *component) Close() error {
log.Logger.Debugw("closing component")

// safe to call stop multiple times
c.poller.Stop(tailscale_id.Name)

return nil
}
133 changes: 0 additions & 133 deletions components/tailscale/component_output.go

This file was deleted.

40 changes: 0 additions & 40 deletions components/tailscale/tailscale.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
package tailscale

import (
"context"
"encoding/json"
"fmt"
"os/exec"
"time"
)

func TailscaleExists() bool {
Expand All @@ -15,39 +11,3 @@ func TailscaleExists() bool {
}
return p != ""
}

// CheckVersion returns the tailscale version by running `tailscale version --json`.
func CheckVersion() (*VersionInfo, error) {
p, err := exec.LookPath("tailscale")
if err != nil {
return nil, fmt.Errorf("tailscale version check requires tailscale (%w)", err)
}

ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
b, err := exec.CommandContext(ctx, p, "version", "--json").CombinedOutput()
cancel()
if err != nil {
return nil, err
}

return ParseVersion(b)
}

// VersionInfo represents the JSON structure of the Tailscale version information.
type VersionInfo struct {
MajorMinorPatch string `json:"majorMinorPatch"`
Short string `json:"short"`
Long string `json:"long"`
UnstableBranch bool `json:"unstableBranch"`
Cap int `json:"cap"`
}

// ParseVersion parses the JSON-encoded version information from a byte slice.
func ParseVersion(b []byte) (*VersionInfo, error) {
var v VersionInfo
err := json.Unmarshal(b, &v)
if err != nil {
return nil, fmt.Errorf("error parsing version info: %w", err)
}
return &v, nil
}
55 changes: 0 additions & 55 deletions components/tailscale/tailscale_test.go

This file was deleted.

Loading