Skip to content

Commit

Permalink
clustermesh: Add JSON marshalling to AddrCluster
Browse files Browse the repository at this point in the history
Implement JSON marshalling for AddrCluster type so it can
be used in StateDB objects and dumped with cilium-dbg.

Signed-off-by: Jussi Maki <jussi@isovalent.com>
  • Loading branch information
joamaki committed Apr 25, 2024
1 parent 4951fbc commit f2ce936
Showing 1 changed file with 38 additions and 0 deletions.
38 changes: 38 additions & 0 deletions pkg/clustermesh/types/addressing.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
package types

import (
"bytes"
"errors"
"fmt"
"net"
"net/netip"
Expand Down Expand Up @@ -42,6 +44,42 @@ type AddrCluster struct {

const AddrClusterLen = 20

// MarshalJSON marshals the address as a string in the form
// <addr>@<clusterID>, e.g. "1.2.3.4@1"
func (a *AddrCluster) MarshalJSON() ([]byte, error) {
var b bytes.Buffer
b.WriteByte('"')
if !a.addr.IsValid() {
b.WriteString("0.0.0.0")
} else {
b.WriteString(a.addr.String())
}
if a.clusterID != 0 {
b.WriteByte('@')
b.WriteString(
strconv.FormatUint(uint64(a.clusterID), 10),
)
}
b.WriteByte('"')
return b.Bytes(), nil
}

func (a *AddrCluster) UnmarshalJSON(data []byte) error {
if len(data) == 0 || data[0] != '"' || data[len(data)-1] != '"' {
return errors.New("AddrCluster.UnmarshalJSON: bad address")
}
// Drop the parens
data = data[1 : len(data)-1]

a2, err := ParseAddrCluster(string(data))
if err != nil {
return err
}
a.addr = a2.addr
a.clusterID = a2.clusterID
return nil
}

// ParseAddrCluster parses s as an IP + ClusterID and returns AddrCluster.
// The string s can be a bare IP string (any IP address format allowed in
// netip.ParseAddr()) or IP string + @ + ClusterID with decimal. Bare IP
Expand Down

0 comments on commit f2ce936

Please sign in to comment.