forked from grafana/agent
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(rules/k8s): refactor common
rules.k8s
logic into `common/kuber…
…netes` package (grafana#6592) Signed-off-by: hainenber <[email protected]> Co-authored-by: William Dumont <[email protected]>
- Loading branch information
Showing
18 changed files
with
210 additions
and
544 deletions.
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,61 @@ | ||
package kubernetes | ||
|
||
import ( | ||
"github.com/go-kit/log" | ||
"github.com/grafana/agent/internal/flow/logging/level" | ||
"k8s.io/client-go/tools/cache" | ||
"k8s.io/client-go/util/workqueue" | ||
) | ||
|
||
// This type must be hashable, so it is kept simple. The indexer will maintain a | ||
// cache of current state, so this is mostly used for logging. | ||
type Event struct { | ||
Typ EventType | ||
ObjectKey string | ||
} | ||
|
||
type EventType string | ||
|
||
const ( | ||
EventTypeResourceChanged EventType = "resource-changed" | ||
) | ||
|
||
type queuedEventHandler struct { | ||
log log.Logger | ||
queue workqueue.RateLimitingInterface | ||
} | ||
|
||
func NewQueuedEventHandler(log log.Logger, queue workqueue.RateLimitingInterface) *queuedEventHandler { | ||
return &queuedEventHandler{ | ||
log: log, | ||
queue: queue, | ||
} | ||
} | ||
|
||
// OnAdd implements the cache.ResourceEventHandler interface. | ||
func (c *queuedEventHandler) OnAdd(obj interface{}, _ bool) { | ||
c.publishEvent(obj) | ||
} | ||
|
||
// OnUpdate implements the cache.ResourceEventHandler interface. | ||
func (c *queuedEventHandler) OnUpdate(oldObj, newObj interface{}) { | ||
c.publishEvent(newObj) | ||
} | ||
|
||
// OnDelete implements the cache.ResourceEventHandler interface. | ||
func (c *queuedEventHandler) OnDelete(obj interface{}) { | ||
c.publishEvent(obj) | ||
} | ||
|
||
func (c *queuedEventHandler) publishEvent(obj interface{}) { | ||
key, err := cache.MetaNamespaceKeyFunc(obj) | ||
if err != nil { | ||
level.Error(c.log).Log("msg", "failed to get key for object", "err", err) | ||
return | ||
} | ||
|
||
c.queue.AddRateLimited(Event{ | ||
Typ: EventTypeResourceChanged, | ||
ObjectKey: key, | ||
}) | ||
} |
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,34 @@ | ||
package kubernetes | ||
|
||
import ( | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/apimachinery/pkg/labels" | ||
) | ||
|
||
type LabelSelector struct { | ||
MatchLabels map[string]string `river:"match_labels,attr,optional"` | ||
MatchExpressions []MatchExpression `river:"match_expression,block,optional"` | ||
} | ||
|
||
type MatchExpression struct { | ||
Key string `river:"key,attr"` | ||
Operator string `river:"operator,attr"` | ||
Values []string `river:"values,attr,optional"` | ||
} | ||
|
||
func ConvertSelectorToListOptions(selector LabelSelector) (labels.Selector, error) { | ||
matchExpressions := []metav1.LabelSelectorRequirement{} | ||
|
||
for _, me := range selector.MatchExpressions { | ||
matchExpressions = append(matchExpressions, metav1.LabelSelectorRequirement{ | ||
Key: me.Key, | ||
Operator: metav1.LabelSelectorOperator(me.Operator), | ||
Values: me.Values, | ||
}) | ||
} | ||
|
||
return metav1.LabelSelectorAsSelector(&metav1.LabelSelector{ | ||
MatchLabels: selector.MatchLabels, | ||
MatchExpressions: matchExpressions, | ||
}) | ||
} |
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,13 @@ | ||
package kubernetes | ||
|
||
import ( | ||
"testing" | ||
|
||
"k8s.io/client-go/util/workqueue" | ||
) | ||
|
||
func TestEventTypeIsHashable(t *testing.T) { | ||
// This test is here to ensure that the EventType type is hashable according to the workqueue implementation | ||
queue := workqueue.NewRateLimitingQueue(workqueue.DefaultControllerRateLimiter()) | ||
queue.AddRateLimited(Event{}) | ||
} |
Oops, something went wrong.