mirror of
https://github.com/rancher/k3k.git
synced 2026-05-05 17:06:48 +00:00
Update and fix to k3kcli for new ClusterSet integration (#321)
* added clusterset flag to cluster creation and displayname to clusterset creation * updated cli docs
This commit is contained in:
@@ -3,7 +3,9 @@ package cmds
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/url"
|
||||
"slices"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
@@ -35,6 +37,7 @@ type CreateConfig struct {
|
||||
version string
|
||||
mode string
|
||||
kubeconfigServerHost string
|
||||
clusterset string
|
||||
}
|
||||
|
||||
func NewClusterCreateCmd(appCtx *AppContext) *cli.Command {
|
||||
@@ -46,7 +49,7 @@ func NewClusterCreateCmd(appCtx *AppContext) *cli.Command {
|
||||
Usage: "Create new cluster",
|
||||
UsageText: "k3kcli cluster create [command options] NAME",
|
||||
Action: createAction(appCtx, createConfig),
|
||||
Flags: append(CommonFlags, createFlags...),
|
||||
Flags: WithCommonFlags(appCtx, createFlags...),
|
||||
HideHelpCommand: true,
|
||||
}
|
||||
}
|
||||
@@ -65,18 +68,37 @@ func createAction(appCtx *AppContext, config *CreateConfig) cli.ActionFunc {
|
||||
return errors.New("invalid cluster name")
|
||||
}
|
||||
|
||||
namespace := Namespace(name)
|
||||
namespace := appCtx.Namespace(name)
|
||||
|
||||
ns := &v1.Namespace{ObjectMeta: metav1.ObjectMeta{Name: namespace}}
|
||||
if err := client.Get(ctx, types.NamespacedName{Name: namespace}, ns); err != nil {
|
||||
if !apierrors.IsNotFound(err) {
|
||||
return err
|
||||
// if clusterset is set, use the namespace of the clusterset
|
||||
if config.clusterset != "" {
|
||||
namespace = appCtx.Namespace(config.clusterset)
|
||||
}
|
||||
|
||||
if err := createNamespace(ctx, client, namespace); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// if clusterset is set, create the cluster set
|
||||
if config.clusterset != "" {
|
||||
namespace = appCtx.Namespace(config.clusterset)
|
||||
|
||||
clusterSet := &v1alpha1.ClusterSet{}
|
||||
if err := client.Get(ctx, types.NamespacedName{Name: "default", Namespace: namespace}, clusterSet); err != nil {
|
||||
if !apierrors.IsNotFound(err) {
|
||||
return err
|
||||
}
|
||||
|
||||
clusterSet, err = createClusterSet(ctx, client, namespace, v1alpha1.ClusterMode(config.mode), config.clusterset)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
logrus.Infof(`Creating namespace [%s]`, namespace)
|
||||
logrus.Infof("ClusterSet in namespace [%s] available", namespace)
|
||||
|
||||
if err := client.Create(ctx, ns); err != nil {
|
||||
return err
|
||||
if !slices.Contains(clusterSet.Spec.AllowedModeTypes, v1alpha1.ClusterMode(config.mode)) {
|
||||
return fmt.Errorf("invalid '%s' Cluster mode. ClusterSet only allows %v", config.mode, clusterSet.Spec.AllowedModeTypes)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -94,5 +94,10 @@ func NewCreateFlags(config *CreateConfig) []cli.Flag {
|
||||
Usage: "override the kubeconfig server host",
|
||||
Destination: &config.kubeconfigServerHost,
|
||||
},
|
||||
&cli.StringFlag{
|
||||
Name: "clusterset",
|
||||
Usage: "The clusterset to create the cluster in",
|
||||
Destination: &config.clusterset,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
@@ -25,7 +25,7 @@ func NewClusterDeleteCmd(appCtx *AppContext) *cli.Command {
|
||||
Usage: "Delete an existing cluster",
|
||||
UsageText: "k3kcli cluster delete [command options] NAME",
|
||||
Action: delete(appCtx),
|
||||
Flags: append(CommonFlags, &cli.BoolFlag{
|
||||
Flags: WithCommonFlags(appCtx, &cli.BoolFlag{
|
||||
Name: "keep-data",
|
||||
Usage: "keeps persistence volumes created for the cluster after deletion",
|
||||
Destination: &keepData,
|
||||
@@ -48,7 +48,7 @@ func delete(appCtx *AppContext) cli.ActionFunc {
|
||||
return errors.New("invalid cluster name")
|
||||
}
|
||||
|
||||
namespace := Namespace(name)
|
||||
namespace := appCtx.Namespace(name)
|
||||
|
||||
logrus.Infof("Deleting [%s] cluster in namespace [%s]", name, namespace)
|
||||
|
||||
|
||||
@@ -12,10 +12,12 @@ import (
|
||||
apierrors "k8s.io/apimachinery/pkg/api/errors"
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
"k8s.io/apimachinery/pkg/types"
|
||||
"sigs.k8s.io/controller-runtime/pkg/client"
|
||||
)
|
||||
|
||||
type ClusterSetCreateConfig struct {
|
||||
mode string
|
||||
mode string
|
||||
displayName string
|
||||
}
|
||||
|
||||
func NewClusterSetCreateCmd(appCtx *AppContext) *cli.Command {
|
||||
@@ -36,6 +38,11 @@ func NewClusterSetCreateCmd(appCtx *AppContext) *cli.Command {
|
||||
}
|
||||
},
|
||||
},
|
||||
&cli.StringFlag{
|
||||
Name: "display-name",
|
||||
Usage: "The display name of the clusterset",
|
||||
Destination: &config.displayName,
|
||||
},
|
||||
}
|
||||
|
||||
return &cli.Command{
|
||||
@@ -43,7 +50,7 @@ func NewClusterSetCreateCmd(appCtx *AppContext) *cli.Command {
|
||||
Usage: "Create new clusterset",
|
||||
UsageText: "k3kcli clusterset create [command options] NAME",
|
||||
Action: clusterSetCreateAction(appCtx, config),
|
||||
Flags: append(CommonFlags, createFlags...),
|
||||
Flags: WithCommonFlags(appCtx, createFlags...),
|
||||
HideHelpCommand: true,
|
||||
}
|
||||
}
|
||||
@@ -62,45 +69,70 @@ func clusterSetCreateAction(appCtx *AppContext, config *ClusterSetCreateConfig)
|
||||
return errors.New("invalid cluster name")
|
||||
}
|
||||
|
||||
namespace := Namespace(name)
|
||||
|
||||
ns := &v1.Namespace{ObjectMeta: metav1.ObjectMeta{Name: namespace}}
|
||||
if err := client.Get(ctx, types.NamespacedName{Name: namespace}, ns); err != nil {
|
||||
if !apierrors.IsNotFound(err) {
|
||||
return err
|
||||
}
|
||||
|
||||
logrus.Infof(`Creating namespace [%s]`, namespace)
|
||||
|
||||
if err := client.Create(ctx, ns); err != nil {
|
||||
return err
|
||||
}
|
||||
displayName := config.displayName
|
||||
if displayName == "" {
|
||||
displayName = name
|
||||
}
|
||||
|
||||
logrus.Infof("Creating clusterset [%s] in namespace [%s]", name, namespace)
|
||||
|
||||
clusterSet := &v1alpha1.ClusterSet{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Name: name,
|
||||
Namespace: namespace,
|
||||
},
|
||||
TypeMeta: metav1.TypeMeta{
|
||||
Kind: "ClusterSet",
|
||||
APIVersion: "k3k.io/v1alpha1",
|
||||
},
|
||||
Spec: v1alpha1.ClusterSetSpec{
|
||||
AllowedModeTypes: []v1alpha1.ClusterMode{v1alpha1.ClusterMode(config.mode)},
|
||||
},
|
||||
// if both display name and namespace are set the name is ignored
|
||||
if config.displayName != "" && appCtx.namespace != "" {
|
||||
logrus.Warnf("Ignoring name [%s] because display name and namespace are set", name)
|
||||
}
|
||||
|
||||
if err := client.Create(ctx, clusterSet); err != nil {
|
||||
if apierrors.IsAlreadyExists(err) {
|
||||
logrus.Infof("ClusterSet [%s] already exists", name)
|
||||
} else {
|
||||
return err
|
||||
}
|
||||
namespace := appCtx.Namespace(name)
|
||||
|
||||
if err := createNamespace(ctx, client, namespace); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
_, err := createClusterSet(ctx, client, namespace, v1alpha1.ClusterMode(config.mode), displayName)
|
||||
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
func createNamespace(ctx context.Context, client client.Client, name string) error {
|
||||
ns := &v1.Namespace{ObjectMeta: metav1.ObjectMeta{Name: name}}
|
||||
if err := client.Get(ctx, types.NamespacedName{Name: name}, ns); err != nil {
|
||||
if !apierrors.IsNotFound(err) {
|
||||
return err
|
||||
}
|
||||
|
||||
logrus.Infof(`Creating namespace [%s]`, name)
|
||||
|
||||
if err := client.Create(ctx, ns); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func createClusterSet(ctx context.Context, client client.Client, namespace string, mode v1alpha1.ClusterMode, displayName string) (*v1alpha1.ClusterSet, error) {
|
||||
logrus.Infof("Creating clusterset in namespace [%s]", namespace)
|
||||
|
||||
clusterSet := &v1alpha1.ClusterSet{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Name: "default",
|
||||
Namespace: namespace,
|
||||
},
|
||||
TypeMeta: metav1.TypeMeta{
|
||||
Kind: "ClusterSet",
|
||||
APIVersion: "k3k.io/v1alpha1",
|
||||
},
|
||||
Spec: v1alpha1.ClusterSetSpec{
|
||||
AllowedModeTypes: []v1alpha1.ClusterMode{mode},
|
||||
DisplayName: displayName,
|
||||
},
|
||||
}
|
||||
|
||||
if err := client.Create(ctx, clusterSet); err != nil {
|
||||
if apierrors.IsAlreadyExists(err) {
|
||||
logrus.Infof("ClusterSet in namespace [%s] already exists", namespace)
|
||||
} else {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
return clusterSet, nil
|
||||
}
|
||||
|
||||
@@ -18,7 +18,7 @@ func NewClusterSetDeleteCmd(appCtx *AppContext) *cli.Command {
|
||||
Usage: "Delete an existing clusterset",
|
||||
UsageText: "k3kcli clusterset delete [command options] NAME",
|
||||
Action: clusterSetDeleteAction(appCtx),
|
||||
Flags: CommonFlags,
|
||||
Flags: WithCommonFlags(appCtx),
|
||||
HideHelpCommand: true,
|
||||
}
|
||||
}
|
||||
@@ -37,20 +37,20 @@ func clusterSetDeleteAction(appCtx *AppContext) cli.ActionFunc {
|
||||
return errors.New("invalid cluster name")
|
||||
}
|
||||
|
||||
namespace := Namespace(name)
|
||||
namespace := appCtx.Namespace(name)
|
||||
|
||||
logrus.Infof("Deleting clusterset [%s] in namespace [%s]", name, namespace)
|
||||
logrus.Infof("Deleting clusterset in namespace [%s]", namespace)
|
||||
|
||||
clusterSet := &v1alpha1.ClusterSet{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Name: name,
|
||||
Name: "default",
|
||||
Namespace: namespace,
|
||||
},
|
||||
}
|
||||
|
||||
if err := client.Delete(ctx, clusterSet); err != nil {
|
||||
if apierrors.IsNotFound(err) {
|
||||
logrus.Warnf("ClusterSet [%s] not found", name)
|
||||
logrus.Warnf("ClusterSet not found in namespace [%s]", namespace)
|
||||
} else {
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -88,7 +88,7 @@ func NewKubeconfigGenerateCmd(appCtx *AppContext) *cli.Command {
|
||||
Usage: "Generate kubeconfig for clusters",
|
||||
SkipFlagParsing: false,
|
||||
Action: generate(appCtx),
|
||||
Flags: append(CommonFlags, generateKubeconfigFlags...),
|
||||
Flags: WithCommonFlags(appCtx, generateKubeconfigFlags...),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -96,9 +96,10 @@ func generate(appCtx *AppContext) cli.ActionFunc {
|
||||
return func(clx *cli.Context) error {
|
||||
ctx := context.Background()
|
||||
client := appCtx.Client
|
||||
|
||||
clusterKey := types.NamespacedName{
|
||||
Name: name,
|
||||
Namespace: Namespace(name),
|
||||
Namespace: appCtx.Namespace(name),
|
||||
}
|
||||
|
||||
var cluster v1alpha1.Cluster
|
||||
|
||||
@@ -14,36 +14,14 @@ import (
|
||||
"sigs.k8s.io/controller-runtime/pkg/client"
|
||||
)
|
||||
|
||||
var (
|
||||
Scheme = runtime.NewScheme()
|
||||
|
||||
debug bool
|
||||
Kubeconfig string
|
||||
namespace string
|
||||
|
||||
CommonFlags = []cli.Flag{
|
||||
&cli.StringFlag{
|
||||
Name: "kubeconfig",
|
||||
Usage: "kubeconfig path",
|
||||
Destination: &Kubeconfig,
|
||||
DefaultText: "$HOME/.kube/config or $KUBECONFIG if set",
|
||||
},
|
||||
&cli.StringFlag{
|
||||
Name: "namespace",
|
||||
Usage: "namespace to create the k3k cluster in",
|
||||
Destination: &namespace,
|
||||
},
|
||||
}
|
||||
)
|
||||
|
||||
func init() {
|
||||
_ = clientgoscheme.AddToScheme(Scheme)
|
||||
_ = v1alpha1.AddToScheme(Scheme)
|
||||
}
|
||||
|
||||
type AppContext struct {
|
||||
RestConfig *rest.Config
|
||||
Client client.Client
|
||||
|
||||
// Global flags
|
||||
Debug bool
|
||||
Kubeconfig string
|
||||
namespace string
|
||||
}
|
||||
|
||||
func NewApp() *cli.App {
|
||||
@@ -52,26 +30,23 @@ func NewApp() *cli.App {
|
||||
app := cli.NewApp()
|
||||
app.Name = "k3kcli"
|
||||
app.Usage = "CLI for K3K"
|
||||
app.Flags = []cli.Flag{
|
||||
&cli.BoolFlag{
|
||||
Name: "debug",
|
||||
Usage: "Turn on debug logs",
|
||||
Destination: &debug,
|
||||
EnvVars: []string{"K3K_DEBUG"},
|
||||
},
|
||||
}
|
||||
app.Flags = WithCommonFlags(appCtx)
|
||||
|
||||
app.Before = func(clx *cli.Context) error {
|
||||
if debug {
|
||||
if appCtx.Debug {
|
||||
logrus.SetLevel(logrus.DebugLevel)
|
||||
}
|
||||
|
||||
restConfig, err := loadRESTConfig()
|
||||
restConfig, err := loadRESTConfig(appCtx.Kubeconfig)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
ctrlClient, err := client.New(restConfig, client.Options{Scheme: Scheme})
|
||||
scheme := runtime.NewScheme()
|
||||
_ = clientgoscheme.AddToScheme(scheme)
|
||||
_ = v1alpha1.AddToScheme(scheme)
|
||||
|
||||
ctrlClient, err := client.New(restConfig, client.Options{Scheme: scheme})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -96,23 +71,47 @@ func NewApp() *cli.App {
|
||||
return app
|
||||
}
|
||||
|
||||
func Namespace(clusterName string) string {
|
||||
if namespace != "" {
|
||||
return namespace
|
||||
func (ctx *AppContext) Namespace(name string) string {
|
||||
if ctx.namespace != "" {
|
||||
return ctx.namespace
|
||||
}
|
||||
|
||||
return "k3k-" + clusterName
|
||||
return "k3k-" + name
|
||||
}
|
||||
|
||||
func loadRESTConfig() (*rest.Config, error) {
|
||||
func loadRESTConfig(kubeconfig string) (*rest.Config, error) {
|
||||
loadingRules := clientcmd.NewDefaultClientConfigLoadingRules()
|
||||
configOverrides := &clientcmd.ConfigOverrides{}
|
||||
|
||||
if Kubeconfig != "" {
|
||||
loadingRules.ExplicitPath = Kubeconfig
|
||||
if kubeconfig != "" {
|
||||
loadingRules.ExplicitPath = kubeconfig
|
||||
}
|
||||
|
||||
kubeConfig := clientcmd.NewNonInteractiveDeferredLoadingClientConfig(loadingRules, configOverrides)
|
||||
|
||||
return kubeConfig.ClientConfig()
|
||||
}
|
||||
|
||||
func WithCommonFlags(appCtx *AppContext, flags ...cli.Flag) []cli.Flag {
|
||||
commonFlags := []cli.Flag{
|
||||
&cli.BoolFlag{
|
||||
Name: "debug",
|
||||
Usage: "Turn on debug logs",
|
||||
Destination: &appCtx.Debug,
|
||||
EnvVars: []string{"K3K_DEBUG"},
|
||||
},
|
||||
&cli.StringFlag{
|
||||
Name: "kubeconfig",
|
||||
Usage: "kubeconfig path",
|
||||
Destination: &appCtx.Kubeconfig,
|
||||
DefaultText: "$HOME/.kube/config or $KUBECONFIG if set",
|
||||
},
|
||||
&cli.StringFlag{
|
||||
Name: "namespace",
|
||||
Usage: "namespace to create the k3k cluster in",
|
||||
Destination: &appCtx.namespace,
|
||||
},
|
||||
}
|
||||
|
||||
return append(commonFlags, flags...)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user