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

feat: git interactor for remote instance registration #125

Closed
wants to merge 9 commits into from

Conversation

lvlcn-t
Copy link
Collaborator

@lvlcn-t lvlcn-t commented Mar 14, 2024

Motivation

We currently only support gitlab as remote registration backend through its API. This should change to support all git based repositories. For more information on that please refer to the issue.

Closes #66

Changes

I've introduced a new remote interactor type, that is able to perform our defined interactor interfaces' actions on any git repository. Currently the git interactor is only able to communicate via HTTP, but that could be changed in a future PR if a user needs ssh communication.

As you've already seen with my last two PRs regarding the issue, I've tried to do it as simple as possible so I hope the code is easy to understand. 😅 I'm also sorry that it's still ~1,5k lines changed but I think that was inevitable.

For additional information look at the commits.

Tests done

I've provided unit tests for the interactor. To do that I've done several things like hiding the remote interactions (Clone, Pull, Push) behind an interface so we can mock it and setup a repomock package that sets up a new local repository where the local git interactions are performed. I've consciously decided not to hide the whole go-git library behind an interface because it'd not bring us that much more benefit, but if you think we should still be able to test error cases of local git actions then we'd need to create an interface for it.

  • Unit tests succeeded
  • E2E tests succeeded - we have none for the target manager

Manual e2e tests

Config

targetManager:
  type: git
  checkInterval: 1m
  registrationInterval: 1m
  updateInterval: 1m
  unhealthyThreshold: 3m
  git:
    repoUrl: https://github.com/lvlcn-t/sparrow-registration.git

Logs

❯ go run ./main.go run --config .tmp/start-config.yaml
Using config file: .tmp/start-config.yaml
time=20:29:11 level=INFO source=/home/tom/dev/sparrow/cmd/run.go:89 msg="Running sparrow"
time=20:29:11 level=INFO source=/home/tom/dev/sparrow/pkg/sparrow/targets/manager.go:82 msg="Starting target manager reconciliation"
time=20:29:11 level=INFO source=/home/tom/dev/sparrow/pkg/api/api.go:76 msg="Serving Api" addr=:8080
time=20:29:11 level=INFO source=/home/tom/dev/sparrow/pkg/config/file.go:71 msg="File Loader disabled"
time=20:30:11 level=INFO source=/home/tom/dev/sparrow/pkg/sparrow/targets/remote/git/git.go:82 msg="Successfully fetched all target files" files=0
time=20:30:11 level=DEBUG source=/home/tom/dev/sparrow/pkg/sparrow/targets/manager.go:251 msg="Updated global targets" targets=0
time=20:30:11 level=DEBUG source=/home/tom/dev/sparrow/pkg/sparrow/targets/manager.go:196 msg="Not registered as global target, no update done."
time=20:30:11 level=DEBUG source=/home/tom/dev/sparrow/pkg/sparrow/targets/manager.go:177 msg="Registering as global target"
time=20:30:11 level=DEBUG source=/home/tom/dev/sparrow/pkg/sparrow/targets/remote/git/git.go:97 msg="Creating new file in repository" file=dev-tom.local.sparrow.com.json
time=20:30:12 level=INFO source=/home/tom/dev/sparrow/pkg/sparrow/targets/remote/git/git.go:121 msg="File committed and pushed" file=dev-tom.local.sparrow.com.json
time=20:30:12 level=DEBUG source=/home/tom/dev/sparrow/pkg/sparrow/targets/manager.go:183 msg="Successfully registered"
time=20:31:12 level=INFO source=/home/tom/dev/sparrow/pkg/sparrow/targets/remote/git/git.go:82 msg="Successfully fetched all target files" files=1
time=20:31:12 level=DEBUG source=/home/tom/dev/sparrow/pkg/sparrow/targets/manager.go:251 msg="Updated global targets" targets=1
time=20:31:12 level=DEBUG source=/home/tom/dev/sparrow/pkg/sparrow/targets/manager.go:209 msg="Updating instance registration"
time=20:31:12 level=DEBUG source=/home/tom/dev/sparrow/pkg/sparrow/targets/remote/git/git.go:89 msg="Updating file in repository" file="{Branch:main AuthorEmail:dev-tom.local.sparrow.com@sparrow AuthorName:dev-tom.local.sparrow.com CommitMessage:Updated registration Content:{Url:https://dev-tom.local.sparrow.com LastSeen:2024-03-14 19:31:12.212252862 +0000 UTC} Name:dev-tom.local.sparrow.com.json}"
time=20:31:13 level=INFO source=/home/tom/dev/sparrow/pkg/sparrow/targets/remote/git/git.go:121 msg="File committed and pushed" file=dev-tom.local.sparrow.com.json
time=20:31:13 level=DEBUG source=/home/tom/dev/sparrow/pkg/sparrow/targets/manager.go:215 msg="Successfully updated registration"
time=20:31:13 level=DEBUG source=/home/tom/dev/sparrow/pkg/sparrow/targets/manager.go:164 msg="Already registered as global target"
^Ctime=20:31:20 level=INFO source=/home/tom/dev/sparrow/cmd/run.go:98 msg="Signal received, shutting down"
time=20:31:20 level=ERROR source=/home/tom/dev/sparrow/pkg/sparrow/targets/manager.go:86 msg="Error while reconciling targets" err="context canceled"
time=20:31:20 level=INFO source=/home/tom/dev/sparrow/pkg/sparrow/run.go:168 msg="Shutting down sparrow gracefully"
time=20:31:20 level=DEBUG source=/home/tom/dev/sparrow/pkg/sparrow/targets/manager.go:127 msg="Shut down signal received"
time=20:31:21 level=INFO source=/home/tom/dev/sparrow/pkg/sparrow/targets/remote/git/git.go:149 msg="File deleted, committed and pushed successfully" file="{Branch:main AuthorEmail:dev-tom.local.sparrow.com@sparrow AuthorName:dev-tom.local.sparrow.com CommitMessage:Unregistering global target Content:{Url: LastSeen:0001-01-01 00:00:00 +0000 UTC} Name:dev-tom.local.sparrow.com.json}" file=dev-tom.local.sparrow.com.json
time=20:31:21 level=DEBUG source=/home/tom/dev/sparrow/pkg/sparrow/targets/manager.go:149 msg="Stopping gitlab reconciliation routine"
time=20:31:21 level=ERROR source=/home/tom/dev/sparrow/pkg/api/api.go:78 msg="Failed to serve api" error="http: Server closed"
time=20:31:21 level=DEBUG source=/home/tom/dev/sparrow/pkg/config/file.go:135 msg="Sending signal to shut down file loader"
time=20:31:21 level=INFO source=/home/tom/dev/sparrow/pkg/sparrow/controller.go:83 msg="Shutting down checks controller"
time=20:31:21 level=ERROR source=/home/tom/dev/sparrow/pkg/sparrow/run.go:123 msg="Non-recoverable error in sparrow component" error="context canceled"
time=20:31:21 level=ERROR source=/home/tom/dev/sparrow/pkg/sparrow/run.go:123 msg="Non-recoverable error in sparrow component" error="context canceled"
time=20:31:21 level=ERROR source=/home/tom/dev/sparrow/pkg/sparrow/run.go:123 msg="Non-recoverable error in sparrow component" error="failed serving API: context canceled"

