cluster

package
v0.0.0-...-7aea6f4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 6, 2025 License: Apache-2.0 Imports: 17 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Command = &cli.Command{
	Name:  "cluster",
	Usage: "manage Kubernetes",

	Commands: []*cli.Command{
		CommandSetup,

		CommandContext,
		CommandNamespace,

		CommandCredentials,
	},
}
View Source
var CommandContext = &cli.Command{
	Name:  "context",
	Usage: "change default Kubernetes context",

	Action: func(ctx context.Context, cmd *cli.Command) error {
		context := cmd.Args().Get(0)

		path := kubernetes.ConfigPath()

		if path == "" {
			return errors.New("unable to determine kubeconfig path")
		}

		config, err := clientcmd.LoadFromFile(path)

		if err != nil {
			return err
		}

		var contexts []string

		for key := range config.Contexts {
			contexts = append(contexts, key)
		}

		if context == "" {
			_, context = cli.MustSelect("Select context", contexts)
		}

		if !slices.Contains(contexts, context) {
			return errors.New("context not found")
		}

		config.CurrentContext = context

		if err := clientcmd.WriteToFile(*config, path); err != nil {
			return err
		}

		return nil
	},
}
View Source
var CommandCredentials = &cli.Command{
	Name:  "credentials",
	Usage: "credentials helper for kubectl",

	Hidden: true,

	Action: func(ctx context.Context, cmd *cli.Command) error {
		app := app.MustApp(ctx, cmd)

		token, err := kubelogin.Token(ctx, app.DefaultTenant)

		if err != nil {
			return err
		}

		writeExecCredential(os.Stdout, token)
		return nil
	},
}
View Source
var CommandNamespace = &cli.Command{
	Name:  "namespace",
	Usage: "change default Kubernetes namespace",

	Action: func(ctx context.Context, cmd *cli.Command) error {
		namespace := cmd.Args().Get(0)

		path := kubernetes.ConfigPath()

		if path == "" {
			return errors.New("unable to determine kubeconfig path")
		}

		if namespace == "" {
			client, err := kubernetes.NewFromFile(path)

			if err != nil {
				return err
			}

			list, err := client.CoreV1().Namespaces().List(ctx, metav1.ListOptions{})

			if err != nil {
				return err
			}

			var items []string

			for _, item := range list.Items {
				items = append(items, item.Name)
			}

			_, namespace = cli.MustSelect("Select namespace", items)
		}

		config, err := clientcmd.LoadFromFile(path)

		if err != nil {
			return err
		}

		context := config.Contexts[config.CurrentContext]
		context.Namespace = namespace

		if err := clientcmd.WriteToFile(*config, path); err != nil {
			return err
		}

		return nil
	},
}
View Source
var CommandSetup = &cli.Command{
	Name:  "setup",
	Usage: "setup local Kubernetes config",

	Action: func(ctx context.Context, cmd *cli.Command) error {
		app := app.MustApp(ctx, cmd)
		data := app.MustClusterConfig()

		path := kubernetes.ConfigPath()

		if path == "" {
			return errors.New("unable to determine kubeconfig path")
		}

		os.MkdirAll(filepath.Dir(path), 0755)

		var configs [][]byte

		if source, _ := os.ReadFile(path); source != nil {
			configs = append(configs, source)
		}

		configs = append(configs, data)

		result, err := mergeConfig(configs...)

		if err != nil {
			return os.WriteFile(path, data, 0600)
		}

		return os.WriteFile(path, result, 0600)
	},
}

Functions

This section is empty.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL