gitproviders

package
v1.17.1-rc.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 9, 2024 License: Apache-2.0 Imports: 29 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// MainBranch is static as behavior when switching branches is never under
	// test.
	MainBranch = "main"

	// GitKeepFileName is a conventional name for an empty file you add to
	// directories in git when you want to make sure the directory is retained even
	// when all the other files are deleted.
	// Without this file, the directory may remain locally, but won't exist in git.
	GitKeepFileName = ".gitkeep"

	// DefaultSyncDir is created in new repositories by default.
	DefaultSyncDir = "acme"
)
View Source
const (
	// GitUser is the user for all Git providers.
	GitUser = "config-sync-ci-bot"
)
View Source
const (

	// PrivateSSHKey is secret name of the private SSH key stored in the Cloud Secret Manager.
	PrivateSSHKey = "config-sync-ci-ssh-private-key"
)

Variables

This section is empty.

Functions

func FetchCloudSecret

func FetchCloudSecret(name string) (string, error)

FetchCloudSecret fetches secret from Google Cloud Secret Manager.

func GetProjectID

func GetProjectID(g *GitlabClient, name string) (string, error)

GetProjectID is a helper function for DeleteRepositories since Gitlab API only deletes by id

Types

type BitbucketClient

type BitbucketClient struct {
	// contains filtered or unexported fields
}

BitbucketClient is the client that calls the Bitbucket REST APIs.

func (*BitbucketClient) CreateRepository

func (b *BitbucketClient) CreateRepository(localName string) (string, error)

CreateRepository calls the POST API to create a remote repository on Bitbucket. The remote repo name is unique with a prefix of the local name.

func (*BitbucketClient) DeleteObsoleteRepos

func (b *BitbucketClient) DeleteObsoleteRepos() error

DeleteObsoleteRepos deletes all repos that were created more than 24 hours ago.

func (*BitbucketClient) DeleteRepositories

func (b *BitbucketClient) DeleteRepositories(names ...string) error

DeleteRepositories calls the DELETE API to delete all remote repositories on Bitbucket. It deletes multiple repos in a single function in order to reuse the access_token.

func (*BitbucketClient) RemoteURL

func (b *BitbucketClient) RemoteURL(name string) (string, error)

RemoteURL returns the Git URL for the Bitbucket repository. name refers to the repo name in the format of <NAMESPACE>/<NAME> of RootSync|RepoSync.

func (*BitbucketClient) SyncURL

func (b *BitbucketClient) SyncURL(name string) string

SyncURL returns a URL for Config Sync to sync from. name refers to the repo name in the format of <NAMESPACE>/<NAME> of RootSync|RepoSync. The Bitbucket Rest API doesn't allow slash in the repository name, so slash has to be replaced with dash in the name.

func (*BitbucketClient) Type

func (b *BitbucketClient) Type() string

Type returns the provider type.

type GitProvider

type GitProvider interface {
	Type() string

	// RemoteURL returns remote URL of the repository.
	// It is used to set the url for the remote origin using `git remote add origin <REMOTE_URL>.
	// For the testing git-server, RemoteURL uses localhost and forwarded port, while SyncURL uses the DNS.
	// For other git providers, RemoteURL should be the same as SyncURL.
	// name refers to the repo name in the format of <NAMESPACE>/<NAME> of RootSync|RepoSync.
	RemoteURL(name string) (string, error)

	// SyncURL returns the git repository URL for Config Sync to sync from.
	// name refers to the repo name in the format of <NAMESPACE>/<NAME> of RootSync|RepoSync.
	SyncURL(name string) string
	CreateRepository(name string) (string, error)
	DeleteRepositories(names ...string) error
	DeleteObsoleteRepos() error
}

GitProvider is an interface for the remote Git providers.

func NewGitProvider

func NewGitProvider(t testing.NTB, provider string, logger *testlogger.TestLogger) GitProvider

NewGitProvider creates a GitProvider for the specific provider type.

type GitlabClient

type GitlabClient struct {
	// contains filtered or unexported fields
}

GitlabClient is the client that will call Gitlab REST APIs.

func (*GitlabClient) CreateRepository

func (g *GitlabClient) CreateRepository(name string) (string, error)

