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

feat: Add periodic job support #1474

Closed
wants to merge 61 commits into from
Closed
Show file tree
Hide file tree
Changes from 59 commits
Commits
Show all changes
61 commits
Select commit Hold shift + click to select a range
44074c2
Add periodics controller
dippynark Sep 18, 2022
ee32402
Improve comments
dippynark Sep 18, 2022
a7cca17
Add comments to reconiliation logic
dippynark Sep 18, 2022
2783e6a
Add a
dippynark Sep 18, 2022
89bd2ec
Rename to strobe
dippynark Sep 18, 2022
086be29
Merge branch 'main' into add-periodics-controller
dippynark Sep 24, 2022
098567b
Implement reconciliation and update docs
dippynark Sep 25, 2022
f954770
Merge branch 'main' into add-periodics-controller
dippynark Sep 25, 2022
40472e3
Implement maximum concurrency
dippynark Sep 25, 2022
9160b1f
Update docs
dippynark Sep 25, 2022
c45ca3b
Update Helm chart
dippynark Sep 25, 2022
b2b7532
Add strobe Docker build
dippynark Sep 25, 2022
11c7c20
Fix indent
dippynark Sep 25, 2022
36ebbf0
Fix RBAC
dippynark Sep 25, 2022
17bca56
Fix spelling
dippynark Sep 25, 2022
af48ed3
Add release build
dippynark Sep 25, 2022
03c89e9
Update docs
dippynark Sep 25, 2022
d32e121
Check for nil
dippynark Sep 25, 2022
ecb22d3
Remove print
dippynark Sep 25, 2022
9e4813f
Add comment
dippynark Sep 25, 2022
281bcac
Use UTC time
dippynark Sep 25, 2022
173ecb5
Use periodicJobFirstObserved
dippynark Sep 25, 2022
b894164
Improve ordering and comments
dippynark Sep 25, 2022
222fd39
Improve comments
dippynark Sep 25, 2022
021aa21
Use NamespacedName
dippynark Sep 25, 2022
51b9f92
Fix earliestScheduleTime
dippynark Sep 25, 2022
75c58cb
Fix LighthouseJob generation and add tests
dippynark Sep 26, 2022
ffa809a
Reorder
dippynark Sep 26, 2022
d1db0d8
Add cron parse comment
dippynark Sep 27, 2022
a9c642c
Only enqueue jobs at next schedule time
dippynark Sep 27, 2022
b1e50ce
Check if job has already been triggered
dippynark Sep 28, 2022
7283ce9
Recover from downtime
dippynark Sep 28, 2022
3f54b19
Update docs
dippynark Sep 28, 2022
28d69d0
Update docs
dippynark Sep 28, 2022
30c4fda
Improve recovery comment
dippynark Sep 28, 2022
c745145
Improve recovery comment
dippynark Sep 28, 2022
252a2b8
Improve recovery comment
dippynark Sep 28, 2022
12c117f
Use 0
dippynark Sep 28, 2022
e59242f
Use >0
dippynark Sep 28, 2022
5400640
Merge branch 'main' into add-periodics-controller
dippynark Sep 28, 2022
8dca4b4
Use rolling update
dippynark Sep 28, 2022
8026c48
Use Unix time
dippynark Sep 28, 2022
a0c24ee
Print Unix time properly
dippynark Sep 28, 2022
d75422a
Improve time hashing and explanation
dippynark Sep 29, 2022
04d671c
Improve time hashing and explanation
dippynark Sep 29, 2022
d922423
Fix name suffix
dippynark Sep 29, 2022
59012ad
Improve lock explanation
dippynark Sep 29, 2022
3001c1b
Improve lock explanation
dippynark Sep 29, 2022
4897cb9
Add generateLighthouseJob test
dippynark Sep 29, 2022
0611afc
Add comment
dippynark Sep 29, 2022
17bf289
Use actualLighthouseJob
dippynark Sep 29, 2022
122752b
Fix test file name
dippynark Sep 29, 2022
454b8b4
Remove the
dippynark Sep 29, 2022
c0a2d13
Use common pipelineRunSpec
dippynark Sep 29, 2022
6826cd5
Merge branch 'main' into add-periodics-controller
dippynark Oct 7, 2022
d007a2e
Merge branch 'main' into add-periodics-controller
dippynark Oct 10, 2022
d463bb0
Merge branch 'main' into add-periodics-controller
dippynark Nov 25, 2022
9ca5a16
Merge branch 'main' into add-periodics-controller
dippynark Nov 25, 2022
27c7bdd
Merge branch 'main' into add-periodics-controller
dippynark Jan 11, 2023
e4adcf7
Merge branch 'main' into add-periodics-controller
dippynark May 13, 2023
29c0b9a
Merge branch 'main' into add-periodics-controller
dippynark Jan 9, 2024
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
7 changes: 7 additions & 0 deletions .lighthouse/jenkins-x/pullrequest.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,13 @@ spec:
source .jx/variables.sh
cp /tekton/creds-secrets/tekton-container-registry-auth/.dockerconfigjson /kaniko/.docker/config.json
/kaniko/executor $KANIKO_FLAGS --context=/workspace/source --dockerfile=docker/keeper/Dockerfile --destination=ghcr.io/jenkins-x/lighthouse-keeper:$VERSION --build-arg=VERSION=$VERSION
- name: build-container-build:strobe
resources: {}
script: |
#!/busybox/sh
source .jx/variables.sh
cp /tekton/creds-secrets/tekton-container-registry-auth/.dockerconfigjson /kaniko/.docker/config.json
/kaniko/executor $KANIKO_FLAGS --context=/workspace/source --dockerfile=docker/strobe/Dockerfile --destination=ghcr.io/jenkins-x/lighthouse-strobe:$VERSION --build-arg=VERSION=$VERSION
- name: build-container-build:foghorn
resources: {}
script: |
Expand Down
7 changes: 7 additions & 0 deletions .lighthouse/jenkins-x/release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,13 @@ spec:
source .jx/variables.sh
cp /tekton/creds-secrets/tekton-container-registry-auth/.dockerconfigjson /kaniko/.docker/config.json
/kaniko/executor $KANIKO_FLAGS --context=/workspace/source --dockerfile=docker/keeper/Dockerfile --destination=ghcr.io/jenkins-x/lighthouse-keeper:$VERSION --destination=ghcr.io/jenkins-x/lighthouse-keeper:latest --build-arg=VERSION=$VERSION
- name: build-and-push-image:strobe
resources: {}
script: |
#!/busybox/sh
source .jx/variables.sh
cp /tekton/creds-secrets/tekton-container-registry-auth/.dockerconfigjson /kaniko/.docker/config.json
/kaniko/executor $KANIKO_FLAGS --context=/workspace/source --dockerfile=docker/strobe/Dockerfile --destination=ghcr.io/jenkins-x/lighthouse-strobe:$VERSION --destination=ghcr.io/jenkins-x/lighthouse-strobe:latest --build-arg=VERSION=$VERSION
- name: build-and-push-image:foghorn
resources: {}
script: |
Expand Down
14 changes: 12 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ POLLER_EXECUTABLE := poller
KEEPER_EXECUTABLE := keeper
FOGHORN_EXECUTABLE := foghorn
GC_JOBS_EXECUTABLE := gc-jobs
STROBE_EXECUTABLE := strobe
TEKTON_CONTROLLER_EXECUTABLE := lighthouse-tekton-controller
JENKINS_CONTROLLER_EXECUTABLE := jenkins-controller

