You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@trafficserver.apache.org by ki...@apache.org on 2022/06/07 22:45:37 UTC

[trafficserver-ingress-controller] branch master updated: upgrade go 1.17.11 (#123)

This is an automated email from the ASF dual-hosted git repository.

kichan pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/trafficserver-ingress-controller.git


The following commit(s) were added to refs/heads/master by this push:
     new c6004e5  upgrade go 1.17.11 (#123)
c6004e5 is described below

commit c6004e5c284b39582a1b9d00b50914368a335c79
Author: Kit Chan <ki...@apache.org>
AuthorDate: Tue Jun 7 15:45:33 2022 -0700

    upgrade go 1.17.11 (#123)
    
    * upgrade go 1.17.11
    
    * Add support for golangci-lint
    
    * Update yml for license
    
    * Add support for GO_VERSION
    
    * reorder steps in actions
    
    * fix go fmt issue
    
    * Add go vet github action
    
    * add go fmt
    
    * fix typo
    
    * test go fmt
    
    * Add typo and add go mod tidy
    
    * test go mod tidy
    
    * undo test
    
    * add tasks
    
    * add license text
---
 .github/workflows/go.fmt.yml        | 57 ++++++++++++++++++++++++++++++++
 .github/workflows/go.mod.yml        | 66 +++++++++++++++++++++++++++++++++++++
 .github/workflows/go.vet.yml        | 49 +++++++++++++++++++++++++++
 .github/workflows/golangci-lint.yml | 59 +++++++++++++++++++++++++++++++++
 .github/workflows/test.yml          | 12 ++++---
 .golangci.yaml                      | 42 +++++++++++++++++++++++
 .vimrc                              |  5 ---
 .vscode/tasks.json                  | 44 +++++++++++++++++++++++++
 Dockerfile                          |  6 ++--
 GO_VERSION                          |  1 +
 README.md                           |  2 +-
 docs/DEVELOPMENT.md                 | 11 +++++--
 main/main.go                        | 11 +++----
 util/util.go                        | 14 ++++----
 14 files changed, 350 insertions(+), 29 deletions(-)

diff --git a/.github/workflows/go.fmt.yml b/.github/workflows/go.fmt.yml
new file mode 100644
index 0000000..e87dc36
--- /dev/null
+++ b/.github/workflows/go.fmt.yml
@@ -0,0 +1,57 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+
+name: Go Fmt
+
+on:
+  push:
+    branches:
+    - master
+    paths-ignore:
+    - '.vscode/**'
+    - 'charts/**'
+    - 'docs/**'
+  pull_request:
+    branches:
+    - master
+    paths-ignore:
+    - '.vscode/**'
+    - 'charts/**'
+    - 'docs/**'
+
+jobs:
+  go-fmt:
+    runs-on: ubuntu-latest
+    steps:
+    - name: Checkout
+      uses: actions/checkout@master
+    - name: go-version
+      run: echo "::set-output name=value::$(cat GO_VERSION)"
+      id: go-version
+    - uses: actions/setup-go@v2
+      with:
+        go-version: ${{ steps.go-version.outputs.value }} # The Go version to download (if necessary) and use.
+    - name: go fmt
+      run: |
+        go fmt ./... > /tmp/go.fmt.temp
+        if [ -s /tmp/go.fmt.temp ]; then
+          printf "Files need to go through go fmt.\n";
+          cat /tmp/go.fmt.temp;
+          rm /tmp/go.fmt.temp;
+          exit 1;
+        fi
+
diff --git a/.github/workflows/go.mod.yml b/.github/workflows/go.mod.yml
new file mode 100644
index 0000000..a26fcce
--- /dev/null
+++ b/.github/workflows/go.mod.yml
@@ -0,0 +1,66 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+
+name: Go Mod
+
+on:
+  push:
+    branches:
+    - master
+    paths-ignore:
+    - '.vscode/**'
+    - 'charts/**'
+    - 'docs/**'
+  pull_request:
+    branches:
+    - master
+    paths-ignore:
+    - '.vscode/**'
+    - 'charts/**'
+    - 'docs/**'
+
+jobs:
+  go-mod:
+    runs-on: ubuntu-latest
+    steps:
+    - name: Checkout
+      uses: actions/checkout@master
+    - name: go-version
+      run: echo "::set-output name=value::$(cat GO_VERSION)"
+      id: go-version
+    - uses: actions/setup-go@v2
+      with:
+        go-version: ${{ steps.go-version.outputs.value }} # The Go version to download (if necessary) and use.
+    - name: go mod
+      run: |
+        go mod tidy
+    - name: check go.sum
+      run: |
+        if git diff --exit-code -- go.sum; then
+          printf "go.sum is up-to-date.\n"
+          exit 0
+        fi
+        printf "Changes found in go.sum. Please run go mod tidy\n"
+        exit 1
+    - name: check go.mod
+      run: |
+        if git diff --exit-code -- go.mod; then
+          printf "go.mod is up-to-date.\n"
+          exit 0
+        fi
+        printf "Changes found in go.mod. Please run go mod tidy\n"
+        exit 1
diff --git a/.github/workflows/go.vet.yml b/.github/workflows/go.vet.yml
new file mode 100644
index 0000000..3e2c0b1
--- /dev/null
+++ b/.github/workflows/go.vet.yml
@@ -0,0 +1,49 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+
+name: Go Vet
+
+on:
+  push:
+    branches:
+    - master
+    paths-ignore:
+    - '.vscode/**'
+    - 'charts/**'
+    - 'docs/**'
+  pull_request:
+    branches:
+    - master
+    paths-ignore:
+    - '.vscode/**'
+    - 'charts/**'
+    - 'docs/**'
+
+jobs:
+  go-vet:
+    runs-on: ubuntu-latest
+    steps:
+    - name: Checkout
+      uses: actions/checkout@master
+    - name: go-version
+      run: echo "::set-output name=value::$(cat GO_VERSION)"
+      id: go-version
+    - uses: actions/setup-go@v2
+      with:
+        go-version: ${{ steps.go-version.outputs.value }} # The Go version to download (if necessary) and use.
+    - name: go vet
+      run: go vet ./...
diff --git a/.github/workflows/golangci-lint.yml b/.github/workflows/golangci-lint.yml
new file mode 100644
index 0000000..8956597
--- /dev/null
+++ b/.github/workflows/golangci-lint.yml
@@ -0,0 +1,59 @@
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements.  See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You under the Apache License, Version 2.0
+# (the "License"); you may not use this file except in compliance with
+# the License.  You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+
+name: golangci-lint
+on:
+  push:
+    branches:
+    - master
+    paths-ignore:
+    - '.vscode/**'
+    - 'charts/**'
+    - 'docs/**'
+  pull_request:
+    branches:
+    - master
+    paths-ignore:
+    - '.vscode/**'
+    - 'charts/**'
+    - 'docs/**'
+
+permissions:
+  contents: read
+
+jobs:
+  golangci:
+    permissions:
+      contents: read  # for actions/checkout to fetch code
+      pull-requests: read  # for golangci/golangci-lint-action to fetch pull requests
+    name: lint
+    runs-on: ubuntu-latest
+
+    steps:
+      - name: Check out code into the Go module directory
+        uses: actions/checkout@v3
+      - name: go-version
+        run: echo "::set-output name=value::$(cat GO_VERSION)"
+        id: go-version
+      - uses: actions/setup-go@v3
+        with:
+          go-version: ${{ steps.go-version.outputs.value }}
+      - name: golangci-lint
+        uses: golangci/golangci-lint-action@v3.2.0
+        with:
+          version: v1.46.2
+          args: --timeout=10m
+
diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml
index 5ed693f..9491eab 100644
--- a/.github/workflows/test.yml
+++ b/.github/workflows/test.yml
@@ -23,14 +23,18 @@ jobs:
     runs-on: ubuntu-latest
     steps:
 
+    - name: Check out code into the Go module directory
+      uses: actions/checkout@v2
+
+    - name: go-version
+      run: echo "::set-output name=value::$(cat GO_VERSION)"
+      id: go-version
+
     - name: Set up Go 1.x
       uses: actions/setup-go@v2
       with:
-        go-version: ^1.17.10
+        go-version: ${{ steps.go-version.outputs.value }}
       id: go
-
-    - name: Check out code into the Go module directory
-      uses: actions/checkout@v2
    
     # commit for v8.0.0 
     - uses: leafo/gh-actions-lua@ea0ae38722c0b45aa4e770f7c4a650c6b26800d0
diff --git a/.golangci.yaml b/.golangci.yaml
new file mode 100644
index 0000000..17b9f00
--- /dev/null
+++ b/.golangci.yaml
@@ -0,0 +1,42 @@
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements.  See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You under the Apache License, Version 2.0
+# (the "License"); you may not use this file except in compliance with
+# the License.  You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+
+# https://golangci-lint.run/usage/linters/
+linters:
+  disable-all: true
+  enable:
+    - deadcode
+    - errcheck
+    - gosimple
+    - govet
+    - ineffassign
+    - staticcheck
+    - structcheck
+    - typecheck
+    - unused
+    - varcheck
+
+# Options for analysis running.
+run:
+  concurrency: 4
+  timeout: 5m
+  tests: false
+
+  output:
+    format: colored-line-number
+    print-issued-lines: true
+    print-linter-name: true
+
diff --git a/.vimrc b/.vimrc
deleted file mode 100644
index 515ebef..0000000
--- a/.vimrc
+++ /dev/null
@@ -1,5 +0,0 @@
-" Basic whitespace rules.
-set tabstop=4
-set softtabstop=4
-set shiftwidth=4
-set expandtab
diff --git a/.vscode/tasks.json b/.vscode/tasks.json
new file mode 100644
index 0000000..434309b
--- /dev/null
+++ b/.vscode/tasks.json
@@ -0,0 +1,44 @@
+//   Licensed under the Apache License, Version 2.0 (the "License");
+//   you may not use this file except in compliance with the License.
+//   You may obtain a copy of the License at
+//   http://www.apache.org/licenses/LICENSE-2.0
+//   Unless required by applicable law or agreed to in writing, software
+//   distributed under the License is distributed on an "AS IS" BASIS,
+//   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+//   See the License for the specific language governing permissions and
+//   limitations under the License.
+{
+    "version": "2.0.0",
+    "tasks": [
+        {
+            "label": "golangci-lint",
+            "type": "shell",
+            "command": "golangci-lint run"
+        },
+        {
+            "label": "go mod tidy",
+            "type": "shell",
+            "command": "go mod tidy"
+        },
+        {
+            "label": "go vet",
+            "type": "shell",
+            "command": "go vet ./..."
+        },
+        {
+            "label": "go fmt",
+            "type": "shell",
+            "command": "go fmt ./..."
+        },
+        {
+            "label": "Build",
+            "type": "shell",
+            "command": "go build -o ingress-ats main/main.go"
+        },
+        {
+            "label": "Test",
+            "type": "shell",
+            "command": "go test ./watcher/ && go test ./redis/"
+        }
+     ]
+}
diff --git a/Dockerfile b/Dockerfile
index bd11377..d67e37c 100644
--- a/Dockerfile
+++ b/Dockerfile
@@ -70,8 +70,10 @@ RUN apk add --no-cache --virtual .ingress-build-deps \
   bash gcc musl-dev openssl go
 
 # Installing Golang https://github.com/CentOS/CentOS-Dockerfiles/blob/master/golang/centos7/Dockerfile
-RUN wget https://dl.google.com/go/go1.17.10.src.tar.gz \
-    && tar -C /opt/ats -xzf go1.17.10.src.tar.gz && cd /opt/ats/go/src/ && ./make.bash
+COPY GO_VERSION /
+RUN go_version=$(cat /GO_VERSION) \
+    && wget https://dl.google.com/go/go${go_version}.src.tar.gz \
+    && tar -C /opt/ats -xzf go${go_version}.src.tar.gz && cd /opt/ats/go/src/ && ./make.bash
 ENV PATH=${PATH}:/opt/ats/go/bin
 ENV GOPATH="/opt/ats/go/bin"
 
diff --git a/GO_VERSION b/GO_VERSION
new file mode 100644
index 0000000..b8f1e3f
--- /dev/null
+++ b/GO_VERSION
@@ -0,0 +1 @@
+1.17.11
diff --git a/README.md b/README.md
index 9e99e7f..c4f37e6 100644
--- a/README.md
+++ b/README.md
@@ -35,7 +35,7 @@ Card](https://goreportcard.com/badge/github.com/apache/trafficserver-ingress-con
 - Alpine Linux 3.14.6
 - Apache Traffic Server 9.1.2
 - LuaJIT 2.0.4
-- Go 1.17.10
+- Go (Version can be found in `GO_VERSION` file found at the base of this repository)
 - Other Packages
   - luasocket 3.0.0
   - redis-lua 2.0.4
diff --git a/docs/DEVELOPMENT.md b/docs/DEVELOPMENT.md
index 8397656..cd2dfcf 100644
--- a/docs/DEVELOPMENT.md
+++ b/docs/DEVELOPMENT.md
@@ -20,7 +20,7 @@
 ## Development
 
 ### Develop with Go-Lang in Linux
-1. Get Go-lang 1.17.10 from [official site](https://golang.org/dl/)
+1. Get Go-lang from [official site](https://golang.org/dl/). Check out the version used in `GO_VERSION` file at the base of this repository.
 2. Add `go` command to your PATH: `export PATH=$PATH:/usr/local/go/bin`
 3. Define GOPATH: `export GOPATH=$(go env GOPATH)`
 4. Add Go workspace to your PATH: `export PATH=$PATH:$(go env GOPATH)/bin`
@@ -31,6 +31,12 @@
    - `cd $GOPATH/src/github.com/apache/<project>`
 7. Define GO111MODULE: `export GO111MODULE=on` to be able to compile locally. 
 
+### GO code guidelines
+1. Run `golangci-lint run`
+2. Run `go mod tidy`
+3. Run `go fmt ./...`
+4. Run `go vet ./...`
+
 ### Compilation
 To compile, type: `go build -o ingress-ats main/main.go`
 
@@ -47,11 +53,10 @@ To run the Lua unit tests:
 - `busted connect_redis_test.lua` 
 
 ### Text-Editor
-The repository comes with basic support for both [vscode](https://code.visualstudio.com/) and `vim`. 
+The repository comes with basic support for both [vscode](https://code.visualstudio.com/). 
 
 If you're using `vscode`:
 - `.vscode/settings.json` contains some basic settings for whitespaces and tabs
 - `.vscode/extensions.json` contains a few recommended extensions for this project.
 - It is highly recommended to install the Go extension since it contains the code lint this project used during development.
 
-If you're using `vim`, a `vimrc` file with basic whitespace and tab configurations is also provided
diff --git a/main/main.go b/main/main.go
index a74368c..e636c00 100644
--- a/main/main.go
+++ b/main/main.go
@@ -161,12 +161,9 @@ func main() {
 	/* Program termination */
 	signalChan := make(chan os.Signal, 1)
 	signal.Notify(signalChan, syscall.SIGINT, syscall.SIGTERM)
-	for {
-		select {
-		case <-signalChan:
-			log.Println("Shutdown signal received, exiting...")
-			close(stopChan)
-			os.Exit(0)
-		}
+	for range signalChan {
+		log.Println("Shutdown signal received, exiting...")
+		close(stopChan)
+		os.Exit(0)
 	}
 }
diff --git a/util/util.go b/util/util.go
index d0f6bad..55ccce3 100644
--- a/util/util.go
+++ b/util/util.go
@@ -70,7 +70,10 @@ func (w *Writer) SyncWriteJSONFile(obj interface{}) error {
 	if writeErr != nil {
 		return writeErr
 	}
-	file.Sync() // flushing to disk
+	syncErr := file.Sync() // flushing to disk
+	if syncErr != nil {
+		return syncErr
+	}
 	return nil
 }
 
@@ -157,11 +160,11 @@ func ExtractIngressClassName(obj interface{}) (class string, err error) {
 	ingressObj, ok := obj.(*nv1.Ingress)
 	if !ok {
 		log.Println("Extracting ingress class name; cannot cast to *nv1.Ingress")
-		return "", fmt.Errorf("Extracting ingress class name; cannot cast to *nv1.Ingress")
+		return "", fmt.Errorf("extracting ingress class name; cannot cast to *nv1.Ingress")
 	}
 
 	if ingressObj.Spec.IngressClassName == nil {
-		return "", fmt.Errorf("Extracting ingress class name; missing the field")
+		return "", fmt.Errorf("extracting ingress class name; missing the field")
 	}
 
 	return *ingressObj.Spec.IngressClassName, nil
@@ -228,8 +231,5 @@ func IsSameSlice(x, y []string) bool {
 			delete(diff, _y)
 		}
 	}
-	if len(diff) == 0 {
-		return true
-	}
-	return false
+	return len(diff) == 0
 }