Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix cloud cluster autoselection #568

Merged
merged 1 commit into from
Oct 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion cmd/command/cd/cd.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ type Plural struct {
HelmConfiguration *action.Configuration
}

func SetConsoleURL(url string) {
consoleURL = url
}

func Command(clients client.Plural, helmConfiguration *action.Configuration) cli.Command {
return cli.Command{
Name: "deployments",
Expand Down Expand Up @@ -234,7 +238,10 @@ func (p *Plural) HandleCdLogin(c *cli.Context) (err error) {
}
}

url := c.String("url")
url := consoleURL
if url == "" {
url = c.String("url")
}
if url == "" {
url, err = utils.ReadLine("Enter the url of your console: ")
if err != nil {
Expand Down
61 changes: 48 additions & 13 deletions cmd/command/up/up.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@ import (

"github.com/AlecAivazis/survey/v2"
"github.com/pluralsh/plural-cli/cmd/command/cd"
cdpkg "github.com/pluralsh/plural-cli/cmd/command/cd"
"github.com/pluralsh/plural-cli/pkg/client"
"github.com/pluralsh/plural-cli/pkg/common"
"github.com/pluralsh/plural-cli/pkg/provider"
"github.com/pluralsh/plural-cli/pkg/up"
"github.com/pluralsh/plural-cli/pkg/utils"
"github.com/pluralsh/plural-cli/pkg/utils/git"
Expand Down Expand Up @@ -54,14 +56,24 @@ func Command(clients client.Plural) cli.Command {

func (p *Plural) handleUp(c *cli.Context) error {
// provider.IgnoreProviders([]string{"GENERIC", "KIND"})
if err := p.HandleInit(c); err != nil {
if err := common.HandleLogin(c); err != nil {
return err
}
p.InitPluralClient()

cd := &cd.Plural{Plural: p.Plural}
cd := &cdpkg.Plural{Plural: p.Plural}

var name, url string
var err error

if c.Bool("cloud") {
name, url, err = p.choseCluster()
if err != nil {
return err
}

cdpkg.SetConsoleURL(url)
provider.SetClusterFlag(name)
if err := cd.HandleCdLogin(c); err != nil {
return err
}
Expand All @@ -71,6 +83,10 @@ func (p *Plural) handleUp(c *cli.Context) error {
}
}

if err := p.HandleInit(c); err != nil {
return err
}

repoRoot, err := git.Root()
if err != nil {
return err
Expand All @@ -82,10 +98,11 @@ func (p *Plural) handleUp(c *cli.Context) error {
}

if c.Bool("cloud") {
id, name, err := getCluster(cd)
id, err := getCluster(cd)
if err != nil {
return err
}

ctx.ImportCluster = lo.ToPtr(id)
ctx.CloudCluster = name
}
Expand Down Expand Up @@ -120,21 +137,18 @@ func (p *Plural) handleUp(c *cli.Context) error {
return nil
}

func getCluster(cd *cd.Plural) (id string, name string, err error) {
if cd == nil {
return "", "", fmt.Errorf("your CLI is not logged into Plural, try running `plural login` to generate local credentials")
}
clusters, err := cd.ListClusters()
func (p *Plural) choseCluster() (name, url string, err error) {
instances, err := p.GetConsoleInstances()
if err != nil {
return "", "", err
return
}

clusterNames := []string{}
clusterMap := map[string]string{}

for _, cluster := range clusters {
clusterNames = append(clusterNames, cluster.Node.Name)
clusterMap[cluster.Node.Name] = cluster.Node.ID
for _, cluster := range instances {
clusterNames = append(clusterNames, cluster.Name)
clusterMap[cluster.Name] = cluster.URL
}

prompt := &survey.Select{
Expand All @@ -144,6 +158,27 @@ func getCluster(cd *cd.Plural) (id string, name string, err error) {
if err = survey.AskOne(prompt, &name, survey.WithValidator(survey.Required)); err != nil {
return
}
id = clusterMap[name]
url = clusterMap[name]
return
}

func getCluster(cd *cd.Plural) (id string, err error) {
if cd == nil {
err = fmt.Errorf("your CLI is not logged into Plural, try running `plural login` to generate local credentials")
return
}

clusters, err := cd.ListClusters()
if err != nil {
return
}

for _, cluster := range clusters {
if *cluster.Node.Handle == "mgmt" {
return cluster.Node.ID, nil
}
}

err = fmt.Errorf("could not find the management cluster in your Plural cloud instance, contact support for assistance")
return
}
1 change: 1 addition & 0 deletions pkg/client/plural.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ func (p *Plural) HandleInit(c *cli.Context) error {
if err := common.HandleLogin(c); err != nil {
return err
}

p.InitPluralClient()

me, err := p.Me()
Expand Down
11 changes: 11 additions & 0 deletions pkg/common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ import (
"github.com/pluralsh/plural-cli/pkg/utils/git"
)

var (
loggedIn = false
)

func AppReadme(name string, dryRun bool) error {
repoRoot, err := git.Root()
if err != nil {
Expand Down Expand Up @@ -74,6 +78,13 @@ func DoBuild(client api.Client, installation *api.Installation, force bool) erro
}

func HandleLogin(c *cli.Context) error {
if loggedIn {
return nil
}
defer func() {
loggedIn = true
}()

conf := &config.Config{}
conf.Token = ""
conf.Endpoint = c.String("endpoint")
Expand Down
26 changes: 13 additions & 13 deletions pkg/provider/aws.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,21 +67,21 @@ var (
}
)

var awsSurvey = []*survey.Question{
{
Name: "cluster",
Prompt: &survey.Input{Message: "Enter the name of your cluster:"},
Validate: validCluster,
},
{
Name: "region",
Prompt: &survey.Select{Message: "What region will you deploy to?", Default: "us-east-2", Options: awsRegions},
Validate: survey.Required,
},
}

func mkAWS(conf config.Config) (provider *AWSProvider, err error) {
provider = &AWSProvider{}
var awsSurvey = []*survey.Question{
{
Name: "cluster",
Prompt: &survey.Input{Message: "Enter the name of your cluster:", Default: clusterFlag},
Validate: validCluster,
},
{
Name: "region",
Prompt: &survey.Select{Message: "What region will you deploy to?", Default: "us-east-2", Options: awsRegions},
Validate: survey.Required,
},
}

if err = survey.Ask(awsSurvey, provider); err != nil {
return
}
Expand Down
46 changes: 23 additions & 23 deletions pkg/provider/azure.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,36 +116,36 @@ var (
}
)

var azureSurvey = []*survey.Question{
{
Name: "cluster",
Prompt: &survey.Input{Message: "Enter the name of your cluster:"},
Validate: validCluster,
},
{
Name: "storage",
Prompt: &survey.Input{Message: "Enter the name of the storage account to use for your stage, must be globally unique or already owned by your subscription: "},
Validate: utils.ValidateStorageAccountName,
},
{
Name: "region",
Prompt: &survey.Select{Message: "Enter the region you want to deploy to:", Default: "eastus", Options: azureRegions},
Validate: survey.Required,
},
{
Name: "resource",
Prompt: &survey.Input{Message: "Enter the name of the resource group to use as default: "},
Validate: utils.ValidateResourceGroupName,
},
}

func mkAzure(conf config.Config) (prov *AzureProvider, err error) {
var resp struct {
Cluster string
Storage string
Region string
Resource string
}
var azureSurvey = []*survey.Question{
{
Name: "cluster",
Prompt: &survey.Input{Message: "Enter the name of your cluster:", Default: clusterFlag},
Validate: validCluster,
},
{
Name: "storage",
Prompt: &survey.Input{Message: "Enter the name of the storage account to use for your stage, must be globally unique or already owned by your subscription: "},
Validate: utils.ValidateStorageAccountName,
},
{
Name: "region",
Prompt: &survey.Select{Message: "Enter the region you want to deploy to:", Default: "eastus", Options: azureRegions},
Validate: survey.Required,
},
{
Name: "resource",
Prompt: &survey.Input{Message: "Enter the name of the resource group to use as default: "},
Validate: utils.ValidateResourceGroupName,
},
}

err = survey.Ask(azureSurvey, &resp)
if err != nil {
return
Expand Down
2 changes: 1 addition & 1 deletion pkg/provider/gcp.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ func getGCPSurvey() []*survey.Question {
return []*survey.Question{
{
Name: "cluster",
Prompt: &survey.Input{Message: "Enter the name of your cluster"},
Prompt: &survey.Input{Message: "Enter the name of your cluster", Default: clusterFlag},
Validate: validCluster,
},
{
Expand Down
2 changes: 1 addition & 1 deletion pkg/provider/linode.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func getLinodeSurvey() (surveys []*survey.Question, err error) {
surveys = []*survey.Question{
{
Name: "cluster",
Prompt: &survey.Input{Message: "Enter the name of your cluster"},
Prompt: &survey.Input{Message: "Enter the name of your cluster", Default: clusterFlag},
Validate: validCluster,
},
{
Expand Down
5 changes: 5 additions & 0 deletions pkg/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
)

var cloudFlag bool
var clusterFlag string

type Provider interface {
Name() string
Expand Down Expand Up @@ -106,6 +107,10 @@ func SetCloudFlag(cloud bool) {
cloudFlag = cloud
}

func SetClusterFlag(cluster string) {
clusterFlag = cluster
}

func FromManifest(man *manifest.ProjectManifest) (Provider, error) {
switch man.Provider {
case api.ProviderGCP:
Expand Down
Loading