Skip to content

Commit 70ca5b0

Browse files
tiancandevlopertangxinxing
andauthored
Feature golangci lint (#39)
* add lint check * add action * update code * update code * update CodeQL Analysis * update workflow --------- Co-authored-by: tangxinxing <[email protected]>
1 parent 3a50afa commit 70ca5b0

File tree

9 files changed

+130
-11
lines changed

9 files changed

+130
-11
lines changed
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
# For most projects, this workflow file will not need changing; you simply need
2+
# to commit it to your repository.
3+
#
4+
# You may wish to alter this file to override the set of languages analyzed,
5+
# or to provide custom queries or build logic.
6+
#
7+
# ******** NOTE ********
8+
# We have attempted to detect the languages in your repository. Please check
9+
# the `language` matrix defined below to confirm you have the correct set of
10+
# supported CodeQL languages.
11+
#
12+
name: "CodeQL"
13+
14+
on:
15+
push:
16+
branches: [ main,release-* ]
17+
pull_request:
18+
# The branches below must be a subset of the branches above
19+
branches: [ main,release-* ]
20+
schedule:
21+
- cron: '27 21 * * 2'
22+
23+
jobs:
24+
analyze:
25+
name: Analyze
26+
runs-on: ubuntu-latest
27+
permissions:
28+
actions: read
29+
contents: read
30+
security-events: write
31+
32+
strategy:
33+
fail-fast: false
34+
matrix:
35+
language: [ 'go' ]
36+
# CodeQL supports [ 'cpp', 'csharp', 'go', 'java', 'javascript', 'python' ]
37+
# Learn more:
38+
# https://docs.github.com/en/free-pro-team@latest/github/finding-security-vulnerabilities-and-errors-in-your-code/configuring-code-scanning#changing-the-languages-that-are-analyzed
39+
40+
steps:
41+
- name: Checkout repository
42+
uses: actions/checkout@v2
43+
44+
# Initializes the CodeQL tools for scanning.
45+
- name: Initialize CodeQL
46+
uses: github/codeql-action/init@v2
47+
with:
48+
languages: ${{ matrix.language }}
49+
# If you wish to specify custom queries, you can do so here or in a config file.
50+
# By default, queries listed here will override any specified in a config file.
51+
# Prefix the list here with "+" to use these queries and those in the config file.
52+
# queries: ./path/to/local/query, your-org/your-repo/queries@main
53+
54+
# Autobuild attempts to build any compiled languages (C/C++, C#, or Java).
55+
# If this step fails, then you should remove it and run the build manually (see below)
56+
- name: Autobuild
57+
uses: github/codeql-action/autobuild@v2
58+
59+
# ℹ️ Command-line programs to run using the OS shell.
60+
# 📚 https://git.io/JvXDl
61+
62+
# ✏️ If the Autobuild fails above, remove it and uncomment the following three lines
63+
# and modify them (or add more) to build your code if your project
64+
# uses a compiled language
65+
66+
#- run: |
67+
# make bootstrap
68+
# make release
69+
70+
- name: Perform CodeQL Analysis
71+
uses: github/codeql-action/analyze@v2

.github/workflows/lint-ci.yml

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
name: Makefile CI
2+
3+
on:
4+
push:
5+
branches:
6+
- main
7+
- release-*
8+
tags:
9+
- v*
10+
pull_request:
11+
branches:
12+
- main
13+
- release-*
14+
15+
jobs:
16+
lint:
17+
runs-on: ubuntu-latest
18+
steps:
19+
- name: Set up Go 1.15
20+
uses: actions/setup-go@v2
21+
with:
22+
go-version: 1.15
23+
id: go
24+
- uses: actions/checkout@v2
25+
- name: Run golangci-lint
26+
run: make lint

Makefile

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,18 @@ docker-build-webconsole: #test ## Build docker image with the manager.
3535

3636
docker-build-webconsole-multi-arch: #test
3737
MULTI_ARCH=true
38-
docker buildx build -f ./Dockerfile -t ${IMG} --platform=linux/arm,linux/arm64,linux/amd64 . --push
38+
docker buildx build -f ./Dockerfile -t ${IMG} --platform=linux/arm,linux/arm64,linux/amd64 . --push
39+
40+
lint: golangci-lint ## Run golangci-lint
41+
$(GOLANGCI-LINT) run --timeout=10m
42+
43+
GOLANGCI-LINT = ./bin/golangci-lint
44+
golangci-lint: ## Download golangci-lint locally if necessary.
45+
$(call get-golangci-lint,$(GOLANGCI-LINT))
46+
47+
define get-golangci-lint
48+
@[ -f $(1) ] || { \
49+
set -e ;\
50+
curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s v1.45.2 ;\
51+
}
52+
endef

errdef/handler.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,12 @@ func HandleInternalError(response *restful.Response, err error) {
3434
statusCode = int(statusError.Status().Code)
3535
}
3636
response.AddHeader("Content-Type", "text/plain")
37-
response.WriteErrorString(statusCode, err.Error()+"\n")
37+
_ = response.WriteErrorString(statusCode, err.Error()+"\n")
3838
}
3939

