Skip to content

Commit

Permalink
chore: Fix linter findings for revive:exported in plugins/inputs/d*
Browse files Browse the repository at this point in the history
  • Loading branch information
zak-pawel committed Oct 14, 2024
1 parent 23fc01c commit e26131e
Show file tree
Hide file tree
Showing 27 changed files with 557 additions and 555 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/docker/docker/api/types/container"
)

// CalculateCPUPercentUnix calculate CPU usage (for Unix, in percentages)
func CalculateCPUPercentUnix(previousCPU, previousSystem uint64, v *container.StatsResponse) float64 {
var (
cpuPercent = 0.0
Expand All @@ -25,7 +26,8 @@ func CalculateCPUPercentUnix(previousCPU, previousSystem uint64, v *container.St
return cpuPercent
}

func calculateCPUPercentWindows(v *container.StatsResponse) float64 {
// CalculateCPUPercentWindows calculate CPU usage (for Windows, in percentages)
func CalculateCPUPercentWindows(v *container.StatsResponse) float64 {
// Max number of 100ns intervals between the previous time read and now
possIntervals := uint64(v.Read.Sub(v.PreRead).Nanoseconds()) // Start with number of ns intervals
possIntervals /= 100 // Convert to number of 100ns intervals
Expand Down Expand Up @@ -66,6 +68,7 @@ func CalculateMemUsageUnixNoCache(mem container.MemoryStats) float64 {
return float64(mem.Usage)
}

// CalculateMemPercentUnixNoCache calculate memory usage of the container, in percentages.
func CalculateMemPercentUnixNoCache(limit, usedNoCache float64) float64 {
// MemoryStats.Limit will never be 0 unless the container is not running and we haven't
// got any data from cgroup
Expand Down
80 changes: 40 additions & 40 deletions plugins/inputs/dcos/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,23 @@ const (
loginDuration = 65 * time.Minute
)

// Client is an interface for communicating with the DC/OS API.
type Client interface {
SetToken(token string)

Login(ctx context.Context, sa *ServiceAccount) (*authToken, error)
GetSummary(ctx context.Context) (*summary, error)
GetContainers(ctx context.Context, node string) ([]container, error)
GetNodeMetrics(ctx context.Context, node string) (*metrics, error)
GetContainerMetrics(ctx context.Context, node, container string) (*metrics, error)
GetAppMetrics(ctx context.Context, node, container string) (*metrics, error)
// client is an interface for communicating with the DC/OS API.
type client interface {
setToken(token string)

login(ctx context.Context, sa *serviceAccount) (*authToken, error)
getSummary(ctx context.Context) (*summary, error)
getContainers(ctx context.Context, node string) ([]container, error)
getNodeMetrics(ctx context.Context, node string) (*metrics, error)
getContainerMetrics(ctx context.Context, node, container string) (*metrics, error)
getAppMetrics(ctx context.Context, node, container string) (*metrics, error)
}

type apiError struct {
URL string
StatusCode int
Title string
Description string
url string
statusCode int
title string
description string
}

// login is request data for logging in.
Expand Down Expand Up @@ -90,7 +90,7 @@ type authToken struct {
Expire time.Time
}

// clusterClient is a Client that uses the cluster URL.
// clusterClient is a client that uses the cluster URL.
type clusterClient struct {
clusterURL *url.URL
httpClient *http.Client
Expand All @@ -104,13 +104,13 @@ type claims struct {
}

func (e apiError) Error() string {
if e.Description != "" {
return fmt.Sprintf("[%s] %s: %s", e.URL, e.Title, e.Description)
if e.description != "" {
return fmt.Sprintf("[%s] %s: %s", e.url, e.title, e.description)
}
return fmt.Sprintf("[%s] %s", e.URL, e.Title)
return fmt.Sprintf("[%s] %s", e.url, e.title)
}

func NewClusterClient(clusterURL *url.URL, timeout time.Duration, maxConns int, tlsConfig *tls.Config) *clusterClient {
func newClusterClient(clusterURL *url.URL, timeout time.Duration, maxConns int, tlsConfig *tls.Config) *clusterClient {
httpClient := &http.Client{
Transport: &http.Transport{
MaxIdleConns: maxConns,
Expand All @@ -128,11 +128,11 @@ func NewClusterClient(clusterURL *url.URL, timeout time.Duration, maxConns int,
return c
}

func (c *clusterClient) SetToken(token string) {
func (c *clusterClient) setToken(token string) {
c.token = token
}

func (c *clusterClient) Login(ctx context.Context, sa *ServiceAccount) (*authToken, error) {
func (c *clusterClient) login(ctx context.Context, sa *serviceAccount) (*authToken, error) {
token, err := c.createLoginToken(sa)
if err != nil {
return nil, err
Expand All @@ -141,7 +141,7 @@ func (c *clusterClient) Login(ctx context.Context, sa *ServiceAccount) (*authTok
exp := time.Now().Add(loginDuration)

body := &login{
UID: sa.AccountID,
UID: sa.accountID,
Exp: exp.Unix(),
Token: token,
}
Expand Down Expand Up @@ -185,23 +185,23 @@ func (c *clusterClient) Login(ctx context.Context, sa *ServiceAccount) (*authTok
err = dec.Decode(loginError)
if err != nil {
err := &apiError{
URL: loc,
StatusCode: resp.StatusCode,
Title: resp.Status,
url: loc,
statusCode: resp.StatusCode,
title: resp.Status,
}
return nil, err
}

err = &apiError{
URL: loc,
StatusCode: resp.StatusCode,
Title: loginError.Title,
Description: loginError.Description,
url: loc,
statusCode: resp.StatusCode,
title: loginError.Title,
description: loginError.Description,
}
return nil, err
}

func (c *clusterClient) GetSummary(ctx context.Context) (*summary, error) {
func (c *clusterClient) getSummary(ctx context.Context) (*summary, error) {
summary := &summary{}
err := c.doGet(ctx, c.toURL("/mesos/master/state-summary"), summary)
if err != nil {
Expand All @@ -211,7 +211,7 @@ func (c *clusterClient) GetSummary(ctx context.Context) (*summary, error) {
return summary, nil
}

func (c *clusterClient) GetContainers(ctx context.Context, node string) ([]container, error) {
func (c *clusterClient) getContainers(ctx context.Context, node string) ([]container, error) {
list := []string{}

path := fmt.Sprintf("/system/v1/agent/%s/metrics/v0/containers", node)
Expand Down Expand Up @@ -239,17 +239,17 @@ func (c *clusterClient) getMetrics(ctx context.Context, address string) (*metric
return metrics, nil
}

func (c *clusterClient) GetNodeMetrics(ctx context.Context, node string) (*metrics, error) {
func (c *clusterClient) getNodeMetrics(ctx context.Context, node string) (*metrics, error) {
path := fmt.Sprintf("/system/v1/agent/%s/metrics/v0/node", node)
return c.getMetrics(ctx, c.toURL(path))
}

func (c *clusterClient) GetContainerMetrics(ctx context.Context, node, container string) (*metrics, error) {
func (c *clusterClient) getContainerMetrics(ctx context.Context, node, container string) (*metrics, error) {
path := fmt.Sprintf("/system/v1/agent/%s/metrics/v0/containers/%s", node, container)
return c.getMetrics(ctx, c.toURL(path))
}

func (c *clusterClient) GetAppMetrics(ctx context.Context, node, container string) (*metrics, error) {
func (c *clusterClient) getAppMetrics(ctx context.Context, node, container string) (*metrics, error) {
path := fmt.Sprintf("/system/v1/agent/%s/metrics/v0/containers/%s/app", node, container)
return c.getMetrics(ctx, c.toURL(path))
}
Expand Down Expand Up @@ -298,9 +298,9 @@ func (c *clusterClient) doGet(ctx context.Context, address string, v interface{}

if resp.StatusCode < 200 || resp.StatusCode >= 300 {
return &apiError{
URL: address,
StatusCode: resp.StatusCode,
Title: resp.Status,
url: address,
statusCode: resp.StatusCode,
title: resp.Status,
}
}

Expand All @@ -318,13 +318,13 @@ func (c *clusterClient) toURL(path string) string {
return clusterURL.String()
}

func (c *clusterClient) createLoginToken(sa *ServiceAccount) (string, error) {
func (c *clusterClient) createLoginToken(sa *serviceAccount) (string, error) {
token := jwt.NewWithClaims(jwt.SigningMethodRS256, claims{
UID: sa.AccountID,
UID: sa.accountID,
RegisteredClaims: jwt.RegisteredClaims{
// How long we have to login with this token
ExpiresAt: jwt.NewNumericDate(time.Now().Add(time.Minute * 5)),
},
})
return token.SignedString(sa.PrivateKey)
return token.SignedString(sa.privateKey)
}
36 changes: 18 additions & 18 deletions plugins/inputs/dcos/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,10 @@ func TestLogin(t *testing.T) {
responseCode: http.StatusUnauthorized,
responseBody: `{"title": "x", "description": "y"}`,
expectedError: &apiError{
URL: ts.URL + "/acs/api/v1/auth/login",
StatusCode: http.StatusUnauthorized,
Title: "x",
Description: "y",
url: ts.URL + "/acs/api/v1/auth/login",
statusCode: http.StatusUnauthorized,
title: "x",
description: "y",
},
expectedToken: "",
},
Expand All @@ -62,12 +62,12 @@ func TestLogin(t *testing.T) {
require.NoError(t, err)

ctx := context.Background()
sa := &ServiceAccount{
AccountID: "telegraf",
PrivateKey: key,
sa := &serviceAccount{
accountID: "telegraf",
privateKey: key,
}
client := NewClusterClient(u, defaultResponseTimeout, 1, nil)
auth, err := client.Login(ctx, sa)
client := newClusterClient(u, defaultResponseTimeout, 1, nil)
auth, err := client.login(ctx, sa)

require.Equal(t, tt.expectedError, err)

Expand Down Expand Up @@ -104,9 +104,9 @@ func TestGetSummary(t *testing.T) {
responseBody: `<html></html>`,
expectedValue: nil,
expectedError: &apiError{
URL: ts.URL + "/mesos/master/state-summary",
StatusCode: http.StatusUnauthorized,
Title: "401 Unauthorized",
url: ts.URL + "/mesos/master/state-summary",
statusCode: http.StatusUnauthorized,
title: "401 Unauthorized",
},
},
{
Expand Down Expand Up @@ -136,8 +136,8 @@ func TestGetSummary(t *testing.T) {
require.NoError(t, err)

ctx := context.Background()
client := NewClusterClient(u, defaultResponseTimeout, 1, nil)
summary, err := client.GetSummary(ctx)
client := newClusterClient(u, defaultResponseTimeout, 1, nil)
summary, err := client.getSummary(ctx)

require.Equal(t, tt.expectedError, err)
require.Equal(t, tt.expectedValue, summary)
Expand Down Expand Up @@ -177,8 +177,8 @@ func TestGetNodeMetrics(t *testing.T) {
require.NoError(t, err)

ctx := context.Background()
client := NewClusterClient(u, defaultResponseTimeout, 1, nil)
m, err := client.GetNodeMetrics(ctx, "foo")
client := newClusterClient(u, defaultResponseTimeout, 1, nil)
m, err := client.getNodeMetrics(ctx, "foo")

require.Equal(t, tt.expectedError, err)
require.Equal(t, tt.expectedValue, m)
Expand Down Expand Up @@ -218,8 +218,8 @@ func TestGetContainerMetrics(t *testing.T) {
require.NoError(t, err)

ctx := context.Background()
client := NewClusterClient(u, defaultResponseTimeout, 1, nil)
m, err := client.GetContainerMetrics(ctx, "foo", "bar")
client := newClusterClient(u, defaultResponseTimeout, 1, nil)
m, err := client.getContainerMetrics(ctx, "foo", "bar")

require.Equal(t, tt.expectedError, err)
require.Equal(t, tt.expectedValue, m)
Expand Down
30 changes: 15 additions & 15 deletions plugins/inputs/dcos/creds.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,39 +15,39 @@ const (
relogDuration = 5 * time.Minute
)

type Credentials interface {
Token(ctx context.Context, client Client) (string, error)
IsExpired() bool
type credentials interface {
token(ctx context.Context, client client) (string, error)
isExpired() bool
}

type ServiceAccount struct {
AccountID string
PrivateKey *rsa.PrivateKey
type serviceAccount struct {
accountID string
privateKey *rsa.PrivateKey

auth *authToken
}

type TokenCreds struct {
type tokenCreds struct {
Path string
}

type NullCreds struct {
type nullCreds struct {
}

func (c *ServiceAccount) Token(ctx context.Context, client Client) (string, error) {
auth, err := client.Login(ctx, c)
func (c *serviceAccount) token(ctx context.Context, client client) (string, error) {
auth, err := client.login(ctx, c)
if err != nil {
return "", err
}
c.auth = auth
return auth.Text, nil
}

func (c *ServiceAccount) IsExpired() bool {
func (c *serviceAccount) isExpired() bool {
return c.auth.Text != "" || c.auth.Expire.Add(relogDuration).After(time.Now())
}

func (c *TokenCreds) Token(_ context.Context, _ Client) (string, error) {
func (c *tokenCreds) token(_ context.Context, _ client) (string, error) {
octets, err := os.ReadFile(c.Path)
if err != nil {
return "", fmt.Errorf("error reading token file %q: %w", c.Path, err)
Expand All @@ -59,14 +59,14 @@ func (c *TokenCreds) Token(_ context.Context, _ Client) (string, error) {
return token, nil
}

func (c *TokenCreds) IsExpired() bool {
func (c *tokenCreds) isExpired() bool {
return true
}

func (c *NullCreds) Token(_ context.Context, _ Client) (string, error) {
func (c *nullCreds) token(_ context.Context, _ client) (string, error) {
return "", nil
}

func (c *NullCreds) IsExpired() bool {
func (c *nullCreds) isExpired() bool {
return true
}
Loading

0 comments on commit e26131e

Please sign in to comment.