Skip to content

Commit

Permalink
Merge branch 'refactor-config-clients' into linting
Browse files Browse the repository at this point in the history
# Conflicts:
#	pkg/app_watcher/app_watcher.go
  • Loading branch information
djeebus committed Feb 27, 2024
2 parents 6ddae8d + 0a73e21 commit 7ba157d
Show file tree
Hide file tree
Showing 13 changed files with 69 additions and 40 deletions.
19 changes: 11 additions & 8 deletions cmd/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,34 +18,37 @@ import (
func newContainer(ctx context.Context, cfg config.ServerConfig) (container.Container, error) {
var err error

ctr := container.Container{}
ctr.Config = cfg
var ctr = container.Container{
Config: cfg,
}

switch cfg.VcsType {
case "github":
ctr.VcsClient, err = gitlab_client.CreateGitlabClient(cfg)
case "gitlab":
ctr.VcsClient, err = gitlab_client.CreateGitlabClient(cfg)
case "github":
ctr.VcsClient, err = github_client.CreateGithubClient(cfg)
default:
err = fmt.Errorf("unknown vcs-type: %q", cfg.VcsType)
}
if err != nil {
return ctr, err
return ctr, errors.Wrap(err, "failed to create vcs client")
}

ctr.ArgoClient = argo_client.NewArgoClient(cfg)
if ctr.ArgoClient, err = argo_client.NewArgoClient(cfg); err != nil {
return ctr, errors.Wrap(err, "failed to create argo client")
}

vcsToArgoMap := appdir.NewVcsToArgoMap()
ctr.VcsToArgoMap = vcsToArgoMap

if cfg.MonitorAllApplications {
if err = buildAppsMap(ctx, ctr.ArgoClient, ctr.VcsToArgoMap); err != nil {
return ctr, err
return ctr, errors.Wrap(err, "failed to build apps map")
}

ctr.ApplicationWatcher, err = app_watcher.NewApplicationWatcher(vcsToArgoMap)
if err != nil {
return ctr, err
return ctr, errors.Wrap(err, "failed to create watch applications")
}

go ctr.ApplicationWatcher.Run(ctx, 1)
Expand Down
1 change: 1 addition & 0 deletions pkg/app_watcher/app_watcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ func NewApplicationWatcher(vcsToArgoMap appdir.VcsToArgoMap) (*ApplicationWatche

ctrl := ApplicationWatcher{
applicationClientset: appClient,
vcsToArgoMap: vcsToArgoMap,
}

appInformer, appLister := ctrl.newApplicationInformerAndLister(time.Second * 30)
Expand Down
18 changes: 13 additions & 5 deletions pkg/argo_client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,29 @@ type ArgoClient struct {
client apiclient.Client
}

func NewArgoClient(cfg config.ServerConfig) *ArgoClient {
clientOptions := &apiclient.ClientOptions{
func NewArgoClient(cfg config.ServerConfig) (*ArgoClient, error) {
opts := &apiclient.ClientOptions{
ServerAddr: cfg.ArgoCDServerAddr,
AuthToken: cfg.ArgoCDToken,
GRPCWebRootPath: cfg.ArgoCDPathPrefix,
Insecure: cfg.ArgoCDInsecure,
}
argo, err := apiclient.NewClient(clientOptions)

log.Info().
Str("server-addr", opts.ServerAddr).
Int("auth-token", len(opts.AuthToken)).
Str("grpc-web-root-path", opts.GRPCWebRootPath).
Bool("insecure", cfg.ArgoCDInsecure).
Msg("ArgoCD client configuration")

argo, err := apiclient.NewClient(opts)
if err != nil {
log.Fatal().Err(err).Msg("could not create ArgoCD API client")
return nil, err
}

return &ArgoClient{
client: argo,
}
}, nil
}

// GetApplicationClient has related argocd diff code https://github.com/argoproj/argo-cd/blob/d3ff9757c460ae1a6a11e1231251b5d27aadcdd1/cmd/argocd/commands/app.go#L899
Expand Down
9 changes: 7 additions & 2 deletions pkg/vcs/github_client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ import (
type Client struct {
shurcoolClient *githubv4.Client
googleClient *github.Client
cfg config.ServerConfig

tidyOutdatedCommentsMode string
username, email string
username, email string
}

// CreateGithubClient creates a new GitHub client using the auth token provided. We
Expand Down Expand Up @@ -68,6 +68,7 @@ func CreateGithubClient(cfg config.ServerConfig) (*Client, error) {
}

client := &Client{
cfg: cfg,
googleClient: googleClient,
shurcoolClient: shurcoolClient,
}
Expand Down Expand Up @@ -147,6 +148,8 @@ func (c *Client) buildRepoFromEvent(event *github.PullRequestEvent) *vcs.Repo {
Username: c.username,
Email: c.email,
Labels: labels,

Config: c.cfg,
}
}

Expand Down Expand Up @@ -313,6 +316,8 @@ func (c *Client) LoadHook(ctx context.Context, id string) (*vcs.Repo, error) {
Username: userName,
Email: userEmail,
Labels: labels,

Config: c.cfg,
}, nil
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/vcs/github_client/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ func (c *Client) TidyOutdatedComments(ctx context.Context, repo *vcs.Repo) error
nextPage = resp.NextPage
}

if strings.ToLower(c.tidyOutdatedCommentsMode) == "delete" {
if strings.ToLower(c.cfg.TidyOutdatedCommentsMode) == "delete" {
return c.pruneOldComments(ctx, repo, allComments)
}
return c.hideOutdatedMessages(ctx, repo, allComments)
Expand Down
23 changes: 13 additions & 10 deletions pkg/vcs/gitlab_client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ import (
const GitlabTokenHeader = "X-Gitlab-Token"

type Client struct {
*gitlab.Client
c *gitlab.Client
cfg config.ServerConfig

tidyOutdatedCommentsMode string
username, email string
username, email string
}

func CreateGitlabClient(cfg config.ServerConfig) (*Client, error) {
Expand Down Expand Up @@ -54,11 +54,10 @@ func CreateGitlabClient(cfg config.ServerConfig) (*Client, error) {
}

client := &Client{
Client: c,
c: c,
cfg: cfg,
username: user.Username,
email: user.Email,

tidyOutdatedCommentsMode: cfg.TidyOutdatedCommentsMode,
}
if client.username == "" {
client.username = vcs.DefaultVcsUsername
Expand Down Expand Up @@ -132,7 +131,7 @@ func (c *Client) GetHookByUrl(ctx context.Context, repoName, webhookUrl string)
if err != nil {
return nil, errors.Wrap(err, "failed to parse repo url")
}
webhooks, _, err := c.Client.Projects.ListProjectHooks(pid, nil)
webhooks, _, err := c.c.Projects.ListProjectHooks(pid, nil)
if err != nil {
return nil, errors.Wrap(err, "failed to list project webhooks")
}
Expand Down Expand Up @@ -160,7 +159,7 @@ func (c *Client) CreateHook(ctx context.Context, repoName, webhookUrl, webhookSe
return errors.Wrap(err, "failed to parse repo name")
}

_, _, err = c.Client.Projects.AddProjectHook(pid, &gitlab.AddProjectHookOptions{
_, _, err = c.c.Projects.AddProjectHook(pid, &gitlab.AddProjectHookOptions{
URL: pkg.Pointer(webhookUrl),
MergeRequestsEvents: pkg.Pointer(true),
Token: pkg.Pointer(webhookSecret),
Expand All @@ -187,12 +186,12 @@ func (c *Client) LoadHook(ctx context.Context, id string) (*vcs.Repo, error) {
return nil, errors.Wrap(err, "failed to parse merge request number")
}

project, _, err := c.Projects.GetProject(repoPath, nil)
project, _, err := c.c.Projects.GetProject(repoPath, nil)
if err != nil {
return nil, errors.Wrapf(err, "failed to get project '%s'", repoPath)
}

mergeRequest, _, err := c.MergeRequests.GetMergeRequest(repoPath, int(mrNumber), nil)
mergeRequest, _, err := c.c.MergeRequests.GetMergeRequest(repoPath, int(mrNumber), nil)
if err != nil {
return nil, errors.Wrapf(err, "failed to get merge request '%d' in project '%s'", mrNumber, repoPath)
}
Expand All @@ -212,6 +211,8 @@ func (c *Client) LoadHook(ctx context.Context, id string) (*vcs.Repo, error) {
Username: c.username,
Email: c.email,
Labels: mergeRequest.Labels,

Config: c.cfg,
}, nil
}

Expand All @@ -234,5 +235,7 @@ func (c *Client) buildRepoFromEvent(event *gitlab.MergeEvent) *vcs.Repo {
Username: c.username,
Email: c.email,
Labels: labels,

Config: c.cfg,
}
}
2 changes: 1 addition & 1 deletion pkg/vcs/gitlab_client/merge.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func (c *Client) GetMergeChanges(ctx context.Context, projectId int, mergeReqId
defer span.End()

var changes []*Changes
diffs, _, err := c.MergeRequests.ListMergeRequestDiffs(projectId, mergeReqId, &gitlab.ListMergeRequestDiffsOptions{})
diffs, _, err := c.c.MergeRequests.ListMergeRequestDiffs(projectId, mergeReqId, &gitlab.ListMergeRequestDiffsOptions{})
if err != nil {
telemetry.SetError(span, err, "Get MergeRequest Changes")
return changes, err
Expand Down
12 changes: 6 additions & 6 deletions pkg/vcs/gitlab_client/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func (c *Client) PostMessage(ctx context.Context, repo *vcs.Repo, mergeRequestID
message = message[:MaxCommentLength]
}

n, _, err := c.Notes.CreateMergeRequestNote(
n, _, err := c.c.Notes.CreateMergeRequestNote(
repo.FullName, mergeRequestID,
&gitlab.CreateMergeRequestNoteOptions{
Body: pkg.Pointer(message),
Expand Down Expand Up @@ -71,7 +71,7 @@ func (c *Client) hideOutdatedMessages(ctx context.Context, projectName string, m

log.Debug().Str("projectName", projectName).Int("mr", mergeRequestID).Msgf("Updating comment %d as outdated", note.ID)

_, _, err := c.Notes.UpdateMergeRequestNote(projectName, mergeRequestID, note.ID, &gitlab.UpdateMergeRequestNoteOptions{
_, _, err := c.c.Notes.UpdateMergeRequestNote(projectName, mergeRequestID, note.ID, &gitlab.UpdateMergeRequestNoteOptions{
Body: &newBody,
})

Expand All @@ -92,7 +92,7 @@ func (c *Client) UpdateMessage(ctx context.Context, m *msg.Message, message stri
message = message[:MaxCommentLength]
}

n, _, err := c.Notes.UpdateMergeRequestNote(m.Name, m.CheckID, m.NoteID, &gitlab.UpdateMergeRequestNoteOptions{
n, _, err := c.c.Notes.UpdateMergeRequestNote(m.Name, m.CheckID, m.NoteID, &gitlab.UpdateMergeRequestNoteOptions{
Body: pkg.Pointer(message),
})

Expand All @@ -116,7 +116,7 @@ func (c *Client) pruneOldComments(ctx context.Context, projectName string, mrID
for _, note := range notes {
if note.Author.Username == c.username {
log.Debug().Int("mr", mrID).Int("note", note.ID).Msg("deleting old comment")
_, err := c.Notes.DeleteMergeRequestNote(projectName, mrID, note.ID)
_, err := c.c.Notes.DeleteMergeRequestNote(projectName, mrID, note.ID)
if err != nil {
telemetry.SetError(span, err, "Prune Old Comments")
return fmt.Errorf("could not delete old comment: %w", err)
Expand All @@ -137,7 +137,7 @@ func (c *Client) TidyOutdatedComments(ctx context.Context, repo *vcs.Repo) error

for {
// list merge request notes
notes, resp, err := c.Notes.ListMergeRequestNotes(repo.FullName, repo.CheckID, &gitlab.ListMergeRequestNotesOptions{
notes, resp, err := c.c.Notes.ListMergeRequestNotes(repo.FullName, repo.CheckID, &gitlab.ListMergeRequestNotesOptions{
Sort: pkg.Pointer("asc"),
OrderBy: pkg.Pointer("created_at"),
ListOptions: gitlab.ListOptions{
Expand All @@ -156,7 +156,7 @@ func (c *Client) TidyOutdatedComments(ctx context.Context, repo *vcs.Repo) error
nextPage = resp.NextPage
}

if strings.ToLower(c.tidyOutdatedCommentsMode) == "delete" {
if strings.ToLower(c.cfg.TidyOutdatedCommentsMode) == "delete" {
return c.pruneOldComments(ctx, repo.FullName, repo.CheckID, allNotes)
}
return c.hideOutdatedMessages(ctx, repo.FullName, repo.CheckID, allNotes)
Expand Down
2 changes: 1 addition & 1 deletion pkg/vcs/gitlab_client/pipeline.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
)

func (c *Client) GetPipelinesForCommit(projectName string, commitSHA string) ([]*gitlab.PipelineInfo, error) {
pipelines, _, err := c.Pipelines.ListProjectPipelines(projectName, &gitlab.ListProjectPipelinesOptions{
pipelines, _, err := c.c.Pipelines.ListProjectPipelines(projectName, &gitlab.ListProjectPipelinesOptions{
SHA: pkg.Pointer(commitSHA),
})
if err != nil {
Expand Down
6 changes: 3 additions & 3 deletions pkg/vcs/gitlab_client/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ import (
"github.com/zapier/kubechecks/pkg/repo_config"
)

// GetProjectByIDorName gets a project by the given Project Name or ID
// GetProjectByID gets a project by the given Project Name or ID
func (c *Client) GetProjectByID(project int) (*gitlab.Project, error) {
var proj *gitlab.Project
err := backoff.Retry(func() error {
var err error
var resp *gitlab.Response
proj, resp, err = c.Projects.GetProject(project, nil)
proj, resp, err = c.c.Projects.GetProject(project, nil)
return checkReturnForBackoff(resp, err)
}, getBackOff())
return proj, err
Expand All @@ -30,7 +30,7 @@ func (c *Client) GetRepoConfigFile(ctx context.Context, projectId int, mergeReqI

// check MR branch
for _, file := range repo_config.RepoConfigFilenameVariations() {
b, _, err := c.RepositoryFiles.GetRawFile(
b, _, err := c.c.RepositoryFiles.GetRawFile(
projectId,
file,
&gitlab.GetRawFileOptions{Ref: pkg.Pointer("HEAD")},
Expand Down
2 changes: 1 addition & 1 deletion pkg/vcs/gitlab_client/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ func convertState(state pkg.CommitState) gitlab.BuildStateValue {
}

func (c *Client) setCommitStatus(projectWithNS string, commitSHA string, status *gitlab.SetCommitStatusOptions) (*gitlab.CommitStatus, error) {
commitStatus, _, err := c.Commits.SetCommitStatus(projectWithNS, commitSHA, status)
commitStatus, _, err := c.c.Commits.SetCommitStatus(projectWithNS, commitSHA, status)
return commitStatus, err
}

Expand Down
7 changes: 5 additions & 2 deletions pkg/vcs/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ type Repo struct {
Email string // Email of auth'd client
Labels []string // Labels associated with the MR/PR

cfg config.ServerConfig
Config config.ServerConfig
}

func (r *Repo) CloneRepoLocal(ctx context.Context, repoDir string) error {
Expand Down Expand Up @@ -208,13 +208,16 @@ func walk(s string, d fs.DirEntry, err error) error {
}

func (r *Repo) execCommand(name string, args ...string) *exec.Cmd {
cmd := execCommand(r.cfg, name, args...)
cmd := execCommand(r.Config, name, args...)
cmd.Dir = r.RepoDir
return cmd
}

func censorVcsToken(cfg config.ServerConfig, args []string) []string {
vcsToken := cfg.VcsToken
if len(vcsToken) == 0 {
return args
}

var argsToLog []string
for _, arg := range args {
Expand Down
6 changes: 6 additions & 0 deletions pkg/vcs/repo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,9 @@ func TestCensorVcsToken(t *testing.T) {
result := censorVcsToken(cfg, []string{"one", "two", "three"})
assert.Equal(t, []string{"one", "two", "t********e"}, result)
}

func TestCensorEmptyVcsToken(t *testing.T) {
cfg := config.ServerConfig{VcsToken: ""}
result := censorVcsToken(cfg, []string{"one", "two", "three"})
assert.Equal(t, []string{"one", "two", "three"}, result)
}

0 comments on commit 7ba157d

Please sign in to comment.