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 Golang linter #2341

Merged
merged 6 commits into from
Dec 11, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
26 changes: 26 additions & 0 deletions .github/workflows/golangci-lint.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
name: Golang Linter

on:
workflow_dispatch:
pull_request:
branches:
- main
paths:
- '**/*.go'
- '**/go.mod'

jobs:
golang_linter:
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@c85c95e3d7251135ab7dc9ce3241c5835cc595a9 # v3.5.3
- name: Set up Go
uses: actions/setup-go@fac708d6674e30b6ba41289acaab6d4b75aa0753 # v4.0.1
with:
go-version-file: 'go.mod'
- name: golangci-lint
uses: golangci/golangci-lint-action@3a919529898de77ec3da873e3063ca4b10e7f5cc # v3.7.0
with:
version: 'v1.55.2'
skip-pkg-cache: true
33 changes: 33 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
run:
timeout: 5m

issues:
max-per-linter: 0
max-same-issues: 0
exclude-rules:
- path: manifest/provider/resource.go
linters:
- staticcheck
# We need to use ValueFromMsgPack due to some missing abstraction in plugin-go.
text: "SA1019: tftypes.ValueFromMsgPack is deprecated: this function is exported for internal use in terraform-plugin-go."
- path: manifest/provider/import.go
linters:
- staticcheck
# We need to use MarshalMsgPack due to some missing abstraction in plugin-go.
text: "SA1019: impf.MarshalMsgPack is deprecated: this is not meant to be called by third parties."

linters:
disable-all: true
enable:
- gosimple
- gofmt
- staticcheck

linters-settings:
staticcheck:
checks:
- all
gosimple:
checks:
- all
- '-S1040' # Type assertion to current type: https://staticcheck.dev/docs/checks/#S1040
6 changes: 5 additions & 1 deletion GNUmakefile
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,14 @@ tools:
go install github.com/bflad/tfproviderlint/cmd/tfproviderlint@v0.28.1
go install github.com/bflad/tfproviderdocs@v0.9.1
go install github.com/katbyte/terrafmt@v0.5.2
go install github.com/golangci/golangci-lint/cmd/golangci-lint@v1.50.0
go install github.com/golangci/golangci-lint/cmd/golangci-lint@v1.55.2
go install github.com/hashicorp/go-changelog/cmd/changelog-build@latest
go install github.com/hashicorp/go-changelog/cmd/changelog-entry@latest

go-lint: tools
@echo "==> Run Golang CLI linter..."
@golangci-lint run

