-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implements in the TaskRun controller the logic to provide the cloud event pipeline resource. This commits puts together the API, cloud event helper and resource definition from four pull requests: - #1090 - #1091 - #1092 It adds unit tests for the new code and one E2E YAML test. The YAML test runs a simple http server that can receive the cloudevent for test purposes. The list of cloud events to be sent is added to the TaskRun status and processed by the TaskRun controller once the pod associated to the TaskRun completes its execution. The `isDone` definition of the TaskRun is not altered, the reconciler checks for events to be sent once the TaskRun.isDone is true. Retries are not implemented yet in the sense that every scheduled event will be attempted exactly once, but it may be that those attempts happen across different invocations of Reconcile. Signed-off-by: Andrea Frittoli <andrea.frittoli@uk.ibm.com>
- Loading branch information
1 parent
9e964ec
commit 53c936b
Showing
11 changed files
with
879 additions
and
75 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,187 @@ | ||
--- | ||
apiVersion: v1 | ||
kind: Service | ||
metadata: | ||
name: sink | ||
namespace: default | ||
spec: | ||
selector: | ||
app: cloudevent | ||
ports: | ||
- protocol: TCP | ||
port: 8080 | ||
targetPort: 8080 | ||
--- | ||
apiVersion: v1 | ||
kind: Pod | ||
metadata: | ||
labels: | ||
app: cloudevent | ||
name: message-sink | ||
namespace: default | ||
spec: | ||
containers: | ||
- env: | ||
- name: PORT | ||
value: "8080" | ||
name: cloudeventlistener | ||
image: python:3-alpine | ||
imagePullPolicy: IfNotPresent | ||
command: ["/bin/sh"] | ||
args: | ||
- -ce | ||
- | | ||
cat <<EOF | python | ||
from http.server import BaseHTTPRequestHandler, HTTPServer | ||
class GetAndPostHandler(BaseHTTPRequestHandler): | ||
def do_POST(self): | ||
content = self.rfile.read(int(self.headers.get('Content-Length'))) | ||
with open("content.txt", mode="wb") as f: | ||
f.write(content) | ||
self.send_response(200) | ||
self.send_header('Content-type', 'text/html') | ||
self.end_headers() | ||
self.wfile.write(b'<html><body><h1>POST!</h1></body></html>') | ||
def do_GET(self): | ||
with open("content.txt", mode="rb") as f: | ||
content = f.read() | ||
self.send_response(200 if content else 404) | ||
self.send_header('Content-type', 'text/plain') | ||
self.end_headers() | ||
self.wfile.write(content) | ||
if __name__ == "__main__": | ||
open("content.txt", 'a').close() | ||
httpd = HTTPServer(('', $PORT), GetAndPostHandler) | ||
print('Starting httpd...') | ||
httpd.serve_forever() | ||
EOF | ||
ports: | ||
- containerPort: 8080 | ||
name: user-port | ||
protocol: TCP | ||
--- | ||
apiVersion: tekton.dev/v1alpha1 | ||
kind: PipelineResource | ||
metadata: | ||
name: to-message-sink | ||
spec: | ||
type: cloudEvent | ||
params: | ||
- name: targetURI | ||
value: http://sink.default:8080 | ||
--- | ||
apiVersion: tekton.dev/v1alpha1 | ||
kind: PipelineResource | ||
metadata: | ||
name: fake-image | ||
spec: | ||
type: image | ||
params: | ||
- name: url | ||
value: fake-registry/test/fake-image | ||
--- | ||
apiVersion: tekton.dev/v1alpha1 | ||
kind: Task | ||
metadata: | ||
name: send-cloud-event-task | ||
spec: | ||
outputs: | ||
resources: | ||
- name: myimage | ||
type: image | ||
- name: notification | ||
type: cloudEvent | ||
steps: | ||
- name: build-index-json | ||
image: busybox | ||
command: | ||
- /bin/sh | ||
args: | ||
- -ce | ||
- | | ||
set -e | ||
mkdir -p /builder/home/image-outputs/myimage/ | ||
cat <<EOF > /builder/home/image-outputs/myimage/index.json | ||
{ | ||
"schemaVersion": 2, | ||
"manifests": [ | ||
{ | ||
"mediaType": "application/vnd.oci.image.index.v1+json", | ||
"size": 314, | ||
"digest": "sha256:NOTAREALDIGEST" | ||
} | ||
] | ||
} | ||
EOF | ||
--- | ||
apiVersion: tekton.dev/v1alpha1 | ||
kind: Task | ||
metadata: | ||
name: poll-for-content-task | ||
spec: | ||
steps: | ||
- name: polling | ||
image: python:3-alpine | ||
imagePullPolicy: IfNotPresent | ||
command: ["/bin/sh"] | ||
args: | ||
- -ce | ||
- | | ||
cat <<EOF | python | ||
import http.client | ||
import json | ||
import sys | ||
import time | ||
while True: | ||
conn = http.client.HTTPConnection("sink.default:8080") | ||
try: | ||
conn.request("GET", "/") | ||
except: | ||
# Perhaps the service is not setup yet, so service name does not | ||
# resolve or it does not accept connections on 8080 yet | ||
print("Not yet...") | ||
time.sleep(10) | ||
continue | ||
response = conn.getresponse() | ||
if response.status == 200: | ||
print("Got it!") | ||
taskrun = json.loads(response.read().decode('utf-8')) | ||
digest = taskrun['taskRun']['status']['resourcesResult'][0]['digest'] | ||
image_name = taskrun['taskRun']['status']['resourcesResult'][0]['name'] | ||
print("Got digest %s for image %s" % (digest, image_name)) | ||
if image_name == "fake-image" and digest: | ||
break | ||
else: | ||
sys.exit(1) | ||
else: | ||
print("Not yet...") | ||
time.sleep(10) | ||
--- | ||
apiVersion: tekton.dev/v1alpha1 | ||
kind: TaskRun | ||
metadata: | ||
name: send-cloud-event | ||
spec: | ||
outputs: | ||
resources: | ||
- name: myimage | ||
resourceRef: | ||
name: fake-image | ||
- name: notification | ||
resourceRef: | ||
name: to-message-sink | ||
taskRef: | ||
name: send-cloud-event-task | ||
--- | ||
apiVersion: tekton.dev/v1alpha1 | ||
kind: TaskRun | ||
metadata: | ||
name: poll-for-content-run | ||
spec: | ||
taskRef: | ||
name: poll-for-content-task |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.