Skip to content

Commit

Permalink
CI-1658: Implement go-staticcheck suggestions
Browse files Browse the repository at this point in the history
- moved away from `directoryv1.New()` as it's deprecated in favour of
  `directoryv1.NewService()`
- changed `time.Now().Sub(start).Seconds()` to `time.Since(start).Seconds()`
- removed redundant `return` statements
- removed unused vault-manager command flags
- changed `e.Error()` to `e.err` to avoid infinite recursive call in
  `pkg/workloads/console/events/gcp_pubsub.go`
- renamed _ to nop in NopPublisher Publish method to comply with ST1006,
  receiver name should not be an underscore, omit the name if it is unused
- renamed error variables to comply with ST1012, error var should have name
  of the form errFoo.
  • Loading branch information
Nabil372 committed Nov 30, 2022
1 parent 2d51533 commit ae98f53
Show file tree
Hide file tree
Showing 12 changed files with 18 additions and 20 deletions.
2 changes: 1 addition & 1 deletion apis/workloads/v1alpha1/console_attach_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func (c *ConsoleAttachObserverWebhook) Handle(ctx context.Context, req admission
)
logger.Info("starting request", "event", "request.start")
defer func(start time.Time) {
logging.WithNoRecord(logger).Info("completed request", "event", "request.end", "duration", time.Now().Sub(start).Seconds())
logging.WithNoRecord(logger).Info("completed request", "event", "request.end", "duration", time.Since(start).Seconds())
}(time.Now())