CreateRepository calls the POST API to create a project/repository on Gitlab. The remote repo name is unique with a prefix of the local name.

func (*GitlabClient) DeleteObsoleteRepos

func (g *GitlabClient) DeleteObsoleteRepos() error

DeleteObsoleteRepos deletes all projects that has been inactive more than 24 hours

func (*GitlabClient) DeleteRepoByID

func (g *GitlabClient) DeleteRepoByID(ids ...string) error

DeleteRepoByID calls the DELETE API to delete the list of project id in Gitlab.

func (*GitlabClient) DeleteRepositories

func (g *GitlabClient) DeleteRepositories(names ...string) error

DeleteRepositories calls the DELETE API to delete the list of project name in Gitlab.

func (*GitlabClient) GetObsoleteRepos

func (g *GitlabClient) GetObsoleteRepos() ([]string, error)

GetObsoleteRepos is a helper function to get all project ids that has been inactive more than 24 hours

func (*GitlabClient) RemoteURL

func (g *GitlabClient) RemoteURL(name string) (string, error)

RemoteURL returns the Git URL for the Gitlab project repository.

func (*GitlabClient) SyncURL

func (g *GitlabClient) SyncURL(name string) string

SyncURL returns a URL for Config Sync to sync from.

func (*GitlabClient) Type

func (g *GitlabClient) Type() string

Type returns the git provider type

type LocalProvider

type LocalProvider struct {
	// PortForwarder is a port forwarder to the in-cluster git server.
	// This is used to communicate from the tests to the in-cluster git server.
	PortForwarder *portforwarder.PortForwarder
}

LocalProvider refers to the test git-server running on the same test cluster.

func (*LocalProvider) CreateRepository

func (l *LocalProvider) CreateRepository(name string) (string, error)

CreateRepository returns the local name as the remote repo name. It is a no-op for the test git-server because all repos are initialized at once in git-server.go.

func (*LocalProvider) DeleteObsoleteRepos

func (l *LocalProvider) DeleteObsoleteRepos() error

DeleteObsoleteRepos is a no-op for the test git-server because the git-server will be deleted after the test.

func (*LocalProvider) DeleteRepositories

func (l *LocalProvider) DeleteRepositories(...string) error

DeleteRepositories is a no-op for the test git-server because the git-server will be deleted after the test.

func (*LocalProvider) RemoteURL

func (l *LocalProvider) RemoteURL(name string) (string, error)

RemoteURL returns the Git URL for connecting to the test git-server. name refers to the repo name in the format of <NAMESPACE>/<NAME> of RootSync|RepoSync.

func (*LocalProvider) RemoteURLWithPort added in v1.15.1

func (l *LocalProvider) RemoteURLWithPort(localPort int, name string) (string, error)

RemoteURLWithPort returns the Git URL for connecting to the test git-server. localPort refers to the local port the PortForwarder is listening on. name refers to the repo name in the format of <NAMESPACE>/<NAME> of RootSync|RepoSync.

func (*LocalProvider) SyncURL

func (l *LocalProvider) SyncURL(name string) string

SyncURL returns a URL for Config Sync to sync from. name refers to the repo name in the format of <NAMESPACE>/<NAME> of RootSync|RepoSync.

func (*LocalProvider) Type

func (l *LocalProvider) Type() string

Type returns the provider type.

type RepoType added in v1.15.1

type RepoType string

RepoType represents the type of the source repository.

const (
	// RootRepo indicates the resources in the repository are cluster-scoped.
	RootRepo RepoType = "root"
	// NamespaceRepo indicates the resources in the repository are namespace-scoped.
	NamespaceRepo RepoType = "namespace"
)

type Repository added in v1.15.1

