Skip to content

Commit

Permalink
Fix KubeAPIURL for CPLB
Browse files Browse the repository at this point in the history
Signed-off-by: Kimmo Lehto <[email protected]>
  • Loading branch information
kke committed Nov 4, 2024
1 parent 081dfeb commit c4ad9a2
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 11 deletions.
34 changes: 23 additions & 11 deletions pkg/apis/k0sctl.k0sproject.io/v1beta1/cluster/spec.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,21 +83,31 @@ func (s *Spec) Validate() error {
}

func (s *Spec) clusterExternalAddress() string {
if a := s.K0s.Config.DigString("spec", "api", "externalAddress"); a != "" {
return a
}
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 vrrpAddresses, ok := cplb.Dig("keepalived", "virtualServers").([]dig.Mapping); ok && len(vrrpAddresses) > 0 {
if addr, ok := vrrpAddresses[0]["ipAddress"].(string); ok && addr != "" {
return addr
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
}
}
}
}
}
}
}

return s.K0sLeader().Address()
if leader := s.K0sLeader(); leader != nil {
return leader.Address()
}

return ""
}

func (s *Spec) clusterInternalAddress() string {
Expand All @@ -112,8 +122,10 @@ func (s *Spec) clusterInternalAddress() string {
const defaultAPIPort = 6443

func (s *Spec) APIPort() int {
if p, ok := s.K0s.Config.Dig("spec", "api", "port").(int); ok {
return p
if s.K0s != nil {
if p, ok := s.K0s.Config.Dig("spec", "api", "port").(int); ok {
return p
}
}
return defaultAPIPort
}
Expand Down
82 changes: 82 additions & 0 deletions pkg/apis/k0sctl.k0sproject.io/v1beta1/cluster/spec_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package cluster

import (
"testing"

"github.com/k0sproject/dig"
"github.com/k0sproject/rig"
"github.com/stretchr/testify/require"
"gopkg.in/yaml.v2"
)

func TestKubeAPIURL(t *testing.T) {
t.Run("with external address and port", func(t *testing.T) {
spec := &Spec{
K0s: &K0s{
Config: dig.Mapping(map[string]any{
"spec": dig.Mapping(map[string]any{
"api": dig.Mapping(map[string]any{
"port": 6444,
"externalAddress": "test.example.com",
}),
}),
}),
}, Hosts: Hosts{
&Host{
Role: "controller",
Connection: rig.Connection{
SSH: &rig.SSH{
Address: "10.0.0.1",
},
},
},
},
}
require.Equal(t, "https://test.example.com:6444", spec.KubeAPIURL())
})

t.Run("without k0s config", func(t *testing.T) {
spec := &Spec{
Hosts: Hosts{
&Host{
Role: "controller",
PrivateAddress: "10.0.0.1",
Connection: rig.Connection{
SSH: &rig.SSH{
Address: "192.168.0.1",
},
},
},
},
}
require.Equal(t, "https://192.168.0.1:6443", spec.KubeAPIURL())
})

t.Run("with CPLB", func(t *testing.T) {
specYaml := []byte(`
hosts:
- role: controller
ssh:
address: 192.168.0.1
privateAddress: 10.0.0.1
k0s:
config:
spec:
network:
controlPlaneLoadBalancing:
enabled: true
type: Keepalived
keepalived:
vrrpInstances:
- virtualIPs: ["192.168.0.10/24"]
authPass: CPLB
virtualServers:
- ipAddress: 192.168.0.10`)

spec := &Spec{}
err := yaml.Unmarshal(specYaml, spec)
require.NoError(t, err)

require.Equal(t, "https://192.168.0.10:6443", spec.KubeAPIURL())
})
}

0 comments on commit c4ad9a2

Please sign in to comment.