Expand All @@ -16,6 +17,7 @@ POLLER_MAIN_SRC_FILE=cmd/poller/main.go
KEEPER_MAIN_SRC_FILE=cmd/keeper/main.go
FOGHORN_MAIN_SRC_FILE=cmd/foghorn/main.go
GC_JOBS_MAIN_SRC_FILE=cmd/gc/main.go
STROBE_MAIN_SRC_FILE=cmd/strobe/main.go
TEKTON_CONTROLLER_MAIN_SRC_FILE=cmd/tektoncontroller/main.go
JENKINS_CONTROLLER_MAIN_SRC_FILE=cmd/jenkins/main.go

Expand All @@ -32,7 +34,7 @@ GO_DEPENDENCIES := $(call rwildcard,pkg/,*.go) $(call rwildcard,cmd/,*.go)
all: build test check docs ## Default rule, builds all binaries, runs tests and format checks

.PHONY: build
build: build-webhooks build-poller build-keeper build-foghorn build-tekton-controller build-gc-jobs build-jenkins-controller ## Builds all Lighthouse binaries native to your machine
build: build-webhooks build-poller build-keeper build-foghorn build-gc-jobs build-periodics build-tekton-controller build-jenkins-controller ## Builds all Lighthouse binaries native to your machine

.PHONY: build-webhooks
build-webhooks: ## Build the webhooks controller binary for the native OS
Expand All @@ -54,6 +56,10 @@ build-foghorn: ## Build the foghorn controller binary for the native OS
build-gc-jobs: ## Build the GC jobs binary for the native OS
$(GO) build -ldflags "$(GO_LDFLAGS)" -o bin/$(GC_JOBS_EXECUTABLE) $(GC_JOBS_MAIN_SRC_FILE)

