Skip to content
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
4 changes: 2 additions & 2 deletions .gitlab-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ build:

test:lint:
stage: test
image: golangci/golangci-lint:v1.64
image: golangci/golangci-lint:v2.5
variables:
GOLANGCI_LINT_CACHE: $CI_PROJECT_DIR/.cache/golangci-lint
cache:
Expand All @@ -36,7 +36,7 @@ test:lint:
paths:
- $GOLANGCI_LINT_CACHE
script:
- golangci-lint run -v --timeout 5m
- golangci-lint run -v

test:unit:
stage: test
Expand Down
135 changes: 92 additions & 43 deletions .golangci.yaml
Original file line number Diff line number Diff line change
@@ -1,67 +1,116 @@
---
linters-settings:
exhaustive:
default-signifies-exhaustive: true
gci:
sections:
- standard
- default
- prefix(github.com/hetznercloud)
version: "2"

importas:
no-unaliased: true
alias:
# Kubernetes
- pkg: k8s.io/api/core/v1
alias: corev1
- pkg: k8s.io/apimachinery/pkg/apis/meta/v1
alias: metav1

- pkg: github.com/syself/hrobot-go
alias: hrobot
- pkg: github.com/syself/hrobot-go/models
alias: hrobotmodels

misspell:
locale: "US"
run:
timeout: 5m

linters:
disable-all: true
enable:
- asasalint
- asciicheck
- bidichk
- bodyclose
- contextcheck
- copyloopvar
- durationcheck
- errcheck
- errchkjson
- errname
- errorlint
- exhaustive
- gci
- forbidigo
- gocheckcompilerdirectives
- gochecksumtype
- gocritic
- godot
- goimports
- gomoddirectives
- gomodguard
- gosec
- gosimple
- gosmopolitan
- govet
- importas
- ineffassign
- loggercheck
- makezero
- misspell
- musttag
- nilerr
- nilnesserr
- noctx
- prealloc
- protogetter
- reassign
- recvcheck
- revive
- rowserrcheck
- spancheck
- staticcheck
- typecheck
- testifylint
- unparam
- unused
- whitespace

issues:
exclude-rules:
- path: internal/annotation/load_balancer.go
linters:
- godot
- path: (_test\.go|testing\.go|testsupport|e2etests)
linters:
- gosec
- errcheck
- path: internal/mocks
linters:
- unparam
- revive
settings:
exhaustive:
default-signifies-exhaustive: true

staticcheck:
checks: ["all", "-QF1008"]

gomoddirectives:
replace-allow-list:
- github.com/hetznercloud/hcloud-go/v2
- k8s.io/cloud-provider
- k8s.io/controller-manager

importas:
alias:
- pkg: k8s.io/api/core/v1
alias: corev1
- pkg: k8s.io/apimachinery/pkg/apis/meta/v1
alias: metav1
- pkg: github.com/syself/hrobot-go
alias: hrobot
- pkg: github.com/syself/hrobot-go/models
alias: hrobotmodels
no-unaliased: true

exclusions:
generated: lax
presets:
- comments
- common-false-positives
- std-error-handling
rules:
- linters:
- testifylint
text: require-error
- linters:
- godot
path: internal/annotation/load_balancer.go
- linters:
- errcheck
- gosec
path: (_test\.go|testing\.go|testsupport|e2etests)
- linters:
- revive
- unparam
path: internal/mocks
- linters:
- revive
path: internal/utils/.+\.go
text: var-naming

formatters:
enable:
- gci
- goimports

settings:
gci:
sections:
- standard
- default
- prefix(github.com/hetznercloud)

exclusions:
generated: lax
3 changes: 1 addition & 2 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,9 @@ repos:
- id: shellcheck

- repo: https://github.com/golangci/golangci-lint
rev: v1.64.8
rev: v2.5.0
hooks:
- id: golangci-lint-full
args: [--timeout=5m]
language_version: 1.25.1 # renovate: datasource=golang-version depName=go

- repo: local
Expand Down
2 changes: 1 addition & 1 deletion hcloud/cloud.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ func serverIsAttachedToNetwork(metadataClient *metadata.Client, networkID int64)

serverPrivateNetworks, err := metadataClient.PrivateNetworks()
if err != nil {
return false, fmt.Errorf("%s: %s", op, err)
return false, fmt.Errorf("%s: %w", op, err)
}
return strings.Contains(serverPrivateNetworks, fmt.Sprintf("network_id: %d\n", networkID)), nil
}
8 changes: 4 additions & 4 deletions hcloud/load_balancers.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ func (l *loadBalancers) GetLoadBalancer(
if errors.Is(err, hcops.ErrNotFound) {
return nil, false, nil
}
return nil, false, fmt.Errorf("%s: %v", op, err)
return nil, false, fmt.Errorf("%s: %w", op, err)
}

