initial commit

This commit is contained in:
Mike Mason
2023-07-01 00:04:52 +00:00
commit 80fb879ef6
65 changed files with 3544 additions and 0 deletions

2
cmd/doc.go Normal file
View File

@@ -0,0 +1,2 @@
// Package cmd defines all commands and flags.
package cmd

98
cmd/root.go Normal file
View File

@@ -0,0 +1,98 @@
package cmd
import (
"fmt"
"os"
"strings"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"go.infratographer.com/x/loggingx"
"go.infratographer.com/x/otelx"
"go.infratographer.com/x/versionx"
"go.infratographer.com/x/viperx"
"go.uber.org/zap"
"go.equinixmetal.net/infra9-metal-bridge/internal/config"
)
const appName = "infra9-metal-bridge"
var (
cfgFile string
logger *zap.SugaredLogger
)
// rootCmd represents the base command when called without any subcommands
var rootCmd = &cobra.Command{
Use: "infra9-metal-bridge",
Short: "Bridges Metal to Infratographer services",
}
// 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() {
err := rootCmd.Execute()
if err != nil {
os.Exit(1)
}
}
func init() {
cobra.OnInitialize(initConfig)
rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is /etc/infratographer/infra9-metal-bridge.yaml)")
viperx.MustBindFlag(viper.GetViper(), "config", rootCmd.PersistentFlags().Lookup("config"))
// Logging flags
loggingx.MustViperFlags(viper.GetViper(), rootCmd.PersistentFlags())
// Register version command
versionx.RegisterCobraCommand(rootCmd, func() { versionx.PrintVersion(logger) })
// OTEL Flags
otelx.MustViperFlags(viper.GetViper(), rootCmd.Flags())
}
// initConfig reads in config file and ENV variables if set.
func initConfig() {
if cfgFile != "" {
// Use config file from the flag.
viper.SetConfigFile(cfgFile)
} else {
viper.AddConfigPath("/etc/infratographer/")
viper.SetConfigType("yaml")
viper.SetConfigName("infra9-metal-bridge")
}
viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_", "-", "_"))
viper.SetEnvPrefix("infra9metalbridge")
viper.AutomaticEnv() // read in environment variables that match
setupAppConfig()
// setupLogging()
logger = loggingx.InitLogger(appName, config.AppConfig.Logging)
// If a config file is found, read it in.
err := viper.ReadInConfig()
if err == nil {
logger.Infow("using config file",
"file", viper.ConfigFileUsed(),
)
}
setupAppConfig()
}
// setupAppConfig loads our config.AppConfig struct with the values bound by
// viper. Then, anywhere we need these values, we can just return to AppConfig
// instead of performing viper.GetString(...), viper.GetBool(...), etc.
func setupAppConfig() {
err := viper.Unmarshal(&config.AppConfig)
if err != nil {
fmt.Printf("unable to decode app config: %s", err)
os.Exit(1)
}
}

122
cmd/serve.go Normal file
View File

@@ -0,0 +1,122 @@
package cmd
import (
"github.com/spf13/cobra"
"github.com/spf13/viper"
"go.infratographer.com/x/echox"
"go.infratographer.com/x/events"
"go.infratographer.com/x/otelx"
"go.infratographer.com/x/versionx"
"go.infratographer.com/x/viperx"
"go.equinixmetal.net/infra9-metal-bridge/internal/config"
"go.equinixmetal.net/infra9-metal-bridge/internal/metal"
"go.equinixmetal.net/infra9-metal-bridge/internal/permissions"
"go.equinixmetal.net/infra9-metal-bridge/internal/pubsub"
"go.equinixmetal.net/infra9-metal-bridge/internal/routes"
"go.equinixmetal.net/infra9-metal-bridge/internal/service"
)
var serveCmd = &cobra.Command{
Use: "serve",
Short: "starts the metal bridge",
Run: serve,
}
var defaultListen = ":8080"
func init() {
rootCmd.AddCommand(serveCmd)
echox.MustViperFlags(viper.GetViper(), serveCmd.Flags(), defaultListen)
events.MustViperFlagsForPublisher(viper.GetViper(), serveCmd.Flags(), appName)
events.MustViperFlagsForSubscriber(viper.GetViper(), serveCmd.Flags())
serveCmd.PersistentFlags().StringSlice("events-topics", []string{}, "event topics to subscribe to")
viperx.MustBindFlag(viper.GetViper(), "events.topics", serveCmd.PersistentFlags().Lookup("events-topics"))
permissions.MustViperFlags(viper.GetViper(), serveCmd.Flags())
}
func serve(cmd *cobra.Command, _ []string) {
err := otelx.InitTracer(config.AppConfig.OTel, appName, logger)
if err != nil {
logger.Fatalw("error initializing tracing", "error", err)
}
publisher, err := events.NewPublisherWithLogger(config.AppConfig.Events.Publisher, logger)
if err != nil {
logger.Fatalw("unable to initialize event publisher", "error", err)
}
metal, err := metal.New(
metal.WithLogger(logger.Desugar()),
metal.WithConfig(config.AppConfig.EquinixMetal),
)
if err != nil {
logger.Fatalw("error initializing Metal client", "error", err)
}
perms, err := permissions.NewClient("",
permissions.WithLogger(logger),
permissions.WithConfig(config.AppConfig.Permissions),
)
if err != nil {
logger.Fatalw("error initializing Permissions client", "error", err)
}
service, err := service.New(publisher, metal, perms,
service.WithLogger(logger),
service.WithConfig(config.AppConfig.Service),
service.WithRoles(config.AppConfig.Roles),
)
if err != nil {
logger.Fatalw("error initializing service", "error", err)
}
subscriber, err := pubsub.NewSubscriber(cmd.Context(), config.AppConfig.Events.Subscriber, service,
pubsub.WithLogger(logger),
)
if err != nil {
logger.Fatalw("unable to initialize event subscriber", "error", err)
}
for _, topic := range viper.GetStringSlice("events.topics") {
if err := subscriber.Subscribe(topic); err != nil {
logger.Fatalw("error subscribing to topic: "+topic, "topic", topic, "error", err)
}
}
router, err := routes.NewRouter(
routes.WithLogger(logger.Desugar()),
routes.WithService(service),
)
if err != nil {
logger.Fatalw("error initializing router", "error", err)
}
srv, err := echox.NewServer(
logger.Desugar(),
echox.ConfigFromViper(viper.GetViper()),
versionx.BuildDetails(),
)
if err != nil {
logger.Fatalw("failed to initialize new server", "error", err)
}
srv.AddHandler(router)
defer subscriber.Close()
logger.Info("Listening for events")
go func() {
if err := subscriber.Listen(); err != nil {
logger.Fatalw("error listening for events", "error", err)
}
}()
if err := srv.Run(); err != nil {
logger.Fatalw("failed to run server", "error", err)
}
}