Skip to content

Commit

Permalink
Add a workflow that checks lint, build and go mod. On every repo push
Browse files Browse the repository at this point in the history
and pull requestas to main and release branches.
  • Loading branch information
caliskanugur committed Mar 25, 2024
1 parent 060a406 commit b5a91c3
Show file tree
Hide file tree
Showing 11 changed files with 155 additions and 58 deletions.
13 changes: 13 additions & 0 deletions .github/scripts/build-packages.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/bin/bash
set -e

oldPWD="$(pwd)"

dirs=("./clients" "./extensions" "./pkg")

for dir in "${dirs[@]}"; do
echo "Building $dir"
cd "$dir"
go build ./...
cd "$oldPWD"
done
12 changes: 12 additions & 0 deletions .github/scripts/check-go-mod.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/bin/bash
set -e

go mod tidy
go mod verify

if [ -n "$(git status --porcelain --untracked-files=no)" ]; then
echo "Go mod isn't up to date. Please run go mod tidy."
echo "The following go files did differ after tidying them:"
git status --porcelain
exit 1
fi
45 changes: 45 additions & 0 deletions .github/workflows/verify-changes.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
name: Verify Changes

on:
push:
pull_request:
branches:
- 'release/*'
- 'main'

jobs:
verify-changes:
name: verify-changes
runs-on: ubuntu-latest
timeout-minutes: 20
steps:
- name: Checkout
uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4.1.1
with:
submodules: recursive

- name: Setup Go
uses: actions/setup-go@0c52d547c9bc32b1aa3301fd7a9cb496313a4491 # v5.0.0
with:
go-version-file: './go.mod'

- name: Go Version
run: go version

- name: Generate Golang
run: |
export PATH=$PATH:/home/runner/go/bin/
- name: Verify Go Mod
run: ./.github/scripts/check-go-mod.sh

- name: Build Packages
run: ./.github/scripts/build-packages.sh

