Skip to content

Commit 43b162e

Browse files
committed
Enable nosprintfhostport linter
... and fix lints on the way. Inline the IsIPV6String function, as it had only one use left, which was inlined as a one-liner. Signed-off-by: Tom Wieczorek <[email protected]>
1 parent d3eddf9 commit 43b162e

File tree

4 files changed

+24
-29
lines changed

4 files changed

+24
-29
lines changed

.golangci.yml

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,17 @@ run:
1010

1111
linters:
1212
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
2324

2425
linters-settings:
2526
depguard:

inttest/customports/customports_test.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,11 @@ package customports
1818

1919
import (
2020
"bytes"
21-
"fmt"
2221
"html/template"
22+
"net"
23+
"net/url"
2324
"os"
25+
"strconv"
2426
"testing"
2527

2628
"github.com/k0sproject/k0s/inttest/common"
@@ -138,12 +140,13 @@ func (s *customPortsSuite) TestControllerJoinsWithCustomPort() {
138140

139141
// https://github.com/k0sproject/k0s/issues/1202
140142
s.Run("kubeconfigIncludesExternalAddress", func() {
143+
expectedURL := url.URL{Scheme: "https", Host: net.JoinHostPort(ipAddress, strconv.Itoa(kubeAPIPort))}
141144
ssh, err := s.SSH(s.Context(), s.ControllerNode(0))
142145
s.Require().NoError(err)
143146
defer ssh.Disconnect()
144147

145148
out, err := ssh.ExecWithOutput(s.Context(), "/usr/local/bin/k0s kubeconfig create user | awk '$1 == \"server:\" {print $2}'")
146149
s.Require().NoError(err)
147-
s.Require().Equal(fmt.Sprintf("https://%s:%d", ipAddress, kubeAPIPort), out)
150+
s.Require().Equal(expectedURL.String(), out)
148151
})
149152
}

pkg/apis/k0s/v1beta1/api.go

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,9 @@ package v1beta1
1818

1919
import (
2020
"encoding/json"
21-
"fmt"
2221
"net"
22+
"net/url"
23+
"strconv"
2324

2425
"github.com/k0sproject/k0s/internal/pkg/iface"
2526
"github.com/k0sproject/k0s/internal/pkg/stringslice"
@@ -84,12 +85,6 @@ func (a *APISpec) APIAddressURL() string {
8485
return a.getExternalURIForPort(a.Port)
8586
}
8687

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-
9388
// K0sControlPlaneAPIAddress returns the controller join APIs address
9489
func (a *APISpec) K0sControlPlaneAPIAddress() string {
9590
return a.getExternalURIForPort(a.K0sAPIPort)
@@ -100,10 +95,7 @@ func (a *APISpec) getExternalURIForPort(port int) string {
10095
if a.ExternalAddress != "" {
10196
addr = a.ExternalAddress
10297
}
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()
10799
}
108100

109101
// Sans return the given SANS plus all local addresses and externalAddress if given

pkg/apis/k0s/v1beta1/network.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ func (n *Network) DNSAddress() (string, error) {
136136
}
137137

138138
address := ipnet.IP.To4()
139-
if IsIPv6String(ipnet.IP.String()) {
139+
if address == nil {
140140
address = ipnet.IP.To16()
141141
}
142142

@@ -209,11 +209,10 @@ func (n *Network) BuildServiceCIDR(addr string) string {
209209
if !n.DualStack.Enabled {
210210
return n.ServiceCIDR
211211
}
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 {
217216
return n.DualStack.IPv6ServiceCIDR + "," + n.ServiceCIDR
218217
}
219218
return n.ServiceCIDR + "," + n.DualStack.IPv6ServiceCIDR

0 commit comments

Comments
 (0)