Skip to content

Commit bdf6ca4

Browse files
committed
Return error when requested network is not found
1 parent eca91ee commit bdf6ca4

File tree

5 files changed

+78
-18
lines changed

5 files changed

+78
-18
lines changed

.vscode/launch.json

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
{
2+
"version": "0.2.0",
3+
"configurations": [
4+
{
5+
"name": "Debug Single Ginkgo Test",
6+
"type": "go",
7+
"request": "launch",
8+
"mode": "test",
9+
"program": "${fileDirname}",
10+
"args": [
11+
"-ginkgo.v",
12+
"-ginkgo.show-node-events",
13+
"-ginkgo.focus=${input:ginkgoFocus}"
14+
],
15+
"env": {
16+
"KUBEBUILDER_ASSETS": "/Users/jotef/code/go/src/github.com/johannesfrey/cluster-api-provider-hetzner/hack/tools/bin/k8s/1.29.3-darwin-amd64"
17+
},
18+
"showLog": true
19+
},
20+
{
21+
"name": "Debug Current Ginkgo Test File",
22+
"type": "go",
23+
"request": "launch",
24+
"mode": "test",
25+
"program": "${fileDirname}",
26+
"args": [
27+
"-ginkgo.v",
28+
"-ginkgo.show-node-events",
29+
"-ginkgo.focus=${input:ginkgoFocus}"
30+
],
31+
"env": {
32+
"KUBEBUILDER_ASSETS": "/Users/jotef/code/go/src/github.com/johannesfrey/cluster-api-provider-hetzner/hack/tools/bin/k8s/1.29.3-darwin-amd64"
33+
},
34+
"showLog": true
35+
}
36+
],
37+
"inputs": [
38+
{
39+
"id": "ginkgoFocus",
40+
"type": "promptString",
41+
"description": "Enter the Ginkgo focus pattern (test description to run)",
42+
"default": ""
43+
},
44+
{
45+
"id": "kubebuilderAssets",
46+
"type": "command",
47+
"command": "${workspaceFolder}/hack/tools/bin/setup-envtest use --use-env --bin-dir ${workspaceFolder}/hack/tools/bin -p path 1.29.3"
48+
}
49+
]
50+
}

