From b7f9da74d49257e16c2202d2463dfc1fd641a67e Mon Sep 17 00:00:00 2001 From: "Chris West (Faux)" Date: Sun, 15 Mar 2020 21:43:33 +0000 Subject: [PATCH] I CANNOT READ --- .gitignore | 2 ++ Dockerfile | 9 +++++++++ app.c | 57 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ quebec.yml | 41 +++++++++++++++++++++++++++++++++++++++ 4 files changed, 109 insertions(+) create mode 100644 .gitignore create mode 100644 Dockerfile create mode 100644 app.c create mode 100644 quebec.yml diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..eb3b49c --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +*.swp +.idea/ diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..2f15d6f --- /dev/null +++ b/Dockerfile @@ -0,0 +1,9 @@ +FROM alpine +WORKDIR /b +RUN apk add --update gcc libc-dev +ADD app.c ./ +RUN gcc -O1 -static app.c + +FROM scratch +COPY --from=0 /b/a.out /a +CMD ["/a"] diff --git a/app.c b/app.c new file mode 100644 index 0000000..5476027 --- /dev/null +++ b/app.c @@ -0,0 +1,57 @@ +#define _POSIX_C_SOURCE 200112L + +#include +#include +#include +#include +#include +#include +#include + +int main() { + + const char *head = "HTTP/1.0 200\r\nContent-type: text/plain\r\n\r\n"; + char buf[256] = {}; + memcpy(buf, head, strlen(head)); + if (0 != gethostname(buf + strlen(buf), sizeof(buf))) { + return 4; + } + + strcat(buf, "\n"); + + int sockfd = socket(AF_INET, SOCK_STREAM, 0); + +#ifdef REUSEADDR + int enable = 1; + if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &enable, sizeof(int)) < 0) { + return 5; + } +#endif + + struct sockaddr_in servaddr = {}; + servaddr.sin_family = AF_INET; + servaddr.sin_addr.s_addr = htonl(INADDR_ANY); + servaddr.sin_port = htons(1337); + if (0 != bind(sockfd, (struct sockaddr*)&servaddr, sizeof(servaddr))) { + return 1; + } + + if (0 != listen(sockfd, 50)) { + return 2; + } + + for (;;) { + int conn = accept(sockfd, NULL, NULL); + if (conn < 0) { + return 3; + } + + // errors intentionally ignored + write(conn, buf, strlen(buf)); + + // without shutdown, curl sometimes gets the close before the data + shutdown(conn, SHUT_RDWR); + close(conn); + } +} + diff --git a/quebec.yml b/quebec.yml new file mode 100644 index 0000000..ec814ae --- /dev/null +++ b/quebec.yml @@ -0,0 +1,41 @@ +--- +kind: Deployment +apiVersion: apps/v1 +metadata: + name: hosty +spec: + strategy: + rollingUpdate: + maxSurge: 1 + maxUnavailable: 1 + type: RollingUpdate + replicas: 3 + selector: + matchLabels: + name: hosty + template: + metadata: + labels: + name: hosty + spec: + containers: + - name: app + image: faux/hostname-reply + ports: + - containerPort: 1337 + readinessProbe: + httpGet: + path: / + port: 1337 +--- +apiVersion: v1 +kind: Service +metadata: + name: hosty +spec: + type: ClusterIP + ports: + - port: 80 + targetPort: 1337 + selector: + name: hosty