Skip to content

Commit

Permalink
Fix calico plugin support
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielXLee committed Jul 18, 2021
1 parent d9214a5 commit 8b3cdc4
Show file tree
Hide file tree
Showing 38 changed files with 71 additions and 4,058 deletions.
138 changes: 7 additions & 131 deletions controllers/checker/handle_calico.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,69 +20,24 @@ import (
"context"

submarinerv1 "github.com/submariner-io/submariner/pkg/apis/submariner.io/v1"
netconsts "github.com/tkestack/knitnet-operator/controllers/discovery"
"github.com/tkestack/knitnet-operator/controllers/embeddedyamls"
"github.com/tkestack/knitnet-operator/controllers/ensures/broker"
"github.com/tkestack/knitnet-operator/controllers/ensures/common/configmaps"
"github.com/tkestack/knitnet-operator/controllers/ensures/common/daemonsets"
"github.com/tkestack/knitnet-operator/controllers/ensures/common/deployments"
"github.com/tkestack/knitnet-operator/controllers/ensures/common/ippools"
"github.com/tkestack/knitnet-operator/controllers/ensures/common/poddisruptionbudgets"
"github.com/tkestack/knitnet-operator/controllers/ensures/common/serviceaccount"
"github.com/tkestack/knitnet-operator/controllers/utils"
"k8s.io/client-go/rest"
"k8s.io/klog/v2"
"sigs.k8s.io/controller-runtime/pkg/client"
)

var kddCrds = []string{
embeddedyamls.Manifests_fix_calico_crds_crd_projectcalico_org_bgpconfigurations_yaml,
embeddedyamls.Manifests_fix_calico_crds_crd_projectcalico_org_bgppeers_yaml,
embeddedyamls.Manifests_fix_calico_crds_crd_projectcalico_org_blockaffinities_yaml,
embeddedyamls.Manifests_fix_calico_crds_crd_projectcalico_org_clusterinformations_yaml,
embeddedyamls.Manifests_fix_calico_crds_crd_projectcalico_org_felixconfigurations_yaml,
embeddedyamls.Manifests_fix_calico_crds_crd_projectcalico_org_globalnetworkpolicies_yaml,
embeddedyamls.Manifests_fix_calico_crds_crd_projectcalico_org_globalnetworksets_yaml,
embeddedyamls.Manifests_fix_calico_crds_crd_projectcalico_org_hostendpoints_yaml,
embeddedyamls.Manifests_fix_calico_crds_crd_projectcalico_org_ipamblocks_yaml,
embeddedyamls.Manifests_fix_calico_crds_crd_projectcalico_org_ipamconfigs_yaml,
embeddedyamls.Manifests_fix_calico_crds_crd_projectcalico_org_ipamhandles_yaml,
embeddedyamls.Manifests_fix_calico_crds_crd_projectcalico_org_ippools_yaml,
embeddedyamls.Manifests_fix_calico_crds_crd_projectcalico_org_kubecontrollersconfigurations_yaml,
embeddedyamls.Manifests_fix_calico_crds_crd_projectcalico_org_networkpolicies_yaml,
embeddedyamls.Manifests_fix_calico_crds_crd_projectcalico_org_networksets_yaml,
}
netconsts "github.com/tkestack/knitnet-operator/controllers/discovery"
"github.com/tkestack/knitnet-operator/controllers/ensures/broker"
"github.com/tkestack/knitnet-operator/controllers/ensures/common/ippools"
)

