Skip to content

Commit c6efdea

Browse files
authored
Merge pull request #1959 from shiftstack/bastion-cleanup
⚠️ Bastion cleanup
2 parents 9809a60 + c321574 commit c6efdea

24 files changed

+303
-92
lines changed

api/v1alpha5/conversion.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -448,7 +448,7 @@ func Convert_v1alpha5_PortOpts_To_v1beta1_PortOpts(in *PortOpts, out *infrav1.Po
448448
}
449449

450450
func Convert_v1alpha5_Instance_To_v1beta1_BastionStatus(in *Instance, out *infrav1.BastionStatus, _ conversion.Scope) error {
451-
// BastionStatus is the same as Instance with unused fields removed
451+
// BastionStatus is the same as Spec with unused fields removed
452452
out.ID = in.ID
453453
out.Name = in.Name
454454
out.SSHKeyName = in.SSHKeyName
@@ -460,7 +460,7 @@ func Convert_v1alpha5_Instance_To_v1beta1_BastionStatus(in *Instance, out *infra
460460
}
461461

462462
func Convert_v1beta1_BastionStatus_To_v1alpha5_Instance(in *infrav1.BastionStatus, out *Instance, _ conversion.Scope) error {
463-
// BastionStatus is the same as Instance with unused fields removed
463+
// BastionStatus is the same as Spec with unused fields removed
464464
out.ID = in.ID
465465
out.Name = in.Name
466466
out.SSHKeyName = in.SSHKeyName
@@ -671,7 +671,9 @@ func Convert_v1beta1_Bastion_To_v1alpha5_Bastion(in *infrav1.Bastion, out *Basti
671671
if err != nil {
672672
return err
673673
}
674-
in.FloatingIP = out.Instance.FloatingIP
674+
if in.FloatingIP != nil {
675+
out.Instance.FloatingIP = *in.FloatingIP
676+
}
675677
return nil
676678
}
677679

@@ -680,7 +682,9 @@ func Convert_v1alpha5_Bastion_To_v1beta1_Bastion(in *Bastion, out *infrav1.Basti
680682
if err != nil {
681683
return err
682684
}
683-
in.Instance.FloatingIP = out.FloatingIP
685+
if in.Instance.FloatingIP != "" {
686+
out.FloatingIP = &in.Instance.FloatingIP
687+
}
684688
return nil
685689
}
686690

api/v1alpha5/zz_generated.conversion.go

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

api/v1alpha6/openstackcluster_conversion.go

Lines changed: 41 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -412,13 +412,18 @@ func Convert_v1alpha6_OpenStackClusterStatus_To_v1beta1_OpenStackClusterStatus(i
412412
/* Bastion */
413413

414414
func restorev1beta1Bastion(previous **infrav1.Bastion, dst **infrav1.Bastion) {
415-
if *previous != nil && *dst != nil {
416-
restorev1beta1MachineSpec(&(*previous).Instance, &(*dst).Instance)
415+
if *previous != nil {
416+
if *dst != nil && (*previous).Spec != nil && (*dst).Spec != nil {
417+
restorev1beta1MachineSpec((*previous).Spec, (*dst).Spec)
418+
}
419+
420+
optional.RestoreString(&(*previous).FloatingIP, &(*dst).FloatingIP)
421+
optional.RestoreString(&(*previous).AvailabilityZone, &(*dst).AvailabilityZone)
417422
}
418423
}
419424

420425
func Convert_v1alpha6_Instance_To_v1beta1_BastionStatus(in *Instance, out *infrav1.BastionStatus, _ apiconversion.Scope) error {
421-
// BastionStatus is the same as Instance with unused fields removed
426+
// BastionStatus is the same as Spec with unused fields removed
422427
out.ID = in.ID
423428
out.Name = in.Name
424429
out.SSHKeyName = in.SSHKeyName
@@ -429,7 +434,7 @@ func Convert_v1alpha6_Instance_To_v1beta1_BastionStatus(in *Instance, out *infra
429434
}
430435

431436
func Convert_v1beta1_BastionStatus_To_v1alpha6_Instance(in *infrav1.BastionStatus, out *Instance, _ apiconversion.Scope) error {
432-
// BastionStatus is the same as Instance with unused fields removed
437+
// BastionStatus is the same as Spec with unused fields removed
433438
out.ID = in.ID
434439
out.Name = in.Name
435440
out.SSHKeyName = in.SSHKeyName
@@ -445,13 +450,30 @@ func Convert_v1alpha6_Bastion_To_v1beta1_Bastion(in *Bastion, out *infrav1.Basti
445450
return err
446451
}
447452

448-
if in.Instance.ServerGroupID != "" {
449-
out.Instance.ServerGroup = &infrav1.ServerGroupFilter{ID: in.Instance.ServerGroupID}
450-
} else {
451-
out.Instance.ServerGroup = nil
453+
if !reflect.ValueOf(in.Instance).IsZero() {
454+
out.Spec = &infrav1.OpenStackMachineSpec{}
455+
456+
err = Convert_v1alpha6_OpenStackMachineSpec_To_v1beta1_OpenStackMachineSpec(&in.Instance, out.Spec, s)
457+
if err != nil {
458+
return err
459+
}
460+
461+
if in.Instance.ServerGroupID != "" {
462+
out.Spec.ServerGroup = &infrav1.ServerGroupFilter{ID: in.Instance.ServerGroupID}
463+
} else {
464+
out.Spec.ServerGroup = nil
465+
}
466+
467+
err = optional.Convert_string_To_optional_String(&in.Instance.FloatingIP, &out.FloatingIP, s)
468+
if err != nil {
469+
return err
470+
}
452471
}
453472

454-
out.FloatingIP = in.Instance.FloatingIP
473+
// nil the Spec if it's basically an empty object.
474+
if out.Spec != nil && reflect.ValueOf(*out.Spec).IsZero() {
475+
out.Spec = nil
476+
}
455477
return nil
456478
}
457479

@@ -461,10 +483,16 @@ func Convert_v1beta1_Bastion_To_v1alpha6_Bastion(in *infrav1.Bastion, out *Basti
461483
return err
462484
}
463485

464-
if in.Instance.ServerGroup != nil && in.Instance.ServerGroup.ID != "" {
465-
out.Instance.ServerGroupID = in.Instance.ServerGroup.ID
486+
if in.Spec != nil {
487+
err = Convert_v1beta1_OpenStackMachineSpec_To_v1alpha6_OpenStackMachineSpec(in.Spec, &out.Instance, s)
488+
if err != nil {
489+
return err
490+
}
491+
492+
if in.Spec.ServerGroup != nil && in.Spec.ServerGroup.ID != "" {
493+
out.Instance.ServerGroupID = in.Spec.ServerGroup.ID
494+
}
466495
}
467496

468-
out.Instance.FloatingIP = in.FloatingIP
469-
return nil
497+
return optional.Convert_optional_String_To_string(&in.FloatingIP, &out.Instance.FloatingIP, s)
470498
}

api/v1alpha6/openstackmachine_conversion.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,9 @@ func restorev1alpha6MachineSpec(previous *OpenStackMachineSpec, dst *OpenStackMa
133133
}
134134

135135
if kd != k || vd != v {
136+
if dst.ServerMetadata == nil {
137+
dst.ServerMetadata = make(map[string]string)
138+
}
136139
delete(dst.ServerMetadata, kd)
137140
dst.ServerMetadata[k] = v
138141
}

api/v1alpha6/zz_generated.conversion.go

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

api/v1alpha7/openstackcluster_conversion.go

Lines changed: 41 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ limitations under the License.
1717
package v1alpha7
1818

1919
import (
20+
"reflect"
21+
2022
apiconversion "k8s.io/apimachinery/pkg/conversion"
2123
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
2224
ctrlconversion "sigs.k8s.io/controller-runtime/pkg/conversion"
@@ -369,8 +371,13 @@ func restorev1alpha7Bastion(previous **Bastion, dst **Bastion) {
369371
}
370372

371373
func restorev1beta1Bastion(previous **infrav1.Bastion, dst **infrav1.Bastion) {
372-
if *previous != nil && *dst != nil {
373-
restorev1beta1MachineSpec(&(*previous).Instance, &(*dst).Instance)
374+
if *previous != nil {
375+
if *dst != nil && (*previous).Spec != nil && (*dst).Spec != nil {
376+
restorev1beta1MachineSpec((*previous).Spec, (*dst).Spec)
377+
}
378+
379+
optional.RestoreString(&(*previous).FloatingIP, &(*dst).FloatingIP)
380+
optional.RestoreString(&(*previous).AvailabilityZone, &(*dst).AvailabilityZone)
374381
}
375382
}
376383

@@ -380,13 +387,30 @@ func Convert_v1alpha7_Bastion_To_v1beta1_Bastion(in *Bastion, out *infrav1.Basti
380387
return err
381388
}
382389

383-
if in.Instance.ServerGroupID != "" {
384-
out.Instance.ServerGroup = &infrav1.ServerGroupFilter{ID: in.Instance.ServerGroupID}
385-
} else {
386-
out.Instance.ServerGroup = nil
390+
if !reflect.ValueOf(in.Instance).IsZero() {
391+
out.Spec = &infrav1.OpenStackMachineSpec{}
392+
393+
err = Convert_v1alpha7_OpenStackMachineSpec_To_v1beta1_OpenStackMachineSpec(&in.Instance, out.Spec, s)
394+
if err != nil {
395+
return err
396+
}
397+
398+
if in.Instance.ServerGroupID != "" {
399+
out.Spec.ServerGroup = &infrav1.ServerGroupFilter{ID: in.Instance.ServerGroupID}
400+
} else {
401+
out.Spec.ServerGroup = nil
402+
}
403+
404+
err = optional.Convert_string_To_optional_String(&in.Instance.FloatingIP, &out.FloatingIP, s)
405+
if err != nil {
406+
return err
407+
}
387408
}
388409

389-
out.FloatingIP = in.Instance.FloatingIP
410+
// nil the Spec if it's basically an empty object.
411+
if out.Spec != nil && reflect.ValueOf(*out.Spec).IsZero() {
412+
out.Spec = nil
413+
}
390414
return nil
391415
}
392416

@@ -396,12 +420,18 @@ func Convert_v1beta1_Bastion_To_v1alpha7_Bastion(in *infrav1.Bastion, out *Basti
396420
return err
397421
}
398422

399-
if in.Instance.ServerGroup != nil && in.Instance.ServerGroup.ID != "" {
400-
out.Instance.ServerGroupID = in.Instance.ServerGroup.ID
423+
if in.Spec != nil {
424+
err = Convert_v1beta1_OpenStackMachineSpec_To_v1alpha7_OpenStackMachineSpec(in.Spec, &out.Instance, s)
425+
if err != nil {
426+
return err
427+
}
428+
429+
if in.Spec.ServerGroup != nil && in.Spec.ServerGroup.ID != "" {
430+
out.Instance.ServerGroupID = in.Spec.ServerGroup.ID
431+
}
401432
}
402433

403-
out.Instance.FloatingIP = in.FloatingIP
404-
return nil
434+
return optional.Convert_optional_String_To_string(&in.FloatingIP, &out.Instance.FloatingIP, s)
405435
}
406436

407437
func Convert_v1beta1_BastionStatus_To_v1alpha7_BastionStatus(in *infrav1.BastionStatus, out *BastionStatus, s apiconversion.Scope) error {

api/v1alpha7/openstackmachine_conversion.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,9 @@ func restorev1alpha7MachineSpec(previous *OpenStackMachineSpec, dst *OpenStackMa
107107
}
108108

109109
if kd != k || vd != v {
110+
if dst.ServerMetadata == nil {
111+
dst.ServerMetadata = make(map[string]string)
112+
}
110113
delete(dst.ServerMetadata, kd)
111114
dst.ServerMetadata[k] = v
112115
}

api/v1alpha7/zz_generated.conversion.go

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

api/v1beta1/types.go

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -543,21 +543,27 @@ var (
543543
InstanceStateUndefined = InstanceState("")
544544
)
545545

546-
// Bastion represents basic information about the bastion node.
546+
// Bastion represents basic information about the bastion node. If you enable bastion, the spec has to be specified.
547+
// +kubebuilder:validation:XValidation:rule="!self.enabled || has(self.spec)",message="you need to specify the spec if bastion is enabled"
547548
type Bastion struct {
548-
//+optional
549+
// Enabled means that bastion is enabled. Defaults to false.
550+
// +kubebuilder:validation:Required
551+
// +kubebuilder:default:=false
549552
Enabled bool `json:"enabled"`
550553

551-
// Instance for the bastion itself
552-
Instance OpenStackMachineSpec `json:"instance,omitempty"`
554+
// Spec for the bastion itself
555+
Spec *OpenStackMachineSpec `json:"spec,omitempty"`
553556

557+
// AvailabilityZone is the failure domain that will be used to create the Bastion Spec.
554558
//+optional
555-
AvailabilityZone string `json:"availabilityZone,omitempty"`
559+
AvailabilityZone optional.String `json:"availabilityZone,omitempty"`
556560

557-
// FloatingIP which will be associated to the bastion machine.
558-
// The floating IP should already exist and should not be associated with a port.
561+
// FloatingIP which will be associated to the bastion machine. It's the IP address, not UUID.
562+
// The floating IP should already exist and should not be associated with a port. If FIP of this address does not
563+
// exist, CAPO will try to create it, but by default only OpenStack administrators have privileges to do so.
559564
//+optional
560-
FloatingIP string `json:"floatingIP,omitempty"`
565+
//+kubebuilder:validation:Format:=ipv4
566+
FloatingIP optional.String `json:"floatingIP,omitempty"`
561567
}
562568

563569
type APIServerLoadBalancer struct {

api/v1beta1/zz_generated.deepcopy.go

Lines changed: 15 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)