Skip to content

Commit a6732bc

Browse files
authored
Merge pull request kubernetes-sigs#1990 from shiftstack/bastionenabled
⚠️ Bastion is enabled by default if specified
2 parents 85ed836 + 7dd2c35 commit a6732bc

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)