func EnsureCalico(c client.Client) error {
if err := CreateOrUpdateKddCRD(c); err != nil {
return err
}
if err := CreateOrUpdateServiceAccount(c); err != nil {
return err
}
if err := CreateOrUpdateClusterRole(c); err != nil {
return err
}
if err := CreateOrUpdateClusterRoleBinding(c); err != nil {
return err
}
if err := CreateOrUpdateDeployment(c); err != nil {
return err
}
if err := CreateOrUpdateDaemonSet(c); err != nil {
return err
}
if err := CreateOrUpdateConfigMap(c); err != nil {
return err
}
if err := CreateOrUpdatePodDisruptionBudget(c); err != nil {
func EnsureCalico(c client.Client, config *rest.Config, currentClusterID string, clusterInfos *[]broker.ClusterInfo) error {
if err := CreateOrUpdateIPPools(c, config, currentClusterID, clusterInfos); err != nil {
return err
}
return nil
}

func CreateOrUpdateIPPools(c client.Client, config *rest.Config, currentClusterID string, clusterInfos *[]broker.ClusterInfo) error {
klog.V(2).Infof("Creating IPPools")
klog.Infof("Creating IPPools")
clusters, err := GetClusters(c)
if err != nil {
return err
Expand Down Expand Up @@ -119,82 +74,3 @@ func GetClusters(c client.Client) (*submarinerv1.ClusterList, error) {
}
return clusters, nil
}

func CreateOrUpdateKddCRD(c client.Client) error {
for _, crd := range kddCrds {
if err := utils.CreateOrUpdateEmbeddedCRD(c, crd); err != nil {
klog.Errorf("Error creating the CRD: %v", err)
return err
}
}
return nil
}

func CreateOrUpdateServiceAccount(c client.Client) error {
if err := serviceaccount.EnsureServiceAccount(c, "kube-system",
embeddedyamls.Manifests_fix_calico_calico_kube_controllers_sa_yaml); err != nil {
return err
}

if err := serviceaccount.EnsureServiceAccount(c, "kube-system",
embeddedyamls.Manifests_fix_calico_calico_node_sa_yaml); err != nil {
return err
}
return nil
}

func CreateOrUpdateClusterRole(c client.Client) error {
if err := serviceaccount.EnsureClusterRole(c,
embeddedyamls.Manifests_fix_calico_calico_kube_controllers_clusterrole_yaml); err != nil {
return err
}
if err := serviceaccount.EnsureClusterRole(c,
embeddedyamls.Manifests_fix_calico_calico_node_clusterrole_yaml); err != nil {
return err
}
return nil
}

func CreateOrUpdateClusterRoleBinding(c client.Client) error {
if err := serviceaccount.EnsureClusterRoleBinding(c, "kube-system",
embeddedyamls.Manifests_fix_calico_calico_kube_controllers_clusterrolebinding_yaml); err != nil {
return err
}
if err := serviceaccount.EnsureClusterRoleBinding(c, "kube-system",
embeddedyamls.Manifests_fix_calico_calico_node_clusterrolebinding_yaml); err != nil {
return err
}
return nil
}

func CreateOrUpdateDeployment(c client.Client) error {
if err := deployments.EnsureDeployment(c, "kube-system",
embeddedyamls.Manifests_fix_calico_calico_kube_controllers_yaml); err != nil {
return err
}
return nil
}

func CreateOrUpdateDaemonSet(c client.Client) error {
if err := daemonsets.EnsureDaemonSet(c, "kube-system",
embeddedyamls.Manifests_fix_calico_calico_node_yaml); err != nil {
return err
}
return nil
}

func CreateOrUpdateConfigMap(c client.Client) error {
if err := configmaps.EnsureConfigMap(c, "kube-system",
embeddedyamls.Manifests_fix_calico_calico_config_yaml); err != nil {
return err
}
return nil
}

func CreateOrUpdatePodDisruptionBudget(c client.Client) error {
if err := poddisruptionbudgets.EnsurePodDisruptionBudget(c, "kube-system",
embeddedyamls.Manifests_fix_calico_calico_kube_controllers_pdb_yaml); err != nil {
return err
}
return nil
}
4 changes: 2 additions & 2 deletions controllers/checker/handle_k8s.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ import (
"github.com/tkestack/knitnet-operator/controllers/utils"
)

func CreateOrUpdateEndpointslicesCRD(c client.Client) error {
if err := utils.CreateOrUpdateEmbeddedCRD(c, embeddedyamls.Manifests_fix_crds_discovery_k8s_io_endpointslices_yaml); err != nil {
func EnsureK8s(c client.Client) error {
if err := utils.CreateOrUpdateEmbeddedCRD(c, embeddedyamls.Manifests_fix_k8s_crds_discovery_k8s_io_endpointslices_yaml); err != nil {
klog.Errorf("Error creating the EndpointSlice CRD: %v", err)
return err
}
Expand Down
2 changes: 1 addition & 1 deletion controllers/deploy_broker.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func (r *KnitnetReconciler) DeploySubmerinerBroker(instance *operatorv1alpha1.Kn
return err
}
if needPatch {
if err := checker.CreateOrUpdateEndpointslicesCRD(r.Client); err != nil {
if err := checker.EnsureK8s(r.Client); err != nil {
return err
}
}
Expand Down
27 changes: 1 addition & 26 deletions controllers/embeddedyamls/generators/yamls2go.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,32 +69,7 @@ var files = []string{
"manifests/config/rbac/networkplugin_syncer/service_account.yaml",
"manifests/config/rbac/networkplugin_syncer/cluster_role.yaml",
"manifests/config/rbac/networkplugin_syncer/cluster_role_binding.yaml",
"manifests/fix/crds/discovery.k8s.io_endpointslices.yaml",
"manifests/fix/calico/calico-config.yaml",
"manifests/fix/calico/calico-kube-controllers-clusterrole.yaml",
"manifests/fix/calico/calico-kube-controllers-clusterrolebinding.yaml",
"manifests/fix/calico/calico-kube-controllers-pdb.yaml",
"manifests/fix/calico/calico-kube-controllers-sa.yaml",
"manifests/fix/calico/calico-kube-controllers.yaml",
"manifests/fix/calico/calico-node-clusterrole.yaml",
"manifests/fix/calico/calico-node-clusterrolebinding.yaml",
"manifests/fix/calico/calico-node-sa.yaml",
"manifests/fix/calico/calico-node.yaml",
"manifests/fix/calico/crds/crd.projectcalico.org_bgpconfigurations.yaml",
"manifests/fix/calico/crds/crd.projectcalico.org_bgppeers.yaml",
"manifests/fix/calico/crds/crd.projectcalico.org_blockaffinities.yaml",
"manifests/fix/calico/crds/crd.projectcalico.org_clusterinformations.yaml",
"manifests/fix/calico/crds/crd.projectcalico.org_felixconfigurations.yaml",
"manifests/fix/calico/crds/crd.projectcalico.org_globalnetworkpolicies.yaml",
"manifests/fix/calico/crds/crd.projectcalico.org_globalnetworksets.yaml",
"manifests/fix/calico/crds/crd.projectcalico.org_hostendpoints.yaml",
"manifests/fix/calico/crds/crd.projectcalico.org_ipamblocks.yaml",
"manifests/fix/calico/crds/crd.projectcalico.org_ipamconfigs.yaml",
"manifests/fix/calico/crds/crd.projectcalico.org_ipamhandles.yaml",
"manifests/fix/calico/crds/crd.projectcalico.org_ippools.yaml",
"manifests/fix/calico/crds/crd.projectcalico.org_kubecontrollersconfigurations.yaml",
"manifests/fix/calico/crds/crd.projectcalico.org_networkpolicies.yaml",
"manifests/fix/calico/crds/crd.projectcalico.org_networksets.yaml",
"manifests/fix/k8s/crds/discovery.k8s.io_endpointslices.yaml",
}

// Reads all .yaml files in the crdDirectory
Expand Down
3 changes: 2 additions & 1 deletion controllers/ensures/common/configmaps/ensure.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,13 @@ package configmaps
import (
"context"

"github.com/tkestack/knitnet-operator/controllers/embeddedyamls"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/klog/v2"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"

"github.com/tkestack/knitnet-operator/controllers/embeddedyamls"
)

func EnsureConfigMap(c client.Client, namespace, yaml string) error {
Expand Down
3 changes: 2 additions & 1 deletion controllers/ensures/common/daemonsets/ensure.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,13 @@ package daemonsets
import (
"context"

"github.com/tkestack/knitnet-operator/controllers/embeddedyamls"
appsv1 "k8s.io/api/apps/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/klog/v2"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"

"github.com/tkestack/knitnet-operator/controllers/embeddedyamls"
)

func EnsureDaemonSet(c client.Client, namespace, yaml string) error {
Expand Down
3 changes: 2 additions & 1 deletion controllers/ensures/common/deployments/ensure.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,13 @@ package deployments
import (
"context"

"github.com/tkestack/knitnet-operator/controllers/embeddedyamls"
appsv1 "k8s.io/api/apps/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/klog/v2"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"

"github.com/tkestack/knitnet-operator/controllers/embeddedyamls"
)

func EnsureDeployment(c client.Client, namespace, yaml string) error {
Expand Down
48 changes: 0 additions & 48 deletions controllers/ensures/common/poddisruptionbudgets/ensure.go

This file was deleted.

38 changes: 18 additions & 20 deletions controllers/join_broker.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ import (
"github.com/tkestack/knitnet-operator/controllers/ensures/operator/servicediscoverycr"
"github.com/tkestack/knitnet-operator/controllers/ensures/operator/submarinercr"
"github.com/tkestack/knitnet-operator/controllers/ensures/operator/submarinerop"
"github.com/tkestack/knitnet-operator/controllers/utils"
"github.com/tkestack/knitnet-operator/controllers/versions"

netconsts "github.com/tkestack/knitnet-operator/controllers/discovery"
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand All @@ -49,6 +49,8 @@ import (
"k8s.io/client-go/util/retry"
"k8s.io/klog/v2"
"sigs.k8s.io/controller-runtime/pkg/client"

netconsts "github.com/tkestack/knitnet-operator/controllers/discovery"
)

var clienttoken *v1.Secret
Expand All @@ -66,7 +68,7 @@ func (r *KnitnetReconciler) JoinSubmarinerCluster(instance *operatorv1alpha1.Kni
return err
}
if needPatch {
if err := checker.CreateOrUpdateEndpointslicesCRD(r.Client); err != nil {
if err := checker.EnsureK8s(r.Client); err != nil {
return err
}
}
Expand All @@ -83,7 +85,7 @@ func (r *KnitnetReconciler) JoinSubmarinerCluster(instance *operatorv1alpha1.Kni
}

if joinConfig.ClusterID == "" {
// TBD auto generate a cluster ID
joinConfig.ClusterID = utils.RandStringRunes(5)
}

if valid, err := isValidClusterID(joinConfig.ClusterID); !valid {
Expand Down Expand Up @@ -132,27 +134,11 @@ func (r *KnitnetReconciler) JoinSubmarinerCluster(instance *operatorv1alpha1.Kni
GlobalnetCIDR: joinConfig.GlobalnetCIDR,
GlobalnetClusterSize: joinConfig.GlobalnetClusterSize,
}
// if brokerInfo.IsGlobalnetEnabled() {

if err = r.AllocateAndUpdateGlobalCIDRConfigMap(brokerCluster.GetClient(), brokerCluster.GetAPIReader(), instance, brokerNamespace, &netconfig); err != nil {
klog.Errorf("Error Discovering multi cluster details: %v", err)
return err
}
// }

//
if networkDetails.NetworkPlugin == netconsts.NetworkPluginCalico {
if err := checker.EnsureCalico(r.Client); err != nil {
return err
}
clusterInfos, err := broker.GetClusterInfos(brokerCluster.GetAPIReader(), brokerNamespace)
if err != nil {
klog.Errorf("Unable to get cluster infos: %v", err)
return err
}
if err := checker.CreateOrUpdateIPPools(r.Client, r.Config, joinConfig.ClusterID, &clusterInfos); err != nil {
return err
}
}

klog.Info("Deploying the Submariner operator")
if err = submarinerop.Ensure(r.Client, r.Config, true); err != nil {
Expand Down Expand Up @@ -188,6 +174,18 @@ func (r *KnitnetReconciler) JoinSubmarinerCluster(instance *operatorv1alpha1.Kni
}
klog.Info("Service discovery is up and running")
}

// Handle calico network plugin case
if networkDetails.NetworkPlugin == netconsts.NetworkPluginCalico {
clusterInfos, err := broker.GetClusterInfos(brokerCluster.GetAPIReader(), brokerNamespace)
if err != nil {
klog.Errorf("Unable to get cluster infos: %v", err)
return err
}
if err := checker.EnsureCalico(r.Client, r.Config, joinConfig.ClusterID, &clusterInfos); err != nil {
return err
}
}
return nil
}

Expand Down
Loading

0 comments on commit 8b3cdc4

Please sign in to comment.