flickr-immich-k8s-sync-operator 0.0.1__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- flickr_immich_k8s_sync_operator-0.0.1/.gitignore +149 -0
- flickr_immich_k8s_sync_operator-0.0.1/LICENSE.md +163 -0
- flickr_immich_k8s_sync_operator-0.0.1/PKG-INFO +210 -0
- flickr_immich_k8s_sync_operator-0.0.1/README.md +188 -0
- flickr_immich_k8s_sync_operator-0.0.1/flickr_immich_k8s_sync_operator/__init__.py +27 -0
- flickr_immich_k8s_sync_operator-0.0.1/flickr_immich_k8s_sync_operator/__main__.py +47 -0
- flickr_immich_k8s_sync_operator-0.0.1/flickr_immich_k8s_sync_operator/config.py +51 -0
- flickr_immich_k8s_sync_operator-0.0.1/flickr_immich_k8s_sync_operator/operator.py +194 -0
- flickr_immich_k8s_sync_operator-0.0.1/flickr_immich_k8s_sync_operator/py.typed +0 -0
- flickr_immich_k8s_sync_operator-0.0.1/pyproject.toml +53 -0
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
.claude
|
|
2
|
+
|
|
3
|
+
/docker-config
|
|
4
|
+
|
|
5
|
+
*.local.*
|
|
6
|
+
|
|
7
|
+
.idea/
|
|
8
|
+
|
|
9
|
+
# Byte-compiled / optimized / DLL files
|
|
10
|
+
__pycache__/
|
|
11
|
+
*.py[cod]
|
|
12
|
+
*$py.class
|
|
13
|
+
|
|
14
|
+
# C extensions
|
|
15
|
+
*.so
|
|
16
|
+
|
|
17
|
+
# Distribution / packaging
|
|
18
|
+
.Python
|
|
19
|
+
build/
|
|
20
|
+
develop-eggs/
|
|
21
|
+
dist/
|
|
22
|
+
downloads/
|
|
23
|
+
eggs/
|
|
24
|
+
.eggs/
|
|
25
|
+
lib/
|
|
26
|
+
lib64/
|
|
27
|
+
parts/
|
|
28
|
+
sdist/
|
|
29
|
+
var/
|
|
30
|
+
wheels/
|
|
31
|
+
share/python-wheels/
|
|
32
|
+
*.egg-info/
|
|
33
|
+
.installed.cfg
|
|
34
|
+
*.egg
|
|
35
|
+
MANIFEST
|
|
36
|
+
|
|
37
|
+
# PyInstaller
|
|
38
|
+
# Usually these files are written by a python script from a template
|
|
39
|
+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
|
|
40
|
+
*.manifest
|
|
41
|
+
*.spec
|
|
42
|
+
|
|
43
|
+
# Installer logs
|
|
44
|
+
pip-log.txt
|
|
45
|
+
pip-delete-this-directory.txt
|
|
46
|
+
|
|
47
|
+
# Unit test / coverage reports
|
|
48
|
+
htmlcov/
|
|
49
|
+
.tox/
|
|
50
|
+
.nox/
|
|
51
|
+
.coverage
|
|
52
|
+
.coverage.*
|
|
53
|
+
.cache
|
|
54
|
+
nosetests.xml
|
|
55
|
+
coverage.xml
|
|
56
|
+
*.cover
|
|
57
|
+
*.py,cover
|
|
58
|
+
.hypothesis/
|
|
59
|
+
.pytest_cache/
|
|
60
|
+
cover/
|
|
61
|
+
|
|
62
|
+
# Translations
|
|
63
|
+
*.mo
|
|
64
|
+
*.pot
|
|
65
|
+
|
|
66
|
+
# Django stuff:
|
|
67
|
+
*.log
|
|
68
|
+
local_settings.py
|
|
69
|
+
db.sqlite3
|
|
70
|
+
db.sqlite3-journal
|
|
71
|
+
|
|
72
|
+
# Flask stuff:
|
|
73
|
+
instance/
|
|
74
|
+
.webassets-cache
|
|
75
|
+
|
|
76
|
+
# Scrapy stuff:
|
|
77
|
+
.scrapy
|
|
78
|
+
|
|
79
|
+
# Sphinx documentation
|
|
80
|
+
docs/_build/
|
|
81
|
+
|
|
82
|
+
# PyBuilder
|
|
83
|
+
.pybuilder/
|
|
84
|
+
target/
|
|
85
|
+
|
|
86
|
+
# Jupyter Notebook
|
|
87
|
+
.ipynb_checkpoints
|
|
88
|
+
|
|
89
|
+
# IPython
|
|
90
|
+
profile_default/
|
|
91
|
+
ipython_config.py
|
|
92
|
+
|
|
93
|
+
# pyenv
|
|
94
|
+
# .python-version
|
|
95
|
+
|
|
96
|
+
# pipenv
|
|
97
|
+
#Pipfile.lock
|
|
98
|
+
|
|
99
|
+
# poetry
|
|
100
|
+
#poetry.lock
|
|
101
|
+
|
|
102
|
+
# pdm
|
|
103
|
+
.pdm.toml
|
|
104
|
+
|
|
105
|
+
# PEP 582
|
|
106
|
+
__pypackages__/
|
|
107
|
+
|
|
108
|
+
# Celery stuff
|
|
109
|
+
celerybeat-schedule
|
|
110
|
+
celerybeat.pid
|
|
111
|
+
|
|
112
|
+
# SageMath parsed files
|
|
113
|
+
*.sage.py
|
|
114
|
+
|
|
115
|
+
# Environments
|
|
116
|
+
.env
|
|
117
|
+
.venv
|
|
118
|
+
env/
|
|
119
|
+
venv/
|
|
120
|
+
ENV/
|
|
121
|
+
env.bak/
|
|
122
|
+
venv.bak/
|
|
123
|
+
|
|
124
|
+
# Spyder project settings
|
|
125
|
+
.spyderproject
|
|
126
|
+
.spyproject
|
|
127
|
+
|
|
128
|
+
# Rope project settings
|
|
129
|
+
.ropeproject
|
|
130
|
+
|
|
131
|
+
# mkdocs documentation
|
|
132
|
+
/site
|
|
133
|
+
|
|
134
|
+
# mypy
|
|
135
|
+
.mypy_cache/
|
|
136
|
+
.dmypy.json
|
|
137
|
+
dmypy.json
|
|
138
|
+
|
|
139
|
+
# Pyre type checker
|
|
140
|
+
.pyre/
|
|
141
|
+
|
|
142
|
+
# pytype static type analyzer
|
|
143
|
+
.pytype/
|
|
144
|
+
|
|
145
|
+
# Cython debug symbols
|
|
146
|
+
cython_debug/
|
|
147
|
+
|
|
148
|
+
# Local overrides
|
|
149
|
+
*.local.*
|
|
@@ -0,0 +1,163 @@
|
|
|
1
|
+
GNU Lesser General Public License
|
|
2
|
+
=================================
|
|
3
|
+
|
|
4
|
+
_Version 3, 29 June 2007_
|
|
5
|
+
_Copyright © 2007 Free Software Foundation, Inc. <<http://fsf.org/>>_
|
|
6
|
+
|
|
7
|
+
Everyone is permitted to copy and distribute verbatim copies
|
|
8
|
+
of this license document, but changing it is not allowed.
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
This version of the GNU Lesser General Public License incorporates
|
|
12
|
+
the terms and conditions of version 3 of the GNU General Public
|
|
13
|
+
License, supplemented by the additional permissions listed below.
|
|
14
|
+
|
|
15
|
+
### 0. Additional Definitions
|
|
16
|
+
|
|
17
|
+
As used herein, “this License” refers to version 3 of the GNU Lesser
|
|
18
|
+
General Public License, and the “GNU GPL” refers to version 3 of the GNU
|
|
19
|
+
General Public License.
|
|
20
|
+
|
|
21
|
+
“The Library” refers to a covered work governed by this License,
|
|
22
|
+
other than an Application or a Combined Work as defined below.
|
|
23
|
+
|
|
24
|
+
An “Application” is any work that makes use of an interface provided
|
|
25
|
+
by the Library, but which is not otherwise based on the Library.
|
|
26
|
+
Defining a subclass of a class defined by the Library is deemed a mode
|
|
27
|
+
of using an interface provided by the Library.
|
|
28
|
+
|
|
29
|
+
A “Combined Work” is a work produced by combining or linking an
|
|
30
|
+
Application with the Library. The particular version of the Library
|
|
31
|
+
with which the Combined Work was made is also called the “Linked
|
|
32
|
+
Version”.
|
|
33
|
+
|
|
34
|
+
The “Minimal Corresponding Source” for a Combined Work means the
|
|
35
|
+
Corresponding Source for the Combined Work, excluding any source code
|
|
36
|
+
for portions of the Combined Work that, considered in isolation, are
|
|
37
|
+
based on the Application, and not on the Linked Version.
|
|
38
|
+
|
|
39
|
+
The “Corresponding Application Code” for a Combined Work means the
|
|
40
|
+
object code and/or source code for the Application, including any data
|
|
41
|
+
and utility programs needed for reproducing the Combined Work from the
|
|
42
|
+
Application, but excluding the System Libraries of the Combined Work.
|
|
43
|
+
|
|
44
|
+
### 1. Exception to Section 3 of the GNU GPL
|
|
45
|
+
|
|
46
|
+
You may convey a covered work under sections 3 and 4 of this License
|
|
47
|
+
without being bound by section 3 of the GNU GPL.
|
|
48
|
+
|
|
49
|
+
### 2. Conveying Modified Versions
|
|
50
|
+
|
|
51
|
+
If you modify a copy of the Library, and, in your modifications, a
|
|
52
|
+
facility refers to a function or data to be supplied by an Application
|
|
53
|
+
that uses the facility (other than as an argument passed when the
|
|
54
|
+
facility is invoked), then you may convey a copy of the modified
|
|
55
|
+
version:
|
|
56
|
+
|
|
57
|
+
* **a)** under this License, provided that you make a good faith effort to
|
|
58
|
+
ensure that, in the event an Application does not supply the
|
|
59
|
+
function or data, the facility still operates, and performs
|
|
60
|
+
whatever part of its purpose remains meaningful, or
|
|
61
|
+
|
|
62
|
+
* **b)** under the GNU GPL, with none of the additional permissions of
|
|
63
|
+
this License applicable to that copy.
|
|
64
|
+
|
|
65
|
+
### 3. Object Code Incorporating Material from Library Header Files
|
|
66
|
+
|
|
67
|
+
The object code form of an Application may incorporate material from
|
|
68
|
+
a header file that is part of the Library. You may convey such object
|
|
69
|
+
code under terms of your choice, provided that, if the incorporated
|
|
70
|
+
material is not limited to numerical parameters, data structure
|
|
71
|
+
layouts and accessors, or small macros, inline functions and templates
|
|
72
|
+
(ten or fewer lines in length), you do both of the following:
|
|
73
|
+
|
|
74
|
+
* **a)** Give prominent notice with each copy of the object code that the
|
|
75
|
+
Library is used in it and that the Library and its use are
|
|
76
|
+
covered by this License.
|
|
77
|
+
* **b)** Accompany the object code with a copy of the GNU GPL and this license
|
|
78
|
+
document.
|
|
79
|
+
|
|
80
|
+
### 4. Combined Works
|
|
81
|
+
|
|
82
|
+
You may convey a Combined Work under terms of your choice that,
|
|
83
|
+
taken together, effectively do not restrict modification of the
|
|
84
|
+
portions of the Library contained in the Combined Work and reverse
|
|
85
|
+
engineering for debugging such modifications, if you also do each of
|
|
86
|
+
the following:
|
|
87
|
+
|
|
88
|
+
* **a)** Give prominent notice with each copy of the Combined Work that
|
|
89
|
+
the Library is used in it and that the Library and its use are
|
|
90
|
+
covered by this License.
|
|
91
|
+
|
|
92
|
+
* **b)** Accompany the Combined Work with a copy of the GNU GPL and this license
|
|
93
|
+
document.
|
|
94
|
+
|
|
95
|
+
* **c)** For a Combined Work that displays copyright notices during
|
|
96
|
+
execution, include the copyright notice for the Library among
|
|
97
|
+
these notices, as well as a reference directing the user to the
|
|
98
|
+
copies of the GNU GPL and this license document.
|
|
99
|
+
|
|
100
|
+
* **d)** Do one of the following:
|
|
101
|
+
- **0)** Convey the Minimal Corresponding Source under the terms of this
|
|
102
|
+
License, and the Corresponding Application Code in a form
|
|
103
|
+
suitable for, and under terms that permit, the user to
|
|
104
|
+
recombine or relink the Application with a modified version of
|
|
105
|
+
the Linked Version to produce a modified Combined Work, in the
|
|
106
|
+
manner specified by section 6 of the GNU GPL for conveying
|
|
107
|
+
Corresponding Source.
|
|
108
|
+
- **1)** Use a suitable shared library mechanism for linking with the
|
|
109
|
+
Library. A suitable mechanism is one that **(a)** uses at run time
|
|
110
|
+
a copy of the Library already present on the user's computer
|
|
111
|
+
system, and **(b)** will operate properly with a modified version
|
|
112
|
+
of the Library that is interface-compatible with the Linked
|
|
113
|
+
Version.
|
|
114
|
+
|
|
115
|
+
* **e)** Provide Installation Information, but only if you would otherwise
|
|
116
|
+
be required to provide such information under section 6 of the
|
|
117
|
+
GNU GPL, and only to the extent that such information is
|
|
118
|
+
necessary to install and execute a modified version of the
|
|
119
|
+
Combined Work produced by recombining or relinking the
|
|
120
|
+
Application with a modified version of the Linked Version. (If
|
|
121
|
+
you use option **4d0**, the Installation Information must accompany
|
|
122
|
+
the Minimal Corresponding Source and Corresponding Application
|
|
123
|
+
Code. If you use option **4d1**, you must provide the Installation
|
|
124
|
+
Information in the manner specified by section 6 of the GNU GPL
|
|
125
|
+
for conveying Corresponding Source.)
|
|
126
|
+
|
|
127
|
+
### 5. Combined Libraries
|
|
128
|
+
|
|
129
|
+
You may place library facilities that are a work based on the
|
|
130
|
+
Library side by side in a single library together with other library
|
|
131
|
+
facilities that are not Applications and are not covered by this
|
|
132
|
+
License, and convey such a combined library under terms of your
|
|
133
|
+
choice, if you do both of the following:
|
|
134
|
+
|
|
135
|
+
* **a)** Accompany the combined library with a copy of the same work based
|
|
136
|
+
on the Library, uncombined with any other library facilities,
|
|
137
|
+
conveyed under the terms of this License.
|
|
138
|
+
* **b)** Give prominent notice with the combined library that part of it
|
|
139
|
+
is a work based on the Library, and explaining where to find the
|
|
140
|
+
accompanying uncombined form of the same work.
|
|
141
|
+
|
|
142
|
+
### 6. Revised Versions of the GNU Lesser General Public License
|
|
143
|
+
|
|
144
|
+
The Free Software Foundation may publish revised and/or new versions
|
|
145
|
+
of the GNU Lesser General Public License from time to time. Such new
|
|
146
|
+
versions will be similar in spirit to the present version, but may
|
|
147
|
+
differ in detail to address new problems or concerns.
|
|
148
|
+
|
|
149
|
+
Each version is given a distinguishing version number. If the
|
|
150
|
+
Library as you received it specifies that a certain numbered version
|
|
151
|
+
of the GNU Lesser General Public License “or any later version”
|
|
152
|
+
applies to it, you have the option of following the terms and
|
|
153
|
+
conditions either of that published version or of any later version
|
|
154
|
+
published by the Free Software Foundation. If the Library as you
|
|
155
|
+
received it does not specify a version number of the GNU Lesser
|
|
156
|
+
General Public License, you may choose any version of the GNU Lesser
|
|
157
|
+
General Public License ever published by the Free Software Foundation.
|
|
158
|
+
|
|
159
|
+
If the Library as you received it specifies that a proxy can decide
|
|
160
|
+
whether future versions of the GNU Lesser General Public License shall
|
|
161
|
+
apply, that proxy's public statement of acceptance of any version is
|
|
162
|
+
permanent authorization for you to choose that version for the
|
|
163
|
+
Library.
|
|
@@ -0,0 +1,210 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: flickr-immich-k8s-sync-operator
|
|
3
|
+
Version: 0.0.1
|
|
4
|
+
Summary: Operator for syncing photos from Flickr to Immich on Kubernetes
|
|
5
|
+
Project-URL: Homepage, https://github.com/vroomfondel/flickr-immich-k8s-sync-operator
|
|
6
|
+
Project-URL: Repository, https://github.com/vroomfondel/flickr-immich-k8s-sync-operator
|
|
7
|
+
Project-URL: Issues, https://github.com/vroomfondel/flickr-immich-k8s-sync-operator/issues
|
|
8
|
+
Author-email: Henning Thieß <ht@xomox.cc>
|
|
9
|
+
License-File: LICENSE.md
|
|
10
|
+
Keywords: flickr,immich,k8s,kubernetes,operator,photo-management,self-hosted,sync
|
|
11
|
+
Classifier: Development Status :: 4 - Beta
|
|
12
|
+
Classifier: License :: OSI Approved :: GNU Lesser General Public License v3 (LGPLv3)
|
|
13
|
+
Classifier: Operating System :: OS Independent
|
|
14
|
+
Classifier: Programming Language :: Python :: 3
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
16
|
+
Classifier: Topic :: Multimedia :: Graphics
|
|
17
|
+
Classifier: Topic :: System :: Archiving
|
|
18
|
+
Requires-Python: >=3.14
|
|
19
|
+
Requires-Dist: kubernetes>=28.1.0
|
|
20
|
+
Requires-Dist: loguru>=0.7.3
|
|
21
|
+
Description-Content-Type: text/markdown
|
|
22
|
+
|
|
23
|
+
# flickr-immich-k8s-sync-operator
|
|
24
|
+
|
|
25
|
+
[](https://github.com/vroomfondel/flickr-immich-k8s-sync-operator/actions/workflows/checkblack.yml)
|
|
26
|
+
[](https://github.com/vroomfondel/flickr-immich-k8s-sync-operator/actions/workflows/mypynpytests.yml)
|
|
27
|
+

|
|
28
|
+
[](https://hub.docker.com/r/xomoxcc/flickr-immich-k8s-sync-operator/tags)
|
|
29
|
+
|
|
30
|
+
Kubernetes operator that watches per-user Flickr download Jobs in a namespace,
|
|
31
|
+
restarts failed Jobs after a configurable delay, and retrieves pod logs and exit
|
|
32
|
+
codes for failed Jobs before restarting them. Designed to run alongside
|
|
33
|
+
[Immich](https://immich.app/) (self-hosted photo management).
|
|
34
|
+
|
|
35
|
+
## Status
|
|
36
|
+
|
|
37
|
+
**Beta (v0.0.1)** — the core job-restart loop is implemented and functional.
|
|
38
|
+
The project scaffolding (packaging, Docker image, CI) is in place.
|
|
39
|
+
|
|
40
|
+
## Architecture
|
|
41
|
+
|
|
42
|
+
- Runs as a single-replica **Deployment** in a dedicated namespace (default: `flickr-downloader`)
|
|
43
|
+
- Uses the **Kubernetes Python client** with in-cluster config
|
|
44
|
+
- Periodically checks configured Job names for failure conditions
|
|
45
|
+
- On failure (after a configurable delay), **deletes** the Job with `Foreground` propagation policy and **recreates** it from a cached manifest
|
|
46
|
+
- Logs pod exit codes and tail logs before every restart
|
|
47
|
+
|
|
48
|
+
## Prerequisites
|
|
49
|
+
|
|
50
|
+
- A running Kubernetes cluster
|
|
51
|
+
- Flickr download Jobs already deployed — the operator manages their lifecycle (restart on failure), not initial creation
|
|
52
|
+
- An [Immich](https://immich.app/) instance (for planned sync functionality)
|
|
53
|
+
|
|
54
|
+
## Configuration
|
|
55
|
+
|
|
56
|
+
| Variable | Description | Default |
|
|
57
|
+
|---|---|---|
|
|
58
|
+
| `LOGURU_LEVEL` | Log verbosity (`DEBUG`, `INFO`, `WARNING`, …) | `DEBUG` |
|
|
59
|
+
| `NAMESPACE` | Namespace to watch | `flickr-downloader` |
|
|
60
|
+
| `JOB_NAMES` | Comma-separated Job names to monitor (**required**) | — |
|
|
61
|
+
| `CHECK_INTERVAL` | Seconds between check cycles | `60` |
|
|
62
|
+
| `RESTART_DELAY` | Seconds to wait after failure before restart | `3600` |
|
|
63
|
+
|
|
64
|
+
## Kubernetes Deployment
|
|
65
|
+
|
|
66
|
+
### RBAC
|
|
67
|
+
|
|
68
|
+
The operator requires a ServiceAccount with a Role scoped to the target namespace:
|
|
69
|
+
|
|
70
|
+
```yaml
|
|
71
|
+
apiVersion: v1
|
|
72
|
+
kind: ServiceAccount
|
|
73
|
+
metadata:
|
|
74
|
+
name: flickr-operator
|
|
75
|
+
namespace: flickr-downloader
|
|
76
|
+
---
|
|
77
|
+
apiVersion: rbac.authorization.k8s.io/v1
|
|
78
|
+
kind: Role
|
|
79
|
+
metadata:
|
|
80
|
+
name: flickr-operator
|
|
81
|
+
namespace: flickr-downloader
|
|
82
|
+
rules:
|
|
83
|
+
- apiGroups: ["batch"]
|
|
84
|
+
resources: ["jobs"]
|
|
85
|
+
verbs: ["get", "list", "create", "delete"]
|
|
86
|
+
- apiGroups: [""]
|
|
87
|
+
resources: ["pods"]
|
|
88
|
+
verbs: ["get", "list", "delete"]
|
|
89
|
+
- apiGroups: [""]
|
|
90
|
+
resources: ["pods/log"]
|
|
91
|
+
verbs: ["get"]
|
|
92
|
+
---
|
|
93
|
+
apiVersion: rbac.authorization.k8s.io/v1
|
|
94
|
+
kind: RoleBinding
|
|
95
|
+
metadata:
|
|
96
|
+
name: flickr-operator
|
|
97
|
+
namespace: flickr-downloader
|
|
98
|
+
roleRef:
|
|
99
|
+
apiGroup: rbac.authorization.k8s.io
|
|
100
|
+
kind: Role
|
|
101
|
+
name: flickr-operator
|
|
102
|
+
subjects:
|
|
103
|
+
- kind: ServiceAccount
|
|
104
|
+
name: flickr-operator
|
|
105
|
+
namespace: flickr-downloader
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
### Deployment
|
|
109
|
+
|
|
110
|
+
```yaml
|
|
111
|
+
apiVersion: apps/v1
|
|
112
|
+
kind: Deployment
|
|
113
|
+
metadata:
|
|
114
|
+
name: flickr-operator
|
|
115
|
+
namespace: flickr-downloader
|
|
116
|
+
spec:
|
|
117
|
+
replicas: 1
|
|
118
|
+
selector:
|
|
119
|
+
matchLabels:
|
|
120
|
+
app: flickr-operator
|
|
121
|
+
template:
|
|
122
|
+
metadata:
|
|
123
|
+
labels:
|
|
124
|
+
app: flickr-operator
|
|
125
|
+
spec:
|
|
126
|
+
serviceAccountName: flickr-operator
|
|
127
|
+
containers:
|
|
128
|
+
- name: operator
|
|
129
|
+
image: flickr-immich-k8s-sync-operator:latest
|
|
130
|
+
env:
|
|
131
|
+
- name: JOB_NAMES
|
|
132
|
+
value: "flickr-downloader-alice,flickr-downloader-bob"
|
|
133
|
+
- name: LOGURU_LEVEL
|
|
134
|
+
value: "DEBUG"
|
|
135
|
+
# - name: NAMESPACE
|
|
136
|
+
# value: "flickr-downloader" # default
|
|
137
|
+
# - name: CHECK_INTERVAL
|
|
138
|
+
# value: "60" # default
|
|
139
|
+
# - name: RESTART_DELAY
|
|
140
|
+
# value: "3600" # default
|
|
141
|
+
resources:
|
|
142
|
+
requests:
|
|
143
|
+
cpu: 50m
|
|
144
|
+
memory: 64Mi
|
|
145
|
+
limits:
|
|
146
|
+
cpu: 1500m
|
|
147
|
+
memory: 128Mi
|
|
148
|
+
```
|
|
149
|
+
|
|
150
|
+
## Installation
|
|
151
|
+
|
|
152
|
+
### From PyPI
|
|
153
|
+
|
|
154
|
+
```bash
|
|
155
|
+
pip install flickr-immich-k8s-sync-operator
|
|
156
|
+
```
|
|
157
|
+
|
|
158
|
+
### From source
|
|
159
|
+
|
|
160
|
+
```bash
|
|
161
|
+
git clone https://github.com/vroomfondel/flickr-immich-k8s-sync-operator.git
|
|
162
|
+
cd flickr-immich-k8s-sync-operator
|
|
163
|
+
make venv
|
|
164
|
+
source .venv/bin/activate
|
|
165
|
+
pip install .
|
|
166
|
+
```
|
|
167
|
+
|
|
168
|
+
### Docker
|
|
169
|
+
|
|
170
|
+
```bash
|
|
171
|
+
docker build -t flickr-immich-k8s-sync-operator .
|
|
172
|
+
docker run --rm flickr-immich-k8s-sync-operator
|
|
173
|
+
```
|
|
174
|
+
|
|
175
|
+
Or via Makefile:
|
|
176
|
+
|
|
177
|
+
```bash
|
|
178
|
+
make docker
|
|
179
|
+
```
|
|
180
|
+
|
|
181
|
+
## Usage
|
|
182
|
+
|
|
183
|
+
```bash
|
|
184
|
+
# Run directly
|
|
185
|
+
flickr-immich-k8s-sync-operator
|
|
186
|
+
|
|
187
|
+
# Or via Python module
|
|
188
|
+
python -m flickr_immich_k8s_sync_operator
|
|
189
|
+
```
|
|
190
|
+
|
|
191
|
+
## Development
|
|
192
|
+
|
|
193
|
+
### Makefile targets
|
|
194
|
+
|
|
195
|
+
| Target | Description |
|
|
196
|
+
|-----------------|----------------------------------------------|
|
|
197
|
+
| `make venv` | Create virtualenv and install all dependencies |
|
|
198
|
+
| `make tests` | Run pytest |
|
|
199
|
+
| `make lint` | Format code with black (line length 120) |
|
|
200
|
+
| `make isort` | Sort imports with isort |
|
|
201
|
+
| `make tcheck` | Static type checking with mypy |
|
|
202
|
+
| `make commit-checks` | Run pre-commit hooks on all files |
|
|
203
|
+
| `make prepare` | Run tests + commit-checks |
|
|
204
|
+
| `make pypibuild`| Build sdist + wheel with hatch |
|
|
205
|
+
| `make pypipush` | Publish to PyPI with hatch |
|
|
206
|
+
| `make docker` | Build Docker image |
|
|
207
|
+
|
|
208
|
+
## License
|
|
209
|
+
|
|
210
|
+
[GNU Lesser General Public License v3](LICENSE.md)
|
|
@@ -0,0 +1,188 @@
|
|
|
1
|
+
# flickr-immich-k8s-sync-operator
|
|
2
|
+
|
|
3
|
+
[](https://github.com/vroomfondel/flickr-immich-k8s-sync-operator/actions/workflows/checkblack.yml)
|
|
4
|
+
[](https://github.com/vroomfondel/flickr-immich-k8s-sync-operator/actions/workflows/mypynpytests.yml)
|
|
5
|
+

|
|
6
|
+
[](https://hub.docker.com/r/xomoxcc/flickr-immich-k8s-sync-operator/tags)
|
|
7
|
+
|
|
8
|
+
Kubernetes operator that watches per-user Flickr download Jobs in a namespace,
|
|
9
|
+
restarts failed Jobs after a configurable delay, and retrieves pod logs and exit
|
|
10
|
+
codes for failed Jobs before restarting them. Designed to run alongside
|
|
11
|
+
[Immich](https://immich.app/) (self-hosted photo management).
|
|
12
|
+
|
|
13
|
+
## Status
|
|
14
|
+
|
|
15
|
+
**Beta (v0.0.1)** — the core job-restart loop is implemented and functional.
|
|
16
|
+
The project scaffolding (packaging, Docker image, CI) is in place.
|
|
17
|
+
|
|
18
|
+
## Architecture
|
|
19
|
+
|
|
20
|
+
- Runs as a single-replica **Deployment** in a dedicated namespace (default: `flickr-downloader`)
|
|
21
|
+
- Uses the **Kubernetes Python client** with in-cluster config
|
|
22
|
+
- Periodically checks configured Job names for failure conditions
|
|
23
|
+
- On failure (after a configurable delay), **deletes** the Job with `Foreground` propagation policy and **recreates** it from a cached manifest
|
|
24
|
+
- Logs pod exit codes and tail logs before every restart
|
|
25
|
+
|
|
26
|
+
## Prerequisites
|
|
27
|
+
|
|
28
|
+
- A running Kubernetes cluster
|
|
29
|
+
- Flickr download Jobs already deployed — the operator manages their lifecycle (restart on failure), not initial creation
|
|
30
|
+
- An [Immich](https://immich.app/) instance (for planned sync functionality)
|
|
31
|
+
|
|
32
|
+
## Configuration
|
|
33
|
+
|
|
34
|
+
| Variable | Description | Default |
|
|
35
|
+
|---|---|---|
|
|
36
|
+
| `LOGURU_LEVEL` | Log verbosity (`DEBUG`, `INFO`, `WARNING`, …) | `DEBUG` |
|
|
37
|
+
| `NAMESPACE` | Namespace to watch | `flickr-downloader` |
|
|
38
|
+
| `JOB_NAMES` | Comma-separated Job names to monitor (**required**) | — |
|
|
39
|
+
| `CHECK_INTERVAL` | Seconds between check cycles | `60` |
|
|
40
|
+
| `RESTART_DELAY` | Seconds to wait after failure before restart | `3600` |
|
|
41
|
+
|
|
42
|
+
## Kubernetes Deployment
|
|
43
|
+
|
|
44
|
+
### RBAC
|
|
45
|
+
|
|
46
|
+
The operator requires a ServiceAccount with a Role scoped to the target namespace:
|
|
47
|
+
|
|
48
|
+
```yaml
|
|
49
|
+
apiVersion: v1
|
|
50
|
+
kind: ServiceAccount
|
|
51
|
+
metadata:
|
|
52
|
+
name: flickr-operator
|
|
53
|
+
namespace: flickr-downloader
|
|
54
|
+
---
|
|
55
|
+
apiVersion: rbac.authorization.k8s.io/v1
|
|
56
|
+
kind: Role
|
|
57
|
+
metadata:
|
|
58
|
+
name: flickr-operator
|
|
59
|
+
namespace: flickr-downloader
|
|
60
|
+
rules:
|
|
61
|
+
- apiGroups: ["batch"]
|
|
62
|
+
resources: ["jobs"]
|
|
63
|
+
verbs: ["get", "list", "create", "delete"]
|
|
64
|
+
- apiGroups: [""]
|
|
65
|
+
resources: ["pods"]
|
|
66
|
+
verbs: ["get", "list", "delete"]
|
|
67
|
+
- apiGroups: [""]
|
|
68
|
+
resources: ["pods/log"]
|
|
69
|
+
verbs: ["get"]
|
|
70
|
+
---
|
|
71
|
+
apiVersion: rbac.authorization.k8s.io/v1
|
|
72
|
+
kind: RoleBinding
|
|
73
|
+
metadata:
|
|
74
|
+
name: flickr-operator
|
|
75
|
+
namespace: flickr-downloader
|
|
76
|
+
roleRef:
|
|
77
|
+
apiGroup: rbac.authorization.k8s.io
|
|
78
|
+
kind: Role
|
|
79
|
+
name: flickr-operator
|
|
80
|
+
subjects:
|
|
81
|
+
- kind: ServiceAccount
|
|
82
|
+
name: flickr-operator
|
|
83
|
+
namespace: flickr-downloader
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
### Deployment
|
|
87
|
+
|
|
88
|
+
```yaml
|
|
89
|
+
apiVersion: apps/v1
|
|
90
|
+
kind: Deployment
|
|
91
|
+
metadata:
|
|
92
|
+
name: flickr-operator
|
|
93
|
+
namespace: flickr-downloader
|
|
94
|
+
spec:
|
|
95
|
+
replicas: 1
|
|
96
|
+
selector:
|
|
97
|
+
matchLabels:
|
|
98
|
+
app: flickr-operator
|
|
99
|
+
template:
|
|
100
|
+
metadata:
|
|
101
|
+
labels:
|
|
102
|
+
app: flickr-operator
|
|
103
|
+
spec:
|
|
104
|
+
serviceAccountName: flickr-operator
|
|
105
|
+
containers:
|
|
106
|
+
- name: operator
|
|
107
|
+
image: flickr-immich-k8s-sync-operator:latest
|
|
108
|
+
env:
|
|
109
|
+
- name: JOB_NAMES
|
|
110
|
+
value: "flickr-downloader-alice,flickr-downloader-bob"
|
|
111
|
+
- name: LOGURU_LEVEL
|
|
112
|
+
value: "DEBUG"
|
|
113
|
+
# - name: NAMESPACE
|
|
114
|
+
# value: "flickr-downloader" # default
|
|
115
|
+
# - name: CHECK_INTERVAL
|
|
116
|
+
# value: "60" # default
|
|
117
|
+
# - name: RESTART_DELAY
|
|
118
|
+
# value: "3600" # default
|
|
119
|
+
resources:
|
|
120
|
+
requests:
|
|
121
|
+
cpu: 50m
|
|
122
|
+
memory: 64Mi
|
|
123
|
+
limits:
|
|
124
|
+
cpu: 1500m
|
|
125
|
+
memory: 128Mi
|
|
126
|
+
```
|
|
127
|
+
|
|
128
|
+
## Installation
|
|
129
|
+
|
|
130
|
+
### From PyPI
|
|
131
|
+
|
|
132
|
+
```bash
|
|
133
|
+
pip install flickr-immich-k8s-sync-operator
|
|
134
|
+
```
|
|
135
|
+
|
|
136
|
+
### From source
|
|
137
|
+
|
|
138
|
+
```bash
|
|
139
|
+
git clone https://github.com/vroomfondel/flickr-immich-k8s-sync-operator.git
|
|
140
|
+
cd flickr-immich-k8s-sync-operator
|
|
141
|
+
make venv
|
|
142
|
+
source .venv/bin/activate
|
|
143
|
+
pip install .
|
|
144
|
+
```
|
|
145
|
+
|
|
146
|
+
### Docker
|
|
147
|
+
|
|
148
|
+
```bash
|
|
149
|
+
docker build -t flickr-immich-k8s-sync-operator .
|
|
150
|
+
docker run --rm flickr-immich-k8s-sync-operator
|
|
151
|
+
```
|
|
152
|
+
|
|
153
|
+
Or via Makefile:
|
|
154
|
+
|
|
155
|
+
```bash
|
|
156
|
+
make docker
|
|
157
|
+
```
|
|
158
|
+
|
|
159
|
+
## Usage
|
|
160
|
+
|
|
161
|
+
```bash
|
|
162
|
+
# Run directly
|
|
163
|
+
flickr-immich-k8s-sync-operator
|
|
164
|
+
|
|
165
|
+
# Or via Python module
|
|
166
|
+
python -m flickr_immich_k8s_sync_operator
|
|
167
|
+
```
|
|
168
|
+
|
|
169
|
+
## Development
|
|
170
|
+
|
|
171
|
+
### Makefile targets
|
|
172
|
+
|
|
173
|
+
| Target | Description |
|
|
174
|
+
|-----------------|----------------------------------------------|
|
|
175
|
+
| `make venv` | Create virtualenv and install all dependencies |
|
|
176
|
+
| `make tests` | Run pytest |
|
|
177
|
+
| `make lint` | Format code with black (line length 120) |
|
|
178
|
+
| `make isort` | Sort imports with isort |
|
|
179
|
+
| `make tcheck` | Static type checking with mypy |
|
|
180
|
+
| `make commit-checks` | Run pre-commit hooks on all files |
|
|
181
|
+
| `make prepare` | Run tests + commit-checks |
|
|
182
|
+
| `make pypibuild`| Build sdist + wheel with hatch |
|
|
183
|
+
| `make pypipush` | Publish to PyPI with hatch |
|
|
184
|
+
| `make docker` | Build Docker image |
|
|
185
|
+
|
|
186
|
+
## License
|
|
187
|
+
|
|
188
|
+
[GNU Lesser General Public License v3](LICENSE.md)
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
__version__ = "0.0.1"
|
|
2
|
+
|
|
3
|
+
import os
|
|
4
|
+
import sys
|
|
5
|
+
from typing import Any, Callable, Dict
|
|
6
|
+
|
|
7
|
+
from loguru import logger as glogger
|
|
8
|
+
|
|
9
|
+
glogger.disable(__name__)
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
def _loguru_skiplog_filter(record: dict) -> bool: # type: ignore[type-arg]
|
|
13
|
+
"""Filter function to hide records with ``extra['skiplog']`` set."""
|
|
14
|
+
return not record.get("extra", {}).get("skiplog", False)
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
def configure_logging(
|
|
18
|
+
loguru_filter: Callable[[Dict[str, Any]], bool] = _loguru_skiplog_filter,
|
|
19
|
+
) -> None:
|
|
20
|
+
"""Configure a default ``loguru`` sink with a convenient format and filter."""
|
|
21
|
+
os.environ["LOGURU_LEVEL"] = os.getenv("LOGURU_LEVEL", "DEBUG")
|
|
22
|
+
glogger.remove()
|
|
23
|
+
logger_fmt: str = (
|
|
24
|
+
"<green>{time:YYYY-MM-DD HH:mm:ss.SSS}</green> | <level>{level: <8}</level> | <cyan>{module}</cyan>::<cyan>{extra[classname]}</cyan>:<cyan>{function}</cyan>:<cyan>{line}</cyan> - <level>{message}</level>"
|
|
25
|
+
)
|
|
26
|
+
glogger.add(sys.stderr, level=os.getenv("LOGURU_LEVEL"), format=logger_fmt, filter=loguru_filter) # type: ignore[arg-type]
|
|
27
|
+
glogger.configure(extra={"classname": "None", "skiplog": False})
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import signal
|
|
2
|
+
import sys
|
|
3
|
+
import threading
|
|
4
|
+
|
|
5
|
+
from loguru import logger as glogger
|
|
6
|
+
|
|
7
|
+
from flickr_immich_k8s_sync_operator import configure_logging
|
|
8
|
+
from flickr_immich_k8s_sync_operator.config import OperatorConfig
|
|
9
|
+
from flickr_immich_k8s_sync_operator.operator import JobRestartOperator
|
|
10
|
+
|
|
11
|
+
configure_logging()
|
|
12
|
+
glogger.enable("flickr_immich_k8s_sync_operator")
|
|
13
|
+
|
|
14
|
+
shutdown_event = threading.Event()
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
def _signal_handler(signum: int, frame: object) -> None:
|
|
18
|
+
signame = signal.Signals(signum).name
|
|
19
|
+
glogger.info(f"Received {signame} — shutting down...")
|
|
20
|
+
shutdown_event.set()
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
def main() -> None:
|
|
24
|
+
signal.signal(signal.SIGTERM, _signal_handler)
|
|
25
|
+
signal.signal(signal.SIGINT, _signal_handler)
|
|
26
|
+
|
|
27
|
+
glogger.info("flickr-immich-k8s-sync-operator starting up")
|
|
28
|
+
|
|
29
|
+
try:
|
|
30
|
+
cfg = OperatorConfig.from_env()
|
|
31
|
+
except ValueError as exc:
|
|
32
|
+
glogger.error("Configuration error: {}", exc)
|
|
33
|
+
sys.exit(1)
|
|
34
|
+
|
|
35
|
+
try:
|
|
36
|
+
operator = JobRestartOperator(cfg)
|
|
37
|
+
except Exception as exc:
|
|
38
|
+
glogger.error("Failed to initialise Kubernetes clients: {}", exc)
|
|
39
|
+
sys.exit(1)
|
|
40
|
+
|
|
41
|
+
operator.run(shutdown_event)
|
|
42
|
+
|
|
43
|
+
glogger.info("flickr-immich-k8s-sync-operator shut down cleanly")
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
if __name__ == "__main__":
|
|
47
|
+
main()
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
"""Operator configuration loaded from environment variables."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import os
|
|
6
|
+
from dataclasses import dataclass
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
@dataclass(frozen=True)
|
|
10
|
+
class OperatorConfig:
|
|
11
|
+
"""Immutable configuration for the Job-restart operator.
|
|
12
|
+
|
|
13
|
+
All values are sourced from environment variables via :meth:`from_env`.
|
|
14
|
+
"""
|
|
15
|
+
|
|
16
|
+
namespace: str
|
|
17
|
+
job_names: list[str]
|
|
18
|
+
check_interval: int
|
|
19
|
+
restart_delay: int
|
|
20
|
+
|
|
21
|
+
@classmethod
|
|
22
|
+
def from_env(cls) -> OperatorConfig:
|
|
23
|
+
"""Build an :class:`OperatorConfig` from environment variables.
|
|
24
|
+
|
|
25
|
+
Environment variables
|
|
26
|
+
---------------------
|
|
27
|
+
NAMESPACE : str, optional
|
|
28
|
+
Kubernetes namespace to watch (default ``"flickr-downloader"``).
|
|
29
|
+
JOB_NAMES : str, **required**
|
|
30
|
+
Comma-separated list of Job names to monitor.
|
|
31
|
+
CHECK_INTERVAL : str, optional
|
|
32
|
+
Seconds between check cycles (default ``60``).
|
|
33
|
+
RESTART_DELAY : str, optional
|
|
34
|
+
Seconds after failure before a Job is restarted (default ``3600``).
|
|
35
|
+
|
|
36
|
+
Raises
|
|
37
|
+
------
|
|
38
|
+
ValueError
|
|
39
|
+
If ``JOB_NAMES`` is missing or contains no non-empty entries.
|
|
40
|
+
"""
|
|
41
|
+
raw_job_names = os.environ.get("JOB_NAMES", "")
|
|
42
|
+
job_names = [name.strip() for name in raw_job_names.split(",") if name.strip()]
|
|
43
|
+
if not job_names:
|
|
44
|
+
raise ValueError("JOB_NAMES environment variable is required and must contain at least one job name")
|
|
45
|
+
|
|
46
|
+
return cls(
|
|
47
|
+
namespace=os.environ.get("NAMESPACE", "flickr-downloader").strip(),
|
|
48
|
+
job_names=job_names,
|
|
49
|
+
check_interval=int(os.environ.get("CHECK_INTERVAL", "60")),
|
|
50
|
+
restart_delay=int(os.environ.get("RESTART_DELAY", "3600")),
|
|
51
|
+
)
|
|
@@ -0,0 +1,194 @@
|
|
|
1
|
+
"""Job-restart operator — watches Kubernetes Jobs and restarts failed ones."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import copy
|
|
6
|
+
import textwrap
|
|
7
|
+
import threading
|
|
8
|
+
from datetime import datetime, timezone
|
|
9
|
+
|
|
10
|
+
from kubernetes import client, config
|
|
11
|
+
from loguru import logger as glogger
|
|
12
|
+
|
|
13
|
+
from flickr_immich_k8s_sync_operator.config import OperatorConfig
|
|
14
|
+
|
|
15
|
+
# Labels auto-added by the Job controller that reference the old
|
|
16
|
+
# Job's UID — must be stripped before creating a new Job.
|
|
17
|
+
SERVER_MANAGED_LABELS: frozenset[str] = frozenset(
|
|
18
|
+
{
|
|
19
|
+
"controller-uid",
|
|
20
|
+
"batch.kubernetes.io/controller-uid",
|
|
21
|
+
"job-name",
|
|
22
|
+
"batch.kubernetes.io/job-name",
|
|
23
|
+
}
|
|
24
|
+
)
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
def build_manifest(job_dict: dict) -> dict: # type: ignore[type-arg]
|
|
28
|
+
"""Build a clean Job manifest from a serialised ``V1Job`` dict.
|
|
29
|
+
|
|
30
|
+
The input is expected to be the result of
|
|
31
|
+
``ApiClient().sanitize_for_serialization(job)`` or equivalent
|
|
32
|
+
``V1Job.to_dict()`` output.
|
|
33
|
+
|
|
34
|
+
The returned manifest is safe to pass to
|
|
35
|
+
``create_namespaced_job`` — server-managed metadata, status, and
|
|
36
|
+
controller labels on the pod template are removed.
|
|
37
|
+
|
|
38
|
+
The *job_dict* is **not** mutated.
|
|
39
|
+
"""
|
|
40
|
+
raw = copy.deepcopy(job_dict)
|
|
41
|
+
|
|
42
|
+
template = raw["spec"]["template"]
|
|
43
|
+
|
|
44
|
+
# Strip server-managed labels from pod template
|
|
45
|
+
tmpl_labels = template.get("metadata", {}).get("labels", {})
|
|
46
|
+
for label in SERVER_MANAGED_LABELS:
|
|
47
|
+
tmpl_labels.pop(label, None)
|
|
48
|
+
|
|
49
|
+
return {
|
|
50
|
+
"apiVersion": "batch/v1",
|
|
51
|
+
"kind": "Job",
|
|
52
|
+
"metadata": {
|
|
53
|
+
"name": raw["metadata"]["name"],
|
|
54
|
+
"namespace": raw["metadata"]["namespace"],
|
|
55
|
+
},
|
|
56
|
+
"spec": {
|
|
57
|
+
"backoffLimit": raw["spec"]["backoffLimit"],
|
|
58
|
+
"template": template,
|
|
59
|
+
},
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
class JobRestartOperator:
|
|
64
|
+
"""Watches a set of Kubernetes Jobs and restarts failed ones after a delay."""
|
|
65
|
+
|
|
66
|
+
def __init__(self, cfg: OperatorConfig) -> None:
|
|
67
|
+
config.load_incluster_config()
|
|
68
|
+
self._batch_v1 = client.BatchV1Api()
|
|
69
|
+
self._core_v1 = client.CoreV1Api()
|
|
70
|
+
self._api_client = client.ApiClient()
|
|
71
|
+
self._cfg = cfg
|
|
72
|
+
self._cached_manifests: dict[str, dict] = {} # type: ignore[type-arg]
|
|
73
|
+
self._log = glogger.bind(classname=self.__class__.__name__)
|
|
74
|
+
|
|
75
|
+
def run(self, shutdown_event: threading.Event) -> None:
|
|
76
|
+
"""Main operator loop — runs until *shutdown_event* is set."""
|
|
77
|
+
self._log.info(
|
|
78
|
+
"Operator started — watching {} job(s) in namespace '{}'",
|
|
79
|
+
len(self._cfg.job_names),
|
|
80
|
+
self._cfg.namespace,
|
|
81
|
+
)
|
|
82
|
+
while not shutdown_event.is_set():
|
|
83
|
+
for job_name in self._cfg.job_names:
|
|
84
|
+
if shutdown_event.is_set():
|
|
85
|
+
break
|
|
86
|
+
self._check_job(job_name, shutdown_event)
|
|
87
|
+
if not shutdown_event.is_set():
|
|
88
|
+
self._log.info("Sleeping for {}s", self._cfg.check_interval)
|
|
89
|
+
shutdown_event.wait(timeout=self._cfg.check_interval)
|
|
90
|
+
|
|
91
|
+
def _check_job(self, job_name: str, shutdown_event: threading.Event) -> None:
|
|
92
|
+
"""Read a single Job and dispatch to the appropriate handler."""
|
|
93
|
+
self._log.opt(raw=True).info("\n")
|
|
94
|
+
self._log.info("Checking {}", job_name)
|
|
95
|
+
try:
|
|
96
|
+
job = self._batch_v1.read_namespaced_job(job_name, self._cfg.namespace)
|
|
97
|
+
self._cached_manifests[job_name] = build_manifest(self._api_client.sanitize_for_serialization(job))
|
|
98
|
+
|
|
99
|
+
conditions = job.status.conditions or []
|
|
100
|
+
failed = any(c.type == "Failed" and c.status == "True" for c in conditions)
|
|
101
|
+
|
|
102
|
+
if job.status.active:
|
|
103
|
+
self._log.info("\t{} is running.", job_name)
|
|
104
|
+
elif failed:
|
|
105
|
+
fail_condition = next(c for c in conditions if c.type == "Failed" and c.status == "True")
|
|
106
|
+
self._handle_failed_job(job_name, fail_condition.last_transition_time, shutdown_event)
|
|
107
|
+
else:
|
|
108
|
+
self._log.info("\t{} succeeded or still pending. No action needed.", job_name)
|
|
109
|
+
except client.ApiException as exc:
|
|
110
|
+
if exc.status == 404:
|
|
111
|
+
self._log.info("\t{} not found. Nothing to do.", job_name)
|
|
112
|
+
else:
|
|
113
|
+
self._log.error("\tKubernetes API error for {}: {}", job_name, exc)
|
|
114
|
+
except Exception:
|
|
115
|
+
self._log.exception("\tUnexpected error for {}", job_name)
|
|
116
|
+
|
|
117
|
+
def _handle_failed_job(
|
|
118
|
+
self,
|
|
119
|
+
job_name: str,
|
|
120
|
+
failure_time: datetime,
|
|
121
|
+
shutdown_event: threading.Event,
|
|
122
|
+
) -> None:
|
|
123
|
+
"""Log pod details and restart the Job if the restart delay has elapsed."""
|
|
124
|
+
elapsed = (datetime.now(timezone.utc) - failure_time).total_seconds()
|
|
125
|
+
|
|
126
|
+
self._log_pod_details(job_name)
|
|
127
|
+
|
|
128
|
+
if elapsed >= self._cfg.restart_delay:
|
|
129
|
+
self._log.info(
|
|
130
|
+
"\t{} failed {:.0f}s ago (>= {}s). Deleting and recreating...",
|
|
131
|
+
job_name,
|
|
132
|
+
elapsed,
|
|
133
|
+
self._cfg.restart_delay,
|
|
134
|
+
)
|
|
135
|
+
self._restart_job(job_name, shutdown_event)
|
|
136
|
+
else:
|
|
137
|
+
remaining = self._cfg.restart_delay - elapsed
|
|
138
|
+
self._log.info(
|
|
139
|
+
"\t{} failed {:.0f}s ago. Waiting {:.0f}s more before restart.",
|
|
140
|
+
job_name,
|
|
141
|
+
elapsed,
|
|
142
|
+
remaining,
|
|
143
|
+
)
|
|
144
|
+
|
|
145
|
+
def _log_pod_details(self, job_name: str) -> None:
|
|
146
|
+
"""List pods for *job_name* and log exit codes + tail logs."""
|
|
147
|
+
try:
|
|
148
|
+
pods = self._core_v1.list_namespaced_pod(
|
|
149
|
+
self._cfg.namespace,
|
|
150
|
+
label_selector=f"job-name={job_name}",
|
|
151
|
+
)
|
|
152
|
+
for pod in pods.items:
|
|
153
|
+
pod_name: str = pod.metadata.name
|
|
154
|
+
exit_code = None
|
|
155
|
+
reason = None
|
|
156
|
+
for cs in pod.status.container_statuses or []:
|
|
157
|
+
if cs.state and cs.state.terminated:
|
|
158
|
+
exit_code = cs.state.terminated.exit_code
|
|
159
|
+
reason = cs.state.terminated.reason
|
|
160
|
+
break
|
|
161
|
+
try:
|
|
162
|
+
tail = self._core_v1.read_namespaced_pod_log(
|
|
163
|
+
pod_name,
|
|
164
|
+
self._cfg.namespace,
|
|
165
|
+
tail_lines=2,
|
|
166
|
+
)
|
|
167
|
+
except Exception:
|
|
168
|
+
tail = "<logs unavailable>"
|
|
169
|
+
indented_tail = textwrap.indent(tail.strip(), "\t")
|
|
170
|
+
self._log.info(
|
|
171
|
+
"\t{}: exit_code={}, reason={}, last log lines:\n{}",
|
|
172
|
+
pod_name,
|
|
173
|
+
exit_code,
|
|
174
|
+
reason,
|
|
175
|
+
indented_tail,
|
|
176
|
+
)
|
|
177
|
+
except Exception:
|
|
178
|
+
self._log.warning("\tCould not retrieve pod details for {}", job_name)
|
|
179
|
+
|
|
180
|
+
def _restart_job(self, job_name: str, shutdown_event: threading.Event) -> None:
|
|
181
|
+
"""Delete and recreate a Job from the cached manifest."""
|
|
182
|
+
self._batch_v1.delete_namespaced_job(
|
|
183
|
+
job_name,
|
|
184
|
+
self._cfg.namespace,
|
|
185
|
+
body=client.V1DeleteOptions(propagation_policy="Foreground"),
|
|
186
|
+
)
|
|
187
|
+
# Wait for Kubernetes to clean up resources (interruptible).
|
|
188
|
+
if shutdown_event.wait(timeout=15):
|
|
189
|
+
return
|
|
190
|
+
self._batch_v1.create_namespaced_job(
|
|
191
|
+
self._cfg.namespace,
|
|
192
|
+
self._cached_manifests[job_name],
|
|
193
|
+
)
|
|
194
|
+
self._log.info("\t{} restarted successfully.", job_name)
|
|
File without changes
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["hatchling"]
|
|
3
|
+
build-backend = "hatchling.build"
|
|
4
|
+
|
|
5
|
+
[tool.hatch.build]
|
|
6
|
+
packages = ["flickr_immich_k8s_sync_operator"]
|
|
7
|
+
|
|
8
|
+
[project]
|
|
9
|
+
name = "flickr-immich-k8s-sync-operator"
|
|
10
|
+
dynamic = ["version"]
|
|
11
|
+
authors = [
|
|
12
|
+
{ name="Henning Thieß", email="ht@xomox.cc" },
|
|
13
|
+
]
|
|
14
|
+
description = "Operator for syncing photos from Flickr to Immich on Kubernetes"
|
|
15
|
+
readme = "README.md"
|
|
16
|
+
requires-python = ">=3.14"
|
|
17
|
+
license-files = ["LICENSE.md"]
|
|
18
|
+
|
|
19
|
+
keywords = ["flickr", "immich", "sync", "kubernetes", "k8s", "operator", "photo-management", "self-hosted"]
|
|
20
|
+
|
|
21
|
+
# https://pypi.org/classifiers/
|
|
22
|
+
classifiers = [
|
|
23
|
+
"Development Status :: 4 - Beta",
|
|
24
|
+
|
|
25
|
+
"Programming Language :: Python :: 3",
|
|
26
|
+
"Programming Language :: Python :: 3.14",
|
|
27
|
+
|
|
28
|
+
"Topic :: Multimedia :: Graphics",
|
|
29
|
+
"Topic :: System :: Archiving",
|
|
30
|
+
|
|
31
|
+
"License :: OSI Approved :: GNU Lesser General Public License v3 (LGPLv3)",
|
|
32
|
+
"Operating System :: OS Independent",
|
|
33
|
+
]
|
|
34
|
+
|
|
35
|
+
dependencies = [
|
|
36
|
+
'kubernetes>=28.1.0',
|
|
37
|
+
'loguru>=0.7.3',
|
|
38
|
+
]
|
|
39
|
+
|
|
40
|
+
[project.scripts]
|
|
41
|
+
flickr-immich-k8s-sync-operator = "flickr_immich_k8s_sync_operator.__main__:main"
|
|
42
|
+
|
|
43
|
+
[project.urls]
|
|
44
|
+
Homepage = "https://github.com/vroomfondel/flickr-immich-k8s-sync-operator"
|
|
45
|
+
Repository = "https://github.com/vroomfondel/flickr-immich-k8s-sync-operator"
|
|
46
|
+
Issues = "https://github.com/vroomfondel/flickr-immich-k8s-sync-operator/issues"
|
|
47
|
+
|
|
48
|
+
[tool.hatch.version]
|
|
49
|
+
path = "flickr_immich_k8s_sync_operator/__init__.py"
|
|
50
|
+
|
|
51
|
+
[tool.isort]
|
|
52
|
+
profile = "black"
|
|
53
|
+
line_length = 120
|