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

Adding golangci linting and also more GH actions #613

Merged
merged 2 commits into from
Mar 25, 2024
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
2 changes: 1 addition & 1 deletion .github/ISSUE_TEMPLATE/bug_report.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -106,4 +106,4 @@ body:
description: By submitting this issue, you agree to follow our [Code of Conduct](https:/fluxcd/terraform-provider-flux/blob/main/CODE_OF_CONDUCT.md)
options:
- label: I agree to follow this project's Code of Conduct
required: true
required: true
76 changes: 68 additions & 8 deletions .github/workflows/tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,43 @@ on:
branches: [ main ]

jobs:
# Ensure project builds before running testing matrix
build:
name: Build
runs-on: ubuntu-latest
timeout-minutes: 5
steps:
- uses: actions/checkout@9bb56186c3b09b4f86b1c65136769dd318469633 # v4.1.2
- uses: actions/setup-go@0c52d547c9bc32b1aa3301fd7a9cb496313a4491 # v5.0.0
with:
go-version-file: 'go.mod'
cache: true
- run: go mod download
- run: go build -v .
- name: Run linters
uses: golangci/golangci-lint-action@3cfe3a4abbb849e10058ce4af15d205b6da42804 # v4.0.0
with:
version: latest
generate:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@9bb56186c3b09b4f86b1c65136769dd318469633 # v4.1.2
- uses: actions/setup-go@0c52d547c9bc32b1aa3301fd7a9cb496313a4491 # v5.0.0
with:
go-version-file: 'go.mod'
cache: true
# Temporarily download Terraform 1.8 prerelease for function documentation support.
# When Terraform 1.8.0 final is released, this can be removed.
- uses: hashicorp/setup-terraform@a1502cd9e758c50496cc9ac5308c4843bcd56d36 # v3.0.0
with:
terraform_version: '1.8.0-alpha20240216'
terraform_wrapper: false
- run: go generate ./...
- name: git diff
run: |
git diff --compact-summary --exit-code || \
(echo; echo "Unexpected difference in directories after code generation. Run 'go generate ./...' command and commit."; exit 1)

docs:
runs-on: ubuntu-latest
steps:
Expand All @@ -31,17 +68,40 @@ jobs:
echo 'run make docs and commit changes'
exit 1
fi
acceptance:

# Run acceptance tests in a matrix with Terraform CLI versions
test:
name: Terraform Provider Acceptance Tests
needs: build
runs-on: ubuntu-latest
timeout-minutes: 15
strategy:
fail-fast: false
matrix:
terraform:
- '1.0.*'
- '1.1.*'
- '1.2.*'
- '1.3.*'
- '1.4.*'
- '1.5.*'
- '1.6.*'
- '1.7.*'
steps:
- name: Checkout
uses: actions/checkout@9bb56186c3b09b4f86b1c65136769dd318469633 # v4.1.2
- name: Setup Go
uses: actions/setup-go@0c52d547c9bc32b1aa3301fd7a9cb496313a4491 # v5.0.0
- uses: actions/checkout@9bb56186c3b09b4f86b1c65136769dd318469633 # v4.1.2
- uses: actions/setup-go@0c52d547c9bc32b1aa3301fd7a9cb496313a4491 # v5.0.0
with:
go-version: 1.20.x
- name: Run tests
run: make testacc
go-version-file: 'go.mod'
cache: true
- uses: hashicorp/setup-terraform@a1502cd9e758c50496cc9ac5308c4843bcd56d36 # v3.0.0
with:
terraform_version: ${{ matrix.terraform }}
terraform_wrapper: false
- run: go mod download
- env:
TF_ACC: "1"
run: go test -v -cover ./internal/provider/
timeout-minutes: 10
e2e:
runs-on: ubuntu-latest
steps:
Expand Down
26 changes: 26 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Visit https://golangci-lint.run/ for usage documentation
# and information on other useful linters
issues:
max-per-linter: 0
max-same-issues: 0

