Skip to content

Commit 90040ca

Browse files
authored
Merge pull request #18 from wangkuiyi/add_service_account
Add class Secret and function service_account
2 parents fddcca4 + b4abfed commit 90040ca

3 files changed

Lines changed: 46 additions & 11 deletions

File tree

examples/build_docker.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ def build_docker_image_from_git_source(
2929
url="dockerhub.com/cxwangyi/leeroy-web")
3030

3131

32-
build_docker_image_from_git_source(
33-
SKAFFOLD_GIT,
34-
SKAFFOLD_IMAGE_LEEROY_WEB,
35-
"Dockerfile")
32+
with fluid.Secret(fluid.service_account("regcred")):
33+
build_docker_image_from_git_source(
34+
SKAFFOLD_GIT,
35+
SKAFFOLD_IMAGE_LEEROY_WEB,
36+
"Dockerfile")

fluid.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,3 +104,34 @@ def image_resource(url):
104104
name = _loc(2)
105105
dump_yaml(tekton.image_resource(name, url))
106106
return name
107+
108+
109+
class Secret:
110+
'''Use with Python with statement to set and unset
111+
tekton.SERVICE_ACCOUNT_NAME'''
112+
113+
def __init__(self, name):
114+
self._name = name
115+
116+
def __enter__(self):
117+
tekton.SERVICE_ACCOUNT_NAME = self._name
118+
return tekton.SERVICE_ACCOUNT_NAME
119+
120+
def __exit__(self, typ, value, traceback):
121+
tekton.SERVICE_ACCOUNT_NAME = None
122+
123+
124+
def service_account(secret):
125+
'''Return a Kubernetes ServiceAccount'''
126+
name = _loc(2)
127+
dump_yaml({
128+
"apiVersion": "v1",
129+
"kind": "ServiceAccount",
130+
"metadata": {
131+
"name": name
132+
},
133+
"secrets": {
134+
"name": secret
135+
}
136+
})
137+
return name

tekton.py

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -86,11 +86,14 @@ def task_resource(name, typ):
8686
}
8787

8888

89+
SERVICE_ACCOUNT_NAME = None
90+
91+
8992
def task_run(func, args):
9093
'''Return a TaskRun'''
9194
argspec = inspect.getfullargspec(func)
9295
_ps, _ir, _or = task_run_params_resources(argspec, args)
93-
return _obj(
96+
_r = _obj(
9497
kind="TaskRun",
9598
name=k8s.safe_name(func.__name__ + "-run"),
9699
spec={
@@ -104,6 +107,10 @@ def task_run(func, args):
104107
"outputs": {
105108
"resources": _or,
106109
}})
110+
global SERVICE_ACCOUNT_NAME
111+
if SERVICE_ACCOUNT_NAME is not None:
112+
_r["spec"]["serviceAccountName"] = SERVICE_ACCOUNT_NAME
113+
return _r
107114

108115

109116
def _obj(kind, name, spec):
@@ -169,8 +176,8 @@ def __init__(self, io, arg):
169176
self.url = f"$({io}s.resources.{k8s.safe_name(arg)}.url)"
170177

171178

172-
def _fake_resource(io, typ, arg):
173-
return FakeGitResource(io, arg) if typ == "git" else FakeImageResource(io, arg)
179+
def _fake_resource(_io, _typ, arg):
180+
return FakeGitResource(_io, arg) if _typ == "git" else FakeImageResource(_io, arg)
174181

175182

176183
STEPS = [] # For holding steps of a Task.
@@ -236,7 +243,3 @@ def image_resource(name, url):
236243
'''Return a PipelineResource of type image'''
237244
return _resource(name, "image", {
238245
"url": url})
239-
240-
241-
INPUT_RESOURCES = []
242-
OUTPUT_RESOURCES = []

0 commit comments

Comments
 (0)