Skip to content

Commit

Permalink
golangci-lint: Add errorlint
Browse files Browse the repository at this point in the history
Add errorlint linter to be consistent with cilium/cilium configuration
[^1], and fix lint errors.

Ref: cilium/design-cfps#9
[^1]: https://github.com/cilium/cilium/blob/3fa34b170493b48ef7befedef72badbd05429b18/.golangci.yaml#L117

Signed-off-by: Michi Mutsuzaki <[email protected]>
  • Loading branch information
michi-covalent committed Jul 14, 2024
1 parent 120fcbb commit c35e393
Show file tree
Hide file tree
Showing 19 changed files with 84 additions and 81 deletions.
1 change: 1 addition & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ linters:
- bodyclose
- gocheckcompilerdirectives
- err113
- errorlint
- gofmt
- goheader
- goimports
Expand Down
4 changes: 2 additions & 2 deletions bgp/peers.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,14 +101,14 @@ func (s *Status) fetchPeeringStateFromPod(ctx context.Context, pod *corev1.Pod)
cmd := []string{"cilium", "bgp", "peers", "-o", "json"}
output, err := s.client.ExecInPod(ctx, pod.Namespace, pod.Name, defaults.AgentContainerName, cmd)
if err != nil {
return nil, fmt.Errorf("failed to fetch bgp state from %s: %v", pod.Name, err)
return nil, fmt.Errorf("failed to fetch bgp state from %s: %w", pod.Name, err)
}

bgpPeers := make([]*models.BgpPeer, 0)

err = json.Unmarshal(output.Bytes(), &bgpPeers)
if err != nil {
return nil, fmt.Errorf("failed to unmarshal bgp state from %s: %v", pod.Name, err)
return nil, fmt.Errorf("failed to unmarshal bgp state from %s: %w", pod.Name, err)
}

