Skip to content

Commit

Permalink
Add Support for NTP servers in lbm controller
Browse files Browse the repository at this point in the history
  • Loading branch information
dergeberl committed Sep 6, 2024
1 parent 988d32e commit 16e97b3
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 3 deletions.
9 changes: 9 additions & 0 deletions cmd/yawol-controller/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"flag"
"os"
"strings"
"time"

"github.com/stackitcloud/yawol/controllers/yawol-controller/loadbalancer"
Expand Down Expand Up @@ -68,6 +69,9 @@ func main() {
var yawolletRequeueTime int
var lbmDeletionGracePeriod time.Duration

var ntpServers string
var ntpPool string

var openstackTimeout time.Duration

// settings for leases
Expand Down Expand Up @@ -108,6 +112,9 @@ func main() {
"Grace period before deleting a load balancer machine AFTER the machine has first been identified as unready.",
)

flag.StringVar(&ntpPool, "ntp-pool", "", "A NTP Pool which is used in LBMs.")
flag.StringVar(&ntpServers, "ntp-servers", "", "A comma separated list of NTP Servers which are used in LBMs.")

flag.DurationVar(&openstackTimeout, "openstack-timeout", 20*time.Second, "Timeout for all requests against Openstack.")

flag.IntVar(&leasesDurationInt, "leases-duration", 60,
Expand Down Expand Up @@ -288,6 +295,8 @@ func main() {
Metrics: &helpermetrics.LoadBalancerMachineMetrics,
OpenstackTimeout: openstackTimeout,
YawolletRequeueTime: yawolletRequeueTime,
NTPServers: strings.Split(ntpServers, ","),
NTPPool: ntpPool,
DiscoveryClient: discoveryClient,
RateLimiter: rateLimiter,
}).SetupWithManager(loadBalancerMachineMgr); err != nil {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ type LoadBalancerMachineReconciler struct { //nolint:revive // naming from kubeb
WorkerCount int
OpenstackTimeout time.Duration
YawolletRequeueTime int
NTPServers []string
NTPPool string
DiscoveryClient *discovery.DiscoveryClient
RateLimiter ratelimiter.RateLimiter
}
Expand Down Expand Up @@ -685,6 +687,8 @@ func (r *LoadBalancerMachineReconciler) reconcileServer(
loadBalancerMachine,
vip,
r.YawolletRequeueTime,
r.NTPPool,
r.NTPServers,
)
if err != nil {
return err
Expand Down
43 changes: 40 additions & 3 deletions internal/helper/loadbalancermachine.go
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,8 @@ func GenerateUserData(
loadbalancerMachine *yawolv1beta1.LoadBalancerMachine,
vip string,
yawolletRequeueTime int,
ntpPool string,
ntpServers []string,
) (string, error) {
var err error
const (
Expand Down Expand Up @@ -281,6 +283,11 @@ func GenerateUserData(
}
promtailConfigBase64 := base64.StdEncoding.EncodeToString([]byte(promtailConfig))

var chronyConfig string
if ntpPool != "" || len(ntpServers) > 0 {
chronyConfig = generateChronyConfig(ntpPool, ntpServers)
}

sshOpenRC := openRCDel
sshOpenRCState := openRCStop
if loadbalancer.Spec.DebugSettings.Enabled {
Expand All @@ -300,9 +307,20 @@ func GenerateUserData(
yawolletArgs = yawolletArgs + "-requeue-time=" + strconv.Itoa(yawolletRequeueTime) + " "
}

return `
cloudInit := `
#cloud-config
write_files:
write_files:`

if chronyConfig != "" {
cloudInit += `
- encoding: b64
content: ` + base64.StdEncoding.EncodeToString([]byte(chronyConfig)) + `
owner: root:root
path: /etc/chrony/chrony.conf
permissions: '0644'`
}

cloudInit += `
- encoding: b64
content: ` + kubeconfigBase64 + `
owner: yawol:yawol
Expand Down Expand Up @@ -334,7 +352,26 @@ runcmd:
- [ /sbin/rc-service, keepalived, restart ]
- [ /sbin/rc-service, envoy, restart ]
- [ /sbin/rc-service, yawollet, restart ]
`, nil
`

return cloudInit, nil
}

func generateChronyConfig(ntpPool string, ntpServers []string) string {
var chronyConfig string
if len(ntpServers) > 0 {
for _, ntpServer := range ntpServers {
chronyConfig += fmt.Sprintf("server %s iburst\n", ntpServer)
}
}
if ntpPool != "" {
// most likely you use pool or server but as the chrony config is valid with both we just add both.
chronyConfig += fmt.Sprintf("pool %s iburst\n", ntpPool)
}
chronyConfig += `driftfile /var/lib/chrony/chrony.drift
rtcsync
cmdport 0`
return chronyConfig
}

func generateKeepalivedConfig(vip string) string {
Expand Down

0 comments on commit 16e97b3

Please sign in to comment.