Skip to content

Commit

Permalink
refactor ServerConfig into 'config' package
Browse files Browse the repository at this point in the history
  • Loading branch information
Matt Morrison committed Jul 25, 2023
1 parent 405eb5b commit 38a529f
Show file tree
Hide file tree
Showing 10 changed files with 85 additions and 76 deletions.
3 changes: 2 additions & 1 deletion Earthfile
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,9 @@ dlv:
SAVE ARTIFACT /go/bin/dlv

docker-debug:
ARG GOARCH="amd64"
ARG CI_REGISTRY_IMAGE="kubechecks"
FROM +docker --GIT_TAG=debug --GIT_COMMIT=abcdef
FROM +docker --GIT_TAG=debug --GIT_COMMIT=abcdef --CI_REGISTRY_IMAGE=$CI_REGISTRY_IMAGE --GOARCH=$GOARCH

COPY (+dlv/dlv --GOARCH=$GOARCH --VARIANT=$TARGETVARIANT) /usr/local/bin/dlv

Expand Down
6 changes: 3 additions & 3 deletions cmd/controller_cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ var controllerCmd = &cobra.Command{
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("Starting KubeChecks:", pkg.GitTag, pkg.GitCommit)

server := server.NewServer(&pkg.ServerConfig{
UrlPrefix: viper.GetString("webhook-url-prefix"),
WebhookSecret: viper.GetString("webhook-secret"),
server := server.NewServer(&config.ServerConfig{
UrlPrefix: viper.GetString("webhook-url-prefix"),
WebhookSecret: viper.GetString("webhook-secret"),
})
go server.Start()

Expand Down
4 changes: 2 additions & 2 deletions pkg/affected_apps/argocd_matcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ package affected_apps

import (
"context"
"github.com/zapier/kubechecks/pkg/config"

"github.com/rs/zerolog/log"
"github.com/zapier/kubechecks/pkg"
"github.com/zapier/kubechecks/pkg/app_directory"
"github.com/zapier/kubechecks/pkg/repo"
)
Expand All @@ -13,7 +13,7 @@ type ArgocdMatcher struct {
appsDirectory *app_directory.AppDirectory
}

func NewArgocdMatcher(vcsToArgoMap pkg.VcsToArgoMap, repo *repo.Repo) *ArgocdMatcher {
func NewArgocdMatcher(vcsToArgoMap config.VcsToArgoMap, repo *repo.Repo) *ArgocdMatcher {
log.Debug().Msgf("looking for %s repos", repo.CloneURL)
repoApps := vcsToArgoMap.GetAppsInRepo(repo.CloneURL)
log.Debug().Msgf("found %d apps", repoApps.Count())
Expand Down
2 changes: 1 addition & 1 deletion pkg/config_test.go → pkg/config/config_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package pkg
package config

import (
"fmt"
Expand Down
55 changes: 55 additions & 0 deletions pkg/config/server_config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package config

import (
"fmt"
"net/url"
"strings"

giturls "github.com/whilp/git-urls"
)

type ServerConfig struct {
ArgoCdNamespace string
UrlPrefix string
WebhookSecret string
VcsToArgoMap VcsToArgoMap
}

func (cfg *ServerConfig) GetVcsRepos() []string {
var repos []string
for key := range cfg.VcsToArgoMap.vcsAppStubsByRepo {
repos = append(repos, key.CloneURL())
}
return repos
}

type repoURL struct {
Host, Path string
}

func (r repoURL) CloneURL() string {
return fmt.Sprintf("git@%s:%s", r.Host, r.Path)
}

func buildNormalizedRepoUrl(host, path string) repoURL {
path = strings.TrimPrefix(path, "/")
path = strings.TrimSuffix(path, ".git")
return repoURL{host, path}
}

func normalizeRepoUrl(s string) (repoURL, error) {
var parser func(string) (*url.URL, error)

if strings.HasPrefix(s, "http") {
parser = url.Parse
} else {
parser = giturls.Parse
}

r, err := parser(s)
if err != nil {
return repoURL{}, err
}

return buildNormalizedRepoUrl(r.Host, r.Path), nil
}
52 changes: 1 addition & 51 deletions pkg/config.go → pkg/config/vcs_argo_app_map.go
Original file line number Diff line number Diff line change
@@ -1,47 +1,11 @@
package pkg
package config

import (
"fmt"
"net/url"
"strings"

"github.com/argoproj/argo-cd/v2/pkg/apis/application/v1alpha1"
"github.com/rs/zerolog/log"
giturls "github.com/whilp/git-urls"
"github.com/zapier/kubechecks/pkg/app_directory"
)

type repoURL struct {
Host, Path string
}

func (r repoURL) CloneURL() string {
return fmt.Sprintf("git@%s:%s", r.Host, r.Path)
}

func buildNormalizedRepoUrl(host, path string) repoURL {
path = strings.TrimPrefix(path, "/")
path = strings.TrimSuffix(path, ".git")
return repoURL{host, path}
}

func normalizeRepoUrl(s string) (repoURL, error) {
var parser func(string) (*url.URL, error)

if strings.HasPrefix(s, "http") {
parser = url.Parse
} else {
parser = giturls.Parse
}

r, err := parser(s)
if err != nil {
return repoURL{}, err
}

return buildNormalizedRepoUrl(r.Host, r.Path), nil
}

type VcsToArgoMap struct {
vcsAppStubsByRepo map[repoURL]*app_directory.AppDirectory
}
Expand Down Expand Up @@ -80,17 +44,3 @@ func (v2a *VcsToArgoMap) AddApp(app v1alpha1.Application) {
appDirectory.AddApp(app)
v2a.vcsAppStubsByRepo[cleanRepoUrl] = appDirectory
}

type ServerConfig struct {
UrlPrefix string
WebhookSecret string
VcsToArgoMap VcsToArgoMap
}

func (cfg *ServerConfig) GetVcsRepos() []string {
var repos []string
for key := range cfg.VcsToArgoMap.vcsAppStubsByRepo {
repos = append(repos, key.CloneURL())
}
return repos
}
5 changes: 3 additions & 2 deletions pkg/events/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package events
import (
"context"
"fmt"
"github.com/zapier/kubechecks/pkg/config"
"os"
"strings"
"sync/atomic"
Expand Down Expand Up @@ -40,7 +41,7 @@ type CheckEvent struct {

affectedItems affected_apps.AffectedItems

cfg *pkg.ServerConfig
cfg *config.ServerConfig
}

const (
Expand All @@ -63,7 +64,7 @@ func init() {
hostname, _ = os.Hostname()
}

func NewCheckEvent(repo *repo.Repo, client vcs_clients.Client, cfg *pkg.ServerConfig) *CheckEvent {
func NewCheckEvent(repo *repo.Repo, client vcs_clients.Client, cfg *config.ServerConfig) *CheckEvent {
ce := &CheckEvent{
cfg: cfg,
client: client,
Expand Down
6 changes: 3 additions & 3 deletions pkg/server/hook_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ package server
import (
"context"
"fmt"
"github.com/zapier/kubechecks/pkg/config"
"net/http"
"strings"
"sync"

"github.com/labstack/echo/v4"
"github.com/rs/zerolog/log"
"github.com/spf13/viper"
"github.com/zapier/kubechecks/pkg"
"github.com/zapier/kubechecks/pkg/events"
"github.com/zapier/kubechecks/pkg/github_client"
"github.com/zapier/kubechecks/pkg/gitlab_client"
Expand All @@ -25,7 +25,7 @@ import (
type VCSHookHandler struct {
client vcs_clients.Client
tokenUser string
cfg *pkg.ServerConfig
cfg *config.ServerConfig
// labelFilter is a string specifying the required label name to filter merge events by; if empty, all merge events will pass the filter.
labelFilter string
}
Expand Down Expand Up @@ -60,7 +60,7 @@ func createVCSClient() (vcs_clients.Client, string) {

}

func NewVCSHookHandler(cfg *pkg.ServerConfig) *VCSHookHandler {
func NewVCSHookHandler(cfg *config.ServerConfig) *VCSHookHandler {
client, tokenUser := GetVCSClient()
labelFilter := viper.GetString("label-filter")

Expand Down
13 changes: 8 additions & 5 deletions pkg/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package server

import (
"context"
"github.com/zapier/kubechecks/pkg/config"
"net/url"
"strings"

Expand All @@ -12,7 +13,6 @@ import (
"github.com/pkg/errors"
"github.com/rs/zerolog/log"
"github.com/spf13/viper"
"github.com/zapier/kubechecks/pkg"
"github.com/zapier/kubechecks/pkg/argo_client"
"github.com/zapier/kubechecks/pkg/vcs_clients"
"github.com/ziflex/lecho/v3"
Expand All @@ -23,11 +23,14 @@ const KubeChecksHooksPathPrefix = "/hooks"
var singleton *Server

type Server struct {
cfg *pkg.ServerConfig
cfg *config.ServerConfig
}

func NewServer(cfg *pkg.ServerConfig) *Server {
singleton = &Server{cfg: cfg}
func NewServer(cfg *config.ServerConfig) *Server {
singleton = &Server{
cfg: cfg,
}

return singleton
}

Expand Down Expand Up @@ -128,7 +131,7 @@ func (s *Server) buildVcsToArgoMap() error {

ctx := context.TODO()

result := pkg.NewVcsToArgoMap()
result := config.NewVcsToArgoMap()

argoClient := argo_client.GetArgoClient()

Expand Down
15 changes: 7 additions & 8 deletions pkg/server/server_test.go
Original file line number Diff line number Diff line change
@@ -1,49 +1,48 @@
package server

import (
"github.com/zapier/kubechecks/pkg/config"
"testing"

"github.com/zapier/kubechecks/pkg"
)

func TestHooksPrefix(t *testing.T) {
tests := []struct {
name string
want string
cfg *pkg.ServerConfig
cfg *config.ServerConfig
}{
{
name: "no-prefix",
want: "/hooks",
cfg: &pkg.ServerConfig{
cfg: &config.ServerConfig{
UrlPrefix: "",
},
},
{
name: "prefix-no-slash",
want: "/test/hooks",
cfg: &pkg.ServerConfig{
cfg: &config.ServerConfig{
UrlPrefix: "test",
},
},
{
name: "prefix-trailing-slash",
want: "/test/hooks",
cfg: &pkg.ServerConfig{
cfg: &config.ServerConfig{
UrlPrefix: "test/",
},
},
{
name: "prefix-leading-slash",
want: "/test/hooks",
cfg: &pkg.ServerConfig{
cfg: &config.ServerConfig{
UrlPrefix: "/test",
},
},
{
name: "prefix-slash-sandwich",
want: "/test/hooks",
cfg: &pkg.ServerConfig{
cfg: &config.ServerConfig{
UrlPrefix: "/test/",
},
},
Expand Down

0 comments on commit 38a529f

Please sign in to comment.