4040
func HandleInternalErrorByCode(response *restful.Response, errCode ErrorInfo) {
4141
clog.Error("%v", errCode)
4242
response.AddHeader("Content-Type", "text/plain")
4343
msg, _ := json.Marshal(errCode)
44-
response.WriteErrorString(errCode.Code, string(msg))
44+
_ = response.WriteErrorString(errCode.Code, string(msg))
4545
}

handler/apihandler.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ func handleExecShell(request *restful.Request, response *restful.Response) {
115115
}
116116
cacheConnInfo(sessionId, cInfo)
117117

118-
response.WriteHeaderAndEntity(http.StatusOK, TerminalResponse{Id: sessionId})
118+
_ = response.WriteHeaderAndEntity(http.StatusOK, TerminalResponse{Id: sessionId})
119119
}
120120

121121
func cacheConnInfo(sessionId string, info *ConnInfo) {

handler/auth.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,13 +59,13 @@ func PodAuthorityVerify(request *restful.Request, response *restful.Response, ch
5959
// 2. determine whether the operated pod belongs to the namespace
6060
if !isAuthValid(request) {
6161
clog.Info("user has no permission to operate the pod or the pod does not belong to the namespace")
62-
response.WriteHeaderAndEntity(http.StatusUnauthorized, TerminalResponse{Message: "permission denied"})
62+
_ = response.WriteHeaderAndEntity(http.StatusUnauthorized, TerminalResponse{Message: "permission denied"})
6363
return
6464
}
6565
if isNsOrPodBelongToNamespace(request) {
6666
chain.ProcessFilter(request, response)
6767
} else {
68-
response.WriteHeaderAndEntity(http.StatusUnauthorized, TerminalResponse{Message: "the pod is not found"})
68+
_ = response.WriteHeaderAndEntity(http.StatusUnauthorized, TerminalResponse{Message: "the pod is not found"})
6969
}
7070
}
7171

@@ -122,8 +122,11 @@ func isNsOrPodBelongToNamespace(request *restful.Request) bool {
122122
clusterName := request.PathParameter("cluster")
123123
pivotClient := clients.Interface().Kubernetes(constants.LocalCluster)
124124
memberCluster := v1.Cluster{}
125-
pivotClient.Cache().Get(request.Request.Context(), types.NamespacedName{Name: clusterName}, &memberCluster)
126-
125+
err := pivotClient.Cache().Get(request.Request.Context(), types.NamespacedName{Name: clusterName}, &memberCluster)
126+
if err != nil {
127+
clog.Error("get cluster list error: %s", err)
128+
return false
129+
}
127130
config := memberCluster.Spec.KubeConfig
128131
kubeConfig, err := kubeconfig.LoadKubeConfigFromBytes(config)
129132
if err != nil {

handler/cloudShellHandler.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,11 @@ func handleCloudShellExec(request *restful.Request, response *restful.Response)
3838
// check cluster exists
3939
clusterName := request.PathParameter("cluster")
4040
clusterInfo, err := GetClusterInfoByName(clusterName)
41+
if err != nil {
42+
clog.Warn("get cluster failed. Error msg: " + err.Error())
43+
errdef.HandleInternalError(response, err)
44+
return
45+
}
4146
ctrlCluster, err := GetPivotCluster()
4247
if err != nil {
4348
clog.Error("get pivot cluster failed. Error msg: " + err.Error())
@@ -116,7 +121,7 @@ func handleCloudShellExec(request *restful.Request, response *restful.Response)
116121

117122
// save container-connect info to memory
118123
connMap.Store(sessionId, string(connInfoBytes))
119-
response.WriteHeaderAndEntity(http.StatusOK, TerminalResponse{Id: sessionId})
124+
_ = response.WriteHeaderAndEntity(http.StatusOK, TerminalResponse{Id: sessionId})
120125
}
121126

122127
func fetchRandomRunningPod(podArr []v12.Pod) *v12.Pod {

handler/cluster.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ func GetPivotCluster() (*clusterv1.Cluster, error) {
5757
}
5858

5959
for _, cluster := range list.Items {
60-
if cluster.Spec.IsMemberCluster == false {
60+
if !cluster.Spec.IsMemberCluster {
6161
pivotCluster = &cluster
6262
clog.Info("found pivot cluster %v", pivotCluster.Name)
6363
return pivotCluster, nil

utils/token.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ func GetTokenFromReq(request *restful.Request) string {
5050

5151
// parse bearer token
5252
parts := strings.Split(bearerToken, string(bearerToken[6]))
53-
if len(parts) < 2 || strings.ToLower(parts[0]) != strings.ToLower(bearerTokenPrefix) {
53+
if len(parts) < 2 || !strings.EqualFold(parts[0], bearerTokenPrefix) {
5454
return ""
5555
}
5656
return parts[1]

0 commit comments

Comments
 (0)