Skip to content

Commit

Permalink
Merge pull request #46 from ksctl/fix/45
Browse files Browse the repository at this point in the history
✨ Feature: CNI and Addons
  • Loading branch information
Horiodino authored Feb 13, 2025
2 parents 0b53961 + 076fcbd commit 93c3410
Show file tree
Hide file tree
Showing 20 changed files with 700 additions and 221 deletions.
181 changes: 166 additions & 15 deletions cmd/addons.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,17 @@

package cmd

import "github.com/spf13/cobra"
import (
"fmt"
"os"
"strconv"

"github.com/ksctl/cli/v2/pkg/cli"
"github.com/ksctl/ksctl/v2/pkg/handler/cluster/controller"

addonsHandler "github.com/ksctl/ksctl/v2/pkg/handler/addons"
"github.com/spf13/cobra"
)

func (k *KsctlCommand) Addons() *cobra.Command {

Expand All @@ -30,20 +40,6 @@ ksctl addons --help
return cmd
}

func (k *KsctlCommand) ListAddon() *cobra.Command {

cmd := &cobra.Command{
Use: "list",
Example: `
ksctl addons list --help
`,
Short: "Use to list the addons",
Long: "It is used to list the addons",
}

return cmd
}

func (k *KsctlCommand) EnableAddon() *cobra.Command {

cmd := &cobra.Command{
Expand All @@ -53,6 +49,67 @@ ksctl addons enable --help
`,
Short: "Use to enable an addon",
Long: "It is used to enable an addon",
Run: func(cmd *cobra.Command, args []string) {
m, ok := k.addonClientSetup()
if !ok {
os.Exit(1)
}

c, err := addonsHandler.NewController(
k.Ctx,
k.l,
&controller.Client{
Metadata: *m,
},
)
if err != nil {
k.l.Error("Error in creating the controller", "Error", err)
os.Exit(1)
}

addons, err := c.ListAllAddons()
if err != nil {
k.l.Error("Error in listing the addons", "Error", err)
os.Exit(1)
}

addonSku, err := k.menuDriven.DropDownList(
"Select the addon to enable",
addons,
)
if err != nil {
k.l.Error("Failed to get userinput", "Reason", err)
os.Exit(1)
}

addonVers, err := c.ListAvailableVersions(addonSku)
if err != nil {
k.l.Error("Error in listing the versions", "Error", err)
os.Exit(1)
}

addonVer, err := k.menuDriven.DropDownList(
"Select the version to enable",
addonVers,
cli.WithDefaultValue(addonVers[0]),
)
if err != nil {
k.l.Error("Failed to get userinput", "Reason", err)
os.Exit(1)
}

if cc, err := c.GetAddon(addonSku); err != nil {
k.l.Error("Error in getting the addon", "Error", err)
os.Exit(1)
} else {
if _err := cc.Install(addonVer); _err != nil {
k.l.Error("Error in enabling the addon", "Error", _err)
os.Exit(1)
}
}

k.l.Success(k.Ctx, "Addon enabled successfully", "sku", addonSku, "version", addonVer)
},
}
return cmd
}
Expand All @@ -66,6 +123,100 @@ ksctl addons disable --help
`,
Short: "Use to disable an addon",
Long: "It is used to disable an addon",
Run: func(cmd *cobra.Command, args []string) {
m, ok := k.addonClientSetup()
if !ok {
os.Exit(1)
}

c, err := addonsHandler.NewController(
k.Ctx,
k.l,
&controller.Client{
Metadata: *m,
},
)
if err != nil {
k.l.Error("Error in creating the controller", "Error", err)
os.Exit(1)
}

addons, err := c.ListInstalledAddons()
if err != nil {
k.l.Error("Error in listing the installed addons", "Error", err)
os.Exit(1)
}

vals := make(map[string]string, len(addons))
for _, addon := range addons {
ver := "NaN"
if addon.Version != "" {
ver = "@" + addon.Version
}
vals[fmt.Sprintf("%s%s", addon.Name, ver)] = addon.Name
}

selectedAddon, err := k.menuDriven.DropDown(
"Select the addon to disable",
vals,
)
if err != nil {
k.l.Error("Failed to get userinput", "Reason", err)
os.Exit(1)
}

if cc, err := c.GetAddon(selectedAddon); err != nil {
k.l.Error("Error in getting the addon", "Error", err)
os.Exit(1)
} else {
if _err := cc.Uninstall(); _err != nil {
k.l.Error("Error in disabling the addon", "Error", _err)
os.Exit(1)
}
}

k.l.Success(k.Ctx, "Addon disabled successfully", "sku", selectedAddon)
},
}
return cmd
}