controllers/hetznercluster_controller_test.go

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -886,10 +886,7 @@ var _ = Describe("Hetzner ClusterReconciler", func() {
886886
networkName := utils.GenerateName(nil, "network1-")
887887
network, err := hcloudClient.CreateNetwork(context.Background(), hcloud.NetworkCreateOpts{Name: networkName})
888888
Expect(err).To(Succeed())
889-
defer func() {
890-
err := hcloudClient.DeleteNetwork(context.Background(), network)
891-
Expect(err).To(Succeed())
892-
}()
889+
893890
networksBeforeClusterCreate, err := hcloudClient.ListNetworks(context.Background(), hcloud.NetworkListOpts{})
894891
Expect(err).To(Succeed())
895892

@@ -911,9 +908,6 @@ var _ = Describe("Hetzner ClusterReconciler", func() {
911908
},
912909
}
913910
Expect(testEnv.Create(ctx, capiCluster)).To(Succeed())
914-
defer func() {
915-
Expect(testEnv.Cleanup(ctx, capiCluster)).To(Succeed())
916-
}()
917911

918912
instance := &infrav1.HetznerCluster{
919913
ObjectMeta: metav1.ObjectMeta{
@@ -934,9 +928,6 @@ var _ = Describe("Hetzner ClusterReconciler", func() {
934928
// existing network was given.
935929
instance.Spec.HCloudNetwork.ID = ptr.To(network.ID)
936930
Expect(testEnv.Create(ctx, instance)).To(Succeed())
937-
defer func() {
938-
Expect(testEnv.Cleanup(ctx, instance)).To(Succeed())
939-
}()
940931

941932
key := client.ObjectKey{Namespace: instance.Namespace, Name: instance.Name}
942933

@@ -968,10 +959,7 @@ var _ = Describe("Hetzner ClusterReconciler", func() {
968959
networkName := utils.GenerateName(nil, "network2-")
969960
network, err := hcloudClient.CreateNetwork(context.Background(), hcloud.NetworkCreateOpts{Name: networkName})
970961
Expect(err).To(Succeed())
971-
defer func() {
972-
err := hcloudClient.DeleteNetwork(context.Background(), network)
973-
Expect(err).To(Succeed())
974-
}()
962+
975963
networksBeforeClusterDelete, err := hcloudClient.ListNetworks(context.Background(), hcloud.NetworkListOpts{})
976964
Expect(err).To(Succeed())
977965

pkg/services/hcloud/client/client.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@ const errStringUnauthorized = "(unauthorized)"
3838
// ErrUnauthorized means that the API call is unauthorized.
3939
var ErrUnauthorized = fmt.Errorf("unauthorized")
4040

41+
// ErrNotFound means that the requested resource cannot be found.
42+
var ErrNotFound = fmt.Errorf("not found")
43+
4144
// Client collects all methods used by the controller in the hcloud cloud API.
4245
type Client interface {
4346
// Reset resets the local cache. Only implemented in the fake client.
@@ -303,6 +306,9 @@ func (c *realClient) ListNetworks(ctx context.Context, opts hcloud.NetworkListOp
303306

304307
func (c *realClient) GetNetwork(ctx context.Context, id int64) (*hcloud.Network, error) {
305308
res, _, err := c.client.Network.GetByID(ctx, id)
309+
if res == nil {
310+
return nil, fmt.Errorf("%w: id: %d", ErrNotFound, id)
311+
}
306312
return res, err
307313
}
308314

pkg/services/hcloud/client/fake/hcloud_client.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -653,12 +653,18 @@ func (c *cacheHCloudClient) ListNetworks(_ context.Context, opts hcloud.NetworkL
653653
return networks, nil
654654
}
655655
func (c *cacheHCloudClient) GetNetwork(_ context.Context, id int64) (*hcloud.Network, error) {
656-
return c.networkCache.idMap[id], nil
656+
n, found := c.networkCache.idMap[id]
657+
if !found {
658+
return nil, fmt.Errorf("%w: id: %d", hcloudclient.ErrNotFound, id)
659+
}
660+
661+
return n, nil
657662
}
658663

659664
func (c *cacheHCloudClient) DeleteNetwork(_ context.Context, network *hcloud.Network) error {
665+
660666
if _, found := c.networkCache.idMap[network.ID]; !found {
661-
return hcloud.Error{Code: hcloud.ErrorCodeNotFound, Message: "not found"}
667+
return nil
662668
}
663669
n := c.networkCache.idMap[network.ID]
664670
delete(c.networkCache.nameMap, n.Name)

pkg/services/hcloud/network/network.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -178,14 +178,24 @@ func (s *Service) Delete(ctx context.Context) error {
178178
return nil
179179
}
180180

181+
func (s *Service) findNetworkByID(ctx context.Context, id int64) (*hcloud.Network, error) {
182+
network, err := s.scope.HCloudClient.GetNetwork(ctx, id)
183+
if err != nil {
184+
hcloudutil.HandleRateLimitExceeded(s.scope.HetznerCluster, err, "GetNetwork")
185+
return nil, fmt.Errorf("failed to get network %d: %w", id, err)
186+
}
187+
188+
return network, nil
189+
}
190+
181191
func (s *Service) findNetwork(ctx context.Context) (*hcloud.Network, error) {
182192
// if an ID was provided we want to use the existing Network.
183193
id := s.scope.HetznerCluster.Spec.HCloudNetwork.ID
184194
if id != nil {
185-
network, err := s.scope.HCloudClient.GetNetwork(ctx, *id)
195+
network, err := s.findNetworkByID(ctx, *id)
186196
if err != nil {
187197
hcloudutil.HandleRateLimitExceeded(s.scope.HetznerCluster, err, "GetNetwork")
188-
return nil, fmt.Errorf("failed to get network %d: %w", *id, err)
198+
return nil, fmt.Errorf("failed to find network with id %d: %w", *id, err)
189199
}
190200

191201
if network != nil {

0 commit comments

Comments
 (0)