Skip to content

Commit 9816344

Browse files
Merge pull request #426 from tthvo/CORS-4209
CORS-4209: ensure NodeIPFamilies field is kept as-is after transforming
2 parents 3413151 + b8b3cc5 commit 9816344

2 files changed

Lines changed: 59 additions & 11 deletions

File tree

pkg/cloud/aws/aws_config_transformer.go

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -66,16 +66,6 @@ func marshalAWSConfig(cfg *awsconfig.CloudConfig) (string, error) {
6666
}
6767
}
6868

69-
for _, section := range file.Sections() {
70-
for key, value := range section.KeysHash() {
71-
// Ignore anything that is the zero value for its type.
72-
// Everything appears as a string in the INI file, so 0 and false are also considered zero values.
73-
if value == "" {
74-
section.DeleteKey(key)
75-
}
76-
}
77-
}
78-
7969
// Ensure service override sections are last and ordered numerically.
8070
// We need to create a new file with sorted sections because file.Sections()
8171
// returns a new slice each time, so sorting it doesn't modify the original.
@@ -85,7 +75,8 @@ func marshalAWSConfig(cfg *awsconfig.CloudConfig) (string, error) {
8575
})
8676

8777
// Create a new INI file with sections in sorted order
88-
sortedFile := ini.Empty()
78+
// Configure iniv1 to allow shadow fields to enable multiple entries of NodeIPFamilies.
79+
sortedFile := ini.Empty(ini.LoadOptions{AllowShadows: true})
8980
for _, section := range sections {
9081
newSection, err := sortedFile.NewSection(section.Name())
9182
if err != nil {
@@ -96,6 +87,35 @@ func marshalAWSConfig(cfg *awsconfig.CloudConfig) (string, error) {
9687
}
9788
}
9889

90+
// In dual-stack environment, the CCM expects NodeIPFamilies to be in the format:
91+
//
92+
// NodeIPFamilies=ipv4
93+
// NodeIPFamilies=ipv6
94+
//
95+
// However, iniv1 is serializing go slices as comma-separated list, for example:
96+
//
97+
// NodeIPFamilies=ipv4,ipv6
98+
//
99+
// Below logic ensures the original NodeIPFamilies field is kept as-is after transforming.
100+
nodeIPKey := sortedFile.Section("Global").Key("NodeIPFamilies")
101+
for i, ipFamily := range cfg.Global.NodeIPFamilies {
102+
if i == 0 {
103+
nodeIPKey.SetValue(ipFamily)
104+
} else if err := nodeIPKey.AddShadow(ipFamily); err != nil {
105+
return "", fmt.Errorf("failed to set NodeIPFamilies: %w", err)
106+
}
107+
}
108+
109+
for _, section := range sortedFile.Sections() {
110+
for key, value := range section.KeysHash() {
111+
// Ignore anything that is the zero value for its type.
112+
// Everything appears as a string in the INI file, so 0 and false are also considered zero values.
113+
if value == "" {
114+
section.DeleteKey(key)
115+
}
116+
}
117+
}
118+
99119
buf := &bytes.Buffer{}
100120

101121
if _, err := sortedFile.WriteTo(buf); err != nil {

pkg/cloud/aws/aws_config_transformer_test.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,34 @@ ClusterServiceSharedLoadBalancerHealthProbePort = 0
183183
`,
184184
features: mockDisabledFeatureGates,
185185
},
186+
{
187+
name: "with NodeIPFamilies with ipv4 first",
188+
source: `[Global]
189+
NodeIPFamilies = ipv4
190+
NodeIPFamilies = ipv6
191+
`,
192+
expected: `[Global]
193+
DisableSecurityGroupIngress = false
194+
NodeIPFamilies = ipv4
195+
NodeIPFamilies = ipv6
196+
ClusterServiceLoadBalancerHealthProbeMode = Shared
197+
ClusterServiceSharedLoadBalancerHealthProbePort = 0
198+
`,
199+
},
200+
{
201+
name: "with NodeIPFamilies with ipv6 first",
202+
source: `[Global]
203+
NodeIPFamilies = ipv6
204+
NodeIPFamilies = ipv4
205+
`,
206+
expected: `[Global]
207+
DisableSecurityGroupIngress = false
208+
NodeIPFamilies = ipv6
209+
NodeIPFamilies = ipv4
210+
ClusterServiceLoadBalancerHealthProbeMode = Shared
211+
ClusterServiceSharedLoadBalancerHealthProbePort = 0
212+
`,
213+
},
186214
}
187215

188216
for _, tc := range testCases {

0 commit comments

Comments
 (0)