.PHONY: build-strobe
build-strobe: ## Build the strobe controller binary for the native OS
$(GO) build -i -ldflags "$(GO_LDFLAGS)" -o bin/$(STROBE_EXECUTABLE) $(STROBE_MAIN_SRC_FILE)

.PHONY: build-tekton-controller
build-tekton-controller: ## Build the Tekton controller binary for the native OS
$(GO) build -ldflags "$(GO_LDFLAGS)" -o bin/$(TEKTON_CONTROLLER_EXECUTABLE) $(TEKTON_CONTROLLER_MAIN_SRC_FILE)
Expand All @@ -69,7 +75,7 @@ release: linux
linux: build-linux

.PHONY: build-linux
build-linux: build-webhooks-linux build-poller-linux build-foghorn-linux build-gc-jobs-linux build-keeper-linux build-tekton-controller-linux build-jenkins-controller-linux ## Build all binaries for Linux
build-linux: build-webhooks-linux build-poller-linux build-foghorn-linux build-gc-jobs-linux build-keeper-linux build-strobe-linux build-tekton-controller-linux build-jenkins-controller-linux ## Build all binaries for Linux

.PHONY: build-webhooks-linux ## Build the webhook controller binary for Linux
build-webhooks-linux:
Expand All @@ -91,6 +97,10 @@ build-foghorn-linux: ## Build the foghorn controller binary for Linux
build-gc-jobs-linux: ## Build the GC jobs binary for Linux
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 $(GO) build -ldflags "$(GO_LDFLAGS)" -o bin/$(GC_JOBS_EXECUTABLE) $(GC_JOBS_MAIN_SRC_FILE)

.PHONY: build-strobe-linux
build-strobe-linux: ## Build the strobe controller binary for Linux
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 $(GO) build -ldflags "$(GO_LDFLAGS)" -o bin/$(STROBE_EXECUTABLE) $(STROBE_MAIN_SRC_FILE)

.PHONY: build-tekton-controller-linux
build-tekton-controller-linux: ## Build the Tekton controller binary for Linux
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 $(GO) build -ldflags "$(GO_LDFLAGS)" -o bin/$(TEKTON_CONTROLLER_EXECUTABLE) $(TEKTON_CONTROLLER_MAIN_SRC_FILE)
Expand Down
5 changes: 5 additions & 0 deletions charts/lighthouse/templates/_helpers.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ We truncate at 63 chars because some Kubernetes name fields are limited to this
{{- printf "%s-%s" .Chart.Name $name | trunc 63 | trimSuffix "-" -}}
{{- end -}}

{{- define "strobe.name" -}}
{{- $name := default "strobe" .Values.strobe.nameOverride -}}
{{- printf "%s-%s" .Chart.Name $name | trunc 63 | trimSuffix "-" -}}
{{- end -}}