return bgpPeers, nil
Expand Down
2 changes: 1 addition & 1 deletion bgp/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ func (s *Status) fetchRoutesFromPod(ctx context.Context, fetchCmd []string, pod

err = json.Unmarshal(output.Bytes(), &bgpRoutes)
if err != nil {
return nil, fmt.Errorf("failed to unmarshal bgp routes from %s: %v", pod.Name, err)
return nil, fmt.Errorf("failed to unmarshal bgp routes from %s: %w", pod.Name, err)
}

return bgpRoutes, nil
Expand Down
2 changes: 1 addition & 1 deletion clustermesh/clustermesh.go
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ func (k *K8sClusterMesh) getSecret(ctx context.Context, client k8sClusterMeshImp
func (k *K8sClusterMesh) getCACert(ctx context.Context, client k8sClusterMeshImplementation) ([]byte, error) {
secret, err := client.GetSecret(ctx, k.params.Namespace, defaults.CASecretName, metav1.GetOptions{})
if err != nil {
return nil, fmt.Errorf("get secret %q to retrieve CA: %s", defaults.CASecretName, err)
return nil, fmt.Errorf("get secret %q to retrieve CA: %w", defaults.CASecretName, err)
}

// The helm and cronjob certificate generation methods currently store
Expand Down
2 changes: 1 addition & 1 deletion config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ func (k *K8sConfig) restartPodsUponConfigChange(ctx context.Context, params Para

if err := k.client.DeletePodCollection(ctx, params.Namespace,
metav1.DeleteOptions{}, metav1.ListOptions{LabelSelector: defaults.AgentPodSelector}); err != nil {
return fmt.Errorf("⚠️ unable to restart Cilium pods: %v", err)
return fmt.Errorf("⚠️ unable to restart Cilium pods: %w", err)
}

fmt.Println("♻️ Restarted Cilium pods")
Expand Down
2 changes: 1 addition & 1 deletion connectivity/check/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,7 @@ func (ct *ConnectivityTest) SetupAndValidate(ctx context.Context, extra SetupHoo
}
if ct.params.Hubble {
if err := ct.enableHubbleClient(ctx); err != nil {
return fmt.Errorf("unable to create hubble client: %s", err)
return fmt.Errorf("unable to create hubble client: %w", err)
}
}
if match, _ := ct.Features.MatchRequirements(features.RequireEnabled(features.NodeWithoutCilium)); match {
Expand Down
44 changes: 22 additions & 22 deletions connectivity/check/deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -589,7 +589,7 @@ func (ct *ConnectivityTest) deploy(ctx context.Context) error {
ct.Logf("✨ [%s] Deploying DNS test server configmap...", ct.clients.src.ClusterName())
_, err = ct.clients.src.CreateConfigMap(ctx, ct.params.TestNamespace, dnsConfigMap, metav1.CreateOptions{})
if err != nil {
return fmt.Errorf("unable to create configmap %s: %s", corednsConfigMapName, err)
return fmt.Errorf("unable to create configmap %s: %w", corednsConfigMapName, err)
}
}
if ct.params.MultiCluster != "" {
Expand All @@ -598,7 +598,7 @@ func (ct *ConnectivityTest) deploy(ctx context.Context) error {
ct.Logf("✨ [%s] Deploying DNS test server configmap...", ct.clients.dst.ClusterName())
_, err = ct.clients.dst.CreateConfigMap(ctx, ct.params.TestNamespace, dnsConfigMap, metav1.CreateOptions{})
if err != nil {
return fmt.Errorf("unable to create configmap %s: %s", corednsConfigMapName, err)
return fmt.Errorf("unable to create configmap %s: %w", corednsConfigMapName, err)
}
}
}
Expand Down Expand Up @@ -634,11 +634,11 @@ func (ct *ConnectivityTest) deploy(ctx context.Context) error {
}, ct.params.DNSTestServerImage)
_, err = ct.clients.src.CreateServiceAccount(ctx, ct.params.TestNamespace, k8s.NewServiceAccount(echoSameNodeDeploymentName), metav1.CreateOptions{})
if err != nil {
return fmt.Errorf("unable to create service account %s: %s", echoSameNodeDeploymentName, err)
return fmt.Errorf("unable to create service account %s: %w", echoSameNodeDeploymentName, err)
}
_, err = ct.clients.src.CreateDeployment(ctx, ct.params.TestNamespace, echoDeployment, metav1.CreateOptions{})
if err != nil {
return fmt.Errorf("unable to create deployment %s: %s", echoSameNodeDeploymentName, err)
return fmt.Errorf("unable to create deployment %s: %w", echoSameNodeDeploymentName, err)
}
}

Expand All @@ -655,11 +655,11 @@ func (ct *ConnectivityTest) deploy(ctx context.Context) error {
})
_, err = ct.clients.src.CreateServiceAccount(ctx, ct.params.TestNamespace, k8s.NewServiceAccount(clientDeploymentName), metav1.CreateOptions{})
if err != nil {
return fmt.Errorf("unable to create service account %s: %s", clientDeploymentName, err)
return fmt.Errorf("unable to create service account %s: %w", clientDeploymentName, err)
}
_, err = ct.clients.src.CreateDeployment(ctx, ct.params.TestNamespace, clientDeployment, metav1.CreateOptions{})
if err != nil {
return fmt.Errorf("unable to create deployment %s: %s", clientDeploymentName, err)
return fmt.Errorf("unable to create deployment %s: %w", clientDeploymentName, err)
}
}

Expand Down Expand Up @@ -692,11 +692,11 @@ func (ct *ConnectivityTest) deploy(ctx context.Context) error {
})
_, err = ct.clients.src.CreateServiceAccount(ctx, ct.params.TestNamespace, k8s.NewServiceAccount(client2DeploymentName), metav1.CreateOptions{})
if err != nil {
return fmt.Errorf("unable to create service account %s: %s", client2DeploymentName, err)
return fmt.Errorf("unable to create service account %s: %w", client2DeploymentName, err)
}
_, err = ct.clients.src.CreateDeployment(ctx, ct.params.TestNamespace, clientDeployment, metav1.CreateOptions{})
if err != nil {
return fmt.Errorf("unable to create deployment %s: %s", client2DeploymentName, err)
return fmt.Errorf("unable to create deployment %s: %w", client2DeploymentName, err)
}
}

