Skip to content

Commit

Permalink
Proper generation of ext urls (#521)
Browse files Browse the repository at this point in the history
When a user wants to split the graph between public/private, we're too blase about generating urls.  This is a bit more precise and ensures the agent is configured correctly from the cli.
  • Loading branch information
michaeljguarino authored Jun 24, 2024
1 parent c780c78 commit 70f9395
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 3 deletions.
14 changes: 12 additions & 2 deletions cmd/plural/cd_clusters.go
Original file line number Diff line number Diff line change
Expand Up @@ -401,7 +401,13 @@ func (p *Plural) reinstallOperator(c *cli.Context, id, handle *string) error {
return err
}

url := fmt.Sprintf("%s/ext/gql", p.ConsoleClient.Url())
url := p.ConsoleClient.ExtUrl()
if cluster, err := p.ConsoleClient.GetCluster(id, handle); err == nil {
if agentUrl, err := p.ConsoleClient.AgentUrl(cluster.ID); err == nil {
url = agentUrl
}
}

return p.doInstallOperator(url, deployToken, c.String("values"))
}

Expand Down Expand Up @@ -446,8 +452,12 @@ func (p *Plural) handleClusterBootstrap(c *cli.Context) error {
return fmt.Errorf("could not fetch deploy token from cluster")
}

url := p.ConsoleClient.ExtUrl()
if agentUrl, err := p.ConsoleClient.AgentUrl(existing.CreateCluster.ID); err == nil {
url = agentUrl
}

deployToken := *existing.CreateCluster.DeployToken
url := fmt.Sprintf("%s/ext/gql", p.ConsoleClient.Url())
utils.Highlight("installing agent on %s with url %s and initial deploy token %s\n", c.String("name"), p.ConsoleClient.Url(), deployToken)
return p.doInstallOperator(url, deployToken, c.String("values"))
}
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.0
github.com/pluralsh/console-client-go v0.9.1
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 @@ -1885,6 +1885,8 @@ github.com/pluralsh/cluster-api-migration v0.2.16 h1:MQGrLQAhGSSpyjEDamhnJZaQ8Mk
github.com/pluralsh/cluster-api-migration v0.2.16/go.mod h1:24PjMsYv3vSlUiYw7BeUQ0GAtK0Jk2B1iwh35WGQLx8=
github.com/pluralsh/console-client-go v0.9.0 h1:2rCPLVeooSV6WKfIKxqssloE1e6AkdP6LWwueQQlBsg=
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/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
13 changes: 13 additions & 0 deletions pkg/console/clusters.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,19 @@ func (c *consoleClient) GetCluster(clusterId, clusterName *string) (*consoleclie
return result.Cluster, nil
}

func (c *consoleClient) AgentUrl(id string) (string, error) {
res, err := c.client.GetAgentURL(c.ctx, id)
if err != nil {
return "", err
}

if res == nil {
return "", fmt.Errorf("cluster not found")
}

return lo.FromPtr(res.Cluster.AgentURL), nil
}

func (c *consoleClient) GetDeployToken(clusterId, clusterName *string) (string, error) {
res, err := c.client.GetClusterWithToken(c.ctx, clusterId, clusterName)
if err != nil {
Expand Down
22 changes: 22 additions & 0 deletions pkg/console/console.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"
"net/http"
"net/url"
"strings"

consoleclient "github.com/pluralsh/console-client-go"
Expand All @@ -13,12 +14,15 @@ type consoleClient struct {
ctx context.Context
client consoleclient.ConsoleClient
url string
extUrl string
token string
}

type ConsoleClient interface {
Url() string
ExtUrl() string
Token() string
AgentUrl(id string) (string, error)
ListClusters() (*consoleclient.ListClusters, error)
GetCluster(clusterId, clusterName *string) (*consoleclient.ClusterFragment, error)
GetDeployToken(clusterId, clusterName *string) (string, error)
Expand Down Expand Up @@ -71,12 +75,26 @@ func NewConsoleClient(token, url string) (ConsoleClient, error) {

return &consoleClient{
url: url,
extUrl: NormalizeExtUrl(url),
token: token,
client: consoleclient.NewClient(&httpClient, NormalizeUrl(url), nil),
ctx: context.Background(),
}, nil
}

func NormalizeExtUrl(uri string) string {
if !strings.HasPrefix(uri, "https://") {
uri = fmt.Sprintf("https://%s", uri)
}

parsed, err := url.Parse(uri)
if err != nil {
panic(err)
}

return fmt.Sprintf("https://%s/ext/gql", parsed.Host)
}

func NormalizeUrl(url string) string {
if !strings.HasPrefix(url, "https://") {
url = fmt.Sprintf("https://%s", url)
Expand All @@ -92,6 +110,10 @@ func (c *consoleClient) Url() string {
return c.url
}

func (c *consoleClient) ExtUrl() string {
return c.extUrl
}

func (c *consoleClient) Token() string {
return c.token
}
46 changes: 46 additions & 0 deletions pkg/test/mocks/ConsoleClient.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 70f9395

Please sign in to comment.