Skip to content
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

first pass at a garbage collector #481

Merged
merged 5 commits into from
Jul 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions Tiltfile
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,17 @@ k8s_resource(
]
)

k8s_resource(
workload = 'kargo-garbage-collector',
new_name = 'garbage-collector',
labels = ['kargo'],
objects = [
'kargo-garbage-collector:clusterrole',
'kargo-garbage-collector:clusterrolebinding',
'kargo-garbage-collector:serviceaccount'
]
)

k8s_resource(
workload = 'kargo-webhooks-server',
new_name = 'webhooks-server',
Expand Down
8 changes: 8 additions & 0 deletions charts/kargo/templates/_helpers.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ If release name contains chart name it will be used as a full name.
{{ include "kargo.fullname" . | printf "%s-controller" }}
{{- end -}}

{{- define "kargo.garbageCollector.fullname" -}}
{{ include "kargo.fullname" . | printf "%s-garbage-collector" }}
{{- end -}}

{{- define "kargo.webhooksServer.fullname" -}}
{{ include "kargo.fullname" . | printf "%s-webhooks-server" }}
{{- end -}}
Expand Down Expand Up @@ -76,6 +80,10 @@ app.kubernetes.io/component: api
app.kubernetes.io/component: controller
{{- end -}}

{{- define "kargo.garbageCollector.labels" -}}
app.kubernetes.io/component: garbage-collector
{{- end -}}

{{- define "kargo.webhooksServer.labels" -}}
app.kubernetes.io/component: webhooks-server
{{- end -}}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{{- if and .Values.garbageCollector.enabled .Values.rbac.installClusterRoleBindings }}
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
name: {{ include "kargo.garbageCollector.fullname" . }}
labels:
{{- include "kargo.labels" . | nindent 4 }}
{{- include "kargo.garbageCollector.labels" . | nindent 4 }}
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: ClusterRole
name: {{ include "kargo.garbageCollector.fullname" . }}
subjects:
- kind: ServiceAccount
namespace: {{ .Release.Namespace }}
name: {{ include "kargo.garbageCollector.fullname" . }}
{{- end }}
27 changes: 27 additions & 0 deletions charts/kargo/templates/garbage-collector/cluster-roles.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{{- if and .Values.garbageCollector.enabled .Values.rbac.installClusterRoles }}
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
name: {{ include "kargo.garbageCollector.fullname" . }}
labels:
{{- include "kargo.labels" . | nindent 4 }}
{{- include "kargo.garbageCollector.labels" . | nindent 4 }}
rules:
- apiGroups:
- ""
resources:
- namespaces
verbs:
- get
- list
- watch
- apiGroups:
- kargo.akuity.io
resources:
- promotions
verbs:
- delete
- get
- list
- watch
{{- end }}
40 changes: 40 additions & 0 deletions charts/kargo/templates/garbage-collector/cron-job.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
{{- if .Values.garbageCollector.enabled }}
apiVersion: batch/v1
kind: CronJob
metadata:
name: {{ include "kargo.garbageCollector.fullname" . }}
labels:
{{- include "kargo.labels" . | nindent 4 }}
{{- include "kargo.garbageCollector.labels" . | nindent 4 }}
spec:
schedule: {{ quote .Values.garbageCollector.schedule }}
jobTemplate:
spec:
template:
spec:
serviceAccountName: {{ include "kargo.garbageCollector.fullname" . }}
containers:
- name: garbage-collector
image: {{ include "kargo.image" . }}
imagePullPolicy: {{ .Values.image.pullPolicy }}
command: ["/usr/local/bin/kargo", "garbage-collector"]
env:
- name: NUM_WORKERS
value: {{ quote .Values.garbageCollector.workers }}
- name: MAX_RETAINED_PROMOTIONS
value: {{ quote .Values.garbageCollector.maxRetainedPromotions }}
- name: LOG_LEVEL
value: {{ .Values.garbageCollector.logLevel }}
resources:
{{- toYaml .Values.garbageCollector.resources | nindent 14 }}
restartPolicy: Never
concurrencyPolicy: Forbid
{{- with .Values.garbageCollector.nodeSelector }}
nodeSelector:
{{- toYaml . | nindent 12 }}
{{- end }}
{{- with .Values.garbageCollector.tolerations }}
tolerations:
{{- toYaml . | nindent 12 }}
{{- end }}
{{- end }}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{{- if .Values.garbageCollector.enabled }}
apiVersion: v1
kind: ServiceAccount
metadata:
name: {{ include "kargo.garbageCollector.fullname" . }}
labels:
{{- include "kargo.labels" . | nindent 4 }}
{{- include "kargo.garbageCollector.labels" . | nindent 4 }}
{{- end }}
34 changes: 34 additions & 0 deletions charts/kargo/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -156,3 +156,37 @@ webhooksServer:
nodeSelector: {}

tolerations: []

garbageCollector:

## Whether the garbage collector is enabled.
enabled: true

## When to run the garbage collector.
schedule: "0 * * * *"
## The number of concurrent workers to run. Tuning this too low will result in
## slow garbage collection. Tuning this too high will result in too many API
## calls and may result in throttling.
workers: 3
## The maximum number of Promotions in terminal phases PER PROJECT that may be
## spared by the garbage collector.
maxRetainedPromotions: 20

logLevel: INFO

resources: {}
# We usually recommend not to specify default resources and to leave this as
# a conscious choice for the user. This also increases chances charts run on
# environments with little resources, such as KinD. If you do want to
# specify resources, uncomment the following lines, adjust them as
# necessary, and remove the curly braces after 'resources:'.
# limits:
# cpu: 100m
# memory: 128Mi
# requests:
# cpu: 100m
# memory: 128Mi

nodeSelector: {}

tolerations: []
60 changes: 60 additions & 0 deletions cmd/controlplane/garbage_collector.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package main

import (
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/runtime"
"sigs.k8s.io/controller-runtime/pkg/client"

api "github.com/akuity/kargo/api/v1alpha1"
"github.com/akuity/kargo/internal/garbage"
"github.com/akuity/kargo/internal/os"
versionpkg "github.com/akuity/kargo/internal/version"
)

func newGarbageCollectorCommand() *cobra.Command {
return &cobra.Command{
Use: "garbage-collector",
DisableAutoGenTag: true,
SilenceErrors: true,
SilenceUsage: true,
RunE: func(cmd *cobra.Command, args []string) error {
ctx := cmd.Context()

version := versionpkg.GetVersion()
log.WithFields(log.Fields{
"version": version.Version,
"commit": version.GitCommit,
}).Info("Starting Kargo Garbage Collector")

cfg := garbage.CollectorConfigFromEnv()

var kubeClient client.Client
{
restCfg, err := getRestConfig(ctx, os.GetEnv("KUBECONFIG", ""))
if err != nil {
return errors.Wrap(err, "error loading REST config")
}
scheme := runtime.NewScheme()
if err = corev1.AddToScheme(scheme); err != nil {
return errors.Wrap(err, "error adding Kubernetes core API to scheme")
}
if err = api.AddToScheme(scheme); err != nil {
return errors.Wrap(err, "error adding Kargo API to scheme")
}
if kubeClient, err = client.New(
restCfg,
client.Options{
Scheme: scheme,
},
); err != nil {
return errors.Wrap(err, "error initializing Kubernetes client")
}
}

return garbage.NewCollector(kubeClient, cfg).Run(ctx)
},
}
}
1 change: 1 addition & 0 deletions cmd/controlplane/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ var (
func Execute(ctx context.Context) error {
rootCmd.AddCommand(newAPICommand())
rootCmd.AddCommand(newControllerCommand())
rootCmd.AddCommand(newGarbageCollectorCommand())
rootCmd.AddCommand(newVersionCommand())
rootCmd.AddCommand(newWebhooksServerCommand())
return rootCmd.ExecuteContext(ctx)
Expand Down
Loading
Loading