Expand Down Expand Up @@ -730,11 +730,11 @@ func (ct *ConnectivityTest) deploy(ctx context.Context) error {
})
_, err = ct.clients.src.CreateServiceAccount(ctx, ct.params.TestNamespace, k8s.NewServiceAccount(client3DeploymentName), metav1.CreateOptions{})
if err != nil {
return fmt.Errorf("unable to create service account %s: %s", client3DeploymentName, err)
return fmt.Errorf("unable to create service account %s: %w", client3DeploymentName, err)
}
_, err = ct.clients.src.CreateDeployment(ctx, ct.params.TestNamespace, clientDeployment, metav1.CreateOptions{})
if err != nil {
return fmt.Errorf("unable to create deployment %s: %s", client3DeploymentName, err)
return fmt.Errorf("unable to create deployment %s: %w", client3DeploymentName, err)
}
}
}
Expand All @@ -759,11 +759,11 @@ func (ct *ConnectivityTest) deploy(ctx context.Context) error {
})
_, err = ct.clients.src.CreateServiceAccount(ctx, ct.params.TestNamespace, k8s.NewServiceAccount(clientCPDeployment), metav1.CreateOptions{})
if err != nil && !k8sErrors.IsAlreadyExists(err) {
return fmt.Errorf("unable to create service account %s: %s", clientCPDeployment, err)
return fmt.Errorf("unable to create service account %s: %w", clientCPDeployment, err)
}
_, err = ct.clients.src.CreateDeployment(ctx, ct.params.TestNamespace, clientDeployment, metav1.CreateOptions{})
if err != nil && !k8sErrors.IsAlreadyExists(err) {
return fmt.Errorf("unable to create deployment %s: %s", clientCPDeployment, err)
return fmt.Errorf("unable to create deployment %s: %w", clientCPDeployment, err)
}
}

Expand Down Expand Up @@ -834,7 +834,7 @@ func (ct *ConnectivityTest) deploy(ctx context.Context) error {
}, ct.params.DNSTestServerImage)
_, err = ct.clients.dst.CreateServiceAccount(ctx, ct.params.TestNamespace, k8s.NewServiceAccount(echoOtherNodeDeploymentName), metav1.CreateOptions{})
if err != nil {
return fmt.Errorf("unable to create service account %s: %s", echoOtherNodeDeploymentName, err)
return fmt.Errorf("unable to create service account %s: %w", echoOtherNodeDeploymentName, err)
}
_, err = ct.clients.dst.CreateDeployment(ctx, ct.params.TestNamespace, echoOtherNodeDeployment, metav1.CreateOptions{})
if err != nil {
Expand Down Expand Up @@ -909,11 +909,11 @@ func (ct *ConnectivityTest) deploy(ctx context.Context) error {
})
_, err = ct.clients.src.CreateServiceAccount(ctx, ct.params.TestNamespace, k8s.NewServiceAccount(echoExternalNodeDeploymentName), metav1.CreateOptions{})
if err != nil {
return fmt.Errorf("unable to create service account %s: %s", echoExternalNodeDeploymentName, err)
return fmt.Errorf("unable to create service account %s: %w", echoExternalNodeDeploymentName, err)
}
_, err = ct.clients.src.CreateDeployment(ctx, ct.params.TestNamespace, echoExternalDeployment, metav1.CreateOptions{})
if err != nil {
return fmt.Errorf("unable to create deployment %s: %s", echoExternalNodeDeploymentName, err)
return fmt.Errorf("unable to create deployment %s: %w", echoExternalNodeDeploymentName, err)
}

svc := newService(echoExternalNodeDeploymentName,
Expand Down Expand Up @@ -958,11 +958,11 @@ func (ct *ConnectivityTest) deploy(ctx context.Context) error {
})
_, err = ct.clients.src.CreateServiceAccount(ctx, ct.params.TestNamespace, k8s.NewServiceAccount(lrpClientDeploymentName), metav1.CreateOptions{})
if err != nil {
return fmt.Errorf("unable to create service account %s: %s", lrpClientDeployment, err)
return fmt.Errorf("unable to create service account %s: %w", lrpClientDeployment, err)
}
_, err = ct.clients.src.CreateDeployment(ctx, ct.params.TestNamespace, lrpClientDeployment, metav1.CreateOptions{})
if err != nil {
return fmt.Errorf("unable to create deployment %s: %s", lrpClientDeployment, err)
return fmt.Errorf("unable to create deployment %s: %w", lrpClientDeployment, err)
}
ct.Logf("✨ [%s] Deploying lrp-backend deployment...", ct.clients.src.ClusterName())
containerPort := 8080
Expand Down Expand Up @@ -993,11 +993,11 @@ func (ct *ConnectivityTest) deploy(ctx context.Context) error {
})
_, err = ct.clients.src.CreateServiceAccount(ctx, ct.params.TestNamespace, k8s.NewServiceAccount(lrpBackendDeploymentName), metav1.CreateOptions{})
if err != nil {
return fmt.Errorf("unable to create service account %s: %s", lrpBackendDeployment, err)
return fmt.Errorf("unable to create service account %s: %w", lrpBackendDeployment, err)
}
_, err = ct.clients.src.CreateDeployment(ctx, ct.params.TestNamespace, lrpBackendDeployment, metav1.CreateOptions{})
if err != nil {
return fmt.Errorf("unable to create deployment %s: %s", lrpBackendDeployment, err)
return fmt.Errorf("unable to create deployment %s: %w", lrpBackendDeployment, err)
}
}

