Skip to content

Commit

Permalink
Improve calico CNI detection
Browse files Browse the repository at this point in the history
A K8s cluster can be installed with Calico CNI in multiple ways.
It was seen that in some deployments the calico-config map is not
created but we can find calico-node cni pods running as daemonSet.
This PR enhances the code to look for calico-node pods when the config
map is not found.

Signed-off-by: Sridhar Gaddam <[email protected]>
  • Loading branch information
sridhargaddam authored and skitt committed Sep 12, 2023
1 parent 8151e3d commit 14b948a
Showing 1 changed file with 42 additions and 11 deletions.
53 changes: 42 additions & 11 deletions pkg/discovery/network/calico.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,30 +23,27 @@ import (

"github.com/pkg/errors"
"github.com/submariner-io/submariner/pkg/cni"
v1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
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 discoverCalicoNetwork(ctx context.Context, client controllerClient.Client) (*ClusterNetwork, error) {
cmList := &corev1.ConfigMapList{}

err := client.List(ctx, cmList, controllerClient.InNamespace(metav1.NamespaceAll))
found, err := calicoConfigMapExists(ctx, client)
if err != nil {
return nil, errors.Wrapf(err, "error listing ConfigMaps")
return nil, err
}

findCalicoConfigMap := false

for i := range cmList.Items {
if cmList.Items[i].Name == "calico-config" {
findCalicoConfigMap = true
break
if !found {
found, err = calicoDaemonSetExists(ctx, client)
if err != nil {
return nil, err
}
}

if !findCalicoConfigMap {
if !found {
return nil, nil
}

Expand All @@ -62,3 +59,37 @@ func discoverCalicoNetwork(ctx context.Context, client controllerClient.Client)

return nil, nil
}

func calicoConfigMapExists(ctx context.Context, client controllerClient.Client) (bool, error) {
cmList := &corev1.ConfigMapList{}

err := client.List(ctx, cmList, controllerClient.InNamespace(metav1.NamespaceAll))
if err != nil {
return false, errors.Wrapf(err, "error listing ConfigMaps")
}

for i := range cmList.Items {
if cmList.Items[i].Name == "calico-config" {
return true, nil
}
}

return false, nil
}

func calicoDaemonSetExists(ctx context.Context, client controllerClient.Client) (bool, error) {
dsList := &v1.DaemonSetList{}

err := client.List(ctx, dsList, controllerClient.InNamespace(metav1.NamespaceAll))
if err != nil {
return false, errors.Wrapf(err, "error listing DaemonSets")
}

for i := range dsList.Items {
if dsList.Items[i].Name == "calico-node" {
return true, nil
}
}

return false, nil
}

0 comments on commit 14b948a

Please sign in to comment.