Skip to content

Commit 028f798

Browse files
committed
Review suggestions
1 parent 1a69d44 commit 028f798

File tree

6 files changed

+32
-20
lines changed

6 files changed

+32
-20
lines changed

api/v1beta1/conditions_const.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,8 @@ const (
8080
NetworkReadyCondition clusterv1.ConditionType = "NetworkReady"
8181
// NetworkReconcileFailedReason indicates that reconciling the network failed.
8282
NetworkReconcileFailedReason = "NetworkReconcileFailed"
83+
// MultipleSubnetsExistReason indicates that the network has multiple subnets.
84+
MultipleSubnetsExistReason = "MultipleSubnetsExist"
8385
)
8486

8587
const (

api/v1beta1/types.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ type HCloudNetworkSpec struct {
226226
Enabled bool `json:"enabled"`
227227

228228
// CIDRBlock defines the cidrBlock of the HCloud Network.
229-
// Defaults to "10.0.0.0/16".
229+
// The webhook defaults this to "10.0.0.0/16".
230230
// Mutually exclusive with ID.
231231
// +optional
232232
CIDRBlock *string `json:"cidrBlock,omitempty"`

config/crd/bases/infrastructure.cluster.x-k8s.io_hetznerclusters.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ spec:
192192
cidrBlock:
193193
description: |-
194194
CIDRBlock defines the cidrBlock of the HCloud Network.
195-
Defaults to "10.0.0.0/16".
195+
The webhook defaults this to "10.0.0.0/16".
196196
Mutually exclusive with ID.
197197
type: string
198198
enabled:

config/crd/bases/infrastructure.cluster.x-k8s.io_hetznerclustertemplates.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ spec:
223223
cidrBlock:
224224
description: |-
225225
CIDRBlock defines the cidrBlock of the HCloud Network.
226-
Defaults to "10.0.0.0/16".
226+
The webhook defaults this to "10.0.0.0/16".
227227
Mutually exclusive with ID.
228228
type: string
229229
enabled:

controllers/hetznercluster_controller.go

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -346,15 +346,9 @@ func (r *HetznerClusterReconciler) reconcileDelete(ctx context.Context, clusterS
346346
return reconcile.Result{}, fmt.Errorf("failed to delete load balancers for HetznerCluster %s/%s: %w", hetznerCluster.Namespace, hetznerCluster.Name, err)
347347
}
348348

349-
// delete the network only if it is owned by us
350-
if hetznerCluster.Status.Network != nil {
351-
if hetznerCluster.Status.Network.Labels[hetznerCluster.ClusterTagKey()] == string(infrav1.ResourceLifecycleOwned) {
352-
if err := network.NewService(clusterScope).Delete(ctx); err != nil {
353-
return reconcile.Result{}, fmt.Errorf("failed to delete network for HetznerCluster %s/%s: %w", hetznerCluster.Namespace, hetznerCluster.Name, err)
354-
}
355-
} else {
356-
clusterScope.V(1).Info("network is not owned by us", "id", hetznerCluster.Status.Network.ID, "labels", hetznerCluster.Status.Network.Labels)
357-
}
349+
// delete the network
350+
if err := network.NewService(clusterScope).Delete(ctx); err != nil {
351+
return reconcile.Result{}, fmt.Errorf("failed to delete network for HetznerCluster %s/%s: %w", hetznerCluster.Namespace, hetznerCluster.Name, err)
358352
}
359353

360354
// delete the placement groups

pkg/services/hcloud/network/network.go

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,17 @@ func (s *Service) Reconcile(ctx context.Context) (err error) {
8181
}
8282
}
8383

84+
if len(network.Subnets) > 1 {
85+
conditions.MarkFalse(
86+
s.scope.HetznerCluster,
87+
infrav1.NetworkReadyCondition,
88+
infrav1.MultipleSubnetsExistReason,
89+
clusterv1.ConditionSeverityWarning,
90+
"multiple subnets not allowed",
91+
)
92+
return nil
93+
}
94+
8495
conditions.MarkTrue(s.scope.HetznerCluster, infrav1.NetworkReadyCondition)
8596
s.scope.HetznerCluster.Status.Network = statusFromHCloudNetwork(network)
8697

@@ -137,25 +148,33 @@ func (s *Service) createOpts() (hcloud.NetworkCreateOpts, error) {
137148

138149
// Delete implements deletion of the network.
139150
func (s *Service) Delete(ctx context.Context) error {
140-
if s.scope.HetznerCluster.Status.Network == nil {
151+
hetznerCluster := s.scope.HetznerCluster
152+
153+
if hetznerCluster.Status.Network == nil {
141154
// nothing to delete
142155
return nil
143156
}
144157

145-
id := s.scope.HetznerCluster.Status.Network.ID
158+
// only delete the network if it is owned by us
159+
if hetznerCluster.Status.Network.Labels[hetznerCluster.ClusterTagKey()] != string(infrav1.ResourceLifecycleOwned) {
160+
s.scope.V(1).Info("network is not owned by us", "id", hetznerCluster.Status.Network.ID, "labels", hetznerCluster.Status.Network.Labels)
161+
return nil
162+
}
163+
164+
id := hetznerCluster.Status.Network.ID
146165

147166
if err := s.scope.HCloudClient.DeleteNetwork(ctx, &hcloud.Network{ID: id}); err != nil {
148-
hcloudutil.HandleRateLimitExceeded(s.scope.HetznerCluster, err, "DeleteNetwork")
167+
hcloudutil.HandleRateLimitExceeded(hetznerCluster, err, "DeleteNetwork")
149168
// if resource has been deleted already then do nothing
150169
if hcloud.IsError(err, hcloud.ErrorCodeNotFound) {
151170
s.scope.V(1).Info("deleting network failed - not found", "id", id)
152171
return nil
153172
}
154-
record.Warnf(s.scope.HetznerCluster, "NetworkDeleteFailed", "Failed to delete network with ID %v", id)
173+
record.Warnf(hetznerCluster, "NetworkDeleteFailed", "Failed to delete network with ID %v", id)
155174
return fmt.Errorf("failed to delete network: %w", err)
156175
}
157176

158-
record.Eventf(s.scope.HetznerCluster, "NetworkDeleted", "Deleted network with ID %v", id)
177+
record.Eventf(hetznerCluster, "NetworkDeleted", "Deleted network with ID %v", id)
159178
return nil
160179
}
161180

@@ -170,9 +189,6 @@ func (s *Service) findNetwork(ctx context.Context) (*hcloud.Network, error) {
170189
}
171190

172191
if network != nil {
173-
if len(network.Subnets) > 1 {
174-
return nil, fmt.Errorf("multiple subnets not allowed")
175-
}
176192
s.scope.V(1).Info("found network", "id", network.ID, "name", network.Name, "labels", network.Labels)
177193
return network, nil
178194
}

0 commit comments

Comments
 (0)