Skip to content

Commit 3963b9b

Browse files
authored
Merge pull request #29 from zakkg3/value-from-Issue3
Update to kopf 1.53 and k8s 1.25
2 parents fda79b3 + bacc06a commit 3963b9b

13 files changed

+87
-53
lines changed

Dockerfile

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
FROM flag5/clustersecretbase:0.0.4
1+
FROM flag5/clustersecretbase:0.0.5
22
ADD /src /src
3-
CMD kopf run /src/handlers.py
3+
CMD kopf run -A /src/handlers.py

Dockerfile.arm

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
FROM flag5/clustersecretbase:0.0.4_arm32
1+
FROM flag5/clustersecretbase:0.0.5_arm32
22
ADD /src /src
3-
CMD kopf run /src/handlers.py
3+
CMD kopf run -A /src/handlers.py

Makefile

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
IMG_NAMESPACE = flag5
22
IMG_NAME = clustersecret
33
IMG_FQNAME = $(IMG_NAMESPACE)/$(IMG_NAME)
4-
IMG_VERSION = 0.0.6
4+
IMG_VERSION = 0.0.7
55

6-
.PHONY: container push clean
6+
.PHONY: container push clean arm-container arm-push arm-clean
77
all: container push
88
arm: arm-container arm-push
99
clean: clean arm-clean

README.md

+22-6
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
---
55

6-
Introduce Kubernetes ClusterSecret
6+
Kubernetes ClusterSecret
77

88
Global inter-namespace cluster secrets - Secrets that work across namespaces - Clusterwide secrets
99

@@ -58,7 +58,13 @@ Clustersecrets automates this. It keep track of any modification in your secret
5858

5959
# installation
6060

61-
## tl;dr
61+
## Requirements
62+
63+
Current version 0.0.7 is tested for Kubernetes >= 1.19 up to 1.25
64+
65+
For older kubernes (<1.19) use the image tag "0.0.6" in yaml/02_deployment.yaml
66+
67+
## tl;dr install
6268