linters:
disable-all: true
enable:
- durationcheck
- errcheck
- exportloopref
- forcetypeassert
- godot
- gofmt
- gosimple
- makezero
- misspell
- nilerr
- predeclared
- staticcheck
- tenv
- unconvert
- unparam
- unused
- vet
23 changes: 23 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
repos:
- repo: https:/pre-commit/pre-commit-hooks
rev: v4.3.0
hooks:
- id: check-byte-order-marker
- id: check-case-conflict
- id: check-merge-conflict
- id: check-symlinks
- id: detect-aws-credentials
args: ['--allow-missing-credentials']
- id: detect-private-key
- id: end-of-file-fixer
- id: mixed-line-ending
- id: trailing-whitespace
- repo: https:/dnephin/pre-commit-golang.git
rev: v0.5.0
hooks:
- id: go-fmt
- id: go-imports
- id: go-mod-tidy
- id: go-build
- id: go-unit-tests
- id: golangci-lint
1 change: 0 additions & 1 deletion docs/guides/extra-syncs.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,3 @@ resource "helm_release" "my-app-sync" {
]
}
```

4 changes: 2 additions & 2 deletions docs/guides/github.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ resource "kind_cluster" "this" {
```

The GitHub repository is created separatly so a datasource is used to get a reference to the repository. Creating GitHub repositories with Terraform is generally not a good idea as they could easily be removed. Additionally it is not possible to use the same repository for multiple environments if the repository is created with Terraform.

```terraform
provider "github" {
owner = var.github_org
Expand All @@ -80,7 +80,7 @@ resource "github_repository_deploy_key" "this" {
}
```

The Flux provider needs to be configured both with Git and Kubernetes credentials.
The Flux provider needs to be configured both with Git and Kubernetes credentials.

```terraform
provider "flux" {
Expand Down
4 changes: 2 additions & 2 deletions docs/guides/gitlab.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ resource "kind_cluster" "this" {
```

The GitLab repository is created separatly so a datasource is used to get a reference to the repository. Creating GitLab repositories with Terraform is generally not a good idea as they could easily be removed. Additionally it is not possible to use the same repository for multiple environments if the repository is created with Terraform.

```terraform
provider "gitlab" {
token = var.gitlab_token
Expand All @@ -84,7 +84,7 @@ resource "gitlab_deploy_key" "this" {
}
```

The Flux provider needs to be configured both with Git and Kubernetes credentials.
The Flux provider needs to be configured both with Git and Kubernetes credentials.