vet:
@echo "go vet ./..."
@go vet $$(go list ./...) ; if [ $$? -eq 1 ]; then \
Expand Down
4 changes: 2 additions & 2 deletions kubernetes/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@ func Provider() *schema.Provider {
// admission control
"kubernetes_validating_webhook_configuration": resourceKubernetesValidatingWebhookConfigurationV1Beta1(),
"kubernetes_validating_webhook_configuration_v1": resourceKubernetesValidatingWebhookConfigurationV1(),
"kubernetes_mutating_webhook_configuration": resourceKubernetesMutatingWebhookConfigurationV1(),
"kubernetes_mutating_webhook_configuration": resourceKubernetesMutatingWebhookConfiguration(),
"kubernetes_mutating_webhook_configuration_v1": resourceKubernetesMutatingWebhookConfigurationV1(),

// storage
Expand Down Expand Up @@ -464,7 +464,7 @@ func providerConfigure(ctx context.Context, d *schema.ResourceData, terraformVer
if logging.IsDebugOrHigher() {
log.Printf("[DEBUG] Enabling HTTP requests/responses tracing")
cfg.WrapTransport = func(rt http.RoundTripper) http.RoundTripper {
return logging.NewTransport("Kubernetes", rt)
return logging.NewSubsystemLoggingHTTPTransport("Kubernetes", rt)
}
}

Expand Down
9 changes: 4 additions & 5 deletions kubernetes/provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ func unsetEnv(t *testing.T) func() {
"KUBE_TOKEN": e.Token,
}

for k, _ := range envVars {
for k := range envVars {
if err := os.Unsetenv(k); err != nil {
t.Fatalf("Error unsetting env var %s: %s", k, err)
}
Expand Down Expand Up @@ -194,7 +194,6 @@ func testAccPreCheck(t *testing.T) {
if diags.HasError() {
t.Fatal(diags[0].Summary)
}
return
}

func getClusterVersion() (*gversion.Version, error) {
Expand All @@ -220,20 +219,20 @@ func getClusterVersion() (*gversion.Version, error) {
func setClusterVersionVar(t *testing.T, varName string) {
cv, err := getClusterVersion()
if err != nil {
t.Skip(fmt.Sprint("Could not get cluster version"))
t.Skipf("Could not get cluster version")
}
os.Setenv(varName, fmt.Sprintf("v%s", cv.Core().Original()))
}

func skipIfClusterVersionLessThan(t *testing.T, vs string) {
if clusterVersionLessThan(vs) {
t.Skip(fmt.Sprintf("This test does not run on cluster versions below %v", vs))
t.Skipf("This test does not run on cluster versions below %v", vs)
}
}

func skipIfClusterVersionGreaterThanOrEqual(t *testing.T, vs string) {
if clusterVersionGreaterThanOrEqual(vs) {
t.Skip(fmt.Sprintf("This test does not run on cluster versions %v and above", vs))
t.Skipf("This test does not run on cluster versions %v and above", vs)
}
}

Expand Down
15 changes: 8 additions & 7 deletions kubernetes/resource_kubernetes_certificate_signing_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,16 @@ package kubernetes
import (
"context"
"fmt"
"log"
"reflect"
"time"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/retry"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"k8s.io/api/certificates/v1beta1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/util/retry"
"log"
"reflect"
"time"
kretry "k8s.io/client-go/util/retry"
)

func resourceKubernetesCertificateSigningRequest() *schema.Resource {
Expand Down Expand Up @@ -115,7 +116,7 @@ func resourceKubernetesCertificateSigningRequestCreate(ctx context.Context, d *s
defer conn.CertificatesV1beta1().CertificateSigningRequests().Delete(ctx, csrName, metav1.DeleteOptions{})

if d.Get("auto_approve").(bool) {
retryErr := retry.RetryOnConflict(retry.DefaultRetry, func() error {
retryErr := kretry.RetryOnConflict(kretry.DefaultRetry, func() error {
pendingCSR, getErr := conn.CertificatesV1beta1().CertificateSigningRequests().Get(ctx, csrName, metav1.GetOptions{})
if getErr != nil {
return getErr
Expand All @@ -137,7 +138,7 @@ func resourceKubernetesCertificateSigningRequestCreate(ctx context.Context, d *s
}

log.Printf("[DEBUG] Waiting for certificate to be issued")
stateConf := &resource.StateChangeConf{
stateConf := &retry.StateChangeConf{
Target: []string{"Issued"},
Pending: []string{"", "Approved"},
Timeout: d.Timeout(schema.TimeoutCreate),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,18 @@ package kubernetes

import (
"context"
"fmt"
"errors"
"log"
"time"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/retry"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"

certificates "k8s.io/api/certificates/v1"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/util/retry"
kretry "k8s.io/client-go/util/retry"
)

func resourceKubernetesCertificateSigningRequestV1() *schema.Resource {
Expand Down Expand Up @@ -115,7 +115,7 @@ func resourceKubernetesCertificateSigningRequestV1Create(ctx context.Context, d
defer conn.CertificatesV1().CertificateSigningRequests().Delete(ctx, csrName, metav1.DeleteOptions{})

if d.Get("auto_approve").(bool) {
retryErr := retry.RetryOnConflict(retry.DefaultRetry, func() error {
retryErr := kretry.RetryOnConflict(kretry.DefaultRetry, func() error {
pendingCSR, getErr := conn.CertificatesV1().CertificateSigningRequests().Get(
ctx, csrName, metav1.GetOptions{})
if getErr != nil {
Expand All @@ -140,11 +140,11 @@ func resourceKubernetesCertificateSigningRequestV1Create(ctx context.Context, d
}

log.Printf("[DEBUG] Waiting for certificate to be issued")
err = resource.RetryContext(ctx, d.Timeout(schema.TimeoutCreate), func() *resource.RetryError {
err = retry.RetryContext(ctx, d.Timeout(schema.TimeoutCreate), func() *retry.RetryError {
out, err := conn.CertificatesV1().CertificateSigningRequests().Get(ctx, csrName, metav1.GetOptions{})
if err != nil {
log.Printf("[ERROR] Received error: %v", err)
return resource.NonRetryableError(err)
return retry.NonRetryableError(err)
}

// Check to see if a certificate has been issued, and update status accordingly,
Expand All @@ -159,8 +159,7 @@ func resourceKubernetesCertificateSigningRequestV1Create(ctx context.Context, d
}
}
log.Printf("[DEBUG] CertificateSigningRequest %s status received: %#v", csrName, out.Status)
return resource.RetryableError(fmt.Errorf(
"Waiting for certificate to be issued"))
return retry.RetryableError(errors.New("Waiting for certificate to be issued"))
})
if err != nil {
return diag.FromErr(err)
Expand Down
8 changes: 4 additions & 4 deletions kubernetes/resource_kubernetes_cron_job_v1.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/retry"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"k8s.io/apimachinery/pkg/api/errors"

Expand Down Expand Up @@ -179,17 +179,17 @@ func resourceKubernetesCronJobV1Delete(ctx context.Context, d *schema.ResourceDa
return diag.FromErr(err)
}

err = resource.RetryContext(ctx, d.Timeout(schema.TimeoutDelete), func() *resource.RetryError {
err = retry.RetryContext(ctx, d.Timeout(schema.TimeoutDelete), func() *retry.RetryError {
_, err := conn.BatchV1().CronJobs(namespace).Get(ctx, name, metav1.GetOptions{})
if err != nil {
if statusErr, ok := err.(*errors.StatusError); ok && errors.IsNotFound(statusErr) {
return nil
}
return resource.NonRetryableError(err)
return retry.NonRetryableError(err)
}

e := fmt.Errorf("Cron Job %s still exists", name)
return resource.RetryableError(e)
return retry.RetryableError(e)
})
if err != nil {
return diag.FromErr(err)
Expand Down
8 changes: 4 additions & 4 deletions kubernetes/resource_kubernetes_cron_job_v1beta1.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/retry"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"k8s.io/api/batch/v1beta1"
"k8s.io/apimachinery/pkg/api/errors"
Expand Down Expand Up @@ -192,17 +192,17 @@ func resourceKubernetesCronJobV1Beta1Delete(ctx context.Context, d *schema.Resou
return diag.FromErr(err)
}

err = resource.RetryContext(ctx, d.Timeout(schema.TimeoutDelete), func() *resource.RetryError {
err = retry.RetryContext(ctx, d.Timeout(schema.TimeoutDelete), func() *retry.RetryError {
_, err := conn.BatchV1beta1().CronJobs(namespace).Get(ctx, name, metav1.GetOptions{})
if err != nil {
if statusErr, ok := err.(*errors.StatusError); ok && errors.IsNotFound(statusErr) {
return nil
}
return resource.NonRetryableError(err)
return retry.NonRetryableError(err)
}

e := fmt.Errorf("Cron Job %s still exists", name)
return resource.RetryableError(e)
return retry.RetryableError(e)
})
if err != nil {
return diag.FromErr(err)
Expand Down
10 changes: 5 additions & 5 deletions kubernetes/resource_kubernetes_csi_driver_v1.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/retry"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"

Expand All @@ -35,7 +35,7 @@ func resourceKubernetesCSIDriverV1() *schema.Resource {
"metadata": metadataSchema("csi driver", true),
"spec": {
Type: schema.TypeList,
Description: fmt.Sprintf("Spec of the CSIDriver"),
Description: "Spec of the CSIDriver",
Optional: true,
MaxItems: 1,
Elem: &schema.Resource{
Expand Down Expand Up @@ -174,17 +174,17 @@ func resourceKubernetesCSIDriverV1Delete(ctx context.Context, d *schema.Resource
return diag.FromErr(err)
}

err = resource.RetryContext(ctx, d.Timeout(schema.TimeoutDelete), func() *resource.RetryError {
err = retry.RetryContext(ctx, d.Timeout(schema.TimeoutDelete), func() *retry.RetryError {
_, err := conn.StorageV1().CSIDrivers().Get(ctx, d.Id(), metav1.GetOptions{})
if err != nil {
if statusErr, ok := err.(*errors.StatusError); ok && errors.IsNotFound(statusErr) {
return nil
}
return resource.NonRetryableError(err)
return retry.NonRetryableError(err)
}

e := fmt.Errorf("CSIDriver (%s) still exists", d.Id())
return resource.RetryableError(e)
return retry.RetryableError(e)
})
if err != nil {
return diag.FromErr(err)
Expand Down
10 changes: 5 additions & 5 deletions kubernetes/resource_kubernetes_csi_driver_v1beta1.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/retry"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
storage "k8s.io/api/storage/v1beta1"
Expand All @@ -32,7 +32,7 @@ func resourceKubernetesCSIDriverV1Beta1() *schema.Resource {
"metadata": metadataSchema("csi driver", true),
"spec": {
Type: schema.TypeList,
Description: fmt.Sprint("Spec of the CSIDriver"),
Description: "Spec of the CSIDriver",
Optional: true,
MaxItems: 1,
Elem: &schema.Resource{
Expand Down Expand Up @@ -171,17 +171,17 @@ func resourceKubernetesCSIDriverV1Beta1Delete(ctx context.Context, d *schema.Res
return diag.FromErr(err)
}

err = resource.RetryContext(ctx, d.Timeout(schema.TimeoutDelete), func() *resource.RetryError {
err = retry.RetryContext(ctx, d.Timeout(schema.TimeoutDelete), func() *retry.RetryError {
_, err := conn.StorageV1beta1().CSIDrivers().Get(ctx, d.Id(), metav1.GetOptions{})
if err != nil {
if statusErr, ok := err.(*errors.StatusError); ok && errors.IsNotFound(statusErr) {
return nil
}
return resource.NonRetryableError(err)
return retry.NonRetryableError(err)
}

e := fmt.Errorf("CSIDriver (%s) still exists", d.Id())
return resource.RetryableError(e)
return retry.RetryableError(e)
})
if err != nil {
return diag.FromErr(err)
Expand Down
Loading