Skip to content

Commit

Permalink
chore: merge 0.8.1 fixes (#53)
Browse files Browse the repository at this point in the history
  • Loading branch information
colesnodgrass authored Jul 23, 2024
1 parent 59f418e commit 15d3efd
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 7 deletions.
31 changes: 26 additions & 5 deletions internal/cmd/local/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/airbytehq/abctl/internal/cmd/local/docker"
"github.com/airbytehq/abctl/internal/cmd/local/localerr"
"github.com/pterm/pterm"
"io"
"net"
"net/http"
"strings"
Expand Down Expand Up @@ -61,17 +62,21 @@ func portAvailable(ctx context.Context, port int) error {
return nil
}

listener, err := net.Listen("tcp", fmt.Sprintf("localhost:%d", port))
// net.Listen doesn't support providing a context
lc := &net.ListenConfig{}
listener, err := lc.Listen(ctx, "tcp", fmt.Sprintf("localhost:%d", port))
if err != nil {
pterm.Debug.Println(fmt.Sprintf("Unable to listen on port '%d': %s", port, err))

// check if an existing airbyte installation is already listening on this port
req, err := http.NewRequestWithContext(ctx, http.MethodGet, fmt.Sprintf("http://localhost:%d", port), nil)
if err != nil {
req, errInner := http.NewRequestWithContext(ctx, http.MethodGet, fmt.Sprintf("http://localhost:%d", port), nil)
if errInner != nil {
pterm.Error.Printfln("Port %d request could not be created", port)
return fmt.Errorf("%w: could not create request: %w", localerr.ErrPort, err)
}

res, err := httpClient.Do(req)
if err != nil {
res, errInner := httpClient.Do(req)
if errInner != nil {
pterm.Error.Printfln("Port %d appears to already be in use", port)
return fmt.Errorf("%w: could not send request: %w", localerr.ErrPort, err)
}
Expand All @@ -80,6 +85,22 @@ func portAvailable(ctx context.Context, port int) error {
pterm.Success.Printfln("Port %d appears to be running a previous Airbyte installation", port)
return nil
}

// if we're here, we haven't been able to determine why this port may or may not be available
body, errInner := io.ReadAll(res.Body)
if errInner != nil {
pterm.Debug.Println(fmt.Sprintf("Unable to read response body: %s", errInner))
}
pterm.Debug.Println(fmt.Sprintf(
"Unable to determine if port '%d' is in use:\n StatusCode: %d\n Body: %s",
port, res.StatusCode, body,
))

pterm.Error.Println(fmt.Sprintf(
"Unable to determine if port '%d' is available, consider specifying a different port",
port,
))
return fmt.Errorf("unable to determine if port '%d' is available: %w", port, err)
}
// if we're able to bind to the port (and then release it), it should be available
defer func() {
Expand Down
2 changes: 1 addition & 1 deletion internal/cmd/local/k8s/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ nodes:
port)

opts := []cluster.CreateOption{
cluster.CreateWithWaitForReady(120 * time.Second),
cluster.CreateWithWaitForReady(5 * time.Minute),
cluster.CreateWithKubeconfigPath(k.kubeconfig),
cluster.CreateWithNodeImage("kindest/node:" + k8sVersion),
cluster.CreateWithRawConfig([]byte(rawCfg)),
Expand Down
45 changes: 44 additions & 1 deletion internal/cmd/local/k8s/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ package k8s
import (
"fmt"
"github.com/airbytehq/abctl/internal/cmd/local/paths"
"github.com/pterm/pterm"
"os"
"path/filepath"
"sigs.k8s.io/kind/pkg/cluster"
"sigs.k8s.io/kind/pkg/log"
)

// Provider represents a k8s provider.
Expand All @@ -29,12 +31,53 @@ func (p Provider) Cluster() (Cluster, error) {
}

return &kindCluster{
p: cluster.NewProvider(),
p: cluster.NewProvider(cluster.ProviderWithLogger(&kindLogger{pterm: pterm.Debug})),
kubeconfig: p.Kubeconfig,
clusterName: p.ClusterName,
}, nil
}

var _ log.Logger = (*kindLogger)(nil)
var _ log.InfoLogger = (*kindLogger)(nil)

// kindLogger implements the k8s logger interfaces.
// Necessarily in order to capture kind specify logging for debug purposes
type kindLogger struct {
pterm pterm.PrefixPrinter
}

func (k *kindLogger) Info(message string) {
k.pterm.Println("kind - INFO: " + message)
}

func (k *kindLogger) Infof(format string, args ...interface{}) {
k.pterm.Println(fmt.Sprintf("kind - INFO: "+format, args...))
}

func (k *kindLogger) Enabled() bool {
return true
}

func (k *kindLogger) Warn(message string) {
k.pterm.Println("kind - WARN: " + message)
}

func (k *kindLogger) Warnf(format string, args ...interface{}) {
k.pterm.Println(fmt.Sprintf("kind - WARN: "+format, args...))
}

func (k *kindLogger) Error(message string) {
k.pterm.Println("kind - ERROR: " + message)
}

func (k *kindLogger) Errorf(format string, args ...interface{}) {
k.pterm.Println(fmt.Sprintf("kind - ERROR: "+format, args...))
}

func (k *kindLogger) V(level log.Level) log.InfoLogger {
return k
}

const (
Kind = "kind"
Test = "test"
Expand Down

0 comments on commit 15d3efd

Please sign in to comment.