func (k *KsctlCommand) addonClientSetup() (*controller.Metadata, bool) {
clusters, err := k.fetchAllClusters()
if err != nil {
k.l.Error("Error in fetching the clusters", "Error", err)
return nil, false
}

if len(clusters) == 0 {
k.l.Error("No clusters found to delete")
return nil, false
}

selectDisplay := make(map[string]string, len(clusters))
valueMaping := make(map[string]controller.Metadata, len(clusters))

for idx, cluster := range clusters {
selectDisplay[makeHumanReadableList(cluster)] = strconv.Itoa(idx)
valueMaping[strconv.Itoa(idx)] = controller.Metadata{
ClusterName: cluster.Name,
ClusterType: cluster.ClusterType,
Provider: cluster.CloudProvider,
Region: cluster.Region,
StateLocation: k.KsctlConfig.PreferedStateStore,
K8sDistro: cluster.K8sDistro,
}
}

selectedCluster, err := k.menuDriven.DropDown(
"Select the cluster for addon operation",
selectDisplay,
)
if err != nil {
k.l.Error("Failed to get userinput", "Reason", err)
return nil, false
}

m := valueMaping[selectedCluster]
return &m, true
}
4 changes: 3 additions & 1 deletion cmd/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"context"
"os"

"github.com/ksctl/cli/v2/pkg/cli"
"github.com/ksctl/cli/v2/pkg/config"
cLogger "github.com/ksctl/cli/v2/pkg/logger"
"github.com/ksctl/ksctl/v2/pkg/consts"
Expand All @@ -34,7 +35,8 @@ type KsctlCommand struct {
ksctlStorage storage.Storage
root *cobra.Command
verbose int
dryRun bool
debugMode bool
menuDriven cli.MenuDriven
KsctlConfig *config.Config
inMemInstanceTypesInReg map[string]provider.InstanceRegionOutput
}
Expand Down
29 changes: 14 additions & 15 deletions cmd/configure.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,13 +73,13 @@ func (k *KsctlCommand) ConfigureCloud() *cobra.Command {
}

