tinkerbell / hegel

Instance Metadata Service
https://tinkerbell.org
Apache License 2.0
97 stars 32 forks source link

PublicIPv4 of EC2Instance always takes 1st IP address from Hardware #325

Closed rockc2020 closed 8 months ago

rockc2020 commented 8 months ago

In my tinkerbell stack k8s cluster for CAPT, I had a Hardware resource which includes the follow fields:

  metadata:
    instance:
      hostname: host-01
      id: 10:70:fd:19:e6:56
      ips:
        - address: 10.52.107.72
          family: 4
          gateway: 10.52.107.65
          netmask: 255.255.255.192
          public: false
        - address: 69.19.7.42
          family: 4
          gateway: 69.19.7.33
          netmask: 255.255.255.224
          public: true

When the hegel was called with local-ipv4 and public-ipv4, it always returns the local one 10.52.107.72.

Expected Behaviour

Current Behaviour

Possible Solution

In the backend.go file, it should take values from the ip iteratively in the loop.

Current code https://github.com/tinkerbell/hegel/blob/main/internal/backend/kubernetes/backend.go#L185-L200

                for _, ip := range hw.Spec.Metadata.Instance.Ips {
                        // Public IPv4
                        if ip.Family == 4 && ip.Public && i.Metadata.PublicIPv4 == "" {
                                i.Metadata.PublicIPv4 = hw.Spec.Metadata.Instance.Ips[0].Address
                        }

                        // Private IPv4
                        if ip.Family == 4 && !ip.Public && i.Metadata.LocalIPv4 == "" {
                                i.Metadata.LocalIPv4 = hw.Spec.Metadata.Instance.Ips[0].Address
                        }

                        // Public IPv6
                        if ip.Family == 6 && i.Metadata.PublicIPv6 == "" {
                                i.Metadata.PublicIPv6 = hw.Spec.Metadata.Instance.Ips[0].Address
                        }
                }

Possible fix

                for _, ip := range hw.Spec.Metadata.Instance.Ips {
                        // Public IPv4
                        if ip.Family == 4 && ip.Public && i.Metadata.PublicIPv4 == "" {
                                i.Metadata.PublicIPv4 = ip.Address
                        }

                        // Private IPv4
                        if ip.Family == 4 && !ip.Public && i.Metadata.LocalIPv4 == "" {
                                i.Metadata.LocalIPv4 = ip.Address
                        }

                        // Public IPv6
                        if ip.Family == 6 && i.Metadata.PublicIPv6 == "" {
                                i.Metadata.PublicIPv6 = ip.Address
                        }
                }

Steps to Reproduce (for bugs)

It's simple as described in the summary.

Context

Your Environment

chrisdoherty4 commented 8 months ago

@rockc2020 Thank you for raising the bug. We'll look into it shortly.