@@ -27,6 +27,7 @@ import (
27
27
"os"
28
28
"os/exec"
29
29
"path/filepath"
30
+ "strings"
30
31
"time"
31
32
32
33
"github.com/coreos/ignition/v2/config/v3_6_experimental/types"
52
53
}
53
54
metadataServiceUrlIPv6 = url.URL {
54
55
Scheme : "http" ,
55
- Host : "[fe80::a9fe:a9fe%]" ,
56
+ Host : "[fe80::a9fe:a9fe%iface ]" ,
56
57
Path : "openstack/latest/user_data" ,
57
58
}
58
59
)
@@ -181,10 +182,10 @@ func isIPv6Address(ip net.IP) bool {
181
182
// findNetworkInterfaceWithIPv6 returns the name of the first network interface with an active IPv6 address.
182
183
// This interface name is needed to format the link-local address for accessing the IPv6 metadata service.
183
184
// For more details, see: https://docs.openstack.org/nova/2024.1/user/metadata.html
184
- func findNetworkInterfaceWithIPv6 () ( string , error ) {
185
+ func findNetworkInterfaceWithIPv6 () error {
185
186
interfaces , err := net .Interfaces ()
186
187
if err != nil {
187
- return "" , fmt .Errorf ("error fetching network interfaces: %v" , err )
188
+ return fmt .Errorf ("error fetching network interfaces: %v" , err )
188
189
}
189
190
190
191
for _ , iface := range interfaces {
@@ -201,46 +202,47 @@ func findNetworkInterfaceWithIPv6() (string, error) {
201
202
202
203
for _ , addr := range addrs {
203
204
if ipnet , ok := addr .(* net.IPNet ); ok && isIPv6Address (ipnet .IP ) {
204
- return iface .Name , nil
205
+ // Prepare the IPv6 metadata service URL
206
+ metadataServiceUrlIPv6 .Host = strings .Replace (metadataServiceUrlIPv6 .Host , "iface" , iface .Name , 1 )
207
+ return nil
205
208
}
206
209
}
207
210
}
208
- return "" , fmt .Errorf ("no active IPv6 network interface found" )
211
+ return fmt .Errorf ("no active IPv6 network interface found" )
209
212
}
210
213
211
214
// Fetches configuration from IPv4 and IPv6 metadata services
212
215
func fetchConfigFromMetadataService (f * resource.Fetcher ) ([]byte , error ) {
213
- var ipv4Res , ipv6Res []byte
214
- var ipv4Err , ipv6Err error
216
+ var response []byte
217
+ var err error
215
218
216
219
// Try fetching from IPv4 first
217
- ipv4Res , ipv4Err = f .FetchToBuffer (metadataServiceUrlIPv4 , resource.FetchOptions {})
218
- if ipv4Err == nil {
219
- fmt .Println ("Successfully fetched configuration from IPv4." )
220
- return ipv4Res , nil
221
- }
222
- fmt .Printf ("IPv4 fetch failed: %v. Attempting to fetch from IPv6...\n " , ipv4Err )
220
+ response , err = f .FetchToBuffer (metadataServiceUrlIPv4 , resource.FetchOptions {})
223
221
224
- // If IPv4 fails, find the network interface for IPv6
225
- interfaceName , err := findNetworkInterfaceWithIPv6 ()
226
222
if err != nil {
227
- fmt .Printf ("IPv6 metadata service lookup failed: %v\n " , err )
228
- return nil , fmt .Errorf ("both IPv4 and IPv6 lookup failed" )
229
- }
223
+ f .Logger .Info ("IPv4 fetch failed: %v. Attempting to fetch from IPv6...\n " , err )
224
+
225
+ // If IPv4 fails, find the network interface for IPv6
226
+ err := findNetworkInterfaceWithIPv6 ()
227
+ if err != nil {
228
+ f .Logger .Info ("IPv6 metadata service lookup failed: %v\n " , err )
229
+ return nil , fmt .Errorf ("both IPv4 and IPv6 lookup failed" )
230
+ }
231
+ f .Logger .Debug ("Fetching from IPv6 metadata service at %s...\n " , metadataServiceUrlIPv6 .String ())
230
232
231
- // Prepare the IPv6 metadata service URL
232
- metadataServiceUrlIPv6 .Host = fmt .Sprintf ("[%s%s]" , "fe80::a9fe:a9fe%" , interfaceName )
233
- metadataServiceUrlIPv6Str := fmt .Sprintf ("http://[%s%s]/openstack/latest/user_data" , metadataServiceUrlIPv6 .Host , interfaceName )
234
- fmt .Printf ("Fetching from IPv6 metadata service at %s...\n " , metadataServiceUrlIPv6Str )
233
+ // Try to fetch from IPv6
234
+ f .Logger .Debug ("IPv6 URL:" , metadataServiceUrlIPv6 .Host )
235
+ f .Logger .Debug ("Attempting to fetch from IPv6 metadata service at: %s\n " , metadataServiceUrlIPv6 .String ())
235
236
236
- // Try to fetch from IPv6
237
- fmt .Printf ("Attempting to fetch from IPv6 metadata service at: %s\n " , metadataServiceUrlIPv6Str )
237
+ response , err = f .FetchToBuffer (metadataServiceUrlIPv6 , resource.FetchOptions {})
238
+ if err != nil {
239
+ return nil , fmt .Errorf ("IPv4 and IPv6 services failed" )
240
+ }
238
241
239
- ipv6Res , ipv6Err = f .FetchToBuffer (metadataServiceUrlIPv6 , resource.FetchOptions {})
240
- if ipv6Err != nil {
241
- return nil , fmt .Errorf ("IPv4 and IPv6 services failed" )
242
+ f .Logger .Debug ("Successfully fetched configuration from IPv6." )
243
+ return response , nil
242
244
}
243
245
244
- fmt . Println ( "Successfully fetched configuration from IPv6." )
245
- return ipv6Res , nil
246
+ return response , nil
247
+
246
248
}
0 commit comments