{{- define "tektoncontroller.name" -}}
{{- $name := default "tekton-controller" .Values.tektoncontroller.nameOverride -}}
{{- printf "%s-%s" .Chart.Name $name | trunc 63 | trimSuffix "-" -}}
Expand Down
53 changes: 53 additions & 0 deletions charts/lighthouse/templates/strobe-deployment.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
{{- if .Values.strobe.enabled }}
apiVersion: apps/v1
kind: Deployment
metadata:
name: {{ template "strobe.name" . }}
labels:
chart: "{{ .Chart.Name }}-{{ .Chart.Version | replace "+" "_" }}"
app: {{ template "strobe.name" . }}
spec:
replicas: {{ .Values.strobe.replicaCount }}
minReadySeconds: 10
selector:
matchLabels:
app: {{ template "strobe.name" . }}
template:
metadata:
labels:
app: {{ template "strobe.name" . }}
{{- if .Values.podAnnotations }}
annotations:
{{ toYaml .Values.podAnnotations | indent 8 }}
{{- end }}
spec:
serviceAccountName: {{ template "strobe.name" . }}
containers:
- name: {{ template "strobe.name" . }}
image: {{ tpl .Values.strobe.image.repository . }}:{{ tpl .Values.strobe.image.tag . }}
imagePullPolicy: {{ tpl .Values.strobe.image.pullPolicy . }}
args:
- "--namespace={{ .Release.Namespace }}"
{{- if hasKey .Values "env" }}
env:
{{- range $pkey, $pval := .Values.env }}
- name: {{ $pkey }}
value: {{ quote $pval }}
{{- end }}
{{- end }}
resources:
{{ toYaml .Values.strobe.resources | indent 10 }}
terminationGracePeriodSeconds: {{ .Values.strobe.terminationGracePeriodSeconds }}
{{- with .Values.strobe.nodeSelector }}
nodeSelector:
{{ toYaml . | indent 8 }}
{{- end }}
{{- with .Values.strobe.affinity }}
affinity:
{{ toYaml . | indent 8 }}
{{- end }}
{{- with .Values.strobe.tolerations }}
tolerations:
{{ toYaml . | indent 8 }}
{{- end }}
{{- end }}
13 changes: 13 additions & 0 deletions charts/lighthouse/templates/strobe-rb.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{{- if .Values.strobe.enabled }}
kind: RoleBinding
apiVersion: rbac.authorization.k8s.io/v1
metadata:
name: {{ template "strobe.name" . }}
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: Role
name: {{ template "strobe.name" . }}
subjects:
- kind: ServiceAccount
name: {{ template "strobe.name" . }}
{{- end }}
28 changes: 28 additions & 0 deletions charts/lighthouse/templates/strobe-role.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{{- if .Values.strobe.enabled }}
kind: Role
apiVersion: rbac.authorization.k8s.io/v1
metadata:
name: {{ template "strobe.name" . }}
rules:
- apiGroups:
- ""
resources:
- configmaps
verbs:
- get
- list
- watch
- apiGroups:
- lighthouse.jenkins.io
resources:
- lighthousejobs
verbs:
- list
- create
- apiGroups:
- lighthouse.jenkins.io
resources:
- lighthousejobs/status
verbs:
- update
{{- end }}
6 changes: 6 additions & 0 deletions charts/lighthouse/templates/strobe-sa.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{{- if .Values.strobe.enabled }}
kind: ServiceAccount
apiVersion: v1
metadata:
name: {{ template "strobe.name" . }}
{{- end }}
39 changes: 39 additions & 0 deletions charts/lighthouse/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,45 @@ foghorn:
# foghorn.tolerations -- [Tolerations](https://kubernetes.io/docs/concepts/scheduling-eviction/taint-and-toleration/) applied to the foghorn pods
tolerations: []

strobe:
# strobe.enabled -- Whether to enable the strobe controller
enabled: false

# strobe.replicaCount -- Number of replicas of the strobe controller
replicaCount: 1

image:
# strobe.image.repository -- Template for computing the strobe controller docker image repository
repository: "{{ .Values.image.parentRepository }}/lighthouse-strobe"

# strobe.image.tag -- Template for computing the strobe controller docker image tag
tag: "{{ .Values.image.tag }}"

# strobe.image.pullPolicy -- Template for computing the strobe controller docker image pull policy
pullPolicy: "{{ .Values.image.pullPolicy }}"

resources:
# strobe.resources.requests -- Resource requests applied to the strobe controller pods
requests:
cpu: 80m
memory: 128Mi

# strobe.resources.limits -- Resource limits applied to the strobe controller pods
limits:
cpu: 100m
memory: 256Mi

