Skip to content

Commit 7dd2c35

Browse files
committed
Bastion is enabled by default if specified
Bastion.Enabled is a wart. Ideally it would not be required, but because of limitations in how we delete the bastion we require an intermediate 'disabled' step before removal. Ultimately we intend to remove this limitation. Eventually we would like to be able to ignore enabled entirely. e.g. To create a Bastion just specify it: ```yaml spec: bastion: spec: ... floatingIP: x.x.x.x ``` and to delete it just remove the bastion field. Right now with enabled defaulting to `false`, doing the above will not result in the creation of a bastion, because enabled must be explicitly set to true. Paving the way for the eventual deprecation of Bastion.Enabled, we change the default value of enabled to be true so the above does today what we eventually want it to do. This is also generally more intuitive: why would you include a bastion and a spec if you didn't want to create a bastion? Having to also set enabled to true is currently a trip hazard. Until we resolve the limitations of bastion deletion, though, we still need to be able to disable the bastion. For this case enabled can be explicitly set to false. In the future when we remove the requirement to disable the bastion before deletion the user can simply ignore Bastion.Enabled, which will continue to work but without the limitations.
1 parent 4e2150f commit 7dd2c35

19 files changed

+118
-56
lines changed

api/v1alpha5/zz_generated.conversion.go

Lines changed: 6 additions & 2 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: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -416,6 +416,7 @@ func restorev1beta1Bastion(previous **infrav1.Bastion, dst **infrav1.Bastion) {
416416

417417
optional.RestoreString(&(*previous).FloatingIP, &(*dst).FloatingIP)
418418
optional.RestoreString(&(*previous).AvailabilityZone, &(*dst).AvailabilityZone)
419+
optional.RestoreBool(&(*previous).Enabled, &(*dst).Enabled)
419420
}
420421

421422
func Convert_v1alpha6_Bastion_To_v1beta1_Bastion(in *Bastion, out *infrav1.Bastion, s apiconversion.Scope) error {

api/v1alpha6/zz_generated.conversion.go

Lines changed: 6 additions & 2 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: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -384,6 +384,7 @@ func restorev1beta1Bastion(previous **infrav1.Bastion, dst **infrav1.Bastion) {
384384

385385
optional.RestoreString(&(*previous).FloatingIP, &(*dst).FloatingIP)
386386
optional.RestoreString(&(*previous).AvailabilityZone, &(*dst).AvailabilityZone)
387+
optional.RestoreBool(&(*previous).Enabled, &(*dst).Enabled)
387388
}
388389
}
389390

api/v1alpha7/zz_generated.conversion.go

Lines changed: 6 additions & 2 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: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -719,12 +719,18 @@ var (
719719
)
720720

721721
// Bastion represents basic information about the bastion node. If you enable bastion, the spec has to be specified.
722-
// +kubebuilder:validation:XValidation:rule="!self.enabled || has(self.spec)",message="you need to specify the spec if bastion is enabled"
722+
// +kubebuilder:validation:XValidation:rule="!self.enabled || has(self.spec)",message="spec is required if bastion is enabled"
723723
type Bastion struct {
724-
// Enabled means that bastion is enabled. Defaults to false.
725-
// +kubebuilder:validation:Required
726-
// +kubebuilder:default:=false
727-
Enabled bool `json:"enabled"`
724+
// Enabled means that bastion is enabled. The bastion is enabled by
725+
// default if this field is not specified. Set this field to false to disable the
726+
// bastion.
727+
//
728+
// It is not currently possible to remove the bastion from the cluster
729+
// spec without first disabling it by setting this field to false and
730+
// waiting until the bastion has been deleted.
731+
// +kubebuilder:default:=true
732+
// +optional
733+
Enabled optional.Bool `json:"enabled,omitempty"`
728734

729735
// Spec for the bastion itself
730736
Spec *OpenStackMachineSpec `json:"spec,omitempty"`
@@ -741,6 +747,13 @@ type Bastion struct {
741747
FloatingIP optional.String `json:"floatingIP,omitempty"`
742748
}
743749

750+
func (b *Bastion) IsEnabled() bool {
751+
if b == nil {
752+
return false
753+
}
754+
return b.Enabled == nil || *b.Enabled
755+
}
756+
744757
type APIServerLoadBalancer struct {
745758
// Enabled defines whether a load balancer should be created. This value
746759
// defaults to true if an APIServerLoadBalancer is given.

api/v1beta1/zz_generated.deepcopy.go

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

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

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

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

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

controllers/openstackcluster_controller.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ func contains(arr []string, target string) bool {
219219

220220
func resolveBastionResources(scope *scope.WithLogger, clusterResourceName string, openStackCluster *infrav1.OpenStackCluster) (bool, error) {
221221
// Resolve and store resources for the bastion
222-
if openStackCluster.Spec.Bastion != nil && openStackCluster.Spec.Bastion.Enabled {
222+
if openStackCluster.Spec.Bastion.IsEnabled() {
223223
if openStackCluster.Status.Bastion == nil {
224224
openStackCluster.Status.Bastion = &infrav1.BastionStatus{}
225225
}
@@ -406,7 +406,7 @@ func reconcileBastion(scope *scope.WithLogger, cluster *clusterv1.Cluster, openS
406406
}
407407

408408
// No Bastion defined
409-
if openStackCluster.Spec.Bastion == nil || !openStackCluster.Spec.Bastion.Enabled {
409+
if !openStackCluster.Spec.Bastion.IsEnabled() {
410410
// Delete any existing bastion
411411
if openStackCluster.Status.Bastion != nil {
412412
if err := deleteBastion(scope, cluster, openStackCluster); err != nil {
@@ -440,6 +440,8 @@ func reconcileBastion(scope *scope.WithLogger, cluster *clusterv1.Cluster, openS
440440
return nil, fmt.Errorf("failed computing bastion hash from instance spec: %w", err)
441441
}
442442
if bastionHashHasChanged(bastionHash, openStackCluster.ObjectMeta.Annotations) {
443+
scope.Logger().Info("Bastion instance spec has changed, deleting existing bastion")
444+
443445
if err := deleteBastion(scope, cluster, openStackCluster); err != nil {
444446
return nil, err
445447
}
@@ -544,7 +546,7 @@ func bastionAddFloatingIP(openStackCluster *infrav1.OpenStackCluster, clusterRes
544546
func bastionToInstanceSpec(openStackCluster *infrav1.OpenStackCluster, cluster *clusterv1.Cluster) (*compute.InstanceSpec, error) {
545547
bastion := openStackCluster.Spec.Bastion
546548
if bastion == nil {
547-
return nil, fmt.Errorf("bastion spec is nil")
549+
bastion = &infrav1.Bastion{}
548550
}
549551
if bastion.Spec == nil {
550552
// For the case when Bastion is deleted but we don't have spec, let's use an empty one.

0 commit comments

Comments
 (0)