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

Add dns and ntp servers flag to machine and partition creation #266

Merged
merged 7 commits into from
Nov 8, 2024
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
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
16 changes: 14 additions & 2 deletions cmd/firewall_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ var (
SSHPubKeys: []string{"sshpubkey"},
Succeeded: pointer.Pointer(true),
UserData: "---userdata---",
DNSServers: []*models.V1DNSServer{{IP: pointer.Pointer("8.8.8.8")}},
NtpServers: []*models.V1NTPServer{{Address: pointer.Pointer("1.pool.ntp.org")}},
},
Bios: &models.V1MachineBIOS{
Date: pointer.Pointer("biosdata"),
Expand Down Expand Up @@ -275,13 +277,21 @@ ID AGE HOSTNAME PROJECT NETWORKS IPS PARTITION
name: "create",
cmd: func(want *models.V1FirewallResponse) []string {
var (
ips []string
networks []string
ips []string
networks []string
dnsServers []string
ntpservers []string
)
for _, s := range want.Allocation.Networks {
ips = append(ips, s.Ips...)
networks = append(networks, *s.Networkid+":noauto")
}
for _, dns := range want.Allocation.DNSServers {
dnsServers = append(dnsServers, *dns.IP)
}
for _, ntp := range want.Allocation.NtpServers {
ntpservers = append(ntpservers, *ntp.Address)
}

args := []string{"firewall", "create",
"--id", *want.ID,
Expand All @@ -299,6 +309,8 @@ ID AGE HOSTNAME PROJECT NETWORKS IPS PARTITION
"--tags", strings.Join(want.Tags, ","),
"--userdata", want.Allocation.UserData,
"--firewall-rules-file", "",
"--dnsservers", strings.Join(dnsServers, ","),
"--ntpservers", strings.Join(ntpservers, ","),
}
assertExhaustiveArgs(t, args, commonExcludedFileArgs()...)
return args
Expand Down
33 changes: 32 additions & 1 deletion cmd/machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -492,6 +492,8 @@ If ~/.ssh/[id_ed25519.pub | id_rsa.pub | id_dsa.pub] is present it will be picke
cmd.Flags().StringSlice("tags", []string{}, "tags to add to the "+name+", use it like: --tags \"tag1,tag2\" or --tags \"tag3\".")
cmd.Flags().StringP("userdata", "", "", `cloud-init.io compatible userdata. [optional]
Can be either the userdata as string, or pointing to the userdata file to use e.g.: "@/tmp/userdata.cfg".`)
cmd.Flags().StringSlice("dnsservers", []string{}, "dns servers to add to the machine or firewall. [optional]")
cmd.Flags().StringSlice("ntpservers", []string{}, "ntp servers to add to the machine or firewall. [optional]")

switch name {
case "machine":
Expand Down Expand Up @@ -637,6 +639,8 @@ func machineResponseToCreate(r *models.V1MachineResponse) *models.V1MachineAlloc
ips []string
networks []*models.V1MachineAllocationNetwork
allocation = pointer.SafeDeref(r.Allocation)
dnsServers []*models.V1DNSServer
ntpServers []*models.V1NTPServer
)
for _, s := range allocation.Networks {
ips = append(ips, s.Ips...)
Expand All @@ -646,6 +650,14 @@ func machineResponseToCreate(r *models.V1MachineResponse) *models.V1MachineAlloc
})
}

for _, s := range allocation.DNSServers {
dnsServers = append(dnsServers, &models.V1DNSServer{IP: s.IP})
}

for _, s := range allocation.NtpServers {
ntpServers = append(ntpServers, &models.V1NTPServer{Address: s.Address})
}

return &models.V1MachineAllocateRequest{
Description: allocation.Description,
Filesystemlayoutid: pointer.SafeDeref(pointer.SafeDeref(allocation.Filesystemlayout).ID),
Expand All @@ -661,6 +673,8 @@ func machineResponseToCreate(r *models.V1MachineResponse) *models.V1MachineAlloc
Tags: r.Tags,
UserData: base64.StdEncoding.EncodeToString([]byte(allocation.UserData)),
UUID: pointer.SafeDeref(r.ID),
DNSServers: dnsServers,
NtpServers: ntpServers,
}
}