type Repository struct {
	// Name of the repository.
	// <NAMESPACE>/<NAME> of the RootSync|RepoSync.
	Name string
	// Root is the location on the machine running the test at which the local
	// repository is stored.
	Root string
	// Format is the source format for parsing the repository (hierarchy or
	// unstructured).
	Format filesystem.SourceFormat
	// PrivateKeyPath is the local path to the private key on disk to use to
	// authenticate with the git server.
	PrivateKeyPath string
	// Type refers to the type of the repository, i.e. if it is a root repo or a namespace repo.
	Type RepoType
	// SafetyNSPath is the path to the safety namespace yaml file.
	SafetyNSPath string
	// SafetyNSName is the name of the safety namespace.
	SafetyNSName string
	// SafetyClusterRolePath is the path to the safety namespace yaml file.
	SafetyClusterRolePath string
	// SafetyClusterRoleName is the name of the safety namespace.
	SafetyClusterRoleName string
	// RemoteRepoName is the name of the remote repository.
	// It is the same as Name for the testing git-server.
	// For other git providers, it appends a UUID to Name for uniqueness.
	RemoteRepoName string
	// GitProvider is the provider that hosts the Git repositories.
	GitProvider GitProvider
	// Scheme used for encoding and decoding objects.
	Scheme *runtime.Scheme
	// Logger for methods to use.
	Logger *testlogger.TestLogger
	// contains filtered or unexported fields
}

Repository is a local git repository with a connection to a repository on the git-server for the test.

We shell out for git commands as the git libraries are difficult to configure ssh for, and git-server requires ssh authentication.

func NewRepository added in v1.15.1

func NewRepository(
	repoType RepoType,
	syncNN types.NamespacedName,
	sourceFormat filesystem.SourceFormat,
	scheme *runtime.Scheme,
	logger *testlogger.TestLogger,
	provider GitProvider,
	tmpDir string,
	privateKeyPath string,
	defaultWaitTimeout time.Duration,
) *Repository

NewRepository creates a remote repo on the git provider. Locally, it writes the repository to `tmpdir`/repos/`name`. Name is the <NAMESPACE>/<NAME> of the RootSync|RepoSync.

func (*Repository) Add added in v1.15.1

func (g *Repository) Add(path string, obj client.Object) error

Add writes a YAML or JSON representation of obj to `path` in the git repository, and `git add`s the file. Does not commit/push.

Overwrites the file if it already exists. Automatically writes YAML or JSON based on the path's extension.

Don't put multiple manifests in the same file unless parsing multi-manifest files is the behavior under test. In that case, use AddFile.

func (*Repository) AddEmptyDir added in v1.15.1

func (g *Repository) AddEmptyDir(path string) error

AddEmptyDir creates an empty dir containing an empty .gitkeep file, so the empty dir will be retained in git.

Use this when creating empty sync directories, otherwise Config Sync will error that the directory doesn't exist.

func (*Repository) AddFile added in v1.15.1

func (g *Repository) AddFile(path string, bytes []byte) error

AddFile writes `bytes` to `file` in the git repository. This function should only be directly used for testing the literal YAML/JSON parsing logic.

Path is relative to the Git repository root. Overwrites `file` if it already exists. Does not commit/push.

func (*Repository) AddRepoObject added in v1.15.1

func (g *Repository) AddRepoObject(syncDir string) error

AddRepoObject adds a system.repo.yaml under the specified directory path. Use this for structured repositories.

func (*Repository) AddSafetyClusterRole added in v1.15.1

func (g *Repository) AddSafetyClusterRole() error

AddSafetyClusterRole adds a ClusterRole to prevent the mono-repo safety check (KNV2006) from preventing deletion of other objects.

func (*Repository) AddSafetyNamespace added in v1.15.1

func (g *Repository) AddSafetyNamespace() error

AddSafetyNamespace adds a Namespace to prevent the mono-repo safety check (KNV2006) from preventing deletion of other objects.

func (*Repository) BulkGit added in v1.15.1

func (g *Repository) BulkGit(cmds ...[]string) error

BulkGit executes a list of git commands sequentially. If any command errors, execution is halted and the error is logged and returned.

func (*Repository) CheckoutBranch added in v1.15.1

func (g *Repository) CheckoutBranch(branch string) error

CheckoutBranch checkouts a branch.

func (*Repository) CommitAndPush added in v1.15.1

func (g *Repository) CommitAndPush(msg string) error

CommitAndPush commits any changes to the git repository, and pushes them to the git server. We don't care about differentiating between committing and pushing for tests.

func (*Repository) CommitAndPushBranch added in v1.15.1

func (g *Repository) CommitAndPushBranch(msg, branch string) error

CommitAndPushBranch commits any changes to the git branch, and pushes them to the git server.

