Skip to content

feature: support for /project endpoint (GetKubeconfig) #4

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 17, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 26 additions & 5 deletions example/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,34 @@ ceClient, err := ibmcloudcodeenginev1.NewIbmCloudCodeEngineV1(&ibmcloudcodeengin
})
```

### Use an HTTP library to get a Delegated Refresh Token from IAM
```go
iamRequestData := url.Values{}
iamRequestData.Set("grant_type", "urn:ibm:params:oauth:grant-type:apikey")
iamRequestData.Set("apikey", os.Getenv("CE_API_KEY"))
iamRequestData.Set("response_type", "delegated_refresh_token")
iamRequestData.Set("receiver_client_ids", "ce")
iamRequestData.Set("delegated_refresh_token_expiry", "3600")

client := &http.Client{}
req, _ := http.NewRequest("POST", "https://iam.cloud.ibm.com/identity/token", strings.NewReader(iamRequestData.Encode()))
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
resp, _ := client.Do(req)

var iamResponseData map[string]string
json.NewDecoder(resp.Body).Decode(&iamResponseData)
delegatedRefreshToken := iamResponseData["delegated_refresh_token"]
```

### Use the Code Engine client to get a Kubernetes config
```go
projectID := os.Getenv("CE_PROJECT_ID")
iamToken, _ := authenticator.RequestToken()
refreshToken := iamToken.RefreshToken
result, _, err := ceClient.ListKubeconfig(&ibmcloudcodeenginev1.ListKubeconfigOptions{
RefreshToken: &refreshToken,
ID: &projectID,
result, _, err := ceClient.GetKubeconfig(&ibmcloudcodeenginev1.GetKubeconfigOptions{
XDelegatedRefreshToken: &delegatedRefreshToken,
ID: &projectID,
})
```

## Deprecated endpoint

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.
40 changes: 31 additions & 9 deletions example/example.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
package main

import (
"encoding/json"
"fmt"
"net/http"
"net/url"
"os"
"strings"

"github.com/IBM/code-engine-go-sdk/ibmcloudcodeenginev1"
"github.com/IBM/go-sdk-core/v4/core"
Expand Down Expand Up @@ -43,23 +47,42 @@ func main() {
return
}

// Get an IAM token
iamToken, err := authenticator.RequestToken()
// Use the http library to get an IAM Delegated Refresh Token
iamRequestData := url.Values{}
iamRequestData.Set("grant_type", "urn:ibm:params:oauth:grant-type:apikey")
iamRequestData.Set("apikey", os.Getenv("CE_API_KEY"))
iamRequestData.Set("response_type", "delegated_refresh_token")
iamRequestData.Set("receiver_client_ids", "ce")
iamRequestData.Set("delegated_refresh_token_expiry", "3600")

client := &http.Client{}
req, err := http.NewRequest("POST", "https://iam.cloud.ibm.com/identity/token", strings.NewReader(iamRequestData.Encode()))
if err != nil {
fmt.Printf("RequestToken error: %s\n", err.Error())
fmt.Printf("NewRequest err: %s\n", err)
os.Exit(1)
return
}
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
resp, err := client.Do(req)
if err != nil {
fmt.Printf("POST /identity/token err: %s\n", err)
os.Exit(1)
return
}

var iamResponseData map[string]string
json.NewDecoder(resp.Body).Decode(&iamResponseData)
resp.Body.Close()
delegatedRefreshToken := iamResponseData["delegated_refresh_token"]

// Get Code Engine project config using the Code Engine Client
projectID := os.Getenv("CE_PROJECT_ID")
refreshToken := iamToken.RefreshToken
result, _, err := ceClient.ListKubeconfig(&ibmcloudcodeenginev1.ListKubeconfigOptions{
RefreshToken: &refreshToken,
ID: &projectID,
result, _, err := ceClient.GetKubeconfig(&ibmcloudcodeenginev1.GetKubeconfigOptions{
XDelegatedRefreshToken: &delegatedRefreshToken,
ID: &projectID,
})
if err != nil {
fmt.Printf("ListKubeconfig error: %s\n", err.Error())
fmt.Printf("GetKubeconfig error: %s\n", err.Error())
os.Exit(1)
return
}
Expand Down Expand Up @@ -98,5 +121,4 @@ func main() {
return
}
fmt.Printf("Project %s has %d configmaps.\n", os.Getenv("CE_PROJECT_ID"), len(configMapList.Items))

}
102 changes: 102 additions & 0 deletions example/example_deprecated.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
package main

import (
"fmt"
"os"

"github.com/IBM/code-engine-go-sdk/ibmcloudcodeenginev1"
"github.com/IBM/go-sdk-core/v4/core"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
_ "k8s.io/client-go/plugin/pkg/client/auth/oidc"
"k8s.io/client-go/tools/clientcmd"
)

func deprecated() {

// Validate environment
requiredEnvs := []string{"CE_API_KEY", "CE_PROJECT_REGION", "CE_PROJECT_ID"}
for _, env := range requiredEnvs {
if os.Getenv(env) == "" {
fmt.Printf("Environment variable %s must be set\n", env)
os.Exit(1)
return
}
}

// Create an IAM authenticator.
authenticator := &core.IamAuthenticator{
ApiKey: os.Getenv("CE_API_KEY"),
ClientId: "bx",
ClientSecret: "bx",
}

// Setup a Code Engine client
ceClient, err := ibmcloudcodeenginev1.NewIbmCloudCodeEngineV1(&ibmcloudcodeenginev1.IbmCloudCodeEngineV1Options{
Authenticator: authenticator,
URL: "https://api." + os.Getenv("CE_PROJECT_REGION") + ".codeengine.cloud.ibm.com/api/v1",
})
if err != nil {
fmt.Printf("NewIbmCloudCodeEngineV1 error: %s\n", err.Error())
os.Exit(1)
return
}

// Get an IAM token
iamToken, err := authenticator.RequestToken()
if err != nil {
fmt.Printf("RequestToken error: %s\n", err.Error())
os.Exit(1)
return
}

// Get Code Engine project config using the Code Engine Client
projectID := os.Getenv("CE_PROJECT_ID")
refreshToken := iamToken.RefreshToken
result, _, err := ceClient.ListKubeconfig(&ibmcloudcodeenginev1.ListKubeconfigOptions{
RefreshToken: &refreshToken,
ID: &projectID,
})
if err != nil {
fmt.Printf("ListKubeconfig error: %s\n", err.Error())
os.Exit(1)
return
}

// Get Kubernetes client using Code Engine project config
kubeConfig, err := clientcmd.NewClientConfigFromBytes([]byte(*result))
if err != nil {
fmt.Printf("NewClientConfigFromBytes error: %s\n", err.Error())
os.Exit(1)
return
}
kubeClientConfig, err := kubeConfig.ClientConfig()
if err != nil {
fmt.Printf("ClientConfig error: %s\n", err.Error())
os.Exit(1)
return
}
kubeClient, err := kubernetes.NewForConfig(kubeClientConfig)
if err != nil {
fmt.Printf("NewForConfig error: %s\n", err.Error())
os.Exit(1)
return
}

// Get something from project
namespace, _, err := kubeConfig.Namespace()
if err != nil {
fmt.Printf("Namespace error: %s\n", err.Error())
os.Exit(1)
return
}
configMapList, err := kubeClient.CoreV1().ConfigMaps(namespace).List(metav1.ListOptions{})
if err != nil {
fmt.Printf("Pods list error: %s\n", err.Error())
os.Exit(1)
return
}
fmt.Printf("Project %s has %d configmaps.\n", os.Getenv("CE_PROJECT_ID"), len(configMapList.Items))

}
3 changes: 1 addition & 2 deletions example/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@ go 1.13

require (
github.com/IBM/code-engine-go-sdk v0.0.0-00010101000000-000000000000
github.com/IBM/go-sdk-core/v4 v4.6.1
github.com/IBM/go-sdk-core/v4 v4.10.0
github.com/asaskevich/govalidator v0.0.0-20200907205600-7a23bdc65eef // indirect
github.com/go-openapi/errors v0.19.7 // indirect
github.com/go-openapi/strfmt v0.19.6 // indirect
github.com/gogo/protobuf v1.3.1 // indirect
github.com/google/gofuzz v1.2.0 // indirect
github.com/googleapis/gnostic v0.3.1 // indirect
Expand Down
12 changes: 12 additions & 0 deletions example/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ github.com/IBM/go-sdk-core/v4 v4.6.0 h1:HPaNgDpQJlm0EbJ+8Uo+F2aZiCGNeHNRMdNj3C0A
github.com/IBM/go-sdk-core/v4 v4.6.0/go.mod h1:lTUXbqIX6/aAbSCkP6q59+dyFsTwZAc0ewRS2vJWVbg=
github.com/IBM/go-sdk-core/v4 v4.6.1 h1:T/ITOcjCU1KEOhkjEjH4oMA9U+6knNUh5eobeOr3+CM=
github.com/IBM/go-sdk-core/v4 v4.6.1/go.mod h1:lTUXbqIX6/aAbSCkP6q59+dyFsTwZAc0ewRS2vJWVbg=
github.com/IBM/go-sdk-core/v4 v4.10.0 h1:aLoKusSFVsxMJeKHf8csj9tBWt4Y50kVvfxoKh6scN0=
github.com/IBM/go-sdk-core/v4 v4.10.0/go.mod h1:0uz2ca0MZ2DwsBRGl9Jp3EaCTqxmKZTdvV/CkCB7JnI=
github.com/asaskevich/govalidator v0.0.0-20190424111038-f61b66f89f4a h1:idn718Q4B6AGu/h5Sxe66HYVdqdGu2l9Iebqhi/AEoA=
github.com/asaskevich/govalidator v0.0.0-20190424111038-f61b66f89f4a/go.mod h1:lB+ZfQJz7igIIfQNfa7Ml4HSf2uFQQRzpGGRXenZAgY=
github.com/asaskevich/govalidator v0.0.0-20200907205600-7a23bdc65eef h1:46PFijGLmAjMPwCCCo7Jf0W6f9slllCkkv7vyc1yOSg=
Expand Down Expand Up @@ -75,6 +77,8 @@ github.com/go-openapi/strfmt v0.19.5 h1:0utjKrw+BAh8s57XE9Xz8DUBsVvPmRUB6styvl9w
github.com/go-openapi/strfmt v0.19.5/go.mod h1:eftuHTlB/dI8Uq8JJOyRlieZf+WkkxUuk0dgdHXr2Qk=
github.com/go-openapi/strfmt v0.19.6 h1:epWc+q5qSgsy7A7+/HYyxLF37vLEYdPSkNB9G8mRqjw=
github.com/go-openapi/strfmt v0.19.6/go.mod h1:eftuHTlB/dI8Uq8JJOyRlieZf+WkkxUuk0dgdHXr2Qk=
github.com/go-openapi/strfmt v0.19.10 h1:FEv6Pt/V4wLwP4vOCZbWlpfmi8kj4UiRip34IDE6SGw=
github.com/go-openapi/strfmt v0.19.10/go.mod h1:qBBipho+3EoIqn6YDI+4RnQEtj6jT/IdKm+PAlXxSUc=
github.com/go-playground/locales v0.13.0 h1:HyWk6mgj5qFqCT5fjGBuRArbVDfE4hi8+e8ceBS/t7Q=
github.com/go-playground/locales v0.13.0/go.mod h1:taPMhCMXrRLJO55olJkUXHZBHCxTMfnGwq/HNwmWNS8=
github.com/go-playground/universal-translator v0.17.0 h1:icxd5fm+REJzpZx7ZfpaD876Lmtgy7VtROAbHHXk8no=
Expand Down Expand Up @@ -169,6 +173,11 @@ github.com/googleapis/gnostic v0.3.1 h1:WeAefnSUHlBb0iJKwxFDZdbfGwkd7xRNuV+IpXMJ
github.com/googleapis/gnostic v0.3.1/go.mod h1:on+2t9HRStVgn95RSsFWFz+6Q0Snyqv1awfrALZdbtU=
github.com/googleapis/gnostic v0.5.1 h1:A8Yhf6EtqTv9RMsU6MQTyrtV1TjWlR6xU9BsZIwuTCM=
github.com/googleapis/gnostic v0.5.1/go.mod h1:6U4PtQXGIEt/Z3h5MAT7FNofLnw9vXk2cUuW7uA/OeU=
github.com/hashicorp/go-cleanhttp v0.5.1 h1:dH3aiDG9Jvb5r5+bYHsikaOUIpcM0xvgMXVoDkXMzJM=
github.com/hashicorp/go-cleanhttp v0.5.1/go.mod h1:JpRdi6/HCYpAwUzNwuwqhbovhLtngrth3wmdIIUrZ80=
github.com/hashicorp/go-hclog v0.9.2/go.mod h1:5CU+agLiy3J7N7QjHK5d05KxGsuXiQLrjA0H7acj2lQ=
github.com/hashicorp/go-retryablehttp v0.6.6 h1:HJunrbHTDDbBb/ay4kxa1n+dLmttUlnP3V9oNE4hmsM=
github.com/hashicorp/go-retryablehttp v0.6.6/go.mod h1:vAew36LZh98gCBJNLH42IQ1ER/9wtLZZ8meHqQvEYWY=
github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
github.com/hashicorp/golang-lru v0.5.1/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
Expand Down Expand Up @@ -220,10 +229,12 @@ github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+W
github.com/onsi/ginkgo v1.12.1/go.mod h1:zj2OWP4+oCPe1qIXoGWkgMRwljMUYCdkwsT2108oapk=
github.com/onsi/ginkgo v1.14.1 h1:jMU0WaQrP0a/YAEq8eJmJKjBoMs+pClEr1vDMlM/Do4=
github.com/onsi/ginkgo v1.14.1/go.mod h1:iSB4RoI2tjJc9BBv4NKIKWKya62Rps+oPG/Lv9klQyY=
github.com/onsi/ginkgo v1.14.2/go.mod h1:iSB4RoI2tjJc9BBv4NKIKWKya62Rps+oPG/Lv9klQyY=
github.com/onsi/gomega v1.7.1/go.mod h1:XdKZgCCFLUoM/7CFJVPcG8C1xQ1AJ0vpAezJrB7JYyY=
github.com/onsi/gomega v1.10.1/go.mod h1:iN09h71vgCQne3DLsj+A5owkum+a2tYe+TOCB1ybHNo=
github.com/onsi/gomega v1.10.2 h1:aY/nuoWlKJud2J6U0E3NWsjlg+0GtwXxgEqthRdzlcs=
github.com/onsi/gomega v1.10.2/go.mod h1:iN09h71vgCQne3DLsj+A5owkum+a2tYe+TOCB1ybHNo=
github.com/onsi/gomega v1.10.3/go.mod h1:V9xEwhxec5O8UDM77eCW8vLymOMltsqPVYWrpDsH8xc=
github.com/pelletier/go-toml v1.7.0/go.mod h1:vwGMzjaWMwyfHwgIBhI2YUM4fB6nL6lVAvS1LBMMhTE=
github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
Expand Down Expand Up @@ -341,6 +352,7 @@ golang.org/x/net v0.0.0-20200707034311-ab3426394381 h1:VXak5I6aEWmAXeQjA+QSZzlgN
golang.org/x/net v0.0.0-20200707034311-ab3426394381/go.mod h1:/O7V0waA8r7cgGh81Ro3o1hOxt32SMVPicZroKQ2sZA=
golang.org/x/net v0.0.0-20200822124328-c89045814202 h1:VvcQYSHwXgi7W+TpUR6A9g6Up98WAHf3f/ulnJ62IyA=
golang.org/x/net v0.0.0-20200822124328-c89045814202/go.mod h1:/O7V0waA8r7cgGh81Ro3o1hOxt32SMVPicZroKQ2sZA=
golang.org/x/net v0.0.0-20201006153459-a7d1128ccaa0/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU=
golang.org/x/net v0.0.0-20201010224723-4f7140c49acb h1:mUVeFHoDKis5nxCAzoAi7E8Ghb86EXh/RK6wtvJIqRY=
golang.org/x/net v0.0.0-20201010224723-4f7140c49acb/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU=
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be h1:vEDujvNQGv4jgYKudGeI/+DAX4Jffq6hpD55MmoEvKs=
Expand Down
8 changes: 4 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ module github.com/IBM/code-engine-go-sdk
go 1.13

require (
github.com/IBM/go-sdk-core/v4 v4.5.0
github.com/go-openapi/strfmt v0.19.5
github.com/IBM/go-sdk-core/v4 v4.10.0
github.com/go-openapi/strfmt v0.19.10
github.com/imdario/mergo v0.3.11 // indirect
github.com/onsi/ginkgo v1.14.1
github.com/onsi/gomega v1.10.2
github.com/onsi/ginkgo v1.14.2
github.com/onsi/gomega v1.10.3
github.com/stretchr/testify v1.6.1
golang.org/x/time v0.0.0-20200630173020-3af7569d3a1e // indirect
k8s.io/api v0.19.2 // indirect
Expand Down
Loading