Skip to content

Commit

Permalink
server.go should handle the intervals
Browse files Browse the repository at this point in the history
  • Loading branch information
robertvolkmann committed Sep 26, 2023
1 parent 188ec56 commit cc98985
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 64 deletions.
76 changes: 36 additions & 40 deletions cmd/internal/core/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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

Expand All @@ -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

Expand All @@ -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,
}
}
5 changes: 2 additions & 3 deletions cmd/internal/core/phone-home.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,14 @@ import (
)

const (
phonedHomeInterval = time.Minute // lldpd sends messages every two seconds
provisioningEventPhonedHome = "Phoned Home"
)

// ConstantlyPhoneHome sends every minute a single phone-home
// 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.
Expand Down Expand Up @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions cmd/internal/core/reconfigure-switch.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
39 changes: 20 additions & 19 deletions cmd/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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()
Expand All @@ -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)
Expand Down

0 comments on commit cc98985

Please sign in to comment.