attachOptions := &corev1.PodAttachOptions{}
Expand Down
2 changes: 1 addition & 1 deletion apis/workloads/v1alpha1/console_authenticator_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func (c *ConsoleAuthenticatorWebhook) Handle(ctx context.Context, req admission.
logger := c.logger.WithValues("uuid", string(req.UID))
logger.Info("starting request", "event", "request.start")
defer func(start time.Time) {
logger.Info("completed request", "event", "request.end", "duration", time.Now().Sub(start).Seconds())
logger.Info("completed request", "event", "request.end", "duration", time.Since(start).Seconds())
}(time.Now())

csl := &Console{}
Expand Down
2 changes: 1 addition & 1 deletion apis/workloads/v1alpha1/console_authorisation_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func (c *ConsoleAuthorisationWebhook) Handle(ctx context.Context, req admission.
logger := c.logger.WithValues("uuid", string(req.UID))
logger.Info("starting request", "event", "request.start")
defer func(start time.Time) {
logger.Info("completed request", "event", "request.end", "duration", time.Now().Sub(start).Seconds())
logger.Info("completed request", "event", "request.end", "duration", time.Since(start).Seconds())
}(time.Now())

// request console authorisation object
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func (c *ConsoleTemplateValidationWebhook) Handle(ctx context.Context, req admis
logger.Info("starting request", "event", "request.start")

defer func(start time.Time) {
logger.Info("request completed", "event", "request.end", "duration", time.Now().Sub(start).Seconds())
logger.Info("request completed", "event", "request.end", "duration", time.Since(start).Seconds())
}(time.Now())

template := &ConsoleTemplate{}
Expand Down
3 changes: 2 additions & 1 deletion cmd/rbac-manager/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/alecthomas/kingpin"
"golang.org/x/oauth2/google"
directoryv1 "google.golang.org/api/admin/directory/v1"
"google.golang.org/api/option"
"k8s.io/apimachinery/pkg/runtime"
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
_ "k8s.io/client-go/plugin/pkg/client/auth/gcp" // this is required to auth against GCP
Expand Down Expand Up @@ -113,5 +114,5 @@ func createGoogleDirectory(ctx context.Context, subject string) (*directoryv1.Se
// Access to the directory API must be signed with a Subject to enable domain selection.
conf.Subject = subject

return directoryv1.New(conf.Client(ctx))
return directoryv1.NewService(ctx, option.WithHTTPClient(conf.Client(ctx)))
}
2 changes: 0 additions & 2 deletions cmd/vault-manager/acceptance/acceptance.go
Original file line number Diff line number Diff line change
Expand Up @@ -402,7 +402,6 @@ func (r *Runner) Run(logger kitlog.Logger, config *rest.Config) {
Expect(buffer.String()).To(
ContainSubstring(fmt.Sprintf("VAULT_TEST_SHELLWORD=%s", SentinelSecretValueShellword)),
)
return
}

expectsFuncFiles := func(buffer bytes.Buffer) {
Expand All @@ -415,7 +414,6 @@ func (r *Runner) Run(logger kitlog.Logger, config *rest.Config) {
Expect(buffer.String()).To(
ContainSubstring(fmt.Sprintf("ascii:%s", strings.Split(SentinelSecretValueNonASCII, "\n")[0])),
)
return
}

Describe("theatre-secrets", func() {
Expand Down
3 changes: 0 additions & 3 deletions cmd/vault-manager/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,6 @@ var (

commonOpts = cmd.NewCommonOptions(app).WithMetrics(app)

namespace = app.Flag("namespace", "Kubernetes webhook service namespace").Default("theatre-system").String()
serviceName = app.Flag("service-name", "Name of service for webhook").Default("theatre-vault-manager").String()
webhookName = app.Flag("webhook-name", "Name of webhook").Default("theatre-vault").String()
theatreImage = app.Flag("theatre-image", "Set to the same image as current binary").Required().String()
installPath = app.Flag("install-path", "Location to install theatre binaries").Default("/var/run/theatre").String()
namespaceLabel = app.Flag("namespace-label", "Namespace label that enables webhook to operate on").Default("theatre-secrets-injector").String()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"time"

directoryv1 "google.golang.org/api/admin/directory/v1"
"google.golang.org/api/option"
gock "gopkg.in/h2non/gock.v1"
"sigs.k8s.io/controller-runtime/pkg/log/zap"

Expand Down Expand Up @@ -121,7 +122,7 @@ var _ = Describe("NewGoogleDirectory", func() {
gock.DisableNetworking()
gock.New("") // this shouldn't be necessary, but is

service, err := directoryv1.New(client)
service, err := directoryv1.NewService(context.TODO(), option.WithHTTPClient(client))
Expect(err).NotTo(HaveOccurred())

directory = NewGoogleDirectory(service.Members)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"net/http"

directoryv1 "google.golang.org/api/admin/directory/v1"
"google.golang.org/api/option"
gock "gopkg.in/h2non/gock.v1"

. "github.com/onsi/ginkgo"
Expand All @@ -25,7 +26,7 @@ var _ = Describe("NewGoogleDirectory", func() {
gock.DisableNetworking()
gock.New("") // this shouldn't be necessary, but is

service, err := directoryv1.New(client)
service, err := directoryv1.NewService(context.TODO(), option.WithHTTPClient(client))
Expect(err).NotTo(HaveOccurred())

directory = NewGoogleDirectory(service.Members)
Expand Down
2 changes: 1 addition & 1 deletion pkg/workloads/console/events/gcp_pubsub.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ func (e ErrorPubsubFailedConnect) Unwrap() error { return e.err }
func (e ErrorPubsubFailedConnect) Error() string {
return fmt.Sprintf(
"failed to connect to pubsub topic: %s",
e.Error(),
e.err,
)
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/workloads/console/events/publisher.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ var _ Publisher = &NopPublisher{}
// https://en.wikipedia.org/wiki/NOP_(code)
type NopPublisher struct{}

func (_ NopPublisher) Publish(_ context.Context, _ interface{}) (string, error) { return "nop", nil }
func (nop NopPublisher) Publish(_ context.Context, _ interface{}) (string, error) { return "nop", nil }

func NewNopPublisher() *NopPublisher {
return &NopPublisher{}
Expand Down
12 changes: 6 additions & 6 deletions pkg/workloads/console/runner/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ func (c *Runner) Create(ctx context.Context, opts CreateOptions) (*workloadsv1al

// Wait for authorisation step or until ready
_, err = c.WaitUntilReady(ctx, *csl, false)
if err == consolePendingAuthorisationError {
if err == errConsolePendingAuthorisation {
rule, err := tpl.GetAuthorisationRuleForCommand(opts.Command)
if err != nil {
return csl, fmt.Errorf("failed to get authorisation rule %w", err)
Expand Down Expand Up @@ -838,8 +838,8 @@ func (c *Runner) WaitUntilReady(ctx context.Context, createdCsl workloadsv1alpha
}

var (
consolePendingAuthorisationError = errors.New("console pending authorisation")
consoleNotFoundError = errors.New("console not found")
errConsoleNotFound = errors.New("console not found")
errConsolePendingAuthorisation = errors.New("console pending authorisation")
)

func (c *Runner) waitForConsole(ctx context.Context, createdCsl workloadsv1alpha1.Console, waitForAuthorisation bool) (*workloadsv1alpha1.Console, error) {
Expand Down Expand Up @@ -876,7 +876,7 @@ func (c *Runner) waitForConsole(ctx context.Context, createdCsl workloadsv1alpha
return csl, nil
}
if isPendingAuthorisation(csl) {
return csl, consolePendingAuthorisationError
return csl, errConsolePendingAuthorisation
}
// If the console has already stopped it may have already run to
// completion, so let's return it
Expand All @@ -903,7 +903,7 @@ func (c *Runner) waitForConsole(ctx context.Context, createdCsl workloadsv1alpha
return csl, nil
}
if isPendingAuthorisation(csl) {
return csl, consolePendingAuthorisationError
return csl, errConsolePendingAuthorisation
}
// If the console has already stopped it may have already run to
// completion, so let's return it
Expand All @@ -912,7 +912,7 @@ func (c *Runner) waitForConsole(ctx context.Context, createdCsl workloadsv1alpha
}
case <-ctx.Done():
if csl == nil {
return nil, fmt.Errorf("%s: %w", consoleNotFoundError, ctx.Err())
return nil, fmt.Errorf("%s: %w", errConsoleNotFound, ctx.Err())
}
return nil, fmt.Errorf("console's last phase was: %v: %w", csl.Status.Phase, ctx.Err())
}
Expand Down

0 comments on commit ae98f53

Please sign in to comment.