Skip to content

Commit c54e5f0

Browse files
EmilienMMatthew Booth
and
Matthew Booth
committed
AllNodes security groups API
Co-Authored-By: Emilien Macchi <[email protected]> Co-Authored-By: Matthew Booth <[email protected]>
1 parent 05abe55 commit c54e5f0

35 files changed

+2180
-573
lines changed

api/v1alpha5/conversion.go

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"strings"
2121

2222
conversion "k8s.io/apimachinery/pkg/conversion"
23+
"k8s.io/utils/pointer"
2324
utilconversion "sigs.k8s.io/cluster-api/util/conversion"
2425
ctrlconversion "sigs.k8s.io/controller-runtime/pkg/conversion"
2526

@@ -209,6 +210,10 @@ func Convert_v1alpha8_OpenStackClusterSpec_To_v1alpha5_OpenStackClusterSpec(in *
209210
}
210211
}
211212

213+
if in.ManagedSecurityGroups != nil {
214+
out.ManagedSecurityGroups = true
215+
}
216+
212217
return nil
213218
}
214219

@@ -243,6 +248,13 @@ func Convert_v1alpha5_OpenStackClusterSpec_To_v1alpha8_OpenStackClusterSpec(in *
243248
}
244249
// We're dropping DNSNameservers even if these were set as without NodeCIDR it doesn't make sense.
245250

251+
if in.ManagedSecurityGroups {
252+
out.ManagedSecurityGroups = &infrav1.ManagedSecurityGroups{}
253+
if !in.AllowAllInClusterTraffic {
254+
out.ManagedSecurityGroups.AllNodesSecurityGroupRules = infrav1.LegacyCalicoSecurityGroupRules()
255+
}
256+
}
257+
246258
return nil
247259
}
248260

@@ -554,3 +566,58 @@ func Convert_v1alpha5_Bastion_To_v1alpha8_Bastion(in *Bastion, out *infrav1.Bast
554566
in.Instance.FloatingIP = out.FloatingIP
555567
return nil
556568
}
569+
570+
func Convert_v1alpha8_SecurityGroupStatus_To_v1alpha5_SecurityGroup(in *infrav1.SecurityGroupStatus, out *SecurityGroup, s conversion.Scope) error { //nolint:revive
571+
out.ID = in.ID
572+
out.Name = in.Name
573+
out.Rules = make([]SecurityGroupRule, len(in.Rules))
574+
for i, rule := range in.Rules {
575+
out.Rules[i] = SecurityGroupRule{
576+
ID: rule.ID,
577+
Direction: rule.Direction,
578+
}
579+
if rule.Description != nil {
580+
out.Rules[i].Description = *rule.Description
581+
}
582+
if rule.EtherType != nil {
583+
out.Rules[i].EtherType = *rule.EtherType
584+
}
585+
if rule.PortRangeMin != nil {
586+
out.Rules[i].PortRangeMin = *rule.PortRangeMin
587+
}
588+
if rule.PortRangeMax != nil {
589+
out.Rules[i].PortRangeMax = *rule.PortRangeMax
590+
}
591+
if rule.Protocol != nil {
592+
out.Rules[i].Protocol = *rule.Protocol
593+
}
594+
if rule.RemoteGroupID != nil {
595+
out.Rules[i].RemoteGroupID = *rule.RemoteGroupID
596+
}
597+
if rule.RemoteIPPrefix != nil {
598+
out.Rules[i].RemoteIPPrefix = *rule.RemoteIPPrefix
599+
}
600+
}
601+
return nil
602+
}
603+
604+
func Convert_v1alpha5_SecurityGroup_To_v1alpha8_SecurityGroupStatus(in *SecurityGroup, out *infrav1.SecurityGroupStatus, s conversion.Scope) error { //nolint:revive
605+
out.ID = in.ID
606+
out.Name = in.Name
607+
out.Rules = make([]infrav1.SecurityGroupRuleStatus, len(in.Rules))
608+
for i, rule := range in.Rules {
609+
out.Rules[i] = infrav1.SecurityGroupRuleStatus{
610+
ID: rule.ID,
611+
Description: pointer.String(rule.Description),
612+
Direction: rule.Direction,
613+
EtherType: pointer.String(rule.EtherType),
614+
PortRangeMin: pointer.Int(rule.PortRangeMin),
615+
PortRangeMax: pointer.Int(rule.PortRangeMax),
616+
Protocol: pointer.String(rule.Protocol),
617+
RemoteGroupID: pointer.String(rule.RemoteGroupID),
618+
RemoteIPPrefix: pointer.String(rule.RemoteIPPrefix),
619+
}
620+
}
621+
622+
return nil
623+
}

api/v1alpha5/conversion_test.go

