From 8734a21543dfb915ee810d9463ca013acc0b8ca0 Mon Sep 17 00:00:00 2001 From: Xin Li Date: Thu, 15 Jul 2021 16:21:15 +0800 Subject: [PATCH] Sync broker info configmap --- README.md | 3 +- api/v1alpha1/knitnet_types.go | 2 +- .../bases/operator.tkestack.io_knitnets.yaml | 2 +- controllers/deploy_broker.go | 12 +++---- controllers/ensures/broker/brokerinfo_cm.go | 19 +++++++--- controllers/join_broker.go | 36 ++++++++++++++++++- controllers/knitnet_controller.go | 7 +--- 7 files changed, 60 insertions(+), 21 deletions(-) diff --git a/README.md b/README.md index 1f3bc47..b79bd97 100644 --- a/README.md +++ b/README.md @@ -104,7 +104,7 @@ The setup can be done by using `kustomize`. kubectl -n knitnet-operator-system get cm submariner-broker-info -oyaml > submariner-broker-info.yaml ``` -2. Join cluster to broker +1. Join cluster to broker - Install knitnet operator @@ -123,6 +123,7 @@ The setup can be done by using `kustomize`. - Create `submariner-broker-info` configmap ```shell + kubectl create ns submariner-k8s-broker kubectl apply -f submariner-broker-info.yaml ``` diff --git a/api/v1alpha1/knitnet_types.go b/api/v1alpha1/knitnet_types.go index 37d2825..ba5d7c9 100644 --- a/api/v1alpha1/knitnet_types.go +++ b/api/v1alpha1/knitnet_types.go @@ -135,7 +135,7 @@ type JoinConfig struct { NatTraversal bool `json:"natTraversal,omitempty"` // GlobalnetEnabled represents enable/disable Globalnet for this cluster. // +optional - // +kubebuilder:default=true + // +kubebuilder:default=false GlobalnetEnabled bool `json:"globalnetEnabled,omitempty"` // IpsecDebug represents enable/disable IPsec debugging (verbose logging). // +optional diff --git a/config/crd/bases/operator.tkestack.io_knitnets.yaml b/config/crd/bases/operator.tkestack.io_knitnets.yaml index 0274c90..b7052a1 100644 --- a/config/crd/bases/operator.tkestack.io_knitnets.yaml +++ b/config/crd/bases/operator.tkestack.io_knitnets.yaml @@ -177,7 +177,7 @@ spec: GlobalCIDR allocated to this cluster (amount of global IPs). type: integer globalnetEnabled: - default: true + default: false description: GlobalnetEnabled represents enable/disable Globalnet for this cluster. type: boolean diff --git a/controllers/deploy_broker.go b/controllers/deploy_broker.go index 7542ada..be10186 100644 --- a/controllers/deploy_broker.go +++ b/controllers/deploy_broker.go @@ -63,6 +63,12 @@ func (r *KnitnetReconciler) DeploySubmerinerBroker(instance *operatorv1alpha1.Kn return err } + if err := broker.CreateGlobalnetConfigMap(r.Client, brokerConfig.GlobalnetEnable, brokerConfig.GlobalnetCIDRRange, + brokerConfig.DefaultGlobalnetClusterSize, consts.SubmarinerBrokerNamespace); err != nil { + klog.Errorf("Error creating globalCIDR configmap on Broker: %v", err) + return err + } + if brokerConfig.GlobalnetEnable { if err := globalnet.ValidateExistingGlobalNetworks(r.Reader, consts.SubmarinerBrokerNamespace); err != nil { klog.Errorf("Error validating existing globalCIDR configmap: %v", err) @@ -70,12 +76,6 @@ func (r *KnitnetReconciler) DeploySubmerinerBroker(instance *operatorv1alpha1.Kn } } - if err := broker.CreateGlobalnetConfigMap(r.Client, brokerConfig.GlobalnetEnable, brokerConfig.GlobalnetCIDRRange, - brokerConfig.DefaultGlobalnetClusterSize, consts.SubmarinerBrokerNamespace); err != nil { - klog.Errorf("Error creating globalCIDR configmap on Broker: %v", err) - return err - } - if err := broker.CreateBrokerInfoConfigMap(r.Client, r.Config, instance); err != nil { klog.Errorf("Error writing the broker information: %v", err) return err diff --git a/controllers/ensures/broker/brokerinfo_cm.go b/controllers/ensures/broker/brokerinfo_cm.go index 178f339..eb41a64 100644 --- a/controllers/ensures/broker/brokerinfo_cm.go +++ b/controllers/ensures/broker/brokerinfo_cm.go @@ -96,7 +96,7 @@ func (data *BrokerInfo) WriteConfigMap(c client.Client, instance *operatorv1alph cm := &v1.ConfigMap{ ObjectMeta: metav1.ObjectMeta{ Name: consts.SubmarinerBrokerInfo, - Namespace: consts.KnitnetOperatorNamespace, + Namespace: consts.SubmarinerBrokerNamespace, }, } labels := make(map[string]string) @@ -120,10 +120,8 @@ func (data *BrokerInfo) WriteConfigMap(c client.Client, instance *operatorv1alph } func NewFromConfigMap(c client.Client) (*BrokerInfo, error) { - cm := &v1.ConfigMap{} - cmKey := types.NamespacedName{Name: consts.SubmarinerBrokerInfo, Namespace: consts.KnitnetOperatorNamespace} - if err := c.Get(context.TODO(), cmKey, cm); err != nil { - klog.Errorf("Get submariner-broker-info configmap failed: %v", err) + cm, err := GetBrokerInfoConfigMap(c) + if err != nil { return nil, err } return NewFromString(cm.Data["brokerInfo"]) @@ -178,6 +176,17 @@ func CreateBrokerInfoConfigMap(c client.Client, restConfig *rest.Config, instanc return nil } +func GetBrokerInfoConfigMap(reader client.Reader) (*v1.ConfigMap, error) { + klog.Info("Get broker info configmap") + cm := &v1.ConfigMap{} + cmKey := types.NamespacedName{Name: consts.SubmarinerBrokerInfo, Namespace: consts.SubmarinerBrokerNamespace} + if err := reader.Get(context.TODO(), cmKey, cm); err != nil { + klog.Errorf("Get submariner-broker-info configmap failed: %v", err) + return nil, err + } + return cm, nil +} + func (data *BrokerInfo) GetBrokerAdministratorCluster() (cluster.Cluster, error) { config := data.GetBrokerAdministratorConfig() scheme := runtime.NewScheme() diff --git a/controllers/join_broker.go b/controllers/join_broker.go index af2adb8..f1774f8 100644 --- a/controllers/join_broker.go +++ b/controllers/join_broker.go @@ -59,7 +59,11 @@ var nodeLabelBackoff wait.Backoff = wait.Backoff{ Jitter: 1, } -func (r *KnitnetReconciler) JoinSubmarinerCluster(instance *operatorv1alpha1.Knitnet, brokerInfo *broker.BrokerInfo) error { +func (r *KnitnetReconciler) JoinSubmarinerCluster(instance *operatorv1alpha1.Knitnet) error { + brokerInfo, err := SyncBrokerInfo(r.Client, r.Reader) + if err != nil { + return err + } joinConfig := instance.Spec.JoinConfig if err := isValidCustomCoreDNSConfig(instance); err != nil { @@ -220,6 +224,36 @@ func (r *KnitnetReconciler) AllocateAndUpdateGlobalCIDRConfigMap(c client.Client return retryErr } +func SyncBrokerInfo(c client.Client, reader client.Reader) (*broker.BrokerInfo, error) { + localConfigmap, err := broker.GetBrokerInfoConfigMap(reader) + if err != nil { + return nil, err + } + brokerInfo, err := broker.NewFromString(localConfigmap.Data["brokerInfo"]) + if err != nil { + return nil, err + } + brokerCluster, err := brokerInfo.GetBrokerAdministratorCluster() + if err != nil { + return nil, err + } + brokerClusterConfigmap, err := broker.GetBrokerInfoConfigMap(brokerCluster.GetAPIReader()) + if err != nil { + return nil, err + } + + if localConfigmap.Data["brokerInfo"] != brokerClusterConfigmap.Data["brokerInfo"] { + localConfigmap.Data["brokerInfo"] = brokerClusterConfigmap.Data["brokerInfo"] + if err := c.Update(context.TODO(), localConfigmap); err != nil { + klog.Errorf("Update local broker info configmap failed: %v", err) + return nil, err + } + return broker.NewFromString(localConfigmap.Data["brokerInfo"]) + } + + return brokerInfo, nil +} + func (r *KnitnetReconciler) GetNetworkDetails() (*network.ClusterNetwork, error) { dynClient, err := dynamic.NewForConfig(r.Config) if err != nil { diff --git a/controllers/knitnet_controller.go b/controllers/knitnet_controller.go index d19c4ef..fb48d1f 100644 --- a/controllers/knitnet_controller.go +++ b/controllers/knitnet_controller.go @@ -37,7 +37,6 @@ import ( operatorv1alpha1 "github.com/tkestack/knitnet-operator/api/v1alpha1" consts "github.com/tkestack/knitnet-operator/controllers/ensures" - "github.com/tkestack/knitnet-operator/controllers/ensures/broker" ) // KnitnetReconciler reconciles a Knitnet object @@ -122,11 +121,7 @@ func (r *KnitnetReconciler) Reconcile(ctx context.Context, req ctrl.Request) (_ // Join managed cluster to submeriner borker if instance.Spec.Action == JoinAction || instance.Spec.Action == AllAction { klog.Info("Join managed cluster to submeriner broker") - brokerInfo, err := broker.NewFromConfigMap(r.Client) - if err != nil { - return ctrl.Result{}, err - } - if err := r.JoinSubmarinerCluster(instance, brokerInfo); err != nil { + if err := r.JoinSubmarinerCluster(instance); err != nil { return ctrl.Result{}, err } }