Skip to content

Commit cd67c25

Browse files
authored
support for /project endpoint (GetKubeconfig) (#4)
1 parent d3de6b9 commit cd67c25

File tree

10 files changed

+562
-43
lines changed

10 files changed

+562
-43
lines changed

example/README.md

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,34 @@ ceClient, err := ibmcloudcodeenginev1.NewIbmCloudCodeEngineV1(&ibmcloudcodeengin
2727
})
2828
```
2929

30+
### Use an HTTP library to get a Delegated Refresh Token from IAM
31+
```go
32+
iamRequestData := url.Values{}
33+
iamRequestData.Set("grant_type", "urn:ibm:params:oauth:grant-type:apikey")
34+
iamRequestData.Set("apikey", os.Getenv("CE_API_KEY"))
35+
iamRequestData.Set("response_type", "delegated_refresh_token")
36+
iamRequestData.Set("receiver_client_ids", "ce")
37+
iamRequestData.Set("delegated_refresh_token_expiry", "3600")
38+
39+
client := &http.Client{}
40+
req, _ := http.NewRequest("POST", "https://iam.cloud.ibm.com/identity/token", strings.NewReader(iamRequestData.Encode()))
41+
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
42+
resp, _ := client.Do(req)
43+
44+
var iamResponseData map[string]string
45+
json.NewDecoder(resp.Body).Decode(&iamResponseData)
46+
delegatedRefreshToken := iamResponseData["delegated_refresh_token"]
47+
```
48+
3049
### Use the Code Engine client to get a Kubernetes config
3150
```go
3251
projectID := os.Getenv("CE_PROJECT_ID")
33-
iamToken, _ := authenticator.RequestToken()
34-
refreshToken := iamToken.RefreshToken
35-
result, _, err := ceClient.ListKubeconfig(&ibmcloudcodeenginev1.ListKubeconfigOptions{
36-
RefreshToken: &refreshToken,
37-
ID: &projectID,
52+
result, _, err := ceClient.GetKubeconfig(&ibmcloudcodeenginev1.GetKubeconfigOptions{
53+
XDelegatedRefreshToken: &delegatedRefreshToken,
54+
ID: &projectID,
3855
})
3956
```
57+
58+
## Deprecated endpoint
59+
60+
The `/namespaces/{id}/config` endpoint function, `ListKubeconfig()`, is deprecated, and will be removed before Code Engine is out of Beta. Please use the `GetKubeconfig` function, demonstrated in the example above.

example/example.go

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
package main
22

33
import (
4+
"encoding/json"
45
"fmt"
6+
"net/http"
7+
"net/url"
58
"os"
9+
"strings"
610

711
"github.com/IBM/code-engine-go-sdk/ibmcloudcodeenginev1"
812
"github.com/IBM/go-sdk-core/v4/core"
@@ -43,23 +47,42 @@ func main() {
4347
return
4448
}
4549

46-
// Get an IAM token
47-
iamToken, err := authenticator.RequestToken()
50+
// Use the http library to get an IAM Delegated Refresh Token
51+
iamRequestData := url.Values{}
52+
iamRequestData.Set("grant_type", "urn:ibm:params:oauth:grant-type:apikey")
53+
iamRequestData.Set("apikey", os.Getenv("CE_API_KEY"))
54+
iamRequestData.Set("response_type", "delegated_refresh_token")
55+
iamRequestData.Set("receiver_client_ids", "ce")
56+
iamRequestData.Set("delegated_refresh_token_expiry", "3600")
57+
58+
client := &http.Client{}
59+
req, err := http.NewRequest("POST", "https://iam.cloud.ibm.com/identity/token", strings.NewReader(iamRequestData.Encode()))
4860
if err != nil {
49-
fmt.Printf("RequestToken error: %s\n", err.Error())
61+
fmt.Printf("NewRequest err: %s\n", err)
5062
os.Exit(1)
5163
return
5264
}
65+
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
66+
resp, err := client.Do(req)
67+
if err != nil {
68+
fmt.Printf("POST /identity/token err: %s\n", err)
69+
os.Exit(1)
70+
return
71+
}
72+
73+
var iamResponseData map[string]string
74+
json.NewDecoder(resp.Body).Decode(&iamResponseData)
75+
resp.Body.Close()
76+
delegatedRefreshToken := iamResponseData["delegated_refresh_token"]
5377

5478
// Get Code Engine project config using the Code Engine Client
5579
projectID := os.Getenv("CE_PROJECT_ID")
56-
refreshToken := iamToken.RefreshToken
57-
result, _, err := ceClient.ListKubeconfig(&ibmcloudcodeenginev1.ListKubeconfigOptions{
58-
RefreshToken: &refreshToken,
59-
ID: &projectID,
80+
result, _, err := ceClient.GetKubeconfig(&ibmcloudcodeenginev1.GetKubeconfigOptions{
81+
XDelegatedRefreshToken: &delegatedRefreshToken,
82+
ID: &projectID,
6083
})
6184
if err != nil {
62-
fmt.Printf("ListKubeconfig error: %s\n", err.Error())
85+
fmt.Printf("GetKubeconfig error: %s\n", err.Error())
6386
os.Exit(1)
6487
return
6588
}
@@ -98,5 +121,4 @@ func main() {
98121
return
99122
}
100123
fmt.Printf("Project %s has %d configmaps.\n", os.Getenv("CE_PROJECT_ID"), len(configMapList.Items))
101-
102124
}

example/example_deprecated.go

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"os"
6+
7+
"github.com/IBM/code-engine-go-sdk/ibmcloudcodeenginev1"
8+
"github.com/IBM/go-sdk-core/v4/core"
9+
10+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
11+
"k8s.io/client-go/kubernetes"
12+
_ "k8s.io/client-go/plugin/pkg/client/auth/oidc"
13+
"k8s.io/client-go/tools/clientcmd"
14+
)
15+
16+
func deprecated() {
17+
18+
// Validate environment
19+
requiredEnvs := []string{"CE_API_KEY", "CE_PROJECT_REGION", "CE_PROJECT_ID"}
20+
for _, env := range requiredEnvs {
21+
if os.Getenv(env) == "" {
22+
fmt.Printf("Environment variable %s must be set\n", env)
23+
os.Exit(1)
24+
return
25+
}
26+
}
27+
28+
// Create an IAM authenticator.
29+
authenticator := &core.IamAuthenticator{
30+
ApiKey: os.Getenv("CE_API_KEY"),
31+
ClientId: "bx",
32+
ClientSecret: "bx",
33+
}
34+
35+
// Setup a Code Engine client
36+
ceClient, err := ibmcloudcodeenginev1.NewIbmCloudCodeEngineV1(&ibmcloudcodeenginev1.IbmCloudCodeEngineV1Options{
37+
Authenticator: authenticator,
38+
URL: "https://api." + os.Getenv("CE_PROJECT_REGION") + ".codeengine.cloud.ibm.com/api/v1",
39+
})
40+
if err != nil {
41+
fmt.Printf("NewIbmCloudCodeEngineV1 error: %s\n", err.Error())
42+
os.Exit(1)
43+
return
44+
}
45+
46+
// Get an IAM token
47+
iamToken, err := authenticator.RequestToken()
48+
if err != nil {
49+
fmt.Printf("RequestToken error: %s\n", err.Error())
50+
os.Exit(1)
51+
return
52+
}
53+
54+
// Get Code Engine project config using the Code Engine Client
55+
projectID := os.Getenv("CE_PROJECT_ID")
56+
refreshToken := iamToken.RefreshToken
57+
result, _, err := ceClient.ListKubeconfig(&ibmcloudcodeenginev1.ListKubeconfigOptions{
58+
RefreshToken: &refreshToken,
59+
ID: &projectID,
60+
})
61+
if err != nil {
62+
fmt.Printf("ListKubeconfig error: %s\n", err.Error())
63+
os.Exit(1)
64+
return
65+
}
66+
67+
// Get Kubernetes client using Code Engine project config
68+
kubeConfig, err := clientcmd.NewClientConfigFromBytes([]byte(*result))
69+
if err != nil {
70+
fmt.Printf("NewClientConfigFromBytes error: %s\n", err.Error())
71+
os.Exit(1)
72+
return
73+
}
74+
kubeClientConfig, err := kubeConfig.ClientConfig()
75+
if err != nil {
76+
fmt.Printf("ClientConfig error: %s\n", err.Error())
77+
os.Exit(1)
78+
return
79+
}
80+
kubeClient, err := kubernetes.NewForConfig(kubeClientConfig)
81+
if err != nil {
82+
fmt.Printf("NewForConfig error: %s\n", err.Error())
83+
os.Exit(1)
84+
return
85+
}
86+
87+
// Get something from project
88+
namespace, _, err := kubeConfig.Namespace()
89+
if err != nil {
90+
fmt.Printf("Namespace error: %s\n", err.Error())
91+
os.Exit(1)
92+
return
93+
}
94+
configMapList, err := kubeClient.CoreV1().ConfigMaps(namespace).List(metav1.ListOptions{})
95+
if err != nil {
96+
fmt.Printf("Pods list error: %s\n", err.Error())
97+
os.Exit(1)
98+
return
99+
}
100+
fmt.Printf("Project %s has %d configmaps.\n", os.Getenv("CE_PROJECT_ID"), len(configMapList.Items))
101+
102+
}

example/go.mod

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,9 @@ go 1.13
44

55
require (
66
github.com/IBM/code-engine-go-sdk v0.0.0-00010101000000-000000000000
7-
github.com/IBM/go-sdk-core/v4 v4.6.1
7+
github.com/IBM/go-sdk-core/v4 v4.10.0
88
github.com/asaskevich/govalidator v0.0.0-20200907205600-7a23bdc65eef // indirect
99
github.com/go-openapi/errors v0.19.7 // indirect
10-
github.com/go-openapi/strfmt v0.19.6 // indirect
1110
github.com/gogo/protobuf v1.3.1 // indirect
1211
github.com/google/gofuzz v1.2.0 // indirect
1312
github.com/googleapis/gnostic v0.3.1 // indirect

example/go.sum

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ github.com/IBM/go-sdk-core/v4 v4.6.0 h1:HPaNgDpQJlm0EbJ+8Uo+F2aZiCGNeHNRMdNj3C0A
3838
github.com/IBM/go-sdk-core/v4 v4.6.0/go.mod h1:lTUXbqIX6/aAbSCkP6q59+dyFsTwZAc0ewRS2vJWVbg=
3939
github.com/IBM/go-sdk-core/v4 v4.6.1 h1:T/ITOcjCU1KEOhkjEjH4oMA9U+6knNUh5eobeOr3+CM=
4040
github.com/IBM/go-sdk-core/v4 v4.6.1/go.mod h1:lTUXbqIX6/aAbSCkP6q59+dyFsTwZAc0ewRS2vJWVbg=
41+
github.com/IBM/go-sdk-core/v4 v4.10.0 h1:aLoKusSFVsxMJeKHf8csj9tBWt4Y50kVvfxoKh6scN0=
42+
github.com/IBM/go-sdk-core/v4 v4.10.0/go.mod h1:0uz2ca0MZ2DwsBRGl9Jp3EaCTqxmKZTdvV/CkCB7JnI=
4143
github.com/asaskevich/govalidator v0.0.0-20190424111038-f61b66f89f4a h1:idn718Q4B6AGu/h5Sxe66HYVdqdGu2l9Iebqhi/AEoA=
4244
github.com/asaskevich/govalidator v0.0.0-20190424111038-f61b66f89f4a/go.mod h1:lB+ZfQJz7igIIfQNfa7Ml4HSf2uFQQRzpGGRXenZAgY=
4345
github.com/asaskevich/govalidator v0.0.0-20200907205600-7a23bdc65eef h1:46PFijGLmAjMPwCCCo7Jf0W6f9slllCkkv7vyc1yOSg=
@@ -75,6 +77,8 @@ github.com/go-openapi/strfmt v0.19.5 h1:0utjKrw+BAh8s57XE9Xz8DUBsVvPmRUB6styvl9w
7577
github.com/go-openapi/strfmt v0.19.5/go.mod h1:eftuHTlB/dI8Uq8JJOyRlieZf+WkkxUuk0dgdHXr2Qk=
7678
github.com/go-openapi/strfmt v0.19.6 h1:epWc+q5qSgsy7A7+/HYyxLF37vLEYdPSkNB9G8mRqjw=
7779
github.com/go-openapi/strfmt v0.19.6/go.mod h1:eftuHTlB/dI8Uq8JJOyRlieZf+WkkxUuk0dgdHXr2Qk=
80+
github.com/go-openapi/strfmt v0.19.10 h1:FEv6Pt/V4wLwP4vOCZbWlpfmi8kj4UiRip34IDE6SGw=
81+
github.com/go-openapi/strfmt v0.19.10/go.mod h1:qBBipho+3EoIqn6YDI+4RnQEtj6jT/IdKm+PAlXxSUc=
7882
github.com/go-playground/locales v0.13.0 h1:HyWk6mgj5qFqCT5fjGBuRArbVDfE4hi8+e8ceBS/t7Q=
7983
github.com/go-playground/locales v0.13.0/go.mod h1:taPMhCMXrRLJO55olJkUXHZBHCxTMfnGwq/HNwmWNS8=
8084
github.com/go-playground/universal-translator v0.17.0 h1:icxd5fm+REJzpZx7ZfpaD876Lmtgy7VtROAbHHXk8no=
@@ -169,6 +173,11 @@ github.com/googleapis/gnostic v0.3.1 h1:WeAefnSUHlBb0iJKwxFDZdbfGwkd7xRNuV+IpXMJ
169173
github.com/googleapis/gnostic v0.3.1/go.mod h1:on+2t9HRStVgn95RSsFWFz+6Q0Snyqv1awfrALZdbtU=
170174
github.com/googleapis/gnostic v0.5.1 h1:A8Yhf6EtqTv9RMsU6MQTyrtV1TjWlR6xU9BsZIwuTCM=
171175
github.com/googleapis/gnostic v0.5.1/go.mod h1:6U4PtQXGIEt/Z3h5MAT7FNofLnw9vXk2cUuW7uA/OeU=
176+
github.com/hashicorp/go-cleanhttp v0.5.1 h1:dH3aiDG9Jvb5r5+bYHsikaOUIpcM0xvgMXVoDkXMzJM=
177+
github.com/hashicorp/go-cleanhttp v0.5.1/go.mod h1:JpRdi6/HCYpAwUzNwuwqhbovhLtngrth3wmdIIUrZ80=
178+
github.com/hashicorp/go-hclog v0.9.2/go.mod h1:5CU+agLiy3J7N7QjHK5d05KxGsuXiQLrjA0H7acj2lQ=
179+
github.com/hashicorp/go-retryablehttp v0.6.6 h1:HJunrbHTDDbBb/ay4kxa1n+dLmttUlnP3V9oNE4hmsM=
180+
github.com/hashicorp/go-retryablehttp v0.6.6/go.mod h1:vAew36LZh98gCBJNLH42IQ1ER/9wtLZZ8meHqQvEYWY=
172181
github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
173182
github.com/hashicorp/golang-lru v0.5.1/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
174183
github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
@@ -220,10 +229,12 @@ github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+W
220229
github.com/onsi/ginkgo v1.12.1/go.mod h1:zj2OWP4+oCPe1qIXoGWkgMRwljMUYCdkwsT2108oapk=
221230
github.com/onsi/ginkgo v1.14.1 h1:jMU0WaQrP0a/YAEq8eJmJKjBoMs+pClEr1vDMlM/Do4=
222231
github.com/onsi/ginkgo v1.14.1/go.mod h1:iSB4RoI2tjJc9BBv4NKIKWKya62Rps+oPG/Lv9klQyY=
232+
github.com/onsi/ginkgo v1.14.2/go.mod h1:iSB4RoI2tjJc9BBv4NKIKWKya62Rps+oPG/Lv9klQyY=
223233
github.com/onsi/gomega v1.7.1/go.mod h1:XdKZgCCFLUoM/7CFJVPcG8C1xQ1AJ0vpAezJrB7JYyY=
224234
github.com/onsi/gomega v1.10.1/go.mod h1:iN09h71vgCQne3DLsj+A5owkum+a2tYe+TOCB1ybHNo=
225235
github.com/onsi/gomega v1.10.2 h1:aY/nuoWlKJud2J6U0E3NWsjlg+0GtwXxgEqthRdzlcs=
226236
github.com/onsi/gomega v1.10.2/go.mod h1:iN09h71vgCQne3DLsj+A5owkum+a2tYe+TOCB1ybHNo=
237+
github.com/onsi/gomega v1.10.3/go.mod h1:V9xEwhxec5O8UDM77eCW8vLymOMltsqPVYWrpDsH8xc=
227238
github.com/pelletier/go-toml v1.7.0/go.mod h1:vwGMzjaWMwyfHwgIBhI2YUM4fB6nL6lVAvS1LBMMhTE=
228239
github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
229240
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
@@ -341,6 +352,7 @@ golang.org/x/net v0.0.0-20200707034311-ab3426394381 h1:VXak5I6aEWmAXeQjA+QSZzlgN
341352
golang.org/x/net v0.0.0-20200707034311-ab3426394381/go.mod h1:/O7V0waA8r7cgGh81Ro3o1hOxt32SMVPicZroKQ2sZA=
342353
golang.org/x/net v0.0.0-20200822124328-c89045814202 h1:VvcQYSHwXgi7W+TpUR6A9g6Up98WAHf3f/ulnJ62IyA=
343354
golang.org/x/net v0.0.0-20200822124328-c89045814202/go.mod h1:/O7V0waA8r7cgGh81Ro3o1hOxt32SMVPicZroKQ2sZA=
355+
golang.org/x/net v0.0.0-20201006153459-a7d1128ccaa0/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU=
344356
golang.org/x/net v0.0.0-20201010224723-4f7140c49acb h1:mUVeFHoDKis5nxCAzoAi7E8Ghb86EXh/RK6wtvJIqRY=
345357
golang.org/x/net v0.0.0-20201010224723-4f7140c49acb/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU=
346358
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be h1:vEDujvNQGv4jgYKudGeI/+DAX4Jffq6hpD55MmoEvKs=

go.mod

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@ module github.com/IBM/code-engine-go-sdk
33
go 1.13
44

55
require (
6-
github.com/IBM/go-sdk-core/v4 v4.5.0
7-
github.com/go-openapi/strfmt v0.19.5
6+
github.com/IBM/go-sdk-core/v4 v4.10.0
7+
github.com/go-openapi/strfmt v0.19.10
88
github.com/imdario/mergo v0.3.11 // indirect
9-
github.com/onsi/ginkgo v1.14.1
10-
github.com/onsi/gomega v1.10.2
9+
github.com/onsi/ginkgo v1.14.2
10+
github.com/onsi/gomega v1.10.3
1111
github.com/stretchr/testify v1.6.1
1212
golang.org/x/time v0.0.0-20200630173020-3af7569d3a1e // indirect
1313
k8s.io/api v0.19.2 // indirect

0 commit comments

Comments
 (0)