Skip to content

Commit

Permalink
fix: wrap controlplaneEndpoint in [], if provided address is an IPv6 …
Browse files Browse the repository at this point in the history
…address

Fixes #143.
  • Loading branch information
pborn-ionos committed Mar 15, 2024
1 parent a745a6a commit cfd0443
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
21 changes: 19 additions & 2 deletions internal/webhook/proxmoxcluster_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,14 +96,31 @@ func validateIPs(cluster *infrav1.ProxmoxCluster) error {

gk, name := cluster.GroupVersionKind().GroupKind(), cluster.GetName()

ipAddr, err := netip.ParseAddrPort(fmt.Sprintf("%s:%d", ep.Host, ep.Port))
endpointHostIp := ep.Host

Check warning on line 99 in internal/webhook/proxmoxcluster_webhook.go

View workflow job for this annotation

GitHub Actions / lint

var-naming: var endpointHostIp should be endpointHostIP (revive)

addr, err := netip.ParseAddr(endpointHostIp)
if err != nil {
return apierrors.NewInvalid(
gk,
name,
field.ErrorList{
field.Invalid(
field.NewPath("spec", "controlplaneEndpoint"), endpointHostIp, "provided endpoint address is not a valid IP"),
})
}
// If the passed control-plane endppoint is an IPv6 address, wrap it in [], so it can properly pass ParseAddrPort validation
if addr.Is6() {
endpointHostIp = fmt.Sprintf("[%s]", ep.Host)
}

ipAddr, err := netip.ParseAddrPort(fmt.Sprintf("%s:%d", endpointHostIp, ep.Port))
if err != nil {
return apierrors.NewInvalid(
gk,
name,
field.ErrorList{
field.Invalid(
field.NewPath("spec", "controlplaneEndpoint"), fmt.Sprintf("%s:%d", ep.Host, ep.Port), "provided endpoint is not in a valid IP and port format"),
field.NewPath("spec", "controlplaneEndpoint"), fmt.Sprintf("%s:%d", endpointHostIp, ep.Port), "provided endpoint is not in a valid IP and port format"),
})
}

Expand Down
2 changes: 1 addition & 1 deletion internal/webhook/proxmoxcluster_webhook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ var _ = Describe("Controller Test", func() {
It("should disallow invalid endpoint IP", func() {
cluster := invalidProxmoxCluster("test-cluster")
cluster.Spec.ControlPlaneEndpoint.Host = "invalid"
g.Expect(k8sClient.Create(testEnv.GetContext(), &cluster)).To(MatchError(ContainSubstring("provided endpoint is not in a valid IP and port format")))
g.Expect(k8sClient.Create(testEnv.GetContext(), &cluster)).To(MatchError(ContainSubstring("provided endpoint address is not a valid IP")))
})

It("should disallow invalid IPV4 IPs", func() {
Expand Down

0 comments on commit cfd0443

Please sign in to comment.