-
Notifications
You must be signed in to change notification settings - Fork 120
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
OIDC: allow retrieving kubeconfig from BTP (#2124)
- Loading branch information
Showing
2 changed files
with
164 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package cis | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"io" | ||
"net/http" | ||
|
||
"github.com/kyma-project/cli.v3/internal/clierror" | ||
) | ||
|
||
const environmentsEndpoint = "provisioning/v1/environments" | ||
|
||
type Labels struct { | ||
APIServerURL string `json:"APIServerURL"` | ||
KubeconfigURL string `json:"KubeconfigURL"` | ||
Name string `json:"Name"` | ||
} | ||
|
||
type environmentInstances struct { | ||
EnvironmentInstances []ProvisionResponse `json:"environmentInstances"` | ||
} | ||
|
||
func (c *LocalClient) GetKymaKubeconfig() (string, clierror.Error) { | ||
provisionURL := fmt.Sprintf("%s/%s", c.credentials.Endpoints.ProvisioningServiceURL, environmentsEndpoint) | ||
|
||
response, err := c.cis.get(provisionURL, requestOptions{}) | ||
if err != nil { | ||
// TODO: finish - error codes? | ||
return "", clierror.New(err.Error()) | ||
} | ||
|
||
defer response.Body.Close() | ||
|
||
return decodeResponse(response) | ||
|
||
} | ||
|
||
func decodeResponse(response *http.Response) (string, clierror.Error) { | ||
envInstances := environmentInstances{} | ||
err := json.NewDecoder(response.Body).Decode(&envInstances) | ||
if err != nil { | ||
return "", clierror.Wrap(err, clierror.New("failed to decode response")) | ||
} | ||
|
||
// we assume there can be only one Kyma environment in the BTP subaccount | ||
for _, env := range envInstances.EnvironmentInstances { | ||
if env.EnvironmentType == "kyma" { | ||
// parse labels to get kubeconfig URL | ||
labels := Labels{} | ||
err := json.Unmarshal([]byte(env.Labels), &labels) | ||
if err != nil { | ||
return "", clierror.Wrap(err, clierror.New("failed to unmarshal labels")) | ||
} | ||
|
||
kubeconfig, err := downloadKubeconfig(labels.KubeconfigURL) | ||
if err != nil { | ||
return "", clierror.Wrap(err, clierror.New("failed to get kubeconfig")) | ||
} | ||
return kubeconfig, nil | ||
} | ||
} | ||
|
||
return "", clierror.New("no Kyma environment found") | ||
} | ||
|
||
func downloadKubeconfig(url string) (string, error) { | ||
response, err := http.Get(url) | ||
if err != nil { | ||
return "", err | ||
} | ||
defer response.Body.Close() | ||
|
||
kubeconfig, err := io.ReadAll(response.Body) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
return string(kubeconfig), nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters