Skip to content

Commit

Permalink
Add clustersetip module to discovery
Browse files Browse the repository at this point in the history
...to allow subctl to allocate clustersetIP cidrs during join

Signed-off-by: Vishal Thapar <[email protected]>
  • Loading branch information
vthapar authored and tpantelis committed Sep 20, 2024
1 parent a13a7e0 commit e2de28e
Show file tree
Hide file tree
Showing 6 changed files with 552 additions and 38 deletions.
46 changes: 46 additions & 0 deletions pkg/cidr/cidr.go
Original file line number Diff line number Diff line change
Expand Up @@ -272,3 +272,49 @@ func uintToIP(ip uint32) net.IP {

return netIP
}

func GetValidAllocationSize(cidrRange string, allocationSize uint) (uint, error) {
_, network, err := net.ParseCIDR(cidrRange)
if err != nil {
return 0, err //nolint:wrapcheck // No need to wrap here
}

ones, totalbits := network.Mask.Size()
availableSize := uint(1) << uint(totalbits-ones) //nolint:gosec // Ignore overflow conversion int -> uint
userClusterSize := allocationSize
allocationSize = nextPowerOf2(allocationSize)

if allocationSize > (availableSize / 2) {
return 0, fmt.Errorf("cluster size %d, should be <= %d", userClusterSize, availableSize/2)
}

if allocationSize == 0 {
return 0, errors.New("cluster size must be > 0")
}

return allocationSize, nil
}

// Refer: https://graphics.stanford.edu/~seander/bithacks.html#RoundUpPowerOf2
func nextPowerOf2(n uint) uint {
n--
n |= n >> 1
n |= n >> 2
n |= n >> 4
n |= n >> 8
n |= n >> 16
n++

return n
}

