Skip to content

Commit

Permalink
Add bearer token defaults for Kubernetes plugins (#6356)
Browse files Browse the repository at this point in the history
  • Loading branch information
rawkode authored and danielnelson committed Nov 6, 2019
1 parent 6881c64 commit 284c7fc
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 26 deletions.
3 changes: 3 additions & 0 deletions plugins/inputs/kube_inventory/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ avoid cardinality issues:
# namespace = "default"

## Use bearer token for authorization. ('bearer_token' takes priority)
## If both of these are empty, we'll use the default serviceaccount:
## at: /run/secrets/kubernetes.io/serviceaccount/token
# bearer_token = "/path/to/bearer/token"
## OR
# bearer_token_string = "abc_123"
Expand Down Expand Up @@ -265,6 +267,7 @@ The persistentvolumeclaim "phase" is saved in the `phase` tag with a correlated
| pending | 2 |
| unknown | 3 |


### Example Output:

```
Expand Down
44 changes: 28 additions & 16 deletions plugins/inputs/kube_inventory/kube_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ import (
"github.com/influxdata/telegraf/plugins/inputs"
)

const (
defaultServiceAccountPath = "/run/secrets/kubernetes.io/serviceaccount/token"
)

// KubernetesInventory represents the config object for the plugin.
type KubernetesInventory struct {
URL string `toml:"url"`
Expand All @@ -42,6 +46,8 @@ var sampleConfig = `
# namespace = "default"
## Use bearer token for authorization. ('bearer_token' takes priority)
## If both of these are empty, we'll use the default serviceaccount:
## at: /run/secrets/kubernetes.io/serviceaccount/token
# bearer_token = "/path/to/bearer/token"
## OR
# bearer_token_string = "abc_123"
Expand Down Expand Up @@ -77,14 +83,32 @@ func (ki *KubernetesInventory) Description() string {
return "Read metrics from the Kubernetes api"
}

// Gather collects kubernetes metrics from a given URL.
func (ki *KubernetesInventory) Gather(acc telegraf.Accumulator) (err error) {
if ki.client == nil {
if ki.client, err = ki.initClient(); err != nil {
func (ki *KubernetesInventory) Init() error {
// If neither are provided, use the default service account.
if ki.BearerToken == "" && ki.BearerTokenString == "" {
ki.BearerToken = defaultServiceAccountPath
}

if ki.BearerToken != "" {
token, err := ioutil.ReadFile(ki.BearerToken)
if err != nil {
return err
}
ki.BearerTokenString = strings.TrimSpace(string(token))
}

var err error
ki.client, err = newClient(ki.URL, ki.Namespace, ki.BearerTokenString, ki.ResponseTimeout.Duration, ki.ClientConfig)

if err != nil {
return err
}

return nil
}

// Gather collects kubernetes metrics from a given URL.
func (ki *KubernetesInventory) Gather(acc telegraf.Accumulator) (err error) {
resourceFilter, err := filter.NewIncludeExcludeFilter(ki.ResourceInclude, ki.ResourceExclude)
if err != nil {
return err
Expand Down Expand Up @@ -121,18 +145,6 @@ var availableCollectors = map[string]func(ctx context.Context, acc telegraf.Accu
"persistentvolumeclaims": collectPersistentVolumeClaims,
}

func (ki *KubernetesInventory) initClient() (*client, error) {
if ki.BearerToken != "" {
token, err := ioutil.ReadFile(ki.BearerToken)
if err != nil {
return nil, err
}
ki.BearerTokenString = strings.TrimSpace(string(token))
}

return newClient(ki.URL, ki.Namespace, ki.BearerTokenString, ki.ResponseTimeout.Duration, ki.ClientConfig)
}

func atoi(s string) int64 {
i, err := strconv.ParseInt(s, 10, 64)
if err != nil {
Expand Down
2 changes: 2 additions & 0 deletions plugins/inputs/kubernetes/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ avoid cardinality issues:
url = "http://127.0.0.1:10255"

## Use bearer token for authorization. ('bearer_token' takes priority)
## If both of these are empty, we'll use the default serviceaccount:
## at: /run/secrets/kubernetes.io/serviceaccount/token
# bearer_token = "/path/to/bearer/token"
## OR
# bearer_token_string = "abc_123"
Expand Down
32 changes: 22 additions & 10 deletions plugins/inputs/kubernetes/kubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ var sampleConfig = `
url = "http://127.0.0.1:10255"
## Use bearer token for authorization. ('bearer_token' takes priority)
## If both of these are empty, we'll use the default serviceaccount:
## at: /run/secrets/kubernetes.io/serviceaccount/token
# bearer_token = "/path/to/bearer/token"
## OR
# bearer_token_string = "abc_123"
Expand All @@ -52,7 +54,8 @@ var sampleConfig = `
`

const (
summaryEndpoint = `%s/stats/summary`
summaryEndpoint = `%s/stats/summary`
defaultServiceAccountPath = "/run/secrets/kubernetes.io/serviceaccount/token"
)

func init() {
Expand All @@ -71,6 +74,23 @@ func (k *Kubernetes) Description() string {
return "Read metrics from the kubernetes kubelet api"
}

func (k *Kubernetes) Init() error {
// If neither are provided, use the default service account.
if k.BearerToken == "" && k.BearerTokenString == "" {
k.BearerToken = defaultServiceAccountPath
}

if k.BearerToken != "" {
token, err := ioutil.ReadFile(k.BearerToken)
if err != nil {
return err
}
k.BearerTokenString = strings.TrimSpace(string(token))
}

return nil
}

//Gather collects kubernetes metrics from a given URL
func (k *Kubernetes) Gather(acc telegraf.Accumulator) error {
acc.AddError(k.gatherSummary(k.URL, acc))
Expand Down Expand Up @@ -108,15 +128,7 @@ func (k *Kubernetes) gatherSummary(baseURL string, acc telegraf.Accumulator) err
}
}

if k.BearerToken != "" {
token, err := ioutil.ReadFile(k.BearerToken)
if err != nil {
return err
}
req.Header.Set("Authorization", "Bearer "+strings.TrimSpace(string(token)))
} else if k.BearerTokenString != "" {
req.Header.Set("Authorization", "Bearer "+k.BearerTokenString)
}
req.Header.Set("Authorization", "Bearer "+k.BearerTokenString)
req.Header.Add("Accept", "application/json")

resp, err = k.RoundTripper.RoundTrip(req)
Expand Down

0 comments on commit 284c7fc

Please sign in to comment.