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

centralize clients and config #143

Merged
merged 6 commits into from
Feb 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 70 additions & 0 deletions cmd/container.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package cmd

import (
"context"
"fmt"

"github.com/pkg/errors"

"github.com/zapier/kubechecks/pkg/app_watcher"
"github.com/zapier/kubechecks/pkg/appdir"
"github.com/zapier/kubechecks/pkg/argo_client"
"github.com/zapier/kubechecks/pkg/config"
"github.com/zapier/kubechecks/pkg/container"
"github.com/zapier/kubechecks/pkg/vcs/github_client"
"github.com/zapier/kubechecks/pkg/vcs/gitlab_client"
)

func newContainer(ctx context.Context, cfg config.ServerConfig) (container.Container, error) {
var err error

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

switch cfg.VcsType {
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, errors.Wrap(err, "failed to create vcs client")
}

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, errors.Wrap(err, "failed to build apps map")
}

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

go ctr.ApplicationWatcher.Run(ctx, 1)
}

return ctr, nil
}

func buildAppsMap(ctx context.Context, argoClient *argo_client.ArgoClient, result container.VcsToArgoMap) error {
apps, err := argoClient.GetApplications(ctx)
if err != nil {
return errors.Wrap(err, "failed to list applications")
}
for _, app := range apps.Items {
result.AddApp(&app)
}

return nil
}
118 changes: 75 additions & 43 deletions cmd/controller_cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package cmd

