Skip to content
This repository has been archived by the owner on Oct 10, 2023. It is now read-only.

Commit

Permalink
Fix various lint error
Browse files Browse the repository at this point in the history
Remove whitespaces
Removed duplicate imports
Replaced duplicate strings with const (goconst)
Removed deadcode
Added missing headers (goheader)
Removed shadow declarations (govet)
Added package comment (stylecheck)
Removed unused code (unused)
Removed unused value from for loop (revive)
Simplified function signature (gocritic)
Removed unecessary nil check for map (gosimple)
Removed possible security issue
Importing "_ net/http/pprof" could lead to security problems.
  error is: G108: Profiling endpoint is automatically exposed on /debug/pprof (gosec)
  see https://docs.embold.io/gosec-high-level-issues/ for more details
Added //nolint to correct cyclomatic complexity  and funclen
 The functions being tagged with //nolint have already been in production
 for some time. Correcting the lint error requires heavy refactoring
 which could introduce problems or new bugs. At this time is best to
 flagg the functions with //nolint because the risk/reward ratio of refactoring
 is too high. There is no reward at this time since this functions have already
 gone extensive testing elsewhere.
  • Loading branch information
Adolfo Duarte committed Oct 3, 2022
1 parent aa66687 commit 584a33a
Show file tree
Hide file tree
Showing 23 changed files with 67 additions and 88 deletions.
3 changes: 1 addition & 2 deletions addons/controllers/antrea/antreaconfig_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (

"github.com/pkg/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
clusterapiv1beta1 "sigs.k8s.io/cluster-api/api/v1beta1"
clusterv1beta1 "sigs.k8s.io/cluster-api/api/v1beta1"
clusterapiutil "sigs.k8s.io/cluster-api/util"
ctrl "sigs.k8s.io/controller-runtime"
Expand Down Expand Up @@ -140,7 +139,7 @@ func (r *AntreaConfigReconciler) ClusterToAntreaConfig(o client.Object) []ctrl.R
return requests
}

