Skip to content

Commit 15b539b

Browse files
committed
fix: use new API for network auto detection (#131)
Network auto detection in GKE was not working due to deprecated API. This commit updates the code by using the new ones. Signed-off-by: Elis Lulja <[email protected]>
1 parent c7cef4d commit 15b539b

File tree

1 file changed

+29
-2
lines changed

1 file changed

+29
-2
lines changed

pkg/cluster/network.go

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,10 @@ package cluster
2020

2121
import (
2222
"context"
23+
"errors"
2324
"fmt"
25+
"net/http"
26+
"path"
2427
"strings"
2528
"time"
2629

@@ -30,6 +33,7 @@ import (
3033
awssess "github.com/aws/aws-sdk-go/aws/session"
3134
"github.com/aws/aws-sdk-go/service/ec2"
3235
gccontainer "google.golang.org/api/container/v1"
36+
"google.golang.org/api/googleapi"
3337
gcoption "google.golang.org/api/option"
3438
)
3539

@@ -121,9 +125,32 @@ func GetNetworkFromGKE(ctx context.Context, opts ...gcoption.ClientOption) (*Net
121125
return nil, err
122126
}
123127

124-
cluster, err := cli.Projects.Zones.Clusters.Get(projectID, zone, clusterName).Do()
128+
plcs := gccontainer.NewProjectsLocationsClustersService(cli)
129+
cluster, err := plcs.Get(path.Join("projects", projectID, "locations", zone, "clusters", clusterName)).Do()
125130
if err != nil {
126-
return nil, err
131+
var e *googleapi.Error
132+
if errors.As(err, &e) {
133+
if e.Code != http.StatusNotFound {
134+
return nil, err
135+
}
136+
}
137+
138+
// Retry with a region, not with a zone.
139+
// TODO: check if this always applies or it is just a special case.
140+
// My guess is that this does not happen when running GKE in single
141+
// zone, but only in multi-zone, e.g. when using autopilot.
142+
// TODO: remove this fmt line after checking the above.
143+
// See #131.
144+
fmt.Println("could not get cluster data, retrying with region...")
145+
lastDash := strings.LastIndex(zone, "-")
146+
if lastDash == -1 {
147+
return nil, err
148+
}
149+
150+
cluster, err = plcs.Get(path.Join("projects", projectID, "locations", zone[:lastDash], "clusters", clusterName)).Do()
151+
if err != nil {
152+
return nil, err
153+
}
127154
}
128155

129156
return &NetworkConfiguration{cluster.Network, cluster.Subnetwork}, nil

0 commit comments

Comments
 (0)