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

Add support for local machine ID #91

Merged
merged 11 commits into from
Nov 20, 2023
18 changes: 9 additions & 9 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,19 @@ module github.com/signadot/cli
go 1.21

require (
github.com/denisbrodbeck/machineid v1.0.1
github.com/docker/go-units v0.5.0
github.com/go-openapi/runtime v0.26.0
github.com/go-openapi/strfmt v0.21.7
github.com/goccy/go-yaml v1.10.0
github.com/golang/protobuf v1.5.3
github.com/hashicorp/go-multierror v1.1.1
github.com/nsf/jsondiff v0.0.0-20210926074059-1e845ec5d249
github.com/signadot/go-sdk v0.3.8-0.20230917202415-61cf5bfada91
github.com/signadot/libconnect v0.1.1-0.20231027085512-a9f6abcdcf30
github.com/signadot/go-sdk v0.3.8-0.20231107200143-6c7fe4258298
github.com/signadot/libconnect v0.1.1-0.20231109133751-e261a2f69044
github.com/spf13/cobra v1.6.0
github.com/spf13/viper v1.11.0
github.com/theckman/yacspin v0.13.12
golang.org/x/exp v0.0.0-20230522175609-2e198f4a06a1
golang.org/x/net v0.10.0
golang.org/x/term v0.9.0
google.golang.org/grpc v1.56.0
Expand Down Expand Up @@ -43,7 +43,7 @@ require (
github.com/emicklei/go-restful/v3 v3.9.0 // indirect
github.com/fatih/color v1.15.0
github.com/fsnotify/fsnotify v1.6.0 // indirect
github.com/go-logr/logr v1.2.4 // indirect
github.com/go-logr/logr v1.3.0 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/go-openapi/analysis v0.21.4 // indirect
github.com/go-openapi/errors v0.20.4 // indirect
Expand Down Expand Up @@ -85,9 +85,9 @@ require (
github.com/spf13/pflag v1.0.5 // indirect
github.com/subosito/gotenv v1.4.2 // indirect
go.mongodb.org/mongo-driver v1.12.1 // indirect
go.opentelemetry.io/otel v1.18.0 // indirect
go.opentelemetry.io/otel/metric v1.18.0 // indirect
go.opentelemetry.io/otel/trace v1.18.0 // indirect
go.opentelemetry.io/otel v1.19.0 // indirect
go.opentelemetry.io/otel/metric v1.19.0 // indirect
go.opentelemetry.io/otel/trace v1.19.0 // indirect
golang.org/x/crypto v0.10.0 // indirect
golang.org/x/oauth2 v0.7.0 // indirect
golang.org/x/sys v0.9.0 // indirect
Expand All @@ -111,5 +111,5 @@ require (
)

// Used for local dev
//replace github.com/signadot/libconnect => ../libconnect/
//replace github.com/signadot/go-sdk => ../go-sdk
// replace github.com/signadot/libconnect => ../libconnect/
// replace github.com/signadot/go-sdk => ../go-sdk
30 changes: 14 additions & 16 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ3
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/denisbrodbeck/machineid v1.0.1 h1:geKr9qtkB876mXguW2X6TU4ZynleN6ezuMSRhl4D7AQ=
github.com/denisbrodbeck/machineid v1.0.1/go.mod h1:dJUwb7PTidGDeYyUBmXZ2GphQBbjJCrnectwCyxcUSI=
github.com/docker/go-units v0.5.0 h1:69rxXcBk27SvSaaxTtLh/8llcHD8vYHT7WSdRZ/jvr4=
github.com/docker/go-units v0.5.0/go.mod h1:fgPhTUdO+D/Jk86RDLlptpiXQzgHJF7gydDDbaIK4Dk=
github.com/docopt/docopt-go v0.0.0-20180111231733-ee0de3bc6815/go.mod h1:WwZ+bS3ebgob9U8Nd0kOddGdZWjyMGR8Wziv+TBNwSE=
Expand All @@ -103,8 +105,8 @@ github.com/go-gl/glfw/v3.3/glfw v0.0.0-20191125211704-12ad95a8df72/go.mod h1:tQ2
github.com/go-gl/glfw/v3.3/glfw v0.0.0-20200222043503-6f7a984d4dc4/go.mod h1:tQ2UAYgL5IevRw8kRxooKSPJfGvJ9fJQFa0TUsXzTg8=
github.com/go-logr/logr v1.2.0/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A=
github.com/go-logr/logr v1.2.2/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A=
github.com/go-logr/logr v1.2.4 h1:g01GSCwiDw2xSZfjJ2/T9M+S6pFdcNtFYsp+Y43HYDQ=
github.com/go-logr/logr v1.2.4/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A=
github.com/go-logr/logr v1.3.0 h1:2y3SDp0ZXuc6/cjLSZ+Q3ir+QB9T/iG5yYRXqsagWSY=
github.com/go-logr/logr v1.3.0/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY=
github.com/go-logr/stdr v1.2.2 h1:hSWxHoqTgW2S2qGc0LTAI563KZ5YKYRhT3MFKZMbjag=
github.com/go-logr/stdr v1.2.2/go.mod h1:mMo/vtBO5dYbehREoey6XUKy/eSumjCCveDpRre4VKE=
github.com/go-openapi/analysis v0.21.2/go.mod h1:HZwRk4RRisyG8vx2Oe6aqeSQcoxRp47Xkp3+K6q+LdY=
Expand Down Expand Up @@ -360,12 +362,10 @@ github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFR
github.com/rogpeppe/go-internal v1.11.0 h1:cWPaGQEPrBb5/AsnsZesgZZ9yb1OQ+GOISoDNXVBh4M=
github.com/rogpeppe/go-internal v1.11.0/go.mod h1:ddIwULY96R17DhadqLgMfk9H9tvdUzkipdSkR5nkCZA=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/signadot/go-sdk v0.3.8-0.20230917202415-61cf5bfada91 h1:DVxA0GXvgVmrlpUo48xd0SDbcXHLO+KkUHkUFbWfSwg=
github.com/signadot/go-sdk v0.3.8-0.20230917202415-61cf5bfada91/go.mod h1:X86A7rI8J//a2T8aKVCCVXxOMBjh32c2GJ50WK7iYzs=
github.com/signadot/libconnect v0.1.1-0.20231011194636-a0941512a00a h1:o+GNGQP7MKm/wX6S/rXoCMVyjSHGVcCtROMpiQLJnJk=
github.com/signadot/libconnect v0.1.1-0.20231011194636-a0941512a00a/go.mod h1:LIU36XBqFs+GlaE05UIJxI7+O/MHISAAMGupQdTAEUE=
github.com/signadot/libconnect v0.1.1-0.20231027085512-a9f6abcdcf30 h1:tmnSWg79tDjV0sU+nU7Iiv91Rc66Dq4hsj/O4EKqFMo=
github.com/signadot/libconnect v0.1.1-0.20231027085512-a9f6abcdcf30/go.mod h1:ZX3YLJ5zYTxaVkeOAMsufYQZTOEwgFgAb2w1hZWIS3s=
github.com/signadot/go-sdk v0.3.8-0.20231107200143-6c7fe4258298 h1:zNEYJUQEbYMpEkFekq8wDjC6atbKRxPHIrC5UT6pE/A=
github.com/signadot/go-sdk v0.3.8-0.20231107200143-6c7fe4258298/go.mod h1:mLzoMuSE0GsFLONRjjYYHQBsU9twXML1BrExSvpMqeI=
github.com/signadot/libconnect v0.1.1-0.20231109133751-e261a2f69044 h1:sl4y7UE8aar1gud3yCZUfMyUHeLkstNXJ2MRnoX7jBI=
github.com/signadot/libconnect v0.1.1-0.20231109133751-e261a2f69044/go.mod h1:4OzWoyxyoh56h4jgCw/FBJVxyHB45qpwHDVWbsGfFTg=
github.com/sirupsen/logrus v1.4.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo=
github.com/sirupsen/logrus v1.4.1/go.mod h1:ni0Sbl8bgC9z8RoU9G6nDWqqs/fq4eDPysMBDgk/93Q=
github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE=
Expand Down Expand Up @@ -429,14 +429,14 @@ go.opencensus.io v0.22.2/go.mod h1:yxeiOL68Rb0Xd1ddK5vPZ/oVn4vY4Ynel7k9FzqtOIw=
go.opencensus.io v0.22.3/go.mod h1:yxeiOL68Rb0Xd1ddK5vPZ/oVn4vY4Ynel7k9FzqtOIw=
go.opencensus.io v0.22.4/go.mod h1:yxeiOL68Rb0Xd1ddK5vPZ/oVn4vY4Ynel7k9FzqtOIw=
go.opencensus.io v0.22.5/go.mod h1:5pWMHQbX5EPX2/62yrJeAkowc+lfs/XD7Uxpq3pI6kk=
go.opentelemetry.io/otel v1.18.0 h1:TgVozPGZ01nHyDZxK5WGPFB9QexeTMXEH7+tIClWfzs=
go.opentelemetry.io/otel v1.18.0/go.mod h1:9lWqYO0Db579XzVuCKFNPDl4s73Voa+zEck3wHaAYQI=
go.opentelemetry.io/otel/metric v1.18.0 h1:JwVzw94UYmbx3ej++CwLUQZxEODDj/pOuTCvzhtRrSQ=
go.opentelemetry.io/otel/metric v1.18.0/go.mod h1:nNSpsVDjWGfb7chbRLUNW+PBNdcSTHD4Uu5pfFMOI0k=
go.opentelemetry.io/otel v1.19.0 h1:MuS/TNf4/j4IXsZuJegVzI1cwut7Qc00344rgH7p8bs=
go.opentelemetry.io/otel v1.19.0/go.mod h1:i0QyjOq3UPoTzff0PJB2N66fb4S0+rSbSB15/oyH9fY=
go.opentelemetry.io/otel/metric v1.19.0 h1:aTzpGtV0ar9wlV4Sna9sdJyII5jTVJEvKETPiOKwvpE=
go.opentelemetry.io/otel/metric v1.19.0/go.mod h1:L5rUsV9kM1IxCj1MmSdS+JQAcVm319EUrDVLrt7jqt8=
go.opentelemetry.io/otel/sdk v1.14.0 h1:PDCppFRDq8A1jL9v6KMI6dYesaq+DFcDZvjsoGvxGzY=
go.opentelemetry.io/otel/sdk v1.14.0/go.mod h1:bwIC5TjrNG6QDCHNWvW4HLHtUQ4I+VQDsnjhvyZCALM=
go.opentelemetry.io/otel/trace v1.18.0 h1:NY+czwbHbmndxojTEKiSMHkG2ClNH2PwmcHrdo0JY10=
go.opentelemetry.io/otel/trace v1.18.0/go.mod h1:T2+SGJGuYZY3bjj5rgh/hN7KIrlpWC5nS8Mjvzckz+0=
go.opentelemetry.io/otel/trace v1.19.0 h1:DFVQmlVbfVeOuBRrwdtaehRrWiL1JoVs9CPIQ1Dzxpg=
go.opentelemetry.io/otel/trace v1.19.0/go.mod h1:mfaSyvGyEJEI0nyV2I4qhNQnbBOUUmYZpYojqMnX2vo=
golang.org/x/crypto v0.0.0-20180904163835-0709b304e793/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20190422162423-af44ce270edf/go.mod h1:WFFai1msRO1wXaEeE5yQxYXgSfI8pQAWXbQop6sCtWE=
Expand All @@ -462,8 +462,6 @@ golang.org/x/exp v0.0.0-20191227195350-da58074b4299/go.mod h1:2RIsYlXP63K8oxa1u0
golang.org/x/exp v0.0.0-20200119233911-0405dc783f0a/go.mod h1:2RIsYlXP63K8oxa1u096TMicItID8zy7Y6sNkU49FU4=
golang.org/x/exp v0.0.0-20200207192155-f17229e696bd/go.mod h1:J/WKrq2StrnmMY6+EHIKF9dgMWnmCNThgcyBT1FY9mM=
golang.org/x/exp v0.0.0-20200224162631-6cc2880d07d6/go.mod h1:3jZMyOhIsHpP37uCMkUooju7aAi5cS1Q23tOzKc+0MU=
golang.org/x/exp v0.0.0-20230522175609-2e198f4a06a1 h1:k/i9J1pBpvlfR+9QsetwPyERsqu1GIbi967PQMq3Ivc=
golang.org/x/exp v0.0.0-20230522175609-2e198f4a06a1/go.mod h1:V1LtkGg67GoY2N1AnLN78QLrzxkLyJw7RJb1gzOOz9w=
golang.org/x/image v0.0.0-20190227222117-0694c2d4d067/go.mod h1:kZ7UVZpmo3dzQBMxlp+ypCbDeSB+sBbTgSJuh5dn5js=
golang.org/x/image v0.0.0-20190802002840-cff245a6509b/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0=
golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE=
Expand Down
11 changes: 5 additions & 6 deletions internal/command/local/connect.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,15 @@ import (
"path/filepath"
"time"

"log/slog"

"github.com/fatih/color"
"github.com/signadot/cli/internal/config"
sbmapi "github.com/signadot/cli/internal/locald/api/sandboxmanager"
sbmgr "github.com/signadot/cli/internal/locald/sandboxmanager"
"github.com/signadot/cli/internal/utils/system"
"github.com/signadot/libconnect/common/processes"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"log/slog"
"sigs.k8s.io/yaml"
)

Expand Down Expand Up @@ -87,8 +88,6 @@ func runConnect(cmd *cobra.Command, out io.Writer, cfg *config.LocalConnect, arg
Username: user.Username,
},
ConnectionConfig: connConfig,
API: cfg.API,
APIKey: viper.GetString("api_key"),
Debug: cfg.LocalConfig.Debug,
}
if cfg.DumpCIConfig {
Expand Down Expand Up @@ -192,7 +191,7 @@ func waitConnect(localConfig *config.LocalConnect, out io.Writer) error {
)
defer ticker.Stop()
for {
status, err = getStatus()
status, err = sbmgr.GetStatus()
if err != nil {
fmt.Fprintf(out, "error getting status: %s", err.Error())
connectErrs = []error{err}
Expand All @@ -204,7 +203,7 @@ func waitConnect(localConfig *config.LocalConnect, out io.Writer) error {
goto tick

}
connectErrs = checkLocalStatusConnectErrors(ciConfig, status)
connectErrs = sbmgr.CheckStatusConnectErrors(status, ciConfig)
if len(connectErrs) == 0 {
break
}
Expand Down
84 changes: 4 additions & 80 deletions internal/command/local/printers.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/signadot/cli/internal/config"
commonapi "github.com/signadot/cli/internal/locald/api"
sbmapi "github.com/signadot/cli/internal/locald/api/sandboxmanager"
sbmgr "github.com/signadot/cli/internal/locald/sandboxmanager"
connectcfg "github.com/signadot/libconnect/config"
)

Expand All @@ -16,7 +17,7 @@ func printRawStatus(cfg *config.LocalStatus, out io.Writer, printer func(out io.
// unmarshal the ci config
ciConfig, err := sbmapi.ToCIConfig(status.CiConfig)
if err != nil {
return fmt.Errorf("couldn't unmarshal ci-config from sandbox manager status, %v", err)
return fmt.Errorf("couldn't unmarshal ci-config from sandboxmanager status, %v", err)
}

// convert the status into a map (useful to convert snake-case fields to camel-case,
Expand Down Expand Up @@ -71,7 +72,6 @@ func getRawRuntimeConfig(cfg *config.LocalStatus, ciConfig *config.ConnectInvoca
ConfigDir string `json:"configDir"`
User *PrintableUser `json:"user"`
ConnectionConfig *connectcfg.ConnectionConfig `json:"connectionConfig"`
API *PrintableAPI `json:"api"`
Debug bool `json:"debug"`
}

Expand All @@ -87,13 +87,7 @@ func getRawRuntimeConfig(cfg *config.LocalStatus, ciConfig *config.ConnectInvoca
UIDHome: ciConfig.User.UIDHome,
},
ConnectionConfig: ciConfig.ConnectionConfig,
API: &PrintableAPI{
ConfigFile: ciConfig.API.ConfigFile,
Org: ciConfig.API.Org,
MaskedAPIKey: ciConfig.API.MaskedAPIKey,
APIURL: ciConfig.API.APIURL,
},
Debug: ciConfig.Debug,
Debug: ciConfig.Debug,
}
} else {
// Standard view
Expand Down Expand Up @@ -237,37 +231,12 @@ func getRawPortforward(cfg *config.LocalStatus, ciConfig *config.ConnectInvocati
return result
}

func checkLocalStatusConnectErrors(ciConfig *config.ConnectInvocationConfig, status *sbmapi.StatusResponse) []error {
var errs []error
// check port forward status
if ciConfig.ConnectionConfig.Type == connectcfg.PortForwardLinkType {
err := checkPortforwardStatus(status.Portforward)
if err != nil {
errs = append(errs, err)
}
}
// check root manager (if running)
if ciConfig.WithRootManager {
// check localnet service
err := checkLocalNetStatus(status.Localnet)
if err != nil {
errs = append(errs, err)
}
// check hosts service
err = checkHostsStatus(status.Hosts)
if err != nil {
errs = append(errs, err)
}
}
return errs
}

func printLocalStatus(cfg *config.LocalStatus, out io.Writer, status *sbmapi.StatusResponse) error {
ciConfig, err := sbmapi.ToCIConfig(status.CiConfig)
if err != nil {
return fmt.Errorf("couldn't unmarshal ci-config from sandbox manager status, %v", err)
}
connectErrs := checkLocalStatusConnectErrors(ciConfig, status)
connectErrs := sbmgr.CheckStatusConnectErrors(status, ciConfig)

// create a printer
printer := statusPrinter{
Expand All @@ -290,51 +259,6 @@ func printLocalStatus(cfg *config.LocalStatus, out io.Writer, status *sbmapi.Sta
return nil
}

func checkPortforwardStatus(portforward *commonapi.PortForwardStatus) error {
errorMsg := "failed to establish port-forward"
if portforward != nil {
if portforward.Health != nil {
if portforward.Health.Healthy {
return nil
}
if portforward.Health.LastErrorReason != "" {
errorMsg += fmt.Sprintf(" (%q)", portforward.Health.LastErrorReason)
}
}
}
return fmt.Errorf(errorMsg)
}

func checkLocalNetStatus(localnet *commonapi.LocalNetStatus) error {
errorMsg := "failed to setup localnet"
if localnet != nil {
if localnet.Health != nil {
if localnet.Health.Healthy {
return nil
}
if localnet.Health.LastErrorReason != "" {
errorMsg += fmt.Sprintf(" (%q)", localnet.Health.LastErrorReason)
}
}
}
return fmt.Errorf(errorMsg)
}

func checkHostsStatus(hosts *commonapi.HostsStatus) error {
errorMsg := "failed to configure hosts in /etc/hosts"
if hosts != nil {
if hosts.Health != nil {
if hosts.Health.Healthy {
return nil
}
if hosts.Health.LastErrorReason != "" {
errorMsg += fmt.Sprintf(" (%q)", hosts.Health.LastErrorReason)
}
}
}
return fmt.Errorf(errorMsg)
}

type statusPrinter struct {
cfg *config.LocalStatus
status *sbmapi.StatusResponse
Expand Down
27 changes: 4 additions & 23 deletions internal/command/local/status.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,16 @@
package local

import (
"context"
"fmt"
"io"
"path/filepath"

"github.com/signadot/cli/internal/config"
sbmapi "github.com/signadot/cli/internal/locald/api/sandboxmanager"
sbmgr "github.com/signadot/cli/internal/locald/sandboxmanager"
"github.com/signadot/cli/internal/print"
"github.com/signadot/cli/internal/utils/system"
"github.com/signadot/libconnect/common/processes"
"github.com/spf13/cobra"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
)

func newStatus(localConfig *config.Local) *cobra.Command {
Expand Down Expand Up @@ -50,10 +47,11 @@ func runStatus(cfg *config.LocalStatus, out io.Writer, args []string) error {
if !isRunning {
return fmt.Errorf("signadot is not connected\n")
}
status, err := getStatus()

// Get the status from sandbox manager
status, err := sbmgr.GetStatus()
if err != nil {
return fmt.Errorf("couldn't get status from sandbox manager api: %w", err)
return err
}

switch cfg.OutputFormat {
Expand All @@ -67,20 +65,3 @@ func runStatus(cfg *config.LocalStatus, out io.Writer, args []string) error {
return fmt.Errorf("unsupported output format: %q", cfg.OutputFormat)
}
}

func getStatus() (*sbmapi.StatusResponse, error) {
// Get a sandbox manager API client
grpcConn, err := grpc.Dial("127.0.0.1:6666", grpc.WithTransportCredentials(insecure.NewCredentials()))
if err != nil {
return nil, fmt.Errorf("couldn't connect sandbox manager api: %w", err)
}
defer grpcConn.Close()

// get the status
sbManagerClient := sbmapi.NewSandboxManagerAPIClient(grpcConn)
status, err := sbManagerClient.Status(context.Background(), &sbmapi.StatusRequest{})
if err != nil {
return nil, fmt.Errorf("couldn't get status from sandbox manager api: %w", err)
}
return status, nil
}
Loading