Skip to content

Commit

Permalink
Add support for setting project in plural cd clusters bootstrap
Browse files Browse the repository at this point in the history
Can be useful for testing/experimentation on a multi-project structure
  • Loading branch information
michaeljguarino committed Jul 19, 2024
1 parent 4e31edc commit 71b0754
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 1 deletion.
13 changes: 13 additions & 0 deletions cmd/plural/cd_clusters.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ func (p *Plural) cdClusterCommands() []cli.Command {
cli.StringFlag{Name: "name", Usage: "The name you'll give the cluster", Required: true},
cli.StringFlag{Name: "handle", Usage: "optional handle for the cluster"},
cli.StringFlag{Name: "values", Usage: "values file to use for the deployment agent helm chart", Required: false},
cli.StringFlag{Name: "project", Usage: "the project this cluster will belong to", Required: false},
cli.StringSliceFlag{
Name: "tag",
Usage: "a cluster tag to add, useful for targeting with global services",
Expand Down Expand Up @@ -421,6 +422,18 @@ func (p *Plural) handleClusterBootstrap(c *cli.Context) error {
attrs.Handle = lo.ToPtr(c.String("handle"))
}

if c.String("project") != "" {
project, err := p.ConsoleClient.GetProject(c.String("project"))
if err != nil {
return nil
}
if project == nil {
return fmt.Errorf("Could not find project %s", c.String("project"))
}

attrs.ProjectID = lo.ToPtr(project.ID)
}

if c.IsSet("tag") {
attrs.Tags = lo.Map(c.StringSlice("tag"), func(tag string, index int) *gqlclient.TagAttributes {
tags := strings.Split(tag, "=")
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ require (
github.com/packethost/packngo v0.29.0
github.com/pkg/browser v0.0.0-20210911075715-681adbf594b8
github.com/pluralsh/cluster-api-migration v0.2.16
github.com/pluralsh/console-client-go v0.9.1
github.com/pluralsh/console-client-go v0.12.3
github.com/pluralsh/gqlclient v1.11.0
github.com/pluralsh/plural-operator v0.5.5
github.com/pluralsh/polly v0.1.8
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1887,6 +1887,8 @@ github.com/pluralsh/console-client-go v0.9.0 h1:2rCPLVeooSV6WKfIKxqssloE1e6AkdP6
github.com/pluralsh/console-client-go v0.9.0/go.mod h1:eyCiLA44YbXiYyJh8303jk5JdPkt9McgCo5kBjk4lKo=
github.com/pluralsh/console-client-go v0.9.1 h1:o2khvyRu/d3ndehbX02CjnMbU6i0smhulhe0OYnp8yE=
github.com/pluralsh/console-client-go v0.9.1/go.mod h1:eyCiLA44YbXiYyJh8303jk5JdPkt9McgCo5kBjk4lKo=
github.com/pluralsh/console-client-go v0.12.3 h1:cx/a9eAPgohzcDyNs/5BQXYA9WRWcXCyNzNzLwTd3T8=
github.com/pluralsh/console-client-go v0.12.3/go.mod h1:eyCiLA44YbXiYyJh8303jk5JdPkt9McgCo5kBjk4lKo=
github.com/pluralsh/controller-reconcile-helper v0.0.4 h1:1o+7qYSyoeqKFjx+WgQTxDz4Q2VMpzprJIIKShxqG0E=
github.com/pluralsh/controller-reconcile-helper v0.0.4/go.mod h1:AfY0gtteD6veBjmB6jiRx/aR4yevEf6K0M13/pGan/s=
github.com/pluralsh/gqlclient v1.11.0 h1:FfXW7FiEJLHOfTAa7NxDb8jb3aMZNIpCAcG+bg8uHYA=
Expand Down
9 changes: 9 additions & 0 deletions pkg/console/clusters.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,15 @@ import (
"github.com/samber/lo"
)

func (c *consoleClient) GetProject(name string) (*consoleclient.ProjectFragment, error) {
result, err := c.client.GetProject(c.ctx, nil, lo.ToPtr(name))
if err != nil {
return nil, api.GetErrorResponse(err, "GetProject")
}

return result.Project, nil
}

func (c *consoleClient) ListClusters() (*consoleclient.ListClusters, error) {

result, err := c.client.ListClusters(c.ctx, nil, nil, nil)
Expand Down
1 change: 1 addition & 0 deletions pkg/console/console.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ type ConsoleClient interface {
Token() string
AgentUrl(id string) (string, error)
ListClusters() (*consoleclient.ListClusters, error)
GetProject(name string) (*consoleclient.ProjectFragment, error)
GetCluster(clusterId, clusterName *string) (*consoleclient.ClusterFragment, error)
GetDeployToken(clusterId, clusterName *string) (string, error)
UpdateCluster(id string, attr consoleclient.ClusterUpdateAttributes) (*consoleclient.UpdateCluster, error)
Expand Down

0 comments on commit 71b0754

Please sign in to comment.