From 58c15c8ed94809729c56c0565b4f0b90506388ba Mon Sep 17 00:00:00 2001 From: whitewindmills Date: Wed, 12 Jun 2024 17:01:14 +0800 Subject: [PATCH] fix code inspection errors Signed-off-by: whitewindmills --- .../genkarmadactldocs/gen_karmadactl_docs.go | 12 ++++---- .../federatedhpa/metrics/client.go | 30 +++++++++---------- .../endpointslice_collect_controller.go | 10 +++---- .../framework/plugins/cluster/cluster.go | 3 +- pkg/util/hash/hash.go | 2 +- pkg/util/lifted/validatingmcs.go | 2 +- pkg/util/proxy/upgrader.go | 2 +- test/e2e/framework/karmadactl.go | 6 ++-- 8 files changed, 33 insertions(+), 34 deletions(-) diff --git a/hack/tools/genkarmadactldocs/gen_karmadactl_docs.go b/hack/tools/genkarmadactldocs/gen_karmadactl_docs.go index f7b962024..5a4dbce77 100644 --- a/hack/tools/genkarmadactldocs/gen_karmadactl_docs.go +++ b/hack/tools/genkarmadactldocs/gen_karmadactl_docs.go @@ -111,15 +111,15 @@ func main() { // Set environment variables used by karmadactl so the output is consistent, // regardless of where we run. os.Setenv("HOME", "/home/username") - karmadactl := karmadactl.NewKarmadaCtlCommand("karmadactl", "karmadactl") - karmadactl.DisableAutoGenTag = true - err = doc.GenMarkdownTree(karmadactl, outDir) + karmadactlCmd := karmadactl.NewKarmadaCtlCommand("karmadactl", "karmadactl") + karmadactlCmd.DisableAutoGenTag = true + err = doc.GenMarkdownTree(karmadactlCmd, outDir) if err != nil { fmt.Fprintf(os.Stderr, "failed to generate docs: %v\n", err) os.Exit(1) } - err = GenMarkdownTreeForIndex(karmadactl, outDir) + err = GenMarkdownTreeForIndex(karmadactlCmd, outDir) if err != nil { fmt.Fprintf(os.Stderr, "failed to generate index docs: %v\n", err) os.Exit(1) @@ -153,8 +153,8 @@ func main() { newlines := []string{"---", "title: " + title} newlines = append(newlines, lines...) - newcontent := strings.Join(newlines, "\n") - return os.WriteFile(path, []byte(newcontent), info.Mode()) + newContent := strings.Join(newlines, "\n") + return os.WriteFile(path, []byte(newContent), info.Mode()) }) if err != nil { fmt.Fprintf(os.Stderr, "failed to process docs: %v\n", err) diff --git a/pkg/controllers/federatedhpa/metrics/client.go b/pkg/controllers/federatedhpa/metrics/client.go index 7e98b05a3..6e63a6548 100644 --- a/pkg/controllers/federatedhpa/metrics/client.go +++ b/pkg/controllers/federatedhpa/metrics/client.go @@ -72,24 +72,24 @@ func (c *resourceMetricsClient) GetResourceMetric(ctx context.Context, resource startTime := time.Now() defer metrics.ObserveFederatedHPAPullMetricsLatency(err, "ResourceMetric", startTime) - metrics, err := c.client.PodMetricses(namespace).List(ctx, metav1.ListOptions{LabelSelector: selector.String()}) + podMetrics, err := c.client.PodMetricses(namespace).List(ctx, metav1.ListOptions{LabelSelector: selector.String()}) if err != nil { return nil, time.Time{}, fmt.Errorf("unable to fetch metrics from resource metrics API: %v", err) } - if len(metrics.Items) == 0 { + if len(podMetrics.Items) == 0 { return nil, time.Time{}, fmt.Errorf("no metrics returned from resource metrics API") } var res PodMetricsInfo if container != "" { - res, err = getContainerMetrics(metrics.Items, resource, container) + res, err = getContainerMetrics(podMetrics.Items, resource, container) if err != nil { return nil, time.Time{}, fmt.Errorf("failed to get container metrics: %v", err) } } else { - res = getPodMetrics(metrics.Items, resource) + res = getPodMetrics(podMetrics.Items, resource) } - timestamp := metrics.Items[0].Timestamp.Time + timestamp := podMetrics.Items[0].Timestamp.Time return res, timestamp, nil } @@ -156,17 +156,17 @@ func (c *customMetricsClient) GetRawMetric(metricName string, namespace string, startTime := time.Now() defer metrics.ObserveFederatedHPAPullMetricsLatency(err, "RawMetric", startTime) - metrics, err := c.client.NamespacedMetrics(namespace).GetForObjects(schema.GroupKind{Kind: "Pod"}, selector, metricName, metricSelector) + metricList, err := c.client.NamespacedMetrics(namespace).GetForObjects(schema.GroupKind{Kind: "Pod"}, selector, metricName, metricSelector) if err != nil { return nil, time.Time{}, fmt.Errorf("unable to fetch metrics from custom metrics API: %v", err) } - if len(metrics.Items) == 0 { + if len(metricList.Items) == 0 { return nil, time.Time{}, fmt.Errorf("no metrics returned from custom metrics API") } - res := make(PodMetricsInfo, len(metrics.Items)) - for _, m := range metrics.Items { + res := make(PodMetricsInfo, len(metricList.Items)) + for _, m := range metricList.Items { window := metricServerDefaultMetricWindow if m.WindowSeconds != nil { window = time.Duration(*m.WindowSeconds) * time.Second @@ -174,13 +174,13 @@ func (c *customMetricsClient) GetRawMetric(metricName string, namespace string, res[m.DescribedObject.Name] = PodMetric{ Timestamp: m.Timestamp.Time, Window: window, - Value: int64(m.Value.MilliValue()), + Value: m.Value.MilliValue(), } m.Value.MilliValue() } - timestamp := metrics.Items[0].Timestamp.Time + timestamp := metricList.Items[0].Timestamp.Time return res, timestamp, nil } @@ -225,19 +225,19 @@ func (c *externalMetricsClient) GetExternalMetric(metricName, namespace string, startTime := time.Now() defer metrics.ObserveFederatedHPAPullMetricsLatency(err, "ExternalMetric", startTime) - metrics, err := c.client.NamespacedMetrics(namespace).List(metricName, selector) + externalMetrics, err := c.client.NamespacedMetrics(namespace).List(metricName, selector) if err != nil { return []int64{}, time.Time{}, fmt.Errorf("unable to fetch metrics from external metrics API: %v", err) } - if len(metrics.Items) == 0 { + if len(externalMetrics.Items) == 0 { return nil, time.Time{}, fmt.Errorf("no metrics returned from external metrics API") } res := make([]int64, 0) - for _, m := range metrics.Items { + for _, m := range externalMetrics.Items { res = append(res, m.Value.MilliValue()) } - timestamp := metrics.Items[0].Timestamp.Time + timestamp := externalMetrics.Items[0].Timestamp.Time return res, timestamp, nil } diff --git a/pkg/controllers/multiclusterservice/endpointslice_collect_controller.go b/pkg/controllers/multiclusterservice/endpointslice_collect_controller.go index 08cd0292b..0952c7dbc 100644 --- a/pkg/controllers/multiclusterservice/endpointslice_collect_controller.go +++ b/pkg/controllers/multiclusterservice/endpointslice_collect_controller.go @@ -402,7 +402,7 @@ func getEndpointSliceWorkMeta(c client.Client, ns string, workName string, endpo return metav1.ObjectMeta{}, err } - labels := map[string]string{ + ls := map[string]string{ util.MultiClusterServiceNamespaceLabel: endpointSlice.GetNamespace(), util.MultiClusterServiceNameLabel: endpointSlice.GetLabels()[discoveryv1.LabelServiceName], // indicate the Work should be not propagated since it's collected resource. @@ -410,18 +410,18 @@ func getEndpointSliceWorkMeta(c client.Client, ns string, workName string, endpo util.EndpointSliceWorkManagedByLabel: util.MultiClusterServiceKind, } if existWork.Labels == nil || (err != nil && apierrors.IsNotFound(err)) { - workMeta := metav1.ObjectMeta{Name: workName, Namespace: ns, Labels: labels} + workMeta := metav1.ObjectMeta{Name: workName, Namespace: ns, Labels: ls} return workMeta, nil } - labels = util.DedupeAndMergeLabels(labels, existWork.Labels) + ls = util.DedupeAndMergeLabels(ls, existWork.Labels) if value, ok := existWork.Labels[util.EndpointSliceWorkManagedByLabel]; ok { controllerSet := sets.New[string]() controllerSet.Insert(strings.Split(value, ".")...) controllerSet.Insert(util.MultiClusterServiceKind) - labels[util.EndpointSliceWorkManagedByLabel] = strings.Join(controllerSet.UnsortedList(), ".") + ls[util.EndpointSliceWorkManagedByLabel] = strings.Join(controllerSet.UnsortedList(), ".") } - workMeta := metav1.ObjectMeta{Name: workName, Namespace: ns, Labels: labels} + workMeta := metav1.ObjectMeta{Name: workName, Namespace: ns, Labels: ls} return workMeta, nil } diff --git a/pkg/search/proxy/framework/plugins/cluster/cluster.go b/pkg/search/proxy/framework/plugins/cluster/cluster.go index 5128020ac..9434fb7e3 100644 --- a/pkg/search/proxy/framework/plugins/cluster/cluster.go +++ b/pkg/search/proxy/framework/plugins/cluster/cluster.go @@ -150,8 +150,7 @@ func modifyRequest(req *http.Request, cluster string) error { return nil } - changed := false - changed = store.RemoveCacheSourceAnnotation(obj) || changed + changed := store.RemoveCacheSourceAnnotation(obj) changed = store.RecoverClusterResourceVersion(obj, cluster) || changed if changed { diff --git a/pkg/util/hash/hash.go b/pkg/util/hash/hash.go index 31facf6c2..dd21a9220 100644 --- a/pkg/util/hash/hash.go +++ b/pkg/util/hash/hash.go @@ -27,5 +27,5 @@ import ( // ensuring the hash does not change when a pointer changes. func DeepHashObject(hasher hash.Hash, objectToWrite interface{}) { hasher.Reset() - pretty.Fprintf(hasher, "%# v", objectToWrite) + pretty.Fprintf(hasher, "%v", objectToWrite) } diff --git a/pkg/util/lifted/validatingmcs.go b/pkg/util/lifted/validatingmcs.go index 6e61c7bb3..cb573e89d 100644 --- a/pkg/util/lifted/validatingmcs.go +++ b/pkg/util/lifted/validatingmcs.go @@ -22,7 +22,7 @@ package lifted import ( corev1 "k8s.io/api/core/v1" - validation "k8s.io/apimachinery/pkg/util/validation" + "k8s.io/apimachinery/pkg/util/validation" "k8s.io/apimachinery/pkg/util/validation/field" netutils "k8s.io/utils/net" ) diff --git a/pkg/util/proxy/upgrader.go b/pkg/util/proxy/upgrader.go index 3c5242a17..b0a314471 100644 --- a/pkg/util/proxy/upgrader.go +++ b/pkg/util/proxy/upgrader.go @@ -292,7 +292,7 @@ func (h *UpgradeAwareHandler) tryUpgrade(w http.ResponseWriter, req *http.Reques if len(rawResponse) > 0 { klog.V(6).Infof("Writing %d bytes to hijacked connection", len(rawResponse)) if _, err = requestHijackedConn.Write(rawResponse); err != nil { - utilruntime.HandleError(fmt.Errorf("Error proxying response from backend to client: %v", err)) + utilruntime.HandleError(fmt.Errorf("error proxying response from backend to client: %v", err)) } } diff --git a/test/e2e/framework/karmadactl.go b/test/e2e/framework/karmadactl.go index 30c7b8e7a..627ca88c2 100644 --- a/test/e2e/framework/karmadactl.go +++ b/test/e2e/framework/karmadactl.go @@ -64,7 +64,7 @@ func NewTestKubeconfig(kubeConfig, karmadaContext, karmadactlpath, namespace str // KarmadactlCmd runs the karmadactl executable through the wrapper script. func (tk *TestKubeconfig) KarmadactlCmd(args ...string) *exec.Cmd { - defaultArgs := []string{} + var defaultArgs []string if tk.KubeConfig != "" { defaultArgs = append(defaultArgs, "--"+clientcmd.RecommendedConfigPathFlag+"="+tk.KubeConfig) @@ -116,7 +116,7 @@ func (k *KarmadactlBuilder) exec() (string, error) { } // execWithFullOutput runs the karmadactl executable, and returns the stdout and stderr. -func (k KarmadactlBuilder) execWithFullOutput() (string, string, error) { +func (k *KarmadactlBuilder) execWithFullOutput() (string, string, error) { var stdout, stderr bytes.Buffer cmd := k.cmd cmd.Stdout, cmd.Stderr = &stdout, &stderr @@ -133,7 +133,7 @@ func (k KarmadactlBuilder) execWithFullOutput() (string, string, error) { if err != nil { var rc = 127 if ee, ok := err.(*exec.ExitError); ok { - rc = int(ee.Sys().(syscall.WaitStatus).ExitStatus()) + rc = ee.Sys().(syscall.WaitStatus).ExitStatus() } return stdout.String(), stderr.String(), uexec.CodeExitError{ Err: fmt.Errorf("error running %v:\nCommand stdout:\n%v\nstderr:\n%v\nerror:\n%v", cmd, cmd.Stdout, cmd.Stderr, err),