func (k *KsctlCommand) handleStorageConfig() bool {
if v, err := cli.DropDown(
if v, err := k.menuDriven.DropDown(
"What should be your default storageDriver?",
map[string]string{
"MongoDb": string(consts.StoreExtMongo),
"Local": string(consts.StoreLocal),
},
"Local",
cli.WithDefaultValue("Local"),
); err != nil {
k.l.Error("Failed to get the storageDriver", "Reason", err)
return false
Expand All @@ -103,13 +103,12 @@ func (k *KsctlCommand) handleStorageConfig() bool {
}

func (k *KsctlCommand) handleCloudConfig() bool {
if v, err := cli.DropDown(
if v, err := k.menuDriven.DropDown(
"Credentials",
map[string]string{
"Amazon Web Services": string(consts.CloudAws),
"Azure": string(consts.CloudAzure),
},
"",
); err != nil {
k.l.Error("Failed to get the credentials", "Reason", err)
return false
Expand All @@ -133,11 +132,11 @@ func (k *KsctlCommand) handleCloudConfig() bool {

func (k *KsctlCommand) storeAwsCredentials() (err error) {
c := new(statefile.CredentialsAws)
c.AccessKeyId, err = cli.TextInputPassword("Enter your AWS Access Key ID")
c.AccessKeyId, err = k.menuDriven.TextInputPassword("Enter your AWS Access Key ID")
if err != nil {
return err
}
c.SecretAccessKey, err = cli.TextInputPassword("Enter your AWS Secret Access Key")
c.SecretAccessKey, err = k.menuDriven.TextInputPassword("Enter your AWS Secret Access Key")
if err != nil {
return err
}
Expand All @@ -160,21 +159,21 @@ func (k *KsctlCommand) loadAwsCredentials() error {

func (k *KsctlCommand) storeAzureCredentials() (err error) {
c := new(statefile.CredentialsAzure)
c.SubscriptionID, err = cli.TextInputPassword("Enter your Azure Subscription ID")
c.SubscriptionID, err = k.menuDriven.TextInputPassword("Enter your Azure Subscription ID")
if err != nil {
return err
}

c.TenantID, err = cli.TextInputPassword("Enter your Azure Tenant ID")
c.TenantID, err = k.menuDriven.TextInputPassword("Enter your Azure Tenant ID")
if err != nil {
return err
}

c.ClientID, err = cli.TextInputPassword("Enter your Azure Client ID")
c.ClientID, err = k.menuDriven.TextInputPassword("Enter your Azure Client ID")
if err != nil {
return err
}
c.ClientSecret, err = cli.TextInputPassword("Enter your Azure Client Secret")
c.ClientSecret, err = k.menuDriven.TextInputPassword("Enter your Azure Client Secret")
if err != nil {
return err
}
Expand All @@ -197,26 +196,26 @@ func (k *KsctlCommand) loadAzureCredentials() error {

func (k *KsctlCommand) storeMongoCredentials() (err error) {
c := new(statefile.CredentialsMongodb)
srv, err := cli.Confirmation("Enter whether MongoDB has SRV record or not", "no")
srv, err := k.menuDriven.Confirmation("Enter whether MongoDB has SRV record or not", cli.WithDefaultValue("no"))
if err != nil {
return err
}
c.SRV = srv

c.Domain, err = cli.TextInput("Enter your MongoDB URI", "")
c.Domain, err = k.menuDriven.TextInput("Enter your MongoDB URI")
if err != nil {
return err
}
c.Username, err = cli.TextInputPassword("Enter your MongoDB Username")
c.Username, err = k.menuDriven.TextInputPassword("Enter your MongoDB Username")
if err != nil {
return err
}
c.Password, err = cli.TextInputPassword("Enter your MongoDB Password")
c.Password, err = k.menuDriven.TextInputPassword("Enter your MongoDB Password")
if err != nil {
return err
}
port := ""
if port, err = cli.TextInput("Enter your MongoDB Port", ""); err != nil {
if port, err = k.menuDriven.TextInput("Enter your MongoDB Port"); err != nil {
return err
}
if len(port) != 0 {
Expand Down
7 changes: 3 additions & 4 deletions cmd/connect.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,9 @@ ksctl connect --help
}
}

selectedCluster, err := cli.DropDown(
selectedCluster, err := k.menuDriven.DropDown(
"Select the cluster to delete",
selectDisplay,
"",
)
if err != nil {
k.l.Error("Failed to get userinput", "Reason", err)
Expand Down Expand Up @@ -108,14 +107,14 @@ ksctl connect --help

k.writeKubeconfig([]byte(*kubeconfig))

accessMode, err := cli.DropDown(
accessMode, err := k.menuDriven.DropDown(
"Select the access mode",
map[string]string{
"k9s": "k9s",
"bash": "shell",
"none": "none",
},
"none",
cli.WithDefaultValue("none"),
)
if err != nil {
k.l.Error("Failed to get userinput", "Reason", err)
Expand Down
Loading

0 comments on commit 93c3410

Please sign in to comment.