func IsCIDRPreConfigured(clusterID string, clustersetIPNetworks map[string]*ClusterInfo) bool {
// ClustersetIPCIDR is not pre-configured
if clustersetIPNetworks[clusterID] == nil || clustersetIPNetworks[clusterID].CIDRs == nil ||
len(clustersetIPNetworks[clusterID].CIDRs) == 0 {
return false
}

// ClustersetIPCIDR is pre-configured
return true
}
223 changes: 223 additions & 0 deletions pkg/discovery/clustersetip/clustersetip.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,223 @@
/*
SPDX-License-Identifier: Apache-2.0
Copyright Contributors to the Submariner project.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package clustersetip

import (
"context"
"encoding/json"
"fmt"

"github.com/pkg/errors"
"github.com/submariner-io/admiral/pkg/reporter"
"github.com/submariner-io/submariner-operator/pkg/cidr"
v1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/client-go/util/retry"
controllerClient "sigs.k8s.io/controller-runtime/pkg/client"
)

type Info struct {
Enabled bool
cidr.Info
}

type Config struct {
ClusterID string
ClustersetIPCIDR string
AllocationSize uint
}

func AllocateClustersetIPCIDR(clustersetIPInfo *Info) (string, error) {
return cidr.Allocate(&clustersetIPInfo.Info) //nolint:wrapcheck // No need to wrap
}

func ValidateClustersetIPConfiguration(clustersetIPInfo *Info, netconfig Config, status reporter.Interface) (string, error) {
status.Start("Validating ClustersetIP configuration")
defer status.End()

clustersetIPClusterSize := netconfig.AllocationSize
clustersetIPCIDR := netconfig.ClustersetIPCIDR

if clustersetIPClusterSize != 0 && clustersetIPClusterSize != clustersetIPInfo.AllocationSize {
clusterSize, err := cidr.GetValidAllocationSize(clustersetIPInfo.CIDR, clustersetIPClusterSize)
if err != nil {
return "", status.Error(err, "invalid cluster size")
}

clustersetIPInfo.AllocationSize = clusterSize
}

if clustersetIPCIDR != "" && clustersetIPClusterSize != 0 {
status.Failure("Only one of cluster size and clustersetip CIDR can be specified")

return "", errors.New("only one of cluster size and clustersetip CIDR can be specified")
}

if clustersetIPCIDR != "" {
err := cidr.IsValid(clustersetIPCIDR)
if err != nil {
return "", errors.Wrap(err, "specified clustersetip-cidr is invalid")
}
}

return clustersetIPCIDR, nil
}

func GetClustersetIPNetworks(ctx context.Context, client controllerClient.Client, brokerNamespace string) (*Info, *v1.ConfigMap, error) {
configMap, err := GetConfigMap(ctx, client, brokerNamespace)
if err != nil {
return nil, nil, errors.Wrap(err, "error retrieving clustersetip ConfigMap")
}

clustersetIPInfo := Info{}

err = json.Unmarshal([]byte(configMap.Data[clustersetIPEnabledKey]), &clustersetIPInfo.Enabled)
if err != nil {
return nil, nil, errors.Wrap(err, "error reading clusersetIPEnabled status")
}

err = json.Unmarshal([]byte(configMap.Data[clustersetIPClusterSize]), &clustersetIPInfo.AllocationSize)
if err != nil {
return nil, nil, errors.Wrap(err, "error reading ClustersetIPClusterSize")
}

err = json.Unmarshal([]byte(configMap.Data[clustersetIPCidrRange]), &clustersetIPInfo.CIDR)
if err != nil {
return nil, nil, errors.Wrap(err, "error reading ClustersetIPCidrRange")
}

clustersetIPInfo.Clusters, err = cidr.ExtractClusterInfo(configMap)

return &clustersetIPInfo, configMap, err //nolint:wrapcheck // No need to wrap
}

func assignClustersetIPs(clustersetIPInfo *Info, netconfig Config, status reporter.Interface) (string, error) {
status.Start("Assigning ClustersetIP IPs")
defer status.End()

clustersetIPCIDR := netconfig.ClustersetIPCIDR
clusterID := netconfig.ClusterID
var err error

if clustersetIPCIDR == "" {
// ClustersetIPCIDR not specified by the user
if cidr.IsCIDRPreConfigured(clusterID, clustersetIPInfo.Clusters) {
// clustersetipCidr already configured on this cluster
clustersetIPCIDR = clustersetIPInfo.Clusters[clusterID].CIDRs[0]
status.Success("Using pre-configured clustersetip CIDR %s", clustersetIPCIDR)
} else {
// no clustersetipCidr configured on this cluster
clustersetIPCIDR, err = AllocateClustersetIPCIDR(clustersetIPInfo)
if err != nil {
return "", status.Error(err, "unable to allocate clustersetip CIDR")
}

status.Success(fmt.Sprintf("Allocated clustersetip CIDR %s", clustersetIPCIDR))
}
} else {
// ClustersetIP enabled, clustersetIPCIDR specified by user
if cidr.IsCIDRPreConfigured(clusterID, clustersetIPInfo.Clusters) {
// clustersetipCidr pre-configured on this cluster
clustersetIPCIDR = clustersetIPInfo.Clusters[clusterID].CIDRs[0]
status.Warning("A pre-configured clustersetip CIDR %s was detected - not using the specified CIDR %s",
clustersetIPCIDR, netconfig.ClustersetIPCIDR)
} else {
// clustersetipCidr as specified by the user
err := cidr.CheckForOverlappingCIDRs(clustersetIPInfo.Clusters, netconfig.ClustersetIPCIDR, netconfig.ClusterID)
if err != nil {
return "", status.Error(err, "error validating overlapping clustersetip CIDRs %s", clustersetIPCIDR)
}

status.Success("Using specified clustersetip CIDR %s", clustersetIPCIDR)
}
}

return clustersetIPCIDR, nil
}

func ValidateExistingClustersetIPNetworks(ctx context.Context, client controllerClient.Client, namespace string) error {
clustersetIPInfo, _, err := GetClustersetIPNetworks(ctx, client, namespace)
if err != nil {
if apierrors.IsNotFound(err) {
return nil
}

return errors.Wrap(err, "error getting existing clustersetip configmap")
}

if clustersetIPInfo != nil {
if err = cidr.IsValid(clustersetIPInfo.CIDR); err != nil {
return errors.Wrap(err, "invalid ClustersetIPCidrRange")
}
}

return nil
}

func AllocateCIDRFromConfigMap(ctx context.Context, brokerAdminClient controllerClient.Client, brokerNamespace string,
netconfig *Config, status reporter.Interface,
) error {
// Setup default clustersize if nothing specified
if netconfig.ClustersetIPCIDR == "" && netconfig.AllocationSize == 0 {
netconfig.AllocationSize = DefaultAllocationSize
}

retryErr := retry.RetryOnConflict(retry.DefaultRetry, func() error {
status.Start("Retrieving ClustersetIP information from the Broker")
defer status.End()

clustersetIPInfo, clustersetIPConfigMap, err := GetClustersetIPNetworks(ctx, brokerAdminClient, brokerNamespace)
if err != nil {
return status.Error(err, "unable to retrieve ClustersetIP information")
}

netconfig.ClustersetIPCIDR, err = ValidateClustersetIPConfiguration(clustersetIPInfo, *netconfig, status)
if err != nil {
return status.Error(err, "error validating the ClustersetIP configuration")
}

netconfig.ClustersetIPCIDR, err = assignClustersetIPs(clustersetIPInfo, *netconfig, status)
if err != nil {
return status.Error(err, "error assigning ClustersetIP IPs")
}

if clustersetIPInfo.Clusters[netconfig.ClusterID] == nil ||
clustersetIPInfo.Clusters[netconfig.ClusterID].CIDRs[0] != netconfig.ClustersetIPCIDR {
newClusterInfo := cidr.ClusterInfo{
ClusterID: netconfig.ClusterID,
CIDRs: []string{netconfig.ClustersetIPCIDR},
}

status.Start("Updating the ClustersetIP information on the Broker")

err = updateConfigMap(ctx, brokerAdminClient, clustersetIPConfigMap, newClusterInfo)
if apierrors.IsConflict(err) {
status.Warning("Conflict occurred updating the ClustersetIP ConfigMap - retrying")
} else {
return status.Error(err, "error updating the ClustersetIP ConfigMap")
}

return err
}

return nil
})

return retryErr //nolint:wrapcheck // No need to wrap here
}
30 changes: 30 additions & 0 deletions pkg/discovery/clustersetip/clustersetip_suite_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
SPDX-License-Identifier: Apache-2.0
Copyright Contributors to the Submariner project.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package clustersetip_test

import (
"testing"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)

func TestClsutersetIP(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "ClustersetIP Suite")
}
Loading

0 comments on commit e2de28e

Please sign in to comment.