-
Notifications
You must be signed in to change notification settings - Fork 2.4k
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
[gcp-deployer] add service account key handler and a check health handler #1329
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this call synchronous? What does the caller of the SA key endpoint expect to happen after making the API call? Do they then have to wait or poll somehow if they want to block on this operation?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's synchronous. When handler returns, either the sa key already inserted into GKE or an error happened and included in response.