Skip to content

Commit 9788e83

Browse files
committed
chore: lint code and improve linter settings
Besides lint of the code this commit introduces: - Explicit allowed modules with depguard - Add missing license header. - Fix GCI referenced project go module name. Signed-off-by: Massimiliano Giovagnoli <[email protected]>
1 parent 971d1b7 commit 9788e83

18 files changed

+98
-14
lines changed

.golangci.yml

+18-2
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,33 @@
11
linters-settings:
22
cyclop:
33
max-complexity: 27
4+
depguard:
5+
rules:
6+
main:
7+
list-mode: lax
8+
allow:
9+
- $gostd
10+
- k8s.io/api
11+
- k8s.io/apimachinery
12+
- k8s.io/client-go
13+
- github.com/projectcapsule
14+
- github.com/go-logr/logr
15+
- github.com/pkg/errors
16+
- github.com/spf13/cobra
17+
- sigs.k8s.io/controller-runtime
18+
funlen:
19+
lines: 110
420
gci:
521
sections:
622
- standard # Captures all standard packages if they do not match another section.
723
- default # Contains all imports that could not be matched to another section type.
8-
- prefix(github.com/projectcapsule/capsule-addon-fluxcd) # Groups all imports with the specified Prefix.
24+
- prefix(github.com/projectcapsule/capsule-addon-flux) # Groups all imports with the specified Prefix.
925
goconst:
1026
min-len: 2
1127
min-occurrences: 3
1228
goheader:
1329
template: |-
14-
Copyright 2020-2023 Project Capsule Authors.
30+
Copyright 2020-2024 Project Capsule Authors.
1531
SPDX-License-Identifier: Apache-2.0
1632
govet:
1733
check-shadowing: true

cmd/constants.go

+3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
// Copyright 2020-2024 Project Capsule Authors.
2+
// SPDX-License-Identifier: Apache-2.0
3+
14
package cmd
25

