Skip to content

Commit

Permalink
chore: support ipv6 (#6857) (#6862)
Browse files Browse the repository at this point in the history
  • Loading branch information
sophon-zt authored Mar 22, 2024
1 parent 65c95ba commit 5cbcd36
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 13 deletions.
19 changes: 18 additions & 1 deletion cmd/reloader/app/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ import (
"context"
"fmt"
"net"
"net/netip"
"os"
"strconv"
"strings"
"time"

Expand All @@ -39,6 +41,13 @@ import (
cfgproto "github.com/apecloud/kubeblocks/pkg/configuration/proto"
)

const (
InaddrAny = "0.0.0.0"
Inaddr6Any = "::"
InaddrLoop = "localhost"
Inaddr6Loop = "::1"
)

var logger *zap.SugaredLogger

// NewConfigManagerCommand is used to reload configuration
Expand Down Expand Up @@ -90,6 +99,7 @@ func run(ctx context.Context, opt *VolumeWatcherOpts) error {
if configHandler, err = cfgcore.CreateCombinedHandler(opt.CombConfig, opt.BackupPath); err != nil {
return err
}

if len(opt.VolumeDirs) > 0 {
if volumeWatcher, err = startVolumeWatcher(ctx, opt, configHandler); err != nil {
return err
Expand Down Expand Up @@ -145,8 +155,15 @@ func startGRPCService(opt *VolumeWatcherOpts, ctx context.Context, handler cfgco
return err
}

tcpSpec := fmt.Sprintf("%s:%d", proxy.opt.PodIP, proxy.opt.GrpcPort)
// ipv4 unspecified address: 0.0.0.0
hostIP := InaddrAny
if ip, _ := netip.ParseAddr(proxy.opt.PodIP); ip.Is6() {
// ipv6 unspecified address: ::
hostIP = Inaddr6Any
}

tcpSpec := net.JoinHostPort(hostIP, strconv.Itoa(proxy.opt.GrpcPort))
// tcpSpec := fmt.Sprintf("[::]:%d", proxy.opt.GrpcPort)
logger.Infof("starting reconfigure service: %s", tcpSpec)
listener, err := net.Listen("tcp", tcpSpec)
if err != nil {
Expand Down
6 changes: 1 addition & 5 deletions cmd/reloader/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,6 @@ import (
viper "github.com/apecloud/kubeblocks/pkg/viperx"
)

// Reload configuration
// TODO(zt) support unix signal
// TODO(zt) support shell

func main() {
ctx, cancel := context.WithCancel(context.Background())
sig := make(chan os.Signal, 1)
Expand All @@ -48,7 +44,7 @@ func main() {

viper.AutomaticEnv()
cmd := app.NewConfigManagerCommand(ctx, filepath.Base(os.Args[0]))
if err := cmd.Execute(); err != nil && errors.Cause(err) != context.Canceled {
if err := cmd.Execute(); err != nil && errors.Is(errors.Cause(err), context.Canceled) {
fmt.Println(err)
os.Exit(-1)
}
Expand Down
36 changes: 29 additions & 7 deletions controllers/apps/configuration/policy_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"context"
"fmt"
"net"
"net/netip"
"strconv"

appv1 "k8s.io/api/apps/v1"
Expand Down Expand Up @@ -163,16 +164,37 @@ func cfgManagerGrpcURL(pod *corev1.Pod) (string, error) {
}

func getURLFromPod(pod *corev1.Pod, portPort int) (string, error) {
ip := net.ParseIP(pod.Status.PodIP)
if ip == nil {
return "", core.MakeError("%s is not a valid IP", pod.Status.PodIP)
ip, err := ipAddressFromPod(pod.Status)
if err != nil {
return "", err
}
return net.JoinHostPort(ip.String(), strconv.Itoa(portPort)), nil
}

// Sanity check PodIP
if ip.To4() == nil && ip.To16() == nil {
return "", fmt.Errorf("%s is not a valid IPv4/IPv6 address", pod.Status.PodIP)
func ipAddressFromPod(status corev1.PodStatus) (net.IP, error) {
// IPv4 address priority
for _, ip := range status.PodIPs {
address, err := netip.ParseAddr(ip.IP)
if err != nil || address.Is6() {
continue
}
return net.ParseIP(ip.IP), nil
}
return net.JoinHostPort(ip.String(), strconv.Itoa(portPort)), nil

// Using status.PodIP
address := net.ParseIP(status.PodIP)
if !validIPv4Address(address) && !validIPv6Address(address) {
return nil, fmt.Errorf("%s is not a valid IPv4/IPv6 address", status.PodIP)
}
return address, nil
}

func validIPv4Address(ip net.IP) bool {
return ip != nil && ip.To4() != nil
}

func validIPv6Address(ip net.IP) bool {
return ip != nil && ip.To16() != nil
}

func restartWorkloadComponent[T generics.Object, PT generics.PObject[T], L generics.ObjList[T], PL generics.PObjList[T, L]](cli client.Client, ctx context.Context, annotationKey, annotationValue string, obj PT, _ func(T, PT, L, PL)) error {
Expand Down

0 comments on commit 5cbcd36

Please sign in to comment.