Skip to content

Commit

Permalink
Merge pull request #9 from datum-cloud/bug/update-kubeconfig-hostname…
Browse files Browse the repository at this point in the history
…-propagation

bug: Corrected update-kubeconfig method to propagate target API server information to get-token command.
  • Loading branch information
joshlreese authored Dec 30, 2024
2 parents b701f7f + bd08c03 commit 8f93b96
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 16 deletions.
28 changes: 14 additions & 14 deletions internal/cmd/auth/update-kubeconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package auth
import (
"errors"
"fmt"
"net/url"
"os"

"github.com/spf13/cobra"
Expand All @@ -12,7 +11,7 @@ import (
)

func updateKubeconfigCmd() *cobra.Command {
var kubeconfig, baseURL, projectName, organizationName string
var kubeconfig, hostname, projectName, organizationName string

cmd := &cobra.Command{
Use: "update-kubeconfig",
Expand All @@ -28,15 +27,11 @@ func updateKubeconfigCmd() *cobra.Command {
kubeconfigPath = clientcmd.RecommendedHomeFile
}

serverURL, err := url.Parse(baseURL)
if err != nil {
return fmt.Errorf("failed to parse base URL option: %w", err)
}

var path string
if projectName != "" {
serverURL.Path = "/apis/resourcemanager.datumapis.com/v1alpha/projects/" + projectName + "/control-plane"
path = "/apis/resourcemanager.datumapis.com/v1alpha/projects/" + projectName + "/control-plane"
} else {
serverURL.Path = "/apis/resourcemanager.datumapis.com/v1alpha/organizations/" + organizationName + "/control-plane"
path = "/apis/resourcemanager.datumapis.com/v1alpha/organizations/" + organizationName + "/control-plane"
}

// Load existing config
Expand All @@ -55,7 +50,7 @@ func updateKubeconfigCmd() *cobra.Command {
}

cfg.Clusters[clusterName] = &api.Cluster{
Server: serverURL.String(),
Server: fmt.Sprintf("https://%s%s", hostname, path),
}

cfg.Contexts[clusterName] = &api.Context{
Expand All @@ -65,9 +60,14 @@ func updateKubeconfigCmd() *cobra.Command {
cfg.CurrentContext = clusterName
cfg.AuthInfos["datum-user"] = &api.AuthInfo{
Exec: &api.ExecConfig{
InstallHint: execPluginInstallHint,
Command: "datumctl",
Args: []string{"auth", "get-token", "--output=client.authentication.k8s.io/v1"},
InstallHint: execPluginInstallHint,
Command: "datumctl",
Args: []string{
"auth",
"get-token",
fmt.Sprintf("--hostname=%s", hostname),
"--output=client.authentication.k8s.io/v1",
},
APIVersion: "client.authentication.k8s.io/v1",
ProvideClusterInfo: true,
InteractiveMode: "IfAvailable",
Expand All @@ -85,7 +85,7 @@ func updateKubeconfigCmd() *cobra.Command {
}

cmd.Flags().StringVar(&kubeconfig, "kubeconfig", "", "Path to the kubeconfig file")
cmd.Flags().StringVar(&baseURL, "base-url", "https://api.datum.net", "The base URL of the Datum Cloud API")
cmd.Flags().StringVar(&hostname, "hostname", "api.datum.net", "The hostname of the Datum Cloud instance to authenticate with")
cmd.Flags().StringVar(&projectName, "project", "", "Configure kubectl to access a specific project's control plane instead of the core control plane.")
cmd.Flags().StringVar(&organizationName, "organization", "", "The organization name that is being connected to.")

Expand Down
2 changes: 1 addition & 1 deletion internal/datum/api_token_credentials.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ type tokenResponse struct {
func (s *apiTokenSource) Token() (*oauth2.Token, error) {
client := http.DefaultClient

url := fmt.Sprintf("https://%s/oauth/token/exchange", s.Hostname)
url := fmt.Sprintf("https://%s/datum-os/oauth/token/exchange", s.Hostname)

req, err := http.NewRequestWithContext(context.Background(), http.MethodGet, url, nil)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion internal/resourcemanager/organizations.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func (r *OrganizationsAPI) ListOrganizations(ctx context.Context, req *resourcem
httpReq, err := http.NewRequestWithContext(
ctx,
http.MethodPost,
fmt.Sprintf("https://%s/query", r.Hostname),
fmt.Sprintf("https://%s/datum-os/query", r.Hostname),
strings.NewReader(getAllOrganizationsRequest),
)
if err != nil {
Expand Down

0 comments on commit 8f93b96

Please sign in to comment.