func mapAntreaConfigSpec(cluster *clusterapiv1beta1.Cluster, config *cniv1alpha1.AntreaConfig) (*antreaConfigSpec, error) {
func mapAntreaConfigSpec(cluster *clusterv1beta1.Cluster, config *cniv1alpha1.AntreaConfig) (*antreaConfigSpec, error) {
configSpec := &antreaConfigSpec{}

// Derive InfraProvider from the cluster
Expand Down
5 changes: 2 additions & 3 deletions addons/controllers/calico/calicoconfig_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (

"github.com/pkg/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
clusterapiv1beta1 "sigs.k8s.io/cluster-api/api/v1beta1"
clusterv1beta1 "sigs.k8s.io/cluster-api/api/v1beta1"
clusterapiutil "sigs.k8s.io/cluster-api/util"
ctrl "sigs.k8s.io/controller-runtime"
Expand Down Expand Up @@ -90,7 +89,7 @@ func (r *CalicoConfigReconciler) ClusterToCalicoConfig(o client.Object) []ctrl.R
return requests
}

func mapCalicoConfigSpec(cluster *clusterapiv1beta1.Cluster, config *cniv1alpha1.CalicoConfig) (*calicoConfigSpec, error) {
func mapCalicoConfigSpec(cluster *clusterv1beta1.Cluster, config *cniv1alpha1.CalicoConfig) (*calicoConfigSpec, error) {
var err error

configSpec := &calicoConfigSpec{}
Expand All @@ -112,7 +111,7 @@ func mapCalicoConfigSpec(cluster *clusterapiv1beta1.Cluster, config *cniv1alpha1
return configSpec, nil
}

func getCalicoNetworkSettings(cluster *clusterapiv1beta1.Cluster) (string, string, error) {
func getCalicoNetworkSettings(cluster *clusterv1beta1.Cluster) (string, string, error) {
clusterNetwork := cluster.Spec.ClusterNetwork
if clusterNetwork == nil {
return "", "", fmt.Errorf("cluster.Spec.ClusterNetwork is not set for cluster '%s'", cluster.Name)
Expand Down
8 changes: 3 additions & 5 deletions addons/controllers/clusterbootstrap_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -332,10 +332,9 @@ func (r *ClusterBootstrapReconciler) addFinalizersToClusterResources(cluster *cl
return nil
}

func (r *ClusterBootstrapReconciler) addFinalizer(o client.Object, deepCopy client.Object) error {
func (r *ClusterBootstrapReconciler) addFinalizer(o, deepCopy client.Object) error {
controllerutil.AddFinalizer(deepCopy, addontypes.AddonFinalizer)
return r.Client.Patch(r.context, deepCopy, client.MergeFrom(o))

}

// handleClusterUnpause unpauses the cluster if the cluster pause annotation is set by cluster pause webhook (cluster has "tkg.tanzu.vmware.com/paused" annotation)
Expand Down Expand Up @@ -1143,7 +1142,7 @@ func (r *ClusterBootstrapReconciler) watchProvider(providerRef *corev1.TypedLoca
// Returns:
// - string: The secret name which references to the Secret CR on mgmt cluster under a particular cluster namespace.
// - error: whether there is error when getting the secret name.
func (r *ClusterBootstrapReconciler) GetDataValueSecretNameFromBootstrapPackage(cbPkg *runtanzuv1alpha3.ClusterBootstrapPackage, cluster *clusterapiv1beta1.Cluster) (string, error) {
func (r *ClusterBootstrapReconciler) GetDataValueSecretNameFromBootstrapPackage(cbPkg *runtanzuv1alpha3.ClusterBootstrapPackage, cluster *clusterapiv1beta1.Cluster) (string, error) { //nolint
var (
packageRefName string
err error
Expand Down Expand Up @@ -1446,13 +1445,12 @@ func (r *ClusterBootstrapReconciler) reconcileDelete(cluster *clusterapiv1beta1.
return ctrl.Result{}, nil
}

func (r *ClusterBootstrapReconciler) removeFinalizer(o client.Object, deepCopy client.Object) error {
func (r *ClusterBootstrapReconciler) removeFinalizer(o, deepCopy client.Object) error {
if controllerutil.ContainsFinalizer(deepCopy, addontypes.AddonFinalizer) {
controllerutil.RemoveFinalizer(deepCopy, addontypes.AddonFinalizer)
return r.Client.Patch(r.context, deepCopy, client.MergeFrom(o))
}
return nil

}

func hasPackageInstalls(ctx context.Context, remoteClient client.Client,
Expand Down
10 changes: 6 additions & 4 deletions addons/controllers/clusterbootstrap_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ var _ = Describe("ClusterBootstrap Reconciler", func() {
foobar1CarvelPackageName = "foobar1.example.com.1.17.2"
foobar2CarvelPackageRefName = "foobar2.example.com"
foobar = "foobar"
foobarUpdated = "foobar-updated"
foobar1Updated = "foobar1-updated"
)

JustBeforeEach(func() {
Expand Down Expand Up @@ -318,14 +320,14 @@ var _ = Describe("ClusterBootstrap Reconciler", func() {
s := &corev1.Secret{}
Expect(k8sClient.Get(ctx, client.ObjectKey{Namespace: clusterNamespace, Name: fmt.Sprintf("%s-foobar1-package", cluster.Name)}, s)).To(Succeed())
s.StringData = make(map[string]string)
s.StringData["values.yaml"] = "foobar1-updated"
s.StringData["values.yaml"] = foobar1Updated
Expect(k8sClient.Update(ctx, s)).To(Succeed())
Eventually(func() bool {
s := &corev1.Secret{}
if err := k8sClient.Get(ctx, client.ObjectKey{Namespace: constants.TKGSystemNS, Name: util.GenerateDataValueSecretName(clusterName, foobar1CarvelPackageRefName)}, s); err != nil {
return false
}
if string(s.Data["values.yaml"]) != "foobar1-updated" {
if string(s.Data["values.yaml"]) != foobar1Updated {
return false
}
// TKGS data value should not exist because no VirtualMachine is related to cluster1
Expand Down Expand Up @@ -500,15 +502,15 @@ var _ = Describe("ClusterBootstrap Reconciler", func() {
},
}
s.StringData = make(map[string]string)
s.StringData["values.yaml"] = "foobar-updated"
s.StringData["values.yaml"] = foobarUpdated
Expect(k8sClient.Update(ctx, s)).To(Succeed())

Eventually(func() bool {
s := &corev1.Secret{}
if err := k8sClient.Get(ctx, client.ObjectKey{Namespace: constants.TKGSystemNS, Name: util.GenerateDataValueSecretName(clusterName, foobarCarvelPackageRefName)}, s); err != nil {
return false
}
if string(s.Data["values.yaml"]) != "foobar-updated" {
if string(s.Data["values.yaml"]) != foobarUpdated {
return false
}

Expand Down
27 changes: 0 additions & 27 deletions addons/controllers/cpi/vspherecpiconfig_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ import (
cutil "github.com/vmware-tanzu/tanzu-framework/addons/controllers/utils"
"github.com/vmware-tanzu/tanzu-framework/addons/pkg/constants"
pkgtypes "github.com/vmware-tanzu/tanzu-framework/addons/pkg/types"
"github.com/vmware-tanzu/tanzu-framework/addons/pkg/util"
cpiv1alpha1 "github.com/vmware-tanzu/tanzu-framework/apis/addonconfigs/cpi/v1alpha1"
)

Expand All @@ -51,7 +50,6 @@ func (r *VSphereCPIConfigReconciler) ClusterToVSphereCPIConfig(o client.Object)
for i := 0; i < len(cs.Items); i++ {
config := &cs.Items[i]
if config.Namespace == cluster.Namespace {

// avoid enqueuing reconcile requests for template vSphereCPIConfig CRs in event handler of Cluster CR
if _, ok := config.Annotations[constants.TKGAnnotationTemplateConfig]; ok && config.Namespace == r.Config.SystemNamespace {
continue
Expand Down Expand Up @@ -435,11 +433,6 @@ func getUsernameAndPasswordFromSecret(s *v1.Secret) (string, string, error) {
return string(username), string(password), nil
}

// controlPlaneName returns the control plane name for a cluster name
func controlPlaneName(clusterName string) string {
return fmt.Sprintf("%s-control-plane", clusterName)
}

// getCCMName returns the name of cloud control manager for a cluster
func getCCMName(cluster *capvvmwarev1beta1.VSphereCluster) string {
return fmt.Sprintf("%s-%s", cluster.Name, "ccm")
Expand All @@ -452,23 +445,3 @@ func tryParseString(src string, sub *string) string {
}
return src
}

// tryParseClusterVariableBool tries to parse a boolean cluster variable,
// info any error that occurs
func (r *VSphereCPIConfigReconciler) tryParseClusterVariableBool(cluster *clusterapiv1beta1.Cluster, variableName string) bool {
res, err := util.ParseClusterVariableBool(cluster, variableName)
if err != nil {
r.Log.Info(fmt.Sprintf("Cannot parse cluster variable with key %s", variableName))
}
return res
}

// tryParseClusterVariableString tries to parse a string cluster variable,
// info any error that occurs
func (r *VSphereCPIConfigReconciler) tryParseClusterVariableString(cluster *clusterapiv1beta1.Cluster, variableName string) string {
res, err := util.ParseClusterVariableString(cluster, variableName)
if err != nil {
r.Log.Info(fmt.Sprintf("cannot parse cluster variable with key %s", variableName))
}
return res
}
15 changes: 7 additions & 8 deletions addons/controllers/csi/vspherecsiconfig_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
capvvmwarev1beta1 "sigs.k8s.io/cluster-api-provider-vsphere/apis/vmware/v1beta1"
clusterapiv1beta1 "sigs.k8s.io/cluster-api/api/v1beta1"
clusterv1beta1 "sigs.k8s.io/cluster-api/api/v1beta1"
capictrlpkubeadmv1beta1 "sigs.k8s.io/cluster-api/controlplane/kubeadm/api/v1beta1"
clusterapiutil "sigs.k8s.io/cluster-api/util"
Expand Down Expand Up @@ -82,7 +81,7 @@ func (r *VSphereCSIConfigReconciler) ClusterToVSphereCSIConfig(o client.Object)
// mapVSphereCSIConfigToDataValues maps VSphereCSIConfig CR to data values
func (r *VSphereCSIConfigReconciler) mapVSphereCSIConfigToDataValues(ctx context.Context,
vcsiConfig *csiv1alpha1.VSphereCSIConfig,
cluster *clusterapiv1beta1.Cluster) (*DataValues, error) {
cluster *clusterv1beta1.Cluster) (*DataValues, error) {

switch vcsiConfig.Spec.VSphereCSI.Mode {
case VSphereCSINonParavirtualMode:
Expand All @@ -98,7 +97,7 @@ func (r *VSphereCSIConfigReconciler) mapVSphereCSIConfigToDataValues(ctx context
}

func (r *VSphereCSIConfigReconciler) mapVSphereCSIConfigToDataValuesParavirtual(ctx context.Context,
cluster *clusterapiv1beta1.Cluster) (*DataValues, error) {
cluster *clusterv1beta1.Cluster) (*DataValues, error) {

dvs := &DataValues{}
dvs.VSpherePVCSI = &DataValuesVSpherePVCSI{}
Expand Down Expand Up @@ -136,7 +135,7 @@ func (r *VSphereCSIConfigReconciler) mapVSphereCSIConfigToDataValuesParavirtual(

func (r *VSphereCSIConfigReconciler) mapVSphereCSIConfigToDataValuesNonParavirtual(ctx context.Context,
vcsiConfig *csiv1alpha1.VSphereCSIConfig,
cluster *clusterapiv1beta1.Cluster) (*DataValues, error) {
cluster *clusterv1beta1.Cluster) (*DataValues, error) {

dvs := &DataValues{}

Expand Down Expand Up @@ -206,10 +205,10 @@ func (r *VSphereCSIConfigReconciler) mapVSphereCSIConfigToDataValuesNonParavirtu
// and returns the cluster. It tries to read the cluster name from the VSphereCSIConfig's owner reference objects.
// If not there, we assume the owner cluster and VSphereCSIConfig always has the same name.
func (r *VSphereCSIConfigReconciler) getOwnerCluster(ctx context.Context,
vcsiConfig *csiv1alpha1.VSphereCSIConfig) (*clusterapiv1beta1.Cluster, error) {
vcsiConfig *csiv1alpha1.VSphereCSIConfig) (*clusterv1beta1.Cluster, error) {

logger := log.FromContext(ctx)
cluster := &clusterapiv1beta1.Cluster{}
cluster := &clusterv1beta1.Cluster{}
clusterName := vcsiConfig.Name // usually the corresponding 'cluster' shares the same name

// retrieve the owner cluster for the VSphereCSIConfig object
Expand Down Expand Up @@ -387,7 +386,7 @@ func (r *VSphereCSIConfigReconciler) constrainNumberOfDeploymentReplicas(ctx con
}

func (r *VSphereCSIConfigReconciler) computeRecommendedNumberOfDeploymentReplicas(ctx context.Context,
cluster *clusterapiv1beta1.Cluster) (int32, error) {
cluster *clusterv1beta1.Cluster) (int32, error) {

cpNodeCount, err := r.getNumberOfControlPlaneNodes(ctx, cluster)

Expand All @@ -399,7 +398,7 @@ func (r *VSphereCSIConfigReconciler) computeRecommendedNumberOfDeploymentReplica
}

func (r *VSphereCSIConfigReconciler) getNumberOfControlPlaneNodes(ctx context.Context,
cluster *clusterapiv1beta1.Cluster) (int32, error) {
cluster *clusterv1beta1.Cluster) (int32, error) {

name := cluster.Spec.ControlPlaneRef.Name
namespace := cluster.Spec.ControlPlaneRef.Namespace
Expand Down
8 changes: 6 additions & 2 deletions addons/controllers/kappcontrollerconfig_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ import (
runv1alpha3 "github.com/vmware-tanzu/tanzu-framework/apis/run/v1alpha3"
)

const (
testKappController1 = "test-kapp-controller-1"
)

var _ = Describe("KappControllerConfig Reconciler", func() {
var (
kappConfigCRName string
Expand Down Expand Up @@ -47,7 +51,7 @@ var _ = Describe("KappControllerConfig Reconciler", func() {
Context("reconcile KappControllerConfig for management cluster", func() {

BeforeEach(func() {
kappConfigCRName = "test-kapp-controller-1"
kappConfigCRName = testKappController1
clusterResourceFilePath = "testdata/test-kapp-controller-1.yaml"
})

Expand Down Expand Up @@ -150,7 +154,7 @@ var _ = Describe("KappControllerConfig Reconciler", func() {
Context("Reconcile KappControllerConfig used as template", func() {

BeforeEach(func() {
kappConfigCRName = "test-kapp-controller-1"
kappConfigCRName = testKappController1
clusterResourceFilePath = "testdata/test-kapp-controller-template-config-1.yaml"
})

Expand Down
3 changes: 0 additions & 3 deletions addons/controllers/packageinstallstatus_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,6 @@ import (
runtanzuv1alpha3 "github.com/vmware-tanzu/tanzu-framework/apis/run/v1alpha3"
)

const kappCtrlPkgPrefix = "kapp-controller"

type ClusterRole int

const (
Expand Down Expand Up @@ -167,7 +165,6 @@ func (r *PackageInstallStatusReconciler) Reconcile(_ context.Context, req reconc
if err := r.reconcile(r.Client, cluster, clusterRole, log); err != nil {
return ctrl.Result{}, err
}

} else {
// the cluster is a remote workload cluster
clusterRole = clusterRoleWorkload
Expand Down
3 changes: 1 addition & 2 deletions addons/controllers/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ var (
addonConfigCRDBasesPath = filepath.Join("..", "..", "apis", "addonconfigs", "config", "crd", "bases")
runCRDPath = filepath.Join("..", "..", "apis", "run", "config", "crd", "bases")
localCRDPath = filepath.Join("testdata", "internal-crds")
setupLog = ctrl.Log.WithName("controllers").WithName("Addon")
)

func TestAddonController(t *testing.T) {
Expand Down Expand Up @@ -246,8 +247,6 @@ var _ = BeforeSuite(func(done Done) {
clientSet, err = kubernetes.NewForConfig(cfg)
Expect(err).ToNot(HaveOccurred())

setupLog := ctrl.Log.WithName("controllers").WithName("Addon")

ctx, cancel = context.WithCancel(ctx)
crdwaiter := crdwait.CRDWaiter{
Ctx: ctx,
Expand Down
2 changes: 1 addition & 1 deletion addons/controllers/vspherecpiconfig_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,7 @@ var _ = Describe("VSphereCPIConfig Reconciler", func() {
By("create aggregated cluster role")
clusterRole := &rbacv1.ClusterRole{}
Eventually(func() bool {
key := client.ObjectKey{
key = client.ObjectKey{
Name: constants.VsphereCPIProviderServiceAccountAggregatedClusterRole,
}
if err := k8sClient.Get(ctx, key, clusterRole); err != nil {
Expand Down
2 changes: 1 addition & 1 deletion addons/controllers/vspherecsiconfig_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,7 @@ var _ = Describe("VSphereCSIConfig Reconciler", func() {
It("Should reconcile aggregated cluster role", func() {
clusterRole := &rbacv1.ClusterRole{}
Eventually(func() error {
key := client.ObjectKey{
key = client.ObjectKey{
Name: constants.VsphereCSIProviderServiceAccountAggregatedClusterRole,
}
if err := k8sClient.Get(ctx, key, clusterRole); err != nil {
Expand Down
3 changes: 0 additions & 3 deletions addons/controllers/webhooks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
cert2 "k8s.io/client-go/util/cert"
ctrl "sigs.k8s.io/controller-runtime"

"github.com/vmware-tanzu/tanzu-framework/addons/pkg/constants"
"github.com/vmware-tanzu/tanzu-framework/addons/pkg/webhooks"
Expand All @@ -22,8 +21,6 @@ const (
zeroTime = time.Second * 0
)

var setupLog = ctrl.Log.WithName("controllers").WithName("Addon")

var _ = Describe("when webhook TLS is being continuously managed", func() {

Context("if check frequency time is reached", func() {
Expand Down
4 changes: 4 additions & 0 deletions addons/fakegen/fakegen.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
// Copyright 2022 VMware, Inc. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

// Package fakegen provides a fake CtrClient for testing
package fakegen

import (
Expand Down
1 change: 0 additions & 1 deletion addons/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"context"
"flag"
"net/http"
_ "net/http/pprof"
"os"
"path"
"time"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ func (h *Helper) AddMissingSpecFieldsFromTemplate(clusterBootstrapTemplate *runt
return nil
}

func addMissingFields(copyFrom, destination, keysToSkip map[string]interface{}) error {
func addMissingFields(copyFrom, destination, keysToSkip map[string]interface{}) error { //nolint
for keyInFrom, valueInFrom := range copyFrom {
if keysToSkip != nil {
// There might be some keys we do not want to add to destination.
Expand Down Expand Up @@ -287,7 +287,7 @@ func addMissingFields(copyFrom, destination, keysToSkip map[string]interface{})

// removeFields deletes the map entries by comparing the keys with keysToRemove
func removeFields(m, keysToRemove map[string]interface{}) error {
if keysToRemove == nil || len(keysToRemove) == 0 {
if len(keysToRemove) == 0 {
return nil
}
for key, value := range m {
Expand All @@ -313,7 +313,7 @@ func removeFields(m, keysToRemove map[string]interface{}) error {
// object has embedded TypedLocalObjectReference(e.g., CPI has VSphereCPIConfig as valuesFrom, VSphereCPIConfig has
// a Secret reference under its Spec), this function clones the embedded object into the cluster namespace as well.
// 3. Add OwnerReferences and Labels to the cloned objects.
func (h *Helper) HandleExistingClusterBootstrap(clusterBootstrap *runtanzuv1alpha3.ClusterBootstrap, cluster *clusterapiv1beta1.Cluster, tkrName, systemNamespace string) (*runtanzuv1alpha3.ClusterBootstrap, error) {
func (h *Helper) HandleExistingClusterBootstrap(clusterBootstrap *runtanzuv1alpha3.ClusterBootstrap, cluster *clusterapiv1beta1.Cluster, tkrName, systemNamespace string) (*runtanzuv1alpha3.ClusterBootstrap, error) { //nolint
packages := append([]*runtanzuv1alpha3.ClusterBootstrapPackage{
clusterBootstrap.Spec.CNI,
clusterBootstrap.Spec.CPI,
Expand Down
Loading

0 comments on commit 584a33a

Please # to comment.