Skip to content

Commit

Permalink
feat: multiple pod/ext cidr API
Browse files Browse the repository at this point in the history
  • Loading branch information
cheina97 committed Jan 10, 2025
1 parent 09b5e04 commit f28bf1b
Show file tree
Hide file tree
Showing 26 changed files with 236 additions and 113 deletions.
4 changes: 2 additions & 2 deletions apis/networking/v1beta1/configuration_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ var ConfigurationGroupVersionResource = GroupVersion.WithResource(ConfigurationR
// ClusterConfigCIDR defines the CIDR of the cluster.
type ClusterConfigCIDR struct {
// Pod CIDR of the cluster.
Pod CIDR `json:"pod,omitempty"`
Pod []CIDR `json:"pod,omitempty"`
// External CIDR of the cluster.
External CIDR `json:"external,omitempty"`
External []CIDR `json:"external,omitempty"`
}

// ClusterConfig defines the configuration of a cluster.
Expand Down
18 changes: 14 additions & 4 deletions apis/networking/v1beta1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,18 @@ spec:
properties:
external:
description: External CIDR of the cluster.
format: cidr
type: string
items:
description: CIDR defines a syntax validated CIDR.
format: cidr
type: string
type: array
pod:
description: Pod CIDR of the cluster.
format: cidr
type: string
items:
description: CIDR defines a syntax validated CIDR.
format: cidr
type: string
type: array
type: object
type: object
remote:
Expand All @@ -92,12 +98,18 @@ spec:
properties:
external:
description: External CIDR of the cluster.
format: cidr
type: string
items:
description: CIDR defines a syntax validated CIDR.
format: cidr
type: string
type: array
pod:
description: Pod CIDR of the cluster.
format: cidr
type: string
items:
description: CIDR defines a syntax validated CIDR.
format: cidr
type: string
type: array
type: object
type: object
type: object
Expand All @@ -113,12 +125,18 @@ spec:
properties:
external:
description: External CIDR of the cluster.
format: cidr
type: string
items:
description: CIDR defines a syntax validated CIDR.
format: cidr
type: string
type: array
pod:
description: Pod CIDR of the cluster.
format: cidr
type: string
items:
description: CIDR defines a syntax validated CIDR.
format: cidr
type: string
type: array
type: object
type: object
type: object
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import (
networkingv1beta1 "github.com/liqotech/liqo/apis/networking/v1beta1"
"github.com/liqotech/liqo/pkg/consts"
configuration "github.com/liqotech/liqo/pkg/liqo-controller-manager/networking/external-network/configuration"
"github.com/liqotech/liqo/pkg/utils/cidr"
ipamutils "github.com/liqotech/liqo/pkg/utils/ipam"
"github.com/liqotech/liqo/pkg/utils/resource"
)
Expand Down Expand Up @@ -74,8 +75,8 @@ func (r *ConfigurationReconciler) Reconcile(ctx context.Context, req ctrl.Reques
}
klog.V(4).Infof("Reconciling configuration %q", req.NamespacedName)

extCIDR := cfg.Status.Remote.CIDR.External
remoteUnknownSourceIP, err := ipamutils.GetUnknownSourceIP(string(extCIDR))
extCIDR := cidr.GetPrimary(cfg.Status.Remote.CIDR.External)
remoteUnknownSourceIP, err := ipamutils.GetUnknownSourceIP(extCIDR.String())
if err != nil {
return ctrl.Result{}, fmt.Errorf("unable to get the unknown source IP: %w", err)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
ipamv1alpha1 "github.com/liqotech/liqo/apis/ipam/v1alpha1"
networkingv1beta1 "github.com/liqotech/liqo/apis/networking/v1beta1"
"github.com/liqotech/liqo/pkg/consts"
"github.com/liqotech/liqo/pkg/utils/cidr"
"github.com/liqotech/liqo/pkg/utils/events"
ipamutils "github.com/liqotech/liqo/pkg/utils/ipam"
)
Expand Down Expand Up @@ -110,8 +111,8 @@ func (r *ConfigurationReconciler) defaultLocalNetwork(ctx context.Context, cfg *
}

r.localCIDR = &networkingv1beta1.ClusterConfigCIDR{
Pod: networkingv1beta1.CIDR(podCIDR),
External: networkingv1beta1.CIDR(externalCIDR),
Pod: cidr.SetPrimary(networkingv1beta1.CIDR(podCIDR)),
External: cidr.SetPrimary(networkingv1beta1.CIDR(externalCIDR)),
}
}