Remote Git Repository

image

You can find the commit history of the repository here.

TODO

  • I've assigned this PR to myself
  • I've labeled this PR correctly

lvlcn-t added 7 commits March 3, 2024 13:51
* fix: validate target manager config
* refactor: split config validation in smaller methods
* chore: use errors instead of ok booleans
* refactor: generalize Gitlab interface to remote interactor interface
* refactor: mv gitlab config to interactor config
* chore: adjust imports
* feat: add interactor type
* feat: adjust target manager validation for interactor type
* chore: adjust naming
* feat: implement remote interactor interface with git client
* feat: add remote interactions
* feat: add git config to interactor config
* feat: validate git interactor type
* chore: hide remote interactions behind remote operations interface
* test: add git client unit tests
* test: add git interactions unit tests
cmd/run.go Show resolved Hide resolved
@lvlcn-t lvlcn-t self-assigned this Mar 14, 2024
@lvlcn-t lvlcn-t added feature Introduces a new feature area/target-manager Issues/PRs related to the TargetManager labels Mar 14, 2024
@lvlcn-t lvlcn-t changed the title feat: git remote backend interactor feat: git interactor for remote instance registration Mar 14, 2024
cmd/run.go Show resolved Hide resolved
pkg/sparrow/targets/remote/git/interactions.go Outdated Show resolved Hide resolved
func (g *client) pushChanges(ctx context.Context) error {
return g.repo.remote.PushContext(ctx, g.repo, &git.PushOptions{
Auth: g.auth,
Force: true,
Copy link
Collaborator

Choose a reason for hiding this comment

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

Is there a reason for the force push? Iirc there is one file per sparrow, so there should be no conflicts

@niklastreml
Copy link
Collaborator

I discussed whether we should merge this now with @lvlcn-t. We decided that it's best to leave this out-of-scope for now, as it is not required currently and to make future changes easier. We will keep the code around in case we need to implement the git integration later on

@lvlcn-t
Copy link
Collaborator Author

lvlcn-t commented Mar 15, 2024

I'll still address your reviews. After the PR would be ready to merge I'll close it, and move the branch into my fork so it won't be accidentally deleted. Additional to that I'll close the issue as not planned for now and label it with out-of-scope.

@lvlcn-t lvlcn-t mentioned this pull request Mar 16, 2024
4 tasks
@lvlcn-t lvlcn-t closed this Mar 18, 2024
@lvlcn-t lvlcn-t added the out-of-scope Something is not relevant for now but could be beneficial in the future. label Mar 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/target-manager Issues/PRs related to the TargetManager feature Introduces a new feature out-of-scope Something is not relevant for now but could be beneficial in the future.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Feature: Support other remote state backends
2 participants