Skip to content

Commit

Permalink
API changes for Dependent & Referenced resources
Browse files Browse the repository at this point in the history
  • Loading branch information
EmilienM committed Feb 29, 2024
1 parent a8425da commit 9d40ba2
Show file tree
Hide file tree
Showing 14 changed files with 161 additions and 122 deletions.
2 changes: 1 addition & 1 deletion api/v1alpha5/conversion_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ func TestConvertFrom(t *testing.T) {
Spec: OpenStackMachineSpec{},
ObjectMeta: metav1.ObjectMeta{
Annotations: map[string]string{
"cluster.x-k8s.io/conversion-data": "{\"spec\":{\"cloudName\":\"\",\"flavor\":\"\",\"image\":{}},\"status\":{\"dependentResources\":{},\"ready\":false,\"referencedResources\":{}}}",
"cluster.x-k8s.io/conversion-data": "{\"spec\":{\"cloudName\":\"\",\"flavor\":\"\",\"image\":{}},\"status\":{\"ready\":false}}",
},
},
},
Expand Down
10 changes: 5 additions & 5 deletions api/v1alpha6/conversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,11 +129,11 @@ func restorev1beta1ClusterStatus(previous *infrav1.OpenStackClusterStatus, dst *
dst.WorkerSecurityGroup = previous.WorkerSecurityGroup
dst.BastionSecurityGroup = previous.BastionSecurityGroup

if previous.Bastion != nil {
if previous.Bastion != nil && previous.Bastion.ReferencedResources != nil {
dst.Bastion.ReferencedResources = previous.Bastion.ReferencedResources
}
if previous.Bastion != nil && previous.Bastion.DependentResources.PortsStatus != nil {
dst.Bastion.DependentResources.PortsStatus = previous.Bastion.DependentResources.PortsStatus
if previous.Bastion != nil && previous.Bastion.DependentResources != nil {
dst.Bastion.DependentResources = previous.Bastion.DependentResources
}
}

Expand Down Expand Up @@ -389,13 +389,13 @@ var v1beta1OpenStackMachineRestorer = conversion.RestorerFor[*infrav1.OpenStackM
restorev1beta1MachineSpec,
),
"depresources": conversion.UnconditionalFieldRestorer(
func(c *infrav1.OpenStackMachine) *infrav1.DependentMachineResources {
func(c *infrav1.OpenStackMachine) **infrav1.DependentMachineResources {
return &c.Status.DependentResources
},
),
// No equivalent in v1alpha6
"refresources": conversion.UnconditionalFieldRestorer(
func(c *infrav1.OpenStackMachine) *infrav1.ReferencedMachineResources {
func(c *infrav1.OpenStackMachine) **infrav1.ReferencedMachineResources {
return &c.Status.ReferencedResources
},
),
Expand Down
10 changes: 5 additions & 5 deletions api/v1alpha7/conversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,12 +121,12 @@ func restorev1beta1ClusterStatus(previous *infrav1.OpenStackClusterStatus, dst *
restorev1beta1SecurityGroupStatus(previous.BastionSecurityGroup, dst.BastionSecurityGroup)

// ReferencedResources have no equivalent in v1alpha7
if previous.Bastion != nil {
if previous.Bastion != nil && previous.Bastion.ReferencedResources != nil {
dst.Bastion.ReferencedResources = previous.Bastion.ReferencedResources
}

if previous.Bastion != nil && previous.Bastion.DependentResources.PortsStatus != nil {
dst.Bastion.DependentResources.PortsStatus = previous.Bastion.DependentResources.PortsStatus
if previous.Bastion != nil && previous.Bastion.DependentResources != nil {
dst.Bastion.DependentResources = previous.Bastion.DependentResources
}
}

Expand Down Expand Up @@ -354,14 +354,14 @@ var v1beta1OpenStackMachineRestorer = conversion.RestorerFor[*infrav1.OpenStackM
restorev1beta1MachineSpec,
),
"depresources": conversion.UnconditionalFieldRestorer(
func(c *infrav1.OpenStackMachine) *infrav1.DependentMachineResources {
func(c *infrav1.OpenStackMachine) **infrav1.DependentMachineResources {
return &c.Status.DependentResources
},
),

// No equivalent in v1alpha7
"refresources": conversion.UnconditionalFieldRestorer(
func(c *infrav1.OpenStackMachine) *infrav1.ReferencedMachineResources {
func(c *infrav1.OpenStackMachine) **infrav1.ReferencedMachineResources {
return &c.Status.ReferencedResources
},
),
Expand Down
4 changes: 2 additions & 2 deletions api/v1beta1/openstackmachine_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,10 +117,10 @@ type OpenStackMachineStatus struct {
InstanceState *InstanceState `json:"instanceState,omitempty"`

// ReferencedResources contains resolved references to resources that the machine depends on.
ReferencedResources ReferencedMachineResources `json:"referencedResources,omitempty"`
ReferencedResources *ReferencedMachineResources `json:"referencedResources,omitempty"`

// DependentResources contains resolved dependent resources that were created by the machine.
DependentResources DependentMachineResources `json:"dependentResources,omitempty"`
DependentResources *DependentMachineResources `json:"dependentResources,omitempty"`

FailureReason *errors.MachineStatusError `json:"failureReason,omitempty"`

Expand Down
16 changes: 8 additions & 8 deletions api/v1beta1/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,14 +190,14 @@ type AddressPair struct {
}

type BastionStatus struct {
ID string `json:"id,omitempty"`
Name string `json:"name,omitempty"`
SSHKeyName string `json:"sshKeyName,omitempty"`
State InstanceState `json:"state,omitempty"`
IP string `json:"ip,omitempty"`
FloatingIP string `json:"floatingIP,omitempty"`
ReferencedResources ReferencedMachineResources `json:"referencedResources,omitempty"`
DependentResources DependentMachineResources `json:"dependentResources,omitempty"`
ID string `json:"id,omitempty"`
Name string `json:"name,omitempty"`
SSHKeyName string `json:"sshKeyName,omitempty"`
State InstanceState `json:"state,omitempty"`
IP string `json:"ip,omitempty"`
FloatingIP string `json:"floatingIP,omitempty"`
ReferencedResources *ReferencedMachineResources `json:"referencedResources,omitempty"`
DependentResources *DependentMachineResources `json:"dependentResources,omitempty"`
}

type RootVolume struct {
Expand Down
24 changes: 20 additions & 4 deletions api/v1beta1/zz_generated.deepcopy.go

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

32 changes: 18 additions & 14 deletions controllers/openstackcluster_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,24 +123,28 @@ func (r *OpenStackClusterReconciler) Reconcile(ctx context.Context, req ctrl.Req
scope := scope.NewWithLogger(clientScope, log)

// Resolve and store referenced & dependent resources for the bastion
if openStackCluster.Spec.Bastion != nil && openStackCluster.Spec.Bastion.Enabled {
// Note: the bastion won't be created before networking is done, which is why we only adopt resources once we have a network in status.
if openStackCluster.Spec.Bastion != nil && openStackCluster.Spec.Bastion.Enabled && openStackCluster.Status.Network != nil {
if openStackCluster.Status.Bastion == nil {
openStackCluster.Status.Bastion = &infrav1.BastionStatus{}
}
changed, err := compute.ResolveReferencedMachineResources(scope, openStackCluster, &openStackCluster.Spec.Bastion.Instance, &openStackCluster.Status.Bastion.ReferencedResources)
if err != nil {
return reconcile.Result{}, err
}
if changed {
if openStackCluster.Status.Bastion.ReferencedResources == nil {
openStackCluster.Status.Bastion.ReferencedResources = &infrav1.ReferencedMachineResources{}
referencedResources, err := compute.ResolveReferencedMachineResources(scope, openStackCluster, &openStackCluster.Spec.Bastion.Instance, openStackCluster.Status.Bastion.ReferencedResources)
if err != nil {
return reconcile.Result{}, err
}
openStackCluster.Status.Bastion.ReferencedResources = referencedResources
// If the referenced resources have changed, we need to update the OpenStackCluster status now.
return reconcile.Result{}, nil
}

changed, err = compute.ResolveDependentBastionResources(scope, openStackCluster, bastionName(cluster.Name))
if err != nil {
return reconcile.Result{}, err
}
if changed {
if openStackCluster.Status.Bastion.DependentResources == nil {
openStackCluster.Status.Bastion.DependentResources = &infrav1.DependentMachineResources{}
err = compute.ResolveDependentBastionResources(scope, openStackCluster, bastionName(cluster.Name))
if err != nil {
return reconcile.Result{}, err
}
// If the dependent resources have changed, we need to update the OpenStackCluster status now.
return reconcile.Result{}, nil
}
Expand Down Expand Up @@ -292,7 +296,7 @@ func deleteBastion(scope *scope.WithLogger, cluster *clusterv1.Cluster, openStac
}
}

if openStackCluster.Status.Bastion != nil && len(openStackCluster.Status.Bastion.DependentResources.PortsStatus) > 0 {
if openStackCluster.Status.Bastion != nil && openStackCluster.Status.Bastion.DependentResources != nil && len(openStackCluster.Status.Bastion.DependentResources.PortsStatus) > 0 {
trunkSupported, err := networkingService.IsTrunkExtSupported()
if err != nil {
return err
Expand Down Expand Up @@ -403,7 +407,7 @@ func reconcileBastion(scope *scope.WithLogger, cluster *clusterv1.Cluster, openS
}
}

err = getOrCreateBastionPorts(scope, cluster, openStackCluster, networkingService, cluster.Name)
err = getOrCreateBastionPorts(cluster, openStackCluster, networkingService, cluster.Name)
if err != nil {
handleUpdateOSCError(openStackCluster, fmt.Errorf("failed to get or create ports for bastion: %w", err))
return ctrl.Result{}, fmt.Errorf("failed to get or create ports for bastion: %w", err)
Expand Down Expand Up @@ -543,7 +547,7 @@ func getBastionSecurityGroups(openStackCluster *infrav1.OpenStackCluster) []infr
return instanceSpecSecurityGroups
}

func getOrCreateBastionPorts(scope *scope.WithLogger, cluster *clusterv1.Cluster, openStackCluster *infrav1.OpenStackCluster, networkingService *networking.Service, clusterName string) error {
func getOrCreateBastionPorts(cluster *clusterv1.Cluster, openStackCluster *infrav1.OpenStackCluster, networkingService *networking.Service, clusterName string) error {
if openStackCluster.Status.Bastion == nil {
openStackCluster.Status.Bastion = &infrav1.BastionStatus{}
}
Expand Down
30 changes: 15 additions & 15 deletions controllers/openstackcluster_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ var _ = Describe("OpenStackCluster controller", func() {
Expect(err).To(BeNil())
testCluster.Status = infrav1.OpenStackClusterStatus{
Bastion: &infrav1.BastionStatus{
ReferencedResources: infrav1.ReferencedMachineResources{
ReferencedResources: &infrav1.ReferencedMachineResources{
ImageID: "imageID",
PortsOpts: []infrav1.PortOpts{
{
Expand All @@ -241,7 +241,7 @@ var _ = Describe("OpenStackCluster controller", func() {
},
},
},
DependentResources: infrav1.DependentMachineResources{
DependentResources: &infrav1.DependentMachineResources{
PortsStatus: []infrav1.PortStatus{
{
ID: "portID1",
Expand Down Expand Up @@ -282,7 +282,7 @@ var _ = Describe("OpenStackCluster controller", func() {
Expect(testCluster.Status.Bastion).To(Equal(&infrav1.BastionStatus{
ID: "adopted-bastion-uuid",
State: "ACTIVE",
ReferencedResources: infrav1.ReferencedMachineResources{
ReferencedResources: &infrav1.ReferencedMachineResources{
ImageID: "imageID",
PortsOpts: []infrav1.PortOpts{
{
Expand All @@ -292,7 +292,7 @@ var _ = Describe("OpenStackCluster controller", func() {
},
},
},
DependentResources: infrav1.DependentMachineResources{
DependentResources: &infrav1.DependentMachineResources{
PortsStatus: []infrav1.PortStatus{
{
ID: "portID1",
Expand Down Expand Up @@ -323,7 +323,7 @@ var _ = Describe("OpenStackCluster controller", func() {
},
Bastion: &infrav1.BastionStatus{
ID: "adopted-fip-bastion-uuid",
ReferencedResources: infrav1.ReferencedMachineResources{
ReferencedResources: &infrav1.ReferencedMachineResources{
ImageID: "imageID",
PortsOpts: []infrav1.PortOpts{
{
Expand All @@ -333,7 +333,7 @@ var _ = Describe("OpenStackCluster controller", func() {
},
},
},
DependentResources: infrav1.DependentMachineResources{
DependentResources: &infrav1.DependentMachineResources{
PortsStatus: []infrav1.PortStatus{
{
ID: "portID1",
Expand Down Expand Up @@ -367,7 +367,7 @@ var _ = Describe("OpenStackCluster controller", func() {
ID: "adopted-fip-bastion-uuid",
FloatingIP: "1.2.3.4",
State: "ACTIVE",
ReferencedResources: infrav1.ReferencedMachineResources{
ReferencedResources: &infrav1.ReferencedMachineResources{
ImageID: "imageID",
PortsOpts: []infrav1.PortOpts{
{
Expand All @@ -377,7 +377,7 @@ var _ = Describe("OpenStackCluster controller", func() {
},
},
},
DependentResources: infrav1.DependentMachineResources{
DependentResources: &infrav1.DependentMachineResources{
PortsStatus: []infrav1.PortStatus{
{
ID: "portID1",
Expand Down Expand Up @@ -408,7 +408,7 @@ var _ = Describe("OpenStackCluster controller", func() {
},
Bastion: &infrav1.BastionStatus{
ID: "requeue-bastion-uuid",
ReferencedResources: infrav1.ReferencedMachineResources{
ReferencedResources: &infrav1.ReferencedMachineResources{
ImageID: "imageID",
PortsOpts: []infrav1.PortOpts{
{
Expand All @@ -418,7 +418,7 @@ var _ = Describe("OpenStackCluster controller", func() {
},
},
},
DependentResources: infrav1.DependentMachineResources{
DependentResources: &infrav1.DependentMachineResources{
PortsStatus: []infrav1.PortStatus{
{
ID: "portID1",
Expand Down Expand Up @@ -446,7 +446,7 @@ var _ = Describe("OpenStackCluster controller", func() {
Expect(testCluster.Status.Bastion).To(Equal(&infrav1.BastionStatus{
ID: "requeue-bastion-uuid",
State: "BUILD",
ReferencedResources: infrav1.ReferencedMachineResources{
ReferencedResources: &infrav1.ReferencedMachineResources{
ImageID: "imageID",
PortsOpts: []infrav1.PortOpts{
{
Expand All @@ -456,7 +456,7 @@ var _ = Describe("OpenStackCluster controller", func() {
},
},
},
DependentResources: infrav1.DependentMachineResources{
DependentResources: &infrav1.DependentMachineResources{
PortsStatus: []infrav1.PortStatus{
{
ID: "portID1",
Expand All @@ -478,7 +478,7 @@ var _ = Describe("OpenStackCluster controller", func() {
Expect(err).To(BeNil())
testCluster.Status = infrav1.OpenStackClusterStatus{
Bastion: &infrav1.BastionStatus{
ReferencedResources: infrav1.ReferencedMachineResources{
ReferencedResources: &infrav1.ReferencedMachineResources{
ImageID: "imageID",
},
},
Expand Down Expand Up @@ -530,7 +530,7 @@ var _ = Describe("OpenStackCluster controller", func() {
}
testCluster.Status = infrav1.OpenStackClusterStatus{
Bastion: &infrav1.BastionStatus{
DependentResources: infrav1.DependentMachineResources{
DependentResources: &infrav1.DependentMachineResources{
PortsStatus: []infrav1.PortStatus{
{
ID: "port-id",
Expand Down Expand Up @@ -613,7 +613,7 @@ var _ = Describe("OpenStackCluster controller", func() {
}
testCluster.Status = infrav1.OpenStackClusterStatus{
Bastion: &infrav1.BastionStatus{
DependentResources: infrav1.DependentMachineResources{
DependentResources: &infrav1.DependentMachineResources{
PortsStatus: []infrav1.PortStatus{
{
ID: "port-id",
Expand Down
Loading

0 comments on commit 9d40ba2

Please sign in to comment.