Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 1 addition & 4 deletions api/v1alpha1/metalstackcluster_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ const (

ClusterControlPlaneEndpointDefaultPort = 443

ClusterNodeNetworkEnsured clusterv1.ConditionType = "ClusterNodeNetworkEnsured"
ClusterControlPlaneIPEnsured clusterv1.ConditionType = "ClusterControlPlaneIPEnsured"
)

Expand All @@ -52,9 +51,7 @@ type MetalStackClusterSpec struct {
ProjectID string `json:"projectID"`

// NodeNetworkID is the network ID in metal-stack in which the worker nodes and the firewall of the cluster are placed.
// If not provided this will automatically be acquired during reconcile.
// +optional
NodeNetworkID *string `json:"nodeNetworkID,omitempty"`
NodeNetworkID string `json:"nodeNetworkID"`

// ControlPlaneIP is the ip address in metal-stack on which the control plane will be exposed.
// If this ip and the control plane endpoint are not provided, an ephemeral ip will automatically be acquired during reconcile.
Expand Down
5 changes: 0 additions & 5 deletions api/v1alpha1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,8 @@ spec:
Static ip addresses will not be deleted.
type: string
nodeNetworkID:
description: |-
NodeNetworkID is the network ID in metal-stack in which the worker nodes and the firewall of the cluster are placed.
If not provided this will automatically be acquired during reconcile.
description: NodeNetworkID is the network ID in metal-stack in which
the worker nodes and the firewall of the cluster are placed.
type: string
partition:
description: Partition is the data center partition in which the resources
Expand All @@ -105,6 +104,7 @@ spec:
in which the associated metal-stack resources are created.
type: string
required:
- nodeNetworkID
- partition
- projectID
type: object
Expand Down
49 changes: 0 additions & 49 deletions internal/controller/metalstackcluster_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,16 +140,6 @@ func (r *MetalStackClusterReconciler) SetupWithManager(mgr ctrl.Manager) error {
}

func (r *clusterReconciler) reconcile() error {
nodeNetworkID, err := r.ensureNodeNetwork()
if err != nil {
conditions.MarkFalse(r.infraCluster, v1alpha1.ClusterNodeNetworkEnsured, "InternalError", clusterv1.ConditionSeverityError, "%s", err.Error())
return fmt.Errorf("unable to ensure node network: %w", err)
}
conditions.MarkTrue(r.infraCluster, v1alpha1.ClusterNodeNetworkEnsured)
r.infraCluster.Spec.NodeNetworkID = &nodeNetworkID

r.log.Info("reconciled node network", "network-id", nodeNetworkID)

if r.infraCluster.Spec.ControlPlaneEndpoint.Host == "" {
ip, err := r.ensureControlPlaneIP()
if err != nil {
Expand Down Expand Up @@ -189,51 +179,12 @@ func (r *clusterReconciler) delete() error {
}
r.infraCluster.Spec.ControlPlaneIP = nil

err = r.deleteNodeNetwork()
if err != nil {
return fmt.Errorf("unable to delete node network: %w", err)
}
r.infraCluster.Spec.NodeNetworkID = nil

r.log.Info("deletion finished, removing finalizer")
controllerutil.RemoveFinalizer(r.infraCluster, v1alpha1.ClusterFinalizer)

return err
}

func (r *clusterReconciler) ensureNodeNetwork() (string, error) {
if r.infraCluster.Spec.NodeNetworkID != nil {
return *r.infraCluster.Spec.NodeNetworkID, nil
}

resp, err := r.metalClient.Network().AllocateNetwork(network.NewAllocateNetworkParams().WithBody(&models.V1NetworkAllocateRequest{
Projectid: r.infraCluster.Spec.ProjectID,
Partitionid: r.infraCluster.Spec.Partition,
Name: r.infraCluster.GetName(),
Description: fmt.Sprintf("%s/%s", r.infraCluster.GetNamespace(), r.infraCluster.GetName()),
Labels: map[string]string{tag.ClusterID: string(r.infraCluster.GetUID())},
}).WithContext(r.ctx), nil)
if err != nil {
return "", fmt.Errorf("error creating node network: %w", err)
}

return *resp.Payload.ID, nil
}

func (r *clusterReconciler) deleteNodeNetwork() error {
if r.infraCluster.Spec.NodeNetworkID == nil {
return nil
}

_, err := r.metalClient.Network().FreeNetwork(network.NewFreeNetworkParams().WithID(*r.infraCluster.Spec.NodeNetworkID).WithContext(r.ctx), nil)
if err != nil {
return err
}
r.log.Info("deleted node network")

return nil
}

func (r *clusterReconciler) ensureControlPlaneIP() (string, error) {
if r.infraCluster.Spec.ControlPlaneIP != nil {
return *r.infraCluster.Spec.ControlPlaneIP, nil
Expand Down
36 changes: 2 additions & 34 deletions internal/controller/metalstackcluster_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ var _ = Describe("MetalStackCluster Controller", func() {
resource.Spec = v1alpha1.MetalStackClusterSpec{
ControlPlaneEndpoint: v1alpha1.APIEndpoint{},
ProjectID: "test-project",
NodeNetworkID: nil,
NodeNetworkID: "node-network-id",
ControlPlaneIP: nil,
Partition: "test-partition",
}
Expand Down Expand Up @@ -156,26 +156,6 @@ var _ = Describe("MetalStackCluster Controller", func() {
}, nil)
},
Network: func(m *mock.Mock) {
m.On("AllocateNetwork", testcommon.MatchIgnoreContext(testingT, metalnetwork.NewAllocateNetworkParams().WithBody(&models.V1NetworkAllocateRequest{
Name: resource.Name,
Description: resource.Namespace + "/" + resource.Name,
Labels: map[string]string{
"cluster.metal-stack.io/id": string(resource.UID),
},
Partitionid: "test-partition",
Projectid: "test-project",
})), nil).Return(&metalnetwork.AllocateNetworkCreated{
Payload: &models.V1NetworkResponse{
Labels: map[string]string{
"cluster.metal-stack.io/id": string(resource.UID),
},
Partitionid: "test-partition",
Projectid: "test-project",
ID: ptr.To("test-network"),
Prefixes: []string{"192.168.42.0/24"},
},
}, nil)

m.On("FindNetworks", testcommon.MatchIgnoreContext(testingT, metalnetwork.NewFindNetworksParams().WithBody(&models.V1NetworkFindRequest{
Labels: map[string]string{
"network.metal-stack.io/default": "",
Expand Down Expand Up @@ -215,10 +195,6 @@ var _ = Describe("MetalStackCluster Controller", func() {

Expect(k8sClient.Get(ctx, typeNamespacedName, resource)).To(Succeed())

Expect(resource.Status.Conditions).To(ContainElement(MatchFields(IgnoreExtras, Fields{
"Type": Equal(v1alpha1.ClusterNodeNetworkEnsured),
"Status": Equal(corev1.ConditionTrue),
})))
Expect(resource.Status.Conditions).To(ContainElement(MatchFields(IgnoreExtras, Fields{
"Type": Equal(v1alpha1.ClusterControlPlaneIPEnsured),
"Status": Equal(corev1.ConditionTrue),
Expand All @@ -244,7 +220,7 @@ var _ = Describe("MetalStackCluster Controller", func() {
resource.Spec = v1alpha1.MetalStackClusterSpec{
ControlPlaneEndpoint: v1alpha1.APIEndpoint{},
ProjectID: "test-project",
NodeNetworkID: &nodeNetworkID,
NodeNetworkID: nodeNetworkID,
ControlPlaneIP: &controlPlaneIP,
Partition: "test-partition",
}
Expand Down Expand Up @@ -287,10 +263,6 @@ var _ = Describe("MetalStackCluster Controller", func() {

return resource.Status.Conditions
}, "20s").Should(ContainElements(
MatchFields(IgnoreExtras, Fields{
"Type": Equal(v1alpha1.ClusterNodeNetworkEnsured),
"Status": Equal(corev1.ConditionTrue),
}),
MatchFields(IgnoreExtras, Fields{
"Type": Equal(v1alpha1.ClusterControlPlaneIPEnsured),
"Status": Equal(corev1.ConditionTrue),
Expand Down Expand Up @@ -336,10 +308,6 @@ var _ = Describe("MetalStackCluster Controller", func() {

return resource.Status.Conditions
}, "20s").Should(ContainElements(
MatchFields(IgnoreExtras, Fields{
"Type": Equal(v1alpha1.ClusterNodeNetworkEnsured),
"Status": Equal(corev1.ConditionTrue),
}),
MatchFields(IgnoreExtras, Fields{
"Type": Equal(v1alpha1.ClusterControlPlaneIPEnsured),
"Status": Equal(corev1.ConditionTrue),
Expand Down
7 changes: 1 addition & 6 deletions internal/controller/metalstackmachine_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,11 +172,6 @@ func (r *MetalStackMachineReconciler) SetupWithManager(mgr ctrl.Manager) error {
}

func (r *machineReconciler) reconcile() (ctrl.Result, error) {
if r.infraCluster.Spec.NodeNetworkID == nil {
// this should not happen because before setting this id the cluster status should not become ready, but we check it anyway
return ctrl.Result{}, errors.New("waiting until node network id was set to infrastructure cluster status")
}

if r.infraCluster.Spec.ControlPlaneEndpoint.Host == "" {
return ctrl.Result{}, errors.New("waiting until control plane ip was set to infrastructure cluster spec")
}
Expand Down Expand Up @@ -276,7 +271,7 @@ func (r *machineReconciler) create() (*models.V1MachineResponse, error) {
nws = []*models.V1MachineAllocationNetwork{
{
Autoacquire: ptr.To(true),
Networkid: r.infraCluster.Spec.NodeNetworkID,
Networkid: &r.infraCluster.Spec.NodeNetworkID,
},
}
)
Expand Down
Loading