- name: Golangci Lint
uses: golangci/golangci-lint-action@3cfe3a4abbb849e10058ce4af15d205b6da42804 # v4.0.0
with:
# Patch version isn't needed; https://github.com/golangci/golangci-lint-action?tab=readme-ov-file#internals
version: v1.57
# Skip cache cause: https://github.com/golangci/golangci-lint-action/issues/135
skip-cache: true
40 changes: 40 additions & 0 deletions .golangci.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
linters:
disable-all: true
enable:
- ineffassign
- goimports
- govet
- misspell
- revive
- unused
- goconst
- forbidigo
linters-settings:
revive:
rules:
- name: confusing-naming
disabled: true
- name: exported
disabled: false
goconst:
min-len: 2
min-occurrences: 10
match-constant: true
ignore-strings: "metadata.+"
forbidigo:
forbid:
- p: ^*\.Sleep.*$
msg: "No sleeps please use the appropriate polls and watch"
- p: ^fmt\.Print.*$
msg: "No format prints, please testing or logrus packages"
run:
tests: false
timeout: 10m
issues:
exclude-dirs:
- pkg/generated/*
- clients/rancher/generated/*
exclude-files:
- ^*\.yaml$
- ^*\.yml$
exclude-generated-strict: true
3 changes: 1 addition & 2 deletions clients/k3d/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,5 @@ const ConfigurationFileKey = "k3d"

// Config is configuration needed to create k3d cluster for integration testing.
type Config struct {
image string `yaml:"image" default:"rancher/k3s:v1.21.3-k3s1"`
createTimeout int `yaml:"createTimeout" default:"120s"`
createTimeout int `yaml:"createTimeout" default:"120s"`
}
2 changes: 1 addition & 1 deletion clients/tfexec/tfexec.go
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ func (c *Client) Output(opts ...tfexec.OutputOption) ([]map[string]any, error) {
}

// WorkingDir simply returns the configured terraform working directory for terraform
// The WorkingDir is the directory that terraform is targetting when it runs a command
// The WorkingDir is the directory that terraform is targeting when it runs a command
func (c *Client) WorkingDir() string {
return c.Terraform.WorkingDir()
}
Expand Down
6 changes: 1 addition & 5 deletions extensions/kubectl/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,7 @@ const (
JobName = "kubectl"
)

var (
importTimeout = int64(60 * 1)
group int64
user int64
)
var importTimeout = int64(60 * 1)

// CreateJobAndRunKubectlCommands is a helper to create a job and run the kubectl commands in the pods of the Job.
// It then returns errors or nil from the job.
Expand Down
10 changes: 0 additions & 10 deletions extensions/rke1/nodetemplates/nodetemplates.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,3 @@ type NodeTemplate struct {
UUID string `json:"uuid,omitempty" yaml:"uuid,omitempty"`
UseInternalIPAddress *bool `json:"useInternalIpAddress,omitempty" yaml:"useInternalIpAddress,omitempty"`
}

func providerTemplateConfigKeys() []string {
return []string{
AmazonEC2NodeTemplateConfigurationFileKey,
AzureNodeTemplateConfigurationFileKey,
HarvesterNodeTemplateConfigurationFileKey,
LinodeNodeTemplateConfigurationFileKey,
VmwareVsphereNodeTemplateConfigurationFileKey,
}
}
23 changes: 12 additions & 11 deletions pkg/clientbase/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import (
"os"
"time"

"github.com/sirupsen/logrus"

"github.com/gorilla/websocket"
"github.com/pkg/errors"
"github.com/rancher/norman/types"
Expand All @@ -23,9 +25,7 @@ const (
COLLECTION = "collection"
)

var (
Debug = false
)
var Debug = false

type APIBaseClientInterface interface {
Websocket(url string, headers map[string][]string) (*websocket.Conn, *http.Response, error)
Expand Down Expand Up @@ -185,7 +185,7 @@ func NewAPIClient(opts *ClientOpts) (APIBaseClient, error) {

if opts.CACerts != "" {
if Debug {
fmt.Println("Some CAcerts are provided.")
logrus.Infoln("Some CAcerts are provided.")
}
roots := x509.NewCertPool()
ok := roots.AppendCertsFromPEM([]byte(opts.CACerts))
Expand All @@ -203,7 +203,7 @@ func NewAPIClient(opts *ClientOpts) (APIBaseClient, error) {

if opts.Insecure {
if Debug {
fmt.Println("Insecure TLS set.")
logrus.Infoln("Insecure TLS set.")
}
tr := &http.Transport{
TLSClientConfig: &tls.Config{
Expand All @@ -216,7 +216,7 @@ func NewAPIClient(opts *ClientOpts) (APIBaseClient, error) {

if !(opts.Insecure) && (opts.CACerts == "") {
if Debug {
fmt.Println("Insecure TLS not set and no CAcerts is provided.")
logrus.Infoln("Insecure TLS not set and no CAcerts is provided.")
}
tr := &http.Transport{
Proxy: http.ProxyFromEnvironment,
Expand Down Expand Up @@ -255,7 +255,7 @@ func NewAPIClient(opts *ClientOpts) (APIBaseClient, error) {
req.Header.Add("Authorization", opts.getAuthHeader())

if Debug {
fmt.Println("GET " + req.URL.String())
logrus.Infoln("GET " + req.URL.String())
}

resp, err = client.Do(req)
Expand All @@ -278,7 +278,7 @@ func NewAPIClient(opts *ClientOpts) (APIBaseClient, error) {
}

if Debug {
fmt.Println("Response <= " + string(bytes))
logrus.Infoln("Response <= " + string(bytes))
}

err = json.Unmarshal(bytes, &schemas)
Expand Down Expand Up @@ -327,7 +327,7 @@ func (a *APIBaseClient) Websocket(url string, headers map[string][]string) (*web
}

if Debug {
fmt.Println("WS " + url)
logrus.Infoln("WS " + url)
}

return a.Ops.Dialer.Dial(url, http.Header(httpHeaders))
Expand Down Expand Up @@ -383,13 +383,14 @@ func (a *APIBaseClient) Reload(existing *types.Resource, output interface{}) err
}

func (a *APIBaseClient) Action(schemaType string, action string,
existing *types.Resource, inputObject, respObject interface{}) error {
existing *types.Resource, inputObject, respObject interface{},
) error {
return a.Ops.DoAction(schemaType, action, existing, inputObject, respObject)
}

func init() {
Debug = os.Getenv("RANCHER_CLIENT_DEBUG") == "true"
if Debug {
fmt.Println("Rancher client debug on")
logrus.Infoln("Rancher client debug on")
}
}
25 changes: 13 additions & 12 deletions pkg/clientbase/ops.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/pkg/errors"
"github.com/rancher/norman/types"
"github.com/rancher/shepherd/pkg/session"
"github.com/sirupsen/logrus"
)

type APIOperations struct {
Expand Down Expand Up @@ -63,7 +64,7 @@ func (a *APIOperations) DoGet(url string, opts *types.ListOpts, respObject inter
}

if Debug {
fmt.Println("GET " + url)
logrus.Infoln("GET " + url)
}

req, err := http.NewRequest("GET", url, nil)
Expand All @@ -90,7 +91,7 @@ func (a *APIOperations) DoGet(url string, opts *types.ListOpts, respObject inter
}

if Debug {
fmt.Println("Response <= " + string(byteContent))
logrus.Infoln("Response <= " + string(byteContent))
}

if err := json.Unmarshal(byteContent, respObject); err != nil {
Expand Down Expand Up @@ -142,8 +143,8 @@ func (a *APIOperations) DoModify(method string, url string, createObj interface{
}

if Debug {
fmt.Println(method + " " + url)
fmt.Println("Request => " + string(bodyContent))
logrus.Infoln(method + " " + url)
logrus.Infoln("Request => " + string(bodyContent))
}

req, err := http.NewRequest(method, url, bytes.NewBuffer(bodyContent))
Expand Down Expand Up @@ -172,7 +173,7 @@ func (a *APIOperations) DoModify(method string, url string, createObj interface{

if len(byteContent) > 0 {
if Debug {
fmt.Println("Response <= " + string(byteContent))
logrus.Infoln("Response <= " + string(byteContent))
}
return json.Unmarshal(byteContent, respObject)
}
Expand Down Expand Up @@ -329,8 +330,8 @@ func (a *APIOperations) DoResourceDelete(schemaType string, existing *types.Reso
}

func (a *APIOperations) DoAction(schemaType string, action string,
existing *types.Resource, inputObject, respObject interface{}) error {

existing *types.Resource, inputObject, respObject interface{},
) error {
if existing == nil {
return errors.New("Existing object is nil")
}
Expand All @@ -344,8 +345,8 @@ func (a *APIOperations) DoAction(schemaType string, action string,
}

func (a *APIOperations) DoCollectionAction(schemaType string, action string,
existing *types.Collection, inputObject, respObject interface{}) error {

existing *types.Collection, inputObject, respObject interface{},
) error {
if existing == nil {
return errors.New("Existing object is nil")
}
Expand Down Expand Up @@ -373,7 +374,7 @@ func (a *APIOperations) doAction(
var input io.Reader

if Debug {
fmt.Println("POST " + actionURL)
logrus.Infoln("POST " + actionURL)
}

if inputObject != nil {
Expand All @@ -382,7 +383,7 @@ func (a *APIOperations) doAction(
return err
}
if Debug {
fmt.Println("Request => " + string(bodyContent))
logrus.Infoln("Request => " + string(bodyContent))
}
input = bytes.NewBuffer(bodyContent)
}
Expand Down Expand Up @@ -413,7 +414,7 @@ func (a *APIOperations) doAction(
}

if Debug {
fmt.Println("Response <= " + string(byteContent))
logrus.Infoln("Response <= " + string(byteContent))
}

if nil != respObject {
Expand Down
Loading

0 comments on commit b5a91c3

Please sign in to comment.