Skip to content

Commit

Permalink
actions: add golangci-lint action for sealer
Browse files Browse the repository at this point in the history
Signed-off-by: Allen Sun <[email protected]>
  • Loading branch information
allencloud committed Jun 11, 2022
1 parent 61c1a72 commit d0303da
Show file tree
Hide file tree
Showing 12 changed files with 86 additions and 55 deletions.
27 changes: 6 additions & 21 deletions .github/workflows/go.yml → .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Go
name: Binary Building on OS Distributions

on:
push:
Expand All @@ -13,24 +13,17 @@ on:
jobs:

build:
name: ubuntu - Go v1.17
name: build on ubuntu - Go v1.16
runs-on: ubuntu-latest

steps:

- name: Set up Go
uses: actions/setup-go@v2
- uses: actions/checkout@v2
with:
go-version: '1.17.3'
id: go

- name: Check out code into the Go module directory
uses: actions/checkout@v2
fetch-depth: 2
- uses: actions/setup-go@v2
with:
ref: ${{ github.ref }}
path: src/github.com/sealerio/sealer
go-version: '1.16'
- name: Check out code lic
working-directory: src/github.com/sealerio/sealer
run: |
wget https://github.com/google/addlicense/releases/download/v1.0.0/addlicense_1.0.0_Linux_x86_64.tar.gz
tar -zxvf addlicense_1.0.0_Linux_x86_64.tar.gz -C $(go env GOPATH)/bin
Expand All @@ -46,19 +39,11 @@ jobs:
exit -1
fi
- name: Install go ci lint
run: curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(go env GOPATH)/bin v1.39.0

- name: Run Linter
run: golangci-lint run -v
working-directory: src/github.com/sealerio/sealer

- name: Make linux
shell: bash
run: |
export MULTI_PLATFORM_BUILD=true
make build
working-directory: src/github.com/sealerio/sealer
- name: Save build binaries
uses: actions/upload-artifact@v2
Expand Down
48 changes: 48 additions & 0 deletions .github/workflows/golangci-lint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
name: golangci-lint
on:
push:
tags:
- v*
branches:
- main
pull_request:
permissions:
contents: read
# Optional: allow read access to pull request. Use with `only-new-issues` option.
# pull-requests: read
jobs:
golangci:
name: lint
runs-on: ubuntu-latest
steps:
- uses: actions/setup-go@v3
with:
go-version: 1.16
- uses: actions/checkout@v3
- name: golangci-lint
uses: golangci/golangci-lint-action@v3
with:
# Optional: version of golangci-lint to use in form of v1.2 or v1.2.3 or `latest` to use the latest version
version: v1.46
args: "--out-${NO_FUTURE}format colored-line-number"

args: --max-same-issues=20

# Optional: working directory, useful for monorepos
# working-directory: somedir

# Optional: golangci-lint command line arguments.
# args: --issues-exit-code=0

# Optional: show only new issues if it's a pull request. The default value is `false`.
# only-new-issues: true

# Optional: if set to true then the all caching functionality will be complete disabled,
# takes precedence over all other caching options.
# skip-cache: true

# Optional: if set to true then the action don't cache or restore ~/go/pkg.
# skip-pkg-cache: true

# Optional: if set to true then the action don't cache or restore ~/.cache/go-build.
# skip-build-cache: true
16 changes: 0 additions & 16 deletions .github/workflows/gosec.yml

This file was deleted.

File renamed without changes.
10 changes: 9 additions & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,14 @@ linters:
- unparam
- ifshort
- unconvert
- predeclared
- gosec
- forcetypeassert
- bodyclose
- containedctx
- exportloopref
- nosprintfhostport
- noctx

issues:
exclude-rules:
Expand All @@ -49,4 +57,4 @@ issues:
# https://github.com/golangci/golangci/wiki/Configuration
service:
# use the fixed version to not introduce new linters unexpectedly
golangci-lint-version: 1.39.0
golangci-lint-version: 1.43.0
2 changes: 1 addition & 1 deletion pkg/image/distributionutil/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func Login(ctx context.Context, authConfig *types.AuthConfig) error {
}

