Skip to content

Commit

Permalink
Use new struct for DNS and NTP servers
Browse files Browse the repository at this point in the history
  • Loading branch information
simcod committed Oct 17, 2024
1 parent 139ca1b commit fe45192
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 18 deletions.
16 changes: 14 additions & 2 deletions cmd/metal-api/internal/metal/machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,8 +153,8 @@ type MachineAllocation struct {
VPN *MachineVPN `rethinkdb:"vpn" json:"vpn"`
UUID string `rethinkdb:"uuid" json:"uuid"`
FirewallRules *FirewallRules `rethinkdb:"firewall_rules" json:"firewall_rules"`
DNSServers []string `rethinkdb:"dns_servers" json:"dns_servers"`
NTPServers []string `rethinkdb:"ntp_servers" json:"ntp_servers"`
DNSServers DNSServers `rethinkdb:"dns_servers" json:"dns_servers"`
NTPServers NTPServers `rethinkdb:"ntp_servers" json:"ntp_servers"`
}

type FirewallRules struct {
Expand All @@ -177,6 +177,18 @@ type IngressRule struct {
Comment string `rethinkdb:"comment" json:"comment"`
}

type DNSServers []DNSServer

type DNSServer struct {
IP string `rethinkdb:"ip" json:"ip"`
}

type NTPServers []NTPServer

type NTPServer struct {
Address string `address:"address" json:"address"`
}

type Protocol string

const (
Expand Down
12 changes: 6 additions & 6 deletions cmd/metal-api/internal/service/machine-service.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,8 @@ type machineAllocationSpec struct {
PlacementTags []string
EgressRules []metal.EgressRule
IngressRules []metal.IngressRule
DNSServers []string
NTPServers []string
DNSServers metal.DNSServers
NTPServers metal.NTPServers
}

// allocationNetwork is intermediate struct to create machine networks from regular networks during machine allocation
Expand Down Expand Up @@ -1151,7 +1151,7 @@ func createMachineAllocationSpec(ds *datastore.RethinkStore, machineRequest v1.M
return nil, errors.New("please specify a maximum of three dns servers")
}
for _, dnsip := range machineRequest.DNSServers {
_, err := netip.ParseAddr(dnsip)
_, err := netip.ParseAddr(dnsip.IP)
if err != nil {
return nil, fmt.Errorf("IP: %s for DNS server not correct err: %w", dnsip, err)
}
Expand All @@ -1161,13 +1161,13 @@ func createMachineAllocationSpec(ds *datastore.RethinkStore, machineRequest v1.M
return nil, errors.New("please specify a minimum of 3 and a maximum of 5 ntp servers")
}
for _, ntpserver := range machineRequest.NTPServers {
if net.ParseIP(ntpserver) != nil {
_, err := netip.ParseAddr(ntpserver)
if net.ParseIP(ntpserver.Address) != nil {
_, err := netip.ParseAddr(ntpserver.Address)
if err != nil {
return nil, fmt.Errorf("IP: %s for NTP server not correct err: %w", ntpserver, err)
}
} else {
if !govalidator.IsDNSName(ntpserver) {
if !govalidator.IsDNSName(ntpserver.Address) {
return nil, fmt.Errorf("DNS name: %s for NTP server not correct err: %w", ntpserver, err)
}
}
Expand Down
8 changes: 4 additions & 4 deletions cmd/metal-api/internal/service/v1/machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ type MachineAllocation struct {
VPN *MachineVPN `json:"vpn" description:"vpn connection info for machine" optional:"true"`
AllocationUUID string `json:"allocationuuid" description:"a unique identifier for this machine allocation, can be used to distinguish between machine allocations over time."`
FirewallRules *FirewallRules `json:"firewall_rules,omitempty" description:"a set of firewall rules to apply" optional:"true"`
DNSServers []string `json:"dns_servers,omitempty" description:"the dns servers used for the machine" optional:"true"`
NTPServers []string `json:"ntp_servers,omitempty" description:"the ntp servers used for the machine" optional:"true"`
DNSServers metal.DNSServers `json:"dns_servers,omitempty" description:"the dns servers used for the machine" optional:"true"`
NTPServers metal.NTPServers `json:"ntp_servers,omitempty" description:"the ntp servers used for the machine" optional:"true"`
}

type FirewallRules struct {
Expand Down Expand Up @@ -231,8 +231,8 @@ type MachineAllocateRequest struct {
Networks MachineAllocationNetworks `json:"networks" description:"the networks that this machine will be placed in." optional:"true"`
IPs []string `json:"ips" description:"the ips to attach to this machine additionally" optional:"true"`
PlacementTags []string `json:"placement_tags,omitempty" description:"by default machines are spread across the racks inside a partition for every project. if placement tags are provided, the machine candidate has an additional anti-affinity to other machines having the same tags"`
DNSServers []string `json:"dns_servers,omitempty" description:"the dns servers used for the machine" optional:"true"`
NTPServers []string `json:"ntp_servers,omitempty" description:"the ntp servers used for the machine" optional:"true"`
DNSServers metal.DNSServers `json:"dns_servers,omitempty" description:"the dns servers used for the machine" optional:"true"`
NTPServers metal.NTPServers `json:"ntp_servers,omitempty" description:"the ntp servers used for the machine" optional:"true"`
}

type MachineAllocationNetworks []MachineAllocationNetwork
Expand Down
32 changes: 26 additions & 6 deletions spec/metal-api.json
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,26 @@
"type": "HTTPErrorResponse"
}
},
"metal.DNSServer": {
"properties": {
"ip": {
"type": "string"
}
},
"required": [
"ip"
]
},
"metal.NTPServer": {
"properties": {
"address": {
"type": "string"
}
},
"required": [
"address"
]
},
"rest.HealthResponse": {
"properties": {
"message": {
Expand Down Expand Up @@ -1012,7 +1032,7 @@
"dns_servers": {
"description": "the dns servers used for the machine",
"items": {
"type": "string"
"$ref": "#/definitions/metal.DNSServer"
},
"type": "array"
},
Expand Down Expand Up @@ -1053,7 +1073,7 @@
"ntp_servers": {
"description": "the ntp servers used for the machine",
"items": {
"type": "string"
"$ref": "#/definitions/metal.NTPServer"
},
"type": "array"
},
Expand Down Expand Up @@ -2001,7 +2021,7 @@
"dns_servers": {
"description": "the dns servers used for the machine",
"items": {
"type": "string"
"$ref": "#/definitions/metal.DNSServer"
},
"type": "array"
},
Expand Down Expand Up @@ -2038,7 +2058,7 @@
"ntp_servers": {
"description": "the ntp servers used for the machine",
"items": {
"type": "string"
"$ref": "#/definitions/metal.NTPServer"
},
"type": "array"
},
Expand Down Expand Up @@ -2118,7 +2138,7 @@
"dns_servers": {
"description": "the dns servers used for the machine",
"items": {
"type": "string"
"$ref": "#/definitions/metal.DNSServer"
},
"type": "array"
},
Expand Down Expand Up @@ -2153,7 +2173,7 @@
"ntp_servers": {
"description": "the ntp servers used for the machine",
"items": {
"type": "string"
"$ref": "#/definitions/metal.NTPServer"
},
"type": "array"
},
Expand Down

0 comments on commit fe45192

Please sign in to comment.