penguin/monok8s

k8s image for Mono Gateway Dev Kit

clitools/pkg/cmd/create/create.go

raw ยท 4294 bytes

package create

import (
	"bytes"
	"fmt"
	"github.com/spf13/cobra"
	"k8s.io/cli-runtime/pkg/genericclioptions"
	"os"
	"strings"

	assets "example.com/monok8s/pkg/assets"
	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:   "crds",
			Short: "Print the bundled CRDs",
			RunE: func(cmd *cobra.Command, _ []string) error {
				return assets.PrintCRDs(cmd.OutOrStdout())
			},
		},
	)

	var authorizedKeysPath string

	sshdcmd := cobra.Command{
		Use:   "sshd",
		Short: "Print sshd deployments 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)

	cconf := render.ControllerConf{}
	controllercmd := cobra.Command{
		Use:   "controller",
		Short: "Print controller deployments template",
		RunE: func(cmd *cobra.Command, _ []string) error {
			if len(cconf.ImagePullSecrets) > 0 && strings.TrimSpace(cconf.Image) == "" {
				return fmt.Errorf("--image-pull-secret requires --image")
			}

			ns, _, err := flags.ToRawKubeConfigLoader().Namespace()
			if err != nil {
				return err
			}

			cconf.Namespace = ns

			out, err := render.RenderControllerDeployments(cconf)
			if err != nil {
				return err
			}

			_, err = fmt.Fprint(cmd.OutOrStdout(), out)
			return err
		},
	}

	controllercmd.Flags().StringVar(
		&cconf.Image,
		"image",
		"",
		"Controller image, including optional registry and tag",
	)
	controllercmd.Flags().StringSliceVar(
		&cconf.ImagePullSecrets,
		"image-pull-secret",
		nil,
		"Image pull secret name for the agent image; may be specified multiple times or as a comma-separated list",
	)

	cmd.AddCommand(&controllercmd)

	aconf := render.AgentConf{}
	agentcmd := cobra.Command{
		Use:   "agent",
		Short: "Print agent daemonsets template",
		RunE: func(cmd *cobra.Command, _ []string) error {
			if len(aconf.ImagePullSecrets) > 0 && strings.TrimSpace(aconf.Image) == "" {
				return fmt.Errorf("--image-pull-secret requires --image")
			}

			ns, _, err := flags.ToRawKubeConfigLoader().Namespace()
			if err != nil {
				return err
			}

			aconf.Namespace = ns

			out, err := render.RenderAgentDaemonSets(aconf)
			if err != nil {
				return err
			}

			_, err = fmt.Fprint(cmd.OutOrStdout(), out)
			return err
		},
	}

	agentcmd.Flags().StringVar(
		&aconf.Image,
		"image",
		"",
		"Agent image, including optional registry and tag",
	)
	agentcmd.Flags().StringSliceVar(
		&aconf.ImagePullSecrets,
		"image-pull-secret",
		nil,
		"Image pull secret name for the agent image; may be specified multiple times or as a comma-separated list",
	)

	cmd.AddCommand(&agentcmd)

	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
}