Skip to content

Commit

Permalink
Move CloudName into IdentityRef and make cluster IdentityRef required
Browse files Browse the repository at this point in the history
This change came from attempting to write validation markers for
CloudName and IdentityRef in both the machine and cluster specs.

Firstly I noticed that IdentityRef was marked optional in the cluster
spec, but it is certainly required: the cluster cannot be provisioned
without cloud credentials. I made IdentityRef required in the cluster
spec.

In contrast, IdentityRef is genuinely optional in the machine spec
because, if not specified, we will use the credentials defined in the
cluster spec.

CloudName on the machine spec is also marked optional. However, it is
required if IdentityRef was specified. This is because it refers to the
same object as IdentityRef. The most sensible way to to represent this
in the API is to put it in the IdentityRef. This means that if
IdentityRef is provided, it must be provided completely, including
CloudName.
  • Loading branch information
mdbooth committed Feb 29, 2024
1 parent 9e00969 commit 564b6bd
Show file tree
Hide file tree
Showing 29 changed files with 458 additions and 401 deletions.
28 changes: 28 additions & 0 deletions api/v1alpha5/conversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,9 @@ func Convert_v1beta1_OpenStackClusterSpec_To_v1alpha5_OpenStackClusterSpec(in *i
out.AllowAllInClusterTraffic = in.ManagedSecurityGroups.AllowAllInClusterTraffic
}

out.CloudName = in.IdentityRef.CloudName
out.IdentityRef = &OpenStackIdentityReference{Name: in.IdentityRef.Name}

return nil
}

Expand Down Expand Up @@ -258,6 +261,11 @@ func Convert_v1alpha5_OpenStackClusterSpec_To_v1beta1_OpenStackClusterSpec(in *O
}
}

out.IdentityRef.CloudName = in.CloudName
if in.IdentityRef != nil {
out.IdentityRef.Name = in.IdentityRef.Name
}

return nil
}

Expand Down Expand Up @@ -321,6 +329,16 @@ func Convert_v1alpha5_OpenStackMachineSpec_To_v1beta1_OpenStackMachineSpec(in *O
}
out.Image = imageFilter

if in.IdentityRef != nil {
out.IdentityRef = &infrav1.OpenStackIdentityReference{Name: in.IdentityRef.Name}
}
if in.CloudName != "" {
if out.IdentityRef == nil {
out.IdentityRef = &infrav1.OpenStackIdentityReference{}
}
out.IdentityRef.CloudName = in.CloudName
}

return nil
}

Expand Down Expand Up @@ -606,6 +624,11 @@ func Convert_v1beta1_OpenStackMachineSpec_To_v1alpha5_OpenStackMachineSpec(in *i
out.ImageUUID = in.Image.ID
}

if in.IdentityRef != nil {
out.IdentityRef = &OpenStackIdentityReference{Name: in.IdentityRef.Name}
out.CloudName = in.IdentityRef.CloudName
}

return nil
}

