Skip to content

Commit

Permalink
Sync broker info configmap
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielXLee committed Jul 15, 2021
1 parent 76c4057 commit 8734a21
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 21 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
```

Expand Down
2 changes: 1 addition & 1 deletion api/v1alpha1/knitnet_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion config/crd/bases/operator.tkestack.io_knitnets.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 6 additions & 6 deletions controllers/deploy_broker.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,19 +63,19 @@ 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)
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 err := broker.CreateBrokerInfoConfigMap(r.Client, r.Config, instance); err != nil {
klog.Errorf("Error writing the broker information: %v", err)
return err
Expand Down
19 changes: 14 additions & 5 deletions controllers/ensures/broker/brokerinfo_cm.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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"])
Expand Down Expand Up @@ -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()
Expand Down
36 changes: 35 additions & 1 deletion controllers/join_broker.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down
7 changes: 1 addition & 6 deletions controllers/knitnet_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}
}
Expand Down

0 comments on commit 8734a21

Please sign in to comment.