diff --git a/.golangci.yml b/.golangci.yml index 30a20fbc4..28e4237af 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -140,6 +140,7 @@ linters: enable-all: true disable: - errorlint + - exhaustruct - exhaustivestruct - forbidigo - forcetypeassert @@ -156,7 +157,9 @@ linters: - maintidx - nestif - nilnil # we return "nil, nil" + - nonamedreturns - nolintlint + - nosnakecase - paralleltest - promlinter # https://github.com/golangci/golangci-lint/issues/2222 - tagliatelle # we have many different conventions diff --git a/Makefile b/Makefile index 12ac86c14..1188a8199 100644 --- a/Makefile +++ b/Makefile @@ -19,7 +19,7 @@ EXTRAS ?= v1.2.0-alpha.0-1-g17a319f GO_VERSION ?= 1.18 GOIMPORTS_VERSION ?= v0.1.11 GOFUMPT_VERSION ?= v0.3.0 -GOLANGCILINT_VERSION ?= v1.45.2 +GOLANGCILINT_VERSION ?= v1.47.2 STRINGER_VERSION ?= v0.1.10 ENUMER_VERSION ?= v1.1.2 DEEPCOPY_GEN_VERSION ?= v0.21.3 diff --git a/cmd/talosctl/cmd/mgmt/cluster/create.go b/cmd/talosctl/cmd/mgmt/cluster/create.go index e76d77f2f..1d721bc5e 100644 --- a/cmd/talosctl/cmd/mgmt/cluster/create.go +++ b/cmd/talosctl/cmd/mgmt/cluster/create.go @@ -40,6 +40,7 @@ import ( "github.com/talos-systems/talos/pkg/machinery/config/types/v1alpha1/generate" "github.com/talos-systems/talos/pkg/machinery/config/types/v1alpha1/machine" "github.com/talos-systems/talos/pkg/machinery/constants" + "github.com/talos-systems/talos/pkg/machinery/nethelpers" "github.com/talos-systems/talos/pkg/provision" "github.com/talos-systems/talos/pkg/provision/access" "github.com/talos-systems/talos/pkg/provision/providers" @@ -495,7 +496,7 @@ func create(ctx context.Context, flags *pflag.FlagSet) (err error) { bundle.WithInputOptions( &bundle.InputOptions{ ClusterName: clusterName, - Endpoint: fmt.Sprintf("https://%s:%d", defaultInternalLB, controlPlanePort), + Endpoint: fmt.Sprintf("https://%s", nethelpers.JoinHostPort(defaultInternalLB, controlPlanePort)), KubeVersion: strings.TrimPrefix(kubernetesVersion, "v"), GenOptions: genOptions, }), @@ -737,7 +738,7 @@ func mergeKubeconfig(ctx context.Context, clusterAccess *access.Adapter) error { if clusterAccess.ForceEndpoint != "" { for name := range config.Clusters { - config.Clusters[name].Server = fmt.Sprintf("https://%s:%d", clusterAccess.ForceEndpoint, controlPlanePort) + config.Clusters[name].Server = fmt.Sprintf("https://%s", nethelpers.JoinHostPort(clusterAccess.ForceEndpoint, controlPlanePort)) } } diff --git a/cmd/talosctl/cmd/talos/pcap.go b/cmd/talosctl/cmd/talos/pcap.go index 908a3f8a9..1875588a3 100644 --- a/cmd/talosctl/cmd/talos/pcap.go +++ b/cmd/talosctl/cmd/talos/pcap.go @@ -112,7 +112,7 @@ e.g. by excluding packets with the port 50000. defer wg.Wait() if pcapCmdFlags.output == "" { - return dumpPackets(ctx, r) + return dumpPackets(r) } var out io.Writer @@ -137,7 +137,7 @@ e.g. by excluding packets with the port 50000. }, } -func dumpPackets(ctx context.Context, r io.Reader) error { +func dumpPackets(r io.Reader) error { src, err := pcapgo.NewReader(r) if err != nil { return fmt.Errorf("error opening pcap reader: %w", err) diff --git a/internal/app/machined/pkg/controllers/hardware/hardware_test.go b/internal/app/machined/pkg/controllers/hardware/hardware_test.go index a2ca6b3df..7e7083462 100644 --- a/internal/app/machined/pkg/controllers/hardware/hardware_test.go +++ b/internal/app/machined/pkg/controllers/hardware/hardware_test.go @@ -48,16 +48,6 @@ func (suite *HardwareSuite) SetupTest() { suite.Require().NoError(err) } -func (suite *HardwareSuite) startRuntime() { - suite.wg.Add(1) - - go func() { - defer suite.wg.Done() - - suite.Assert().NoError(suite.runtime.Run(suite.ctx)) - }() -} - func (suite *HardwareSuite) assertResource(md resource.Metadata, check func(res resource.Resource) error) func() error { return func() error { r, err := suite.state.Get(suite.ctx, md) diff --git a/internal/app/machined/pkg/controllers/network/device_config.go b/internal/app/machined/pkg/controllers/network/device_config.go index 5cbf04a0e..f0654cd27 100644 --- a/internal/app/machined/pkg/controllers/network/device_config.go +++ b/internal/app/machined/pkg/controllers/network/device_config.go @@ -26,6 +26,7 @@ type DeviceConfigController struct { devices map[string]networkDevice } +//nolint:unused type networkDevice struct { hardwareAddress string busPrefix string @@ -103,7 +104,7 @@ func (ctrl *DeviceConfigController) Run(ctx context.Context, r controller.Runtim if device.Selector() != nil { device = device.(*v1alpha1.Device).DeepCopy() - err = ctrl.getDeviceBySelector(ctx, device, links.Items) + err = ctrl.getDeviceBySelector(device, links.Items) if err != nil { logger.Warn("failed to select an interface for a device", zap.Error(err)) @@ -153,7 +154,7 @@ func (ctrl *DeviceConfigController) Run(ctx context.Context, r controller.Runtim } } -func (ctrl *DeviceConfigController) getDeviceBySelector(ctx context.Context, device talosconfig.Device, links []resource.Resource) error { +func (ctrl *DeviceConfigController) getDeviceBySelector(device talosconfig.Device, links []resource.Resource) error { selector := device.Selector() for _, link := range links { diff --git a/internal/app/machined/pkg/controllers/network/device_config_test.go b/internal/app/machined/pkg/controllers/network/device_config_test.go index a25b91830..9c20e9260 100644 --- a/internal/app/machined/pkg/controllers/network/device_config_test.go +++ b/internal/app/machined/pkg/controllers/network/device_config_test.go @@ -165,7 +165,7 @@ func (suite *DeviceConfigSpecSuite) TestSelectors() { return retry.ExpectedError(err) } - deviceConfig = config.(*network.DeviceConfigSpec) //nolint:errcheck,forcedtypeassert + deviceConfig = config.(*network.DeviceConfigSpec) //nolint:errcheck,forcetypeassert return nil }, diff --git a/internal/app/machined/pkg/controllers/network/hostname_config.go b/internal/app/machined/pkg/controllers/network/hostname_config.go index 2761cad6f..5a442f871 100644 --- a/internal/app/machined/pkg/controllers/network/hostname_config.go +++ b/internal/app/machined/pkg/controllers/network/hostname_config.go @@ -134,13 +134,7 @@ func (ctrl *HostnameConfigController) Run(ctx context.Context, r controller.Runt nodeID := identity.(*cluster.Identity).TypedSpec().NodeID - var stableHostname *network.HostnameSpecSpec - - stableHostname, err = ctrl.getStableDefault(nodeID) - if err != nil { - return err - } - + stableHostname := ctrl.getStableDefault(nodeID) specs = append(specs, *stableHostname) } else { specs = append(specs, ctrl.getDefault(defaultAddr)) @@ -205,7 +199,7 @@ func (ctrl *HostnameConfigController) apply(ctx context.Context, r controller.Ru return ids, nil } -func (ctrl *HostnameConfigController) getStableDefault(nodeID string) (*network.HostnameSpecSpec, error) { +func (ctrl *HostnameConfigController) getStableDefault(nodeID string) *network.HostnameSpecSpec { h := sha256.New() h.Write([]byte(nodeID)) @@ -218,7 +212,7 @@ func (ctrl *HostnameConfigController) getStableDefault(nodeID string) (*network. return &network.HostnameSpecSpec{ Hostname: hostname, ConfigLayer: network.ConfigDefault, - }, nil + } } func (ctrl *HostnameConfigController) getDefault(defaultAddr *network.NodeAddress) (spec network.HostnameSpecSpec) { diff --git a/internal/app/machined/pkg/system/services/etcd.go b/internal/app/machined/pkg/system/services/etcd.go index 5cbbaa8cf..de0e65e03 100644 --- a/internal/app/machined/pkg/system/services/etcd.go +++ b/internal/app/machined/pkg/system/services/etcd.go @@ -45,6 +45,7 @@ import ( machineapi "github.com/talos-systems/talos/pkg/machinery/api/machine" "github.com/talos-systems/talos/pkg/machinery/config/types/v1alpha1/machine" "github.com/talos-systems/talos/pkg/machinery/constants" + "github.com/talos-systems/talos/pkg/machinery/nethelpers" etcdresource "github.com/talos-systems/talos/pkg/machinery/resources/etcd" "github.com/talos-systems/talos/pkg/machinery/resources/k8s" "github.com/talos-systems/talos/pkg/machinery/resources/network" @@ -444,11 +445,15 @@ func (e *Etcd) argsForInit(ctx context.Context, r runtime.Runtime) error { } if !extraArgs.Contains("initial-advertise-peer-urls") { - denyListArgs.Set("initial-advertise-peer-urls", fmt.Sprintf("https://%s:2380", net.FormatAddress(primaryAddr))) + denyListArgs.Set("initial-advertise-peer-urls", + fmt.Sprintf("https://%s", nethelpers.JoinHostPort(net.FormatAddress(primaryAddr), 2380)), + ) } if !extraArgs.Contains("advertise-client-urls") { - denyListArgs.Set("advertise-client-urls", fmt.Sprintf("https://%s:2379", net.FormatAddress(primaryAddr))) + denyListArgs.Set("advertise-client-urls", + fmt.Sprintf("https://%s", nethelpers.JoinHostPort(net.FormatAddress(primaryAddr), 2379)), + ) } if err := denyListArgs.Merge(extraArgs, denyList); err != nil { @@ -537,12 +542,16 @@ func (e *Etcd) argsForControlPlane(ctx context.Context, r runtime.Runtime) error } if !extraArgs.Contains("initial-advertise-peer-urls") { - denyListArgs.Set("initial-advertise-peer-urls", fmt.Sprintf("https://%s:2380", net.FormatAddress(primaryAddr))) + denyListArgs.Set("initial-advertise-peer-urls", + fmt.Sprintf("https://%s", nethelpers.JoinHostPort(net.FormatAddress(primaryAddr), 2380)), + ) } } if !extraArgs.Contains("advertise-client-urls") { - denyListArgs.Set("advertise-client-urls", fmt.Sprintf("https://%s:2379", net.FormatAddress(primaryAddr))) + denyListArgs.Set("advertise-client-urls", + fmt.Sprintf("https://%s", nethelpers.JoinHostPort(net.FormatAddress(primaryAddr), 2379)), + ) } if err = denyListArgs.Merge(extraArgs, denyList); err != nil { diff --git a/internal/integration/api/update-hostname.go b/internal/integration/api/update-hostname.go index 6dcc8c90a..dedd6fe77 100644 --- a/internal/integration/api/update-hostname.go +++ b/internal/integration/api/update-hostname.go @@ -77,7 +77,7 @@ func (suite *UpdateHostnameSuite) TestUpdateHostname() { newHostname := "test-update-hostname" - err = suite.updateHostname(nodeCtx, nodeInternalIP, newHostname) + err = suite.updateHostname(nodeCtx, newHostname) suite.Require().NoError(err) nodeReady := func(status corev1.ConditionStatus) bool { @@ -90,7 +90,7 @@ func (suite *UpdateHostnameSuite) TestUpdateHostname() { defer func() { // revert the hostname back to the original one - err = suite.updateHostname(nodeCtx, nodeInternalIP, oldHostname) + err = suite.updateHostname(nodeCtx, oldHostname) suite.Require().NoError(err) // expect node status to be Ready again @@ -119,7 +119,7 @@ func (suite *UpdateHostnameSuite) TestUpdateHostname() { suite.Require().NoError(err) } -func (suite *UpdateHostnameSuite) updateHostname(nodeCtx context.Context, nodeIP string, newHostname string) error { +func (suite *UpdateHostnameSuite) updateHostname(nodeCtx context.Context, newHostname string) error { nodeConfig, err := suite.ReadConfigFromNode(nodeCtx) if err != nil { return err diff --git a/internal/integration/base/cli.go b/internal/integration/base/cli.go index 6da235202..933ddf6f3 100644 --- a/internal/integration/base/cli.go +++ b/internal/integration/base/cli.go @@ -41,15 +41,7 @@ func (cliSuite *CLISuite) DiscoverNodes(ctx context.Context) cluster.Info { return discoveredNodes } - discoveredNodes = cliSuite.discoverKubectl() - if discoveredNodes != nil { - return discoveredNodes - } - - // still no nodes, skip the test - cliSuite.T().Skip("no nodes were discovered") - - return nil + return cliSuite.discoverKubectl() } // DiscoverNodeInternalIPs provides list of Talos node internal IPs in the cluster. diff --git a/internal/integration/provision/upgrade.go b/internal/integration/provision/upgrade.go index 3bd4b0bf2..379fcc03a 100644 --- a/internal/integration/provision/upgrade.go +++ b/internal/integration/provision/upgrade.go @@ -42,6 +42,7 @@ import ( "github.com/talos-systems/talos/pkg/machinery/config/types/v1alpha1/machine" "github.com/talos-systems/talos/pkg/machinery/constants" "github.com/talos-systems/talos/pkg/machinery/generic/slices" + "github.com/talos-systems/talos/pkg/machinery/nethelpers" "github.com/talos-systems/talos/pkg/provision" "github.com/talos-systems/talos/pkg/provision/access" "github.com/talos-systems/talos/pkg/provision/providers/qemu" @@ -340,7 +341,7 @@ func (suite *UpgradeSuite) setupCluster() { } defaultInternalLB, _ := suite.provisioner.GetLoadBalancers(request.Network) - suite.controlPlaneEndpoint = fmt.Sprintf("https://%s:%d", defaultInternalLB, constants.DefaultControlPlanePort) + suite.controlPlaneEndpoint = fmt.Sprintf("https://%s", nethelpers.JoinHostPort(defaultInternalLB, constants.DefaultControlPlanePort)) genOptions := suite.provisioner.GenOptions(request.Network) diff --git a/internal/pkg/tui/components/menubutton.go b/internal/pkg/tui/components/menubutton.go index 286aa4650..f08d52708 100644 --- a/internal/pkg/tui/components/menubutton.go +++ b/internal/pkg/tui/components/menubutton.go @@ -60,9 +60,7 @@ func (b *MenuButton) SetActiveColors(colors ...tcell.Color) { // 1st value is bg color. // 2nd value is label color. func (b *MenuButton) SetInactiveColors(colors ...tcell.Color) { - for i, color := range colors { - b.colors[i] = color - } + copy(b.colors[:], colors) b.updateColors() } diff --git a/internal/pkg/tui/installer/state.go b/internal/pkg/tui/installer/state.go index cbf3e5d37..1a9c377fb 100644 --- a/internal/pkg/tui/installer/state.go +++ b/internal/pkg/tui/installer/state.go @@ -20,6 +20,7 @@ import ( "github.com/talos-systems/talos/pkg/machinery/config/types/v1alpha1" "github.com/talos-systems/talos/pkg/machinery/config/types/v1alpha1/machine" "github.com/talos-systems/talos/pkg/machinery/constants" + "github.com/talos-systems/talos/pkg/machinery/nethelpers" ) const canalCustomCNI = "canal" @@ -54,9 +55,9 @@ func NewState(ctx context.Context, installer *Installer, conn *Connection) (*Sta } if conn.ExpandingCluster() { - opts.ClusterConfig.ControlPlane.Endpoint = fmt.Sprintf("https://%s:%d", conn.bootstrapEndpoint, constants.DefaultControlPlanePort) + opts.ClusterConfig.ControlPlane.Endpoint = fmt.Sprintf("https://%s", nethelpers.JoinHostPort(conn.bootstrapEndpoint, constants.DefaultControlPlanePort)) } else { - opts.ClusterConfig.ControlPlane.Endpoint = fmt.Sprintf("https://%s:%d", conn.nodeEndpoint, constants.DefaultControlPlanePort) + opts.ClusterConfig.ControlPlane.Endpoint = fmt.Sprintf("https://%s", nethelpers.JoinHostPort(conn.nodeEndpoint, constants.DefaultControlPlanePort)) } installDiskOptions := []interface{}{ diff --git a/pkg/kubernetes/errors.go b/pkg/kubernetes/errors.go index 9f3fa6512..cfc4c17fb 100644 --- a/pkg/kubernetes/errors.go +++ b/pkg/kubernetes/errors.go @@ -28,6 +28,8 @@ func IsRetryableError(err error) bool { var netErr net.Error if errors.As(err, &netErr) { + // https://groups.google.com/g/golang-nuts/c/-JcZzOkyqYI/m/xwaZzjCgAwAJ + //nolint:staticcheck if netErr.Temporary() || netErr.Timeout() { return true } diff --git a/pkg/machinery/nethelpers/host_port.go b/pkg/machinery/nethelpers/host_port.go new file mode 100644 index 000000000..9fe8d07f9 --- /dev/null +++ b/pkg/machinery/nethelpers/host_port.go @@ -0,0 +1,19 @@ +// This Source Code Form is subject to the terms of the Mozilla Public +// License, v. 2.0. If a copy of the MPL was not distributed with this +// file, You can obtain one at http://mozilla.org/MPL/2.0/. + +package nethelpers + +import ( + "fmt" + "net" +) + +type ints interface { + ~int16 | ~int32 | ~int64 | ~uint16 | ~uint32 | ~uint64 | int | uint +} + +// JoinHostPort is a wrapper around net.JoinHostPort which accepts port any integer type. +func JoinHostPort[T ints](host string, port T) string { + return net.JoinHostPort(host, fmt.Sprintf("%d", port)) +}