Skip to content

Commit

Permalink
add slog to improve logging
Browse files Browse the repository at this point in the history
  • Loading branch information
ryota-sakamoto committed Jun 15, 2024
1 parent 3726eba commit 4b03833
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 17 deletions.
21 changes: 19 additions & 2 deletions cmd/kom/main.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,26 @@
package main

import "github.com/ryota-sakamoto/kubernetes-on-multipass/pkg/cmd"
import (
"log/slog"
"os"

"github.com/ryota-sakamoto/kubernetes-on-multipass/pkg/cmd"
)

func init() {
logLevel := slog.LevelInfo
if l, b := os.LookupEnv("LOG_LEVEL"); b {
logLevel.UnmarshalText([]byte(l))
}

slog.SetDefault(slog.New(slog.NewJSONHandler(os.Stdout, &slog.HandlerOptions{
Level: logLevel,
})))
}

func main() {
if err := cmd.Execute(); err != nil {
panic(err)
slog.Error(err.Error())
os.Exit(1)
}
}
6 changes: 4 additions & 2 deletions pkg/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ import (
)

var rootCmd = &cobra.Command{
Use: "kom",
Short: "kom is a CLI tool to deploy Kubernetes on Multipass",
Use: "kom",
Short: "kom is a CLI tool to deploy Kubernetes on Multipass",
SilenceUsage: true,
SilenceErrors: true,
Run: func(cmd *cobra.Command, args []string) {
cmd.Help()
},
Expand Down
34 changes: 21 additions & 13 deletions pkg/provisioner/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package provisioner

import (
"fmt"
"log/slog"
"os"

"github.com/goccy/go-yaml"
Expand All @@ -25,7 +26,7 @@ type Config struct {
func CreateMaster(config Config) error {
instance, err := multipass.GetInstance(config.Name)
if err != nil {
return err
return fmt.Errorf("failed to get instance: %w", err)
}
if instance != nil {
return nil
Expand All @@ -36,7 +37,7 @@ func CreateMaster(config Config) error {
"Arch": "amd64",
})
if err != nil {
return err
return fmt.Errorf("failed to generate cloud-init template: %w", err)
}

return multipass.LaunchInstance(multipass.InstanceConfig{
Expand All @@ -51,7 +52,7 @@ func CreateMaster(config Config) error {
func CreateWorker(config Config) error {
instance, err := multipass.GetInstance(config.Name)
if err != nil {
return err
return fmt.Errorf("failed to get instance: %w", err)
}
if instance != nil {
return nil
Expand All @@ -62,7 +63,7 @@ func CreateWorker(config Config) error {
"Arch": "amd64",
})
if err != nil {
return err
return fmt.Errorf("failed to generate cloud-init template: %w", err)
}

return multipass.LaunchInstance(multipass.InstanceConfig{
Expand All @@ -75,52 +76,59 @@ func CreateWorker(config Config) error {
}

func GenerateKubeconfig(name string) error {
slog.Debug("generate kubeconfig", slog.String("name", name))

instance, err := multipass.GetInstance(name)
if err != nil {
return err
return fmt.Errorf("failed to get instance: %w", err)
}

slog.Debug("get instance", slog.Any("instance", instance))
if instance == nil {
return fmt.Errorf("instance not found: %s", name)
}

err = multipass.Exec(name, "/opt/csr.sh")
if err != nil {
return err
return fmt.Errorf("failed to execute csr.sh: %w", err)
}

tempDir, err := os.MkdirTemp("", "kom")
if err != nil {
return err
return fmt.Errorf("failed to create temporary directory: %w", err)
}
defer os.RemoveAll(tempDir)

err = multipass.Transfer(name, "/home/ubuntu/.kube/config", tempDir)
if err != nil {
return err
return fmt.Errorf("failed to transfer kubeconfig file: %w", err)
}

kubeDir := os.Getenv("HOME") + "/.kube"
err = os.MkdirAll(kubeDir, 0755)
if err != nil {
return err
return fmt.Errorf("failed to create .kube directory: %w", err)
}

mergedConfig, err := kubernetes.MergeKubeconfig([]string{kubeDir + "/config", tempDir + "/config"})
if err != nil {
return err
return fmt.Errorf("failed to merge kubeconfig files: %w", err)
}

mergedConfig.Clusters["kubernetes"].Server = fmt.Sprintf("https://%s:6443", instance.Ipv4[0])

json, err := runtime.Encode(clientcmdlatest.Codec, mergedConfig)
if err != nil {
return nil
return fmt.Errorf("failed to encode kubeconfig to JSON: %w", err)
}
output, err := yaml.JSONToYAML(json)
if err != nil {
fmt.Printf("Unexpected error: %v", err)
return fmt.Errorf("failed to convert JSON to YAML: %w", err)
}

err = os.WriteFile(kubeDir+"/config", output, 0644)
if err != nil {
return err
return fmt.Errorf("failed to write kubeconfig file: %w", err)
}

return nil
Expand Down

0 comments on commit 4b03833

Please sign in to comment.