Skip to content

Commit

Permalink
Use refactored cidr module code in globalnet
Browse files Browse the repository at this point in the history
Signed-off-by: Tom Pantelis <[email protected]>
  • Loading branch information
tpantelis authored and aswinsuryan committed Sep 9, 2024
1 parent 6e6b5c6 commit ee76b52
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 568 deletions.
4 changes: 2 additions & 2 deletions controllers/submariner/broker_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@ var _ = Describe("Broker controller tests", func() {

globalnetInfo, _, err := globalnet.GetGlobalNetworks(ctx, t.ScopedClient, submarinerNamespace)
Expect(err).To(Succeed())
Expect(globalnetInfo.CidrRange).To(Equal(broker.Spec.GlobalnetCIDRRange))
Expect(globalnetInfo.ClusterSize).To(Equal(broker.Spec.DefaultGlobalnetClusterSize))
Expect(globalnetInfo.CIDR).To(Equal(broker.Spec.GlobalnetCIDRRange))
Expect(globalnetInfo.AllocationSize).To(Equal(broker.Spec.DefaultGlobalnetClusterSize))
})

It("should create the CRDs", func(ctx SpecContext) {
Expand Down
15 changes: 4 additions & 11 deletions pkg/cidr/cidr.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,22 +77,15 @@ func ExtractClusterInfo(fromConfigMap *corev1.ConfigMap) (map[string]*ClusterInf
}

func AddClusterInfoData(toConfigMap *corev1.ConfigMap, newCluster ClusterInfo) error {
var existingInfo []ClusterInfo
existingInfo, err := unmarshalClusterInfo(toConfigMap)
if err != nil {
return err
}

if toConfigMap.Data == nil {
toConfigMap.Data = map[string]string{}
}

existingData := toConfigMap.Data[ClusterInfoKey]
if existingData == "" {
existingData = "[]"
}

err := json.Unmarshal([]byte(existingData), &existingInfo)
if err != nil {
return errors.Wrapf(err, "error unmarshalling ClusterInfo")
}

exists := false

for k, value := range existingInfo {
Expand Down
36 changes: 4 additions & 32 deletions pkg/discovery/globalnet/config_map.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"fmt"

"github.com/pkg/errors"
"github.com/submariner-io/submariner-operator/pkg/cidr"
corev1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand All @@ -34,18 +35,12 @@ import (
const (
globalCIDRConfigMapName = "submariner-globalnet-info"
globalnetEnabledKey = "globalnetEnabled"
clusterInfoKey = "clusterinfo"
globalnetCidrRange = "globalnetCidrRange"
globalnetClusterSize = "globalnetClusterSize"
DefaultGlobalnetCIDR = "242.0.0.0/8"
DefaultGlobalnetClusterSize = 65536 // i.e., x.x.x.x/16 subnet mask
)

type clusterInfo struct {
ClusterID string `json:"cluster_id"`
GlobalCidr []string `json:"global_cidr"`
}

func CreateConfigMap(ctx context.Context, client controllerClient.Client, globalnetEnabled bool, defaultGlobalCidrRange string,
defaultGlobalClusterSize uint, namespace string,
) error {
Expand Down Expand Up @@ -80,12 +75,10 @@ func NewGlobalnetConfigMap(globalnetEnabled bool, defaultGlobalCidrRange string,
globalnetEnabledKey: "true",
globalnetCidrRange: string(cidrRange),
globalnetClusterSize: fmt.Sprint(defaultGlobalClusterSize),
clusterInfoKey: "[]",
}
} else {
data = map[string]string{
globalnetEnabledKey: "false",
clusterInfoKey: "[]",
}
}

Expand All @@ -101,34 +94,13 @@ func NewGlobalnetConfigMap(globalnetEnabled bool, defaultGlobalCidrRange string,
return cm, nil
}

func updateConfigMap(ctx context.Context, client controllerClient.Client, configMap *corev1.ConfigMap, newCluster clusterInfo,
func updateConfigMap(ctx context.Context, client controllerClient.Client, configMap *corev1.ConfigMap, newCluster cidr.ClusterInfo,
) error {
var existingInfo []clusterInfo

err := json.Unmarshal([]byte(configMap.Data[clusterInfoKey]), &existingInfo)
if err != nil {
return errors.Wrapf(err, "error unmarshalling ClusterInfo")
}

exists := false

for k, value := range existingInfo {
if value.ClusterID == newCluster.ClusterID {
existingInfo[k].GlobalCidr = newCluster.GlobalCidr
exists = true
}
}

if !exists {
existingInfo = append(existingInfo, newCluster)
}

data, err := json.MarshalIndent(existingInfo, "", "\t")
err := cidr.AddClusterInfoData(configMap, newCluster)
if err != nil {
return errors.Wrapf(err, "error marshalling ClusterInfo")
return errors.Wrapf(err, "error adding ClusterInfo")
}

configMap.Data[clusterInfoKey] = string(data)
err = client.Update(ctx, configMap)

return errors.Wrapf(err, "error updating ConfigMap")
Expand Down
Loading

0 comments on commit ee76b52

Please sign in to comment.