Skip to content

Commit

Permalink
feat: Allow explicit definition of what API resources should be monit…
Browse files Browse the repository at this point in the history
…ored (#104)

* Simple implementation of trackable resources

* Update environment variable name

* Add documentation about environment variable
  • Loading branch information
hiddenmarten authored Jan 3, 2025
1 parent e843d11 commit 05ab829
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,12 @@ You can use the following to save yourself from timezone shenanigans:
kubectl annotate pod hello-world k8s-ttl-controller.twin.sh/refreshed-at=$(date -u +"%Y-%m-%dT%H:%M:%SZ")
```

You can use environment variable `API_RESOURCES_TO_WATCH` to specify the resources to watch. By default, the controller watches
all resources in the cluster. You can specify a comma-separated list of resources to watch, such as `pods,deployments`.

```console
export API_RESOURCES_TO_WATCH=pods,deployments
```

## Deploying on Kubernetes
### Using Helm
Expand Down
25 changes: 24 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"time"

"github.com/TwiN/kevent"
str2duration "github.com/xhit/go-str2duration/v2"
"github.com/xhit/go-str2duration/v2"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime/schema"
Expand All @@ -28,6 +28,8 @@ const (
ThrottleDuration = 50 * time.Millisecond // Duration to sleep for throttling purposes

ListLimit = 500 // Maximum number of items to list at once

APIResourcesToWatchEnv = "API_RESOURCES_TO_WATCH"
)

var (
Expand All @@ -38,6 +40,8 @@ var (

logger *slog.Logger // Global logger
programLevel slog.LevelVar // Info by default

apiResourcesToWatch []string
)

func init() {
Expand All @@ -52,6 +56,12 @@ func init() {
if os.Getenv("DEBUG") == "true" {
programLevel.Set(slog.LevelDebug)
}

// Parse the trackable resources from the environment
if os.Getenv(APIResourcesToWatchEnv) != "" {
apiResourcesToWatch = strings.Split(os.Getenv(APIResourcesToWatchEnv), ",")
}

}

func main() {
Expand Down Expand Up @@ -116,6 +126,15 @@ func getStartTime(item unstructured.Unstructured) metav1.Time {
return item.GetCreationTimestamp()
}

func contains(slice []string, item string) bool {
for _, s := range slice {
if s == item {
return true
}
}
return false
}

// DoReconcile goes over all API resources specified, retrieves all sub resources and deletes those who have expired
func DoReconcile(dynamicClient dynamic.Interface, eventManager *kevent.EventManager, resources []*metav1.APIResourceList) bool {
for _, resource := range resources {
Expand All @@ -133,6 +152,10 @@ func DoReconcile(dynamicClient dynamic.Interface, eventManager *kevent.EventMana
continue
}
for _, apiResource := range resource.APIResources {
// Skip resources that are not in the list of trackable resources
if len(apiResourcesToWatch) != 0 && !contains(apiResourcesToWatch, apiResource.Name) {
continue
}
// Make sure that we can list and delete the resource. If we can't, then there's no point querying it.
verbs := apiResource.Verbs.String()
if !strings.Contains(verbs, "list") || !strings.Contains(verbs, "delete") {
Expand Down

0 comments on commit 05ab829

Please sign in to comment.