Skip to content

Commit

Permalink
Add GetKubeConfigWithExpiry (digitalocean#334) (digitalocean#382)
Browse files Browse the repository at this point in the history
* Add GetKubeConfigWithExpiry (digitalocean#334)

* Add expiry_seconds assert in test (digitalocean#334)

Co-authored-by: Andrew Starr-Bochicchio <andrewsomething@users.noreply.github.com>
  • Loading branch information
ivanlemeshev and andrewsomething authored Oct 7, 2020
1 parent 92b1412 commit 02e18c0
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
22 changes: 22 additions & 0 deletions kubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ type KubernetesService interface {
GetUser(context.Context, string) (*KubernetesClusterUser, *Response, error)
GetUpgrades(context.Context, string) ([]*KubernetesVersion, *Response, error)
GetKubeConfig(context.Context, string) (*KubernetesClusterConfig, *Response, error)
GetKubeConfigWithExpiry(context.Context, string, int64) (*KubernetesClusterConfig, *Response, error)
GetCredentials(context.Context, string, *KubernetesClusterCredentialsGetRequest) (*KubernetesClusterCredentials, *Response, error)
List(context.Context, *ListOptions) ([]*KubernetesCluster, *Response, error)
Update(context.Context, string, *KubernetesClusterUpdateRequest) (*KubernetesCluster, *Response, error)
Expand Down Expand Up @@ -555,6 +556,27 @@ func (svc *KubernetesServiceOp) GetKubeConfig(ctx context.Context, clusterID str
return res, resp, nil
}

// GetKubeConfigWithExpiry returns a Kubernetes config file for the specified cluster with expiry_seconds.
func (svc *KubernetesServiceOp) GetKubeConfigWithExpiry(ctx context.Context, clusterID string, expirySeconds int64) (*KubernetesClusterConfig, *Response, error) {
path := fmt.Sprintf("%s/%s/kubeconfig", kubernetesClustersPath, clusterID)
req, err := svc.client.NewRequest(ctx, http.MethodGet, path, nil)
if err != nil {
return nil, nil, err
}
q := req.URL.Query()
q.Add("expiry_seconds", fmt.Sprintf("%d", expirySeconds))
req.URL.RawQuery = q.Encode()
configBytes := bytes.NewBuffer(nil)
resp, err := svc.client.Do(ctx, req, configBytes)
if err != nil {
return nil, resp, err
}
res := &KubernetesClusterConfig{
KubeconfigYAML: configBytes.Bytes(),
}
return res, resp, nil
}

// GetCredentials returns a Kubernetes API server credentials for the specified cluster.
func (svc *KubernetesServiceOp) GetCredentials(ctx context.Context, clusterID string, get *KubernetesClusterCredentialsGetRequest) (*KubernetesClusterCredentials, *Response, error) {
path := fmt.Sprintf("%s/%s/credentials", kubernetesClustersPath, clusterID)
Expand Down
20 changes: 20 additions & 0 deletions kubernetes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,26 @@ func TestKubernetesClusters_GetKubeConfig(t *testing.T) {
require.Equal(t, blob, got.KubeconfigYAML)
}

func TestKubernetesClusters_GetKubeConfigWithExpiry(t *testing.T) {
setup()
defer teardown()

kubeSvc := client.Kubernetes
want := "some YAML"
blob := []byte(want)
mux.HandleFunc("/v2/kubernetes/clusters/deadbeef-dead-4aa5-beef-deadbeef347d/kubeconfig", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodGet)
expirySeconds, ok := r.URL.Query()["expiry_seconds"]
assert.True(t, ok)
assert.Len(t, expirySeconds, 1)
assert.Contains(t, expirySeconds, "3600")
fmt.Fprint(w, want)
})
got, _, err := kubeSvc.GetKubeConfigWithExpiry(ctx, "deadbeef-dead-4aa5-beef-deadbeef347d", 3600)
require.NoError(t, err)
require.Equal(t, blob, got.KubeconfigYAML)
}

func TestKubernetesClusters_GetCredentials(t *testing.T) {
setup()
defer teardown()
Expand Down

0 comments on commit 02e18c0

Please sign in to comment.