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

chore: lint code and improve linter settings #7

Merged
merged 1 commit into from
Feb 4, 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
20 changes: 18 additions & 2 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -1,17 +1,33 @@
linters-settings:
cyclop:
max-complexity: 27
depguard:
rules:
main:
list-mode: lax
allow:
- $gostd
- k8s.io/api
- k8s.io/apimachinery
- k8s.io/client-go
- github.com/projectcapsule
- github.com/go-logr/logr
- github.com/pkg/errors
- github.com/spf13/cobra
- sigs.k8s.io/controller-runtime
funlen:
lines: 110
gci:
sections:
- standard # Captures all standard packages if they do not match another section.
- default # Contains all imports that could not be matched to another section type.
- prefix(github.com/projectcapsule/capsule-addon-fluxcd) # Groups all imports with the specified Prefix.
- prefix(github.com/projectcapsule/capsule-addon-flux) # Groups all imports with the specified Prefix.
goconst:
min-len: 2
min-occurrences: 3
goheader:
template: |-
Copyright 2020-2023 Project Capsule Authors.
Copyright 2020-2024 Project Capsule Authors.
SPDX-License-Identifier: Apache-2.0
govet:
check-shadowing: true
Expand Down
3 changes: 3 additions & 0 deletions cmd/constants.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// Copyright 2020-2024 Project Capsule Authors.
// SPDX-License-Identifier: Apache-2.0

package cmd

