Skip to content

Commit 23e4d3d

Browse files
authored
gcnv san: fix for zonal pools and host group handling
1 parent b4989cf commit 23e4d3d

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
// ///////////////////////////////////////////////////////////////////////////////
@@ -1874,9 +1877,9 @@ func (c Client) HostGroups(ctx context.Context) ([]*HostGroup, error) {
18741877

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

1877-
location, err := c.getLocationFromCapacityPools()
1880+
location, err := c.getRegionalLocation()
18781881
if err != nil {
1879-
return []*HostGroup{}, nil
1882+
return nil, err
18801883
}
18811884

18821885
sdkCtx, cancel := context.WithTimeout(ctx, c.config.SDKTimeout)
@@ -1913,7 +1916,7 @@ func (c Client) HostGroupByName(ctx context.Context, name string) (*HostGroup, e
19131916

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

1916-
location, err := c.getLocationFromCapacityPools()
1919+
location, err := c.getRegionalLocation()
19171920
if err != nil {
19181921
return nil, err
19191922
}
@@ -1951,7 +1954,7 @@ func (c Client) CreateHostGroup(ctx context.Context, request *HostGroupCreateReq
19511954

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

1954-
location, err := c.getLocationFromCapacityPools()
1957+
location, err := c.getRegionalLocation()
19551958
if err != nil {
19561959
return nil, err
19571960
}

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
@@ -2826,6 +2826,22 @@ func TestSANDriver_ReconcileNodeAccess_KeepOrphanedHostGroupsWithVolumes(t *test
28262826
assert.NoError(t, err, "ReconcileNodeAccess should succeed")
28272827
}
28282828

2829+
func TestSANDriver_ReconcileNodeAccess_HostGroupsError(t *testing.T) {
2830+
mockAPI, driver := newMockSANDriver(t)
2831+
2832+
nodes := []*models.Node{
2833+
{Name: "node-1", IQN: "iqn.xxx.node-1"},
2834+
}
2835+
2836+
// Mock expectations - HostGroups returns error (e.g., location not configured)
2837+
mockAPI.EXPECT().RefreshGCNVResources(ctx).Return(nil).Times(1)
2838+
mockAPI.EXPECT().HostGroups(ctx).Return(nil, fmt.Errorf("backend config location is not set"))
2839+
2840+
err := driver.ReconcileNodeAccess(ctx, nodes, "", "")
2841+
assert.Error(t, err, "ReconcileNodeAccess should fail when HostGroups fails")
2842+
assert.Contains(t, err.Error(), "could not list host groups", "Error should mention host groups")
2843+
}
2844+
28292845
// ============================================================================
28302846
// Snapshot tests
28312847
// ============================================================================

0 commit comments

Comments
 (0)