Skip to content

Commit

Permalink
fix(evpn-bridge): revisions after review
Browse files Browse the repository at this point in the history
Signed-off-by: atulpatel261194 <Atul.Patel@intel.com>
  • Loading branch information
atulpatel261194 authored and sandersms committed Apr 25, 2024
1 parent 7c0854f commit 522b415
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 87 deletions.
4 changes: 1 addition & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,7 @@ COPY --from=docker.io/fullstorydev/grpcurl:v1.8.9-alpine /bin/grpcurl /usr/local
COPY --from=builder /app/config.yaml /
RUN apk add --no-cache iproute2 && \
mkdir -p /etc/iproute2/ && \
echo "255 opi_evpn_br" > /etc/iproute2/rt_protos && \
cat /etc/iproute2/rt_protos && \
ls -al /
echo "255 opi_evpn_br" > /etc/iproute2/rt_protos /
EXPOSE 50051 8082
CMD [ "/opi-evpn-bridge", "--grpcport=50051", "--httpport=8082"]
HEALTHCHECK CMD grpcurl -plaintext localhost:50051 list || exit 1
8 changes: 3 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,13 @@ PROJECTNAME=$(shell basename "$(PWD)")
# Make is verbose in Linux. Make it silent.
MAKEFLAGS += --silent

GOARCH ?= $(shell go env GOARCH) # detect automatically the underlying arch

compile: get build

build:
@echo " > Building binaries..."
@CGO_ENABLED=0 go build -o ${PROJECTNAME} ./cmd/...

build-arm:
@echo " > Building binaries..."
@CGO_ENABLED=0 env GOOS=linux GOARCH=arm64 go build -o ${PROJECTNAME} ./cmd/...
@CGO_ENABLED=0 GOARCH=$(GOARCH) go build -o ${PROJECTNAME} ./cmd/...

