File tree Expand file tree Collapse file tree 4 files changed +24
-29
lines changed Expand file tree Collapse file tree 4 files changed +24
-29
lines changed Original file line number Diff line number Diff line change 10
10
11
11
linters :
12
12
enable :
13
- - copyloopvar # Detects unnecessary copies of loop variables for Go >= 1.22
14
- - depguard # Checks if package imports are in a list of acceptable packages
15
- - dupword # Finds word repetitions
16
- - errorlint # Find code that will cause problems with Go's error wrapping scheme
17
- - gofmt # Checks whether code was gofmt-ed
18
- - goheader # Checks is file headers matche a given pattern
19
- - intrange # Checking for loops that could use an integer range
20
- - revive # Stricter drop-in replacement for golint
21
- - testifylint # Checks usage of github.com/stretchr/testify
22
- - unconvert # Checks for unnecessary type conversions
13
+ - copyloopvar # Detects unnecessary copies of loop variables for Go >= 1.22
14
+ - depguard # Checks if package imports are in a list of acceptable packages
15
+ - dupword # Finds word repetitions
16
+ - errorlint # Find code that will cause problems with Go's error wrapping scheme
17
+ - gofmt # Checks whether code was gofmt-ed
18
+ - goheader # Checks is file headers matche a given pattern
19
+ - intrange # Checking for loops that could use an integer range
20
+ - nosprintfhostport # Detects misuses of Sprintf to construct hosts with ports in a URL
21
+ - revive # Stricter drop-in replacement for golint
22
+ - testifylint # Checks usage of github.com/stretchr/testify
23
+ - unconvert # Checks for unnecessary type conversions
23
24
24
25
linters-settings :
25
26
depguard :
Original file line number Diff line number Diff line change @@ -18,9 +18,11 @@ package customports
18
18
19
19
import (
20
20
"bytes"
21
- "fmt"
22
21
"html/template"
22
+ "net"
23
+ "net/url"
23
24
"os"
25
+ "strconv"
24
26
"testing"
25
27
26
28
"github.com/k0sproject/k0s/inttest/common"
@@ -138,12 +140,13 @@ func (s *customPortsSuite) TestControllerJoinsWithCustomPort() {
138
140
139
141
// https://github.com/k0sproject/k0s/issues/1202
140
142
s .Run ("kubeconfigIncludesExternalAddress" , func () {
143
+ expectedURL := url.URL {Scheme : "https" , Host : net .JoinHostPort (ipAddress , strconv .Itoa (kubeAPIPort ))}
141
144
ssh , err := s .SSH (s .Context (), s .ControllerNode (0 ))
142
145
s .Require ().NoError (err )
143
146
defer ssh .Disconnect ()
144
147
145
148
out , err := ssh .ExecWithOutput (s .Context (), "/usr/local/bin/k0s kubeconfig create user | awk '$1 == \" server:\" {print $2}'" )
146
149
s .Require ().NoError (err )
147
- s .Require ().Equal (fmt . Sprintf ( "https://%s:%d" , ipAddress , kubeAPIPort ), out )
150
+ s .Require ().Equal (expectedURL . String ( ), out )
148
151
})
149
152
}
Original file line number Diff line number Diff line change @@ -18,8 +18,9 @@ package v1beta1
18
18
19
19
import (
20
20
"encoding/json"
21
- "fmt"
22
21
"net"
22
+ "net/url"
23
+ "strconv"
23
24
24
25
"github.com/k0sproject/k0s/internal/pkg/iface"
25
26
"github.com/k0sproject/k0s/internal/pkg/stringslice"
@@ -84,12 +85,6 @@ func (a *APISpec) APIAddressURL() string {
84
85
return a .getExternalURIForPort (a .Port )
85
86
}
86
87
87
- // IsIPv6String returns if ip is IPv6.
88
- func IsIPv6String (ip string ) bool {
89
- netIP := net .ParseIP (ip )
90
- return netIP != nil && netIP .To4 () == nil
91
- }
92
-
93
88
// K0sControlPlaneAPIAddress returns the controller join APIs address
94
89
func (a * APISpec ) K0sControlPlaneAPIAddress () string {
95
90
return a .getExternalURIForPort (a .K0sAPIPort )
@@ -100,10 +95,7 @@ func (a *APISpec) getExternalURIForPort(port int) string {
100
95
if a .ExternalAddress != "" {
101
96
addr = a .ExternalAddress
102
97
}
103
- if IsIPv6String (addr ) {
104
- return fmt .Sprintf ("https://[%s]:%d" , addr , port )
105
- }
106
- return fmt .Sprintf ("https://%s:%d" , addr , port )
98
+ return (& url.URL {Scheme : "https" , Host : net .JoinHostPort (addr , strconv .Itoa (port ))}).String ()
107
99
}
108
100
109
101
// Sans return the given SANS plus all local addresses and externalAddress if given
Original file line number Diff line number Diff line change @@ -136,7 +136,7 @@ func (n *Network) DNSAddress() (string, error) {
136
136
}
137
137
138
138
address := ipnet .IP .To4 ()
139
- if IsIPv6String ( ipnet . IP . String ()) {
139
+ if address == nil {
140
140
address = ipnet .IP .To16 ()
141
141
}
142
142
@@ -209,11 +209,10 @@ func (n *Network) BuildServiceCIDR(addr string) string {
209
209
if ! n .DualStack .Enabled {
210
210
return n .ServiceCIDR
211
211
}
212
- // because in the dual-stack mode k8s
213
- // relies on the ordering of the given CIDRs
214
- // we need to first give family on which
215
- // api server listens
216
- if IsIPv6String (addr ) {
212
+ // Because Kubernetes relies on the order of the given CIDRs in dual-stack
213
+ // mode, the CIDR whose version matches the version of the IP address the
214
+ // API server is listening on must be specified first.
215
+ if ip := net .ParseIP (addr ); ip != nil && ip .To4 () == nil {
217
216
return n .DualStack .IPv6ServiceCIDR + "," + n .ServiceCIDR
218
217
}
219
218
return n .ServiceCIDR + "," + n .DualStack .IPv6ServiceCIDR
You can’t perform that action at this time.
0 commit comments