Skip to content

Commit

Permalink
fix(tailscale): remove unused version checks
Browse files Browse the repository at this point in the history
Signed-off-by: Gyuho Lee <[email protected]>
  • Loading branch information
gyuho committed Jan 21, 2025
1 parent 8e9d625 commit 58950d6
Show file tree
Hide file tree
Showing 4 changed files with 6 additions and 280 deletions.
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.

0 comments on commit 58950d6

Please sign in to comment.