Expand Down Expand Up @@ -690,3 +713,8 @@ func Convert_v1alpha5_SecurityGroup_To_v1beta1_SecurityGroupStatus(in *SecurityG
func Convert_v1alpha5_OpenStackIdentityReference_To_v1beta1_OpenStackIdentityReference(in *OpenStackIdentityReference, out *infrav1.OpenStackIdentityReference, s conversion.Scope) error {
return autoConvert_v1alpha5_OpenStackIdentityReference_To_v1beta1_OpenStackIdentityReference(in, out, s)
}

func Convert_v1beta1_OpenStackIdentityReference_To_v1alpha5_OpenStackIdentityReference(in *infrav1.OpenStackIdentityReference, out *OpenStackIdentityReference, _ conversion.Scope) error {
out.Name = in.Name
return nil
}
20 changes: 14 additions & 6 deletions api/v1alpha5/conversion_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,12 @@ func TestConvertFrom(t *testing.T) {
Spec: infrav1.OpenStackClusterSpec{},
},
want: &OpenStackCluster{
Spec: OpenStackClusterSpec{},
Spec: OpenStackClusterSpec{
IdentityRef: &OpenStackIdentityReference{},
},
ObjectMeta: metav1.ObjectMeta{
Annotations: map[string]string{
"cluster.x-k8s.io/conversion-data": "{\"spec\":{\"apiServerLoadBalancer\":{},\"cloudName\":\"\",\"controlPlaneEndpoint\":{\"host\":\"\",\"port\":0},\"disableAPIServerFloatingIP\":false,\"disableExternalNetwork\":false,\"externalNetwork\":{},\"managedSecurityGroups\":null,\"network\":{}},\"status\":{\"ready\":false}}",
"cluster.x-k8s.io/conversion-data": "{\"spec\":{\"apiServerLoadBalancer\":{},\"controlPlaneEndpoint\":{\"host\":\"\",\"port\":0},\"disableAPIServerFloatingIP\":false,\"disableExternalNetwork\":false,\"externalNetwork\":{},\"identityRef\":{\"cloudName\":\"\",\"name\":\"\"},\"managedSecurityGroups\":null,\"network\":{}},\"status\":{\"ready\":false}}",
},
},
},
Expand All @@ -61,10 +63,16 @@ func TestConvertFrom(t *testing.T) {
Spec: infrav1.OpenStackClusterTemplateSpec{},
},
want: &OpenStackClusterTemplate{
Spec: OpenStackClusterTemplateSpec{},
Spec: OpenStackClusterTemplateSpec{
Template: OpenStackClusterTemplateResource{
Spec: OpenStackClusterSpec{
IdentityRef: &OpenStackIdentityReference{},
},
},
},
ObjectMeta: metav1.ObjectMeta{
Annotations: map[string]string{
"cluster.x-k8s.io/conversion-data": "{\"spec\":{\"template\":{\"spec\":{\"apiServerLoadBalancer\":{},\"cloudName\":\"\",\"controlPlaneEndpoint\":{\"host\":\"\",\"port\":0},\"disableAPIServerFloatingIP\":false,\"disableExternalNetwork\":false,\"externalNetwork\":{},\"managedSecurityGroups\":null,\"network\":{}}}}}",
"cluster.x-k8s.io/conversion-data": "{\"spec\":{\"template\":{\"spec\":{\"apiServerLoadBalancer\":{},\"controlPlaneEndpoint\":{\"host\":\"\",\"port\":0},\"disableAPIServerFloatingIP\":false,\"disableExternalNetwork\":false,\"externalNetwork\":{},\"identityRef\":{\"cloudName\":\"\",\"name\":\"\"},\"managedSecurityGroups\":null,\"network\":{}}}}}",
},
},
},
Expand All @@ -79,7 +87,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\":{\"flavor\":\"\",\"image\":{}},\"status\":{\"dependentResources\":{},\"ready\":false,\"referencedResources\":{}}}",
},
},
},
Expand All @@ -94,7 +102,7 @@ func TestConvertFrom(t *testing.T) {
Spec: OpenStackMachineTemplateSpec{},
ObjectMeta: metav1.ObjectMeta{
Annotations: map[string]string{
"cluster.x-k8s.io/conversion-data": "{\"spec\":{\"template\":{\"spec\":{\"cloudName\":\"\",\"flavor\":\"\",\"image\":{}}}}}",
"cluster.x-k8s.io/conversion-data": "{\"spec\":{\"template\":{\"spec\":{\"flavor\":\"\",\"image\":{}}}}}",
},
},
},
Expand Down
42 changes: 10 additions & 32 deletions api/v1alpha5/zz_generated.conversion.go

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

28 changes: 28 additions & 0 deletions api/v1alpha6/conversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -529,6 +529,16 @@ func Convert_v1alpha6_OpenStackMachineSpec_To_v1beta1_OpenStackMachineSpec(in *O
out.ServerMetadata = serverMetadata
}

if in.IdentityRef != nil {
out.IdentityRef = &infrav1.OpenStackIdentityReference{Name: in.IdentityRef.Name}
}
if in.CloudName != "" {
if out.IdentityRef == nil {
out.IdentityRef = &infrav1.OpenStackIdentityReference{}
}
out.IdentityRef.CloudName = in.CloudName
}

return nil
}

Expand Down Expand Up @@ -624,6 +634,9 @@ func Convert_v1beta1_OpenStackClusterSpec_To_v1alpha6_OpenStackClusterSpec(in *i
out.AllowAllInClusterTraffic = in.ManagedSecurityGroups.AllowAllInClusterTraffic
}

out.CloudName = in.IdentityRef.CloudName
out.IdentityRef = &OpenStackIdentityReference{Name: in.IdentityRef.Name}

return nil
}

Expand Down Expand Up @@ -667,6 +680,11 @@ func Convert_v1alpha6_OpenStackClusterSpec_To_v1beta1_OpenStackClusterSpec(in *O
}
}

out.IdentityRef.CloudName = in.CloudName
if in.IdentityRef != nil {
out.IdentityRef.Name = in.IdentityRef.Name
}

return nil
}

Expand Down Expand Up @@ -973,6 +991,11 @@ func Convert_v1beta1_OpenStackMachineSpec_To_v1alpha6_OpenStackMachineSpec(in *i
out.ServerMetadata = serverMetadata
}

if in.IdentityRef != nil {
out.IdentityRef = &OpenStackIdentityReference{Name: in.IdentityRef.Name}
out.CloudName = in.IdentityRef.CloudName
}

return nil
}

Expand Down Expand Up @@ -1069,3 +1092,8 @@ func Convert_v1alpha6_SecurityGroup_To_v1beta1_SecurityGroupStatus(in *SecurityG
func Convert_v1alpha6_OpenStackIdentityReference_To_v1beta1_OpenStackIdentityReference(in *OpenStackIdentityReference, out *infrav1.OpenStackIdentityReference, s apiconversion.Scope) error {
return autoConvert_v1alpha6_OpenStackIdentityReference_To_v1beta1_OpenStackIdentityReference(in, out, s)
}

func Convert_v1beta1_OpenStackIdentityReference_To_v1alpha6_OpenStackIdentityReference(in *infrav1.OpenStackIdentityReference, out *OpenStackIdentityReference, _ apiconversion.Scope) error {
out.Name = in.Name
return nil
}
Loading

0 comments on commit 564b6bd

Please sign in to comment.