From cc989850e39c4ba7a14b008eb9deacbea9a2700f Mon Sep 17 00:00:00 2001 From: Robert Volkmann Date: Tue, 26 Sep 2023 15:32:09 +0200 Subject: [PATCH] server.go should handle the intervals --- cmd/internal/core/core.go | 76 ++++++++++++------------- cmd/internal/core/phone-home.go | 5 +- cmd/internal/core/reconfigure-switch.go | 4 +- cmd/server.go | 39 ++++++------- 4 files changed, 60 insertions(+), 64 deletions(-) diff --git a/cmd/internal/core/core.go b/cmd/internal/core/core.go index 972c2c0c..f87a0e85 100644 --- a/cmd/internal/core/core.go +++ b/cmd/internal/core/core.go @@ -2,7 +2,6 @@ package core import ( "log/slog" - "time" v1 "github.com/metal-stack/metal-api/pkg/api/v1" "github.com/metal-stack/metal-core/cmd/internal/metrics" @@ -14,17 +13,16 @@ type Core struct { log *slog.Logger logLevel string - cidr string - loopbackIP string - asn string - partitionID string - rackID string - enableReconfigureSwitch bool - reconfigureSwitchInterval time.Duration - managementGateway string - additionalBridgePorts []string - additionalBridgeVIDs []string - spineUplinks []string + cidr string + loopbackIP string + asn string + partitionID string + rackID string + enableReconfigureSwitch bool + managementGateway string + additionalBridgePorts []string + additionalBridgeVIDs []string + spineUplinks []string nos switcher.NOS @@ -38,17 +36,16 @@ type Config struct { Log *slog.Logger LogLevel string - CIDR string - LoopbackIP string - ASN string - PartitionID string - RackID string - ReconfigureSwitch bool - ReconfigureSwitchInterval time.Duration - ManagementGateway string - AdditionalBridgePorts []string - AdditionalBridgeVIDs []string - SpineUplinks []string + CIDR string + LoopbackIP string + ASN string + PartitionID string + RackID string + ReconfigureSwitch bool + ManagementGateway string + AdditionalBridgePorts []string + AdditionalBridgeVIDs []string + SpineUplinks []string NOS switcher.NOS @@ -60,22 +57,21 @@ type Config struct { func New(c Config) *Core { return &Core{ - log: c.Log, - logLevel: c.LogLevel, - cidr: c.CIDR, - loopbackIP: c.LoopbackIP, - asn: c.ASN, - partitionID: c.PartitionID, - rackID: c.RackID, - enableReconfigureSwitch: c.ReconfigureSwitch, - reconfigureSwitchInterval: c.ReconfigureSwitchInterval, - managementGateway: c.ManagementGateway, - additionalBridgePorts: c.AdditionalBridgePorts, - additionalBridgeVIDs: c.AdditionalBridgeVIDs, - spineUplinks: c.SpineUplinks, - nos: c.NOS, - driver: c.Driver, - eventServiceClient: c.EventServiceClient, - metrics: c.Metrics, + log: c.Log, + logLevel: c.LogLevel, + cidr: c.CIDR, + loopbackIP: c.LoopbackIP, + asn: c.ASN, + partitionID: c.PartitionID, + rackID: c.RackID, + enableReconfigureSwitch: c.ReconfigureSwitch, + managementGateway: c.ManagementGateway, + additionalBridgePorts: c.AdditionalBridgePorts, + additionalBridgeVIDs: c.AdditionalBridgeVIDs, + spineUplinks: c.SpineUplinks, + nos: c.NOS, + driver: c.Driver, + eventServiceClient: c.EventServiceClient, + metrics: c.Metrics, } } diff --git a/cmd/internal/core/phone-home.go b/cmd/internal/core/phone-home.go index 56d05309..805f2027 100644 --- a/cmd/internal/core/phone-home.go +++ b/cmd/internal/core/phone-home.go @@ -17,7 +17,6 @@ import ( ) const ( - phonedHomeInterval = time.Minute // lldpd sends messages every two seconds provisioningEventPhonedHome = "Phoned Home" ) @@ -25,7 +24,7 @@ const ( // provisioning event to metal-api for each machine that sent at least one // phone-home LLDP package to any interface of the host machine // during this interval. -func (c *Core) ConstantlyPhoneHome(ctx context.Context) { +func (c *Core) ConstantlyPhoneHome(ctx context.Context, interval time.Duration) { // FIXME this list of interfaces is only read on startup // if additional interfaces are configured, no new lldpd client is started and therefore no // phoned home events are sent for these interfaces. @@ -78,7 +77,7 @@ func (c *Core) ConstantlyPhoneHome(ctx context.Context) { }() // send arrived messages on a ticker basis - ticker := time.NewTicker(phonedHomeInterval) + ticker := time.NewTicker(interval) defer ticker.Stop() for { select { diff --git a/cmd/internal/core/reconfigure-switch.go b/cmd/internal/core/reconfigure-switch.go index 2cd06d4e..062987f7 100644 --- a/cmd/internal/core/reconfigure-switch.go +++ b/cmd/internal/core/reconfigure-switch.go @@ -18,10 +18,10 @@ import ( ) // ConstantlyReconfigureSwitch reconfigures the switch. -func (c *Core) ConstantlyReconfigureSwitch(ctx context.Context) { +func (c *Core) ConstantlyReconfigureSwitch(ctx context.Context, interval time.Duration) { host, _ := os.Hostname() - ticker := time.NewTicker(c.reconfigureSwitchInterval) + ticker := time.NewTicker(interval) defer ticker.Stop() for { select { diff --git a/cmd/server.go b/cmd/server.go index fab13da5..ba973d0e 100644 --- a/cmd/server.go +++ b/cmd/server.go @@ -24,6 +24,8 @@ import ( "github.com/metal-stack/v" ) +const phonedHomeInterval = time.Minute // lldpd sends messages every two seconds + func Run() { cfg := &Config{} if err := envconfig.Process("METAL_CORE", cfg); err != nil { @@ -86,23 +88,22 @@ func Run() { metrics := metrics.New() c := core.New(core.Config{ - Log: log, - LogLevel: cfg.LogLevel, - CIDR: cfg.CIDR, - LoopbackIP: cfg.LoopbackIP, - ASN: cfg.ASN, - PartitionID: cfg.PartitionID, - RackID: cfg.RackID, - ReconfigureSwitch: cfg.ReconfigureSwitch, - ReconfigureSwitchInterval: cfg.ReconfigureSwitchInterval, - ManagementGateway: cfg.ManagementGateway, - AdditionalBridgePorts: cfg.AdditionalBridgePorts, - AdditionalBridgeVIDs: cfg.AdditionalBridgeVIDs, - SpineUplinks: cfg.SpineUplinks, - NOS: nos, - Driver: driver, - EventServiceClient: grpcClient.NewEventClient(), - Metrics: metrics, + Log: log, + LogLevel: cfg.LogLevel, + CIDR: cfg.CIDR, + LoopbackIP: cfg.LoopbackIP, + ASN: cfg.ASN, + PartitionID: cfg.PartitionID, + RackID: cfg.RackID, + ReconfigureSwitch: cfg.ReconfigureSwitch, + ManagementGateway: cfg.ManagementGateway, + AdditionalBridgePorts: cfg.AdditionalBridgePorts, + AdditionalBridgeVIDs: cfg.AdditionalBridgeVIDs, + SpineUplinks: cfg.SpineUplinks, + NOS: nos, + Driver: driver, + EventServiceClient: grpcClient.NewEventClient(), + Metrics: metrics, }) err = c.RegisterSwitch() @@ -113,8 +114,8 @@ func Run() { ctx := context.Background() - go c.ConstantlyReconfigureSwitch(ctx) - go c.ConstantlyPhoneHome(ctx) + go c.ConstantlyReconfigureSwitch(ctx, cfg.ReconfigureSwitchInterval) + go c.ConstantlyPhoneHome(ctx, phonedHomeInterval) // Start metrics metricsAddr := fmt.Sprintf("%v:%d", cfg.MetricsServerBindAddress, cfg.MetricsServerPort)