Skip to content

Commit

Permalink
fix: remove kubeconfig state map
Browse files Browse the repository at this point in the history
- only write kubeconfig once at cluster creation
- lookup kubeconfig path using known pattern

fix: fetch harbor cert only once
  • Loading branch information
yusufhm committed May 8, 2022
1 parent 48552f5 commit 709519c
Show file tree
Hide file tree
Showing 8 changed files with 25 additions and 21 deletions.
1 change: 0 additions & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ var r = rockpool.Rockpool{
Clusters: rockpool.ClusterList{},
BinaryPaths: sync.Map{},
HelmReleases: sync.Map{},
Kubeconfig: sync.Map{},
},
Config: rockpool.Config{},
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/rockpool/helm.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
)

func (r *Rockpool) Helm(cn string, ns string, args ...string) *exec.Cmd {
cmd := exec.Command("helm", "--kubeconfig", r.MapStringGet(&r.State.Kubeconfig, cn))
cmd := exec.Command("helm", "--kubeconfig", r.Kubeconfig(cn))
if ns != "" {
cmd.Args = append(cmd.Args, "--namespace", ns)
}
Expand Down
11 changes: 5 additions & 6 deletions pkg/rockpool/k3d.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"fmt"
"os"
"os/exec"
"strings"

"github.com/yusufhm/rockpool/internal"
)
Expand Down Expand Up @@ -115,6 +114,7 @@ func (r *Rockpool) CreateCluster(cn string) {
os.Exit(1)
}
fmt.Printf("[%s] created cluster\n", cn)
r.WriteKubeConfig(r.ControllerClusterName())
}

func (r *Rockpool) StartCluster(cn string) {
Expand Down Expand Up @@ -173,13 +173,12 @@ func (r *Rockpool) DeleteCluster(cn string) {
fmt.Printf("[%s] deleted cluster\n", cn)
}

func (r *Rockpool) GetClusterKubeConfigPath(cn string) {
out, err := exec.Command(r.GetBinaryPath("k3d"), "kubeconfig", "write", cn).CombinedOutput()
func (r *Rockpool) WriteKubeConfig(cn string) {
fmt.Printf("[%s] writing kubeconfig\n", cn)
_, err := exec.Command(r.GetBinaryPath("k3d"), "kubeconfig", "write", cn).CombinedOutput()
if err != nil {
fmt.Println(string(out))
fmt.Printf("[%s] unable to get kubeconfig: %s\n", cn, err)
panic(fmt.Sprintln("unable to write kubeconfig:", err))
}
r.State.Kubeconfig.Store(cn, strings.Trim(string(out), "\n"))
}

func (r *Rockpool) ClusterVersion(cn string) {
Expand Down
2 changes: 1 addition & 1 deletion pkg/rockpool/kube.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
)

func (r *Rockpool) KubeCtl(cn string, ns string, args ...string) *exec.Cmd {
cmd := exec.Command("kubectl", "--kubeconfig", r.MapStringGet(&r.State.Kubeconfig, cn))
cmd := exec.Command("kubectl", "--kubeconfig", r.Kubeconfig(cn))
if ns != "" {
cmd.Args = append(cmd.Args, "--namespace", ns)
}
Expand Down
10 changes: 5 additions & 5 deletions pkg/rockpool/lagoon-components.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func (r *Rockpool) InstallHarbor() {
}
}

func (r *Rockpool) FetchHarborCerts() (string, string) {
func (r *Rockpool) FetchHarborCerts() {
cn := r.ControllerClusterName()
certBytes, _ := r.KubeGetSecret(cn, "harbor", "harbor-harbor-ingress", "")
certData := struct {
Expand Down Expand Up @@ -82,7 +82,8 @@ func (r *Rockpool) FetchHarborCerts() (string, string) {
}
fmt.Printf("[%s] generated harbor ca.crt at %s\n", cn, caCrtFile)

return secretManifest, caCrtFile
r.State.HarborSecretManifest = secretManifest
r.State.HarborCaCrtFile = caCrtFile
}

func (r *Rockpool) InstallHarborCerts(cn string) {
Expand All @@ -96,8 +97,7 @@ func (r *Rockpool) InstallHarborCerts(cn string) {
return
}

secretManifest, caCrtFile := r.FetchHarborCerts()
if _, err := r.KubeApply(cn, "lagoon", secretManifest, true); err != nil {
if _, err := r.KubeApply(cn, "lagoon", r.State.HarborSecretManifest, true); err != nil {
fmt.Printf("[%s] error creating ca.crt: %s\n", cn, err)
os.Exit(1)
}
Expand All @@ -116,7 +116,7 @@ func (r *Rockpool) InstallHarborCerts(cn string) {

// Add harbor's ca.crt to the target.
destCaCrt := fmt.Sprintf("%s:/etc/ssl/certs/harbor-cert.crt", n.Name)
_, err := r.DockerCp(caCrtFile, destCaCrt)
_, err := r.DockerCp(r.State.HarborCaCrtFile, destCaCrt)
if err != nil {
fmt.Printf("[%s] error copying ca.crt: %s\n", cn, internal.GetCmdStdErr(err))
os.Exit(1)
Expand Down
4 changes: 1 addition & 3 deletions pkg/rockpool/rockpool.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ func (r *Rockpool) Up(clusters []string) {
r.GetLagoonApiClient()
r.LagoonApiGetRemotes()
if len(setupTargets) > 0 {
r.FetchHarborCerts()
for _, c := range setupTargets {
r.WgAdd(1)
go r.SetupLagoonTarget(c)
Expand Down Expand Up @@ -108,8 +109,6 @@ func (r *Rockpool) CreateClusters(clusters []string) {
}

func (r *Rockpool) SetupLagoonController() {
r.GetClusterKubeConfigPath(r.ControllerClusterName())

r.InstallMailHog()

r.HelmList(r.ControllerClusterName())
Expand All @@ -134,7 +133,6 @@ func (r *Rockpool) SetupLagoonController() {

func (r *Rockpool) SetupLagoonTarget(cn string) {
defer r.WgDone()
r.GetClusterKubeConfigPath(cn)

r.HelmList(cn)
r.ConfigureTargetCoreDNS(cn)
Expand Down
8 changes: 8 additions & 0 deletions pkg/rockpool/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,14 @@ func (r *Rockpool) VerifyReqs(failOnMissing bool) {
}
}

func (r *Rockpool) Kubeconfig(cn string) string {
home, err := os.UserHomeDir()
if err != nil {
panic(fmt.Sprintln("unable to get user home directory:", err))
}
return fmt.Sprintf("%s/.k3d/kubeconfig-%s.yaml", home, cn)
}

func (r *Rockpool) MapStringGet(m *sync.Map, key string) string {
valueIfc, ok := m.Load(key)
if !ok {
Expand Down
8 changes: 4 additions & 4 deletions pkg/rockpool/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,10 @@ type State struct {
// See https://stackoverflow.com/a/45585833/351590.
BinaryPaths syncmap.Map
// List of Helm releases per cluster.
HelmReleases syncmap.Map
// Kubeconfig per cluster.
Kubeconfig syncmap.Map
Remotes []Remote
HelmReleases syncmap.Map
Remotes []Remote
HarborSecretManifest string
HarborCaCrtFile string
}

type Config struct {
Expand Down

0 comments on commit 709519c

Please sign in to comment.