Skip to content

Commit

Permalink
feat: add support for gcloud cli (#330)
Browse files Browse the repository at this point in the history
Signed-off-by: Matheus Pimenta <[email protected]>
  • Loading branch information
matheuscscp authored Feb 16, 2025
1 parent ced63ed commit 9c42a52
Show file tree
Hide file tree
Showing 19 changed files with 492 additions and 218 deletions.
7 changes: 6 additions & 1 deletion cmd/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ import (
func newServerCommand() *cobra.Command {
var (
serverPort int
projectID string
workloadIdentityProvider string
nodePoolServiceAccountName string
nodePoolServiceAccountNamespace string
Expand Down Expand Up @@ -98,7 +99,7 @@ func newServerCommand() *cobra.Command {
Namespace: nodePoolServiceAccountNamespace,
}
}
googleCredentialsConfig, workloadIdentityPool, err := googlecredentials.NewConfig(googlecredentials.ConfigOptions{
googleCredentialsConfig, numericProjectID, workloadIdentityPool, err := googlecredentials.NewConfig(googlecredentials.ConfigOptions{
WorkloadIdentityProvider: workloadIdentityProvider,
})
if err != nil {
Expand Down Expand Up @@ -235,6 +236,8 @@ func newServerCommand() *cobra.Command {
ServiceAccountTokens: serviceAccountTokens,
MetricsRegistry: metricsRegistry,
NodePoolServiceAccount: nodePoolServiceAccount,
ProjectID: projectID,
NumericProjectID: numericProjectID,
WorkloadIdentityPool: workloadIdentityPool,
})

Expand All @@ -250,6 +253,8 @@ func newServerCommand() *cobra.Command {

cmd.Flags().IntVar(&serverPort, "server-port", 8080,
"Network address where the metadata server must listen on")
cmd.Flags().StringVar(&projectID, "project-id", "",
"Project ID of the GCP project where the GCP Workload Identity Provider is configured")
cmd.Flags().StringVar(&workloadIdentityProvider, "workload-identity-provider", "",
"Mandatory fully-qualified resource name of the GCP Workload Identity Provider (projects/<project_number>/locations/global/workloadIdentityPools/<pool_name>/providers/<provider_name>)")
cmd.Flags().StringVar(&nodePoolServiceAccountName, "node-pool-service-account-name", "",
Expand Down
16 changes: 15 additions & 1 deletion e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ type emulator struct {

type pod struct {
name string
file string
serviceAccountName string
hostNetwork bool
nodePool nodePool
Expand Down Expand Up @@ -129,6 +130,15 @@ func TestEndToEnd(t *testing.T) {
namespace: "kube-system",
},
},
{
name: "test-gcloud",
file: "pod-gcloud.yaml",
serviceAccountName: "test",
nodePool: nodePool{
name: "gke-metadata-server",
namespace: "kube-system",
},
},
// for host network pods the service account is retrieved from the emulator config
{
name: "test-host-network",
Expand Down Expand Up @@ -278,7 +288,11 @@ func applyPods(t *testing.T, pods []pod) {

// execute pod template
var pod string
b, err := os.ReadFile("testdata/pod.yaml")
file := "pod.yaml"
if p.file != "" {
file = p.file
}
b, err := os.ReadFile("testdata/" + file)
require.NoError(t, err)
serviceAccountName := "default"
if sa := p.serviceAccountName; sa != "" {
Expand Down
1 change: 1 addition & 0 deletions helm/gke-metadata-server/templates/daemonset.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ spec:
privileged: true
args:
- server
- --project-id={{ .Values.config.projectID }}
- --workload-identity-provider={{ .Values.config.workloadIdentityProvider }}
{{- if (.Values.config.nodePool | default dict).enable }}
- --node-pool-service-account-name={{ .Release.Name }}
Expand Down
2 changes: 2 additions & 0 deletions helm/gke-metadata-server/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
# Declare variables to be passed into your templates.

config:
# Mandatory GCP project ID.
projectID: ""
# Mandatory fully-qualified name of the GCP Workload Identity Provider.
# This full name can be retrieved on the Google Cloud Console webpage for the provider.
# Must match the pattern: projects/<gcp_project_number>/locations/global/workloadIdentityPools/<pool_name>/providers/<provider_name>
Expand Down
11 changes: 6 additions & 5 deletions internal/googlecredentials/google_credentials.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ type (
}
)

var workloadIdentityProviderRegex = regexp.MustCompile(`^projects/\d+/locations/global/workloadIdentityPools/([^/]+)/providers/[^/]+$`)
var workloadIdentityProviderRegex = regexp.MustCompile(`^projects/(\d+)/locations/global/workloadIdentityPools/([^/]+)/providers/[^/]+$`)

func AccessScopes() []string {
return []string{
Expand All @@ -50,13 +50,14 @@ func AccessScopes() []string {
}
}

func NewConfig(opts ConfigOptions) (*Config, string, error) {
func NewConfig(opts ConfigOptions) (*Config, string, string, error) {
if !workloadIdentityProviderRegex.MatchString(opts.WorkloadIdentityProvider) {
return nil, "", fmt.Errorf("workload identity provider name does not match pattern %s",
return nil, "", "", fmt.Errorf("workload identity provider name does not match pattern %s",
workloadIdentityProviderRegex.String())
}
workloadIdentityPool := workloadIdentityProviderRegex.FindStringSubmatch(opts.WorkloadIdentityProvider)[1]
return &Config{opts}, workloadIdentityPool, nil
numericProjectID := workloadIdentityProviderRegex.FindStringSubmatch(opts.WorkloadIdentityProvider)[1]
workloadIdentityPool := workloadIdentityProviderRegex.FindStringSubmatch(opts.WorkloadIdentityProvider)[2]
return &Config{opts}, numericProjectID, workloadIdentityPool, nil
}

func (c *Config) Get(ctx context.Context, credFile string, googleServiceAccountEmail *string) (*google.Credentials, error) {
Expand Down
Loading

0 comments on commit 9c42a52

Please sign in to comment.