-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathclient.go
40 lines (34 loc) · 1.17 KB
/
client.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package gke
import (
"context"
"github.com/rancher/gke-operator/pkg/gke/services"
"golang.org/x/oauth2"
"golang.org/x/oauth2/google"
gkeapi "google.golang.org/api/container/v1"
"google.golang.org/api/option"
)
// GetGKEClient accepts a JSON credential string and returns a Service client.
func GetGKEClient(ctx context.Context, credential string) (*gkeapi.Service, error) {
ts, err := GetTokenSource(ctx, credential)
if err != nil {
return nil, err
}
return getServiceClientWithTokenSource(ctx, ts)
}
func GetGKEClusterClient(ctx context.Context, credential string) (services.GKEClusterService, error) {
ts, err := GetTokenSource(ctx, credential)
if err != nil {
return nil, err
}
return services.NewGKEClusterService(ctx, ts)
}
func getServiceClientWithTokenSource(ctx context.Context, ts oauth2.TokenSource) (*gkeapi.Service, error) {
return gkeapi.NewService(ctx, option.WithHTTPClient(oauth2.NewClient(ctx, ts)))
}
func GetTokenSource(ctx context.Context, credential string) (oauth2.TokenSource, error) {
ts, err := google.CredentialsFromJSON(ctx, []byte(credential), gkeapi.CloudPlatformScope)
if err != nil {
return nil, err
}
return ts.TokenSource, nil
}