Skip to content

Commit 7daebc7

Browse files
authored
misc: lint and format ci/cd (#196)
Added a CI/CD workflow to lint and format the code.
1 parent 5b47a13 commit 7daebc7

8 files changed

Lines changed: 793 additions & 54 deletions

File tree

.github/workflows/lint-fmt.yaml

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
---
2+
on:
3+
pull_request: {}
4+
5+
name: Lint & Format
6+
7+
jobs:
8+
lint:
9+
name: Lint
10+
runs-on: ubuntu-latest
11+
steps:
12+
- name: Checkout Code
13+
uses: actions/checkout@v5
14+
15+
- name: Set up Go
16+
uses: actions/setup-go@v5
17+
with:
18+
go-version-file: go.mod
19+
20+
- name: Lint
21+
run: make lint
22+
23+
fmt:
24+
name: Format
25+
runs-on: ubuntu-latest
26+
steps:
27+
- name: Checkout Code
28+
uses: actions/checkout@v5
29+
30+
- name: Set up Go
31+
uses: actions/setup-go@v5
32+
with:
33+
go-version-file: go.mod
34+
35+
- name: Format
36+
run: make fmt
37+
38+
- name: Check for Changes
39+
run: |
40+
if ! git diff --quiet --exit-code; then
41+
echo "::error::Code is not formatted. Run 'make fmt' and commit the result."
42+
git diff
43+
exit 1
44+
fi

.golangci.yaml

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
---
2+
version: "2"
3+
4+
linters:
5+
default: standard
6+
enable:
7+
- godoclint
8+
- unparam
9+
disable:
10+
- errcheck
11+
settings:
12+
godoclint:
13+
default-rule-set: basic
14+
rules:
15+
- max-len
16+
options:
17+
max-len:
18+
length: 100
19+
20+
formatters:
21+
enable:
22+
- goimports
23+
- golines
24+
settings:
25+
golines:
26+
max-len: 100
27+
shorten-comments: true
28+
exclusions:
29+
generated: disable
30+
31+
run:
32+
timeout: 5m

Makefile

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,3 +72,13 @@ helm-package: helm-set-version
7272
helm-push: helm-package
7373
$(HELM) push $(IMAGE_NAME)-$(patsubst v%,%,$(VERSION)).tgz \
7474
$(HELM_CHART_REGISTRY)
75+
76+
.PHONY: fmt
77+
fmt:
78+
@ echo "+ Formatting Go code..."
79+
@ go tool -modfile tools/go.mod golangci-lint fmt
80+
81+
.PHONY: lint
82+
lint:
83+
@ echo "+ Running Go linters..."
84+
@ go tool -modfile tools/go.mod golangci-lint run

internal/provider/instances_v2.go

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// This Source Code Form is subject to the terms of the Mozilla Public
22
// License, v. 2.0. If a copy of the MPL was not distributed with this
33
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
4+
45
package provider
56

67
import (
@@ -55,7 +56,10 @@ func (i *InstancesV2) InstanceExists(ctx context.Context, node *v1.Node) (bool,
5556

5657
// InstanceMetadata populates the metadata for the provided node, notably
5758
// setting its provider ID.
58-
func (i *InstancesV2) InstanceMetadata(ctx context.Context, node *v1.Node) (*cloudprovider.InstanceMetadata, error) {
59+
func (i *InstancesV2) InstanceMetadata(
60+
ctx context.Context,
61+
node *v1.Node,
62+
) (*cloudprovider.InstanceMetadata, error) {
5963
ctx, cancel := context.WithTimeout(ctx, 30*time.Second)
6064
defer cancel()
6165

@@ -73,9 +77,12 @@ func (i *InstancesV2) InstanceMetadata(ctx context.Context, node *v1.Node) (*clo
7377
return nil, fmt.Errorf("failed viewing oxide instance: %v", err)
7478
}
7579

76-
nics, err := i.client.InstanceNetworkInterfaceList(ctx, oxide.InstanceNetworkInterfaceListParams{
77-
Instance: oxide.NameOrId(instanceID),
78-
})
80+
nics, err := i.client.InstanceNetworkInterfaceList(
81+
ctx,
82+
oxide.InstanceNetworkInterfaceListParams{
83+
Instance: oxide.NameOrId(instanceID),
84+
},
85+
)
7986
if err != nil {
8087
return nil, fmt.Errorf("failed listing instance network interfaces: %v", err)
8188
}

internal/provider/provider.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// This Source Code Form is subject to the terms of the Mozilla Public
22
// License, v. 2.0. If a copy of the MPL was not distributed with this
33
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
4+
45
package provider
56

67
import (
@@ -44,7 +45,10 @@ type Oxide struct {
4445

4546
// Initialize creates the Oxide and Kubernetes clients and spawns any additional
4647
// controllers, if necessary.
47-
func (o *Oxide) Initialize(clientBuilder cloudprovider.ControllerClientBuilder, stop <-chan struct{}) {
48+
func (o *Oxide) Initialize(
49+
clientBuilder cloudprovider.ControllerClientBuilder,
50+
stop <-chan struct{},
51+
) {
4852
kubernetesClient, err := clientBuilder.Client(Name)
4953
if err != nil {
5054
klog.Fatalf("failed to create kubernetes client: %v", err)

internal/provider/provider_test.go

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// This Source Code Form is subject to the terms of the Mozilla Public
22
// License, v. 2.0. If a copy of the MPL was not distributed with this
33
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
4+
45
package provider
56

67
import (
@@ -31,11 +32,20 @@ func TestInstanceIDFromProviderID(t *testing.T) {
3132
t.Run(tc.name, func(t *testing.T) {
3233
result, err := InstanceIDFromProviderID(tc.providerID)
3334
if err != nil {
34-
t.Errorf("TestInstanceIDFromProviderID(%s) returned non-nil error %v, want nil error", tc.providerID, err)
35+
t.Errorf(
36+
"TestInstanceIDFromProviderID(%s) returned non-nil error %v, want nil error",
37+
tc.providerID,
38+
err,
39+
)
3540
}
3641

3742
if result != tc.expected {
38-
t.Errorf("TestInstanceIDFromProviderID(%s) returned %s, want %s", tc.providerID, result, tc.expected)
43+
t.Errorf(
44+
"TestInstanceIDFromProviderID(%s) returned %s, want %s",
45+
tc.providerID,
46+
result,
47+
tc.expected,
48+
)
3949
}
4050
})
4151
}
@@ -83,11 +93,19 @@ func TestInstanceIDFromProviderID(t *testing.T) {
8393
t.Run(tc.name, func(t *testing.T) {
8494
_, err := InstanceIDFromProviderID(tc.providerID)
8595
if err == nil {
86-
t.Errorf("TestInstanceIDFromProviderID(%s) returned nil error, want non-nil error", tc.providerID)
96+
t.Errorf(
97+
"TestInstanceIDFromProviderID(%s) returned nil error, want non-nil error",
98+
tc.providerID,
99+
)
87100
}
88101

89102
if !strings.Contains(err.Error(), tc.errorMsg) {
90-
t.Errorf("TestInstanceIDFromProviderID(%s) returned error %v, want %s", tc.providerID, err.Error(), tc.errorMsg)
103+
t.Errorf(
104+
"TestInstanceIDFromProviderID(%s) returned error %v, want %s",
105+
tc.providerID,
106+
err.Error(),
107+
tc.errorMsg,
108+
)
91109
}
92110
})
93111
}

0 commit comments

Comments
 (0)