endpointStr := strings.TrimRight(endpointURL.String(), "/") + "/v2/"
req, err := http.NewRequest("GET", endpointStr, nil)
req, err := http.NewRequestWithContext(ctx, "GET", endpointStr, nil)
if err != nil {
return err
}
Expand Down
12 changes: 9 additions & 3 deletions pkg/image/save/distributionpkg/proxy/proxyauth.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,18 +76,21 @@ func getAuthURLs(remoteURL string) ([]string, error) {
authURLs := []string{}

resp, err := http.Get(remoteURL + "/v2/")
if err == nil {
defer resp.Body.Close()
}
if err != nil {
if strings.Contains(err.Error(), certUnknown) {
logrus.Warnf("create connect with unauthenticated registry url: %s", remoteURL)
resp, err = newClientSkipVerify().Get(remoteURL + "/v2/")
resp, err := newClientSkipVerify().Get(remoteURL + "/v2/")
if err != nil {
return nil, err
}
defer resp.Body.Close()
} else {
return nil, err
}
}
defer resp.Body.Close()

for _, c := range challenge.ResponseChallenges(resp) {
if strings.EqualFold(c.Scheme, "bearer") {
Expand All @@ -101,6 +104,10 @@ func getAuthURLs(remoteURL string) ([]string, error) {
// #nosec
func ping(manager challenge.Manager, endpoint string) error {
resp, err := http.Get(endpoint)
if err == nil {
defer resp.Body.Close()
}

if err != nil {
if strings.Contains(err.Error(), certUnknown) {
resp, err = newClientSkipVerify().Get(endpoint)
Expand All @@ -111,7 +118,6 @@ func ping(manager challenge.Manager, endpoint string) error {
return err
}
}
defer resp.Body.Close()

return manager.AddResponse(resp)
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/image/save/filesystem.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ func (d *driver) Reader(ctx context.Context, path string, offset int64) (io.Read
return file, nil
}

func (d *driver) Writer(ctx context.Context, subPath string, append bool) (storagedriver.FileWriter, error) {
func (d *driver) Writer(ctx context.Context, subPath string, appendable bool) (storagedriver.FileWriter, error) {
fullPath := d.fullPath(subPath)
parentDir := path.Dir(fullPath)
if err := os.MkdirAll(parentDir, 0750); err != nil {
Expand All @@ -208,7 +208,7 @@ func (d *driver) Writer(ctx context.Context, subPath string, append bool) (stora

var offset int64

if !append {
if !appendable {
err := fp.Truncate(0)
if err != nil {
return nil, fp.Close()
Expand Down
4 changes: 3 additions & 1 deletion pkg/plugin/etcd_backup_plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import (
"errors"
"fmt"
"io/ioutil"
"net"
"strconv"
"time"

clientv3 "go.etcd.io/etcd/client/v3"
Expand Down Expand Up @@ -108,7 +110,7 @@ func connEtcd(masterIP string) (clientv3.Config, error) {
RootCAs: pool,
}

endpoints := []string{fmt.Sprintf("https://%s:2379", masterIP)}
endpoints := []string{net.JoinHostPort(masterIP, strconv.Itoa(2379))}
cfg := clientv3.Config{
Endpoints: endpoints,
DialTimeout: dialTimeout,
Expand Down
4 changes: 2 additions & 2 deletions pkg/runtime/kubeadm_config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,7 @@ func TestKubeadmConfig_LoadFromClusterfile(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
k := tt.fields.KubeConfig
testfile := "test-Clusterfile"
err := ioutil.WriteFile(testfile, tt.args.kubeadmconfig, 0644)
err := ioutil.WriteFile(testfile, tt.args.kubeadmconfig, 0600)
if err != nil {
t.Errorf("WriteFile %s error = %v, wantErr %v", testfile, err, tt.wantErr)
}
Expand Down Expand Up @@ -410,7 +410,7 @@ func TestKubeadmConfig_Merge(t *testing.T) {
return
}*/
testfile := "test-kubeadm.yml"
err := ioutil.WriteFile(testfile, tt.args.defaultKubeadmConfig, 0644)
err := ioutil.WriteFile(testfile, tt.args.defaultKubeadmConfig, 0600)
if (err != nil) != tt.wantErr {
t.Errorf("WriteFile %s error = %v, wantErr %v", testfile, err, tt.wantErr)
return
Expand Down
6 changes: 2 additions & 4 deletions utils/os/readers.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,7 @@ type fileReader struct {
func (r fileReader) ReadLines() ([]string, error) {
var lines []string

_, err := os.Stat(r.fileName)
if err != nil || os.IsNotExist(err) {
if _, err := os.Stat(r.fileName); err != nil || os.IsNotExist(err) {
return nil, errors.New("no such file")
}

Expand All @@ -63,8 +62,7 @@ func (r fileReader) ReadLines() ([]string, error) {
}

func (r fileReader) ReadAll() ([]byte, error) {
_, err := os.Stat(r.fileName)
if err != nil || os.IsNotExist(err) {
if _, err := os.Stat(r.fileName); err != nil || os.IsNotExist(err) {
return nil, errors.New("no such file")
}

Expand Down
8 changes: 4 additions & 4 deletions utils/strings/strings.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,19 +181,19 @@ func ConvertToMap(env []string) map[string]string {
return envs
}

func Diff(old, new []string) (add, sub []string) {
func Diff(olds, news []string) (add, sub []string) {
diffMap := make(map[string]bool)
for _, v := range old {
for _, v := range olds {
diffMap[v] = true
}
for _, v := range new {
for _, v := range news {
if !diffMap[v] {
add = append(add, v)
} else {
diffMap[v] = false
}
}
for _, v := range old {
for _, v := range olds {
if diffMap[v] {
sub = append(sub, v)
}
Expand Down

0 comments on commit d0303da

Please sign in to comment.