Skip to content

Commit

Permalink
Add go linter for workflow and repo. (Azure#62)
Browse files Browse the repository at this point in the history
Add go linter for workflow and repo.
  • Loading branch information
Tatsinnit authored May 31, 2021
1 parent 8bd57bb commit e7b9fbf
Show file tree
Hide file tree
Showing 7 changed files with 1,041 additions and 10 deletions.
6 changes: 6 additions & 0 deletions .github/workflows/ci-pipeline.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@ jobs:
with:
go-version: 1.15

- name: Check golangci-lint
uses: golangci/golangci-lint-action@v2
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: latest

- name: Build project
run: go build -v ./cmd/aks-periscope/aks-periscope.go

Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ go 1.15
require (
github.com/Azure/azure-storage-blob-go v0.7.0
github.com/Azure/go-autorest/autorest/adal v0.9.13 // indirect
github.com/golangci/golangci-lint v1.40.1 // indirect
github.com/kr/pretty v0.2.1 // indirect
github.com/onsi/gomega v1.11.0
github.com/pkg/errors v0.8.1 // indirect
github.com/onsi/gomega v1.13.0
)
1,012 changes: 1,012 additions & 0 deletions go.sum

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion pkg/collector/networkoutbound_collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,10 @@ func (collector *NetworkOutboundCollector) Collect() error {
networkOutboundFile := filepath.Join(rootPath, outboundType.Type)

f, err := os.OpenFile(networkOutboundFile, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
defer f.Close()
if err != nil {
return fmt.Errorf("Fail to open file %s: %+v", networkOutboundFile, err)
}
defer f.Close()

timeout := time.Duration(5 * time.Second)
_, err = net.DialTimeout("tcp", outboundType.URL, timeout)
Expand Down
7 changes: 5 additions & 2 deletions pkg/diagnoser/networkconfig_diagnoser.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ func NewNetworkConfigDiagnoser(dnsCollector *collector.DNSCollector, kubeletCmdC
// Diagnose implements the interface method
func (diagnoser *NetworkConfigDiagnoser) Diagnose() error {
hostName, err := utils.GetHostName()
if err != nil {
return err
}
rootPath, err := utils.CreateDiagnosticDir()
if err != nil {
return err
Expand All @@ -55,18 +58,18 @@ func (diagnoser *NetworkConfigDiagnoser) Diagnose() error {
networkDiagnosticFile := filepath.Join(rootPath, diagnoser.GetName())

f, err := os.OpenFile(networkDiagnosticFile, os.O_CREATE|os.O_WRONLY, 0644)
defer f.Close()
if err != nil {
return fmt.Errorf("Fail to open file %s: %+v", networkDiagnosticFile, err)
}
defer f.Close()

networkConfigDiagnosticData := networkConfigDiagnosticDatum{HostName: hostName}
for _, file := range diagnoser.dnsCollector.GetCollectorFiles() {
t, err := os.Open(file)
defer t.Close()
if err != nil {
return fmt.Errorf("Fail to open file %s: %+v", file, err)
}
defer t.Close()

dnsLevel := filepath.Base(file)
var dns []string
Expand Down
14 changes: 11 additions & 3 deletions pkg/diagnoser/networkoutbound_diagnoser.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bufio"
"encoding/json"
"fmt"
"log"
"os"
"path/filepath"
"time"
Expand Down Expand Up @@ -43,6 +44,9 @@ func NewNetworkOutboundDiagnoser(networkOutboundCollector *collector.NetworkOutb
// Diagnose implements the interface method
func (diagnoser *NetworkOutboundDiagnoser) Diagnose() error {
hostName, err := utils.GetHostName()
if err != nil {
return err
}
rootPath, err := utils.CreateDiagnosticDir()
if err != nil {
return err
Expand All @@ -51,25 +55,29 @@ func (diagnoser *NetworkOutboundDiagnoser) Diagnose() error {
networkOutboundDiagnosticFile := filepath.Join(rootPath, diagnoser.GetName())

f, err := os.OpenFile(networkOutboundDiagnosticFile, os.O_CREATE|os.O_WRONLY, 0644)
defer f.Close()
if err != nil {
return fmt.Errorf("Fail to open file %s: %+v", networkOutboundDiagnosticFile, err)
}
defer f.Close()

outboundDiagnosticData := []networkOutboundDiagnosticDatum{}

for _, file := range diagnoser.networkOutboundCollector.GetCollectorFiles() {
t, err := os.Open(file)
defer t.Close()
if err != nil {
return fmt.Errorf("Fail to open file %s: %+v", file, err)
}
defer t.Close()

dataPoint := networkOutboundDiagnosticDatum{HostName: hostName}
scanner := bufio.NewScanner(t)
for scanner.Scan() {
var outboundDatum collector.NetworkOutboundDatum
json.Unmarshal([]byte(scanner.Text()), &outboundDatum)
err := json.Unmarshal([]byte(scanner.Text()), &outboundDatum)
if err != nil {
log.Printf("Unmarshal failed: %+v", err)
continue
}

if dataPoint.Start.IsZero() {
setDataPoint(&outboundDatum, &dataPoint)
Expand Down
6 changes: 4 additions & 2 deletions pkg/utils/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,10 @@ func RunCommandOnContainer(command string, arg ...string) (string, error) {
// WriteToFile writes data to a file
func WriteToFile(fileName string, data string) error {
f, err := os.Create(fileName)
defer f.Close()
if err != nil {
return fmt.Errorf("Fail to create file %s: %+v", fileName, err)
}
defer f.Close()

_, err = f.Write([]byte(data))
if err != nil {
Expand Down Expand Up @@ -206,7 +206,9 @@ func CreateCRD() error {

crdName := "aks-periscope-diagnostic" + "-" + hostName

writeDiagnosticCRD(crdName)
if err = writeDiagnosticCRD(crdName); err != nil {
return err
}

_, err = RunCommandOnContainer("kubectl", "apply", "-f", "aks-periscope-diagnostic-crd.yaml")
if err != nil {
Expand Down

0 comments on commit e7b9fbf

Please sign in to comment.