-
Notifications
You must be signed in to change notification settings - Fork 488
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
New components remote.kubernetes.secret and remote.kubernetes.configmap #4854
Merged
Merged
Changes from 28 commits
Commits
Show all changes
40 commits
Select commit
Hold shift + click to select a range
7811445
initial work
captncraig 2ddf059
Merge branch 'main' into cmp_remote_k8s
captncraig da27c63
move to subdir
captncraig 07c0c9d
cleanup
captncraig 48588df
add docs
captncraig 36d13b7
add all files
captncraig 6bbcc7b
lint
captncraig 13a25b8
Update docs/sources/flow/reference/components/remote.kubernetes.confi…
captncraig cab2dfa
Update docs/sources/flow/reference/components/remote.kubernetes.confi…
captncraig 889c17f
Update docs/sources/flow/reference/components/remote.kubernetes.confi…
captncraig 8c3dbc5
Update docs/sources/flow/reference/components/remote.kubernetes.secre…
captncraig 183359e
Update docs/sources/flow/reference/components/remote.kubernetes.secre…
captncraig 854c260
Update docs/sources/flow/reference/components/remote.kubernetes.secre…
captncraig 1f15a59
Update docs/sources/flow/reference/components/remote.kubernetes.secre…
captncraig b13bc6a
Update docs/sources/flow/reference/components/remote.kubernetes.secre…
captncraig c15604a
Update docs/sources/flow/reference/components/remote.kubernetes.confi…
captncraig 7cf1e78
Update docs/sources/flow/reference/components/remote.kubernetes.confi…
captncraig 6812df9
Update docs/sources/flow/reference/components/remote.kubernetes.confi…
captncraig 6a2400a
Update docs/sources/flow/reference/components/remote.kubernetes.secre…
captncraig 6620e9b
Update docs/sources/flow/reference/components/remote.kubernetes.confi…
captncraig ba13c0f
Update docs/sources/flow/reference/components/remote.kubernetes.confi…
captncraig 8f65945
Update docs/sources/flow/reference/components/remote.kubernetes.secre…
captncraig c7e7fa8
Update docs/sources/flow/reference/components/remote.kubernetes.secre…
captncraig 28adfa1
Merge branch 'main' into cmp_remote_k8s
captncraig 3fa70c4
Merge branch 'cmp_remote_k8s' of github.com:grafana/agent into cmp_re…
captncraig 908606e
switch to correct package after river move
captncraig c91f4aa
changelog
captncraig 1a972a9
Merge branch 'main' into cmp_remote_k8s
captncraig 7458ded
Merge branch 'main' into cmp_remote_k8s
captncraig f7e3cde
Update docs/sources/flow/reference/components/remote.kubernetes.confi…
captncraig e0b7562
Update docs/sources/flow/reference/components/remote.kubernetes.secre…
captncraig bbe8651
Update docs/sources/flow/reference/components/remote.kubernetes.confi…
captncraig 4a1a9bb
Update component/remote/kubernetes/kubernetes.go
captncraig f8a91bf
Update docs/sources/flow/reference/components/remote.kubernetes.confi…
captncraig 9e1cb10
Update docs/sources/flow/reference/components/remote.kubernetes.secre…
captncraig 31b92b4
Update component/remote/kubernetes/kubernetes.go
captncraig 1cac49b
Update docs/sources/flow/reference/components/remote.kubernetes.secre…
captncraig 9834560
add test, make timeout optional, and enforced
captncraig ecd414a
require positive timeout
captncraig f27bcf4
newline
captncraig File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package configmap | ||
|
||
import ( | ||
"github.com/grafana/agent/component" | ||
"github.com/grafana/agent/component/remote/kubernetes" | ||
) | ||
|
||
func init() { | ||
component.Register(component.Registration{ | ||
Name: "remote.kubernetes.configmap", | ||
Args: kubernetes.Arguments{}, | ||
Exports: kubernetes.Exports{}, | ||
Build: func(opts component.Options, args component.Arguments) (component.Component, error) { | ||
return kubernetes.New(opts, args.(kubernetes.Arguments), kubernetes.TypeConfigMap) | ||
}, | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,248 @@ | ||
// Package kubernetes implements the logic for remote.kubernetes.secret and remote.kubernetes.configmap component. | ||
package kubernetes | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"reflect" | ||
"sync" | ||
"time" | ||
|
||
"github.com/go-kit/log" | ||
|
||
"github.com/grafana/agent/component" | ||
"github.com/grafana/agent/component/common/kubernetes" | ||
"github.com/grafana/river/rivertypes" | ||
|
||
v1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
client_go "k8s.io/client-go/kubernetes" | ||
) | ||
|
||
type ResourceType string | ||
|
||
const ( | ||
TypeSecret ResourceType = "secret" | ||
TypeConfigMap ResourceType = "configmap" | ||
) | ||
|
||
// Arguments control the component. | ||
type Arguments struct { | ||
Namespace string `river:"namespace,attr"` | ||
Name string `river:"name,attr"` | ||
PollFrequency time.Duration `river:"poll_frequency,attr,optional"` | ||
PollTimeout time.Duration `river:"poll_timeout,attr,optional"` | ||
|
||
// Client settings to connect to Kubernetes. | ||
Client kubernetes.ClientArguments `river:"client,block,optional"` | ||
} | ||
|
||
// DefaultArguments holds default settings for Arguments. | ||
var DefaultArguments = Arguments{ | ||
PollFrequency: 1 * time.Minute, | ||
PollTimeout: 15 * time.Second, | ||
} | ||
|
||
// SetToDefault implements river.Defaulter. | ||
func (args *Arguments) SetToDefault() { | ||
*args = DefaultArguments | ||
} | ||
|
||
// Validate implements river.Validator. | ||
func (args *Arguments) Validate() error { | ||
if args.PollFrequency <= 0 { | ||
return fmt.Errorf("poll_frequency must be greater than 0") | ||
} | ||
return nil | ||
} | ||
|
||
// Exports holds settings exported by this component. | ||
type Exports struct { | ||
Data map[string]rivertypes.OptionalSecret `river:"data,attr"` | ||
} | ||
|
||
// Component implements the remote.kubernetes.* component. | ||
type Component struct { | ||
log log.Logger | ||
opts component.Options | ||
|
||
mut sync.Mutex | ||
args Arguments | ||
|
||
client *client_go.Clientset | ||
kind ResourceType | ||
|
||
lastPoll time.Time | ||
lastExports Exports // Used for determining whether exports should be updated | ||
|
||
healthMut sync.RWMutex | ||
health component.Health | ||
} | ||
|
||
var ( | ||
_ component.Component = (*Component)(nil) | ||
_ component.HealthComponent = (*Component)(nil) | ||
) | ||
|
||
// New returns a new, unstarted, remote.http component. | ||
captncraig marked this conversation as resolved.
Show resolved
Hide resolved
|
||
func New(opts component.Options, args Arguments, rType ResourceType) (*Component, error) { | ||
c := &Component{ | ||
log: opts.Logger, | ||
opts: opts, | ||
|
||
kind: rType, | ||
health: component.Health{ | ||
Health: component.HealthTypeUnknown, | ||
Message: "component started", | ||
UpdateTime: time.Now(), | ||
}, | ||
} | ||
|
||
if err := c.Update(args); err != nil { | ||
return nil, err | ||
} | ||
return c, nil | ||
} | ||
|
||
// Run starts the remote.http component. | ||
captncraig marked this conversation as resolved.
Show resolved
Hide resolved
|
||
func (c *Component) Run(ctx context.Context) error { | ||
for { | ||
select { | ||
case <-ctx.Done(): | ||
return nil | ||
case <-time.After(c.nextPoll()): | ||
c.poll() | ||
} | ||
} | ||
} | ||
|
||
// nextPoll returns how long to wait to poll given the last time a | ||
// poll occurred. nextPoll returns 0 if a poll should occur immediately. | ||
func (c *Component) nextPoll() time.Duration { | ||
c.mut.Lock() | ||
defer c.mut.Unlock() | ||
|
||
nextPoll := c.lastPoll.Add(c.args.PollFrequency) | ||
now := time.Now() | ||
|
||
if now.After(nextPoll) { | ||
// Poll immediately; next poll period was in the past. | ||
return 0 | ||
} | ||
return nextPoll.Sub(now) | ||
} | ||
|
||
// poll performs a HTTP GET for the component's configured URL. c.mut must | ||
// not be held when calling. After polling, the component's health is updated | ||
// with the success or failure status. | ||
func (c *Component) poll() { | ||
err := c.pollError() | ||
c.updatePollHealth(err) | ||
} | ||
|
||
func (c *Component) updatePollHealth(err error) { | ||
c.healthMut.Lock() | ||
defer c.healthMut.Unlock() | ||
|
||
if err == nil { | ||
c.health = component.Health{ | ||
Health: component.HealthTypeHealthy, | ||
Message: "got " + string(c.kind), | ||
UpdateTime: time.Now(), | ||
} | ||
} else { | ||
c.health = component.Health{ | ||
Health: component.HealthTypeUnhealthy, | ||
Message: fmt.Sprintf("polling failed: %s", err), | ||
UpdateTime: time.Now(), | ||
} | ||
} | ||
} | ||
|
||
// pollError is like poll but returns an error if one occurred. | ||
func (c *Component) pollError() error { | ||
c.mut.Lock() | ||
defer c.mut.Unlock() | ||
|
||
c.lastPoll = time.Now() | ||
|
||
ctx, cancel := context.WithTimeout(context.Background(), c.args.PollTimeout) | ||
defer cancel() | ||
|
||
data := map[string]rivertypes.OptionalSecret{} | ||
if c.kind == TypeSecret { | ||
secret, err := c.client.CoreV1().Secrets(c.args.Namespace).Get(ctx, c.args.Name, v1.GetOptions{}) | ||
if err != nil { | ||
return err | ||
} | ||
for k, v := range secret.Data { | ||
data[k] = rivertypes.OptionalSecret{ | ||
Value: string(v), | ||
IsSecret: true, | ||
} | ||
} | ||
} else if c.kind == TypeConfigMap { | ||
cmap, err := c.client.CoreV1().ConfigMaps(c.args.Namespace).Get(ctx, c.args.Name, v1.GetOptions{}) | ||
if err != nil { | ||
return err | ||
} | ||
for k, v := range cmap.Data { | ||
data[k] = rivertypes.OptionalSecret{ | ||
Value: v, | ||
IsSecret: false, | ||
} | ||
} | ||
} | ||
|
||
newExports := Exports{ | ||
Data: data, | ||
} | ||
|
||
// Only send a state change event if the exports have changed from the | ||
// previous poll. | ||
if !reflect.DeepEqual(newExports.Data, c.lastExports.Data) { | ||
c.opts.OnStateChange(newExports) | ||
} | ||
|
||
c.lastExports = newExports | ||
return nil | ||
} | ||
|
||
// Update updates the remote.kubernetes.* component. After the update completes, a | ||
// poll is forced. | ||
func (c *Component) Update(args component.Arguments) (err error) { | ||
// defer initial poll so the lock is released first | ||
defer func() { | ||
if err != nil { | ||
return | ||
} | ||
// Poll after updating and propagate the error if the poll fails. If an error | ||
// occurred during Update, we don't bother to do anything. | ||
// It is important to set err and the health so startup works correctly | ||
err = c.pollError() | ||
c.updatePollHealth(err) | ||
}() | ||
|
||
c.mut.Lock() | ||
defer c.mut.Unlock() | ||
|
||
newArgs := args.(Arguments) | ||
c.args = newArgs | ||
|
||
restConfig, err := c.args.Client.BuildRESTConfig(c.log) | ||
if err != nil { | ||
return err | ||
} | ||
c.client, err = client_go.NewForConfig(restConfig) | ||
if err != nil { | ||
return fmt.Errorf("creating kubernetes client: %w", err) | ||
} | ||
|
||
return err | ||
} | ||
|
||
// CurrentHealth returns the current health of the component. | ||
func (c *Component) CurrentHealth() component.Health { | ||
c.healthMut.RLock() | ||
defer c.healthMut.RUnlock() | ||
return c.health | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package secret | ||
|
||
import ( | ||
"github.com/grafana/agent/component" | ||
"github.com/grafana/agent/component/remote/kubernetes" | ||
) | ||
|
||
func init() { | ||
component.Register(component.Registration{ | ||
Name: "remote.kubernetes.secret", | ||
Args: kubernetes.Arguments{}, | ||
Exports: kubernetes.Exports{}, | ||
Build: func(opts component.Options, args component.Arguments) (component.Component, error) { | ||
return kubernetes.New(opts, args.(kubernetes.Arguments), kubernetes.TypeSecret) | ||
}, | ||
}) | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should there also be a check that the
PollTimeout
is > 0? Otherwise it will always cancel the request