```terraform
provider "flux" {
Expand Down
2 changes: 1 addition & 1 deletion docs/guides/migrating-to-resource.md
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ terraform state rm kubernetes_namespace.flux_system
terraform state rm kubectl_manifest.install
terraform state rm kubectl_manifest.sync
terraform state rm kubernetes_secret.this
terraform state rm data.flux_install.this
terraform state rm data.flux_install.this
terraform state rm data.flux_sync.this
terraform state rm data.kubectl_file_documents.install
terraform state rm data.kubectl_file_documents.sync
Expand Down
2 changes: 1 addition & 1 deletion docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -190,4 +190,4 @@ Required:
Optional:

- `args` (List of String) Client authentication exec command arguments
- `env` (Map of String) Client authentication exec environment variables
- `env` (Map of String) Client authentication exec environment variables
4 changes: 2 additions & 2 deletions docs/resources/bootstrap_git.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ patches:
template:
metadata:
annotations:
cluster-autoscaler.kubernetes.io/safe-to-evict: "true"
cluster-autoscaler.kubernetes.io/safe-to-evict: "true"
target:
kind: Deployment
labelSelector: app.kubernetes.io/part-of=flux
Expand Down Expand Up @@ -131,7 +131,7 @@ patches:
- effect: NoSchedule
key: role
operator: Equal
value: flux
value: flux
target:
kind: Deployment
labelSelector: app.kubernetes.io/part-of=flux
Expand Down
1 change: 0 additions & 1 deletion examples/.tflint.hcl
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,3 @@ rule "terraform_standard_module_structure" {
rule "terraform_workspace_remote" {
enabled = true
}

1 change: 0 additions & 1 deletion examples/customize-flux/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,3 @@ variable "clone_url" {
type = string
description = "Git repository clone url"
}

Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ patches:
template:
metadata:
annotations:
cluster-autoscaler.kubernetes.io/safe-to-evict: "true"
cluster-autoscaler.kubernetes.io/safe-to-evict: "true"
target:
kind: Deployment
labelSelector: app.kubernetes.io/part-of=flux
4 changes: 2 additions & 2 deletions examples/resources/flux_bootstrap_git/kustomization-node.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ patches:
- effect: NoSchedule
key: role
operator: Equal
value: flux
value: flux
target:
kind: Deployment
labelSelector: app.kubernetes.io/part-of=flux
labelSelector: app.kubernetes.io/part-of=flux
8 changes: 4 additions & 4 deletions internal/provider/data_install.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,13 +194,13 @@ func (s *installDataSource) Read(ctx context.Context, req datasource.ReadRequest

// Set data values to option
opt.TargetPath = data.TargetPath.ValueString()
components := []string{}
var components []string
diags = data.Components.ElementsAs(ctx, &components, false)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
componentsExtra := []string{}
var componentsExtra []string
diags = data.ComponentsExtra.ElementsAs(ctx, &componentsExtra, false)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
Expand All @@ -209,7 +209,7 @@ func (s *installDataSource) Read(ctx context.Context, req datasource.ReadRequest
components = append(components, componentsExtra...)
opt.Components = components
opt.ComponentsExtra = componentsExtra
tolerationKeys := []string{}
var tolerationKeys []string
diags = data.TolerationKeys.ElementsAs(ctx, &tolerationKeys, false)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
Expand All @@ -231,7 +231,7 @@ func (s *installDataSource) Read(ctx context.Context, req datasource.ReadRequest
// Compute manifests
manifest, err := install.Generate(opt, "")
if err != nil {
resp.Diagnostics.AddError("coudl not generate manifests", err.Error())
resp.Diagnostics.AddError("could not generate manifests", err.Error())
return
}

Expand Down
8 changes: 4 additions & 4 deletions internal/provider/data_install_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,17 @@ func TestAccDataInstall_basic(t *testing.T) {
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories,
Steps: []resource.TestStep{
{
// Without required target_path set
// Without required target_path set.
Config: testAccDataInstallEmpty,
ExpectError: regexp.MustCompile(`The argument "target_path" is required, but no definition was found\.`),
},
{
// With invalid log level
// With invalid log level.
Config: testAccDataInstallLogLevel,
ExpectError: regexp.MustCompile(`Invalid Attribute Value Match`),
},
{
// Check default values
// Check default values.
Config: testAccDataInstallBasic,
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttrSet(resourceName, "content"),
Expand All @@ -58,7 +58,7 @@ func TestAccDataInstall_basic(t *testing.T) {
resource.TestCheckResourceAttr(resourceName, "baseurl", "https:/fluxcd/flux2/releases"),
),
},
// Ensure attribute value changes are propagated correctly into the state
// Ensure attribute value changes are propagated correctly into the state.
{
Config: testAccDataInstallWithArg("log_level", "debug"),
Check: resource.TestCheckResourceAttr(resourceName, "log_level", "debug"),
Expand Down
10 changes: 5 additions & 5 deletions internal/provider/data_sync_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,22 +30,22 @@ func TestAccDataSync_Basic(t *testing.T) {
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories,
Steps: []resource.TestStep{
{
// Without required target_path set
// Without required target_path set.
Config: testAccDataSyncMissingTargetPath,
ExpectError: regexp.MustCompile(`The argument "target_path" is required, but no definition was found\.`),
},
{
// Without required url set
// Without required url set.
Config: testAccDataSyncMissingURL,
ExpectError: regexp.MustCompile(`The argument "url" is required, but no definition was found\.`),
},
{
// Incorrect url syntax
// Incorrect url syntax.
Config: testAccDataSyncInCorrectURL,
ExpectError: regexp.MustCompile(`Invalid URL scheme`),
},
{
// Check default values
// Check default values.
Config: testAccDataSyncBasic,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrSet(resourceName, "content"),
Expand All @@ -60,7 +60,7 @@ func TestAccDataSync_Basic(t *testing.T) {
resource.TestCheckResourceAttr(resourceName, "content", testAccDataSyncBasicExpectedContent),
),
},
// Ensure attribute value changes are propagated correctly into the state
// Ensure attribute value changes are propagated correctly into the state.
{
Config: testAccDataSyncWithArg("namespace", "test-system"),
Check: resource.TestCheckResourceAttr(resourceName, "namespace", "test-system"),
Expand Down
2 changes: 1 addition & 1 deletion internal/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,7 @@ func (p *fluxProvider) Configure(ctx context.Context, req provider.ConfigureRequ
return
}

// Set default values
// Set default values.
if data.Git.Branch.IsNull() {
data.Git.Branch = types.StringValue(defaultBranch)
}
Expand Down
Loading
Loading