Skip to content

Commit 13f74cb

Browse files
committed
chore: lint and fix gci linter go mod name
Signed-off-by: Massimiliano Giovagnoli <[email protected]>
1 parent d1c255d commit 13f74cb

File tree

8 files changed

+36
-13
lines changed

8 files changed

+36
-13
lines changed

.golangci.yml

+8-1
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,21 @@ linters-settings:
77
list-mode: lax
88
allow:
99
- $gostd
10+
- k8s.io/api
11+
- k8s.io/apimachinery
12+
- k8s.io/client-go
1013
- github.com/projectcapsule
1114
- github.com/go-logr/logr
15+
- github.com/pkg/errors
16+
- github.com/spf13/cobra
1217
- sigs.k8s.io/controller-runtime
18+
funlen:
19+
lines: 110
1320
gci:
1421
sections:
1522
- standard # Captures all standard packages if they do not match another section.
1623
- default # Contains all imports that could not be matched to another section type.
17-
- 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.
1825
goconst:
1926
min-len: 2
2027
min-occurrences: 3

cmd/manager/manager.go

+8-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ package manager
66
import (
77
"flag"
88
"fmt"
9+
"os"
10+
911
"github.com/go-logr/logr"
1012
"github.com/pkg/errors"
1113
capsulev1beta2 "github.com/projectcapsule/capsule/api/v1beta2"
@@ -14,7 +16,6 @@ import (
1416
"k8s.io/apimachinery/pkg/runtime"
1517
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
1618
"k8s.io/client-go/rest"
17-
"os"
1819
ctrl "sigs.k8s.io/controller-runtime"
1920
"sigs.k8s.io/controller-runtime/pkg/client"
2021
"sigs.k8s.io/controller-runtime/pkg/healthz"
@@ -59,6 +60,7 @@ func New() *cobra.Command {
5960

6061
// Add Zap options.
6162
var fs flag.FlagSet
63+
6264
opts.Zo.BindFlags(&fs)
6365
cmd.Flags().AddGoFlagSet(&fs)
6466

@@ -70,6 +72,7 @@ func (o *Options) Run(_ *cobra.Command, _ []string) error {
7072
if err := clientgoscheme.AddToScheme(scheme); err != nil {
7173
return errors.Wrap(err, "unable to add client-go types to the manager's scheme")
7274
}
75+
7376
if err := capsulev1beta2.AddToScheme(scheme); err != nil {
7477
return errors.Wrap(err, "unable to add Capsule types to the manager's scheme")
7578
}
@@ -90,6 +93,7 @@ func (o *Options) Run(_ *cobra.Command, _ []string) error {
9093
})
9194
if err != nil {
9295
o.SetupLog.Error(err, "unable to create manager")
96+
9397
return errors.Wrap(err, "unable to create manager")
9498
}
9599

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

106110
if err = indexer.AddToManager(ctx, o.SetupLog, mgr); err != nil {
107111
o.SetupLog.Error(err, "unable to setup indexers")
112+
108113
return errors.Wrap(err, "unable to setup indexers")
109114
}
110115

@@ -115,11 +120,13 @@ func (o *Options) Run(_ *cobra.Command, _ []string) error {
115120
serviceaccount.WithProxyURL(o.ProxyURL),
116121
).SetupWithManager(ctx, mgr); err != nil {
117122
o.SetupLog.Error(err, "unable to create manager", "controller", "ServiceAccount")
123+
118124
return errors.Wrap(err, "unable to setup the service account controller")
119125
}
120126

121127
if err = mgr.Start(ctx); err != nil {
122128
o.SetupLog.Error(err, "problem running manager")
129+
123130
return errors.Wrap(err, "unable to start the manager")
124131
}
125132

cmd/root.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@
44
package cmd
55

66
import (
7-
"github.com/projectcapsule/capsule-addon-flux/cmd/manager"
87
"github.com/spf13/cobra"
8+
9+
"github.com/projectcapsule/capsule-addon-flux/cmd/manager"
910
)
1011

1112
func New() *cobra.Command {
@@ -20,5 +21,6 @@ func New() *cobra.Command {
2021

2122
func Execute() error {
2223
cmd := New()
24+
2325
return cmd.Execute()
2426
}

main.go

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212

1313
func main() {
1414
if err := cmd.Execute(); err != nil {
15+
//nolint:forbidigo
1516
fmt.Println(err)
1617
os.Exit(1)
1718
}

pkg/controller/serviceaccount/globaltenantresources.go

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ package serviceaccount
55

66
import (
77
"context"
8+
89
capsulev1beta2 "github.com/projectcapsule/capsule/api/v1beta2"
910
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1011
"k8s.io/apimachinery/pkg/runtime"

pkg/controller/serviceaccount/rolebindings.go

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package serviceaccount
66
import (
77
"context"
88
"fmt"
9+
910
rbacv1 "k8s.io/api/rbac/v1"
1011
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1112
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"

pkg/controller/serviceaccount/serviceaccount.go

+10-6
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
"sigs.k8s.io/controller-runtime/pkg/reconcile"
2525
)
2626

27+
//nolint:revive
2728
type ServiceAccountReconciler struct {
2829
proxyURL string
2930
proxyCA string
@@ -86,6 +87,7 @@ func (r *ServiceAccountReconciler) Reconcile(ctx context.Context, request ctrl.R
8687

8788
return reconcile.Result{}, nil
8889
}
90+
8991
r.Log.Error(err, "Error reading the object")
9092

9193
return ctrl.Result{}, err
@@ -105,16 +107,15 @@ func (r *ServiceAccountReconciler) Reconcile(ctx context.Context, request ctrl.R
105107
if err != nil {
106108
return reconcile.Result{}, errors.Wrap(err, "error getting token of the service account")
107109
}
110+
108111
if tokenSecret.Data == nil {
109112
r.Log.Info("ServiceAccount token data is missing. Requeueing.")
113+
110114
return reconcile.Result{Requeue: true}, nil
111115
}
112116

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

119120
configRaw, err := clientcmd.Write(*config)
120121
if err != nil {
@@ -145,10 +146,12 @@ func (r *ServiceAccountReconciler) Reconcile(ctx context.Context, request ctrl.R
145146
if sa.GetAnnotations()[ServiceAccountGlobalAnnotationKey] == ServiceAccountGlobalAnnotationValue {
146147
// Get the Tenant owned by the ServiceAccount.
147148
ownerName := fmt.Sprintf("system:serviceaccount:%s:%s", sa.GetNamespace(), sa.GetName())
149+
148150
tenantList, err := r.listTenantsOwned(ctx, string(capsulev1beta2.ServiceAccountOwner), ownerName)
149151
if err != nil {
150152
return reconcile.Result{}, errors.Wrap(err, "error listing Tenants for owner")
151153
}
154+
152155
if tenantList.Items == nil {
153156
return reconcile.Result{}, errors.New("Tenant list for owner is empty")
154157
}
@@ -179,6 +182,7 @@ func (r *ServiceAccountReconciler) forOption(ctx context.Context) builder.ForOpt
179182
predicate.NewPredicateFuncs(func(object client.Object) bool {
180183
ownerName := fmt.Sprintf("system:serviceaccount:%s:%s", object.GetNamespace(), object.GetName())
181184
tntList, err := r.listTenantsOwned(ctx, string(capsulev1beta2.ServiceAccountOwner), ownerName)
185+
182186
return err == nil && tntList.Items != nil && len(tntList.Items) != 0
183187
}),
184188
),
@@ -199,7 +203,7 @@ func (r *ServiceAccountReconciler) listTenantsOwned(ctx context.Context, ownerKi
199203

200204
// buildKubeconfig returns a client-go/clientcmd/api.Config with a token and server URL specified as arguments.
201205
// The server set is be the proxy configured at ServiceAccountReconciler-level.
202-
func (r *ServiceAccountReconciler) buildKubeconfig(server, token string) (*clientcmdapi.Config, error) {
206+
func (r *ServiceAccountReconciler) buildKubeconfig(server, token string) *clientcmdapi.Config {
203207
// Build the client API Config.
204208
config := clientcmdapi.NewConfig()
205209
config.APIVersion = clientcmdlatest.Version
@@ -231,5 +235,5 @@ func (r *ServiceAccountReconciler) buildKubeconfig(server, token string) (*clien
231235
config.Contexts = contexts
232236
config.CurrentContext = KubeconfigContextName
233237

234-
return config, nil
238+
return config
235239
}

pkg/controller/serviceaccount/tokens.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"fmt"
99

1010
"github.com/pkg/errors"
11-
1211
corev1 "k8s.io/api/core/v1"
1312
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1413
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
@@ -38,6 +37,7 @@ func (r *ServiceAccountReconciler) ensureSATokenSecret(ctx context.Context, name
3837

3938
return nil
4039
}
40+
4141
return err
4242
}
4343

@@ -48,7 +48,6 @@ func (r *ServiceAccountReconciler) ensureSATokenSecret(ctx context.Context, name
4848
// are specified as arguments.
4949
func (r *ServiceAccountReconciler) getSATokenSecret(ctx context.Context, saName, saNamespace string) (*corev1.Secret, error) {
5050
saTokenList := new(corev1.SecretList)
51-
// TODO: filter by Service Account-type and Namespace. Need index by Secret type.
5251
if err := r.Client.List(ctx, saTokenList); err != nil {
5352
return nil, ErrServiceAccountTokenNotFound
5453
}
@@ -58,15 +57,16 @@ func (r *ServiceAccountReconciler) getSATokenSecret(ctx context.Context, saName,
5857
}
5958

6059
var tokenSecret *corev1.Secret
60+
6161
for _, v := range saTokenList.Items {
6262
v := v
63-
switch v.Type {
64-
case corev1.SecretTypeServiceAccountToken:
63+
if v.Type == corev1.SecretTypeServiceAccountToken {
6564
if v.Namespace == saNamespace && v.Annotations[corev1.ServiceAccountNameKey] == saName {
6665
return &v, nil
6766
}
6867
}
6968
}
69+
7070
if tokenSecret == nil {
7171
return nil, ErrServiceAccountTokenNotFound
7272
}

0 commit comments

Comments
 (0)