Skip to content

Commit

Permalink
Enable linter for hack dir and fix all issues (#1726)
Browse files Browse the repository at this point in the history
* Enable linter for hack dir, fix all issues

* Flip 'if' to improve readability
  • Loading branch information
david-kow authored Sep 16, 2019
1 parent e617a9e commit 705978b
Show file tree
Hide file tree
Showing 8 changed files with 62 additions and 61 deletions.
1 change: 0 additions & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ run:
deadline: 300s
skip-dirs:
- config
- hack
skip-files:
- utils/chrono/millis\.test\.go
linters:
Expand Down
2 changes: 1 addition & 1 deletion hack/deployer/cmd/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ overrides:
gCloudProject: %s
`

func CreateCommand() *cobra.Command{
func CreateCommand() *cobra.Command {
var path string

var createCommand = &cobra.Command{
Expand Down
2 changes: 1 addition & 1 deletion hack/deployer/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func Execute() {
}
}

func registerFileFlags(cmd *cobra.Command) (*string, *string){
func registerFileFlags(cmd *cobra.Command) (*string, *string) {
var plansFile, configFile string
cmd.PersistentFlags().StringVar(&plansFile, "plans-file", "config/plans.yml", "File containing execution plans.")
cmd.PersistentFlags().StringVar(&configFile, "config-file", "config/deployer-config.yml", "File containing run config.")
Expand Down
58 changes: 30 additions & 28 deletions hack/deployer/runner/aks.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ import (
)

func init() {
drivers[AksDriverId] = &AksDriverFactory{}
drivers[AksDriverID] = &AksDriverFactory{}
}

const (
AksDriverId = "aks"
AksDriverID = "aks"
AksVaultPath = "secret/devops-ci/cloud-on-k8s/ci-azr-k8s-operator"
AksResourceGroupVaultFieldName = "resource-group"
AksAcrNameVaultFieldName = "acr-name"
Expand Down Expand Up @@ -78,7 +78,9 @@ func (d *AksDriver) Execute() error {
switch d.plan.Operation {
case "delete":
if exists {
err = d.delete()
if err := d.delete(); err != nil {
return err
}
} else {
log.Printf("not deleting as cluster doesn't exist")
}
Expand All @@ -99,7 +101,7 @@ func (d *AksDriver) Execute() error {
return err
}
default:
err = fmt.Errorf("unknown operation %s", d.plan.Operation)
return fmt.Errorf("unknown operation %s", d.plan.Operation)
}

return nil
Expand All @@ -113,21 +115,21 @@ func (d *AksDriver) auth() error {
if err != nil {
return err
}
appId, tenantSecret, tenantId := secrets[0], secrets[1], secrets[2]
appID, tenantSecret, tenantID := secrets[0], secrets[1], secrets[2]

cmd := "az login --service-principal -u {{.AppId}} -p {{.TenantSecret}} --tenant {{.TenantId}}"
return NewCommand(cmd).
AsTemplate(map[string]interface{}{
"AppId": appId,
"AppId": appID,
"TenantSecret": tenantSecret,
"TenantId": tenantId,
"TenantId": tenantID,
}).
WithoutStreaming().
Run()
} else {
log.Print("Authenticating as user...")
return NewCommand("az login").Run()
}

log.Print("Authenticating as user...")
return NewCommand("az login").Run()
}

func (d *AksDriver) clusterExists() (bool, error) {
Expand Down Expand Up @@ -172,29 +174,29 @@ func (d *AksDriver) configureDocker() error {
return err
}

if !d.plan.ServiceAccount {
if d.plan.ServiceAccount {
// it's already set for the ServiceAccount
cmd := `az aks show --resource-group {{.ResourceGroup}} --name {{.ClusterName}} --query "servicePrincipalProfile.clientId" --output tsv`
clientIds, err := NewCommand(cmd).AsTemplate(d.ctx).StdoutOnly().OutputList()
if err != nil {
return err
}
return nil
}

cmd = `az acr show --resource-group {{.ResourceGroup}} --name {{.AcrName}} --query "id" --output tsv`
acrIds, err := NewCommand(cmd).AsTemplate(d.ctx).StdoutOnly().OutputList()
if err != nil {
return err
}
cmd := `az aks show --resource-group {{.ResourceGroup}} --name {{.ClusterName}} --query "servicePrincipalProfile.clientId" --output tsv`
clientIds, err := NewCommand(cmd).AsTemplate(d.ctx).StdoutOnly().OutputList()
if err != nil {
return err
}

return NewCommand(`az role assignment create --assignee {{.ClientId}} --role acrpull --scope {{.AcrId}}`).
AsTemplate(map[string]interface{}{
"ClientId": clientIds[0],
"AcrId": acrIds[0],
}).
Run()
cmd = `az acr show --resource-group {{.ResourceGroup}} --name {{.AcrName}} --query "id" --output tsv`
acrIds, err := NewCommand(cmd).AsTemplate(d.ctx).StdoutOnly().OutputList()
if err != nil {
return err
}

return nil
return NewCommand(`az role assignment create --assignee {{.ClientId}} --role acrpull --scope {{.AcrId}}`).
AsTemplate(map[string]interface{}{
"ClientId": clientIds[0],
"AcrId": acrIds[0],
}).
Run()
}

func (d *AksDriver) GetCredentials() error {
Expand Down
28 changes: 14 additions & 14 deletions hack/deployer/runner/gke.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ import (
)

const (
GkeDriverId = "gke"
GkeDriverID = "gke"
GkeVaultPath = "secret/devops-ci/cloud-on-k8s/ci-gcp-k8s-operator"
GkeServiceAccountVaultFieldName = "service-account"
)

func init() {
drivers[GkeDriverId] = &GkeDriverFactory{}
drivers[GkeDriverID] = &GkeDriverFactory{}
}

type GkeDriverFactory struct {
Expand Down Expand Up @@ -67,7 +67,7 @@ func (d *GkeDriver) Execute() error {
if exists {
log.Printf("not creating as cluster exists")
} else {
if err := d.configSsh(); err != nil {
if err := d.configSSH(); err != nil {
return err
}

Expand Down Expand Up @@ -120,19 +120,19 @@ func (d *GkeDriver) auth() error {
}

return NewCommand("gcloud auth activate-service-account --key-file=" + keyFileName).Run()
} else {
log.Println("Authenticating as user...")
accounts, err := NewCommand(`gcloud auth list "--format=value(account)"`).StdoutOnly().WithoutStreaming().Output()
if err != nil {
return err
}
}

if len(accounts) > 0 {
return nil
}
log.Println("Authenticating as user...")
accounts, err := NewCommand(`gcloud auth list "--format=value(account)"`).StdoutOnly().WithoutStreaming().Output()
if err != nil {
return err
}

return NewCommand("gcloud auth login").Run()
if len(accounts) > 0 {
return nil
}

return NewCommand("gcloud auth login").Run()
}

func (d *GkeDriver) clusterExists() (bool, error) {
Expand All @@ -147,7 +147,7 @@ func (d *GkeDriver) clusterExists() (bool, error) {
return err == nil, err
}

func (d *GkeDriver) configSsh() error {
func (d *GkeDriver) configSSH() error {
log.Println("Configuring ssh...")
return NewCommand("gcloud --quiet --project {{.GCloudProject}} compute config-ssh").AsTemplate(d.ctx).Run()
}
Expand Down
10 changes: 5 additions & 5 deletions hack/deployer/runner/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ type Plans struct {

// Plan encapsulates information needed to provision a cluster
type Plan struct {
Id string `yaml:"id"`
Id string `yaml:"id"` //nolint
Operation string `yaml:"operation"`
ClusterName string `yaml:"clusterName"`
Provider string `yaml:"provider"`
Expand All @@ -26,7 +26,7 @@ type Plan struct {
ServiceAccount bool `yaml:"serviceAccount"`

Psp bool `yaml:"psp"`
VmMapMax bool `yaml:"vmMapMax"`
VmMapMax bool `yaml:"vmMapMax"` //nolint

Gke *GkeSettings `yaml:"gke,omitempty"`
Aks *AksSettings `yaml:"aks,omitempty"`
Expand All @@ -36,8 +36,8 @@ type Plan struct {

type VaultInfo struct {
Address string `yaml:"address"`
RoleId string `yaml:"roleId"`
SecretId string `yaml:"secretId"`
RoleId string `yaml:"roleId"` //nolint
SecretId string `yaml:"secretId"` //nolint
Token string `yaml:"token"`
}

Expand All @@ -60,7 +60,7 @@ type AksSettings struct {

// RunConfig encapsulates Id used to choose a plan and a map of overrides to apply to the plan, expected to map to a file
type RunConfig struct {
Id string `yaml:"id"`
Id string `yaml:"id"` //nolint
Overrides map[string]interface{} `yaml:"overrides"`
}

Expand Down
17 changes: 9 additions & 8 deletions hack/deployer/runner/vault.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ import (

type VaultClient struct {
client *api.Client
roleId string
secretId string
roleID string
secretID string
token string
}

Expand All @@ -26,8 +26,8 @@ func NewClient(info VaultInfo) (*VaultClient, error) {

return &VaultClient{
client: client,
roleId: info.RoleId,
secretId: info.SecretId,
roleID: info.RoleId,
secretID: info.SecretId,
token: info.Token,
}, nil
}
Expand All @@ -41,13 +41,14 @@ func (v *VaultClient) auth() error {
var data map[string]interface{}
var method string

if v.token != "" {
switch {
case v.token != "":
method = "github"
data = map[string]interface{}{"token": v.token}
} else if v.roleId != "" && v.secretId != "" {
case v.roleID != "" && v.secretID != "":
method = "approle"
data = map[string]interface{}{"role_id": v.roleId, "secret_id": v.secretId}
} else {
data = map[string]interface{}{"role_id": v.roleID, "secret_id": v.secretID}
default:
return fmt.Errorf("vault auth info not present")
}

Expand Down
5 changes: 2 additions & 3 deletions hack/release_notes.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ package main

import (
"encoding/json"
"errors"
"fmt"
"html/template"
"io"
Expand Down Expand Up @@ -96,7 +95,7 @@ type TemplateParams struct {
}

func fetch(url string, out interface{}) (string, error) {
resp, err := http.Get(url)
resp, err := http.Get(url) //#nosec
if err != nil {
return "", err
}
Expand All @@ -105,7 +104,7 @@ func fetch(url string, out interface{}) (string, error) {
nextLink := extractNextLink(resp.Header)

if resp.StatusCode < 200 || resp.StatusCode >= 300 {
return "", errors.New(fmt.Sprintf("%s: %d %s ", url, resp.StatusCode, resp.Status))
return "", fmt.Errorf("%s: %d %s ", url, resp.StatusCode, resp.Status)
}

if err = json.NewDecoder(resp.Body).Decode(&out); err != nil {
Expand Down

0 comments on commit 705978b

Please sign in to comment.