diff --git a/cmd/kom/main.go b/cmd/kom/main.go index bbfcbbc..373b433 100644 --- a/cmd/kom/main.go +++ b/cmd/kom/main.go @@ -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) } } diff --git a/pkg/cmd/root.go b/pkg/cmd/root.go index 58c65ec..7aa204a 100644 --- a/pkg/cmd/root.go +++ b/pkg/cmd/root.go @@ -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() }, diff --git a/pkg/provisioner/cluster.go b/pkg/provisioner/cluster.go index 18d3a1f..1bf321c 100644 --- a/pkg/provisioner/cluster.go +++ b/pkg/provisioner/cluster.go @@ -2,6 +2,7 @@ package provisioner import ( "fmt" + "log/slog" "os" "github.com/goccy/go-yaml" @@ -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 @@ -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{ @@ -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 @@ -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{ @@ -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