diff --git a/cmd/smee/main.go b/cmd/smee/main.go index 6a502125..5de1ce03 100644 --- a/cmd/smee/main.go +++ b/cmd/smee/main.go @@ -379,7 +379,7 @@ func (c *config) dhcpHandler(ctx context.Context, log logr.Logger) (server.Handl if _, err := url.Parse(httpScriptURL.String()); err != nil { return nil, fmt.Errorf("invalid http ipxe script url: %w", err) } - ipxeScript := func(d *dhcpv4.DHCPv4) *url.URL { + ipxeScript := func(*dhcpv4.DHCPv4) *url.URL { return httpScriptURL } if c.dhcp.httpIpxeScript.injectMacAddress { diff --git a/go.mod b/go.mod index df26fc63..932a8357 100644 --- a/go.mod +++ b/go.mod @@ -5,6 +5,7 @@ go 1.22.0 toolchain go1.22.5 require ( + github.com/ccoveille/go-safecast v1.1.0 github.com/fsnotify/fsnotify v1.7.0 github.com/ghodss/yaml v1.0.0 github.com/go-logr/logr v1.4.2 diff --git a/go.sum b/go.sum index 818b4ebf..19e63695 100644 --- a/go.sum +++ b/go.sum @@ -2,6 +2,8 @@ dario.cat/mergo v1.0.0 h1:AGCNq9Evsj31mOgNPcLyXc+4PNABt905YmuqPYYpBWk= dario.cat/mergo v1.0.0/go.mod h1:uNxQE+84aUszobStD9th8a29P2fMDhsBdgRYvZOxGmk= github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM= github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw= +github.com/ccoveille/go-safecast v1.1.0 h1:iHKNWaZm+OznO7Eh6EljXPjGfGQsSfa6/sxPlIEKO+g= +github.com/ccoveille/go-safecast v1.1.0/go.mod h1:QqwNjxQ7DAqY0C721OIO9InMk9zCwcsO7tnRuHytad8= github.com/cenkalti/backoff/v4 v4.3.0 h1:MyRJ/UdXutAwSAT+s3wNd7MfTIcy71VQueUuFK343L8= github.com/cenkalti/backoff/v4 v4.3.0/go.mod h1:Y3VNntkOUPxTVeUxJ/G5vcM//AlwfmyYozVcomhLiZE= github.com/cespare/xxhash/v2 v2.3.0 h1:UL815xU9SqsFlibzuggzjXhog7bL6oX9BbNZnL2UFvs= diff --git a/internal/backend/file/file.go b/internal/backend/file/file.go index b20f0926..4f975d17 100644 --- a/internal/backend/file/file.go +++ b/internal/backend/file/file.go @@ -12,6 +12,7 @@ import ( "strings" "sync" + "github.com/ccoveille/go-safecast" "github.com/fsnotify/fsnotify" "github.com/ghodss/yaml" "github.com/go-logr/logr" @@ -292,7 +293,11 @@ func (w *Watcher) translate(r dhcp) (*data.DHCP, *data.Netboot, error) { d.VLANID = r.VLANID // lease time - d.LeaseTime = uint32(r.LeaseTime) + // Default to one week + d.LeaseTime = 604800 + if v, err := safecast.ToUint32(r.LeaseTime); err == nil { + d.LeaseTime = v + } // arch d.Arch = r.Arch diff --git a/internal/backend/kube/kube.go b/internal/backend/kube/kube.go index bea0830c..fb660e40 100644 --- a/internal/backend/kube/kube.go +++ b/internal/backend/kube/kube.go @@ -9,6 +9,7 @@ import ( "net/netip" "net/url" + "github.com/ccoveille/go-safecast" "github.com/tinkerbell/smee/internal/dhcp/data" "github.com/tinkerbell/tink/api/v1alpha1" "go.opentelemetry.io/otel" @@ -255,7 +256,11 @@ func toDHCPData(h *v1alpha1.DHCP) (*data.DHCP, error) { d.Hostname = h.Hostname // lease time required - d.LeaseTime = uint32(h.LeaseTime) + // Default to one week + d.LeaseTime = 604800 + if v, err := safecast.ToUint32(d.LeaseTime); err == nil { + d.LeaseTime = v + } // arch d.Arch = h.Arch diff --git a/internal/backend/kube/kube_test.go b/internal/backend/kube/kube_test.go index f7ad19c2..2ee19ce4 100644 --- a/internal/backend/kube/kube_test.go +++ b/internal/backend/kube/kube_test.go @@ -33,10 +33,10 @@ func TestNewBackend(t *testing.T) { "no config": {shouldErr: true}, "failed index field": {shouldErr: true, conf: new(rest.Config), opt: func(o *cluster.Options) { cl := fake.NewClientBuilder().Build() - o.NewClient = func(config *rest.Config, options client.Options) (client.Client, error) { + o.NewClient = func(*rest.Config, client.Options) (client.Client, error) { return cl, nil } - o.MapperProvider = func(c *rest.Config, httpClient *http.Client) (meta.RESTMapper, error) { + o.MapperProvider = func(*rest.Config, *http.Client) (meta.RESTMapper, error) { return cl.RESTMapper(), nil } }}, @@ -217,7 +217,7 @@ func TestGetByIP(t *testing.T) { if !tc.failToList { ct = ct.WithScheme(rs) ct = ct.WithRuntimeObjects(&v1alpha1.HardwareList{}) - ct = ct.WithIndex(&v1alpha1.Hardware{}, IPAddrIndex, func(obj client.Object) []string { + ct = ct.WithIndex(&v1alpha1.Hardware{}, IPAddrIndex, func(client.Object) []string { var list []string for _, elem := range tc.hwObject { list = append(list, elem.Spec.Interfaces[0].DHCP.IP.Address) @@ -233,13 +233,13 @@ func TestGetByIP(t *testing.T) { cl := ct.Build() fn := func(o *cluster.Options) { - o.NewClient = func(config *rest.Config, options client.Options) (client.Client, error) { + o.NewClient = func(*rest.Config, client.Options) (client.Client, error) { return cl, nil } - o.MapperProvider = func(_ *rest.Config, _ *http.Client) (meta.RESTMapper, error) { + o.MapperProvider = func(*rest.Config, *http.Client) (meta.RESTMapper, error) { return cl.RESTMapper(), nil } - o.NewCache = func(config *rest.Config, options cache.Options) (cache.Cache, error) { + o.NewCache = func(*rest.Config, cache.Options) (cache.Cache, error) { return &informertest.FakeInformers{Scheme: cl.Scheme()}, nil } } @@ -315,7 +315,7 @@ func TestGetByMac(t *testing.T) { if !tc.failToList { ct = ct.WithScheme(rs) ct = ct.WithRuntimeObjects(&v1alpha1.HardwareList{}) - ct = ct.WithIndex(&v1alpha1.Hardware{}, MACAddrIndex, func(obj client.Object) []string { + ct = ct.WithIndex(&v1alpha1.Hardware{}, MACAddrIndex, func(client.Object) []string { var list []string for _, elem := range tc.hwObject { list = append(list, elem.Spec.Interfaces[0].DHCP.MAC) @@ -331,13 +331,13 @@ func TestGetByMac(t *testing.T) { cl := ct.Build() fn := func(o *cluster.Options) { - o.NewClient = func(config *rest.Config, options client.Options) (client.Client, error) { + o.NewClient = func(*rest.Config, client.Options) (client.Client, error) { return cl, nil } - o.MapperProvider = func(c *rest.Config, httpClient *http.Client) (meta.RESTMapper, error) { + o.MapperProvider = func(*rest.Config, *http.Client) (meta.RESTMapper, error) { return cl.RESTMapper(), nil } - o.NewCache = func(config *rest.Config, options cache.Options) (cache.Cache, error) { + o.NewCache = func(*rest.Config, cache.Options) (cache.Cache, error) { return &informertest.FakeInformers{Scheme: cl.Scheme()}, nil } } diff --git a/internal/dhcp/dhcp.go b/internal/dhcp/dhcp.go index c9dde790..2568d8a0 100644 --- a/internal/dhcp/dhcp.go +++ b/internal/dhcp/dhcp.go @@ -3,6 +3,7 @@ package dhcp import ( "bytes" "encoding/hex" + "errors" "fmt" "net" "net/netip" @@ -255,7 +256,7 @@ func IsNetbootClient(pkt *dhcpv4.DHCPv4) error { func wrapNonNil(err error, format string) error { if err == nil { - return fmt.Errorf(format) + return errors.New(format) } return fmt.Errorf("%w: %v", err, format) diff --git a/internal/ipxe/http/http.go b/internal/ipxe/http/http.go index 48ee7af9..76231c3f 100644 --- a/internal/ipxe/http/http.go +++ b/internal/ipxe/http/http.go @@ -90,7 +90,7 @@ func (s *Config) ServeHTTP(ctx context.Context, addr string, handlers HandlerMap } func (s *Config) serveHealthchecker(rev string, start time.Time) http.HandlerFunc { - return func(w http.ResponseWriter, req *http.Request) { + return func(w http.ResponseWriter, _ *http.Request) { w.Header().Set("Content-Type", "application/json") res := struct { GitRev string `json:"git_rev"` diff --git a/internal/ipxe/script/ipxe_test.go b/internal/ipxe/script/ipxe_test.go index 802eca39..14d1aea0 100644 --- a/internal/ipxe/script/ipxe_test.go +++ b/internal/ipxe/script/ipxe_test.go @@ -39,7 +39,7 @@ func TestCustomScript(t *testing.T) { t.Fatal(err) } if diff := cmp.Diff(tt.want, got); diff != "" { - t.Fatalf(diff) + t.Fatal(diff) } }) } @@ -108,7 +108,7 @@ exit } if diff := cmp.Diff(tt.want, got); diff != "" { t.Log(got) - t.Fatalf(diff) + t.Fatal(diff) } }) }