Skip to content
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

Rollouts gitlab support #3853

Merged
merged 11 commits into from
Mar 7, 2023
Merged

Rollouts gitlab support #3853

merged 11 commits into from
Mar 7, 2023

Conversation

droot
Copy link
Contributor

@droot droot commented Mar 2, 2023

This PR adds support for GitLab as package source.

Note: Testing individual components is turning out to be tricky. I have added tests for package discovery for gitlab and github that avoids running controller but the test setup is not ideal. I would like to use gitlab community edition for spinning up git repository with some test data, but I want to tackle that as separate task.

@droot droot marked this pull request as ready for review March 3, 2023 00:43
@droot droot requested a review from a team as a code owner March 3, 2023 00:43
@droot
Copy link
Contributor Author

droot commented Mar 3, 2023

@ChristopherFry @natasha41575 Would be great if you both of you can take a look at it.

@droot
Copy link
Contributor Author

droot commented Mar 3, 2023

@ChristopherFry @natasha41575 The tests are not ideal in current form But they are helpful if one is hacking on packagediscovery itself. I plan to address them by removing the dependency on gitlab with #3855.

Copy link
Contributor

@natasha41575 natasha41575 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor comments, but I have a bigger question.

Porch also has some code to discover packages from Github. Is there any overlap here? I'm wondering if we will eventually want to combine them into a git-package-discovery library.

type GitLabSource struct {
// SecretReference is the reference to a kubernetes secret
// that contains GitLab access token
SecretRef SecretReference `json:"secretRef,omitempty"`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The GithubSource has the SecretRef in the Selector. Is there a reason that you decided to do it differently for GitLabSource?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good question. The reasoning for separating SecretRef from Selector is that SecretRef is relevant to How to connect to the Package Source and Selector is relevant to How to select packages from a source. So mixing the two felt odd so decided to do it the right way for GitLab.

I intend to do the same for GitHub as well but in a separate PR.

SourceType: gitopsv1alpha1.GitHub,
GitHub: gitopsv1alpha1.GitHubSource{
Selector: gitopsv1alpha1.GitHubSelector{
Org: "droot",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we create these test packages under https://github.com/GoogleContainerTools/kpt/tree/main/package-examples (or some other place under GoogleContainerTools)? I think using your username is ok short-term, but long term it would probably be best to have it somewhere more official and not tied to someone's individual account

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So two things:

  1. Absolutely agree on keeping it in an official place. Mine is just a stop gap because of lack of a place.
  2. We can't use ...kpt/tree/main/package-examples for this because it complicates adding/deleting tests. One has to make changes in the test packages and commit it, then update the tests because tests will use whatever is available on main. kpt-samples could be another place but that is used for reference packages and using it for tests can make it really noisy.

But need to find a solution.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps a kpt-test repo?

if isSelectorField(selector.Directory) {
tree, _, err := gitHubClient.Git.GetTree(ctx, selector.Org, repoName, selector.Revision, true)
tree, _, err := gitHubClient.Git.GetTree(ctx, selector.Org, *repo.Name, repo.GetDefaultBranch(), true)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are we using repo.GetDefaultBranch here instead of branch?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch, fixed it.

// initialize a gitlab client
secretName := gitlabSource.SecretRef.Name
if secretName == "" {
return nil, fmt.Errorf("GitLab secret reference is missing from the config")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

super nit: in this error message, we are camel-casing GitLab, but in later error messages, we leave it all lowercase (gitlab). I think we should pick one for consistency

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch, using lower-case everywhere for now.

}

accessToken := string(repositorySecret.Data["token"])
// TODO(droot): BaseURL should also be configurable through the API
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

question: what other baseURLs are possible with gitlab?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Two use-cases that I have in my mind:

  1. Enterprises can self-host gitlab instances and in that case, capability to point to self-hosted gitlab will be needed
  2. Eventually, we will use gitlab docker instance for our e2e tests where being able to point to our instance will be needed.

Copy link
Contributor Author

@droot droot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the review @natasha41575

type GitLabSource struct {
// SecretReference is the reference to a kubernetes secret
// that contains GitLab access token
SecretRef SecretReference `json:"secretRef,omitempty"`
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good question. The reasoning for separating SecretRef from Selector is that SecretRef is relevant to How to connect to the Package Source and Selector is relevant to How to select packages from a source. So mixing the two felt odd so decided to do it the right way for GitLab.

I intend to do the same for GitHub as well but in a separate PR.

SourceType: gitopsv1alpha1.GitHub,
GitHub: gitopsv1alpha1.GitHubSource{
Selector: gitopsv1alpha1.GitHubSelector{
Org: "droot",
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So two things:

  1. Absolutely agree on keeping it in an official place. Mine is just a stop gap because of lack of a place.
  2. We can't use ...kpt/tree/main/package-examples for this because it complicates adding/deleting tests. One has to make changes in the test packages and commit it, then update the tests because tests will use whatever is available on main. kpt-samples could be another place but that is used for reference packages and using it for tests can make it really noisy.

But need to find a solution.

@droot
Copy link
Contributor Author

droot commented Mar 6, 2023

I'm wondering if we will eventually want to combine them into a git-package-discovery library.

Eventually, yes once we understand the usage better.

Copy link
Contributor

@ChristopherFry ChristopherFry left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good!

Not needed now, but In a future change I would definitely suggest splitting the GitHub and GitLab logic into separate files. The Package Discovery file is getting long.

@@ -174,3 +174,14 @@ $(CONTROLLER_GEN): $(LOCALBIN)
envtest: $(ENVTEST) ## Download envtest-setup locally if necessary.
$(ENVTEST): $(LOCALBIN)
test -s $(LOCALBIN)/setup-envtest || GOBIN=$(LOCALBIN) go install sigs.k8s.io/controller-runtime/tools/setup-envtest@latest

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: there's an extra newline here

@@ -453,8 +458,9 @@ func (r *RolloutReconciler) computeTargets(ctx context.Context,

func pkgNeedsUpdate(rollout *gitopsv1alpha1.Rollout, rrs gitopsv1alpha1.RemoteRootSync, pkg *packagediscovery.DiscoveredPackage) (*gitopsv1alpha1.RemoteRootSync, bool) {
// TODO: We need to check other things here besides git.Revision and metadata
metadata := getSpecMetadata(rollout)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would consider creating a pull request with just this change since this change is technically unrelated to the rest of the changes in the pull request.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for pointing it out. This will go away once @natasha41575 PR (that has the fix) is merged.

@droot
Copy link
Contributor Author

droot commented Mar 7, 2023

Not needed now, but In a future change I would definitely suggest splitting the GitHub and GitLab logic into separate files. The Package Discovery file is getting long.

@ChristopherFry Went ahead and did it in the last commit. At some point, I want to introduce an interface for package selection and this sets up well for that change in the future :)

@@ -463,6 +469,25 @@ func pkgNeedsUpdate(rollout *gitopsv1alpha1.Rollout, rrs gitopsv1alpha1.RemoteRo
return nil, false
}

func getSpecMetadata(rollout *gitopsv1alpha1.Rollout) *gitopsv1alpha1.Metadata {
Copy link
Contributor

@natasha41575 natasha41575 Mar 7, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this PR needs a rebase on main. My PR with this fix merged, but I believe I moved this function to the end of the file in my PR and we should try to avoid something funny happening when this PR is merged.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I removed my changes to facilitate clean merge. I had to make small change to make the code aware of syncTemplate being nil.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants