feat: initial commit

This commit is contained in:
2022-09-09 14:16:14 +02:00
commit e6b18c9216
12 changed files with 937 additions and 0 deletions
+50
View File
@@ -0,0 +1,50 @@
package main
import (
"context"
"os"
"github.com/alecthomas/kong"
"github.com/apex/log"
"github.com/apex/log/handlers/json"
"gitlab.com/unboundsoftware/gitlab-cleanup-handler/gitlab"
"gitlab.com/unboundsoftware/gitlab-cleanup-handler/kube"
)
type CLI struct {
Namespaces []string `name:"namespaces" env:"NAMESPACES" help:"Comma-separated list of namespaces to check" required:"true"`
GitlabToken string `name:"gitlab-token" env:"GITLAB_TOKEN" help:"API token to use for communication with Gitlab" required:"true"`
}
var cli CLI
func main() {
_ = kong.Parse(&cli)
log.SetHandler(json.New(os.Stdout))
logger := log.WithField("service", "gitlab-cleanup-handler")
var kubeClient *kube.Client
if kubecfg, exists := os.LookupEnv("KUBECONFIG"); exists {
kubeClient = kube.New(kube.WithKubeConfigProvider(kubecfg))
} else {
kubeClient = kube.New(kube.WithInClusterProvider())
}
gitlabClient := gitlab.New(cli.GitlabToken)
if err := handle(cli, logger, kubeClient, gitlabClient); err != nil {
logger.WithError(err).Error("handler")
}
}
func handle(cli CLI, logger log.Interface, kubeClient *kube.Client, gitlabClient *gitlab.RestClient) error {
images, err := kubeClient.GetImages(context.Background(), logger, cli.Namespaces)
if err != nil {
return err
}
for image, versions := range images {
if err := gitlabClient.UpdateCleanupPolicy(image, versions); err != nil {
return err
}
}
return nil
}