From c9be3f753291cc16a11dfa66b345e5c065ef9823 Mon Sep 17 00:00:00 2001 From: Yongun Seong Date: Thu, 2 Nov 2023 18:04:51 +0900 Subject: [PATCH] feat: add docs and chart --- .github/workflows/build.yaml | 13 +++- .gitignore | 1 + book.toml | 2 + chart/.helmignore | 23 ++++++ chart/Chart.yaml | 4 + chart/templates/NOTES.txt | 22 ++++++ chart/templates/_helpers.tpl | 62 ++++++++++++++++ chart/templates/deployment.yaml | 61 ++++++++++++++++ chart/templates/hpa.yaml | 28 +++++++ chart/templates/ingress.yaml | 61 ++++++++++++++++ chart/templates/service.yaml | 15 ++++ chart/templates/serviceaccount.yaml | 12 +++ chart/templates/tests/test-connection.yaml | 15 ++++ chart/values.yaml | 85 ++++++++++++++++++++++ flake.nix | 22 ++---- src/SUMMARY.md | 3 + src/waiter-ca.pem | 11 +++ src/waiter.md | 37 ++++++++++ 18 files changed, 457 insertions(+), 20 deletions(-) create mode 100644 book.toml create mode 100644 chart/.helmignore create mode 100644 chart/Chart.yaml create mode 100644 chart/templates/NOTES.txt create mode 100644 chart/templates/_helpers.tpl create mode 100644 chart/templates/deployment.yaml create mode 100644 chart/templates/hpa.yaml create mode 100644 chart/templates/ingress.yaml create mode 100644 chart/templates/service.yaml create mode 100644 chart/templates/serviceaccount.yaml create mode 100644 chart/templates/tests/test-connection.yaml create mode 100644 chart/values.yaml create mode 100644 src/SUMMARY.md create mode 100644 src/waiter-ca.pem create mode 100644 src/waiter.md diff --git a/.github/workflows/build.yaml b/.github/workflows/build.yaml index 26c1e71..62bc9d5 100644 --- a/.github/workflows/build.yaml +++ b/.github/workflows/build.yaml @@ -2,8 +2,8 @@ name: Build and push image on: push: - branches: - - master + tags: + - v* permissions: contents: read @@ -24,10 +24,17 @@ jobs: registry: ghcr.io username: ${{ github.actor }} password: ${{ secrets.GITHUB_TOKEN }} + - name: Tag and Push env: - GH_IMAGE: ghcr.io/${{ github.repository }}:${{ github.sha }} + GH_IMAGE: ghcr.io/${{ github.repository }}:${{ github.ref_name }} run: | IMAGE="$(docker load -i result | awk '{print $3}')" docker tag "$IMAGE" "$GH_IMAGE" docker push "$GH_IMAGE" + + - uses: Azure/setup-helm@v3 + - run: | + VERSION="${GITHUB_REF_NAME#v}" + helm package --version "${VERSION}" -d pkg/ chart/ + helm push ./pkg/*.tgz "oci://ghcr.io/$GITHUB_REPOSITORY_OWNER" diff --git a/.gitignore b/.gitignore index 7c5f3f9..f72dc68 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ +book /result /result-* diff --git a/book.toml b/book.toml new file mode 100644 index 0000000..3d9b6ff --- /dev/null +++ b/book.toml @@ -0,0 +1,2 @@ +[book] +title = "Bacchus Waiter" diff --git a/chart/.helmignore b/chart/.helmignore new file mode 100644 index 0000000..0e8a0eb --- /dev/null +++ b/chart/.helmignore @@ -0,0 +1,23 @@ +# Patterns to ignore when building packages. +# This supports shell glob matching, relative path matching, and +# negation (prefixed with !). Only one pattern per line. +.DS_Store +# Common VCS dirs +.git/ +.gitignore +.bzr/ +.bzrignore +.hg/ +.hgignore +.svn/ +# Common backup files +*.swp +*.bak +*.tmp +*.orig +*~ +# Various IDEs +.project +.idea/ +*.tmproj +.vscode/ diff --git a/chart/Chart.yaml b/chart/Chart.yaml new file mode 100644 index 0000000..be6bf95 --- /dev/null +++ b/chart/Chart.yaml @@ -0,0 +1,4 @@ +apiVersion: v2 +name: waiter-docs +type: application +version: 0.1.0 diff --git a/chart/templates/NOTES.txt b/chart/templates/NOTES.txt new file mode 100644 index 0000000..51d8ee7 --- /dev/null +++ b/chart/templates/NOTES.txt @@ -0,0 +1,22 @@ +1. Get the application URL by running these commands: +{{- if .Values.ingress.enabled }} +{{- range $host := .Values.ingress.hosts }} + {{- range .paths }} + http{{ if $.Values.ingress.tls }}s{{ end }}://{{ $host.host }}{{ .path }} + {{- end }} +{{- end }} +{{- else if contains "NodePort" .Values.service.type }} + export NODE_PORT=$(kubectl get --namespace {{ .Release.Namespace }} -o jsonpath="{.spec.ports[0].nodePort}" services {{ include "waiter-docs.fullname" . }}) + export NODE_IP=$(kubectl get nodes --namespace {{ .Release.Namespace }} -o jsonpath="{.items[0].status.addresses[0].address}") + echo http://$NODE_IP:$NODE_PORT +{{- else if contains "LoadBalancer" .Values.service.type }} + NOTE: It may take a few minutes for the LoadBalancer IP to be available. + You can watch the status of by running 'kubectl get --namespace {{ .Release.Namespace }} svc -w {{ include "waiter-docs.fullname" . }}' + export SERVICE_IP=$(kubectl get svc --namespace {{ .Release.Namespace }} {{ include "waiter-docs.fullname" . }} --template "{{"{{ range (index .status.loadBalancer.ingress 0) }}{{.}}{{ end }}"}}") + echo http://$SERVICE_IP:{{ .Values.service.port }} +{{- else if contains "ClusterIP" .Values.service.type }} + export POD_NAME=$(kubectl get pods --namespace {{ .Release.Namespace }} -l "app.kubernetes.io/name={{ include "waiter-docs.name" . }},app.kubernetes.io/instance={{ .Release.Name }}" -o jsonpath="{.items[0].metadata.name}") + export CONTAINER_PORT=$(kubectl get pod --namespace {{ .Release.Namespace }} $POD_NAME -o jsonpath="{.spec.containers[0].ports[0].containerPort}") + echo "Visit http://127.0.0.1:8080 to use your application" + kubectl --namespace {{ .Release.Namespace }} port-forward $POD_NAME 8080:$CONTAINER_PORT +{{- end }} diff --git a/chart/templates/_helpers.tpl b/chart/templates/_helpers.tpl new file mode 100644 index 0000000..72ce873 --- /dev/null +++ b/chart/templates/_helpers.tpl @@ -0,0 +1,62 @@ +{{/* +Expand the name of the chart. +*/}} +{{- define "waiter-docs.name" -}} +{{- default .Chart.Name .Values.nameOverride | trunc 63 | trimSuffix "-" }} +{{- end }} + +{{/* +Create a default fully qualified app name. +We truncate at 63 chars because some Kubernetes name fields are limited to this (by the DNS naming spec). +If release name contains chart name it will be used as a full name. +*/}} +{{- define "waiter-docs.fullname" -}} +{{- if .Values.fullnameOverride }} +{{- .Values.fullnameOverride | trunc 63 | trimSuffix "-" }} +{{- else }} +{{- $name := default .Chart.Name .Values.nameOverride }} +{{- if contains $name .Release.Name }} +{{- .Release.Name | trunc 63 | trimSuffix "-" }} +{{- else }} +{{- printf "%s-%s" .Release.Name $name | trunc 63 | trimSuffix "-" }} +{{- end }} +{{- end }} +{{- end }} + +{{/* +Create chart name and version as used by the chart label. +*/}} +{{- define "waiter-docs.chart" -}} +{{- printf "%s-%s" .Chart.Name .Chart.Version | replace "+" "_" | trunc 63 | trimSuffix "-" }} +{{- end }} + +{{/* +Common labels +*/}} +{{- define "waiter-docs.labels" -}} +helm.sh/chart: {{ include "waiter-docs.chart" . }} +{{ include "waiter-docs.selectorLabels" . }} +{{- if .Chart.AppVersion }} +app.kubernetes.io/version: {{ .Chart.AppVersion | quote }} +{{- end }} +app.kubernetes.io/managed-by: {{ .Release.Service }} +{{- end }} + +{{/* +Selector labels +*/}} +{{- define "waiter-docs.selectorLabels" -}} +app.kubernetes.io/name: {{ include "waiter-docs.name" . }} +app.kubernetes.io/instance: {{ .Release.Name }} +{{- end }} + +{{/* +Create the name of the service account to use +*/}} +{{- define "waiter-docs.serviceAccountName" -}} +{{- if .Values.serviceAccount.create }} +{{- default (include "waiter-docs.fullname" .) .Values.serviceAccount.name }} +{{- else }} +{{- default "default" .Values.serviceAccount.name }} +{{- end }} +{{- end }} diff --git a/chart/templates/deployment.yaml b/chart/templates/deployment.yaml new file mode 100644 index 0000000..9c5c140 --- /dev/null +++ b/chart/templates/deployment.yaml @@ -0,0 +1,61 @@ +apiVersion: apps/v1 +kind: Deployment +metadata: + name: {{ include "waiter-docs.fullname" . }} + labels: + {{- include "waiter-docs.labels" . | nindent 4 }} +spec: + {{- if not .Values.autoscaling.enabled }} + replicas: {{ .Values.replicaCount }} + {{- end }} + selector: + matchLabels: + {{- include "waiter-docs.selectorLabels" . | nindent 6 }} + template: + metadata: + {{- with .Values.podAnnotations }} + annotations: + {{- toYaml . | nindent 8 }} + {{- end }} + labels: + {{- include "waiter-docs.selectorLabels" . | nindent 8 }} + spec: + {{- with .Values.imagePullSecrets }} + imagePullSecrets: + {{- toYaml . | nindent 8 }} + {{- end }} + serviceAccountName: {{ include "waiter-docs.serviceAccountName" . }} + securityContext: + {{- toYaml .Values.podSecurityContext | nindent 8 }} + containers: + - name: {{ .Chart.Name }} + securityContext: + {{- toYaml .Values.securityContext | nindent 12 }} + image: "{{ .Values.image.repository }}:{{ .Values.image.tag | default .Chart.AppVersion }}" + imagePullPolicy: {{ .Values.image.pullPolicy }} + ports: + - name: http + containerPort: 8080 + protocol: TCP + livenessProbe: + httpGet: + path: / + port: http + readinessProbe: + httpGet: + path: / + port: http + resources: + {{- toYaml .Values.resources | nindent 12 }} + {{- with .Values.nodeSelector }} + nodeSelector: + {{- toYaml . | nindent 8 }} + {{- end }} + {{- with .Values.affinity }} + affinity: + {{- toYaml . | nindent 8 }} + {{- end }} + {{- with .Values.tolerations }} + tolerations: + {{- toYaml . | nindent 8 }} + {{- end }} diff --git a/chart/templates/hpa.yaml b/chart/templates/hpa.yaml new file mode 100644 index 0000000..6b6a282 --- /dev/null +++ b/chart/templates/hpa.yaml @@ -0,0 +1,28 @@ +{{- if .Values.autoscaling.enabled }} +apiVersion: autoscaling/v2beta1 +kind: HorizontalPodAutoscaler +metadata: + name: {{ include "waiter-docs.fullname" . }} + labels: + {{- include "waiter-docs.labels" . | nindent 4 }} +spec: + scaleTargetRef: + apiVersion: apps/v1 + kind: Deployment + name: {{ include "waiter-docs.fullname" . }} + minReplicas: {{ .Values.autoscaling.minReplicas }} + maxReplicas: {{ .Values.autoscaling.maxReplicas }} + metrics: + {{- if .Values.autoscaling.targetCPUUtilizationPercentage }} + - type: Resource + resource: + name: cpu + targetAverageUtilization: {{ .Values.autoscaling.targetCPUUtilizationPercentage }} + {{- end }} + {{- if .Values.autoscaling.targetMemoryUtilizationPercentage }} + - type: Resource + resource: + name: memory + targetAverageUtilization: {{ .Values.autoscaling.targetMemoryUtilizationPercentage }} + {{- end }} +{{- end }} diff --git a/chart/templates/ingress.yaml b/chart/templates/ingress.yaml new file mode 100644 index 0000000..48357c4 --- /dev/null +++ b/chart/templates/ingress.yaml @@ -0,0 +1,61 @@ +{{- if .Values.ingress.enabled -}} +{{- $fullName := include "waiter-docs.fullname" . -}} +{{- $svcPort := .Values.service.port -}} +{{- if and .Values.ingress.className (not (semverCompare ">=1.18-0" .Capabilities.KubeVersion.GitVersion)) }} + {{- if not (hasKey .Values.ingress.annotations "kubernetes.io/ingress.class") }} + {{- $_ := set .Values.ingress.annotations "kubernetes.io/ingress.class" .Values.ingress.className}} + {{- end }} +{{- end }} +{{- if semverCompare ">=1.19-0" .Capabilities.KubeVersion.GitVersion -}} +apiVersion: networking.k8s.io/v1 +{{- else if semverCompare ">=1.14-0" .Capabilities.KubeVersion.GitVersion -}} +apiVersion: networking.k8s.io/v1beta1 +{{- else -}} +apiVersion: extensions/v1beta1 +{{- end }} +kind: Ingress +metadata: + name: {{ $fullName }} + labels: + {{- include "waiter-docs.labels" . | nindent 4 }} + {{- with .Values.ingress.annotations }} + annotations: + {{- toYaml . | nindent 4 }} + {{- end }} +spec: + {{- if and .Values.ingress.className (semverCompare ">=1.18-0" .Capabilities.KubeVersion.GitVersion) }} + ingressClassName: {{ .Values.ingress.className }} + {{- end }} + {{- if .Values.ingress.tls }} + tls: + {{- range .Values.ingress.tls }} + - hosts: + {{- range .hosts }} + - {{ . | quote }} + {{- end }} + secretName: {{ .secretName }} + {{- end }} + {{- end }} + rules: + {{- range .Values.ingress.hosts }} + - host: {{ .host | quote }} + http: + paths: + {{- range .paths }} + - path: {{ .path }} + {{- if and .pathType (semverCompare ">=1.18-0" $.Capabilities.KubeVersion.GitVersion) }} + pathType: {{ .pathType }} + {{- end }} + backend: + {{- if semverCompare ">=1.19-0" $.Capabilities.KubeVersion.GitVersion }} + service: + name: {{ $fullName }} + port: + number: {{ $svcPort }} + {{- else }} + serviceName: {{ $fullName }} + servicePort: {{ $svcPort }} + {{- end }} + {{- end }} + {{- end }} +{{- end }} diff --git a/chart/templates/service.yaml b/chart/templates/service.yaml new file mode 100644 index 0000000..dac9618 --- /dev/null +++ b/chart/templates/service.yaml @@ -0,0 +1,15 @@ +apiVersion: v1 +kind: Service +metadata: + name: {{ include "waiter-docs.fullname" . }} + labels: + {{- include "waiter-docs.labels" . | nindent 4 }} +spec: + type: {{ .Values.service.type }} + ports: + - port: {{ .Values.service.port }} + targetPort: http + protocol: TCP + name: http + selector: + {{- include "waiter-docs.selectorLabels" . | nindent 4 }} diff --git a/chart/templates/serviceaccount.yaml b/chart/templates/serviceaccount.yaml new file mode 100644 index 0000000..d1debee --- /dev/null +++ b/chart/templates/serviceaccount.yaml @@ -0,0 +1,12 @@ +{{- if .Values.serviceAccount.create -}} +apiVersion: v1 +kind: ServiceAccount +metadata: + name: {{ include "waiter-docs.serviceAccountName" . }} + labels: + {{- include "waiter-docs.labels" . | nindent 4 }} + {{- with .Values.serviceAccount.annotations }} + annotations: + {{- toYaml . | nindent 4 }} + {{- end }} +{{- end }} diff --git a/chart/templates/tests/test-connection.yaml b/chart/templates/tests/test-connection.yaml new file mode 100644 index 0000000..c4240d6 --- /dev/null +++ b/chart/templates/tests/test-connection.yaml @@ -0,0 +1,15 @@ +apiVersion: v1 +kind: Pod +metadata: + name: "{{ include "waiter-docs.fullname" . }}-test-connection" + labels: + {{- include "waiter-docs.labels" . | nindent 4 }} + annotations: + "helm.sh/hook": test +spec: + containers: + - name: wget + image: busybox + command: ['wget'] + args: ['{{ include "waiter-docs.fullname" . }}:{{ .Values.service.port }}'] + restartPolicy: Never diff --git a/chart/values.yaml b/chart/values.yaml new file mode 100644 index 0000000..b653a2f --- /dev/null +++ b/chart/values.yaml @@ -0,0 +1,85 @@ +# Default values for waiter-docs. +# This is a YAML-formatted file. +# Declare variables to be passed into your templates. + +replicaCount: 1 + +image: + repository: ghcr.io/bacchus-snu/waiter-docs + pullPolicy: IfNotPresent + # Overrides the image tag whose default is the chart appVersion. + tag: "" + +imagePullSecrets: [] +nameOverride: "" +fullnameOverride: "" + +serviceAccount: + # Specifies whether a service account should be created + create: true + # Annotations to add to the service account + annotations: {} + # The name of the service account to use. + # If not set and create is true, a name is generated using the fullname template + name: "" + +podAnnotations: {} + +podSecurityContext: {} + # fsGroup: 2000 + +securityContext: + allowPrivilegeEscalation: false + capabilities: + drop: + - ALL + readOnlyRootFilesystem: true + runAsNonRoot: true + runAsUser: 1000 + seccompProfile: + type: RuntimeDefault + +service: + type: ClusterIP + port: 80 + +ingress: + enabled: false + className: "" + annotations: {} + # kubernetes.io/ingress.class: nginx + # kubernetes.io/tls-acme: "true" + hosts: + - host: chart-example.local + paths: + - path: / + pathType: ImplementationSpecific + tls: [] + # - secretName: chart-example-tls + # hosts: + # - chart-example.local + +resources: {} + # We usually recommend not to specify default resources and to leave this as a conscious + # choice for the user. This also increases chances charts run on environments with little + # resources, such as Minikube. If you do want to specify resources, uncomment the following + # lines, adjust them as necessary, and remove the curly braces after 'resources:'. + # limits: + # cpu: 100m + # memory: 128Mi + # requests: + # cpu: 100m + # memory: 128Mi + +autoscaling: + enabled: false + minReplicas: 1 + maxReplicas: 100 + targetCPUUtilizationPercentage: 80 + # targetMemoryUtilizationPercentage: 80 + +nodeSelector: {} + +tolerations: [] + +affinity: {} diff --git a/flake.nix b/flake.nix index a6b2392..72539fb 100644 --- a/flake.nix +++ b/flake.nix @@ -4,25 +4,13 @@ let lib = nixpkgs.lib; pkgs = nixpkgs.legacyPackages.${system}; - placeholder = self.packages.${system}.placeholder; + pkg = self.packages.${system}.default; in { packages = { - placeholder = pkgs.stdenvNoCC.mkDerivation { - name = "placeholder"; - src = pkgs.fetchFromGitHub { - owner = "bacchus-snu"; - repo = "snucse-gpu-service-manual"; - rev = "3dd6d4df9b6c8ffbcc6073826527f119c7fd937f"; - hash = "sha256-e9GANnoJpUggZROINZ0kxtrjA4uubrhzEIWjNCONE5U="; - }; - - postPatch = '' - cat < src/robots.txt - User-Agent: * - Disallow: / - EOF - ''; + default = pkgs.stdenvNoCC.mkDerivation { + name = "waiter-docs"; + src = ./.; nativeBuildInputs = with pkgs; [ mdbook mdbook-i18n-helpers ]; @@ -34,7 +22,7 @@ let caddyfile = pkgs.writeText "Caddyfile" '' :8080 { - root * ${placeholder} + root * ${pkg} file_server handle_errors { rewrite * /{err.status_code}.html diff --git a/src/SUMMARY.md b/src/SUMMARY.md new file mode 100644 index 0000000..3730637 --- /dev/null +++ b/src/SUMMARY.md @@ -0,0 +1,3 @@ +# Summary + +[Waiter](waiter.md) diff --git a/src/waiter-ca.pem b/src/waiter-ca.pem new file mode 100644 index 0000000..01aa7e7 --- /dev/null +++ b/src/waiter-ca.pem @@ -0,0 +1,11 @@ +-----BEGIN CERTIFICATE----- +MIIBiTCCAS+gAwIBAgIQXFMXlIsEzHcDwR3LvQIx1jAKBggqhkjOPQQDAjAVMRMw +EQYDVQQKEwprdWJlcm5ldGVzMB4XDTIzMTAyMDA4NTU1NFoXDTMzMTAxNzA4NTU1 +NFowFTETMBEGA1UEChMKa3ViZXJuZXRlczBZMBMGByqGSM49AgEGCCqGSM49AwEH +A0IABDa5QKRFATUZ14HmscdlMcmvu1wCYiI0VlBVxXExZADFicYIAVUsnw8mkwwl +YUXjPBl28q8yMo61iwaH1KOBh3SjYTBfMA4GA1UdDwEB/wQEAwIChDAdBgNVHSUE +FjAUBggrBgEFBQcDAQYIKwYBBQUHAwIwDwYDVR0TAQH/BAUwAwEB/zAdBgNVHQ4E +FgQUMCJfA2BS6NDvF3Tm1CUgajTmq2cwCgYIKoZIzj0EAwIDSAAwRQIhAKdQhs1n +MMINHjTsk3D5AXm/5aVALpfD1XU8UblkV6HPAiB2MX3fgHpa4a1Yl5UPYuGaZvSi +qKF4SXWJOwy8H1hf+g== +-----END CERTIFICATE----- diff --git a/src/waiter.md b/src/waiter.md new file mode 100644 index 0000000..5e8284d --- /dev/null +++ b/src/waiter.md @@ -0,0 +1,37 @@ +# Waiter + +## kubeconfig + +- [waiter-ca.pem](waiter-ca.pem) +- [kubelogin](https://github.com/int128/kubelogin) + +```bash +# Configure the cluster +kubectl config set-cluster bacchus-waiter \ + --server=https://waiter.bacchus.io:6443 \ + --embed-certs \ + --certificate-authority=waiter-ca.pem + +# Configure authentication +kubectl config set-credentials bacchus-waiter \ + --exec-api-version=client.authentication.k8s.io/v1beta1 \ + --exec-command=kubectl \ + --exec-arg=oidc-login \ + --exec-arg=get-token \ + --exec-arg=--oidc-issuer-url=https://auth.bacchus.io/dex \ + --exec-arg=--oidc-client-id=bacchus-waiter \ + --exec-arg=--oidc-extra-scope=email \ + --exec-arg=--oidc-extra-scope=groups \ + --exec-arg=--oidc-use-pkce + +# Configure context +kubectl config set-context bacchus-waiter \ + --cluster=bacchus-waiter \ + --user=bacchus-waiter +``` + +Verify with `kubectl auth whoami`. + +## VPN + +TODO