Skip to content

Commit

Permalink
Return error when requested network is not found
Browse files Browse the repository at this point in the history
  • Loading branch information
johannesfrey committed Nov 24, 2024
1 parent eca91ee commit 4806de2
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 17 deletions.
16 changes: 2 additions & 14 deletions controllers/hetznercluster_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -886,10 +886,7 @@ var _ = Describe("Hetzner ClusterReconciler", func() {
networkName := utils.GenerateName(nil, "network1-")
network, err := hcloudClient.CreateNetwork(context.Background(), hcloud.NetworkCreateOpts{Name: networkName})
Expect(err).To(Succeed())
defer func() {
err := hcloudClient.DeleteNetwork(context.Background(), network)
Expect(err).To(Succeed())
}()

networksBeforeClusterCreate, err := hcloudClient.ListNetworks(context.Background(), hcloud.NetworkListOpts{})
Expect(err).To(Succeed())

Expand All @@ -911,9 +908,6 @@ var _ = Describe("Hetzner ClusterReconciler", func() {
},
}
Expect(testEnv.Create(ctx, capiCluster)).To(Succeed())
defer func() {
Expect(testEnv.Cleanup(ctx, capiCluster)).To(Succeed())
}()

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

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

Expand Down Expand Up @@ -968,10 +959,7 @@ var _ = Describe("Hetzner ClusterReconciler", func() {
networkName := utils.GenerateName(nil, "network2-")
network, err := hcloudClient.CreateNetwork(context.Background(), hcloud.NetworkCreateOpts{Name: networkName})
Expect(err).To(Succeed())
defer func() {
err := hcloudClient.DeleteNetwork(context.Background(), network)
Expect(err).To(Succeed())
}()

networksBeforeClusterDelete, err := hcloudClient.ListNetworks(context.Background(), hcloud.NetworkListOpts{})
Expect(err).To(Succeed())

Expand Down
6 changes: 6 additions & 0 deletions pkg/services/hcloud/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ const errStringUnauthorized = "(unauthorized)"
// ErrUnauthorized means that the API call is unauthorized.
var ErrUnauthorized = fmt.Errorf("unauthorized")

// ErrNotFound means that the requested resource cannot be found.
var ErrNotFound = fmt.Errorf("not found")

// Client collects all methods used by the controller in the hcloud cloud API.
type Client interface {
// Reset resets the local cache. Only implemented in the fake client.
Expand Down Expand Up @@ -303,6 +306,9 @@ func (c *realClient) ListNetworks(ctx context.Context, opts hcloud.NetworkListOp

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

Expand Down
7 changes: 6 additions & 1 deletion pkg/services/hcloud/client/fake/hcloud_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -653,7 +653,12 @@ func (c *cacheHCloudClient) ListNetworks(_ context.Context, opts hcloud.NetworkL
return networks, nil
}
func (c *cacheHCloudClient) GetNetwork(_ context.Context, id int64) (*hcloud.Network, error) {
return c.networkCache.idMap[id], nil
n, found := c.networkCache.idMap[id]
if !found {
return nil, fmt.Errorf("%w: id: %d", hcloudclient.ErrNotFound, id)
}

return n, nil
}

func (c *cacheHCloudClient) DeleteNetwork(_ context.Context, network *hcloud.Network) error {
Expand Down
14 changes: 12 additions & 2 deletions pkg/services/hcloud/network/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,14 +178,24 @@ func (s *Service) Delete(ctx context.Context) error {
return nil
}

func (s *Service) findNetworkByID(ctx context.Context, id int64) (*hcloud.Network, error) {
network, err := s.scope.HCloudClient.GetNetwork(ctx, id)
if err != nil {
hcloudutil.HandleRateLimitExceeded(s.scope.HetznerCluster, err, "GetNetwork")
return nil, fmt.Errorf("failed to get network %d: %w", id, err)
}

return network, nil
}

func (s *Service) findNetwork(ctx context.Context) (*hcloud.Network, error) {
// if an ID was provided we want to use the existing Network.
id := s.scope.HetznerCluster.Spec.HCloudNetwork.ID
if id != nil {
network, err := s.scope.HCloudClient.GetNetwork(ctx, *id)
network, err := s.findNetworkByID(ctx, *id)
if err != nil {
hcloudutil.HandleRateLimitExceeded(s.scope.HetznerCluster, err, "GetNetwork")
return nil, fmt.Errorf("failed to get network %d: %w", *id, err)
return nil, fmt.Errorf("failed to find network with id %d: %w", *id, err)
}

if network != nil {
Expand Down

0 comments on commit 4806de2

Please sign in to comment.