const (
Expand Down
3 changes: 3 additions & 0 deletions cmd/manager/constants.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// Copyright 2020-2024 Project Capsule Authors.
// SPDX-License-Identifier: Apache-2.0

package manager

const (
Expand Down
12 changes: 11 additions & 1 deletion cmd/manager/manager.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
// Copyright 2020-2024 Project Capsule Authors.
// SPDX-License-Identifier: Apache-2.0

package manager

import (
"flag"
"fmt"
"os"

"github.com/go-logr/logr"
"github.com/pkg/errors"
capsulev1beta2 "github.com/projectcapsule/capsule/api/v1beta2"
Expand All @@ -11,7 +16,6 @@ import (
"k8s.io/apimachinery/pkg/runtime"
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
"k8s.io/client-go/rest"
"os"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/healthz"
Expand Down Expand Up @@ -56,6 +60,7 @@ func New() *cobra.Command {

// Add Zap options.
var fs flag.FlagSet

opts.Zo.BindFlags(&fs)
cmd.Flags().AddGoFlagSet(&fs)

Expand All @@ -67,6 +72,7 @@ func (o *Options) Run(_ *cobra.Command, _ []string) error {
if err := clientgoscheme.AddToScheme(scheme); err != nil {
return errors.Wrap(err, "unable to add client-go types to the manager's scheme")
}

if err := capsulev1beta2.AddToScheme(scheme); err != nil {
return errors.Wrap(err, "unable to add Capsule types to the manager's scheme")
}
Expand All @@ -87,6 +93,7 @@ func (o *Options) Run(_ *cobra.Command, _ []string) error {
})
if err != nil {
o.SetupLog.Error(err, "unable to create manager")

return errors.Wrap(err, "unable to create manager")
}

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

if err = indexer.AddToManager(ctx, o.SetupLog, mgr); err != nil {
o.SetupLog.Error(err, "unable to setup indexers")

return errors.Wrap(err, "unable to setup indexers")
}

Expand All @@ -112,11 +120,13 @@ func (o *Options) Run(_ *cobra.Command, _ []string) error {
serviceaccount.WithProxyURL(o.ProxyURL),
).SetupWithManager(ctx, mgr); err != nil {
o.SetupLog.Error(err, "unable to create manager", "controller", "ServiceAccount")

return errors.Wrap(err, "unable to setup the service account controller")
}

if err = mgr.Start(ctx); err != nil {
o.SetupLog.Error(err, "problem running manager")

return errors.Wrap(err, "unable to start the manager")
}

Expand Down
7 changes: 6 additions & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
// Copyright 2020-2024 Project Capsule Authors.
// SPDX-License-Identifier: Apache-2.0

package cmd

import (
"github.com/projectcapsule/capsule-addon-flux/cmd/manager"
"github.com/spf13/cobra"

"github.com/projectcapsule/capsule-addon-flux/cmd/manager"
)

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

func Execute() error {
cmd := New()

return cmd.Execute()
}
3 changes: 3 additions & 0 deletions e2e/charts/serviceaccount_test.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
//go:build e2e

// Copyright 2020-2024 Project Capsule Authors.
// SPDX-License-Identifier: Apache-2.0

package charts

import (
Expand Down
3 changes: 3 additions & 0 deletions e2e/charts/suite_test.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
//go:build e2e

// Copyright 2020-2024 Project Capsule Authors.
// SPDX-License-Identifier: Apache-2.0

package charts

import (
Expand Down
3 changes: 3 additions & 0 deletions e2e/serviceaccount_test.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
//go:build e2e

// Copyright 2020-2024 Project Capsule Authors.
// SPDX-License-Identifier: Apache-2.0

package e2e

import (
Expand Down
3 changes: 3 additions & 0 deletions e2e/suite_test.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
//go:build e2e

// Copyright 2020-2024 Project Capsule Authors.
// SPDX-License-Identifier: Apache-2.0

package e2e

import (
Expand Down
3 changes: 3 additions & 0 deletions e2e/utils/utils.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
//go:build e2e

// Copyright 2020-2024 Project Capsule Authors.
// SPDX-License-Identifier: Apache-2.0

package utils

import (
Expand Down
4 changes: 4 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// Copyright 2020-2024 Project Capsule Authors.
// SPDX-License-Identifier: Apache-2.0

package main

import (
Expand All @@ -9,6 +12,7 @@ import (

func main() {
if err := cmd.Execute(); err != nil {
//nolint:forbidigo
fmt.Println(err)
os.Exit(1)
}
Expand Down
3 changes: 3 additions & 0 deletions pkg/controller/serviceaccount/constants.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// Copyright 2020-2024 Project Capsule Authors.
// SPDX-License-Identifier: Apache-2.0

package serviceaccount

const (
Expand Down
3 changes: 3 additions & 0 deletions pkg/controller/serviceaccount/errors.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// Copyright 2020-2024 Project Capsule Authors.
// SPDX-License-Identifier: Apache-2.0

package serviceaccount

import "github.com/pkg/errors"
Expand Down
4 changes: 4 additions & 0 deletions pkg/controller/serviceaccount/globaltenantresources.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
// Copyright 2020-2024 Project Capsule Authors.
// SPDX-License-Identifier: Apache-2.0

package serviceaccount

import (
"context"

capsulev1beta2 "github.com/projectcapsule/capsule/api/v1beta2"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
Expand Down
4 changes: 4 additions & 0 deletions pkg/controller/serviceaccount/rolebindings.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
// Copyright 2020-2024 Project Capsule Authors.
// SPDX-License-Identifier: Apache-2.0

package serviceaccount

import (
"context"
"fmt"

rbacv1 "k8s.io/api/rbac/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
Expand Down
19 changes: 13 additions & 6 deletions pkg/controller/serviceaccount/serviceaccount.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// Copyright 2020-2024 Project Capsule Authors.
// SPDX-License-Identifier: Apache-2.0

package serviceaccount

import (
Expand All @@ -21,6 +24,7 @@ import (
"sigs.k8s.io/controller-runtime/pkg/reconcile"
)

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

return reconcile.Result{}, nil
}

r.Log.Error(err, "Error reading the object")

return ctrl.Result{}, err
Expand All @@ -102,16 +107,15 @@ func (r *ServiceAccountReconciler) Reconcile(ctx context.Context, request ctrl.R
if err != nil {
return reconcile.Result{}, errors.Wrap(err, "error getting token of the service account")
}

if tokenSecret.Data == nil {
r.Log.Info("ServiceAccount token data is missing. Requeueing.")

return reconcile.Result{Requeue: true}, nil
}

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

configRaw, err := clientcmd.Write(*config)
if err != nil {
Expand Down Expand Up @@ -142,10 +146,12 @@ func (r *ServiceAccountReconciler) Reconcile(ctx context.Context, request ctrl.R
if sa.GetAnnotations()[ServiceAccountGlobalAnnotationKey] == ServiceAccountGlobalAnnotationValue {
// Get the Tenant owned by the ServiceAccount.
ownerName := fmt.Sprintf("system:serviceaccount:%s:%s", sa.GetNamespace(), sa.GetName())

tenantList, err := r.listTenantsOwned(ctx, string(capsulev1beta2.ServiceAccountOwner), ownerName)
if err != nil {
return reconcile.Result{}, errors.Wrap(err, "error listing Tenants for owner")
}

if tenantList.Items == nil {
return reconcile.Result{}, errors.New("Tenant list for owner is empty")
}
Expand Down Expand Up @@ -176,6 +182,7 @@ func (r *ServiceAccountReconciler) forOption(ctx context.Context) builder.ForOpt
predicate.NewPredicateFuncs(func(object client.Object) bool {
ownerName := fmt.Sprintf("system:serviceaccount:%s:%s", object.GetNamespace(), object.GetName())
tntList, err := r.listTenantsOwned(ctx, string(capsulev1beta2.ServiceAccountOwner), ownerName)

return err == nil && tntList.Items != nil && len(tntList.Items) != 0
}),
),
Expand All @@ -196,7 +203,7 @@ func (r *ServiceAccountReconciler) listTenantsOwned(ctx context.Context, ownerKi

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

return config, nil
return config
}
12 changes: 8 additions & 4 deletions pkg/controller/serviceaccount/tokens.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
// Copyright 2020-2024 Project Capsule Authors.
// SPDX-License-Identifier: Apache-2.0

package serviceaccount

import (
"context"
"fmt"
"github.com/pkg/errors"

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

return nil
}

return err
}

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

var tokenSecret *corev1.Secret

for _, v := range saTokenList.Items {
v := v
switch v.Type {
case corev1.SecretTypeServiceAccountToken:
if v.Type == corev1.SecretTypeServiceAccountToken {
if v.Namespace == saNamespace && v.Annotations[corev1.ServiceAccountNameKey] == saName {
return &v, nil
}
}
}

if tokenSecret == nil {
return nil, ErrServiceAccountTokenNotFound
}
Expand Down
3 changes: 3 additions & 0 deletions pkg/indexer/indexer.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// Copyright 2020-2024 Project Capsule Authors.
// SPDX-License-Identifier: Apache-2.0

package indexer

import (
Expand Down
Loading