func (*Repository) Copy added in v1.15.1

func (g *Repository) Copy(sourceDir, destDir string) error

Copy copies the file or directory from source to destination. Overwrites the file if it already exists. Does not commit/push.

func (*Repository) Create added in v1.15.1

func (g *Repository) Create() error

Create the remote repository using the GitProvider, and create the local repository with an initial commit.

func (*Repository) CreateBranch added in v1.15.1

func (g *Repository) CreateBranch(branch string) error

CreateBranch creates and checkouts a new branch at once.

func (*Repository) Exists added in v1.15.1

func (g *Repository) Exists(path string) (bool, error)

Exists returns true if the file or directory exists at the specified path.

func (*Repository) Get added in v1.15.1

func (g *Repository) Get(path string) (client.Object, error)

Get reads, parses, and returns the specified file as an object.

File must have one of these suffixes: .yaml, .yml, .json This is meant to read files written with Add. So it only reads one object per file. If you need to parse multiple objects from one file, use GetFile.

func (*Repository) GetAll added in v1.15.1

func (g *Repository) GetAll(dirPath string, recursive bool) ([]client.Object, error)

GetAll reads, parses, and returns all the files in a specified directory as objects.

func (*Repository) GetFile added in v1.15.1

func (g *Repository) GetFile(path string) ([]byte, error)

GetFile reads and returns the specified file.

func (*Repository) Git added in v1.15.1

func (g *Repository) Git(command ...string) ([]byte, error)

Git executes the command from the repo root. The command is always logged to the debug log. If the command errors, the command and output is logged.

func (*Repository) Hash added in v1.15.1

func (g *Repository) Hash() (string, error)

Hash returns the current hash of the git repository.

Immediately ends the test on error.

func (*Repository) Init added in v1.15.1

func (g *Repository) Init() error

Init initializes this git repository and configures it to talk to the cluster under test.

func (*Repository) InitialCommit added in v1.15.1

func (g *Repository) InitialCommit(sourceFormat filesystem.SourceFormat) error

InitialCommit initializes the Nomos repo with the Repo object.

func (*Repository) MustGet added in v1.15.1

func (g *Repository) MustGet(t testing.NTB, path string) client.Object

MustGet calls Get and fails the test on error, logging the result.

func (*Repository) MustGetAll added in v1.15.1

func (g *Repository) MustGetAll(t testing.NTB, dirPath string, recursive bool) []client.Object

MustGetAll calls GetAll and fails the test on error, logging the result.

func (*Repository) MustGetFile added in v1.15.1

func (g *Repository) MustGetFile(t testing.NTB, path string) []byte

MustGetFile calls GetFile and fails the test on error, logging the result.

func (*Repository) MustHash added in v1.15.1

func (g *Repository) MustHash(t testing.NTB) string

MustHash calls Hash and fails the test on error, logging the result.

func (*Repository) Push added in v1.15.1

func (g *Repository) Push(args ...string) error

Push pushes the provided refspec to the git server. Performs a retry using RemoteURL, which may change if the port forwarding restarts.

func (*Repository) PushAllBranches added in v1.15.1

func (g *Repository) PushAllBranches(remoteURL string) error

PushAllBranches push all local branches to the git server. This is currently intended to only be called from the OnReadyCallback for the in-cluster git server. Accepts a remoteURL to avoid calls to LocalPort, as this would lead to a deadlock

func (*Repository) Remove added in v1.15.1

func (g *Repository) Remove(path string) error

Remove deletes `file` from the git repository. If `file` is a directory, deletes the directory. Returns error if the file does not exist. Does not commit/push.

func (*Repository) RemoveSafetyClusterRole added in v1.15.1

func (g *Repository) RemoveSafetyClusterRole() error

RemoveSafetyClusterRole removes the safety ClusterRole.

func (*Repository) RemoveSafetyNamespace added in v1.15.1

func (g *Repository) RemoveSafetyNamespace() error

RemoveSafetyNamespace removes the safety Namespace.

func (*Repository) RenameBranch added in v1.15.1

func (g *Repository) RenameBranch(current, new string) error

RenameBranch renames the current branch with a new one both locally and remotely. The old branch will be deleted from remote.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL