Skip to content

Commit

Permalink
Enhance flannel discovery by listing DaemonSets across all namespaces
Browse files Browse the repository at this point in the history
Signed-off-by: Aswin A <[email protected]>

Contributes to:  submariner-io/submariner#3210
  • Loading branch information
aswinayyolath committed Nov 5, 2024
1 parent 54cae5f commit b99b801
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 16 deletions.
7 changes: 6 additions & 1 deletion pkg/discovery/network/canal.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,12 @@ func discoverCanalFlannelNetwork(ctx context.Context, client controllerClient.Cl
return nil, nil
}

// Get the namespace from the first DaemonSet
namespace := daemonsets.Items[0].Namespace

// Pass the namespace to the function call
clusterNetwork, err := extractCIDRsFromFlannelConfigMap(ctx, client, findFlannelConfigMapName(
daemonsets.Items[0].Spec.Template.Spec.Volumes))
daemonsets.Items[0].Spec.Template.Spec.Volumes), namespace)
if err != nil || clusterNetwork == nil {
return nil, err
}
Expand All @@ -53,3 +57,4 @@ func discoverCanalFlannelNetwork(ctx context.Context, client controllerClient.Cl

return clusterNetwork, nil
}

31 changes: 17 additions & 14 deletions pkg/discovery/network/flannel.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,65 +28,68 @@ import (
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
controllerClient "sigs.k8s.io/controller-runtime/pkg/client"
)

//nolint:nilnil // Intentional as the purpose is to discover.
func discoverFlannelNetwork(ctx context.Context, client controllerClient.Client) (*ClusterNetwork, error) {
daemonsets := &appsv1.DaemonSetList{}

err := client.List(ctx, daemonsets, controllerClient.InNamespace(metav1.NamespaceSystem))
// Use a label selector to find DaemonSets with the "k8s-app=flannel" label in any namespace
labelSelector := controllerClient.MatchingLabels{"k8s-app": "flannel"}
err := client.List(ctx, daemonsets, labelSelector)
if err != nil {
return nil, errors.WithMessage(err, "error listing the Daemonsets for flannel discovery")
}

var flannelDaemonSet *appsv1.DaemonSet
volumes := make([]corev1.Volume, 0)

// look for a daemonset matching "flannel"
for k := range daemonsets.Items {
if strings.Contains(daemonsets.Items[k].Name, "flannel") {
volumes = daemonsets.Items[k].Spec.Template.Spec.Volumes
for _, ds := range daemonsets.Items {
if strings.Contains(ds.Name, "flannel") {
flannelDaemonSet = &ds
volumes = ds.Spec.Template.Spec.Volumes
break
}
}

if len(volumes) < 1 {
if flannelDaemonSet == nil || len(volumes) < 1 {
return nil, nil
}

clusterNetwork, err := extractCIDRsFromFlannelConfigMap(ctx, client, findFlannelConfigMapName(volumes))
// Extract the ConfigMap name from the DaemonSet's volumes and check in the same namespace
configMapName := findFlannelConfigMapName(volumes)
clusterNetwork, err := extractCIDRsFromFlannelConfigMap(ctx, client, configMapName, flannelDaemonSet.Namespace)
if err != nil || clusterNetwork == nil {
return nil, err
}

clusterNetwork.NetworkPlugin = cni.Flannel

return clusterNetwork, nil
}

//nolint:nilnil // Intentional as the purpose is to discover.
func extractCIDRsFromFlannelConfigMap(ctx context.Context, client controllerClient.Client, configMapName string) (*ClusterNetwork, error) {
func extractCIDRsFromFlannelConfigMap(ctx context.Context, client controllerClient.Client, configMapName, namespace string) (*ClusterNetwork, error) {
var podCIDR *string

if configMapName == "" {
podIPRange, err := findPodIPRange(ctx, client)
if err != nil {
return nil, err
}

podCIDR = &podIPRange
} else {
// look for the configmap details using the configmap name discovered from the daemonset
// Look for the ConfigMap in the specified namespace
cm := &corev1.ConfigMap{}

err := client.Get(ctx, controllerClient.ObjectKey{
Namespace: metav1.NamespaceSystem,
Namespace: namespace,
Name: configMapName,
}, cm)
if err != nil {
if apierrors.IsNotFound(err) {
return nil, nil
}

return nil, errors.WithMessagef(err, "error retrieving the flannel ConfigMap %q", configMapName)
}

Expand Down
5 changes: 4 additions & 1 deletion pkg/discovery/network/flannel_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,13 @@ var flannelDaemonSet = appsv1.DaemonSet{
ObjectMeta: metav1.ObjectMeta{
Name: "kube-flannel-ds",
Namespace: metav1.NamespaceSystem,
Labels: map[string]string{"k8s-app": "flannel"},
},
Spec: appsv1.DaemonSetSpec{
Template: corev1.PodTemplateSpec{
ObjectMeta: metav1.ObjectMeta{},
ObjectMeta: metav1.ObjectMeta{
Labels: map[string]string{"k8s-app": "flannel"},
},
Spec: corev1.PodSpec{
Volumes: []corev1.Volume{flannelCfgVolume},
},
Expand Down

0 comments on commit b99b801

Please sign in to comment.