Skip to content

Commit

Permalink
Install Network at install time
Browse files Browse the repository at this point in the history
  • Loading branch information
fra98 committed Dec 6, 2023
1 parent 64dc8d5 commit bb5c70d
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 32 deletions.
5 changes: 0 additions & 5 deletions apis/ipam/v1alpha1/network_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,6 @@ import (
v1alpha1networking "github.com/liqotech/liqo/apis/networking/v1alpha1"
)

const (
// NetworkLocalLabel is the label used to mark a Network intended to use locally to the cluster.
NetworkLocalLabel = "ipam.liqo.io/network-local"
)

var (
// NetworkKind is the kind name used to register the Network CRD.
NetworkKind = "Network"
Expand Down
25 changes: 8 additions & 17 deletions cmd/ipam/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,50 +177,41 @@ func initializeIPAM(ctx context.Context, ipam *liqoipam.IPAM, opts *liqoipam.Opt
return err
}

// Configure PodCIDR
if err := ipam.SetPodCIDR(opts.PodCIDR.String()); err != nil {
return err
}
if err := createReservedNetwork(ctx, cl, "local-pod", consts.NetworkTypePodCIDR, opts.PodCIDR.String()); err != nil {
return err
}

// Configure ServiceCIDR
if err := ipam.SetServiceCIDR(opts.ServiceCIDR.String()); err != nil {
return err
}
if err := createReservedNetwork(ctx, cl, "local-service", consts.NetworkTypeServiceCIDR, opts.ServiceCIDR.String()); err != nil {
return err
}

// Configure network pools.
for _, pool := range opts.AdditionalPools.StringList.StringList {
if err := ipam.AddNetworkPool(pool); err != nil {
return err
}
if err := createReservedNetwork(ctx, cl, fmt.Sprintf("pool-%s", pool), consts.NetworkTypePool, pool); err != nil {
return err
}
}

// Configure reserved pools.
if err := ipam.SetReservedSubnets(opts.ReservedPools.StringList.StringList); err != nil {
return err
}
for _, pool := range opts.ReservedPools.StringList.StringList {
if err := createReservedNetwork(ctx, cl, fmt.Sprintf("reserved-%s", pool), consts.NetworkTypeReservedSubnet, pool); err != nil {
return err
}
}

// Get an ExternalCIDR from IPAM and create associated Network.
externalCIDR, err := ipam.GetExternalCIDR(liqonetutils.GetMask(options.PodCIDR.String()))
if err != nil {
return err
}
if err := createReservedNetwork(ctx, cl, "local-external", consts.NetworkTypeExternalCIDR, externalCIDR); err != nil {
if err := createNetworkLocal(ctx, cl, "external-cidr", consts.NetworkTypeExternalCIDR, externalCIDR); err != nil {
return err
}

return nil
}

func createReservedNetwork(ctx context.Context, cl client.Client, name, networkType, cidr string) error {
func createNetworkLocal(ctx context.Context, cl client.Client, name, networkType, cidr string) error {
network := &ipamv1alpha1.Network{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Expand All @@ -232,7 +223,7 @@ func createReservedNetwork(ctx context.Context, cl client.Client, name, networkT
if network.Labels == nil {
network.Labels = map[string]string{}
}
network.Labels[ipamv1alpha1.NetworkLocalLabel] = networkType
network.Labels[consts.NetworkLocalLabelKey] = networkType

network.Spec = ipamv1alpha1.NetworkSpec{
CIDR: networkingv1alpha1.CIDR(cidr),
Expand Down
48 changes: 48 additions & 0 deletions deployments/liqo/templates/liqo-ipam-networks.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
---
{{- $ipamConfig := (merge (dict "name" "ipam" "module" "ipam") .) -}}

apiVersion: ipam.liqo.io/v1alpha1
kind: Network
metadata:
name: pod-cidr
labels:
{{- include "liqo.labels" $ipamConfig | nindent 4 }}
ipam.liqo.io/network-local: pod-cidr
spec:
cidr: {{ .Values.ipam.podCIDR }}
---
apiVersion: ipam.liqo.io/v1alpha1
kind: Network
metadata:
name: service-cidr
labels:
{{- include "liqo.labels" $ipamConfig | nindent 4 }}
ipam.liqo.io/network-local: service-cidr
spec:
cidr: {{ .Values.ipam.serviceCIDR }}
---
{{- range $i, $value := .Values.ipam.additionalPools }}
apiVersion: ipam.liqo.io/v1alpha1
kind: Network
metadata:
name: pool-{{ add $i 1 }}
labels:
{{- include "liqo.labels" $ipamConfig | nindent 4 }}
ipam.liqo.io/network-local: pool
spec:
cidr: {{ $value }}
---
{{- end }}
{{- range $i, $value := .Values.ipam.reservedSubnets }}
apiVersion: ipam.liqo.io/v1alpha1
kind: Network
metadata:
name: reserved-{{ add $i 1 }}
labels:
{{- include "liqo.labels" $ipamConfig | nindent 4 }}
ipam.liqo.io/network-local: reserved
spec:
cidr: {{ $value }}
---
{{- end }}

6 changes: 4 additions & 2 deletions pkg/consts/ipam.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ package consts
const (
// IpamPort is the port used by the IPAM gRPC server.
IpamPort = 6000
// NetworkLocalLabelKey is the label key used to mark a Network intended to be used locally to the cluster.
NetworkLocalLabelKey = "ipam.liqo.io/network-local"
// NetworkTypePodCIDR is the constant representing a network of type podCIDR.
NetworkTypePodCIDR = "pod-cidr"
// NetworkTypeServiceCIDR is the constant representing a network of type serviceCIDR.
Expand All @@ -25,6 +27,6 @@ const (
NetworkTypeExternalCIDR = "external-cidr"
// NetworkTypePool is the constant representing a network of type pool.
NetworkTypePool = "pool"
// NetworkTypeReservedSubnet is the constant representing a network of type reserved subnet.
NetworkTypeReservedSubnet = "reserved-subnet"
// NetworkTypeReserved is the constant representing a network of type reserved subnet.
NetworkTypeReserved = "reserved"
)
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"
networkingv1alpha1 "github.com/liqotech/liqo/apis/networking/v1alpha1"
"github.com/liqotech/liqo/pkg/consts"
"github.com/liqotech/liqo/pkg/ipam"
)

Expand Down Expand Up @@ -64,14 +65,14 @@ func (r *NetworkReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ct

desiredCIDR = nw.Spec.CIDR

_, localNetwork := nw.Labels[ipamv1alpha1.NetworkLocalLabel]
_, localNetwork := nw.Labels[consts.NetworkLocalLabelKey]
if localNetwork {
nw.Status.CIDR = desiredCIDR
if err := r.Status().Update(ctx, &nw); err != nil {
klog.Errorf("error while updating Network %q status: %v", req.NamespacedName, err)
return ctrl.Result{}, err
}
klog.Infof("updated Network %q status (spec: %s -> status: %s)", req.NamespacedName, nw.Spec.CIDR, nw.Status.CIDR)
klog.V(4).Infof("updated Network %q status (spec: %s -> status: %s)", req.NamespacedName, nw.Spec.CIDR, nw.Status.CIDR)
return ctrl.Result{}, nil
}

Expand Down
11 changes: 5 additions & 6 deletions pkg/utils/ipam/networks.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,14 @@ import (
"k8s.io/apimachinery/pkg/labels"
"sigs.k8s.io/controller-runtime/pkg/client"

ipamv1alpha1 "github.com/liqotech/liqo/apis/ipam/v1alpha1"
"github.com/liqotech/liqo/pkg/consts"
liqogetters "github.com/liqotech/liqo/pkg/utils/getters"
)

// RetrievePodCIDR retrieves the podCIDR of the local cluster.
func RetrievePodCIDR(ctx context.Context, cl client.Client) (string, error) {
nw, err := liqogetters.RetrieveUniqueNetworkByLabel(ctx, cl, labels.SelectorFromSet(map[string]string{
ipamv1alpha1.NetworkLocalLabel: consts.NetworkTypePodCIDR,
consts.NetworkLocalLabelKey: consts.NetworkTypePodCIDR,
}))
if err != nil {
return "", err
Expand All @@ -40,7 +39,7 @@ func RetrievePodCIDR(ctx context.Context, cl client.Client) (string, error) {
// RetrieveServiceCIDR retrieves the serviceCIDR of the local cluster.
func RetrieveServiceCIDR(ctx context.Context, cl client.Client) (string, error) {
nw, err := liqogetters.RetrieveUniqueNetworkByLabel(ctx, cl, labels.SelectorFromSet(map[string]string{
ipamv1alpha1.NetworkLocalLabel: consts.NetworkTypeServiceCIDR,
consts.NetworkLocalLabelKey: consts.NetworkTypeServiceCIDR,
}))
if err != nil {
return "", err
Expand All @@ -52,7 +51,7 @@ func RetrieveServiceCIDR(ctx context.Context, cl client.Client) (string, error)
// RetrieveExternalCIDR retrieves the externalCIDR of the local cluster.
func RetrieveExternalCIDR(ctx context.Context, cl client.Client) (string, error) {
nw, err := liqogetters.RetrieveUniqueNetworkByLabel(ctx, cl, labels.SelectorFromSet(map[string]string{
ipamv1alpha1.NetworkLocalLabel: consts.NetworkTypeExternalCIDR,
consts.NetworkLocalLabelKey: consts.NetworkTypeExternalCIDR,
}))
if err != nil {
return "", err
Expand All @@ -66,7 +65,7 @@ func RetrieveReservedSubnets(ctx context.Context, cl client.Client) ([]string, e
var reservedSubnets []string

networks, err := liqogetters.RetrieveNetworksByLabel(ctx, cl, labels.SelectorFromSet(map[string]string{
ipamv1alpha1.NetworkLocalLabel: consts.NetworkTypeReservedSubnet,
consts.NetworkLocalLabelKey: consts.NetworkTypeReserved,
}))
if err != nil {
return nil, err
Expand All @@ -84,7 +83,7 @@ func RetrieveAdditionalPools(ctx context.Context, cl client.Client) ([]string, e
var additionalPools []string

networks, err := liqogetters.RetrieveNetworksByLabel(ctx, cl, labels.SelectorFromSet(map[string]string{
ipamv1alpha1.NetworkLocalLabel: consts.NetworkTypePool,
consts.NetworkLocalLabelKey: consts.NetworkTypePool,
}))
if err != nil {
return nil, err
Expand Down

0 comments on commit bb5c70d

Please sign in to comment.