Skip to content

Commit

Permalink
Search pool names naively for the largest numeric suffix, then increm…
Browse files Browse the repository at this point in the history
…ent as a suffix. If none is found then add a new suffix
  • Loading branch information
allanrogerr committed Jan 24, 2024
1 parent 64cb15e commit e4f285f
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 6 deletions.
18 changes: 16 additions & 2 deletions kubectl-minio/cmd/resources/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"io/fs"
"log"
"path"
"strconv"
"strings"

"sigs.k8s.io/kustomize/kyaml/filesys"
Expand Down Expand Up @@ -94,7 +95,7 @@ func Pool(opts *TenantOptions, volumes int32, q resource.Quantity) miniov2.Pool
{
Key: miniov2.PoolLabel,
Operator: "In",
Values: []string{opts.Name},
Values: []string{opts.PoolName},
},
},
},
Expand All @@ -108,7 +109,20 @@ func Pool(opts *TenantOptions, volumes int32, q resource.Quantity) miniov2.Pool
}

// GeneratePoolName Pool Name Generator
func GeneratePoolName(poolNumber int) string {
func GeneratePoolName(pools []miniov2.Pool) string {
// Trivial case
poolNumber := len(pools)
if poolNumber == 0 {
return fmt.Sprintf("pool-%d", poolNumber)
}
// Search pool names for the largest numeric suffix, then increment as a suffix
// If none is found then add a new suffix
for _, pool := range pools {
suffix := pool.Name[strings.LastIndex(pool.Name, "-")+1:]
if poolNumberCurrent, err := strconv.Atoi(suffix); err == nil && poolNumberCurrent > poolNumber {
poolNumber = poolNumberCurrent
}
}
return fmt.Sprintf("pool-%d", poolNumber)
}

Expand Down
1 change: 1 addition & 0 deletions kubectl-minio/cmd/resources/tenant.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package resources

import (
"errors"

"github.com/minio/kubectl-minio/cmd/helpers"
operator "github.com/minio/operator/pkg/apis/minio.min.io"
miniov2 "github.com/minio/operator/pkg/apis/minio.min.io/v2"
Expand Down
8 changes: 5 additions & 3 deletions kubectl-minio/cmd/tenant-expand.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,11 @@ import (
"encoding/json"
"errors"
"fmt"
"io"

"github.com/minio/kubectl-minio/cmd/helpers"
"github.com/minio/kubectl-minio/cmd/resources"
"io"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"k8s.io/klog/v2"
Expand Down Expand Up @@ -135,9 +137,9 @@ func (v *expandCmd) run() error {
return err
}

// Tenant pool id is zero based, generating pool using the count of existing pools in the tenant
// Generate pool name using the state of existing pools in the tenant
if v.tenantOpts.PoolName == "" {
v.tenantOpts.PoolName = resources.GeneratePoolName(len(t.Spec.Pools))
v.tenantOpts.PoolName = resources.GeneratePoolName(t.Spec.Pools)
}

t.Spec.Pools = append(t.Spec.Pools, resources.Pool(&v.tenantOpts, v.tenantOpts.VolumesPerServer, *capacityPerVolume))
Expand Down
16 changes: 15 additions & 1 deletion web-app/src/common/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -453,7 +453,21 @@ export const erasureCodeCalc = (

// Pool Name Generator
export const generatePoolName = (pools: Pool[]) => {
const poolCounter = pools.length;
// Trivial case
let poolCounter = pools.length;
if (pools.length === 0) {
return `pool-${poolCounter}`;
}
// Search pool names for the largest numeric suffix, then increment as a suffix
// If none is found then add a new suffix
const poolNames = pools.map((pool) => pool.name || "");
// If the suffix is a number, then parse as an integer, else this is also a trivial case
const poolSuffixes = poolNames.map((poolName) =>
!isNaN(Number(poolName.slice(poolName.lastIndexOf("-") + 1)))
? parseInt(poolName.slice(poolName.lastIndexOf("-") + 1), 10)
: -1,
);
poolCounter = Math.max(...poolSuffixes) + 1;
return `pool-${poolCounter}`;
};

Expand Down

0 comments on commit e4f285f

Please sign in to comment.