Lines changed: 49 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ func TestConvertFrom(t *testing.T) {
4949
Spec: OpenStackClusterSpec{},
5050
ObjectMeta: metav1.ObjectMeta{
5151
Annotations: map[string]string{
52-
"cluster.x-k8s.io/conversion-data": "{\"spec\":{\"allowAllInClusterTraffic\":false,\"apiServerLoadBalancer\":{},\"cloudName\":\"\",\"controlPlaneEndpoint\":{\"host\":\"\",\"port\":0},\"disableAPIServerFloatingIP\":false,\"disableExternalNetwork\":false,\"externalNetwork\":{},\"managedSecurityGroups\":false,\"network\":{}},\"status\":{\"ready\":false}}",
52+
"cluster.x-k8s.io/conversion-data": "{\"spec\":{\"allowAllInClusterTraffic\":false,\"apiServerLoadBalancer\":{},\"cloudName\":\"\",\"controlPlaneEndpoint\":{\"host\":\"\",\"port\":0},\"disableAPIServerFloatingIP\":false,\"disableExternalNetwork\":false,\"externalNetwork\":{},\"managedSecurityGroups\":null,\"network\":{}},\"status\":{\"ready\":false}}",
5353
},
5454
},
5555
},
@@ -64,7 +64,7 @@ func TestConvertFrom(t *testing.T) {
6464
Spec: OpenStackClusterTemplateSpec{},
6565
ObjectMeta: metav1.ObjectMeta{
6666
Annotations: map[string]string{
67-
"cluster.x-k8s.io/conversion-data": "{\"spec\":{\"template\":{\"spec\":{\"allowAllInClusterTraffic\":false,\"apiServerLoadBalancer\":{},\"cloudName\":\"\",\"controlPlaneEndpoint\":{\"host\":\"\",\"port\":0},\"disableAPIServerFloatingIP\":false,\"disableExternalNetwork\":false,\"externalNetwork\":{},\"managedSecurityGroups\":false,\"network\":{}}}}}",
67+
"cluster.x-k8s.io/conversion-data": "{\"spec\":{\"template\":{\"spec\":{\"allowAllInClusterTraffic\":false,\"apiServerLoadBalancer\":{},\"cloudName\":\"\",\"controlPlaneEndpoint\":{\"host\":\"\",\"port\":0},\"disableAPIServerFloatingIP\":false,\"disableExternalNetwork\":false,\"externalNetwork\":{},\"managedSecurityGroups\":null,\"network\":{}}}}}",
6868
},
6969
},
7070
},
@@ -109,3 +109,50 @@ func TestConvertFrom(t *testing.T) {
109109
})
110110
}
111111
}
112+
113+
func TestConvert_v1alpha5_OpenStackClusterSpec_To_v1alpha8_OpenStackClusterSpec(t *testing.T) {
114+
tests := []struct {
115+
name string
116+
in *OpenStackClusterSpec
117+
expectedOut *infrav1.OpenStackClusterSpec
118+
}{
119+
{
120+
name: "empty",
121+
in: &OpenStackClusterSpec{},
122+
expectedOut: &infrav1.OpenStackClusterSpec{},
123+
},
124+
{
125+
name: "with managed security groups and not allow all in cluster traffic",
126+
in: &OpenStackClusterSpec{
127+
ManagedSecurityGroups: true,
128+
AllowAllInClusterTraffic: false,
129+
},
130+
expectedOut: &infrav1.OpenStackClusterSpec{
131+
ManagedSecurityGroups: &infrav1.ManagedSecurityGroups{
132+
AllNodesSecurityGroupRules: infrav1.LegacyCalicoSecurityGroupRules(),
133+
},
134+
},
135+
},
136+
{
137+
name: "with managed security groups and allow all in cluster traffic",
138+
in: &OpenStackClusterSpec{
139+
ManagedSecurityGroups: true,
140+
AllowAllInClusterTraffic: true,
141+
},
142+
expectedOut: &infrav1.OpenStackClusterSpec{
143+
ManagedSecurityGroups: &infrav1.ManagedSecurityGroups{},
144+
AllowAllInClusterTraffic: true,
145+
},
146+
},
147+
}
148+
149+
for _, tt := range tests {
150+
t.Run(tt.name, func(t *testing.T) {
151+
g := gomega.NewWithT(t)
152+
out := &infrav1.OpenStackClusterSpec{}
153+
err := Convert_v1alpha5_OpenStackClusterSpec_To_v1alpha8_OpenStackClusterSpec(tt.in, out, nil)
154+
g.Expect(err).NotTo(gomega.HaveOccurred())
155+
g.Expect(out).To(gomega.Equal(tt.expectedOut))
156+
})
157+
}
158+
}

api/v1alpha5/zz_generated.conversion.go

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

0 commit comments

Comments
 (0)