Expand All @@ -684,7 +698,15 @@ func (c *machineCmd) createRequestFromCLI() (*models.V1MachineAllocateRequest, e
}

func machineCreateRequest() (*models.V1MachineAllocateRequest, error) {
var (
keys []string
dnsServers []*models.V1DNSServer
ntpServers []*models.V1NTPServer
)

sshPublicKeyArgument := viper.GetString("sshpublickey")
dnsServersArgument := viper.GetStringSlice("dnsservers")
ntpServersArgument := viper.GetStringSlice("ntpservers")

if strings.HasPrefix(sshPublicKeyArgument, "@") {
var err error
Expand All @@ -706,7 +728,6 @@ func machineCreateRequest() (*models.V1MachineAllocateRequest, error) {
}
}

var keys []string
if sshPublicKeyArgument != "" {
keys = append(keys, sshPublicKeyArgument)
}
Expand All @@ -729,6 +750,14 @@ func machineCreateRequest() (*models.V1MachineAllocateRequest, error) {
return nil, err
}

for _, s := range dnsServersArgument {
dnsServers = append(dnsServers, &models.V1DNSServer{IP: pointer.Pointer(s)})
}

for _, s := range ntpServersArgument {
ntpServers = append(ntpServers, &models.V1NTPServer{Address: pointer.Pointer(s)})
}

mcr := &models.V1MachineAllocateRequest{
Description: viper.GetString("description"),
Partitionid: pointer.Pointer(viper.GetString("partition")),
Expand All @@ -743,6 +772,8 @@ func machineCreateRequest() (*models.V1MachineAllocateRequest, error) {
UserData: userDataArgument,
Networks: networks,
Ips: viper.GetStringSlice("ips"),
DNSServers: dnsServers,
NtpServers: ntpServers,
}

if viper.IsSet("filesystemlayout") {
Expand Down
56 changes: 34 additions & 22 deletions cmd/machine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ var (
SSHPubKeys: []string{"sshpubkey"},
Succeeded: pointer.Pointer(true),
UserData: "---userdata---",
DNSServers: []*models.V1DNSServer{{IP: pointer.Pointer("8.8.8.8")}},
NtpServers: []*models.V1NTPServer{{Address: pointer.Pointer("1.pool.ntp.org")}},
},
Bios: &models.V1MachineBIOS{
Date: pointer.Pointer("biosdata"),
Expand Down Expand Up @@ -369,22 +371,22 @@ func Test_MachineCmd_SingleResult(t *testing.T) {
},
want: machine1,
wantTable: pointer.Pointer(`
ID LAST EVENT WHEN AGE HOSTNAME PROJECT SIZE IMAGE PARTITION RACK
1 Phoned Home 7d 14d machine-hostname-1 project-1 1 debian-name 1 rack-1
`),
ID LAST EVENT WHEN AGE HOSTNAME PROJECT SIZE IMAGE PARTITION RACK
1 Phoned Home 7d 14d machine-hostname-1 project-1 1 debian-name 1 rack-1
`),
wantWideTable: pointer.Pointer(`
ID LAST EVENT WHEN AGE DESCRIPTION NAME HOSTNAME PROJECT IPS SIZE IMAGE PARTITION RACK STARTED TAGS LOCK/RESERVE
1 Phoned Home 7d 14d machine allocation 1 machine-1 machine-hostname-1 project-1 1.1.1.1 1 debian-name 1 rack-1 2022-05-05T01:02:03Z a
`),
ID LAST EVENT WHEN AGE DESCRIPTION NAME HOSTNAME PROJECT IPS SIZE IMAGE PARTITION RACK STARTED TAGS LOCK/RESERVE
1 Phoned Home 7d 14d machine allocation 1 machine-1 machine-hostname-1 project-1 1.1.1.1 1 debian-name 1 rack-1 2022-05-05T01:02:03Z a
`),
template: pointer.Pointer("{{ .id }} {{ .name }}"),
wantTemplate: pointer.Pointer(`
1 machine-1
`),
1 machine-1
`),
wantMarkdown: pointer.Pointer(`
| ID | | LAST EVENT | WHEN | AGE | HOSTNAME | PROJECT | SIZE | IMAGE | PARTITION | RACK |
|----|--|-------------|------|-----|--------------------|-----------|------|-------------|-----------|--------|
| 1 | | Phoned Home | 7d | 14d | machine-hostname-1 | project-1 | 1 | debian-name | 1 | rack-1 |
`),
| ID | | LAST EVENT | WHEN | AGE | HOSTNAME | PROJECT | SIZE | IMAGE | PARTITION | RACK |
|----|--|-------------|------|-----|--------------------|-----------|------|-------------|-----------|--------|
| 1 | | Phoned Home | 7d | 14d | machine-hostname-1 | project-1 | 1 | debian-name | 1 | rack-1 |
`),
},
{
name: "delete",
Expand All @@ -404,13 +406,21 @@ ID LAST EVENT WHEN AGE DESCRIPTION NAME HOSTNAME
name: "create",
cmd: func(want *models.V1MachineResponse) []string {
var (
ips []string
networks []string
ips []string
networks []string
dnsServers []string
ntpservers []string
)
for _, s := range want.Allocation.Networks {
ips = append(ips, s.Ips...)
networks = append(networks, *s.Networkid+":noauto")
}
for _, dns := range want.Allocation.DNSServers {
dnsServers = append(dnsServers, *dns.IP)
}
for _, ntp := range want.Allocation.NtpServers {
ntpservers = append(ntpservers, *ntp.Address)
}

args := []string{"machine", "create",
"--id", *want.ID,
Expand All @@ -427,6 +437,8 @@ ID LAST EVENT WHEN AGE DESCRIPTION NAME HOSTNAME
"--sshpublickey", pointer.FirstOrZero(want.Allocation.SSHPubKeys),
"--tags", strings.Join(want.Tags, ","),
"--userdata", want.Allocation.UserData,
"--dnsservers", strings.Join(dnsServers, ","),
"--ntpservers", strings.Join(ntpservers, ","),
}
assertExhaustiveArgs(t, args, commonExcludedFileArgs()...)
return args
Expand Down Expand Up @@ -497,11 +509,11 @@ func Test_MachineIPMICmd_MultiResult(t *testing.T) {
ipmiMachine1,
},
wantTable: pointer.Pointer(`
ID POWER IP MAC BOARD PART NUMBER BIOS BMC SIZE PARTITION RACK UPDATED
ID POWER IP MAC BOARD PART NUMBER BIOS BMC SIZE PARTITION RACK UPDATED
1 ⏻ 16W 1.2.3.4 1.2.3.4 part123 2.0 1.1 1 1 rack-1 5s ago
`),
wantWideTable: pointer.Pointer(`
ID LAST EVENT STATUS POWER IP MAC BOARD PART NUMBER CHASSIS SERIAL PRODUCT SERIAL BIOS VERSION BMC VERSION SIZE PARTITION RACK UPDATED
ID LAST EVENT STATUS POWER IP MAC BOARD PART NUMBER CHASSIS SERIAL PRODUCT SERIAL BIOS VERSION BMC VERSION SIZE PARTITION RACK UPDATED
1 Phoned Home ON 16W 1.2.3.4 1.2.3.4 part123 chassis123 product123 2.0 1.1 1 1 rack-1 5s ago
`),
template: pointer.Pointer("{{ .id }} {{ .name }}"),
Expand Down Expand Up @@ -538,11 +550,11 @@ ID LAST EVENT STATUS POWER IP MAC BOARD PART NUMBER CHA
ipmiMachine2,
},
wantTable: pointer.Pointer(`
ID POWER IP MAC BOARD PART NUMBER BIOS BMC SIZE PARTITION RACK UPDATED
ID POWER IP MAC BOARD PART NUMBER BIOS BMC SIZE PARTITION RACK UPDATED
1 ⏻ 16W 1.2.3.4 1.2.3.4 part123 2.0 1.1 1 1 rack-1 5s ago
`),
wantWideTable: pointer.Pointer(`
ID LAST EVENT STATUS POWER IP MAC BOARD PART NUMBER CHASSIS SERIAL PRODUCT SERIAL BIOS VERSION BMC VERSION SIZE PARTITION RACK UPDATED
ID LAST EVENT STATUS POWER IP MAC BOARD PART NUMBER CHASSIS SERIAL PRODUCT SERIAL BIOS VERSION BMC VERSION SIZE PARTITION RACK UPDATED
1 Phoned Home ON Power Supply NOT-OK 16W 1.2.3.4 1.2.3.4 part123 chassis123 product123 2.0 1.1 1 1 rack-1 5s ago
`),
template: pointer.Pointer("{{ .id }} {{ .name }}"),
Expand Down Expand Up @@ -666,13 +678,13 @@ func Test_MachineIssuesCmd(t *testing.T) {
},
want: machineWithIssues,
wantTable: pointer.Pointer(`
ID POWER ALLOCATED LOCK REASON LAST EVENT WHEN ISSUES
1 ⏻ 16W yes state Phoned Home 7d this is a test issue 1 (issue-1-id)
ID POWER ALLOCATED LOCK REASON LAST EVENT WHEN ISSUES
1 ⏻ 16W yes state Phoned Home 7d this is a test issue 1 (issue-1-id)
this is a test issue 2 (issue-2-id)
`),
wantWideTable: pointer.Pointer(`
ID NAME PARTITION PROJECT POWER STATE LOCK REASON LAST EVENT WHEN ISSUES REF URL DETAILS
1 machine-1 1 project-1 ON 16W state Phoned Home 7d this is a test issue 1 (issue-1-id) https://url-1 more details 1
ID NAME PARTITION PROJECT POWER STATE LOCK REASON LAST EVENT WHEN ISSUES REF URL DETAILS
1 machine-1 1 project-1 ON 16W state Phoned Home 7d this is a test issue 1 (issue-1-id) https://url-1 more details 1
this is a test issue 2 (issue-2-id) https://url-2 more details 2

`),
Expand Down
70 changes: 48 additions & 22 deletions cmd/partition.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,28 +26,16 @@ func newPartitionCmd(c *config) *cobra.Command {
}

cmdsConfig := &genericcli.CmdsConfig[*models.V1PartitionCreateRequest, *models.V1PartitionUpdateRequest, *models.V1PartitionResponse]{
BinaryName: binaryName,
GenericCLI: genericcli.NewGenericCLI[*models.V1PartitionCreateRequest, *models.V1PartitionUpdateRequest, *models.V1PartitionResponse](w).WithFS(c.fs),
Singular: "partition",
Plural: "partitions",
Description: "a partition is a failure domain in the data center.",
ValidArgsFn: c.comp.PartitionListCompletion,
Sorter: sorters.PartitionSorter(),
DescribePrinter: func() printers.Printer { return c.describePrinter },
ListPrinter: func() printers.Printer { return c.listPrinter },
CreateRequestFromCLI: func() (*models.V1PartitionCreateRequest, error) {
return &models.V1PartitionCreateRequest{
ID: pointer.Pointer(viper.GetString("id")),
Description: viper.GetString("description"),
Name: viper.GetString("name"),
Mgmtserviceaddress: viper.GetString("mgmtserver"),
Bootconfig: &models.V1PartitionBootConfiguration{
Commandline: viper.GetString("cmdline"),
Imageurl: viper.GetString("imageurl"),
Kernelurl: viper.GetString("kernelurl"),
},
}, nil
},
BinaryName: binaryName,
GenericCLI: genericcli.NewGenericCLI[*models.V1PartitionCreateRequest, *models.V1PartitionUpdateRequest, *models.V1PartitionResponse](w).WithFS(c.fs),
Singular: "partition",
Plural: "partitions",
Description: "a partition is a failure domain in the data center.",
ValidArgsFn: c.comp.PartitionListCompletion,
Sorter: sorters.PartitionSorter(),
DescribePrinter: func() printers.Printer { return c.describePrinter },
ListPrinter: func() printers.Printer { return c.listPrinter },
CreateRequestFromCLI: w.createRequestFromCLI,
CreateCmdMutateFn: func(cmd *cobra.Command) {
cmd.Flags().StringP("id", "", "", "ID of the partition. [required]")
cmd.Flags().StringP("name", "n", "", "Name of the partition. [optional]")
Expand All @@ -56,6 +44,8 @@ func newPartitionCmd(c *config) *cobra.Command {
cmd.Flags().StringP("cmdline", "", "", "kernel commandline for the metal-hammer in the partition. [required]")
cmd.Flags().StringP("imageurl", "", "", "initrd for the metal-hammer in the partition. [required]")
cmd.Flags().StringP("kernelurl", "", "", "kernel url for the metal-hammer in the partition. [required]")
cmd.Flags().StringP("dnsservers", "", "", "dns servers for the machines and firewalls in the partition. [optional]")
cmd.Flags().StringP("ntpservers", "", "", "ntp servers for the machines and firewalls in the partition. [optional]")
},
}

Expand Down Expand Up @@ -145,6 +135,8 @@ func partitionResponseToCreate(r *models.V1PartitionResponse) *models.V1Partitio
Mgmtserviceaddress: r.Mgmtserviceaddress,
Name: r.Name,
Privatenetworkprefixlength: r.Privatenetworkprefixlength,
DNSServers: r.DNSServers,
NtpServers: r.NtpServers,
}
}

Expand Down Expand Up @@ -188,3 +180,37 @@ func (c *partitionCmd) partitionCapacity() error {

return c.listPrinter.Print(resp.Payload)
}

func (c *partitionCmd) createRequestFromCLI() (*models.V1PartitionCreateRequest, error) {
var (
dnsServers []*models.V1DNSServer
ntpServers []*models.V1NTPServer
)

dnsServersArgument := viper.GetStringSlice("dnsservers")
ntpServersArgument := viper.GetStringSlice("ntpservers")

for _, s := range dnsServersArgument {
dnsServers = append(dnsServers, &models.V1DNSServer{IP: pointer.Pointer(s)})
}

for _, s := range ntpServersArgument {
ntpServers = append(ntpServers, &models.V1NTPServer{Address: pointer.Pointer(s)})
}

pcr := &models.V1PartitionCreateRequest{
ID: pointer.Pointer(viper.GetString("id")),
Description: viper.GetString("description"),
Name: viper.GetString("name"),
Mgmtserviceaddress: viper.GetString("mgmtserver"),
Bootconfig: &models.V1PartitionBootConfiguration{
Commandline: viper.GetString("cmdline"),
Imageurl: viper.GetString("imageurl"),
Kernelurl: viper.GetString("kernelurl"),
},
DNSServers: dnsServers,
NtpServers: ntpServers,
}

return pcr, nil
}
15 changes: 15 additions & 0 deletions cmd/partition_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cmd

import (
"strings"
"testing"

"github.com/metal-stack/metal-go/api/client/partition"
Expand Down Expand Up @@ -224,6 +225,18 @@ ID NAME DESCRIPTION LABELS
{
name: "create",
cmd: func(want *models.V1PartitionResponse) []string {
var (
dnsServers []string
ntpServers []string
)
for _, dns := range want.DNSServers {
dnsServers = append(dnsServers, *dns.IP)
}

for _, ntp := range want.NtpServers {
ntpServers = append(ntpServers, *ntp.Address)
}

args := []string{"partition", "create",
"--id", *want.ID,
"--name", want.Name,
Expand All @@ -232,6 +245,8 @@ ID NAME DESCRIPTION LABELS
"--kernelurl", want.Bootconfig.Kernelurl,
"--imageurl", want.Bootconfig.Imageurl,
"--mgmtserver", want.Mgmtserviceaddress,
"--dnsservers", strings.Join(dnsServers, ","),
"--ntpservers", strings.Join(ntpServers, ","),
}
assertExhaustiveArgs(t, args, commonExcludedFileArgs()...)
return args
Expand Down
Loading