get:
@echo " > Checking if there are any missing dependencies..."
Expand Down
102 changes: 28 additions & 74 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import (
"net/http"
"os"
"path/filepath"
"strconv"
"time"

"github.com/spf13/cobra"
Expand Down Expand Up @@ -46,39 +45,28 @@ import (
"go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc"
)

const (
configFilePath = "./"
)

var rootCmd = &cobra.Command{
Use: "opi-evpn-bridge",
Short: "evpn bridge",
Long: "evpn bridge application",
PreRunE: func(_ *cobra.Command, _ []string) error {
return validateConfigs()
return config.ValidateConfig()
},
Run: func(_ *cobra.Command, _ []string) {

taskmanager.TaskMan.StartTaskManager()

err := infradb.NewInfraDB(config.GlobalConfig.DBAddress, config.GlobalConfig.Database)
if err != nil {
log.Println("error in creating db", err)
exit(err)
}
go runGatewayServer(config.GlobalConfig.GRPCPort, config.GlobalConfig.HTTPPort)

defer func() {
if err := infradb.Close(); err != nil {
log.Fatal(err)
}
}()

switch config.GlobalConfig.Buildenv {
case "ipu":
gen_linux.Init()
ipu_linux.Init()
frr.Init()

case "ci":
gen_linux.Init()
ci_linux.Init()
Expand All @@ -89,42 +77,31 @@ var rootCmd = &cobra.Command{

// Create GRD VRF configuration during startup
if err := createGrdVrf(); err != nil {
log.Printf("Error in creating GRD VRF %+v\n", err)
exit(err)
}

runGrpcServer(config.GlobalConfig.GRPCPort, config.GlobalConfig.TLSFiles)
},
}

// initialize the cobra configuration and bind the flags
func initialize() {
cobra.OnInitialize(initConfig)
func initialize() error {
cobra.OnInitialize(config.Initcfg)

rootCmd.PersistentFlags().StringVarP(&config.GlobalConfig.CfgFile, "config", "c", "config.yaml", "config file path")
rootCmd.PersistentFlags().IntVar(&config.GlobalConfig.GRPCPort, "grpcport", 50151, "The gRPC server port")
rootCmd.PersistentFlags().IntVar(&config.GlobalConfig.HTTPPort, "httpport", 8082, "The HTTP server port")
rootCmd.PersistentFlags().Uint16Var(&config.GlobalConfig.GRPCPort, "grpcport", 50151, "The gRPC server port")
rootCmd.PersistentFlags().Uint16Var(&config.GlobalConfig.HTTPPort, "httpport", 8082, "The HTTP server port")
rootCmd.PersistentFlags().StringVar(&config.GlobalConfig.TLSFiles, "tlsfiles", "", "TLS files in server_cert:server_key:ca_cert format.")
rootCmd.PersistentFlags().StringVar(&config.GlobalConfig.DBAddress, "dbaddress", "127.0.0.1:6379", "db address in ip_address:port format")
rootCmd.PersistentFlags().StringVar(&config.GlobalConfig.FRRAddress, "frraddress", "127.0.0.1", "Frr address in ip_address format, no port")
rootCmd.PersistentFlags().StringVar(&config.GlobalConfig.Database, "database", "redis", "Database connection string")

if err := viper.GetViper().BindPFlags(rootCmd.PersistentFlags()); err != nil {
log.Printf("Error binding flags to Viper: %v\n", err)
os.Exit(1)
return err
}
}

// initConfig read the config from file
func initConfig() {
if config.GlobalConfig.CfgFile != "" {
viper.SetConfigFile(config.GlobalConfig.CfgFile)
} else {
// Search config in the default location
viper.AddConfigPath(configFilePath)
viper.SetConfigType("yaml")
viper.SetConfigName("config.yaml")
}
config.LoadConfig()
return nil
}

const logfile string = "opi-evpn-bridge.log"
Expand All @@ -139,63 +116,40 @@ func setupLogger(filename string) {
if err != nil {
log.Fatal(err)
}
logger = log.New(io.MultiWriter(out), "", log.Lshortfile|log.LstdFlags)
logger = log.New(io.MultiWriter(out, os.Stdout), "", log.Lshortfile|log.LstdFlags)
log.SetOutput(logger.Writer())
}

// validateConfigs validates the config parameters
func validateConfigs() error {
var err error

grpcPort := viper.GetInt("grpcport")
if grpcPort <= 0 || grpcPort > 65535 {
err = fmt.Errorf("grpcPort must be a positive integer between 1 and 65535")
return err
}

httpPort := viper.GetInt("httpport")
if httpPort <= 0 || httpPort > 65535 {
err = fmt.Errorf("httpPort must be a positive integer between 1 and 65535")
return err
}

dbAddr := viper.GetString("dbaddress")
_, port, err := net.SplitHostPort(dbAddr)
if err != nil {
err = fmt.Errorf("invalid DBAddress format. It should be in ip_address:port format")
return err
}

dbPort, err := strconv.Atoi(port)
if err != nil || dbPort <= 0 || dbPort > 65535 {
err = fmt.Errorf("invalid db port. It must be a positive integer between 1 and 65535")
return err
}

frrAddr := viper.GetString("frraddress")
if net.ParseIP(frrAddr) == nil {
err = fmt.Errorf("invalid FRRAddress format. It should be a valid IP address")
return err
}

return nil
func exit(e error) {
log.Println("error while running opi-evpn-bridge", e.Error())
os.Exit(1)
}

// main function
func main() {
// setup file and console logger
setupLogger(logfile)

// initialize cobra config
initialize()
if err := initialize(); err != nil {
// log.Println(err)
exit(err)
}

// start the main cmd
if err := rootCmd.Execute(); err != nil {
log.Println(err)
os.Exit(1)
exit(err)
}

defer func() {
if err := infradb.Close(); err != nil {
exit(err)
}
}()
}

// runGrpcServer start the grpc server for all the components
func runGrpcServer(grpcPort int, tlsFiles string) {
func runGrpcServer(grpcPort uint16, tlsFiles string) {
tp := utils.InitTracerProvider("opi-evpn-bridge")
defer func() {
if err := tp.Shutdown(context.Background()); err != nil {
Expand Down Expand Up @@ -258,7 +212,7 @@ func runGrpcServer(grpcPort int, tlsFiles string) {
}

// runGatewayServer
func runGatewayServer(grpcPort int, httpPort int) {
func runGatewayServer(grpcPort uint16, httpPort uint16) {
ctx := context.Background()
ctx, cancel := context.WithCancel(ctx)
defer cancel()
Expand Down
72 changes: 67 additions & 5 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@
package config

import (
"fmt"
"log"
"net"
"strconv"

"github.com/spf13/viper"
)
Expand Down Expand Up @@ -76,8 +79,8 @@ type NetlinkConfig struct {
// Config global config structure
type Config struct {
CfgFile string
GRPCPort int `yaml:"grpcport"`
HTTPPort int `yaml:"httpport"`
GRPCPort uint16 `yaml:"grpcport"`
HTTPPort uint16 `yaml:"httpport"`
TLSFiles string `yaml:"tlsfiles"`
Database string `yaml:"database"`
DBAddress string `yaml:"dbaddress"`
Expand All @@ -99,21 +102,80 @@ func SetConfig(cfg Config) error {
return nil
}

const (
configFilePath = "./"
)

// Initcfg read the config from file
func Initcfg() {
if GlobalConfig.CfgFile != "" {
viper.SetConfigFile(GlobalConfig.CfgFile)
} else {
// Search config in the default location
viper.AddConfigPath(configFilePath)
viper.SetConfigType("yaml")
viper.SetConfigName("config.yaml")
}
if err := LoadConfig(); err != nil {
log.Fatal(err)
}
}

// LoadConfig loads the config from yaml file
func LoadConfig() {
if err := viper.ReadInConfig(); err == nil {
func LoadConfig() error {
if err := viper.ReadInConfig(); err != nil {
log.Println("Using config file:", viper.ConfigFileUsed())
return err
}

if err := viper.Unmarshal(&GlobalConfig); err != nil {
log.Println(err)
return
return err
}

log.Printf("config %+v", GlobalConfig)
return nil
}

// GetConfig gets the global config
func GetConfig() *Config {
return &GlobalConfig
}

// ValidateConfig validates the config parameters
func ValidateConfig() error {
var err error

grpcPort := viper.GetInt("grpcport")
if grpcPort <= 0 || grpcPort > 65535 {
err = fmt.Errorf("grpcPort must be a positive integer between 1 and 65535")
return err
}

httpPort := viper.GetInt("httpport")
if httpPort <= 0 || httpPort > 65535 {
err = fmt.Errorf("httpPort must be a positive integer between 1 and 65535")
return err
}

dbAddr := viper.GetString("dbaddress")
_, port, err := net.SplitHostPort(dbAddr)
if err != nil {
err = fmt.Errorf("invalid DBAddress format. It should be in ip_address:port format")
return err
}

dbPort, err := strconv.Atoi(port)
if err != nil || dbPort <= 0 || dbPort > 65535 {
err = fmt.Errorf("invalid db port. It must be a positive integer between 1 and 65535")
return err
}

frrAddr := viper.GetString("frraddress")
if net.ParseIP(frrAddr) == nil {
err = fmt.Errorf("invalid FRRAddress format. It should be a valid IP address")
return err
}

return nil
}

0 comments on commit 522b415

Please sign in to comment.