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: format code about errcheck,gosimple,ineffassign,unused #188

Merged
merged 1 commit into from
Feb 11, 2025
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
6 changes: 2 additions & 4 deletions cmd/api/app/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,10 +111,8 @@ func run(ctx context.Context, opts *options.Options) error {
ensureAPIServerConnectionOrDie()
serve(opts)
config.InitDashboardConfig(client.InClusterClient(), ctx.Done())
select {
case <-ctx.Done():
os.Exit(0)
}
<-ctx.Done()
os.Exit(0)
return nil
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cmd/api/app/api.go:114:2: S1000: should use a simple channel send/receive instead of select with a single case (gosimple)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

got it~ thanks

}

Expand Down
2 changes: 1 addition & 1 deletion cmd/api/app/routes/cluster/accesscluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ func (o pullModeOption) makeKarmadaAgentDeployment() *appsv1.Deployment {
//fmt.Sprintf("--controllers=%s", strings.Join(controllers, ",")),
//fmt.Sprintf("--proxy-server-address=%s", proxyServerAddress),
//fmt.Sprintf("--leader-elect-resource-namespace=%s", o.memberClusterNamespace),
fmt.Sprintf("--feature-gates=CustomizedClusterResourceModeling=true,MultiClusterService=true"),
"--feature-gates=CustomizedClusterResourceModeling=true,MultiClusterService=true",
"--cluster-status-update-frequency=10s",
"--bind-address=0.0.0.0",
"--secure-port=10357",
Expand Down
1 change: 0 additions & 1 deletion cmd/api/app/routes/cluster/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,6 @@ func handleDeleteCluster(c *gin.Context) {
return
}
common.Success(c, "ok")
return
}

