forked from kubeflow/kubeflow
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[gcp-deployer] add service account key handler and a check health han…
…dler (kubeflow#1329) * add service account key handler to go backend, and a check health handler * handle review feedback * update struct name
- Loading branch information
1 parent
49d6f99
commit 0e402a6
Showing
4 changed files
with
253 additions
and
21 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,100 @@ | ||
package app | ||
|
||
import ( | ||
"golang.org/x/net/context" | ||
"cloud.google.com/go/container/apiv1" | ||
iamadmin "cloud.google.com/go/iam/admin/apiv1" | ||
"google.golang.org/api/option" | ||
"golang.org/x/oauth2" | ||
"k8s.io/client-go/rest" | ||
containerpb "google.golang.org/genproto/googleapis/container/v1" | ||
"google.golang.org/genproto/googleapis/iam/admin/v1" | ||
"fmt" | ||
log "github.com/sirupsen/logrus" | ||
"k8s.io/api/core/v1" | ||
meta_v1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
clientset "k8s.io/client-go/kubernetes" | ||
"encoding/base64" | ||
) | ||
|
||
|
||
type InsertSaKeyRequest struct { | ||
Cluster string | ||
Namespace string | ||
Project string | ||
SecretKey string | ||
SecretName string | ||
ServiceAccount string | ||
Token string | ||
Zone string | ||
} | ||
|
||
func buildClusterConfig(ctx context.Context, ts oauth2.TokenSource, request InsertSaKeyRequest) (*rest.Config, error) { | ||
c, err := container.NewClusterManagerClient(ctx, option.WithTokenSource(ts)) | ||
if err != nil { | ||
return nil, err | ||
} | ||
req := &containerpb.GetClusterRequest{ | ||
ProjectId: request.Project, | ||
Zone: request.Zone, | ||
ClusterId: request.Cluster, | ||
} | ||
resp, err := c.GetCluster(ctx, req) | ||
if err != nil { | ||
return nil, err | ||
} | ||
Token, err := ts.Token() | ||
caDec, _ := base64.StdEncoding.DecodeString(resp.MasterAuth.ClusterCaCertificate) | ||
return &rest.Config{ | ||
Host: "https://" + resp.Endpoint, | ||
BearerToken: Token.AccessToken, | ||
TLSClientConfig: rest.TLSClientConfig { | ||
CAData: []byte(string(caDec)), | ||
}, | ||
}, nil | ||
} | ||
|
||
func (s *ksServer) InsertSaKey(ctx context.Context, request InsertSaKeyRequest) error { | ||
ts := oauth2.StaticTokenSource(&oauth2.Token{ | ||
AccessToken: request.Token, | ||
}) | ||
k8sConfig, err := buildClusterConfig(ctx, ts, request) | ||
if err != nil { | ||
log.Errorf("Failed getting GKE cluster info: %v", err) | ||
return err | ||
} | ||
|
||
c, err := iamadmin.NewIamClient(ctx, option.WithTokenSource(ts)) | ||
if err != nil { | ||
log.Errorf("Cannot create iam admin client: %v", err) | ||
return err | ||
} | ||
createServiceAccountKeyRequest := admin.CreateServiceAccountKeyRequest{ | ||
Name: fmt.Sprintf("projects/%v/serviceAccounts/%v", request.Project, request.ServiceAccount), | ||
} | ||
|
||
s.iamMux.Lock() | ||
defer s.iamMux.Unlock() | ||
|
||
createdKey, err := c.CreateServiceAccountKey(ctx, &createServiceAccountKeyRequest) | ||
if err != nil { | ||
log.Errorf("Failed creating sa key: %v", err) | ||
return err | ||
} | ||
k8sClientset, err := clientset.NewForConfig(k8sConfig) | ||
secretData := make(map[string][]byte) | ||
secretData[request.SecretKey] = createdKey.PrivateKeyData | ||
_, err = k8sClientset.CoreV1().Secrets(request.Namespace).Create( | ||
&v1.Secret{ | ||
ObjectMeta: meta_v1.ObjectMeta{ | ||
Namespace: request.Namespace, | ||
Name: request.SecretName, | ||
}, | ||
Data: secretData, | ||
}) | ||
if err != nil { | ||
log.Errorf("Failed creating secret in GKE cluster: %v", err) | ||
return err | ||
} | ||
return 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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