Skip to content

Commit

Permalink
Lint cleanup (#1450)
Browse files Browse the repository at this point in the history
* Fix linting errors throughout the codebase
  • Loading branch information
kschoche authored Aug 25, 2022
1 parent a46e208 commit 04a03a5
Show file tree
Hide file tree
Showing 40 changed files with 170 additions and 187 deletions.
4 changes: 2 additions & 2 deletions acceptance/framework/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package config

import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strconv"
"strings"
Expand Down Expand Up @@ -109,7 +109,7 @@ func (t *TestConfig) entImage() (string, error) {
}

// Unmarshal values.yaml to current global.image value.
valuesContents, err := ioutil.ReadFile(filepath.Join(t.helmChartPath, "values.yaml"))
valuesContents, err := os.ReadFile(filepath.Join(t.helmChartPath, "values.yaml"))
if err != nil {
return "", err
}
Expand Down
5 changes: 2 additions & 3 deletions acceptance/framework/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package config

import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"testing"
Expand Down Expand Up @@ -151,10 +150,10 @@ func TestConfig_HelmValuesFromConfig_EntImage(t *testing.T) {
valuesYAML := fmt.Sprintf(`global:
image: %s
`, tt.consulImage)
tmp, err := ioutil.TempDir("", "")
tmp, err := os.MkdirTemp("", "")
require.NoError(t, err)
defer os.RemoveAll(tmp)
require.NoError(t, ioutil.WriteFile(filepath.Join(tmp, "values.yaml"), []byte(valuesYAML), 0644))
require.NoError(t, os.WriteFile(filepath.Join(tmp, "values.yaml"), []byte(valuesYAML), 0644))

cfg := TestConfig{
EnableEnterprise: true,
Expand Down
9 changes: 4 additions & 5 deletions acceptance/framework/k8s/debug.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package k8s
import (
"context"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"testing"
Expand Down Expand Up @@ -46,7 +45,7 @@ func WritePodsDebugInfoIfFailed(t *testing.T, kubectlOptions *k8s.KubectlOptions

// Write logs or err to file name <pod.Name>.log
logFilename := filepath.Join(testDebugDirectory, fmt.Sprintf("%s.log", pod.Name))
require.NoError(t, ioutil.WriteFile(logFilename, []byte(logs), 0600))
require.NoError(t, os.WriteFile(logFilename, []byte(logs), 0600))

// Describe pod and write it to a file.
writeResourceInfoToFile(t, pod.Name, "pod", testDebugDirectory, kubectlOptions)
Expand All @@ -71,8 +70,8 @@ func WritePodsDebugInfoIfFailed(t *testing.T, kubectlOptions *k8s.KubectlOptions
// Write config/clusters or err to file name <pod.Name>-envoy-[configdump/clusters].json
configDumpFilename := filepath.Join(testDebugDirectory, fmt.Sprintf("%s-envoy-configdump.json", mpod.Name))
clustersFilename := filepath.Join(testDebugDirectory, fmt.Sprintf("%s-envoy-clusters.json", mpod.Name))
require.NoError(t, ioutil.WriteFile(configDumpFilename, []byte(configDump), 0600))
require.NoError(t, ioutil.WriteFile(clustersFilename, []byte(clusters), 0600))
require.NoError(t, os.WriteFile(configDumpFilename, []byte(configDump), 0600))
require.NoError(t, os.WriteFile(clustersFilename, []byte(clusters), 0600))

}

Expand Down Expand Up @@ -146,5 +145,5 @@ func writeResourceInfoToFile(t *testing.T, resourceName, resourceType, testDebug
desc = fmt.Sprintf("Error describing %s/%s: %s: %s", resourceType, resourceType, err, desc)
}
descFilename := filepath.Join(testDebugDirectory, fmt.Sprintf("%s-%s.txt", resourceName, resourceType))
require.NoError(t, ioutil.WriteFile(descFilename, []byte(desc), 0600))
require.NoError(t, os.WriteFile(descFilename, []byte(desc), 0600))
}
4 changes: 2 additions & 2 deletions acceptance/framework/suite/suite.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package suite
import (
"flag"
"fmt"
"io/ioutil"
"os"
"testing"

"github.com/hashicorp/consul-k8s/acceptance/framework/config"
Expand Down Expand Up @@ -49,7 +49,7 @@ func (s *suite) Run() int {
// Create test debug directory if it doesn't exist
if s.cfg.DebugDirectory == "" {
var err error
s.cfg.DebugDirectory, err = ioutil.TempDir("", "consul-test")
s.cfg.DebugDirectory, err = os.MkdirTemp("", "consul-test")
if err != nil {
fmt.Printf("Failed to create debug directory: %s\n", err)
return 1
Expand Down
28 changes: 14 additions & 14 deletions cli/cmd/proxy/read/filters.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ import (
// FilterClusters takes a slice of clusters along with parameters for filtering
// those clusters.
//
// - `fqdn` filters clusters to only those with fully qualified domain names
// which contain the given value.
// - `address` filters clusters to only those with endpoint addresses which
// contain the given value.
// - `port` filters clusters to only those with endpoint addresses with ports
// that match the given value. If -1 is passed, no filtering will occur.
// - `fqdn` filters clusters to only those with fully qualified domain names
// which contain the given value.
// - `address` filters clusters to only those with endpoint addresses which
// contain the given value.
// - `port` filters clusters to only those with endpoint addresses with ports
// that match the given value. If -1 is passed, no filtering will occur.
//
// The filters are applied in combination such that a cluster must adhere to
// all of the filtering values which are passed in.
Expand Down Expand Up @@ -51,10 +51,10 @@ func FilterClusters(clusters []Cluster, fqdn, address string, port int) []Cluste
// FilterEndpoints takes a slice of endpoints along with parameters for filtering
// those endpoints:
//
// - `address` filters endpoints to only those with an address which contains
// the given value.
// - `port` filters endpoints to only those with an address which has a port
// that matches the given value. If -1 is passed, no filtering will occur.
// - `address` filters endpoints to only those with an address which contains
// the given value.
// - `port` filters endpoints to only those with an address which has a port
// that matches the given value. If -1 is passed, no filtering will occur.
//
// The filters are applied in combination such that an endpoint must adhere to
// all of the filtering values which are passed in.
Expand All @@ -78,10 +78,10 @@ func FilterEndpoints(endpoints []Endpoint, address string, port int) []Endpoint
// FilterListeners takes a slice of listeners along with parameters for filtering
// those endpoints:
//
// - `address` filters listeners to only those with an address which contains
// the given value.
// - `port` filters listeners to only those with an address which has a port
// that matches the given value. If -1 is passed, no filtering will occur.
// - `address` filters listeners to only those with an address which contains
// the given value.
// - `port` filters listeners to only those with an address which has a port
// that matches the given value. If -1 is passed, no filtering will occur.
//
// The filters are applied in combination such that an listener must adhere to
// all of the filtering values which are passed in.
Expand Down
3 changes: 1 addition & 2 deletions cli/common/flag/set.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"flag"
"fmt"
"io"
"io/ioutil"
"strings"

"github.com/posener/complete"
Expand Down Expand Up @@ -34,7 +33,7 @@ func NewSets() *Sets {
// Errors and usage are expected to be controlled externally by
// checking on the result of Parse.
unionSet.Usage = func() {}
unionSet.SetOutput(ioutil.Discard)
unionSet.SetOutput(io.Discard)

return &Sets{
unionSet: unionSet,
Expand Down
1 change: 1 addition & 0 deletions cli/helm/chart_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
)

// Embed a test chart to test against.
//
//go:embed test_fixtures/consul/* test_fixtures/consul/templates/_helpers.tpl
var testChartFiles embed.FS

Expand Down
11 changes: 6 additions & 5 deletions control-plane/connect-inject/endpoints_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1830,13 +1830,14 @@ func TestReconcileCreateEndpoint(t *testing.T) {

// Tests updating an Endpoints object.
// - Tests updates via the register codepath:
// - When an address in an Endpoint is updated, that the corresponding service instance in Consul is updated.
// - When an address is added to an Endpoint, an additional service instance in Consul is registered.
// - When an address in an Endpoint is updated - via health check change - the corresponding service instance is updated.
// - When an address in an Endpoint is updated, that the corresponding service instance in Consul is updated.
// - When an address is added to an Endpoint, an additional service instance in Consul is registered.
// - When an address in an Endpoint is updated - via health check change - the corresponding service instance is updated.
// - Tests updates via the deregister codepath:
// - When an address is removed from an Endpoint, the corresponding service instance in Consul is deregistered.
// - When an address is removed from an Endpoint *and there are no addresses left in the Endpoint*, the
// - When an address is removed from an Endpoint, the corresponding service instance in Consul is deregistered.
// - When an address is removed from an Endpoint *and there are no addresses left in the Endpoint*, the
// corresponding service instance in Consul is deregistered.
//
// For the register and deregister codepath, this also tests that they work when the Consul service name is different
// from the K8s service name.
// This test covers EndpointsController.deregisterServiceOnAllAgents when services should be selectively deregistered
Expand Down
11 changes: 6 additions & 5 deletions control-plane/connect-inject/health_checks_test.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package connectinject

import (
"github.com/stretchr/testify/require"
"io/ioutil"
"os"
"path/filepath"
"testing"

"github.com/stretchr/testify/require"
)

func TestReady(t *testing.T) {
Expand All @@ -26,14 +27,14 @@ func TestReady(t *testing.T) {

for _, tt := range cases {
t.Run(tt.name, func(t *testing.T) {
tmpDir, err := ioutil.TempDir("", "")
tmpDir, err := os.MkdirTemp("", "")
require.NoError(t, err)
if tt.certFileContents != nil {
err := ioutil.WriteFile(filepath.Join(tmpDir, "tls.crt"), []byte(*tt.certFileContents), 0666)
err := os.WriteFile(filepath.Join(tmpDir, "tls.crt"), []byte(*tt.certFileContents), 0666)
require.NoError(t, err)
}
if tt.keyFileContents != nil {
err := ioutil.WriteFile(filepath.Join(tmpDir, "tls.key"), []byte(*tt.keyFileContents), 0666)
err := os.WriteFile(filepath.Join(tmpDir, "tls.key"), []byte(*tt.keyFileContents), 0666)
require.NoError(t, err)
}
rc := ReadinessCheck{tmpDir}
Expand Down
10 changes: 5 additions & 5 deletions control-plane/connect-inject/peering_acceptor_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,11 @@ const (
// move the current state of the cluster closer to the desired state.
// PeeringAcceptor resources determine whether to generate a new peering token in Consul and store it in the backend
// specified in the spec.
// - If the resource doesn't exist, the peering should be deleted in Consul.
// - If the resource exists, and a peering doesn't exist in Consul, it should be created.
// - If the resource exists, and a peering does exist in Consul, it should be reconciled.
// - If the status of the resource does not match the current state of the specified secret, generate a new token
// and store it according to the spec.
// - If the resource doesn't exist, the peering should be deleted in Consul.
// - If the resource exists, and a peering doesn't exist in Consul, it should be created.
// - If the resource exists, and a peering does exist in Consul, it should be reconciled.
// - If the status of the resource does not match the current state of the specified secret, generate a new token
// and store it according to the spec.
//
// NOTE: It is possible that Reconcile is called multiple times concurrently because we're watching
// two different resource kinds. As a result, we need to make sure that the code in this method
Expand Down
9 changes: 4 additions & 5 deletions control-plane/helper/cert/source_gen_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package cert

import (
"context"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
Expand Down Expand Up @@ -78,15 +77,15 @@ func testGenSource() *GenSource {
func testBundleDir(t *testing.T, bundle *Bundle, dir string) string {
if dir == "" {
// Create a temporary directory for storing the certs
td, err := ioutil.TempDir("", "consul")
td, err := os.MkdirTemp("", "consul")
require.NoError(t, err)
dir = td
}

// Write the cert
require.NoError(t, ioutil.WriteFile(filepath.Join(dir, "ca.pem"), bundle.CACert, 0644))
require.NoError(t, ioutil.WriteFile(filepath.Join(dir, "leaf.pem"), bundle.Cert, 0644))
require.NoError(t, ioutil.WriteFile(filepath.Join(dir, "leaf.key.pem"), bundle.Key, 0644))
require.NoError(t, os.WriteFile(filepath.Join(dir, "ca.pem"), bundle.CACert, 0644))
require.NoError(t, os.WriteFile(filepath.Join(dir, "leaf.pem"), bundle.Cert, 0644))
require.NoError(t, os.WriteFile(filepath.Join(dir, "leaf.key.pem"), bundle.Key, 0644))

return dir
}
Expand Down
7 changes: 3 additions & 4 deletions control-plane/helper/test/test_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package test

import (
"fmt"
"io/ioutil"
"net/http"
"net/http/httptest"
"os"
Expand All @@ -26,13 +25,13 @@ const (
func GenerateServerCerts(t *testing.T) (string, string, string) {
require := require.New(t)

caFile, err := ioutil.TempFile("", "ca")
caFile, err := os.CreateTemp("", "ca")
require.NoError(err)

certFile, err := ioutil.TempFile("", "cert")
certFile, err := os.CreateTemp("", "cert")
require.NoError(err)

certKeyFile, err := ioutil.TempFile("", "key")
certKeyFile, err := os.CreateTemp("", "key")
require.NoError(err)

// Generate CA
Expand Down
6 changes: 3 additions & 3 deletions control-plane/subcommand/acl-init/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"errors"
"flag"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
"sync"
Expand Down Expand Up @@ -237,7 +237,7 @@ func (c *Command) Run(args []string) int {
// Write the data out as a file.
// Must be 0644 because this is written by the consul-k8s user but needs
// to be readable by the consul user.
err = ioutil.WriteFile(filepath.Join(c.flagACLDir, "acl-config.json"), buf.Bytes(), 0644)
err = os.WriteFile(filepath.Join(c.flagACLDir, "acl-config.json"), buf.Bytes(), 0644)
if err != nil {
c.logger.Error("Error writing config file", "error", err)
return 1
Expand All @@ -247,7 +247,7 @@ func (c *Command) Run(args []string) int {
if c.flagTokenSinkFile != "" {
// Must be 0600 in case this command is re-run. In that case we need
// to have permissions to overwrite our file.
err := ioutil.WriteFile(c.flagTokenSinkFile, []byte(secret), 0600)
err := os.WriteFile(c.flagTokenSinkFile, []byte(secret), 0600)
if err != nil {
c.logger.Error("Error writing token to file", "file", c.flagTokenSinkFile, "error", err)
return 1
Expand Down
Loading

0 comments on commit 04a03a5

Please sign in to comment.