Expand All @@ -1024,7 +1024,7 @@ func (ct *ConnectivityTest) createClientPerfDeployment(ctx context.Context, name
})
_, err := ct.clients.src.CreateServiceAccount(ctx, ct.params.TestNamespace, k8s.NewServiceAccount(name), metav1.CreateOptions{})
if err != nil {
return fmt.Errorf("unable to create service account %s: %s", name, err)
return fmt.Errorf("unable to create service account %s: %w", name, err)
}
_, err = ct.clients.src.CreateDeployment(ctx, ct.params.TestNamespace, perfClientDeployment, metav1.CreateOptions{})
if err != nil {
Expand Down Expand Up @@ -1052,7 +1052,7 @@ func (ct *ConnectivityTest) createServerPerfDeployment(ctx context.Context, name
})
_, err := ct.clients.src.CreateServiceAccount(ctx, ct.params.TestNamespace, k8s.NewServiceAccount(name), metav1.CreateOptions{})
if err != nil {
return fmt.Errorf("unable to create service account %s: %s", name, err)
return fmt.Errorf("unable to create service account %s: %w", name, err)
}

_, err = ct.clients.src.CreateDeployment(ctx, ct.params.TestNamespace, perfServerDeployment, metav1.CreateOptions{})
Expand Down Expand Up @@ -1278,7 +1278,7 @@ func (ct *ConnectivityTest) validateDeployment(ctx context.Context) error {

clientPods, err := ct.client.ListPods(ctx, ct.params.TestNamespace, metav1.ListOptions{LabelSelector: "kind=" + kindClientName})
if err != nil {
return fmt.Errorf("unable to list client pods: %s", err)
return fmt.Errorf("unable to list client pods: %w", err)
}

for _, pod := range clientPods.Items {
Expand Down
2 changes: 1 addition & 1 deletion connectivity/check/policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -481,7 +481,7 @@ func (t *Test) applyPolicies(ctx context.Context) error {
if sumMap(revDeltas) > 0 {
t.Debug("Policy difference detected, waiting for Cilium agents to increment policy revisions..")
if err := t.waitCiliumPolicyRevisions(ctx, revisions, revDeltas); err != nil {
return fmt.Errorf("policies were not applied on all Cilium nodes in time: %s", err)
return fmt.Errorf("policies were not applied on all Cilium nodes in time: %w", err)
}
}

Expand Down
8 changes: 4 additions & 4 deletions connectivity/perf/common/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,22 +168,22 @@ func exportSummary(content perfData, reportDir string) error {
filePath := path.Join(reportDir, strings.Join([]string{fileName, "json"}, "."))
contentStr, err := prettyPrintJSON(content)
if err != nil {
return fmt.Errorf("error formatting summary: %v error: %v", content, err)
return fmt.Errorf("error formatting summary: %v error: %w", content, err)
}
if err := os.WriteFile(filePath, []byte(contentStr), 0600); err != nil {
return fmt.Errorf("writing to file %v error: %v", filePath, err)
return fmt.Errorf("writing to file %v error: %w", filePath, err)
}
return nil
}

func prettyPrintJSON(data interface{}) (string, error) {
output := &bytes.Buffer{}
if err := json.NewEncoder(output).Encode(data); err != nil {
return "", fmt.Errorf("building encoder error: %v", err)
return "", fmt.Errorf("building encoder error: %w", err)
}
formatted := &bytes.Buffer{}
if err := json.Indent(formatted, output.Bytes(), "", " "); err != nil {
return "", fmt.Errorf("indenting error: %v", err)
return "", fmt.Errorf("indenting error: %w", err)
}
return formatted.String(), nil
}
6 changes: 3 additions & 3 deletions connectivity/tests/health.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,13 +93,13 @@ func validateHealthStatus(t *check.ConnectivityTest, pod *check.Pod, out bytes.B
var data interface{}
err := json.Unmarshal(out.Bytes(), &data)
if err != nil {
return fmt.Errorf("Failed to unmarshal cilium-health output: %s", err)
return fmt.Errorf("Failed to unmarshal cilium-health output: %w", err)
}

// Check that status of all nodes is reported
nodes, err := filterJSON(data, nodesFilter)
if err != nil {
return fmt.Errorf("Failed to filter nodes: %s", err)
return fmt.Errorf("Failed to filter nodes: %w", err)
}
nodeCount := strings.Split(nodes, " ")
if len(nodeCount) < len(t.CiliumPods()) {
Expand All @@ -113,7 +113,7 @@ func validateHealthStatus(t *check.ConnectivityTest, pod *check.Pod, out bytes.B
kvExpr := fmt.Sprintf(`{range .nodes[*]}{.name}{"%s="}{%s}{"\n"}{end}`, statusPath, statusPath)
healthStatus, err := filterJSON(data, kvExpr)
if err != nil {
return fmt.Errorf("cilium-agent '%s': failed to filter node health status: %s", pod.Name(), err)
return fmt.Errorf("cilium-agent '%s': failed to filter node health status: %w", pod.Name(), err)
}

for path, status := range parseKVPairs(healthStatus) {
Expand Down
2 changes: 1 addition & 1 deletion encrypt/ipsec_key_status.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func (s *Encrypt) IPsecKeyStatus(ctx context.Context) error {
func (s *Encrypt) readIPsecKey(ctx context.Context) (string, error) {
secret, err := s.client.GetSecret(ctx, s.params.CiliumNamespace, defaults.EncryptionSecretName, metav1.GetOptions{})
if err != nil {
return "", fmt.Errorf("failed to fetch IPsec secret: %s", err)
return "", fmt.Errorf("failed to fetch IPsec secret: %w", err)
}

if key, ok := secret.Data["keys"]; ok {
Expand Down
12 changes: 6 additions & 6 deletions encrypt/ipsec_rotate_key.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func (s *Encrypt) IPsecRotateKey(ctx context.Context) error {

secret, err := s.client.GetSecret(ctx, s.params.CiliumNamespace, defaults.EncryptionSecretName, metav1.GetOptions{})
if err != nil {
return fmt.Errorf("failed to fetch IPsec secret: %s", err)
return fmt.Errorf("failed to fetch IPsec secret: %w", err)
}

keyBytes, ok := secret.Data["keys"]
Expand All @@ -48,7 +48,7 @@ func (s *Encrypt) IPsecRotateKey(ctx context.Context) error {

newKey, err := rotateIPsecKey(key, s.params.IPsecKeyAuthAlgo)
if err != nil {
return fmt.Errorf("failed to rotate IPsec key: %s", err)
return fmt.Errorf("failed to rotate IPsec key: %w", err)
}

if s.params.IPsecKeyPerNode != "" {
Expand All @@ -58,7 +58,7 @@ func (s *Encrypt) IPsecRotateKey(ctx context.Context) error {
patch := []byte(`{"stringData":{"keys":"` + newKey.String() + `"}}`)
_, err = s.client.PatchSecret(ctx, s.params.CiliumNamespace, defaults.EncryptionSecretName, types.StrategicMergePatchType, patch, metav1.PatchOptions{})
if err != nil {
return fmt.Errorf("failed to patch IPsec secret with new key: %s", err)
return fmt.Errorf("failed to patch IPsec secret with new key: %w", err)
}

_, err = fmt.Printf("IPsec key successfully rotated, new key SPI: %d\n", newKey.spi)
Expand Down Expand Up @@ -150,14 +150,14 @@ const maxIPsecSPI = 16
func (k ipsecKey) rotate() (ipsecKey, error) {
key, err := generateRandomHex(len(k.key))
if err != nil {
return ipsecKey{}, fmt.Errorf("failed to generate authentication key: %s", err)
return ipsecKey{}, fmt.Errorf("failed to generate authentication key: %w", err)
}

cipherKey := ""
if k.cipherMode != "" {
cipherKey, err = generateRandomHex(len(k.cipherKey))
if err != nil {
return ipsecKey{}, fmt.Errorf("failed to generate symmetric encryption key: %s", err)
return ipsecKey{}, fmt.Errorf("failed to generate symmetric encryption key: %w", err)
}
}

Expand Down Expand Up @@ -197,7 +197,7 @@ func generateRandomHex(size int) (string, error) {
func mustParseBool(v string) bool {
b, err := strconv.ParseBool(v)
if err != nil {
panic(fmt.Errorf("failed to parse string [%s] to bool: %s", v, err))
panic(fmt.Errorf("failed to parse string [%s] to bool: %w", v, err))
}
return b
}
Loading

0 comments on commit c35e393

Please sign in to comment.