Skip to content

Commit

Permalink
tame our configuration (#442)
Browse files Browse the repository at this point in the history
Signed-off-by: Kent <[email protected]>
  • Loading branch information
krancour committed Jun 23, 2023
1 parent d2c7e13 commit 9f90042
Show file tree
Hide file tree
Showing 19 changed files with 196 additions and 466 deletions.
41 changes: 29 additions & 12 deletions cmd/cli/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,16 @@ import (

"github.com/pkg/errors"
"github.com/spf13/cobra"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/client/config"

kargoAPI "github.com/akuity/kargo/api/v1alpha1"
"github.com/akuity/kargo/internal/api"
apioption "github.com/akuity/kargo/internal/api/option"
"github.com/akuity/kargo/internal/cli/env"
"github.com/akuity/kargo/internal/cli/option"
"github.com/akuity/kargo/internal/config"
"github.com/akuity/kargo/internal/kubeclient"
)

Expand All @@ -27,10 +31,28 @@ func NewRootCommand(opt *option.Option) *cobra.Command {
SilenceUsage: true,
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
ctx := buildRootContext(cmd.Context())
cfg := config.NewCLIConfig()
rc, err := cfg.RESTConfig()

restCfg, err := config.GetConfig()
if err != nil {
return errors.Wrap(err, "load kubeconfig")
return errors.Wrap(err, "error loading REST config")
}
var kubeClient client.Client
{
scheme := runtime.NewScheme()
if err = corev1.AddToScheme(scheme); err != nil {
return errors.Wrap(err, "error adding Kubernetes core API to scheme")
}
if err = kargoAPI.AddToScheme(scheme); err != nil {
return errors.Wrap(err, "error adding Kargo API to scheme")
}
if kubeClient, err = client.New(
restCfg,
client.Options{
Scheme: scheme,
},
); err != nil {
return errors.Wrap(err, "error initializing Kubernetes client")
}
}

opt.ClientOption = apioption.NewClientOption(opt.UseLocalServer)
Expand All @@ -40,19 +62,14 @@ func NewRootCommand(opt *option.Option) *cobra.Command {
return errors.Wrap(err, "start local server")
}
ctx = context.WithValue(ctx, localServerListenerKey{}, l)
srv, err := api.NewServer(config.APIConfig{
BaseConfig: cfg.BaseConfig,
LocalMode: true,
}, rc)
srv, err := api.NewServer(kubeClient, api.ServerConfig{})
if err != nil {
return errors.Wrap(err, "new api server")
}
go func() {
_ = srv.Serve(ctx, l)
}()
go srv.Serve(ctx, l, true) // nolint: errcheck
opt.ServerURL = fmt.Sprintf("http://%s", l.Addr())
} else {
cred, err := kubeclient.GetCredential(ctx, rc)
cred, err := kubeclient.GetCredential(ctx, restCfg)
if err != nil {
return errors.Wrap(err, "get credential")
}
Expand Down
65 changes: 44 additions & 21 deletions cmd/controlplane/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,16 @@ import (
"net/http"

"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/client/config"

kargoAPI "github.com/akuity/kargo/api/v1alpha1"
"github.com/akuity/kargo/internal/api"
"github.com/akuity/kargo/internal/config"
"github.com/akuity/kargo/internal/kubeclient"
"github.com/akuity/kargo/internal/logging"
"github.com/akuity/kargo/internal/os"
)

func newAPICommand() *cobra.Command {
Expand All @@ -22,29 +25,49 @@ func newAPICommand() *cobra.Command {
SilenceErrors: true,
SilenceUsage: true,
RunE: func(cmd *cobra.Command, args []string) error {
cfg := config.NewAPIConfig()
rc, err := cfg.RESTConfig()
if err != nil {
return errors.Wrap(err, "load kubeconfig")
}
rc.WrapTransport = func(rt http.RoundTripper) http.RoundTripper {
return kubeclient.NewCredentialInjector(rt)
var kubeClient client.Client
{
restCfg, err := config.GetConfig()
if err != nil {
return errors.Wrap(err, "error loading REST config")
}
restCfg.WrapTransport = func(rt http.RoundTripper) http.RoundTripper {
return kubeclient.NewCredentialInjector(rt)
}
scheme := runtime.NewScheme()
if err = corev1.AddToScheme(scheme); err != nil {
return errors.Wrap(err, "error adding Kubernetes core API to scheme")
}
if err = kargoAPI.AddToScheme(scheme); err != nil {
return errors.Wrap(err, "error adding Kargo API to scheme")
}
if kubeClient, err = client.New(
restCfg,
client.Options{
Scheme: scheme,
},
); err != nil {
return errors.Wrap(err, "error initializing Kubernetes client")
}
}
logger := log.New()
logger.SetLevel(cfg.LogLevel)
ctx := logging.ContextWithLogger(cmd.Context(), logger.WithFields(nil))
srv, err := api.NewServer(cfg, rc)

srv, err := api.NewServer(kubeClient, api.ServerConfigFromEnv())
if err != nil {
return errors.Wrap(err, "new api server")
return errors.Wrap(err, "error creating API server")
}
l, err := net.Listen("tcp", fmt.Sprintf("%s:%d", cfg.Host, cfg.Port))
l, err := net.Listen(
"tcp",
fmt.Sprintf(
"%s:%s",
os.GetEnv("HOST", "0.0.0.0"),
os.GetEnv("PORT", "8080"),
),
)
if err != nil {
return errors.Wrap(err, "new listener")
return errors.Wrap(err, "error creating listener")
}
defer func() {
_ = l.Close()
}()
return srv.Serve(ctx, l)
defer l.Close()
return srv.Serve(cmd.Context(), l, false)
},
}
}
22 changes: 14 additions & 8 deletions cmd/controlplane/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,13 @@ import (

"github.com/akuity/bookkeeper"
api "github.com/akuity/kargo/api/v1alpha1"
libConfig "github.com/akuity/kargo/internal/config"
"github.com/akuity/kargo/internal/controller/applications"
"github.com/akuity/kargo/internal/controller/environments"
"github.com/akuity/kargo/internal/controller/promotions"
"github.com/akuity/kargo/internal/credentials"
"github.com/akuity/kargo/internal/logging"
"github.com/akuity/kargo/internal/os"
"github.com/akuity/kargo/internal/types"
versionpkg "github.com/akuity/kargo/internal/version"
)

Expand All @@ -38,8 +40,6 @@ func newControllerCommand() *cobra.Command {
"commit": version.GitCommit,
}).Info("Starting Kargo Controller")

cfg := libConfig.NewControllerConfig()

var kargoMgr manager.Manager
{
restCfg, err := getRestConfig("kargo", false)
Expand Down Expand Up @@ -83,8 +83,12 @@ func newControllerCommand() *cobra.Command {

var appMgr manager.Manager
{
restCfg, err :=
getRestConfig("argo", cfg.ArgoCDPreferInClusterRestConfig)
restCfg, err := getRestConfig(
"argo",
types.MustParseBool(
os.GetEnv("ARGOCD_PREFER_IN_CLUSTER_REST_CONFIG", "false"),
),
)
if err != nil {
return errors.Wrap(
err,
Expand Down Expand Up @@ -122,12 +126,14 @@ func newControllerCommand() *cobra.Command {
}

argoMgrForCreds := appMgr
if !cfg.ArgoCDCredentialBorrowingEnabled {
if !types.MustParseBool(
os.GetEnv("ARGOCD_ENABLE_CREDENTIAL_BORROWING", "false"),
) {
argoMgrForCreds = nil
}
credentialsDB, err := credentials.NewKubernetesDatabase(
ctx,
cfg.ArgoCDNamespace,
os.GetEnv("ARGOCD_NAMESPACE", "argocd"),
kargoMgr,
argoMgrForCreds,
)
Expand All @@ -150,7 +156,7 @@ func newControllerCommand() *cobra.Command {
credentialsDB,
bookkeeper.NewService(
&bookkeeper.ServiceOptions{
LogLevel: bookkeeper.LogLevel(cfg.LogLevel),
LogLevel: bookkeeper.LogLevel(logging.LoggerFromContext(ctx).Level),
},
),
); err != nil {
Expand Down
10 changes: 5 additions & 5 deletions cmd/controlplane/webhooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
ctrl "sigs.k8s.io/controller-runtime"

api "github.com/akuity/kargo/api/v1alpha1"
libConfig "github.com/akuity/kargo/internal/config"
versionpkg "github.com/akuity/kargo/internal/version"
"github.com/akuity/kargo/internal/webhooks/environments"
"github.com/akuity/kargo/internal/webhooks/promotions"
Expand All @@ -30,8 +29,6 @@ func newWebhooksServerCommand() *cobra.Command {
"commit": version.GitCommit,
}).Info("Starting Kargo Webhooks Server")

webhooksCfg := libConfig.NewWebhooksConfig()

restCfg, err := getRestConfig("kargo", false)
if err != nil {
return errors.Wrap(err, "error getting REST config")
Expand Down Expand Up @@ -60,8 +57,11 @@ func newWebhooksServerCommand() *cobra.Command {
if err = environments.SetupWebhookWithManager(mgr); err != nil {
return errors.Wrap(err, "error initializing Environment webhooks")
}
if err =
promotions.SetupWebhookWithManager(ctx, mgr, webhooksCfg); err != nil {
if err = promotions.SetupWebhookWithManager(
ctx,
mgr,
promotions.WebhookConfigFromEnv(),
); err != nil {
return errors.Wrap(err, "error initializing Promotion webhooks")
}

Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ require (
github.com/bufbuild/connect-go v1.7.0
github.com/bufbuild/connect-grpchealth-go v1.0.0
github.com/cosmos/gogoproto v1.4.10
github.com/kelseyhightower/envconfig v1.4.0
github.com/satori/go.uuid v1.2.0
github.com/technosophos/moniker v0.0.0-20210218184952-3ea787d3943b
gopkg.in/yaml.v3 v3.0.1
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -714,6 +714,8 @@ github.com/jung-kurt/gofpdf v1.0.3-0.20190309125859-24315acbbda5/go.mod h1:7Id9E
github.com/karrick/godirwalk v1.16.1/go.mod h1:j4mkqPuvaLI8mp1DroR3P6ad7cyYd4c1qeJ3RV7ULlk=
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 h1:Z9n2FFNUXsshfwJMBgNA0RU6/i7WVaAegv3PtuIHPMs=
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51/go.mod h1:CzGEWj7cYgsdH8dAjBGEr58BoE7ScuLd+fwFZ44+/x8=
github.com/kelseyhightower/envconfig v1.4.0 h1:Im6hONhd3pLkfDFsbRgu68RDNkGF1r3dvMUtDTo2cv8=
github.com/kelseyhightower/envconfig v1.4.0/go.mod h1:cccZRl6mQpaq41TPp5QxidR+Sa3axMbJDNb//FQX6Gg=
github.com/kevinburke/ssh_config v0.0.0-20190725054713-01f96b0aa0cd/go.mod h1:CT57kijsi8u/K/BOFA39wgDQJ9CxiF4nAY/ojJ6r6mM=
github.com/kevinburke/ssh_config v0.0.0-20201106050909-4977a11b4351 h1:DowS9hvgyYSX4TO5NpyC606/Z4SxnNYbT+WX27or6Ck=
github.com/kevinburke/ssh_config v0.0.0-20201106050909-4977a11b4351/go.mod h1:CT57kijsi8u/K/BOFA39wgDQJ9CxiF4nAY/ojJ6r6mM=
Expand Down
7 changes: 3 additions & 4 deletions internal/api/option/option.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"github.com/bufbuild/connect-go"
log "github.com/sirupsen/logrus"

"github.com/akuity/kargo/internal/config"
"github.com/akuity/kargo/internal/logging"
)

Expand All @@ -22,11 +21,11 @@ func NewClientOption(skipAuth bool) connect.ClientOption {
)
}

func NewHandlerOption(cfg config.APIConfig, logger *log.Entry) connect.HandlerOption {
func NewHandlerOption(ctx context.Context, localMode bool) connect.HandlerOption {
interceptors := []connect.Interceptor{
newLogInterceptor(logger, loggingIgnorableMethods),
newLogInterceptor(logging.LoggerFromContext(ctx), loggingIgnorableMethods),
}
if !cfg.LocalMode {
if !localMode {
interceptors = append(interceptors, newAuthInterceptor())
}
return connect.WithHandlerOptions(
Expand Down
43 changes: 20 additions & 23 deletions internal/api/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,14 @@ import (

"github.com/bufbuild/connect-go"
grpchealth "github.com/bufbuild/connect-grpchealth-go"
"github.com/kelseyhightower/envconfig"
"github.com/pkg/errors"
"golang.org/x/net/http2"
"golang.org/x/net/http2/h2c"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/rest"
"sigs.k8s.io/controller-runtime/pkg/client"

kubev1alpha1 "github.com/akuity/kargo/api/v1alpha1"
"github.com/akuity/kargo/internal/api/handler"
"github.com/akuity/kargo/internal/api/option"
"github.com/akuity/kargo/internal/config"
"github.com/akuity/kargo/internal/logging"
svcv1alpha1 "github.com/akuity/kargo/pkg/api/service/v1alpha1"
"github.com/akuity/kargo/pkg/api/service/v1alpha1/svcv1alpha1connect"
Expand All @@ -29,40 +25,41 @@ var (
_ svcv1alpha1connect.KargoServiceHandler = &server{}
)

type ServerConfig struct {
GracefulShutdownTimeout time.Duration `envconfig:"GRACEFUL_SHUTDOWN_TIMEOUT" default:"30s"`
}

func ServerConfigFromEnv() ServerConfig {
cfg := ServerConfig{}
envconfig.MustProcess("", &cfg)
return cfg
}

type server struct {
cfg config.APIConfig
cfg ServerConfig
kc client.Client
}

type Server interface {
Serve(ctx context.Context, l net.Listener) error
Serve(ctx context.Context, l net.Listener, localMode bool) error
}

func NewServer(cfg config.APIConfig, rc *rest.Config) (Server, error) {
scheme := runtime.NewScheme()
if err := corev1.AddToScheme(scheme); err != nil {
return nil, errors.Wrap(err, "add core api to scheme")
}
if err := kubev1alpha1.AddToScheme(scheme); err != nil {
return nil, errors.Wrap(err, "add kargo api to scheme")
}
kc, err := client.New(rc, client.Options{
Scheme: scheme,
})
if err != nil {
return nil, errors.Wrap(err, "new client")
}
func NewServer(kc client.Client, cfg ServerConfig) (Server, error) {
return &server{
cfg: cfg,
kc: kc,
}, nil
}

func (s *server) Serve(ctx context.Context, l net.Listener) error {
func (s *server) Serve(
ctx context.Context,
l net.Listener,
localMode bool,
) error {
log := logging.LoggerFromContext(ctx)
mux := http.NewServeMux()

opts := option.NewHandlerOption(s.cfg, log)
opts := option.NewHandlerOption(ctx, localMode)
mux.Handle(grpchealth.NewHandler(NewHealthChecker(), opts))
path, svcHandler := svcv1alpha1connect.NewKargoServiceHandler(s, opts)
mux.Handle(path, svcHandler)
Expand Down
Loading

0 comments on commit 9f90042

Please sign in to comment.