Skip to content

Commit 7aadafa

Browse files
authored
gcnv san: fix for zonal pools and host group handling (#2485)
1 parent a5a3d2e commit 7aadafa

File tree

3 files changed

+68
-11
lines changed

3 files changed

+68
-11
lines changed

storage_drivers/gcp/api/gcnv.go

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ var (
5555
volumeNameRegex = regexp.MustCompile(`^projects/(?P<projectNumber>[^/]+)/locations/(?P<location>[^/]+)/volumes/(?P<volume>[^/]+)$`)
5656
snapshotNameRegex = regexp.MustCompile(`^projects/(?P<projectNumber>[^/]+)/locations/(?P<location>[^/]+)/volumes/(?P<volume>[^/]+)/snapshots/(?P<snapshot>[^/]+)$`)
5757
networkNameRegex = regexp.MustCompile(`^projects/(?P<projectNumber>[^/]+)/global/networks/(?P<network>[^/]+)$`)
58+
zoneSuffixRegex = regexp.MustCompile(`-[a-z]$`)
5859
)
5960

6061
// ClientConfig holds configuration data for the API driver object.
@@ -368,14 +369,16 @@ func (c Client) findAllLocationsFromCapacityPool(flexPoolsCount int) map[string]
368369
return locations
369370
}
370371

371-
// getLocationFromCapacityPools returns the location from the first available capacity pool.
372-
// Returns an error if no capacity pools are available.
373-
func (c Client) getLocationFromCapacityPools() (string, error) {
374-
pools := c.CapacityPools()
375-
if pools == nil || len(*pools) == 0 {
376-
return "", fmt.Errorf("no capacity pools available")
372+
// getRegionalLocation returns the regional location from the backend config.
373+
// Host groups are regional resources and must use a regional location (e.g., "us-east4"), even when
374+
// the backend config specifies a zonal location. The zone suffix is stripped if present.
375+
// Zonal locations like "us-east4-a" are trimmed to "us-east4".
376+
// Regional locations like "us-east4" are returned as-is.
377+
func (c Client) getRegionalLocation() (string, error) {
378+
if c.config.Location == "" {
379+
return "", fmt.Errorf("backend config location is not set")
377380
}
378-
return (*pools)[0].Location, nil
381+
return zoneSuffixRegex.ReplaceAllString(c.config.Location, ""), nil
379382
}
380383

381384
// ///////////////////////////////////////////////////////////////////////////////
@@ -1878,9 +1881,9 @@ func (c Client) HostGroups(ctx context.Context) ([]*HostGroup, error) {
18781881

18791882
Logc(ctx).WithFields(logFields).Debug("Listing host groups via v1 protobuf SDK.")
18801883

1881-
location, err := c.getLocationFromCapacityPools()
1884+
location, err := c.getRegionalLocation()
18821885
if err != nil {
1883-
return []*HostGroup{}, nil
1886+
return nil, err
18841887
}
18851888

18861889
sdkCtx, cancel := context.WithTimeout(ctx, c.config.SDKTimeout)
@@ -1917,7 +1920,7 @@ func (c Client) HostGroupByName(ctx context.Context, name string) (*HostGroup, e
19171920

19181921
Logc(ctx).WithFields(logFields).Debug("Fetching host group by name via v1 protobuf SDK.")
19191922

1920-
location, err := c.getLocationFromCapacityPools()
1923+
location, err := c.getRegionalLocation()
19211924
if err != nil {
19221925
return nil, err
19231926
}
@@ -1955,7 +1958,7 @@ func (c Client) CreateHostGroup(ctx context.Context, request *HostGroupCreateReq
19551958

19561959
Logc(ctx).WithFields(logFields).Debug("Creating host group via v1 protobuf SDK.")
19571960

1958-
location, err := c.getLocationFromCapacityPools()
1961+
location, err := c.getRegionalLocation()
19591962
if err != nil {
19601963
return nil, err
19611964
}

storage_drivers/gcp/api/gcnv_test.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1943,3 +1943,41 @@ func TestListComputeZones_Success(t *testing.T) {
19431943
// making it difficult to mock without a full gRPC mock setup.
19441944
t.Skip("ListComputeZones requires compute client mocking which is complex")
19451945
}
1946+
1947+
func TestGetRegionalLocation(t *testing.T) {
1948+
t.Run("regional config location returned as-is", func(t *testing.T) {
1949+
sdk := getFakeSDK(false)
1950+
sdk.config.Location = "us-east4"
1951+
1952+
location, err := sdk.getRegionalLocation()
1953+
assert.NoError(t, err)
1954+
assert.Equal(t, "us-east4", location)
1955+
})
1956+
1957+
t.Run("zonal config location stripped to region", func(t *testing.T) {
1958+
sdk := getFakeSDK(false)
1959+
sdk.config.Location = "us-east4-a"
1960+
1961+
location, err := sdk.getRegionalLocation()
1962+
assert.NoError(t, err)
1963+
assert.Equal(t, "us-east4", location)
1964+
})
1965+
1966+
t.Run("different region zonal stripped", func(t *testing.T) {
1967+
sdk := getFakeSDK(false)
1968+
sdk.config.Location = "europe-west1-b"
1969+
1970+
location, err := sdk.getRegionalLocation()
1971+
assert.NoError(t, err)
1972+
assert.Equal(t, "europe-west1", location)
1973+
})
1974+
1975+
t.Run("empty location returns error", func(t *testing.T) {
1976+
sdk := getFakeSDK(false)
1977+
sdk.config.Location = ""
1978+
1979+
_, err := sdk.getRegionalLocation()
1980+
assert.Error(t, err)
1981+
assert.Contains(t, err.Error(), "backend config location is not set")
1982+
})
1983+
}

storage_drivers/gcp/gcp_gcnv_san_test.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2914,6 +2914,22 @@ func TestSANDriver_ReconcileNodeAccess_KeepOrphanedHostGroupsWithVolumes(t *test
29142914
assert.NoError(t, err, "ReconcileNodeAccess should succeed")
29152915
}
29162916

2917+
func TestSANDriver_ReconcileNodeAccess_HostGroupsError(t *testing.T) {
2918+
mockAPI, driver := newMockSANDriver(t)
2919+
2920+
nodes := []*models.Node{
2921+
{Name: "node-1", IQN: "iqn.xxx.node-1"},
2922+
}
2923+
2924+
// Mock expectations - HostGroups returns error (e.g., location not configured)
2925+
mockAPI.EXPECT().RefreshGCNVResources(ctx).Return(nil).Times(1)
2926+
mockAPI.EXPECT().HostGroups(ctx).Return(nil, fmt.Errorf("backend config location is not set"))
2927+
2928+
err := driver.ReconcileNodeAccess(ctx, nodes, "", "")
2929+
assert.Error(t, err, "ReconcileNodeAccess should fail when HostGroups fails")
2930+
assert.Contains(t, err.Error(), "could not list host groups", "Error should mention host groups")
2931+
}
2932+
29172933
// ============================================================================
29182934
// Snapshot tests
29192935
// ============================================================================

0 commit comments

Comments
 (0)