Skip to content

Commit

Permalink
Enable the unparam linter.
Browse files Browse the repository at this point in the history
Update the code according to the unparam linter rules.
  • Loading branch information
opudrovs committed Mar 14, 2023
1 parent 5724e5f commit 3113f04
Show file tree
Hide file tree
Showing 39 changed files with 117 additions and 165 deletions.
2 changes: 1 addition & 1 deletion .golangci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ linters:
#- tparallel
- typecheck
#- unconvert
#- unparam
- unparam
- unused
- varcheck
#- whitespace
Expand Down
8 changes: 4 additions & 4 deletions cmd/gitops/beta/run/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ func betaRunCommandPreRunE(endpoint *string) func(*cobra.Command, []string) erro
}
}

func getKubeClient(cmd *cobra.Command, args []string) (*kube.KubeHTTP, *rest.Config, error) {
func getKubeClient(cmd *cobra.Command) (*kube.KubeHTTP, *rest.Config, error) {
var err error

log := logger.NewCLILogger(os.Stdout)
Expand Down Expand Up @@ -456,7 +456,7 @@ func runCommandOuterProcess(cmd *cobra.Command, args []string) (retErr error) {
return err
}

kubeClient, _, err := getKubeClient(cmd, args)
kubeClient, _, err := getKubeClient(cmd)
if err != nil {
return err
}
Expand Down Expand Up @@ -605,7 +605,7 @@ func runCommandInnerProcess(cmd *cobra.Command, args []string) error {
return err
}

kubeClient, cfg, err := getKubeClient(cmd, args)
kubeClient, cfg, err := getKubeClient(cmd)
if err != nil {
return err
}
Expand Down Expand Up @@ -884,7 +884,7 @@ func runCommandInnerProcess(cmd *cobra.Command, args []string) error {
ticker := time.NewTicker(680 * time.Millisecond)

go func() {
for { // nolint:gosimple
for { //nolint:gosimple
select {
case <-stopUploadCh:
return
Expand Down
7 changes: 4 additions & 3 deletions cmd/gitops/remove/run/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package run

import (
"fmt"
"os"

"github.com/spf13/cobra"
"github.com/weaveworks/weave-gitops/cmd/gitops/cmderrors"
"github.com/weaveworks/weave-gitops/cmd/gitops/config"
Expand All @@ -11,7 +13,6 @@ import (
"github.com/weaveworks/weave-gitops/pkg/run/session"
"k8s.io/cli-runtime/pkg/genericclioptions"
"k8s.io/client-go/rest"
"os"
)

type RunCommandFlags struct {
Expand Down Expand Up @@ -63,7 +64,7 @@ gitops remove run -n dev --all-sessions
return cmd
}

func getKubeClient(cmd *cobra.Command, args []string) (*kube.KubeHTTP, *rest.Config, error) {
func getKubeClient(cmd *cobra.Command) (*kube.KubeHTTP, *rest.Config, error) {
var err error

log := logger.NewCLILogger(os.Stdout)
Expand Down Expand Up @@ -133,7 +134,7 @@ func removeRunPreRunE(opts *config.Options) func(cmd *cobra.Command, args []stri

func removeRunRunE(opts *config.Options) func(cmd *cobra.Command, args []string) error {
return func(cmd *cobra.Command, args []string) error {
kubeClient, _, err := getKubeClient(cmd, args)
kubeClient, _, err := getKubeClient(cmd)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion core/clustersmngr/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -501,7 +501,7 @@ func createClusterRoleBinding(g *GomegaWithT, user string) *rbacv1.ClusterRoleBi
return crb
}

func createClusterClientsPool(g *GomegaWithT, clusterName string) clustersmngr.ClientsPool {
func createClusterClientsPool(g *GomegaWithT, clusterName string) clustersmngr.ClientsPool { //nolint:unparam
scheme, err := kube.CreateScheme()
g.Expect(err).To(BeNil())

Expand Down
6 changes: 3 additions & 3 deletions core/clustersmngr/cluster/delegating_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ func (dc delegatingCache) Get(ctx context.Context, key client.ObjectKey, obj cli
return err
}

bypass := dc.shouldBypassCheck(obj, gvk)
bypass := dc.shouldBypassCheck(gvk)
if !bypass {
partial := &metav1.PartialObjectMetadata{}
partial.SetGroupVersionKind(gvk)
Expand All @@ -159,7 +159,7 @@ func (dc delegatingCache) List(ctx context.Context, list client.ObjectList, opts
return err
}

bypass := dc.shouldBypassCheck(list, gvk)
bypass := dc.shouldBypassCheck(gvk)
if !bypass {
partial := &metav1.PartialObjectMetadataList{}
partial.SetGroupVersionKind(gvk)
Expand All @@ -174,7 +174,7 @@ func (dc delegatingCache) List(ctx context.Context, list client.ObjectList, opts
return dc.Cache.List(ctx, list, opts...)
}

func (dc *delegatingCache) shouldBypassCheck(obj runtime.Object, gvk schema.GroupVersionKind) bool {
func (dc *delegatingCache) shouldBypassCheck(gvk schema.GroupVersionKind) bool {
dc.checkedGVKsMu.Lock()
defer dc.checkedGVKsMu.Unlock()

Expand Down
8 changes: 4 additions & 4 deletions core/clustersmngr/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -425,7 +425,7 @@ func (cf *clustersManager) getUserClientWithNamespaces(ctx context.Context, user
go func(cluster cluster.Cluster, pool ClientsPool, errChan chan error) {
defer wg.Done()

client, err := cf.getOrCreateClient(ctx, user, cluster)
client, err := cf.getOrCreateClient(user, cluster)
if err != nil {
errChan <- &ClientError{ClusterName: cluster.GetName(), Err: fmt.Errorf("failed creating user client to pool: %w", err)}
return
Expand Down Expand Up @@ -475,7 +475,7 @@ func (cf *clustersManager) GetImpersonatedClientForCluster(ctx context.Context,
return nil, fmt.Errorf("cluster not found: %s", clusterName)
}

client, err := cf.getOrCreateClient(ctx, user, cl)
client, err := cf.getOrCreateClient(user, cl)
if err != nil {
return nil, fmt.Errorf("failed creating cluster client: %w", err)
}
Expand Down Expand Up @@ -520,7 +520,7 @@ func (cf *clustersManager) GetServerClient(ctx context.Context) (Client, error)
go func(cluster cluster.Cluster, pool ClientsPool, errChan chan error) {
defer wg.Done()

client, err := cf.getOrCreateClient(ctx, nil, cluster)
client, err := cf.getOrCreateClient(nil, cluster)
if err != nil {
errChan <- &ClientError{ClusterName: cluster.GetName(), Err: fmt.Errorf("failed creating server client to pool: %w", err)}
return
Expand Down Expand Up @@ -598,7 +598,7 @@ func (cf *clustersManager) userNsList(ctx context.Context, user *auth.UserPrinci
return cf.GetUserNamespaces(user)
}

func (cf *clustersManager) getOrCreateClient(ctx context.Context, user *auth.UserPrincipal, cluster cluster.Cluster) (client.Client, error) {
func (cf *clustersManager) getOrCreateClient(user *auth.UserPrincipal, cluster cluster.Cluster) (client.Client, error) {
isServer := false

if user == nil {
Expand Down
4 changes: 2 additions & 2 deletions core/nsaccess/nsaccess.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ func userCanUseNamespace(ctx context.Context, auth typedauth.AuthorizationV1Inte
return false, err
}

return hasAllRules(authRes.Status, rules, ns.Name), nil
return hasAllRules(authRes.Status, rules), nil
}

var allK8sVerbs = []string{"create", "get", "list", "watch", "patch", "delete", "deletecollection"}
Expand Down Expand Up @@ -158,7 +158,7 @@ func allAPIGroups(rules []rbacv1.PolicyRule) []string {
//
// Secrets don't exist in "apps" according to k8s,
// but the "index checker" that checks this struct does not mind.
func hasAllRules(status authorizationv1.SubjectRulesReviewStatus, rules []rbacv1.PolicyRule, ns string) bool {
func hasAllRules(status authorizationv1.SubjectRulesReviewStatus, rules []rbacv1.PolicyRule) bool {
derivedAccess := map[string]map[string]map[string]bool{}

allResourcesInRules := allResources(rules)
Expand Down
6 changes: 2 additions & 4 deletions core/server/crd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (

func TestIsAvailable(t *testing.T) {
g := NewGomegaWithT(t)
c, _ := makeGRPCServer(k8sEnv.Rest, t)
c := makeGRPCServer(k8sEnv.Rest, t)

scheme, err := kube.CreateScheme()
g.Expect(err).NotTo(HaveOccurred())
Expand Down Expand Up @@ -62,7 +62,7 @@ func newCRD(
g *GomegaWithT,
k client.Client,
info CRDInfo,
) extv1.CustomResourceDefinition {
) {
resource := extv1.CustomResourceDefinition{
ObjectMeta: metav1.ObjectMeta{
Name: fmt.Sprintf("%s.%s", info.Plural, info.Group),
Expand Down Expand Up @@ -96,6 +96,4 @@ func newCRD(
if !info.NoTest {
g.Expect(err).NotTo(HaveOccurred())
}

return resource
}
2 changes: 1 addition & 1 deletion core/server/events_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func TestListEvents(t *testing.T) {

ctx := context.Background()

c, _ := makeGRPCServer(k8sEnv.Rest, t)
c := makeGRPCServer(k8sEnv.Rest, t)

scheme, err := kube.CreateScheme()
g.Expect(err).To(BeNil())
Expand Down
2 changes: 1 addition & 1 deletion core/server/fluxruntime.go
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ func (cs *coreServer) GetReconciledObjects(ctx context.Context, msg *pb.GetRecon
Version: gvk.Version,
})

if err := clustersClient.List(ctx, msg.ClusterName, &listResult, opts, client.InNamespace(namespace)); err != nil {
if err := clustersClient.List(ctx, clusterName, &listResult, opts, client.InNamespace(namespace)); err != nil {
if k8serrors.IsForbidden(err) {
cs.logger.V(logger.LogLevelDebug).Info(
"forbidden list request",
Expand Down
4 changes: 2 additions & 2 deletions core/server/fluxruntime_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func TestGetReconciledObjects(t *testing.T) {

ctx := context.Background()

c, _ := makeGRPCServer(k8sEnv.Rest, t)
c := makeGRPCServer(k8sEnv.Rest, t)

scheme, err := kube.CreateScheme()
g.Expect(err).To(BeNil())
Expand Down Expand Up @@ -195,7 +195,7 @@ func TestGetReconciledObjectsWithSecret(t *testing.T) {

ctx := context.Background()

c, _ := makeGRPCServer(k8sEnv.Rest, t)
c := makeGRPCServer(k8sEnv.Rest, t)

scheme, err := kube.CreateScheme()
g.Expect(err).To(BeNil())
Expand Down
20 changes: 9 additions & 11 deletions core/server/objects.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,12 +125,12 @@ func (cs *coreServer) ListObjects(ctx context.Context, msg *pb.ListObjectsReques
cs.logger.V(logger.LogLevelDebug).Info("Couldn't grab inventory for helm release", "error", err)
}
case "StatefulSet":
name, clusterName, kind, err := parseSessionInfo(unstructuredObj)
clusterName, kind, err := parseSessionInfo(unstructuredObj)
if err != nil {
break
}

created, _ := cs.sessionObjectsCreated(ctx, name, clusterName, "flux-system", kind)
created, _ := cs.sessionObjectsCreated(ctx, clusterName, "flux-system", kind)

if created {
info = sessionObjectsInfo
Expand All @@ -154,18 +154,18 @@ func (cs *coreServer) ListObjects(ctx context.Context, msg *pb.ListObjectsReques
}, nil
}

func parseSessionInfo(unstructuredObj unstructured.Unstructured) (string, string, string, error) {
func parseSessionInfo(unstructuredObj unstructured.Unstructured) (string, string, error) {
var set v1.StatefulSet

err := runtime.DefaultUnstructuredConverter.FromUnstructured(unstructuredObj.UnstructuredContent(), &set)
if err != nil {
return "", "", "", fmt.Errorf("converting unstructured to statefulset: %w", err)
return "", "", fmt.Errorf("converting unstructured to statefulset: %w", err)
}

labels := set.GetLabels()

if labels["app"] != "vcluster" || labels["app.kubernetes.io/part-of"] != "gitops-run" {
return "", "", "", fmt.Errorf("unexpected format of labels")
return "", "", fmt.Errorf("unexpected format of labels")
}

annotations := set.GetAnnotations()
Expand All @@ -179,17 +179,15 @@ func parseSessionInfo(unstructuredObj unstructured.Unstructured) (string, string

ns := annotations["run.weave.works/namespace"]
if ns == "" {
return "", "", "", fmt.Errorf("empty session namespace")
return "", "", fmt.Errorf("empty session namespace")
}

name := set.GetName()
clusterName := ns + "/" + set.GetName()

clusterName := ns + "/" + name

return name, clusterName, kind, nil
return clusterName, kind, nil
}

func (cs *coreServer) sessionObjectsCreated(ctx context.Context, sessionName string, clusterName string, objectNamespace string, automationKind string) (bool, error) {
func (cs *coreServer) sessionObjectsCreated(ctx context.Context, clusterName string, objectNamespace string, automationKind string) (bool, error) {
automationName := constants.RunDevHelmName

if automationKind == kustomizev1.KustomizationKind {
Expand Down
2 changes: 1 addition & 1 deletion core/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func NewCoreConfig(log logr.Logger, cfg *rest.Config, clusterName string, cluste

func NewCoreServer(cfg CoreServerConfig) (pb.CoreServer, error) {
if cfg.CRDService == nil {
cfg.CRDService = crd.NewFetcher(context.Background(), cfg.log, cfg.ClustersManager)
cfg.CRDService = crd.NewFetcher(cfg.log, cfg.ClustersManager)
}

return &coreServer{
Expand Down
2 changes: 1 addition & 1 deletion core/server/session_logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ func (cs *coreServer) GetSessionLogs(ctx context.Context, msg *pb.GetSessionLogs
}, nil
}

func isSecretCreated(ctx context.Context, cli client.Client, namespace string, name string) error {
func isSecretCreated(ctx context.Context, cli client.Client, namespace string, name string) error { //nolint:unparam
secret := corev1.Secret{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Expand Down
4 changes: 2 additions & 2 deletions core/server/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func TestMain(m *testing.M) {
os.Exit(code)
}

func makeGRPCServer(cfg *rest.Config, t *testing.T) (pb.CoreClient, server.CoreServerConfig) {
func makeGRPCServer(cfg *rest.Config, t *testing.T) pb.CoreClient {
log := logr.Discard()
nsChecker = nsaccessfakes.FakeChecker{}
nsChecker.FilterAccessibleNamespacesStub = func(ctx context.Context, t typedauth.AuthorizationV1Interface, n []v1.Namespace) ([]v1.Namespace, error) {
Expand Down Expand Up @@ -118,7 +118,7 @@ func makeGRPCServer(cfg *rest.Config, t *testing.T) (pb.CoreClient, server.CoreS
conn.Close()
})

return pb.NewCoreClient(conn), coreCfg
return pb.NewCoreClient(conn)
}

type userKey struct{}
Expand Down
2 changes: 1 addition & 1 deletion core/server/suspend_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func TestSuspend_Suspend(t *testing.T) {
})
g.Expect(err).NotTo(HaveOccurred())

c, _ := makeGRPCServer(k8sEnv.Rest, t)
c := makeGRPCServer(k8sEnv.Rest, t)

ns := newNamespace(ctx, k, g)

Expand Down
2 changes: 1 addition & 1 deletion core/server/sync_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func TestSync(t *testing.T) {

ctx := context.Background()

c, _ := makeGRPCServer(k8sEnv.Rest, t)
c := makeGRPCServer(k8sEnv.Rest, t)

scheme, err := kube.CreateScheme()
g.Expect(err).To(BeNil())
Expand Down
2 changes: 1 addition & 1 deletion core/server/version_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const (

func TestGetVersion(t *testing.T) {
g := NewGomegaWithT(t)
c, _ := makeGRPCServer(k8sEnv.Rest, t)
c := makeGRPCServer(k8sEnv.Rest, t)

scheme, err := kube.CreateScheme()
g.Expect(err).To(BeNil())
Expand Down
9 changes: 3 additions & 6 deletions pkg/fluxexec/bootstrap_bitbucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,7 @@ func (opt *UsernameOption) configureBootstrapBitbucketServer(conf *bootstrapBitb
}

func (flux *Flux) BootstrapBitbucketServer(ctx context.Context, opts ...BootstrapBitbucketServerOption) error {
bootstrapBitbucketServerCmd, err := flux.bootstrapBitbucketServerCmd(ctx, opts...)
if err != nil {
return err
}
bootstrapBitbucketServerCmd := flux.bootstrapBitbucketServerCmd(ctx, opts...)

if err := flux.runFluxCmd(ctx, bootstrapBitbucketServerCmd); err != nil {
return err
Expand All @@ -92,7 +89,7 @@ func (flux *Flux) BootstrapBitbucketServer(ctx context.Context, opts ...Bootstra
return nil
}

func (flux *Flux) bootstrapBitbucketServerCmd(ctx context.Context, opts ...BootstrapBitbucketServerOption) (*exec.Cmd, error) {
func (flux *Flux) bootstrapBitbucketServerCmd(ctx context.Context, opts ...BootstrapBitbucketServerOption) *exec.Cmd {
c := defaultBootstrapBitbucketServerOptions
for _, opt := range opts {
opt.configureBootstrapBitbucketServer(&c)
Expand Down Expand Up @@ -152,5 +149,5 @@ func (flux *Flux) bootstrapBitbucketServerCmd(ctx context.Context, opts ...Boots
args = append(args, "--username", c.username)
}

return flux.buildFluxCmd(ctx, flux.env, args...), nil
return flux.buildFluxCmd(ctx, flux.env, args...)
}
Loading

0 comments on commit 3113f04

Please sign in to comment.