36
const (

cmd/manager/constants.go

+3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
// Copyright 2020-2024 Project Capsule Authors.
2+
// SPDX-License-Identifier: Apache-2.0
3+
14
package manager
25

36
const (

cmd/manager/manager.go

+11-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
1+
// Copyright 2020-2024 Project Capsule Authors.
2+
// SPDX-License-Identifier: Apache-2.0
3+
14
package manager
25

36
import (
47
"flag"
58
"fmt"
9+
"os"
10+
611
"github.com/go-logr/logr"
712
"github.com/pkg/errors"
813
capsulev1beta2 "github.com/projectcapsule/capsule/api/v1beta2"
@@ -11,7 +16,6 @@ import (
1116
"k8s.io/apimachinery/pkg/runtime"
1217
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
1318
"k8s.io/client-go/rest"
14-
"os"
1519
ctrl "sigs.k8s.io/controller-runtime"
1620
"sigs.k8s.io/controller-runtime/pkg/client"
1721
"sigs.k8s.io/controller-runtime/pkg/healthz"
@@ -56,6 +60,7 @@ func New() *cobra.Command {
5660

5761
// Add Zap options.
5862
var fs flag.FlagSet
63+
5964
opts.Zo.BindFlags(&fs)
6065
cmd.Flags().AddGoFlagSet(&fs)
6166

@@ -67,6 +72,7 @@ func (o *Options) Run(_ *cobra.Command, _ []string) error {
6772
if err := clientgoscheme.AddToScheme(scheme); err != nil {
6873
return errors.Wrap(err, "unable to add client-go types to the manager's scheme")
6974
}
75+
7076
if err := capsulev1beta2.AddToScheme(scheme); err != nil {
7177
return errors.Wrap(err, "unable to add Capsule types to the manager's scheme")
7278
}
@@ -87,6 +93,7 @@ func (o *Options) Run(_ *cobra.Command, _ []string) error {
8793
})
8894
if err != nil {
8995
o.SetupLog.Error(err, "unable to create manager")
96+
9097
return errors.Wrap(err, "unable to create manager")
9198
}
9299

@@ -102,6 +109,7 @@ func (o *Options) Run(_ *cobra.Command, _ []string) error {
102109

103110
if err = indexer.AddToManager(ctx, o.SetupLog, mgr); err != nil {
104111
o.SetupLog.Error(err, "unable to setup indexers")
112+
105113
return errors.Wrap(err, "unable to setup indexers")
106114
}
107115

@@ -112,11 +120,13 @@ func (o *Options) Run(_ *cobra.Command, _ []string) error {
112120
serviceaccount.WithProxyURL(o.ProxyURL),
113121
).SetupWithManager(ctx, mgr); err != nil {
114122
o.SetupLog.Error(err, "unable to create manager", "controller", "ServiceAccount")
123+
115124
return errors.Wrap(err, "unable to setup the service account controller")
116125
}
117126

118127
if err = mgr.Start(ctx); err != nil {
119128
o.SetupLog.Error(err, "problem running manager")
129+
120130
return errors.Wrap(err, "unable to start the manager")
121131
}
122132

cmd/root.go

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
1+
// Copyright 2020-2024 Project Capsule Authors.
2+
// SPDX-License-Identifier: Apache-2.0
3+
14
package cmd
25

36
import (
4-
"github.com/projectcapsule/capsule-addon-flux/cmd/manager"
57
"github.com/spf13/cobra"
8+
9+
"github.com/projectcapsule/capsule-addon-flux/cmd/manager"
610
)
711

812
func New() *cobra.Command {
@@ -17,5 +21,6 @@ func New() *cobra.Command {
1721

1822
func Execute() error {
1923
cmd := New()
24+
2025
return cmd.Execute()
2126
}

e2e/charts/serviceaccount_test.go

+3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
//go:build e2e
22

3+
// Copyright 2020-2024 Project Capsule Authors.
4+
// SPDX-License-Identifier: Apache-2.0
5+
36
package charts
47

58
import (

e2e/charts/suite_test.go

+3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
//go:build e2e
22

3+
// Copyright 2020-2024 Project Capsule Authors.
4+
// SPDX-License-Identifier: Apache-2.0
5+
36
package charts
47

58
import (

e2e/serviceaccount_test.go

+3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
//go:build e2e
22

3+
// Copyright 2020-2024 Project Capsule Authors.
4+
// SPDX-License-Identifier: Apache-2.0
5+
36
package e2e
47

58
import (

e2e/suite_test.go

+3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
//go:build e2e
22

3+
// Copyright 2020-2024 Project Capsule Authors.
4+
// SPDX-License-Identifier: Apache-2.0
5+
36
package e2e
47

58
import (

e2e/utils/utils.go

+3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
//go:build e2e
22

3+
// Copyright 2020-2024 Project Capsule Authors.
4+
// SPDX-License-Identifier: Apache-2.0
5+
36
package utils
47

58
import (

main.go

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
// Copyright 2020-2024 Project Capsule Authors.
2+
// SPDX-License-Identifier: Apache-2.0
3+
14
package main
25

36
import (
@@ -9,6 +12,7 @@ import (
912

1013
func main() {
1114
if err := cmd.Execute(); err != nil {
15+
//nolint:forbidigo
1216
fmt.Println(err)
1317
os.Exit(1)
1418
}

pkg/controller/serviceaccount/constants.go

+3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
// Copyright 2020-2024 Project Capsule Authors.
2+
// SPDX-License-Identifier: Apache-2.0
3+
14
package serviceaccount
25

36
const (

pkg/controller/serviceaccount/errors.go

+3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
// Copyright 2020-2024 Project Capsule Authors.
2+
// SPDX-License-Identifier: Apache-2.0
3+
14
package serviceaccount
25

36
import "github.com/pkg/errors"

pkg/controller/serviceaccount/globaltenantresources.go

+4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
1+
// Copyright 2020-2024 Project Capsule Authors.
2+
// SPDX-License-Identifier: Apache-2.0
3+
14
package serviceaccount
25

36
import (
47
"context"
8+
59
capsulev1beta2 "github.com/projectcapsule/capsule/api/v1beta2"
610
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
711
"k8s.io/apimachinery/pkg/runtime"

pkg/controller/serviceaccount/rolebindings.go

+4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
1+
// Copyright 2020-2024 Project Capsule Authors.
2+
// SPDX-License-Identifier: Apache-2.0
3+
14
package serviceaccount
25

36
import (
47
"context"
58
"fmt"
9+
610
rbacv1 "k8s.io/api/rbac/v1"
711
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
812
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"

pkg/controller/serviceaccount/serviceaccount.go

+13-6
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
// Copyright 2020-2024 Project Capsule Authors.
2+
// SPDX-License-Identifier: Apache-2.0
3+
14
package serviceaccount
25

36
import (
@@ -21,6 +24,7 @@ import (
2124
"sigs.k8s.io/controller-runtime/pkg/reconcile"
2225
)
2326

27+
//nolint:revive
2428
type ServiceAccountReconciler struct {
2529
proxyURL string
2630
proxyCA string
@@ -83,6 +87,7 @@ func (r *ServiceAccountReconciler) Reconcile(ctx context.Context, request ctrl.R
8387

8488
return reconcile.Result{}, nil
8589
}
90+
8691
r.Log.Error(err, "Error reading the object")
8792

8893
return ctrl.Result{}, err
@@ -102,16 +107,15 @@ func (r *ServiceAccountReconciler) Reconcile(ctx context.Context, request ctrl.R
102107
if err != nil {
103108
return reconcile.Result{}, errors.Wrap(err, "error getting token of the service account")
104109
}
110+
105111
if tokenSecret.Data == nil {
106112
r.Log.Info("ServiceAccount token data is missing. Requeueing.")
113+
107114
return reconcile.Result{Requeue: true}, nil
108115
}
109116

110117
// Build the kubeConfig for the ServiceAccount Tenant Owner.
111-
config, err := r.buildKubeconfig(r.proxyURL, string(tokenSecret.Data[corev1.ServiceAccountTokenKey]))
112-
if err != nil {
113-
return reconcile.Result{}, errors.Wrap(err, "error building the tenant owner config")
114-
}
118+
config := r.buildKubeconfig(r.proxyURL, string(tokenSecret.Data[corev1.ServiceAccountTokenKey]))
115119

116120
configRaw, err := clientcmd.Write(*config)
117121
if err != nil {
@@ -142,10 +146,12 @@ func (r *ServiceAccountReconciler) Reconcile(ctx context.Context, request ctrl.R
142146
if sa.GetAnnotations()[ServiceAccountGlobalAnnotationKey] == ServiceAccountGlobalAnnotationValue {
143147
// Get the Tenant owned by the ServiceAccount.
144148
ownerName := fmt.Sprintf("system:serviceaccount:%s:%s", sa.GetNamespace(), sa.GetName())
149+
145150
tenantList, err := r.listTenantsOwned(ctx, string(capsulev1beta2.ServiceAccountOwner), ownerName)
146151
if err != nil {
147152
return reconcile.Result{}, errors.Wrap(err, "error listing Tenants for owner")
148153
}
154+
149155
if tenantList.Items == nil {
150156
return reconcile.Result{}, errors.New("Tenant list for owner is empty")
151157
}
@@ -176,6 +182,7 @@ func (r *ServiceAccountReconciler) forOption(ctx context.Context) builder.ForOpt
176182
predicate.NewPredicateFuncs(func(object client.Object) bool {
177183
ownerName := fmt.Sprintf("system:serviceaccount:%s:%s", object.GetNamespace(), object.GetName())
178184
tntList, err := r.listTenantsOwned(ctx, string(capsulev1beta2.ServiceAccountOwner), ownerName)
185+
179186
return err == nil && tntList.Items != nil && len(tntList.Items) != 0
180187
}),
181188
),
@@ -196,7 +203,7 @@ func (r *ServiceAccountReconciler) listTenantsOwned(ctx context.Context, ownerKi
196203

197204
// buildKubeconfig returns a client-go/clientcmd/api.Config with a token and server URL specified as arguments.
198205
// The server set is be the proxy configured at ServiceAccountReconciler-level.
199-
func (r *ServiceAccountReconciler) buildKubeconfig(server, token string) (*clientcmdapi.Config, error) {
206+
func (r *ServiceAccountReconciler) buildKubeconfig(server, token string) *clientcmdapi.Config {
200207
// Build the client API Config.
201208
config := clientcmdapi.NewConfig()
202209
config.APIVersion = clientcmdlatest.Version
@@ -228,5 +235,5 @@ func (r *ServiceAccountReconciler) buildKubeconfig(server, token string) (*clien
228235
config.Contexts = contexts
229236
config.CurrentContext = KubeconfigContextName
230237

231-
return config, nil
238+
return config
232239
}

pkg/controller/serviceaccount/tokens.go

+8-4
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
1+
// Copyright 2020-2024 Project Capsule Authors.
2+
// SPDX-License-Identifier: Apache-2.0
3+
14
package serviceaccount
25

36
import (
47
"context"
58
"fmt"
6-
"github.com/pkg/errors"
79

10+
"github.com/pkg/errors"
811
corev1 "k8s.io/api/core/v1"
912
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1013
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
@@ -34,6 +37,7 @@ func (r *ServiceAccountReconciler) ensureSATokenSecret(ctx context.Context, name
3437

3538
return nil
3639
}
40+
3741
return err
3842
}
3943

@@ -44,7 +48,6 @@ func (r *ServiceAccountReconciler) ensureSATokenSecret(ctx context.Context, name
4448
// are specified as arguments.
4549
func (r *ServiceAccountReconciler) getSATokenSecret(ctx context.Context, saName, saNamespace string) (*corev1.Secret, error) {
4650
saTokenList := new(corev1.SecretList)
47-
// TODO: filter by Service Account-type and Namespace. Need index by Secret type.
4851
if err := r.Client.List(ctx, saTokenList); err != nil {
4952
return nil, ErrServiceAccountTokenNotFound
5053
}
@@ -54,15 +57,16 @@ func (r *ServiceAccountReconciler) getSATokenSecret(ctx context.Context, saName,
5457
}
5558

5659
var tokenSecret *corev1.Secret
60+
5761
for _, v := range saTokenList.Items {
5862
v := v
59-
switch v.Type {
60-
case corev1.SecretTypeServiceAccountToken:
63+
if v.Type == corev1.SecretTypeServiceAccountToken {
6164
if v.Namespace == saNamespace && v.Annotations[corev1.ServiceAccountNameKey] == saName {
6265
return &v, nil
6366
}
6467
}
6568
}
69+
6670
if tokenSecret == nil {
6771
return nil, ErrServiceAccountTokenNotFound
6872
}

pkg/indexer/indexer.go

+3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
// Copyright 2020-2024 Project Capsule Authors.
2+
// SPDX-License-Identifier: Apache-2.0
3+
14
package indexer
25

36
import (

0 commit comments

Comments
 (0)