112 lines
2.5 KiB
Go
112 lines
2.5 KiB
Go
package create
|
|
|
|
import (
|
|
"bytes"
|
|
"fmt"
|
|
"github.com/spf13/cobra"
|
|
"k8s.io/cli-runtime/pkg/genericclioptions"
|
|
"os"
|
|
|
|
render "example.com/monok8s/pkg/render"
|
|
)
|
|
|
|
func NewCmdCreate(flags *genericclioptions.ConfigFlags) *cobra.Command {
|
|
cmd := &cobra.Command{Use: "create", Short: "Create starter resources"}
|
|
cmd.AddCommand(
|
|
&cobra.Command{
|
|
Use: "config",
|
|
Short: "Print a MonoKSConfig template",
|
|
RunE: func(cmd *cobra.Command, _ []string) error {
|
|
out, err := render.RenderMonoKSConfig()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
_, err = fmt.Fprint(cmd.OutOrStdout(), out)
|
|
return err
|
|
},
|
|
},
|
|
&cobra.Command{
|
|
Use: "osupgrade",
|
|
Short: "Print an OSUpgrade template",
|
|
RunE: func(cmd *cobra.Command, _ []string) error {
|
|
ns, _, err := flags.ToRawKubeConfigLoader().Namespace()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
out, err := render.RenderOSUpgrade(ns)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
_, err = fmt.Fprint(cmd.OutOrStdout(), out)
|
|
return err
|
|
},
|
|
},
|
|
&cobra.Command{
|
|
Use: "controller",
|
|
Short: "Print controller deployment template",
|
|
RunE: func(cmd *cobra.Command, _ []string) error {
|
|
ns, _, err := flags.ToRawKubeConfigLoader().Namespace()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
out, err := render.RenderControllerDeployments(ns)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
_, err = fmt.Fprint(cmd.OutOrStdout(), out)
|
|
return err
|
|
},
|
|
},
|
|
)
|
|
|
|
var authorizedKeysPath string
|
|
|
|
sshdcmd := cobra.Command{
|
|
Use: "sshd",
|
|
Short: "Print sshd deployment template",
|
|
RunE: func(cmd *cobra.Command, _ []string) error {
|
|
ns, _, err := flags.ToRawKubeConfigLoader().Namespace()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
authorizedKeys, err := readAuthorizedKeysFile(authorizedKeysPath)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
out, err := render.RenderSSHDDeployments(ns, authorizedKeys)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
_, err = fmt.Fprint(cmd.OutOrStdout(), out)
|
|
return err
|
|
},
|
|
}
|
|
|
|
sshdcmd.Flags().StringVar(&authorizedKeysPath, "authkeys", "", "path to authorized_keys file")
|
|
|
|
cmd.AddCommand(&sshdcmd)
|
|
return cmd
|
|
}
|
|
|
|
func readAuthorizedKeysFile(path string) (string, error) {
|
|
if path == "" {
|
|
return "", fmt.Errorf("--authkeys is required")
|
|
}
|
|
|
|
b, err := os.ReadFile(path)
|
|
if err != nil {
|
|
return "", fmt.Errorf("read authorized_keys file %q: %w", path, err)
|
|
}
|
|
|
|
if len(bytes.TrimSpace(b)) == 0 {
|
|
return "", fmt.Errorf("authorized_keys file %q is empty", path)
|
|
}
|
|
|
|
return string(b), nil
|
|
}
|