# strobe.terminationGracePeriodSeconds -- Termination grace period for the strobe controller pods
terminationGracePeriodSeconds: 1

# strobe.nodeSelector -- [Node selector](https://kubernetes.io/docs/concepts/scheduling-eviction/assign-pod-node/#nodeselector) applied to the strobe controller pods
nodeSelector: {}

# strobe.affinity -- [Affinity rules](https://kubernetes.io/docs/concepts/scheduling-eviction/assign-pod-node/#affinity-and-anti-affinity) applied to the strobe controller pods
affinity: {}

# strobe.tolerations -- [Tolerations](https://kubernetes.io/docs/concepts/scheduling-eviction/taint-and-toleration/) applied to the strobe controller pods
tolerations: []

tektoncontroller:
# logLevel -- The logging level: trace, debug, info, warn, error, fatal
Expand Down
27 changes: 27 additions & 0 deletions cmd/strobe/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Strobe

Strobe is a controller that implements the periodic jobs defined in the
Lighthouse config ConfigMap:

```yaml
Copy link
Member

@ankitm123 ankitm123 Oct 3, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure if it should be defined in a configmap. Normally in jx, configmap would be defined/configured in the cluster git repo, which should be normally off limits for repository owners. It should be in the same repo, so if a repository author wants to define a periodics, they should be able to define a new periodics job in the triggers yaml, wdyt?
May be a block like pre-submit/post-submit?
Also the lifecycle of lighthouse configmap and a build job are very different.

Copy link
Contributor Author

@dippynark dippynark Oct 7, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ankitm123 By triggers yaml do you mean the TriggerConfig resource? https://github.com/dippynark/kfmt/blob/main/.lighthouse/triggers.yaml

How would the controller get access to this TriggerConfig in all registered repositories? Following through the webhook code, this is done for presubmit and postsubmit jobs when a SCM event is received for a specific repository:

  • agent, err := s.CreateAgent(l, ce.Repo.Namespace, ce.Repo.Name, ce.HeadSha)
  • pc.Config, pc.PluginConfig, err = inrepo.Generate(s.FileBrowsers, fc, cache, pc.Config, pc.PluginConfig, owner, repo, ref)
  • // Generate generates the in repository config if enabled for this repository otherwise return the shared config
    func Generate(fileBrowsers *filebrowser.FileBrowsers, fc filebrowser.FetchCache, cache *ResolverCache, sharedConfig *config.Config, sharedPlugins *plugins.Configuration, owner, repo, eventRef string) (*config.Config, *plugins.Configuration, error) {

But of course periodic jobs don't have any associated SCM events since they are triggered on a timer. One option could be to add a new webhook plugin which persists periodic job configuration by syncing TriggerConfig periodic jobs to a ConfigMap which the controller then picks up? But I guess that work could be done in a subsequent PR

Copy link
Contributor Author

@dippynark dippynark Oct 7, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also sorry to reference other repositories, but would it be possible to get a review for this PR please? jenkins-x/go-scm#321
We depend on the poller component being able to see if a particular job has ever succeeded (rather than just the latest run) which is the reason for the change. We are using a custom fork using that PR at the moment

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the approval! I have created another PR to bump the go-scm version used by Lighthouse: #1493

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suppose the periodics would be registered similar to how pipelines are cached (but periodics will store instead). It being registered within the cluster repo goes away from the jenkins-x ideaollogy of being repo and developer centric as it would make you have to jump through 2 repos to add a feature to your repo

periodics:
- name: hello-world
cron: "*/1 * * * *"
agent: tekton-pipeline
pipeline_run_spec:
pipelineSpec:
tasks:
- name: hello-world
taskSpec:
steps:
- image: busybox
script: echo 'Hello World!'
```

This is done by watching the ConfigMap and processing each periodic job by name.
Inspiration is taken from the [Kubernetes CronJob
controller](https://github.com/kubernetes/kubernetes/blob/v1.25.2/pkg/controller/cronjob/cronjob_controllerv2.go).

Note that if Strobe misses a schedule time for a particular periodic job due to
crashing or being restarted it will attempt to schedule a job only for the last
missed time.
Loading