if v, ok := annotation.LBHostname.StringFromService(service); ok {
Expand All @@ -89,7 +89,7 @@ func (l *loadBalancers) GetLoadBalancer(

ingress, err := l.buildLoadBalancerStatusIngress(lb, service)
if err != nil {
return nil, false, fmt.Errorf("%s: %v", op, err)
return nil, false, fmt.Errorf("%s: %w", op, err)
}

return &corev1.LoadBalancerStatus{Ingress: ingress}, true, nil
Expand Down Expand Up @@ -128,7 +128,7 @@ func (l *loadBalancers) EnsureLoadBalancer(

lb, err = l.lbOps.GetByK8SServiceUID(ctx, svc)
if err != nil && !errors.Is(err, hcops.ErrNotFound) {
return nil, fmt.Errorf("%s: %v", op, err)
return nil, fmt.Errorf("%s: %w", op, err)
}

// Try the load balancer's name if we were not able to find it using the
Expand All @@ -143,7 +143,7 @@ func (l *loadBalancers) EnsureLoadBalancer(
if errors.Is(err, hcops.ErrNotFound) {
lb, err = l.lbOps.GetByName(ctx, lbName)
if err != nil && !errors.Is(err, hcops.ErrNotFound) {
return nil, fmt.Errorf("%s: %v", op, err)
return nil, fmt.Errorf("%s: %w", op, err)
}
}

Expand Down
22 changes: 11 additions & 11 deletions hcloud/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ func (r *routes) ListRoutes(ctx context.Context, _ string) ([]*cloudprovider.Rou

routes := make([]*cloudprovider.Route, 0, len(r.network.Routes))
for _, route := range r.network.Routes {
ro, err := r.hcloudRouteToRoute(route)
ro, err := r.hcloudRouteToRoute(ctx, route)
if err != nil {
return routes, fmt.Errorf("%s: %w", op, err)
}
Expand All @@ -106,17 +106,17 @@ func (r *routes) CreateRoute(ctx context.Context, clusterName string, nameHint s
const op = "hcloud/CreateRoute"
metrics.OperationCalled.WithLabelValues(op).Inc()

srv, err := r.serverCache.ByName(string(route.TargetNode))
srv, err := r.serverCache.ByName(ctx, string(route.TargetNode))
if err != nil {
return fmt.Errorf("%s: %v", op, err)
return fmt.Errorf("%s: %w", op, err)
}

privNet, ok := findServerPrivateNetByID(srv, r.network.ID)
if !ok {
r.serverCache.InvalidateCache()
srv, err = r.serverCache.ByName(string(route.TargetNode))
srv, err = r.serverCache.ByName(ctx, string(route.TargetNode))
if err != nil {
return fmt.Errorf("%s: %v", op, err)
return fmt.Errorf("%s: %w", op, err)
}

privNet, ok = findServerPrivateNetByID(srv, r.network.ID)
Expand All @@ -134,7 +134,7 @@ func (r *routes) CreateRoute(ctx context.Context, clusterName string, nameHint s
clusterNetSize, _ := r.clusterCIDR.Mask.Size()
destNetSize, _ := cidr.Mask.Size()

if !(r.clusterCIDR.Contains(cidr.IP) && destNetSize >= clusterNetSize) {
if !r.clusterCIDR.Contains(cidr.IP) || destNetSize < clusterNetSize {
node := &corev1.Node{
ObjectMeta: metav1.ObjectMeta{
Name: string(route.TargetNode),
Expand Down Expand Up @@ -250,7 +250,7 @@ func (r *routes) deleteRouteFromHcloud(ctx context.Context, cidr *net.IPNet, ip
return nil
}

func (r *routes) hcloudRouteToRoute(route hcloud.NetworkRoute) (*cloudprovider.Route, error) {
func (r *routes) hcloudRouteToRoute(ctx context.Context, route hcloud.NetworkRoute) (*cloudprovider.Route, error) {
const op = "hcloud/hcloudRouteToRoute"
metrics.OperationCalled.WithLabelValues(op).Inc()

Expand All @@ -259,7 +259,7 @@ func (r *routes) hcloudRouteToRoute(route hcloud.NetworkRoute) (*cloudprovider.R
Name: fmt.Sprintf("%s-%s", route.Gateway.String(), route.Destination.String()),
}

srv, err := r.serverCache.ByPrivateIP(route.Gateway)
srv, err := r.serverCache.ByPrivateIP(ctx, route.Gateway)
if err != nil {
if errors.Is(err, hcops.ErrNotFound) {
// Route belongs to non-existing target
Expand All @@ -284,9 +284,9 @@ func (r *routes) checkIfRouteAlreadyExists(ctx context.Context, route *cloudprov

for _, _route := range r.network.Routes {
if _route.Destination.String() == route.DestinationCIDR {
srv, err := r.serverCache.ByName(string(route.TargetNode))
srv, err := r.serverCache.ByName(ctx, string(route.TargetNode))
if err != nil {
return false, fmt.Errorf("%s: %v", op, err)
return false, fmt.Errorf("%s: %w", op, err)
}
privNet, ok := findServerPrivateNetByID(srv, r.network.ID)
if !ok {
Expand All @@ -295,7 +295,7 @@ func (r *routes) checkIfRouteAlreadyExists(ctx context.Context, route *cloudprov
ip := privNet.IP

if !_route.Gateway.Equal(ip) {
action, _, err := r.client.Network.DeleteRoute(context.Background(), r.network, hcloud.NetworkDeleteRouteOpts{
action, _, err := r.client.Network.DeleteRoute(ctx, r.network, hcloud.NetworkDeleteRouteOpts{
Route: _route,
})
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ func getEnvBool(key string, defaultValue bool) (bool, error) {

b, err := strconv.ParseBool(v)
if err != nil {
return false, fmt.Errorf("failed to parse %s: %v", key, err)
return false, fmt.Errorf("failed to parse %s: %w", key, err)
}

return b, nil
Expand All @@ -275,7 +275,7 @@ func getEnvDuration(key string) (time.Duration, error) {

b, err := time.ParseDuration(v)
if err != nil {
return 0, fmt.Errorf("failed to parse %s: %v", key, err)
return 0, fmt.Errorf("failed to parse %s: %w", key, err)
}

return b, nil
Expand Down
4 changes: 2 additions & 2 deletions internal/hcops/certificates.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func (co *CertificateOps) GetCertificateByLabel(ctx context.Context, label strin
opts := hcloud.CertificateListOpts{ListOpts: hcloud.ListOpts{LabelSelector: label}}
certs, err := co.CertClient.AllWithOpts(ctx, opts)
if err != nil {
return nil, fmt.Errorf("%s: %v", op, err)
return nil, fmt.Errorf("%s: %w", op, err)
}
if len(certs) == 0 {
return nil, fmt.Errorf("%s: %w", op, ErrNotFound)
Expand Down Expand Up @@ -86,7 +86,7 @@ func (co *CertificateOps) CreateManagedCertificate(
return fmt.Errorf("%s: %w", op, ErrAlreadyExists)
}
if err != nil {
return fmt.Errorf("%s: %v", op, err)
return fmt.Errorf("%s: %w", op, err)
}
return nil
}
8 changes: 4 additions & 4 deletions internal/hcops/load_balancer.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ func (l *LoadBalancerOps) GetByK8SServiceUID(ctx context.Context, svc *corev1.Se
}
lbs, err := l.LBClient.AllWithOpts(ctx, opts)
if err != nil {
return nil, fmt.Errorf("%s: api error: %v", op, err)
return nil, fmt.Errorf("%s: api error: %w", op, err)
}
if len(lbs) == 0 {
return nil, fmt.Errorf("%s: %w", op, ErrNotFound)
Expand Down Expand Up @@ -236,7 +236,7 @@ func (l *LoadBalancerOps) Delete(ctx context.Context, lb *hcloud.LoadBalancer) e
return nil
}
if err != nil {
return fmt.Errorf("%s: %v", op, err)
return fmt.Errorf("%s: %w", op, err)
}
return nil
}
Expand Down Expand Up @@ -885,7 +885,7 @@ func (l *LoadBalancerOps) ReconcileHCLBServices(
var changed bool

if err := l.reconcileManagedCertificate(ctx, svc); err != nil {
return false, fmt.Errorf("%s: %v", op, err)
return false, fmt.Errorf("%s: %w", op, err)
}

hclbListenPorts := make(map[int]bool, len(lb.Services))
Expand Down Expand Up @@ -1001,7 +1001,7 @@ func (l *LoadBalancerOps) reconcileManagedCertificate(ctx context.Context, svc *
return nil
}
if err != nil {
return fmt.Errorf("%s: %v", op, err)
return fmt.Errorf("%s: %w", op, err)
}
return nil
}
Expand Down
Loading