Skip to content

Commit

Permalink
Merge pull request #310 from hanweisen/feat_cilium_support
Browse files Browse the repository at this point in the history
feat: support find cilium cluster ip cidr
  • Loading branch information
kosmos-robot committed Dec 7, 2023
2 parents 03f7523 + 259e41b commit 8847a72
Show file tree
Hide file tree
Showing 14 changed files with 3,109 additions and 233 deletions.
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ require (
github.com/pkg/errors v0.9.1
github.com/projectcalico/api v0.0.0-20230602153125-fb7148692637
github.com/projectcalico/calico v1.11.0-cni-plugin.0.20220623222645-a52cb86dbaad
github.com/sirupsen/logrus v1.9.0
github.com/spf13/cast v1.6.0
github.com/spf13/cobra v1.6.0
github.com/spf13/pflag v1.0.5
github.com/vishvananda/netlink v1.2.1-beta.2.0.20220630165224-c591ada0fb2b
Expand Down Expand Up @@ -135,7 +137,6 @@ require (
github.com/prometheus/common v0.37.0 // indirect
github.com/prometheus/procfs v0.8.0 // indirect
github.com/russross/blackfriday/v2 v2.1.0 // indirect
github.com/sirupsen/logrus v1.9.0 // indirect
github.com/stoewer/go-strcase v1.2.0 // indirect
github.com/vishvananda/netns v0.0.0-20210104183010-2eb08e3e575f // indirect
github.com/xlab/treeprint v1.1.0 // indirect
Expand Down
1,016 changes: 983 additions & 33 deletions go.sum

Large diffs are not rendered by default.

66 changes: 63 additions & 3 deletions pkg/clusterlink/controllers/cluster/cluster_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
calicoclient "github.com/projectcalico/calico/libcalico-go/lib/clientv3"
"github.com/projectcalico/calico/libcalico-go/lib/options"
cwatch "github.com/projectcalico/calico/libcalico-go/lib/watch"
"github.com/spf13/cast"
corev1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/api/meta"
Expand Down Expand Up @@ -39,7 +40,9 @@ import (
// KubeFlannelNetworkConfig
const (
FlannelCNI = "flannel"
CiliumCNI = "cilium"
KubeFlannelConfigMap = "kube-flannel-cfg"
KubeCiliumConfigMap = "cilium-config"
KubeFlannelNetworkConf = "net-conf.json"
KubeFlannelIPPool = "Network"
)
Expand Down Expand Up @@ -95,7 +98,6 @@ func (c *Controller) Start(ctx context.Context) error {
informer := factory.Core().V1().Pods().Informer()
c.podLister = factory.Core().V1().Pods().Lister()
podFilterFunc := func(pod *corev1.Pod) bool {
//TODO 确认这个写法是否正确
return pod.Labels["component"] == "kube-apiserver"
}

Expand Down Expand Up @@ -123,7 +125,13 @@ func (c *Controller) Start(ctx context.Context) error {
return err
}

if cluster.Spec.ClusterLinkOptions.CNI == FlannelCNI {
if cluster.Spec.ClusterLinkOptions.CNI == CiliumCNI {
c.setClusterPodCIDRFun, err = c.initCiliumInformer(ctx, cluster, c.kubeClient)
if err != nil {
klog.Errorf("cluster %s initCiliumInformer err: %v", err)
return err
}
} else if cluster.Spec.ClusterLinkOptions.CNI == FlannelCNI {
c.setClusterPodCIDRFun, err = c.initFlannelInformer(ctx, cluster, c.kubeClient)
if err != nil {
klog.Errorf("cluster %s initCalicoInformer err: %v", err)
Expand Down Expand Up @@ -212,7 +220,6 @@ func (c *Controller) Reconcile(key utils.QueueKey) error {
}

func (c *Controller) initCalicoInformer(context context.Context, cluster *clusterlinkv1alpha1.Cluster, dynamicClient dynamic.Interface) (SetClusterPodCIDRFun, error) {
//TODO 这里应该判断cluster的cni插件类型,如果是calico才去观测ippool事件,否则可能都没有ippool这个资源对象,watch一个不存在的资源对象可能会导致这里报错
dynamicInformerFactory := dynamicinformer.NewDynamicSharedInformerFactory(dynamicClient, 0)
gvr := schema.GroupVersionResource{
Group: "crd.projectcalico.org",
Expand Down Expand Up @@ -438,6 +445,59 @@ func (c *Controller) initFlannelInformer(context context.Context, cluster *clust
}, nil
}

func (c *Controller) initCiliumInformer(ctx context.Context, cluster *clusterlinkv1alpha1.Cluster, kubeClient *kubernetes.Clientset) (SetClusterPodCIDRFun, error) {
informerFactory := informers.NewSharedInformerFactory(kubeClient, 0)
lister := informerFactory.Core().V1().ConfigMaps().Lister()
_, err := informerFactory.Core().V1().ConfigMaps().Informer().AddEventHandler(
cache.FilteringResourceEventHandler{
FilterFunc: func(obj interface{}) bool {
cm, ok := obj.(*corev1.ConfigMap)
if !ok {
return false
}
return cm.Name == KubeCiliumConfigMap && cm.Namespace == "kube-system"
},
Handler: cache.ResourceEventHandlerFuncs{
AddFunc: func(obj interface{}) {
c.onChange(cluster)
},
UpdateFunc: func(oldObj, newObj interface{}) {
c.onChange(cluster)
},
DeleteFunc: func(obj interface{}) {
c.onChange(cluster)
},
},
})

if err != nil {
return nil, err
}
informerFactory.Start(ctx.Done())
informerFactory.WaitForCacheSync(ctx.Done())
return func(cluster *clusterlinkv1alpha1.Cluster) error {
cm, err := lister.ConfigMaps("kube-system").Get(KubeCiliumConfigMap)
if err != nil {
return err
}
var podCIDRS []string
ipv4CIDR, ok := cm.Data["cluster-pool-ipv4-cidr"]
if !ok {
klog.Warningf("cluster-pool-ipv4-cidr not found in cilium-config ConfigMap")
}
podCIDRS = append(podCIDRS, cast.ToStringSlice(ipv4CIDR)...)

ipv6CIDR, ok := cm.Data["cluster-pool-ipv6-cidr"]
if !ok {
klog.Warningf("cluster-pool-ipv6-cidr not found in cilium-config ConfigMap")
}
podCIDRS = append(podCIDRS, cast.ToStringSlice(ipv6CIDR)...)

cluster.Status.ClusterLinkStatus.PodCIDRs = podCIDRS
return nil
}, nil
}

func createIppoolWatcher(calicoClient calicoclient.Interface, cluster *clusterlinkv1alpha1.Cluster) (cwatch.Interface, error) {
watch, err := calicoClient.IPPools().Watch(context.TODO(), options.ListOptions{})
if err != nil {
Expand Down
156 changes: 78 additions & 78 deletions vendor/github.com/klauspost/compress/fse/README.md

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

Loading

0 comments on commit 8847a72

Please sign in to comment.