Skip to content

Commit

Permalink
fix error handling for k8s api fix kubeconfig path
Browse files Browse the repository at this point in the history
  • Loading branch information
jarededwards committed Nov 19, 2024
1 parent a5bff01 commit cba930f
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 14 deletions.
7 changes: 3 additions & 4 deletions cmd/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ func getInitCommand() *cobra.Command {

log.Info("colony api key provided is valid")

//!
homeDir, err := os.UserHomeDir()
if err != nil {
return fmt.Errorf("error getting user home directory: %w", err)
Expand Down Expand Up @@ -87,15 +86,14 @@ func getInitCommand() *cobra.Command {
if err != nil {
return fmt.Errorf("error executing template: %w", err)
}
//!

dockerCLI, err := docker.New(log)
if err != nil {
return fmt.Errorf("error creating docker client: %w", err)
}
defer dockerCLI.Close()

err = dockerCLI.CreateColonyK3sContainer(ctx, colonyK3sBootstrapPath, colonyKubeconfigPath)
err = dockerCLI.CreateColonyK3sContainer(ctx, colonyK3sBootstrapPath, colonyKubeconfigPath, homeDir)
if err != nil {
return fmt.Errorf("error creating container: %w", err)
}
Expand Down Expand Up @@ -137,7 +135,7 @@ func getInitCommand() *cobra.Command {
return fmt.Errorf("error creating secret: %w", err)
}

k8sconfig, err := os.ReadFile(constants.KubeconfigHostPath)
k8sconfig, err := os.ReadFile(colonyKubeconfigPath)
if err != nil {
return fmt.Errorf("error reading file: %w", err)
}
Expand Down Expand Up @@ -272,6 +270,7 @@ func getInitCommand() *cobra.Command {
if err != nil {
return fmt.Errorf("error patching ClusterRole: %w", err)
}
log.Info("colony init completed successfully")

return nil
},
Expand Down
12 changes: 4 additions & 8 deletions internal/docker/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"io"
"os"
"path/filepath"
"strings"
"time"

Expand Down Expand Up @@ -88,7 +89,7 @@ func (c *Client) RemoveColonyK3sContainer(ctx context.Context) error {
return nil
}

func (c *Client) CreateColonyK3sContainer(ctx context.Context, colonyK3sBootstrapPath, colonyKubeconfigPath string) error {
func (c *Client) CreateColonyK3sContainer(ctx context.Context, colonyK3sBootstrapPath, colonyKubeconfigPath, homeDir string) error {
log := logger.New(logger.Debug)

// check for an existing colony-k3s container
Expand Down Expand Up @@ -124,14 +125,9 @@ func (c *Client) CreateColonyK3sContainer(ctx context.Context, colonyK3sBootstra
mounts := []mount.Mount{
{
Type: mount.TypeBind,
Source: colonyKubeconfigPath,
Target: "/output",
Source: filepath.Join(homeDir, constants.ColonyDir),
Target: filepath.Join(homeDir, constants.ColonyDir),
},
// {
// Type: mount.TypeVolume,
// Source: "k3s-server",
// Target: "/var/lib/rancher/k3s",
// },
{
Type: mount.TypeBind,
Source: colonyK3sBootstrapPath,
Expand Down
8 changes: 6 additions & 2 deletions internal/k8s/k8s.go
Original file line number Diff line number Diff line change
Expand Up @@ -338,11 +338,15 @@ func (c *Client) WaitForKubernetesAPIHealthy(ctx context.Context, timeout time.D
_, err := c.clientSet.Discovery().ServerVersion()
if err != nil {
if isNetworkingError(err) {
c.logger.Warn("connection to kube-apiserver error, retrying: %s", err)
c.logger.Warnf("connection to kube-apiserver error, retrying: %s", err)
return false, nil
}
if k8sErrors.IsServiceUnavailable(err) || k8sErrors.IsTimeout(err) {
c.logger.Warnf("service unavailable or timeout error, retrying: %s", err)
return false, nil
}

return false, nil
return false, fmt.Errorf("error getting server version: %w", err)
}

return true, nil
Expand Down

0 comments on commit cba930f

Please sign in to comment.