Fix AddressPool conversion webhook
We must check the AutoAssign and aggregationLength pointers
are set before using them.
@@ -4,6 +4,7 @@ package v1alpha1
import (
"go.universe.tf/metallb/api/v1beta1"
+ "go.universe.tf/metallb/internal/pointer"
"sigs.k8s.io/controller-runtime/pkg/conversion"
)
@@ -13,11 +14,17 @@ func (src *AddressPool) ConvertTo(dstRaw conversion.Hub) error {
dst.Spec.Protocol = src.Spec.Protocol
dst.Spec.Addresses = make([]string, len(src.Spec.Addresses))
copy(dst.Spec.Addresses, src.Spec.Addresses)
- *dst.Spec.AutoAssign = *src.Spec.AutoAssign
- dst.Spec.BGPAdvertisements = make([]v1beta1.LegacyBgpAdvertisement, 0)
+ if src.Spec.AutoAssign != nil {
+ dst.Spec.AutoAssign = pointer.BoolPtr(*src.Spec.AutoAssign)
+ }
+ dst.Spec.BGPAdvertisements = make([]v1beta1.LegacyBgpAdvertisement, len(src.Spec.BGPAdvertisements))
for i, adv := range src.Spec.BGPAdvertisements {
- *dst.Spec.BGPAdvertisements[i].AggregationLength = *adv.AggregationLength
- *dst.Spec.BGPAdvertisements[i].AggregationLengthV6 = *adv.AggregationLengthV6
+ if adv.AggregationLength != nil {
+ dst.Spec.BGPAdvertisements[i].AggregationLength = pointer.Int32Ptr(*adv.AggregationLength)
+ }
+ if adv.AggregationLengthV6 != nil {
+ dst.Spec.BGPAdvertisements[i].AggregationLengthV6 = pointer.Int32Ptr(*adv.AggregationLengthV6)
+ }
dst.Spec.BGPAdvertisements[i].LocalPref = adv.LocalPref
dst.Spec.BGPAdvertisements[i].Communities = make([]string, len(adv.Communities))
copy(dst.Spec.BGPAdvertisements[i].Communities, adv.Communities)
@@ -34,11 +41,17 @@ func (dst *AddressPool) ConvertFrom(srcRaw conversion.Hub) error {
dst.Spec.Protocol = src.Spec.Protocol
dst.Spec.Addresses = make([]string, len(src.Spec.Addresses))
copy(dst.Spec.Addresses, src.Spec.Addresses)
- *dst.Spec.AutoAssign = *src.Spec.AutoAssign
- dst.Spec.BGPAdvertisements = make([]BgpAdvertisement, 0)
+ if src.Spec.AutoAssign != nil {
+ dst.Spec.AutoAssign = pointer.BoolPtr(*src.Spec.AutoAssign)
+ }
+ dst.Spec.BGPAdvertisements = make([]BgpAdvertisement, len(src.Spec.BGPAdvertisements))
for i, adv := range src.Spec.BGPAdvertisements {
- *dst.Spec.BGPAdvertisements[i].AggregationLength = *adv.AggregationLength
- *dst.Spec.BGPAdvertisements[i].AggregationLengthV6 = *adv.AggregationLengthV6
+ if adv.AggregationLength != nil {
+ dst.Spec.BGPAdvertisements[i].AggregationLength = pointer.Int32Ptr(*adv.AggregationLength)
+ }
+ if adv.AggregationLengthV6 != nil {
+ dst.Spec.BGPAdvertisements[i].AggregationLengthV6 = pointer.Int32Ptr(*adv.AggregationLengthV6)
+ }
dst.Spec.BGPAdvertisements[i].LocalPref = adv.LocalPref
dst.Spec.BGPAdvertisements[i].Communities = make([]string, len(adv.Communities))
copy(dst.Spec.BGPAdvertisements[i].Communities, adv.Communities)