func parseEndpointFromKubeconfig(kubeconfigContents string) (string, error) {
Expand Down
42 changes: 0 additions & 42 deletions cmd/api/app/routes/clusteroverridepolicy/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,48 +91,6 @@ func handlePostClusterOverridePolicy(c *gin.Context) {
common.Success(c, "ok")
}

func handlePutClusterOverridePolicy(c *gin.Context) {
ctx := context.Context(c)
overridepolicyRequest := new(v1.PutOverridePolicyRequest)
if err := c.ShouldBind(&overridepolicyRequest); err != nil {
common.Fail(c, err)
return
}
var err error
karmadaClient := client.InClusterKarmadaClient()
// todo check pp exist
if overridepolicyRequest.IsClusterScope {
clusterOverridePolicy := v1alpha1.ClusterOverridePolicy{}
if err = yaml.Unmarshal([]byte(overridepolicyRequest.OverrideData), &clusterOverridePolicy); err != nil {
klog.ErrorS(err, "Failed to unmarshal ClusterOverridePolicy")
common.Fail(c, err)
return
}
_, err = karmadaClient.PolicyV1alpha1().ClusterOverridePolicies().Update(ctx, &clusterOverridePolicy, metav1.UpdateOptions{})
} else {
overridePolicy := v1alpha1.OverridePolicy{}
if err = yaml.Unmarshal([]byte(overridepolicyRequest.OverrideData), &overridePolicy); err != nil {
klog.ErrorS(err, "Failed to unmarshal OverridePolicy")
common.Fail(c, err)
return
}
var oldPropagationPolicy *v1alpha1.OverridePolicy
oldPropagationPolicy, err = karmadaClient.PolicyV1alpha1().OverridePolicies(overridepolicyRequest.Namespace).Get(ctx, overridepolicyRequest.Name, metav1.GetOptions{})
if err == nil {
// only spec can be updated
overridePolicy.TypeMeta = oldPropagationPolicy.TypeMeta
overridePolicy.ObjectMeta = oldPropagationPolicy.ObjectMeta
_, err = karmadaClient.PolicyV1alpha1().OverridePolicies(overridepolicyRequest.Namespace).Update(ctx, &overridePolicy, metav1.UpdateOptions{})
}
}
if err != nil {
klog.ErrorS(err, "Failed to update OverridePolicy")
common.Fail(c, err)
return
}
common.Success(c, "ok")
}

func init() {
r := router.V1()
r.GET("/clusteroverridepolicy", handleGetClusterOverridePolicyList)
Expand Down
80 changes: 0 additions & 80 deletions cmd/api/app/routes/clusterpropagationpolicy/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,13 @@ import (
"github.com/gin-gonic/gin"
"github.com/karmada-io/karmada/pkg/apis/policy/v1alpha1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/util/retry"
"k8s.io/klog/v2"
"sigs.k8s.io/yaml"

"github.com/karmada-io/dashboard/cmd/api/app/router"
v1 "github.com/karmada-io/dashboard/cmd/api/app/types/api/v1"
"github.com/karmada-io/dashboard/cmd/api/app/types/common"
"github.com/karmada-io/dashboard/pkg/client"
"github.com/karmada-io/dashboard/pkg/common/errors"
"github.com/karmada-io/dashboard/pkg/resource/clusterpropagationpolicy"
)

Expand Down Expand Up @@ -93,84 +91,6 @@ func handlePostClusterPropagationPolicy(c *gin.Context) {
common.Success(c, "ok")
}

func handlePutClusterPropagationPolicy(c *gin.Context) {
ctx := context.Context(c)
propagationpolicyRequest := new(v1.PutPropagationPolicyRequest)
if err := c.ShouldBind(&propagationpolicyRequest); err != nil {
common.Fail(c, err)
return
}
var err error
karmadaClient := client.InClusterKarmadaClient()
// todo check pp exist
if propagationpolicyRequest.IsClusterScope {
clusterPropagationPolicy := v1alpha1.ClusterPropagationPolicy{}
if err = yaml.Unmarshal([]byte(propagationpolicyRequest.PropagationData), &clusterPropagationPolicy); err != nil {
klog.ErrorS(err, "Failed to unmarshal ClusterPropagationPolicy")
common.Fail(c, err)
return
}
_, err = karmadaClient.PolicyV1alpha1().ClusterPropagationPolicies().Update(ctx, &clusterPropagationPolicy, metav1.UpdateOptions{})
} else {
propagationPolicy := v1alpha1.PropagationPolicy{}
if err = yaml.Unmarshal([]byte(propagationpolicyRequest.PropagationData), &propagationPolicy); err != nil {
klog.ErrorS(err, "Failed to unmarshal PropagationPolicy")
common.Fail(c, err)
return
}
var oldPropagationPolicy *v1alpha1.PropagationPolicy
oldPropagationPolicy, err = karmadaClient.PolicyV1alpha1().PropagationPolicies(propagationpolicyRequest.Namespace).Get(ctx, propagationpolicyRequest.Name, metav1.GetOptions{})
if err == nil {
// only spec can be updated
propagationPolicy.TypeMeta = oldPropagationPolicy.TypeMeta
propagationPolicy.ObjectMeta = oldPropagationPolicy.ObjectMeta
_, err = karmadaClient.PolicyV1alpha1().PropagationPolicies(propagationpolicyRequest.Namespace).Update(ctx, &propagationPolicy, metav1.UpdateOptions{})
}
}
if err != nil {
klog.ErrorS(err, "Failed to update PropagationPolicy")
common.Fail(c, err)
return
}
common.Success(c, "ok")
}
func handleDeleteClusterPropagationPolicy(c *gin.Context) {
ctx := context.Context(c)
propagationpolicyRequest := new(v1.DeletePropagationPolicyRequest)
if err := c.ShouldBind(&propagationpolicyRequest); err != nil {
common.Fail(c, err)
return
}
var err error
karmadaClient := client.InClusterKarmadaClient()
if propagationpolicyRequest.IsClusterScope {
err = karmadaClient.PolicyV1alpha1().ClusterPropagationPolicies().Delete(ctx, propagationpolicyRequest.Name, metav1.DeleteOptions{})
if err != nil {
klog.ErrorS(err, "Failed to delete PropagationPolicy")
common.Fail(c, err)
return
}
} else {
err = karmadaClient.PolicyV1alpha1().PropagationPolicies(propagationpolicyRequest.Namespace).Delete(ctx, propagationpolicyRequest.Name, metav1.DeleteOptions{})
if err != nil {
klog.ErrorS(err, "Failed to delete PropagationPolicy")
common.Fail(c, err)
return
}
_ = retry.OnError(
retry.DefaultRetry,
func(err error) bool {
return errors.IsNotFound(err)
},
func() error {
_, getErr := karmadaClient.PolicyV1alpha1().PropagationPolicies(propagationpolicyRequest.Namespace).Get(ctx, propagationpolicyRequest.Name, metav1.GetOptions{})
return getErr
})
}

common.Success(c, "ok")
}

func init() {
r := router.V1()
r.GET("/clusterpropagationpolicy", handleGetClusterPropagationPolicyList)
Expand Down
6 changes: 2 additions & 4 deletions cmd/metrics-scraper/app/metricsscraper.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,8 @@ func run(ctx context.Context, opts *options.Options) error {
go scrape.InitDatabase()

config.InitDashboardConfig(client.InClusterClient(), ctx.Done())
select {
case <-ctx.Done():
os.Exit(0)
}
<-ctx.Done()
os.Exit(0)
return nil
}

Expand Down
4 changes: 3 additions & 1 deletion cmd/metrics-scraper/app/routes/metrics/handlerqueries.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,9 @@ func QueryMetrics(c *gin.Context) {
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to start transaction"})
return
}
defer tx.Rollback()
defer func() {
_ = tx.Rollback()
}()

switch queryType {
case "mname":
Expand Down
6 changes: 3 additions & 3 deletions cmd/metrics-scraper/app/scrape/other.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,14 @@ func saveToDBWithConnection(db *sql.DB, appName, podName string, data *db.Parsed
}
defer func() {
if p := recover(); p != nil {
tx.Rollback()
_ = tx.Rollback()
log.Printf("Recovered from panic: %v", p)
err = fmt.Errorf("panic occurred: %v", p)
} else if err != nil {
tx.Rollback()
_ = tx.Rollback()
log.Printf("Transaction rolled back due to error: %v", err)
} else {
tx.Commit()
_ = tx.Commit()
}
}()

Expand Down
4 changes: 1 addition & 3 deletions cmd/metrics-scraper/app/scrape/scrape.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,11 +143,9 @@ func HandleSyncOperation(c *gin.Context, appName string, syncValue int, queryTyp
}
contextMutex.Unlock()

message := "Sync trigger updated successfully for all apps"
message := "Sync turned off successfully for all apps"
if syncValue == 1 {
message = "Sync turned on successfully for all apps"
} else {
message = "Sync turned off successfully for all apps"
}
c.JSON(http.StatusOK, gin.H{"message": message})
} else {
Expand Down
11 changes: 6 additions & 5 deletions cmd/web/app/web.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,13 @@ func NewWebCommand(ctx context.Context) *cobra.Command {

func run(ctx context.Context, opts *options.Options) error {
klog.InfoS("Starting Karmada Dashboard API", "version", environment.Version)
config.InitDashboardConfigFromMountFile(opts.DashboardConfigPath)
serve(opts)
select {
case <-ctx.Done():
os.Exit(0)
err := config.InitDashboardConfigFromMountFile(opts.DashboardConfigPath)
if err != nil {
return err
}
serve(opts)
<-ctx.Done()
os.Exit(0)
return nil
}

Expand Down
2 changes: 1 addition & 1 deletion hack/verify-staticcheck.sh
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ else
curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(go env GOPATH)/bin ${GOLANGCI_LINT_VER}
fi

if golangci-lint run --enable-only goimports,gci,whitespace,misspell,staticcheck,govet; then
if golangci-lint run --enable-only goimports,gci,whitespace,misspell,staticcheck,govet,errcheck,gosimple,ineffassign,unused; then
echo 'Congratulations! All Go source files have passed staticcheck.'
else
echo # print one empty line, separate from warning messages.
Expand Down
6 changes: 5 additions & 1 deletion pkg/common/errors/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (

"github.com/emicklei/go-restful/v3"
k8sErrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/klog/v2"
)

// nonCriticalErrors is an array of error statuses, that are non-critical. That means, that this error can be
Expand Down Expand Up @@ -52,7 +53,10 @@ func HandleInternalError(response *restful.Response, err error) {
code, err := HandleError(err)

response.AddHeader("Content-Type", "text/plain")
response.WriteError(code, err)
err = response.WriteError(code, err)
if err != nil {
klog.Error(err)
}
}

// AppendError handles single error, that occurred during API GET call. If it is not critical, then it will be
Expand Down
10 changes: 5 additions & 5 deletions pkg/environment/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@ const (
)

var (
Version = dev // Version of this binary
gitVersion = "v0.0.0-master"
gitCommit = "unknown" // sha1 from git, output of $(git rev-parse HEAD)
gitTreeState = "unknown" // state of git tree, either "clean" or "dirty"
buildDate = "unknown" // build date in ISO8601 format, output of $(date -u +'%Y-%m-%dT%H:%M:%SZ')
Version = dev // Version of this binary
gitVersion = "v0.0.0-master" // nolint:unused
gitCommit = "unknown" // nolint:unused // sha1 from git, output of $(git rev-parse HEAD)
gitTreeState = "unknown" // nolint:unused // state of git tree, either "clean" or "dirty"
buildDate = "unknown" // nolint:unused // build date in ISO8601 format, output of $(date -u +'%Y-%m-%dT%H:%M:%SZ')
)

// IsDev returns true if the version is dev.
Expand Down