api: Add UnmarshalJSON method to DHCPOptions
Added spacial handling for the ntpServers field.
In order to make it backwards compatible on go 1.17,
we remove leading zeros from IPv4 addresses, so the rest of the
system won't have spacial treatment for it.
package v1
import (
+ "encoding/json"
+
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
"k8s.io/apimachinery/pkg/types"
@@ -1149,6 +1151,24 @@ type DHCPOptions struct {
PrivateOptions []DHCPPrivateOptions `json:"privateOptions,omitempty"`
}
+func (d *DHCPOptions) UnmarshalJSON(data []byte) error {
+ type DHCPOptionsAlias DHCPOptions
+ var dhcpOptionsAlias DHCPOptionsAlias
+
+ if err := json.Unmarshal(data, &dhcpOptionsAlias); err != nil {
+ return err
+ }
+
+ for i, ntpServer := range dhcpOptionsAlias.NTPServers {
+ if sanitizedIP, err := sanitizeIP(ntpServer); err == nil {
+ dhcpOptionsAlias.NTPServers[i] = sanitizedIP
+ }
+ }
+
+ *d = DHCPOptions(dhcpOptionsAlias)
+ return nil
+}
+
// DHCPExtraOptions defines Extra DHCP options for a VM.
type DHCPPrivateOptions struct {
// Option is an Integer value from 224-254
