From b5ced24cd46e142af1c32f36f61ee097cacc1bdc Mon Sep 17 00:00:00 2001 From: Slavik Panasovets Date: Tue, 27 Aug 2024 18:29:58 +0000 Subject: [PATCH] Remove unused code - Add .golangci.yaml config with only unused linter enabled --- .golangci.yaml | 11 +++++ cmd/gcp-controller-manager/BUILD | 1 - cmd/gcp-controller-manager/ca_cache_test.go | 3 +- cmd/gcp-controller-manager/csr_signer.go | 14 +++---- .../dpwi/nodesyncer/BUILD | 1 - .../dpwi/nodesyncer/nodesyncer.go | 2 - .../dpwi/serviceaccounts/types.go | 16 -------- cmd/gcp-controller-manager/main.go | 9 ++-- .../node_csr_approver_test.go | 41 ------------------- cmd/gke-gcloud-auth-plugin/cred_test.go | 1 - .../gkenetworkparamset_controller_test.go | 26 ++++++------ pkg/controller/nodeipam/BUILD | 1 - .../nodeipam/ipam/cidr_allocator.go | 6 --- .../nodeipam/node_ipam_controller.go | 12 ------ providers/gce/gce.go | 2 - 15 files changed, 34 insertions(+), 112 deletions(-) create mode 100644 .golangci.yaml diff --git a/.golangci.yaml b/.golangci.yaml new file mode 100644 index 0000000000..2e0fe49511 --- /dev/null +++ b/.golangci.yaml @@ -0,0 +1,11 @@ +linters: + # Disable all linters. + # Default: false + disable-all: true + # Enable specific linter + # https://golangci-lint.run/usage/linters/#enabled-by-default-linters + enable: + - unused + +run: + timeout: 3m diff --git a/cmd/gcp-controller-manager/BUILD b/cmd/gcp-controller-manager/BUILD index 8a37576d8f..9230473dca 100644 --- a/cmd/gcp-controller-manager/BUILD +++ b/cmd/gcp-controller-manager/BUILD @@ -121,7 +121,6 @@ go_test( "//vendor/github.com/google/go-cmp/cmp/cmpopts", "//vendor/google.golang.org/api/compute/v0.beta:v0_beta", "//vendor/google.golang.org/api/compute/v1:compute", - "//vendor/google.golang.org/api/container/v1:container", "//vendor/k8s.io/api/authorization/v1:authorization", "//vendor/k8s.io/api/certificates/v1:certificates", "//vendor/k8s.io/api/certificates/v1beta1", diff --git a/cmd/gcp-controller-manager/ca_cache_test.go b/cmd/gcp-controller-manager/ca_cache_test.go index 90d1872870..dd0b0e2df7 100644 --- a/cmd/gcp-controller-manager/ca_cache_test.go +++ b/cmd/gcp-controller-manager/ca_cache_test.go @@ -90,7 +90,6 @@ type fakeCA struct { intermediateCert *x509.Certificate intermediateCertKey *rsa.PrivateKey validCert *x509.Certificate - validCertKey *rsa.PrivateKey invalidCerts map[string]*x509.Certificate srvURL string } @@ -202,7 +201,7 @@ func (ca *fakeCA) regenerateValidCert(t *testing.T, id nodeidentity.Identity) { if err != nil { t.Fatal(err) } - _, ca.validCert, ca.validCertKey = makeCert(t, &x509.Certificate{ + _, ca.validCert, _ = makeCert(t, &x509.Certificate{ SerialNumber: big.NewInt(3), NotBefore: time.Now(), NotAfter: time.Now().Add(time.Hour), diff --git a/cmd/gcp-controller-manager/csr_signer.go b/cmd/gcp-controller-manager/csr_signer.go index c194909d95..9b0a5bcea4 100644 --- a/cmd/gcp-controller-manager/csr_signer.go +++ b/cmd/gcp-controller-manager/csr_signer.go @@ -49,10 +49,9 @@ var ( // gkeSigner uses external calls to GKE in order to sign certificate signing // requests. type gkeSigner struct { - webhook *webhook.GenericWebhook - ctx *controllerContext - retryBackoff *wait.Backoff - validators []csrValidator + webhook *webhook.GenericWebhook + ctx *controllerContext + validators []csrValidator } // newGKESigner will create a new instance of a gkeSigner. @@ -66,10 +65,9 @@ func newGKESigner(ctx *controllerContext) (*gkeSigner, error) { return nil, err } return &gkeSigner{ - webhook: webhook, - ctx: ctx, - retryBackoff: ClusterSigningGKERetryBackoff, - validators: csrValidators(), + webhook: webhook, + ctx: ctx, + validators: csrValidators(), }, nil } diff --git a/cmd/gcp-controller-manager/dpwi/nodesyncer/BUILD b/cmd/gcp-controller-manager/dpwi/nodesyncer/BUILD index aac9869bcd..418dd124f8 100644 --- a/cmd/gcp-controller-manager/dpwi/nodesyncer/BUILD +++ b/cmd/gcp-controller-manager/dpwi/nodesyncer/BUILD @@ -14,7 +14,6 @@ go_library( "//vendor/k8s.io/client-go/informers/core/v1:core", "//vendor/k8s.io/client-go/tools/cache", "//vendor/k8s.io/client-go/tools/clientcmd/api", - "//vendor/k8s.io/client-go/util/workqueue", ], ) diff --git a/cmd/gcp-controller-manager/dpwi/nodesyncer/nodesyncer.go b/cmd/gcp-controller-manager/dpwi/nodesyncer/nodesyncer.go index 2cba12d9c4..7b85f66dbc 100644 --- a/cmd/gcp-controller-manager/dpwi/nodesyncer/nodesyncer.go +++ b/cmd/gcp-controller-manager/dpwi/nodesyncer/nodesyncer.go @@ -31,7 +31,6 @@ import ( coreinformers "k8s.io/client-go/informers/core/v1" "k8s.io/client-go/tools/cache" clientcmdapi "k8s.io/client-go/tools/clientcmd/api" - "k8s.io/client-go/util/workqueue" "k8s.io/cloud-provider-gcp/cmd/gcp-controller-manager/dpwi/auth" "k8s.io/cloud-provider-gcp/cmd/gcp-controller-manager/dpwi/ctxlog" "k8s.io/cloud-provider-gcp/cmd/gcp-controller-manager/dpwi/eventhandler" @@ -48,7 +47,6 @@ type NodeHandler struct { eventhandler.EventHandler podIndexer cache.Indexer nodeIndexer cache.Indexer - queue workqueue.RateLimitingInterface verifier verifier auth *auth.Client nodeMap *nodeMap diff --git a/cmd/gcp-controller-manager/dpwi/serviceaccounts/types.go b/cmd/gcp-controller-manager/dpwi/serviceaccounts/types.go index 0f51b28986..cc6dc3d91f 100644 --- a/cmd/gcp-controller-manager/dpwi/serviceaccounts/types.go +++ b/cmd/gcp-controller-manager/dpwi/serviceaccounts/types.go @@ -22,7 +22,6 @@ import ( "fmt" "sync" - "k8s.io/client-go/tools/cache" "k8s.io/cloud-provider-gcp/cmd/gcp-controller-manager/dpwi/ctxlog" ) @@ -60,14 +59,6 @@ func (sa ServiceAccount) Key() string { return fmt.Sprintf("%s/%s", sa.Namespace, sa.Name) } -func saFromKey(key string) (ServiceAccount, error) { - namespace, name, err := cache.SplitMetaNamespaceKey(key) - if err != nil { - return ServiceAccount{}, err - } - return ServiceAccount{namespace, name}, nil -} - // Serialize returns m in its JSON encoded format or error if serialization had failed. func (m *SAMap) Serialize() ([]byte, error) { m.RLock() @@ -115,13 +106,6 @@ func (m *saMap) get(sa ServiceAccount) (GSAEmail, bool) { return gsa, ok } -// Serialize returns m in its JSON encoded format or error if serialization had failed. -func (m *saMap) serialize() ([]byte, error) { - m.RLock() - defer m.RUnlock() - return json.Marshal(m.ma) -} - type verifyResult struct { preVerifiedGSA GSAEmail curGSA GSAEmail diff --git a/cmd/gcp-controller-manager/main.go b/cmd/gcp-controller-manager/main.go index 2a2f1c0cfd..cb0ec7d722 100644 --- a/cmd/gcp-controller-manager/main.go +++ b/cmd/gcp-controller-manager/main.go @@ -72,10 +72,11 @@ var ( authSyncNodeURL = pflag.String("auth-sync-node-url", "", "URL for reaching the Auth Service SyncNode API.") hmsAuthorizeSAMappingURL = pflag.String("hms-authorize-sa-mapping-url", "", "URL for reaching the Hosted Master Service AuthorizeSAMapping API.") hmsSyncNodeURL = pflag.String("hms-sync-node-url", "", "URL for reaching the Hosted Master Service SyncNode API.") - autopilotEnabled = pflag.Bool("autopilot", false, "Is this a GKE Autopilot cluster.") clearStalePodsOnNodeRegistration = pflag.Bool("clearStalePodsOnNodeRegistration", false, "If true, after node registration, delete pods bound to old node.") - kubeconfigQPS = pflag.Float32("kubeconfig-qps", 100, "QPS to use while talking with kube-apiserver.") - kubeconfigBurst = pflag.Int("kubeconfig-burst", 200, "Burst to use while talking with kube-apiserver.") + // Deprecated: do not use, does not have any effect. + autopilotEnabled = pflag.Bool("autopilot", false, "Is this a GKE Autopilot cluster.") + kubeconfigQPS = pflag.Float32("kubeconfig-qps", 100, "QPS to use while talking with kube-apiserver.") + kubeconfigBurst = pflag.Int("kubeconfig-burst", 200, "Burst to use while talking with kube-apiserver.") ) func main() { @@ -110,7 +111,6 @@ func main() { hmsAuthorizeSAMappingURL: *hmsAuthorizeSAMappingURL, hmsSyncNodeURL: *hmsSyncNodeURL, healthz: healthz.NewHandler(), - autopilotEnabled: *autopilotEnabled, clearStalePodsOnNodeRegistration: *clearStalePodsOnNodeRegistration, } var err error @@ -167,7 +167,6 @@ type controllerManager struct { authSyncNodeURL string hmsAuthorizeSAMappingURL string hmsSyncNodeURL string - autopilotEnabled bool clearStalePodsOnNodeRegistration bool // Fields initialized from other sources. diff --git a/cmd/gcp-controller-manager/node_csr_approver_test.go b/cmd/gcp-controller-manager/node_csr_approver_test.go index f9ab0a9c28..4872485812 100644 --- a/cmd/gcp-controller-manager/node_csr_approver_test.go +++ b/cmd/gcp-controller-manager/node_csr_approver_test.go @@ -37,7 +37,6 @@ import ( "github.com/google/go-cmp/cmp/cmpopts" betacompute "google.golang.org/api/compute/v0.beta" compute "google.golang.org/api/compute/v1" - container "google.golang.org/api/container/v1" authorization "k8s.io/api/authorization/v1" capi "k8s.io/api/certificates/v1" certsv1 "k8s.io/api/certificates/v1" @@ -296,11 +295,6 @@ func TestNodeApproverHandle(t *testing.T) { } } -// stringPointer copies a constant string and returns a pointer to the copy. -func stringPointer(str string) *string { - return &str -} - func TestValidators(t *testing.T) { t.Run("isLegacyNodeClientCert", func(t *testing.T) { goodCase := func(b *csrBuilder, _ *controllerContext) { @@ -725,41 +719,6 @@ func fakeGCPAPI(t *testing.T, ekPub *rsa.PublicKey) (*http.Client, *httptest.Ser return cl, srv } -func fakeGKEAPI(t *testing.T) (*http.Client, *httptest.Server) { - srv := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) { - t.Logf("fakeGKEAPI request %q", req.URL.Path) - switch req.URL.Path { - case "/v1/projects/p0/locations/z0/clusters/c0": - json.NewEncoder(rw).Encode(container.Cluster{ - Name: "c0", - NodePools: []*container.NodePool{ - {InstanceGroupUrls: []string{"https://www.googleapis.com/compute/v1/projects/2/zones/r0/instanceGroupManagers/ig0"}}, - {InstanceGroupUrls: []string{"https://www.googleapis.com/compute/v1/projects/2/zones/z0/instanceGroupManagers/ig0"}}, - }, - }) - case "/v1/projects/p0/locations/z0/clusters/c1": - json.NewEncoder(rw).Encode(container.Cluster{ - Name: "c1", - NodePools: []*container.NodePool{ - {InstanceGroupUrls: []string{"https://www.googleapis.com/compute/v1/projects/2/zones/z0/instanceGroupManagers/ig1"}}, - }, - }) - case "/v1/projects/p0/locations/z0/clusters/c2": - json.NewEncoder(rw).Encode(container.Cluster{ - Name: "c2", - NodePools: []*container.NodePool{ - {InstanceGroupUrls: []string{"https://www.googleapis.com/compute/v1/projects/2/zones/z0/instanceGroupManagers/unknown"}}, - }, - }) - default: - http.Error(rw, "not found", http.StatusNotFound) - } - })) - cl := srv.Client() - cl.Transport = fakeTransport{srv.URL} - return cl, srv -} - type fakeTransport struct{ addr string } func (t fakeTransport) RoundTrip(r *http.Request) (*http.Response, error) { diff --git a/cmd/gke-gcloud-auth-plugin/cred_test.go b/cmd/gke-gcloud-auth-plugin/cred_test.go index f09409f4d8..b88ba6ec94 100644 --- a/cmd/gke-gcloud-auth-plugin/cred_test.go +++ b/cmd/gke-gcloud-auth-plugin/cred_test.go @@ -40,7 +40,6 @@ var ( "extra_args": "%s" } ` - invalidCacheFile = "invalid_cache_file" fakeCurrentContext = "gke_user-gke-dev_us-east1-b_cluster-1" cachedAccessToken = "ya29.cached_token" diff --git a/pkg/controller/gkenetworkparamset/gkenetworkparamset_controller_test.go b/pkg/controller/gkenetworkparamset/gkenetworkparamset_controller_test.go index 84cfa3bc1c..9d77c94d56 100644 --- a/pkg/controller/gkenetworkparamset/gkenetworkparamset_controller_test.go +++ b/pkg/controller/gkenetworkparamset/gkenetworkparamset_controller_test.go @@ -29,13 +29,12 @@ import ( ) type testGKENetworkParamSetController struct { - networkClient *networkfake.Clientset - informerFactory networkinformers.SharedInformerFactory - clusterValues gce.TestClusterValues - controller *Controller - metrics *controllers.ControllerManagerMetrics - cloud *gce.Cloud - nodeStore cache.Store + networkClient *networkfake.Clientset + clusterValues gce.TestClusterValues + controller *Controller + metrics *controllers.ControllerManagerMetrics + cloud *gce.Cloud + nodeStore cache.Store } const ( @@ -94,13 +93,12 @@ func setupGKENetworkParamSetController(ctx context.Context) *testGKENetworkParam fakeGCE.Compute().Networks().Insert(ctx, nonDefaultNetworkKey, nonDefaultNetwork) return &testGKENetworkParamSetController{ - networkClient: fakeNetworking, - informerFactory: nwInfFactory, - clusterValues: testClusterValues, - controller: controller, - metrics: metrics, - cloud: fakeGCE, - nodeStore: fakeNodeInformer.Informer().GetStore(), + networkClient: fakeNetworking, + clusterValues: testClusterValues, + controller: controller, + metrics: metrics, + cloud: fakeGCE, + nodeStore: fakeNodeInformer.Informer().GetStore(), } } diff --git a/pkg/controller/nodeipam/BUILD b/pkg/controller/nodeipam/BUILD index f8c9143a32..2d277f0b63 100644 --- a/pkg/controller/nodeipam/BUILD +++ b/pkg/controller/nodeipam/BUILD @@ -17,7 +17,6 @@ go_library( "//vendor/k8s.io/client-go/informers/core/v1:core", "//vendor/k8s.io/client-go/kubernetes", "//vendor/k8s.io/client-go/kubernetes/typed/core/v1:core", - "//vendor/k8s.io/client-go/listers/core/v1:core", "//vendor/k8s.io/client-go/tools/cache", "//vendor/k8s.io/client-go/tools/record", "//vendor/k8s.io/cloud-provider", diff --git a/pkg/controller/nodeipam/ipam/cidr_allocator.go b/pkg/controller/nodeipam/ipam/cidr_allocator.go index 3fb32e85d4..33edbd6cb5 100644 --- a/pkg/controller/nodeipam/ipam/cidr_allocator.go +++ b/pkg/controller/nodeipam/ipam/cidr_allocator.go @@ -67,12 +67,6 @@ const ( // cidrUpdateRetries is the no. of times a NodeSpec update will be retried before dropping it. cidrUpdateRetries = 3 - // updateRetryTimeout is the time to wait before requeing a failed node for retry - updateRetryTimeout = 250 * time.Millisecond - - // maxUpdateRetryTimeout is the maximum amount of time between timeouts. - maxUpdateRetryTimeout = 5 * time.Second - // updateMaxRetries is the max retries for a failed node updateMaxRetries = 10 ) diff --git a/pkg/controller/nodeipam/node_ipam_controller.go b/pkg/controller/nodeipam/node_ipam_controller.go index 1eb47ec5c5..27f7bba477 100644 --- a/pkg/controller/nodeipam/node_ipam_controller.go +++ b/pkg/controller/nodeipam/node_ipam_controller.go @@ -28,7 +28,6 @@ import ( coreinformers "k8s.io/client-go/informers/core/v1" clientset "k8s.io/client-go/kubernetes" v1core "k8s.io/client-go/kubernetes/typed/core/v1" - corelisters "k8s.io/client-go/listers/core/v1" "k8s.io/client-go/tools/cache" "k8s.io/client-go/tools/record" cloudprovider "k8s.io/cloud-provider" @@ -52,15 +51,9 @@ const ( type Controller struct { allocatorType ipam.CIDRAllocatorType - cloud cloudprovider.Interface - clusterCIDRs []*net.IPNet serviceCIDR *net.IPNet secondaryServiceCIDR *net.IPNet - kubeClient clientset.Interface - // Method for easy mocking in unittest. - lookupIP func(host string) ([]net.IP, error) - nodeLister corelisters.NodeLister nodeInformerSynced cache.InformerSynced cidrAllocator ipam.CIDRAllocator @@ -111,10 +104,6 @@ func NewNodeIpamController( } ic := &Controller{ - cloud: cloud, - kubeClient: kubeClient, - lookupIP: net.LookupIP, - clusterCIDRs: clusterCIDRs, serviceCIDR: serviceCIDR, secondaryServiceCIDR: secondaryServiceCIDR, allocatorType: allocatorType, @@ -139,7 +128,6 @@ func NewNodeIpamController( } } - ic.nodeLister = nodeInformer.Lister() ic.nodeInformerSynced = nodeInformer.Informer().HasSynced return ic, nil diff --git a/providers/gce/gce.go b/providers/gce/gce.go index 3049924293..ae91ad4051 100644 --- a/providers/gce/gce.go +++ b/providers/gce/gce.go @@ -134,7 +134,6 @@ type Cloud struct { containerService *container.Service tpuService *tpuService client clientset.Interface - clientBuilder cloudprovider.ControllerClientBuilder eventBroadcaster record.EventBroadcaster eventRecorder record.EventRecorder projectID string @@ -675,7 +674,6 @@ func tryConvertToProjectNames(configProject, configNetworkProject string, servic // Initialize takes in a clientBuilder and spawns a goroutine for watching the clusterid configmap. // This must be called before utilizing the funcs of gce.ClusterID func (g *Cloud) Initialize(clientBuilder cloudprovider.ControllerClientBuilder, stop <-chan struct{}) { - g.clientBuilder = clientBuilder g.client = clientBuilder.ClientOrDie("cloud-provider") g.eventBroadcaster = record.NewBroadcaster()