Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Configure DNS and NTP in machine allocation #571

Merged
merged 24 commits into from
Nov 8, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
df41e53
Configure DNS servers in machine allocation optionally
simcod Sep 11, 2024
4a18873
Return DNSServers in MachineResponse
simcod Sep 11, 2024
ffa8877
Add ntp servers field
simcod Sep 13, 2024
77f0843
NTP and DNS validation
simcod Sep 13, 2024
cbf9f21
Regenerate metal-api
simcod Sep 13, 2024
fcba3b4
Merge branch 'master' into machine-allocation-with-dns-and-ntp
majst01 Oct 4, 2024
139ca1b
Merge remote-tracking branch 'origin' into machine-allocation-with-dn…
simcod Oct 16, 2024
fe45192
Use new struct for DNS and NTP servers
simcod Oct 17, 2024
9973d7c
Check for empty ntp servers configuration
simcod Oct 22, 2024
f57775b
Add NTP and DNS server field to partition
simcod Oct 22, 2024
7abf175
Add new metal-api JSON
simcod Oct 22, 2024
fd58302
Add dns and ntp servers to partition base to include it in response
simcod Oct 23, 2024
a8f9111
Add DNS and NTP servers to fix integration test
simcod Oct 23, 2024
21bf0c5
Fix typo
simcod Oct 24, 2024
6a6bf09
Set partition default for machine allocation spec
simcod Oct 24, 2024
e82d7d0
Add DNS and NTP servers to partition response
simcod Oct 24, 2024
2df7e17
Fix: Specify a minimum of three NTP servers
simcod Oct 28, 2024
451813f
Integrate review
simcod Oct 29, 2024
b5b0adc
Regenerate spec
simcod Oct 29, 2024
14d08b3
Merge branch 'master' into machine-allocation-with-dns-and-ntp
Gerrit91 Nov 7, 2024
b5236c3
Implement review
simcod Nov 8, 2024
5f678e1
Remove if condition
simcod Nov 8, 2024
5f71f78
Review with Simon.
Gerrit91 Nov 8, 2024
d01ce2d
Fix.
Gerrit91 Nov 8, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 33 additions & 22 deletions cmd/metal-api/internal/service/machine-service.go
Original file line number Diff line number Diff line change
Expand Up @@ -1152,44 +1152,29 @@ func createMachineAllocationSpec(ds *datastore.RethinkStore, machineRequest v1.M
return nil, fmt.Errorf("partition:%s not found err:%w", partitionID, err)
}
var (
dnsServers metal.DNSServers
ntpServers metal.NTPServers
dnsServers = partition.DNSServers
ntpServers = partition.NTPServers
)
if len(machineRequest.DNSServers) != 0 {
if len(machineRequest.DNSServers) > 3 {
return nil, errors.New("please specify a maximum of three dns servers")
}
dnsServers = machineRequest.DNSServers
} else {
dnsServers = partition.DNSServers
}
for _, dnsServer := range dnsServers {
_, err := netip.ParseAddr(dnsServer.IP)
if err != nil {
return nil, fmt.Errorf("ip: %s for dns server not correct err: %w", dnsServer, err)
}

if err := ValidateDNSServers(dnsServers); err != nil {
return nil, err
}

if len(machineRequest.NTPServers) != 0 {
if len(machineRequest.NTPServers) < 3 || len(machineRequest.NTPServers) > 5 {
return nil, errors.New("please specify a minimum of 3 and a maximum of 5 ntp servers")
}
ntpServers = machineRequest.NTPServers
} else {
ntpServers = partition.NTPServers
}

for _, ntpserver := range ntpServers {
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.Address) {
return nil, fmt.Errorf("dns name: %s for ntp server not correct err: %w", ntpserver, err)
}
}
if err := ValidateNTPServers(ntpServers); err != nil {
return nil, err
}

return &machineAllocationSpec{
Expand Down Expand Up @@ -2527,3 +2512,29 @@ func (s machineAllocationSpec) noautoNetworkN() int {
func (s machineAllocationSpec) autoNetworkN() int {
return len(s.Networks) - s.noautoNetworkN()
}

func ValidateDNSServers(d metal.DNSServers) error {
for _, dnsServer := range d {
_, err := netip.ParseAddr(dnsServer.IP)
if err != nil {
return fmt.Errorf("ip: %s for dns server not correct err: %w", dnsServer, err)
}
}
return nil
}

func ValidateNTPServers(dnsServers metal.NTPServers) error {
for _, ntpserver := range dnsServers {
if net.ParseIP(ntpserver.Address) != nil {
_, err := netip.ParseAddr(ntpserver.Address)
if err != nil {
return fmt.Errorf("ip: %s for ntp server not correct err: %w", ntpserver, err)
}
} else {
if !govalidator.IsDNSName(ntpserver.Address) {
return fmt.Errorf("dns name: %s for ntp server not correct", ntpserver)
}
}
}
return nil
}
18 changes: 14 additions & 4 deletions cmd/metal-api/internal/service/partition-service.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,13 +206,23 @@ func (r *partitionResource) createPartition(request *restful.Request, response *
}

var dnsServers metal.DNSServers
if len(requestPayload.DNSServers) > 0 {
dnsServers = requestPayload.DNSServers
reqDNSServers := requestPayload.DNSServers
if len(reqDNSServers) > 0 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think this check can be omited, because the validate func iterates over the dnsservers otherwise returns nil

if err := ValidateDNSServers(reqDNSServers); err != nil {
r.sendError(request, response, httperrors.BadRequest(err))
return
}

dnsServers = reqDNSServers
}

var ntpServers metal.NTPServers
if len(requestPayload.NTPServers) > 0 {
ntpServers = requestPayload.NTPServers
reqNtpServers := requestPayload.NTPServers
if len(ntpServers) > 0 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same here

if err := ValidateNTPServers(reqNtpServers); err != nil {
r.sendError(request, response, httperrors.BadRequest(err))
}
ntpServers = reqNtpServers
}

p := &metal.Partition{
Expand Down
Loading