Expand Down Expand Up @@ -155,11 +156,11 @@ func ForgeConfigurationStatus(cfg *networkingv1beta1.Configuration, net *ipamv1a
cidrNew = net.Status.CIDR
switch cidrType {
case LabelCIDRTypePod:
cidrOld = cfg.Spec.Remote.CIDR.Pod
cfg.Status.Remote.CIDR.Pod = cidrNew
cidrOld = *cidr.GetPrimary(cfg.Spec.Remote.CIDR.Pod)
cfg.Status.Remote.CIDR.Pod = cidr.SetPrimary(cidrNew)
case LabelCIDRTypeExternal:
cidrOld = cfg.Spec.Remote.CIDR.External
cfg.Status.Remote.CIDR.External = cidrNew
cidrOld = *cidr.GetPrimary(cfg.Spec.Remote.CIDR.External)
cfg.Status.Remote.CIDR.External = cidr.SetPrimary(cidrNew)
}
klog.Infof("Configuration %s %s CIDR: %s -> %s", client.ObjectKeyFromObject(cfg).String(), cidrType, cidrOld, cidrNew)
}
Expand All @@ -168,7 +169,7 @@ func isConfigurationConfigured(cfg *networkingv1beta1.Configuration) bool {
if cfg.Status.Remote == nil {
return false
}
return cfg.Status.Remote.CIDR.Pod != "" && cfg.Status.Remote.CIDR.External != ""
return !cidr.IsVoid(cidr.GetPrimary(cfg.Status.Remote.CIDR.Pod)) && !cidr.IsVoid(cidr.GetPrimary(cfg.Status.Remote.CIDR.External))
}

// SetupWithManager register the ConfigurationReconciler to the manager.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (

ipamv1alpha1 "github.com/liqotech/liqo/apis/ipam/v1alpha1"
networkingv1beta1 "github.com/liqotech/liqo/apis/networking/v1beta1"
cidrutils "github.com/liqotech/liqo/pkg/utils/cidr"
"github.com/liqotech/liqo/pkg/utils/events"
"github.com/liqotech/liqo/pkg/utils/getters"
"github.com/liqotech/liqo/pkg/utils/resource"
Expand All @@ -51,9 +52,9 @@ func ForgeNetwork(net *ipamv1alpha1.Network, cfg *networkingv1beta1.Configuratio
var cidr networkingv1beta1.CIDR
switch cidrType {
case LabelCIDRTypePod:
cidr = cfg.Spec.Remote.CIDR.Pod
cidr = *cidrutils.GetPrimary(cfg.Spec.Remote.CIDR.Pod)
case LabelCIDRTypeExternal:
cidr = cfg.Spec.Remote.CIDR.External
cidr = *cidrutils.GetPrimary(cfg.Spec.Remote.CIDR.External)
}
net.Spec = ipamv1alpha1.NetworkSpec{
CIDR: cidr,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
"github.com/liqotech/liqo/apis/networking/v1beta1/firewall"
"github.com/liqotech/liqo/pkg/consts"
"github.com/liqotech/liqo/pkg/gateway/tunnel"
cidrutils "github.com/liqotech/liqo/pkg/utils/cidr"
"github.com/liqotech/liqo/pkg/utils/resource"
)

Expand Down Expand Up @@ -139,11 +140,11 @@ func forgeCIDRFirewallConfigurationDNATRules(cfg *networkingv1beta1.Configuratio
var remoteCIDR, remoteRemapCIDR string
switch cidrtype {
case PodCIDR:
remoteCIDR = cfg.Spec.Remote.CIDR.Pod.String()
remoteRemapCIDR = cfg.Status.Remote.CIDR.Pod.String()
remoteCIDR = cidrutils.GetPrimary(cfg.Spec.Remote.CIDR.Pod).String()
remoteRemapCIDR = cidrutils.GetPrimary(cfg.Status.Remote.CIDR.Pod).String()
case ExternalCIDR:
remoteCIDR = cfg.Spec.Remote.CIDR.External.String()
remoteRemapCIDR = cfg.Status.Remote.CIDR.External.String()
remoteCIDR = cidrutils.GetPrimary(cfg.Spec.Remote.CIDR.External).String()
remoteRemapCIDR = cidrutils.GetPrimary(cfg.Status.Remote.CIDR.External).String()
}
return []firewall.NatRule{
{
Expand Down Expand Up @@ -181,11 +182,11 @@ func forgeCIDRFirewallConfigurationSNATRules(cfg *networkingv1beta1.Configuratio
var localCIDR, remoteRemapCIDR string
switch cidrtype {
case PodCIDR:
localCIDR = cfg.Spec.Local.CIDR.Pod.String()
remoteRemapCIDR = cfg.Status.Remote.CIDR.Pod.String()
localCIDR = cidrutils.GetPrimary(cfg.Spec.Local.CIDR.Pod).String()
remoteRemapCIDR = cidrutils.GetPrimary(cfg.Status.Remote.CIDR.Pod).String()
case ExternalCIDR:
localCIDR = cfg.Spec.Local.CIDR.External.String()
remoteRemapCIDR = cfg.Status.Remote.CIDR.External.String()
localCIDR = cidrutils.GetPrimary(cfg.Spec.Local.CIDR.External).String()
remoteRemapCIDR = cidrutils.GetPrimary(cfg.Status.Remote.CIDR.External).String()
}

return []firewall.NatRule{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import (
networkingv1beta1 "github.com/liqotech/liqo/apis/networking/v1beta1"
"github.com/liqotech/liqo/pkg/consts"
configuration "github.com/liqotech/liqo/pkg/liqo-controller-manager/networking/external-network/configuration"
cidrutils "github.com/liqotech/liqo/pkg/utils/cidr"
)

// cluster-role
Expand Down Expand Up @@ -75,14 +76,14 @@ func (r *RemappingReconciler) Reconcile(ctx context.Context, req ctrl.Request) (
}
klog.V(4).Infof("Reconciling configuration %q", req.NamespacedName)

if conf.Spec.Remote.CIDR.Pod != conf.Status.Remote.CIDR.Pod {
if cidrutils.GetPrimary(conf.Spec.Remote.CIDR.Pod) != cidrutils.GetPrimary(conf.Status.Remote.CIDR.Pod) {
if err := CreateOrUpdateNatMappingCIDR(ctx, r.Client, r.Options, conf,
r.Scheme, PodCIDR); err != nil {
return ctrl.Result{}, err
}
}

if conf.Spec.Remote.CIDR.External != conf.Status.Remote.CIDR.External {
if cidrutils.GetPrimary(conf.Spec.Remote.CIDR.External) != cidrutils.GetPrimary(conf.Status.Remote.CIDR.External) {
if err := CreateOrUpdateNatMappingCIDR(ctx, r.Client, r.Options, conf,
r.Scheme, ExternalCIDR); err != nil {
return ctrl.Result{}, err
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (
"github.com/liqotech/liqo/pkg/consts"
"github.com/liqotech/liqo/pkg/gateway"
"github.com/liqotech/liqo/pkg/gateway/tunnel"
cidrutils "github.com/liqotech/liqo/pkg/utils/cidr"
"github.com/liqotech/liqo/pkg/utils/getters"
"github.com/liqotech/liqo/pkg/utils/resource"
)
Expand Down Expand Up @@ -115,20 +116,20 @@ func forgeMutateRouteConfiguration(cfg *networkingv1beta1.Configuration,
[]networkingv1beta1.Rule{
{
Iif: &internalNodes.Items[i].Spec.Interface.Gateway.Name,
Dst: &cfg.Spec.Remote.CIDR.Pod,
Dst: cidrutils.GetPrimary(cfg.Spec.Remote.CIDR.Pod),
Routes: []networkingv1beta1.Route{
{
Dst: &cfg.Spec.Remote.CIDR.Pod,
Dst: cidrutils.GetPrimary(cfg.Spec.Remote.CIDR.Pod),
Gw: ptr.To(networkingv1beta1.IP(remoteInterfaceIP)),
},
},
},
{
Iif: &internalNodes.Items[i].Spec.Interface.Gateway.Name,
Dst: &cfg.Spec.Remote.CIDR.External,
Dst: cidrutils.GetPrimary(cfg.Spec.Remote.CIDR.External),
Routes: []networkingv1beta1.Route{
{
Dst: &cfg.Spec.Remote.CIDR.External,
Dst: cidrutils.GetPrimary(cfg.Spec.Remote.CIDR.External),
Gw: ptr.To(networkingv1beta1.IP(remoteInterfaceIP)),
},
},
Expand Down
9 changes: 5 additions & 4 deletions pkg/liqo-controller-manager/networking/forge/configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
networkingv1beta1 "github.com/liqotech/liqo/apis/networking/v1beta1"
"github.com/liqotech/liqo/pkg/consts"
liqoutils "github.com/liqotech/liqo/pkg/utils"
cidrutils "github.com/liqotech/liqo/pkg/utils/cidr"
ipamutils "github.com/liqotech/liqo/pkg/utils/ipam"
)

Expand Down Expand Up @@ -62,8 +63,8 @@ func MutateConfiguration(conf *networkingv1beta1.Configuration, remoteClusterID
conf.Labels = make(map[string]string)
}
conf.Labels[consts.RemoteClusterID] = string(remoteClusterID)
conf.Spec.Remote.CIDR.Pod = networkingv1beta1.CIDR(podCIDR)
conf.Spec.Remote.CIDR.External = networkingv1beta1.CIDR(externalCIDR)
conf.Spec.Remote.CIDR.Pod = cidrutils.SetPrimary(networkingv1beta1.CIDR(podCIDR))
conf.Spec.Remote.CIDR.External = cidrutils.SetPrimary(networkingv1beta1.CIDR(externalCIDR))
}

// ConfigurationForRemoteCluster forges a Configuration of the local cluster to be applied to a remote cluster.
Expand Down Expand Up @@ -99,8 +100,8 @@ func ConfigurationForRemoteCluster(ctx context.Context, cl client.Client,
Spec: networkingv1beta1.ConfigurationSpec{
Remote: networkingv1beta1.ClusterConfig{
CIDR: networkingv1beta1.ClusterConfigCIDR{
Pod: networkingv1beta1.CIDR(podCIDR),
External: networkingv1beta1.CIDR(externalCIDR),
Pod: cidrutils.SetPrimary(networkingv1beta1.CIDR(podCIDR)),
External: cidrutils.SetPrimary(networkingv1beta1.CIDR(externalCIDR)),
},
},
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import (
internalnetwork "github.com/liqotech/liqo/pkg/liqo-controller-manager/networking/internal-network"
"github.com/liqotech/liqo/pkg/liqo-controller-manager/networking/internal-network/fabricipam"
"github.com/liqotech/liqo/pkg/utils"
cidrutils "github.com/liqotech/liqo/pkg/utils/cidr"
"github.com/liqotech/liqo/pkg/utils/getters"
"github.com/liqotech/liqo/pkg/utils/resource"
)
Expand Down Expand Up @@ -131,8 +132,8 @@ func (r *ClientReconciler) ensureInternalFabric(ctx context.Context, gwClient *n
internalFabric.Spec.Interface.Gateway.IP = networkingv1beta1.IP(ip.String())

internalFabric.Spec.RemoteCIDRs = []networkingv1beta1.CIDR{
configuration.Status.Remote.CIDR.Pod,
configuration.Status.Remote.CIDR.External,
*cidrutils.GetPrimary(configuration.Status.Remote.CIDR.Pod),
*cidrutils.GetPrimary(configuration.Status.Remote.CIDR.External),
}

return controllerutil.SetControllerReference(gwClient, internalFabric, r.Scheme)
Expand Down
Loading

0 comments on commit f28bf1b

Please sign in to comment.