This repository has been archived on 2023-11-25. You can view files and clone it, but cannot push or open issues or pull requests.
kreaper/main.go

56 lines
1.3 KiB
Go
Raw Normal View History

2022-04-13 06:06:23 +00:00
package main
import (
"context"
2022-04-13 12:34:27 +00:00
"flag"
"path/filepath"
"time"
2022-04-13 06:06:23 +00:00
"k8s.io/client-go/rest"
2022-04-13 12:34:27 +00:00
"k8s.io/client-go/tools/clientcmd"
"k8s.io/client-go/util/homedir"
2022-04-13 06:06:23 +00:00
"sigs.k8s.io/controller-runtime/pkg/client"
2022-04-13 12:34:27 +00:00
"github.com/baez90/kreaper/reaper"
)
type ReaperTarget string
var (
kubeconfig string
target reaper.Target
lifetime time.Duration
2022-04-13 06:06:23 +00:00
)
func main() {
2022-04-13 12:34:27 +00:00
prepareFlags()
restCfg, err := loadRestConfig()
2022-04-13 06:06:23 +00:00
if err != nil {
panic(err)
}
k8sClient, err := client.NewWithWatch(restCfg, client.Options{})
labels := client.MatchingLabels{
"from": "value",
}
k8sClient.Watch(context.Background(), nil, labels)
}
2022-04-13 12:34:27 +00:00
func prepareFlags() {
flag.Var(&target, "target", "Target that should be monitored")
flag.DurationVar(&lifetime, "lifetime", 5*time.Minute, "Lifetime after which all matching targets will be deleted")
if home := homedir.HomeDir(); home != "" {
flag.StringVar(&kubeconfig, "kubeconfig", filepath.Join(home, ".kube", "config"), "(optional) absolute path to the kubeconfig file")
} else {
flag.StringVar(&kubeconfig, "kubeconfig", "", "absolute path to the kubeconfig file")
}
}
func loadRestConfig() (cfg *rest.Config, err error) {
if cfg, err = rest.InClusterConfig(); err == nil {
return cfg, nil
}
return clientcmd.BuildConfigFromFlags("", kubeconfig)
}