6369
```bash
6470
kubectl apply -f ./yaml
@@ -70,7 +76,7 @@ To instal ClusterSecret operator we need to create (in this order):
7076

7177
- RBAC resources (avoid if you are not running RBAC) to allow the operator to create/update/patch secrets: yaml/00_
7278
- Custom resource definition for the ClusterSecret resource: yaml/01_crd.yaml
73-
- The ClusterSecret operator itself: yaml/02_deployment.yaml For ARM architectures yaml/arm32v7/02_deployment.yam
79+
- The ClusterSecret operator itself: yaml/02_deployment.yaml || For **ARM architectures**: yaml/arm32v7/02_deployment.yam
7480

7581

7682
# quick start:
@@ -111,6 +117,10 @@ for a new namespaced role and its correspondient rolebinding.
111117
Here is the official doc:
112118
https://kubernetes.io/docs/reference/access-authn-authz/rbac/
113119
120+
## optional
121+
122+
overwrite the deployment command with kopf namespaces instead of the "-A" (all namespaces)
123+
114124
# Debugging.
115125
116126
@@ -132,12 +142,12 @@ kopf run ./src/handlers.py --verbose
132142

133143
# Build the images
134144

135-
There is makefiles for this, you can clone this repo. edit the makefile and then run make.
145+
There is makefiles for this, you can clone this repo. edit the makefile and then run 'make all'.
136146

137147
You will need the base image first and then the final image.
138148
Find the base one in the folder base-image (yes very original name)
139149

140-
Running make will build and push for all arch's supported.
150+
Running just 'make' builds and push for all arch's supported.
141151

142152
## x86
143153

@@ -154,8 +164,14 @@ In case you want it for your raspberri py:
154164
cd base-images && make arm & cd ..
155165
make arm
156166
```
167+
## Digests
157168
158-
169+
latest = 0.0.7
170+
171+
docker.io/flag5/clustersecret:
172+
173+
0.0.7 digest: sha256:c8dffeefbd3c8c54af67be81cd769e3c18263920729946b75f098065318eddb1
174+
0.0.7_arm32: digest: sha256:ffac630417bd090c958c9facf50a31ba54e0b18c89ef52d8eec5c1326a5f20ad
159175
# Roadmap:
160176
161177
- [] implement `source` to specify a source secret to sync instead of `data` field. (https://github.com/zakkg3/ClusterSecret/issues/3)

base-image/Dockerfile

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
FROM python:3.7-slim
1+
FROM python:3.9-slim
22
ADD requirements.txt /
33
RUN pip install -r requirements.txt

base-image/Dockerfile.arm

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
FROM arm32v7/python:3.8-slim
1+
FROM arm32v7/python:3.9-slim
22
RUN apt update && apt install -y build-essential
33
ADD requirements.txt /
44
RUN pip install -r requirements.txt

base-image/Makefile

+2-5
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
11
IMG_NAMESPACE = flag5
22
IMG_NAME = clustersecretbase
33
IMG_FQNAME = $(IMG_NAMESPACE)/$(IMG_NAME)
4-
IMG_VERSION = 0.0.4
4+
IMG_VERSION = 0.0.5
55

6-
.PHONY: container arm-container push clean clean-arm
6+
.PHONY: container arm-container push arm-push clean clean-arm
77
all: container push
88
arm: arm-container arm-push
99
clean: clean arm-clean
1010

1111

12-
13-
1412
container:
1513
sudo docker build -t $(IMG_FQNAME):$(IMG_VERSION) -t $(IMG_FQNAME):latest .
1614

@@ -20,7 +18,6 @@ push: container
2018

2119
clean:
2220
sudo docker rmi $(IMG_FQNAME):$(IMG_VERSION)
23-
2421

2522
arm-container:
2623
sudo docker build -t $(IMG_FQNAME):$(IMG_VERSION)_arm32 -f Dockerfile.arm .

base-image/requirements.txt

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
kopf===0.27rc6
2-
kubernetes
1+
kopf===1.35.3
2+
kubernetes===19.15.0

src/handlers.py

+31-12
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@
55

66
@kopf.on.delete('clustersecret.io', 'v1', 'clustersecrets')
77
def on_delete(spec,uid,body,name,logger=None, **_):
8-
syncedns = body['status']['create_fn']['syncedns']
8+
try:
9+
syncedns = body['status']['create_fn']['syncedns']
10+
except KeyError:
11+
syncedns=[]
912
v1 = client.CoreV1Api()
1013
for ns in syncedns:
1114
logger.info(f'deleting secret {name} from namespace {ns}')
@@ -84,6 +87,9 @@ def get_ns_list(logger,body,v1=None):
8487
logger.debug("matching all namespaces.")
8588
logger.debug(f'Matching namespaces: {matchNamespace}')
8689

90+
if matchNamespace is None: # if delted key (issue 26)
91+
matchNamespace = '*'
92+
8793
try:
8894
avoidNamespaces = body.get('avoidNamespaces')
8995
except KeyError:
@@ -120,7 +126,7 @@ def create_secret(logger,namespace,body,v1=None):
120126
v1 = client.CoreV1Api()
121127
logger.debug('new client - fn create secret')
122128
try:
123-
name = body['metadata']['name']
129+
sec_name = body['metadata']['name']
124130
except KeyError:
125131
logger.debug("No name in body ?")
126132
raise kopf.TemporaryError("can not get the name.")
@@ -129,36 +135,49 @@ def create_secret(logger,namespace,body,v1=None):
129135
except KeyError:
130136
data = ''
131137
logger.error("Empty secret?? could not get the data.")
132-
138+
139+
if 'valueFrom' in data:
140+
if len(data.keys()) > 1:
141+
raise kopf.TemporaryError("ValueFrom can not coexist with other keys in the data")
142+
143+
try:
144+
ns_from = data['ValueFrom']['namespace']
145+
name_from = data['ValueFrom']['name']
146+
except KeyError:
147+
logger.error("Can not get Values from external secret")
148+
# to-do keys_from
149+
logger.debug(f'Take value from secret {name_from} from namespace {ns_from}')
150+
# data = read_data_secret(name,namespace)
151+
#here - doing the valuform thing. but first fix and update all.
152+
133153
secret_type = 'Opaque'
134154
if 'type' in body:
135155
secret_type = body['type']
136-
137-
metadata = {'name': name, 'namespace': namespace}
138-
api_version = 'v1'
139-
kind = 'Secret'
140-
body = client.V1Secret(api_version, data , kind, metadata, type = secret_type)
156+
body = client.V1Secret()
157+
body.metadata = client.V1ObjectMeta(name=sec_name)
158+
body.type = secret_type
159+
body.data = data
141160
# kopf.adopt(body)
142161
logger.info(f"cloning secret in namespace {namespace}")
143162
try:
144163
api_response = v1.create_namespaced_secret(namespace, body)
145164
except client.rest.ApiException as e:
146165
if e.reason == 'Conflict':
147-
logger.warning(f"secret `{name}` already exist in namesace '{namespace}'")
166+
logger.warning(f"secret `{sec_name}` already exist in namesace '{namespace}'")
148167
return 0
149168
logger.error(f'Can not create a secret, it is base64 encoded? data: {data}')
150169
logger.error(f'Kube exception {e}')
151170
return 1
152171
return 0
153172

154173
@kopf.on.create('', 'v1', 'namespaces')
155-
async def namespace_watcher(patch,logger,meta,body,event,**kwargs):
174+
async def namespace_watcher(spec,patch,logger,meta,body,**kwargs):
156175
"""Watch for namespace events
157176
"""
158177
new_ns = meta['name']
159178
logger.debug(f"New namespace created: {new_ns} re-syncing")
160179
v1 = client.CoreV1Api()
161-
180+
ns_new_list = []
162181
for k,v in csecs.items():
163182
obj_body = v['body']
164183
#logger.debug(f'k: {k} \n v:{v}')
@@ -167,7 +186,7 @@ async def namespace_watcher(patch,logger,meta,body,event,**kwargs):
167186
ns_new_list=get_ns_list(logger,obj_body,v1)
168187
logger.debug(f"new matched list: {ns_new_list}")
169188
if new_ns in ns_new_list:
170-
logger.debug(f"Clonning secret {v['body']['metadata']['name']} into the new namespace {new_ns}")
189+
logger.debug(f"Cloning secret {v['body']['metadata']['name']} into the new namespace {new_ns}")
171190
create_secret(logger,new_ns,v['body'],v1)
172191
# if there is a new matching ns, refresh memory
173192
v['syncedns'] = ns_new_list

yaml/00_rbac.yaml

+4-4
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ metadata:
99
namespace: "clustersecret"
1010
name: clustersecret-account
1111
---
12-
apiVersion: rbac.authorization.k8s.io/v1beta1
12+
apiVersion: rbac.authorization.k8s.io/v1
1313
kind: ClusterRole
1414
metadata:
1515
name: clustersecret-role-cluster
@@ -45,7 +45,7 @@ rules:
4545
resources: [secrets]
4646
verbs: [watch, list, get, patch, update, create, delete]
4747
---
48-
apiVersion: rbac.authorization.k8s.io/v1beta1
48+
apiVersion: rbac.authorization.k8s.io/v1
4949
kind: Role
5050
metadata:
5151
namespace: "clustersecret"
@@ -71,7 +71,7 @@ rules:
7171
resources: [secrets]
7272
verbs: [create,update,patch]
7373
---
74-
apiVersion: rbac.authorization.k8s.io/v1beta1
74+
apiVersion: rbac.authorization.k8s.io/v1
7575
kind: ClusterRoleBinding
7676
metadata:
7777
name: clustersecret-rolebinding-cluster
@@ -84,7 +84,7 @@ subjects:
8484
name: clustersecret-account
8585
namespace: "clustersecret"
8686
---
87-
apiVersion: rbac.authorization.k8s.io/v1beta1
87+
apiVersion: rbac.authorization.k8s.io/v1
8888
kind: RoleBinding
8989
metadata:
9090
namespace: "clustersecret"

yaml/01_crd.yaml

+15-15
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# A demo CRD for the Kopf example operators.
2-
apiVersion: apiextensions.k8s.io/v1beta1
2+
apiVersion: apiextensions.k8s.io/v1
33
kind: CustomResourceDefinition
44
metadata:
55
name: clustersecrets.clustersecret.io
@@ -10,23 +10,23 @@ spec:
1010
- name: v1
1111
served: true
1212
storage: true
13+
additionalPrinterColumns:
14+
- name: Type
15+
type: string
16+
priority: 0
17+
jsonPath: .type
18+
description: Secret Type
19+
# - name: Subject
20+
# type: string
21+
# priority: 0
22+
# JSONPath: .subject
23+
# - name: Status
24+
# type: string
25+
# priority: 0
26+
# JSONPath: .ticket_status
1327
names:
1428
kind: ClusterSecret
1529
plural: clustersecrets
1630
singular: clustersecret
1731
shortNames:
1832
- csec
19-
additionalPrinterColumns:
20-
- name: Type
21-
type: string
22-
priority: 0
23-
JSONPath: .type
24-
description: Secret Type
25-
# - name: Subject
26-
# type: string
27-
# priority: 0
28-
# JSONPath: .subject
29-
# - name: Status
30-
# type: string
31-
# priority: 0
32-
# JSONPath: .ticket_status

yaml/02_deployment.yaml

+2-1
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,12 @@ spec:
2020
# - name: regcred
2121
containers:
2222
- name: clustersecret
23-
image: flag5/clustersecret:0.0.6
23+
image: flag5/clustersecret:0.0.7
2424
# imagePullPolicy: Always
2525
# Uncomment next lines for debug:
2626
# command:
2727
# - "kopf"
2828
# - "run"
29+
# - "-A"
2930
# - "/src/handlers.py"
3031
# - "--verbose"

yaml/Object_example/obj.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ matchNamespace:
1515
avoidNamespaces:
1616
- supersecret-ns
1717
- default
18+
- kube-system
1819
data:
1920
tls.crt: MTIzNDU2Cg==
2021
tls.key: Nzg5MTAxMTIxMgo=

0 commit comments

Comments
 (0)