import (
"context"
"fmt"
"os"
"os/signal"
"syscall"
Expand All @@ -15,9 +14,11 @@ import (

"github.com/zapier/kubechecks/pkg"
"github.com/zapier/kubechecks/pkg/config"
"github.com/zapier/kubechecks/pkg/container"
"github.com/zapier/kubechecks/pkg/events"
"github.com/zapier/kubechecks/pkg/repo"
"github.com/zapier/kubechecks/pkg/server"
"github.com/zapier/kubechecks/pkg/vcs"
"github.com/zapier/kubechecks/telemetry"
)

// ControllerCmd represents the run command
Expand All @@ -26,58 +27,89 @@ var ControllerCmd = &cobra.Command{
Short: "Start the VCS Webhook handler.",
Long: ``,
Run: func(cmd *cobra.Command, args []string) {
clientType := viper.GetString("vcs-type")
client, err := createVCSClient(clientType)
ctx := context.Background()

log.Info().
Str("git-tag", pkg.GitTag).
Str("git-commit", pkg.GitCommit).
Msg("Starting KubeChecks")

log.Info().Msg("parsing configuration")
cfg, err := config.New()
if err != nil {
log.Fatal().Err(err).Msg("failed to create vcs client")
log.Fatal().Err(err).Msg("failed to parse configuration")
}

cfg := config.ServerConfig{
UrlPrefix: viper.GetString("webhook-url-prefix"),
WebhookSecret: viper.GetString("webhook-secret"),
VcsClient: client,
ctr, err := newContainer(ctx, cfg)
if err != nil {
log.Fatal().Err(err).Msg("failed to create container")
}

log.Info().Msg("Initializing git settings")
if err := repo.InitializeGitSettings(cfg.VcsClient.Username(), cfg.VcsClient.Email()); err != nil {
log.Fatal().Err(err).Msg("failed to initialize git settings")
t, err := initTelemetry(ctx, cfg)
if err != nil {
log.Panic().Err(err).Msg("Failed to initialize telemetry")
}
defer t.Shutdown()

fmt.Println("Starting KubeChecks:", pkg.GitTag, pkg.GitCommit)
ctx := context.Background()
server := server.NewServer(ctx, &cfg)

go server.Start(ctx)

// graceful termination handler.
// when we receive a SIGTERM from kubernetes, check for in-flight requests before exiting.
sigs := make(chan os.Signal, 1)
signal.Notify(sigs, syscall.SIGTERM)
done := make(chan bool, 1)

go func() {
sig := <-sigs
log.Debug().Str("signal", sig.String()).Msg("received signal")
done <- true
}()

<-done
log.Info().Msg("shutting down...")
for events.GetInFlight() > 0 {
log.Info().Int("count", events.GetInFlight()).Msg("waiting for in-flight requests to complete")
time.Sleep(time.Second * 3)
log.Info().Msg("initializing git settings")
if err = initializeGit(ctr); err != nil {
log.Fatal().Err(err).Msg("failed to initialize git settings")
}
log.Info().Msg("good bye.")
},
PreRunE: func(cmd *cobra.Command, args []string) error {
log.Info().Msg("Server Configuration: ")
log.Info().Msgf("Webhook URL Base: %s", viper.GetString("webhook-url-base"))
log.Info().Msgf("Webhook URL Prefix: %s", viper.GetString("webhook-url-prefix"))
log.Info().Msgf("VCS Type: %s", viper.GetString("vcs-type"))
return nil

log.Info().Msgf("starting web server")
startWebserver(ctx, ctr)

log.Info().Msgf("listening for requests")
waitForShutdown()

log.Info().Msg("shutting down gracefully")
waitForPendingRequest()
},
}

func initTelemetry(ctx context.Context, cfg config.ServerConfig) (*telemetry.OperatorTelemetry, error) {
return telemetry.Init(
ctx, "kubechecks", pkg.GitTag, pkg.GitCommit,
cfg.EnableOtel, cfg.OtelCollectorHost, cfg.OtelCollectorPort,
)
}

func startWebserver(ctx context.Context, ctr container.Container) {
srv := server.NewServer(ctr)
go srv.Start(ctx)
}

func initializeGit(ctr container.Container) error {
if err := vcs.InitializeGitSettings(ctr.Config, ctr.VcsClient); err != nil {
return err
}

return nil
}

func waitForPendingRequest() {
for events.GetInFlight() > 0 {
log.Info().Int("count", events.GetInFlight()).Msg("waiting for in-flight requests to complete")
time.Sleep(time.Second * 3)
}
}

func waitForShutdown() {
// graceful termination handler.
// when we receive a SIGTERM from kubernetes, check for in-flight requests before exiting.
sigs := make(chan os.Signal, 1)
signal.Notify(sigs, syscall.SIGTERM)
done := make(chan bool, 1)

go func() {
sig := <-sigs
log.Debug().Str("signal", sig.String()).Msg("received signal")
done <- true
}()

<-done
}

func panicIfError(err error) {
if err != nil {
panic(err)
Expand Down
28 changes: 8 additions & 20 deletions cmd/process.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
package cmd

import (
"context"

"github.com/rs/zerolog/log"
"github.com/spf13/cobra"
"github.com/spf13/viper"

"github.com/zapier/kubechecks/pkg/config"
"github.com/zapier/kubechecks/pkg/server"
Expand All @@ -16,34 +13,25 @@ var processCmd = &cobra.Command{
Short: "Process a pull request",
Long: "",
Run: func(cmd *cobra.Command, args []string) {
ctx := context.TODO()

log.Info().Msg("building apps map from argocd")
result, err := config.BuildAppsMap(ctx)
if err != nil {
log.Fatal().Err(err).Msg("failed to build apps map")
}

clientType := viper.GetString("vcs-type")
client, err := createVCSClient(clientType)
if err != nil {
log.Fatal().Err(err).Msg("failed to create vcs client")
}
ctx := cmd.Context()

cfg := config.ServerConfig{
UrlPrefix: "--unused--",
WebhookSecret: "--unused--",
VcsToArgoMap: result,
VcsClient: client,
}

repo, err := client.LoadHook(ctx, args[0])
ctr, err := newContainer(ctx, cfg)
if err != nil {
log.Fatal().Err(err).Msg("failed to create container")
}

repo, err := ctr.VcsClient.LoadHook(ctx, args[0])
if err != nil {
log.Fatal().Err(err).Msg("failed to load hook")
return
}

server.ProcessCheckEvent(ctx, repo, &cfg)
server.ProcessCheckEvent(ctx, repo, cfg, ctr)
},
}

Expand Down
34 changes: 7 additions & 27 deletions cmd/root.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package cmd

import (
"context"
"os"
"strings"

Expand All @@ -10,9 +9,6 @@ import (
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/viper"

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

// RootCmd represents the base command when called without any subcommands
Expand All @@ -26,14 +22,6 @@ var RootCmd = &cobra.Command{
// Execute adds all child commands to the root command and sets flags appropriately.
// This is called by main.main(). It only needs to happen once to the rootCmd.
func Execute() {
ctx := context.Background()
t, err := initTelemetry(ctx)
if err != nil {
log.Panic().Err(err).Msg("Failed to initialize telemetry")
}

defer t.Shutdown()

cobra.CheckErr(RootCmd.Execute())
}

Expand All @@ -42,7 +30,6 @@ const envPrefix = "kubechecks"
var envKeyReplacer = strings.NewReplacer("-", "_")

func init() {

// allows environment variables to use _ instead of -
viper.SetEnvKeyReplacer(envKeyReplacer) // sync-provider becomes SYNC_PROVIDER
viper.SetEnvPrefix(envPrefix) // port becomes KUBECHECKS_PORT
Expand All @@ -51,13 +38,13 @@ func init() {
flags := RootCmd.PersistentFlags()
stringFlag(flags, "log-level", "Set the log output level.",
newStringOpts().
withChoices(
zerolog.LevelErrorValue,
zerolog.LevelWarnValue,
zerolog.LevelInfoValue,
zerolog.LevelDebugValue,
zerolog.LevelTraceValue,
).
withChoices(
zerolog.LevelErrorValue,
zerolog.LevelWarnValue,
zerolog.LevelInfoValue,
zerolog.LevelDebugValue,
zerolog.LevelTraceValue,
).
withDefault("info").
withShortHand("l"),
)
Expand Down Expand Up @@ -94,13 +81,6 @@ func init() {
setupLogOutput()
}

func initTelemetry(ctx context.Context) (*telemetry.OperatorTelemetry, error) {
enableOtel := viper.GetBool("otel-enabled")
otelHost := viper.GetString("otel-collector-host")
otelPort := viper.GetString("otel-collector-port")
return telemetry.Init(ctx, "kubechecks", pkg.GitTag, pkg.GitCommit, enableOtel, otelHost, otelPort)
}

func setupLogOutput() {
output := zerolog.ConsoleWriter{Out: os.Stdout}
log.Logger = log.Output(output)
Expand Down
20 changes: 0 additions & 20 deletions cmd/vcs.go

This file was deleted.

2 changes: 1 addition & 1 deletion cmd/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ var versionCmd = &cobra.Command{
Short: "List version information",
Long: ``,
Run: func(cmd *cobra.Command, args []string) {
fmt.Printf("Arrgh\nVersion:%s\nSHA%s", pkg.GitTag, pkg.GitCommit)
fmt.Printf("kubechecks\nVersion:%s\nSHA%s\n", pkg.GitTag, pkg.GitCommit)
},
}

Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ require (
github.com/rs/zerolog v1.31.0
github.com/sashabaranov/go-openai v1.19.3
github.com/shurcooL/githubv4 v0.0.0-20231126234147-1cffa1f02456
github.com/sirupsen/logrus v1.9.3
github.com/spf13/cobra v1.8.0
github.com/spf13/pflag v1.0.5
github.com/spf13/viper v1.18.2
Expand Down Expand Up @@ -210,6 +209,7 @@ require (
github.com/sergi/go-diff v1.3.1 // indirect
github.com/shteou/go-ignore v0.3.1 // indirect
github.com/shurcooL/graphql v0.0.0-20230722043721-ed46e5a46466 // indirect
github.com/sirupsen/logrus v1.9.3 // indirect
github.com/skeema/knownhosts v1.2.1 // indirect
github.com/sourcegraph/conc v0.3.0 // indirect
github.com/spdx/tools-golang v0.5.3 // indirect
Expand Down
Loading
Loading