|
| 1 | +// Copyright 2025 Google LLC |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package gitproviders |
| 16 | + |
| 17 | +import ( |
| 18 | + "fmt" |
| 19 | + "strings" |
| 20 | + |
| 21 | + "kpt.dev/configsync/e2e" |
| 22 | + "kpt.dev/configsync/e2e/nomostest/gitproviders/util" |
| 23 | + "kpt.dev/configsync/e2e/nomostest/testing" |
| 24 | + "kpt.dev/configsync/e2e/nomostest/testshell" |
| 25 | +) |
| 26 | + |
| 27 | +// SSMServiceAccountEmail returns the email of the google service account with |
| 28 | +// permission to read from Secure Source Manager. |
| 29 | +func SSMServiceAccountEmail() string { |
| 30 | + return fmt.Sprintf("e2e-ssm-reader-sa@%s.iam.gserviceaccount.com", *e2e.GCPProject) |
| 31 | +} |
| 32 | + |
| 33 | +// SSMClient is the client that interacts with Google Secure Source Manager. |
| 34 | +type SSMClient struct { |
| 35 | + // project in which to store the source repo |
| 36 | + project string |
| 37 | + // project number which is needed for the sync URL of the source repo |
| 38 | + projectNumber string |
| 39 | + // SSM instance ID where the source repo will be stored |
| 40 | + instanceID string |
| 41 | + // region of the SSM instance in which to store the source repo |
| 42 | + region string |
| 43 | + // repoPrefix is used to avoid overlap |
| 44 | + repoPrefix string |
| 45 | + // shell used for invoking CLI tools |
| 46 | + shell *testshell.TestShell |
| 47 | +} |
| 48 | + |
| 49 | +var _ GitProvider = &SSMClient{} |
| 50 | + |
| 51 | +// newSSMClient instantiates a new SSM client. |
| 52 | +func newSSMClient(repoPrefix string, shell *testshell.TestShell, projectNumber string) *SSMClient { |
| 53 | + return &SSMClient{ |
| 54 | + project: *e2e.GCPProject, |
| 55 | + instanceID: testing.SSMInstanceID, |
| 56 | + region: *e2e.SSMInstanceRegion, |
| 57 | + repoPrefix: repoPrefix, |
| 58 | + shell: shell, |
| 59 | + projectNumber: projectNumber, |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +func (c *SSMClient) fullName(name string) string { |
| 64 | + return util.SanitizeRepoName(c.repoPrefix, name) |
| 65 | +} |
| 66 | + |
| 67 | +// Type returns the provider type. |
| 68 | +func (c *SSMClient) Type() string { |
| 69 | + return e2e.SSM |
| 70 | +} |
| 71 | + |
| 72 | +// RemoteURL returns the Git URL for the SSM repository. |
| 73 | +// name refers to the repo name in the format of <NAMESPACE>/<NAME> of RootSync|RepoSync. |
| 74 | +func (c *SSMClient) RemoteURL(name string) (string, error) { |
| 75 | + return c.SyncURL(name), nil |
| 76 | +} |
| 77 | + |
| 78 | +// SyncURL returns a URL for Config Sync to sync from. |
| 79 | +func (c *SSMClient) SyncURL(name string) string { |
| 80 | + return fmt.Sprintf("https://%s-%s-git.%s.sourcemanager.dev/%s/%s", c.instanceID, c.projectNumber, c.region, c.project, name) |
| 81 | +} |
| 82 | + |
| 83 | +func (c *SSMClient) login() error { |
| 84 | + _, err := c.shell.ExecWithDebug("gcloud", "init") |
| 85 | + if err != nil { |
| 86 | + return fmt.Errorf("authorizing gcloud: %w", err) |
| 87 | + } |
| 88 | + return nil |
| 89 | +} |
| 90 | + |
| 91 | +// CreateRepository calls the gcloud SDK to create a remote repository on SSM. |
| 92 | +// It returns the full name with a prefix. |
| 93 | +func (c *SSMClient) CreateRepository(name string) (string, error) { |
| 94 | + fullName := c.fullName(name) |
| 95 | + if err := c.login(); err != nil { |
| 96 | + return fullName, err |
| 97 | + } |
| 98 | + |
| 99 | + out, err := c.shell.ExecWithDebug("gcloud", "beta", "source-manager", "repos", |
| 100 | + "describe", fullName, "--region", c.region, |
| 101 | + "--project", c.project) |
| 102 | + if err == nil { |
| 103 | + return fullName, nil // repo already exists, skip creation |
| 104 | + } |
| 105 | + if !strings.Contains(string(out), "NOT_FOUND") { |
| 106 | + return fullName, fmt.Errorf("describing source repository: %w", err) |
| 107 | + } |
| 108 | + |
| 109 | + _, err = c.shell.ExecWithDebug("gcloud", "beta", "source-manager", "repos", |
| 110 | + "create", fullName, "--region", c.region, |
| 111 | + "--instance", c.instanceID, |
| 112 | + "--project", c.project) |
| 113 | + if err != nil { |
| 114 | + return fullName, fmt.Errorf("creating source repository: %w", err) |
| 115 | + } |
| 116 | + |
| 117 | + return fullName, nil |
| 118 | +} |
| 119 | + |
| 120 | +// DeleteRepositories calls the gcloud SDK to delete the provided repositories from SSM. |
| 121 | +func (c *SSMClient) DeleteRepositories(names ...string) error { |
| 122 | + for _, name := range names { |
| 123 | + _, err := c.shell.ExecWithDebug("gcloud", "beta", "source-manager", "repos", |
| 124 | + "delete", name, |
| 125 | + "--region", c.region, |
| 126 | + "--project", c.project) |
| 127 | + if err != nil { |
| 128 | + return fmt.Errorf("deleting source repository: %w", err) |
| 129 | + } |
| 130 | + } |
| 131 | + return nil |
| 132 | +} |
| 133 | + |
| 134 | +// DeleteObsoleteRepos is a no-op because SSM repo names are determined by the |
| 135 | +// test cluster name and RSync namespace and name, so it can be reused if it |
| 136 | +// failed to be deleted after the test. |
| 137 | +func (c *SSMClient) DeleteObsoleteRepos() error { |
| 138 | + return nil |
| 139 | +} |
0 commit comments