From 049b4aa1d733dc810e36bf233a65c398ee7314e1 Mon Sep 17 00:00:00 2001 From: Kimmo Lehto Date: Mon, 4 Nov 2024 12:28:09 +0200 Subject: [PATCH] Replace mapping + interface magic with a struct Signed-off-by: Kimmo Lehto --- .../v1beta1/cluster/spec.go | 34 ++++++++++++++----- 1 file changed, 25 insertions(+), 9 deletions(-) diff --git a/pkg/apis/k0sctl.k0sproject.io/v1beta1/cluster/spec.go b/pkg/apis/k0sctl.k0sproject.io/v1beta1/cluster/spec.go index 23340ca3..01a89461 100644 --- a/pkg/apis/k0sctl.k0sproject.io/v1beta1/cluster/spec.go +++ b/pkg/apis/k0sctl.k0sproject.io/v1beta1/cluster/spec.go @@ -6,7 +6,7 @@ import ( "github.com/creasty/defaults" "github.com/jellydator/validation" - "github.com/k0sproject/dig" + "gopkg.in/yaml.v2" ) // Spec defines cluster config spec section @@ -82,20 +82,36 @@ func (s *Spec) Validate() error { ) } +type k0sCPLBConfig struct { + Spec struct { + Network struct { + ControlPlaneLoadBalancing struct { + Enabled bool `yaml:"enabled"` + Type string `yaml:"type"` + Keepalived struct { + VirtualServers []struct { + IPAddress string `yaml:"ipAddress"` + } `yaml:"virtualServers"` + } `yaml:"keepalived"` + } `yaml:"controlPlaneLoadBalancing"` + } `yaml:"network"` + } `yaml:"spec"` +} + func (s *Spec) clusterExternalAddress() string { if s.K0s != nil { if a := s.K0s.Config.DigString("spec", "api", "externalAddress"); a != "" { return a } - if cplb, ok := s.K0s.Config.Dig("spec", "network", "controlPlaneLoadBalancing").(dig.Mapping); ok { - if enabled, ok := cplb.Dig("enabled").(bool); ok && enabled && cplb.DigString("type") == "Keepalived" { - if virtualServers, ok := cplb.Dig("keepalived", "virtualServers").([]any); ok { - for _, vs := range virtualServers { - if vserver, ok := vs.(dig.Mapping); ok { - if addr := vserver.DigString("ipAddress"); addr != "" { - return addr - } + if cfg, err := yaml.Marshal(s.K0s.Config); err == nil { + k0scfg := k0sCPLBConfig{} + if err := yaml.Unmarshal(cfg, &k0scfg); err == nil { + cplb := k0scfg.Spec.Network.ControlPlaneLoadBalancing + if cplb.Enabled && cplb.Type == "Keepalived" { + for _, vs := range cplb.Keepalived.VirtualServers { + if addr := vs.IPAddress; addr != "" { + return addr } } }