├── .github └── workflows │ └── workflow.yaml ├── Dockerfile ├── LICENSE ├── README.md ├── examples └── example.yaml ├── hooks └── service-hook.sh ├── media └── recording.gif ├── operator.yaml └── rbac.yaml /.github/workflows/workflow.yaml: -------------------------------------------------------------------------------- 1 | name: Test, build and push the docker image 2 | on: 3 | push: 4 | paths: 5 | - "hooks/**" 6 | - ".github/**" 7 | - Dockerfile 8 | 9 | jobs: 10 | build-and-push: 11 | runs-on: ubuntu-latest 12 | steps: 13 | - name: Set up Docker Buildx 14 | uses: docker/setup-buildx-action@v2 15 | 16 | - name: Login to Docker Hub 17 | uses: docker/login-action@v2 18 | with: 19 | username: ${{ secrets.DOCKERHUB_USERNAME }} 20 | password: ${{ secrets.DOCKERHUB_TOKEN }} 21 | 22 | - name: Build and push 23 | uses: docker/build-push-action@v3 24 | with: 25 | platforms: linux/amd64,linux/arm64,linux/arm/v7 26 | push: true 27 | tags: nospamplease/subdomain-mapper:v1 -------------------------------------------------------------------------------- /Dockerfile: -------------------------------------------------------------------------------- 1 | FROM flant/shell-operator:latest 2 | ENV LOG_TYPE=text 3 | ADD hooks /hooks -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2022 Andrei Surugiu 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # subdomain-mapper-operator (IN-PROGRESS) 2 | ![](media/recording.gif) 3 | 4 | ### Why? 5 | I wanted something similar to vercel previews where an environment is created automatically with a subdomain. 6 | 7 | ### What? 8 | This operator maps the subdomain `..` to ``. 9 | 10 | ### How? 11 | It watches for new services that have the annotation and patches the ingress when a service is created, modified or removed. 12 | 13 | It uses https://github.com/flant/shell-operator 14 | 15 | ## Usage and example 16 | 17 | The services must have the following annotation: 18 | 19 | ```yaml 20 | annotations: 21 | subdomain-mapper/ingress: "your-ingress-name" 22 | subdomain-mapper/domain: "your-domain" 23 | ``` 24 | **example:** 25 | ```yaml 26 | annotations: 27 | subdomain-mapper/ingress: "main-ingress" 28 | subdomain-mapper/domain: "andreisurugiu.com" 29 | ``` 30 | 31 | You need to specify the ingress to add the hosts to. 32 | 33 | Create the RBAC permissions and the service account: 34 | 35 | ```sh 36 | # apply rbac.yaml 37 | kubectl apply -f https://raw.githubusercontent.com/DeluxeOwl/subdomain-mapper-operator/main/rbac.yaml 38 | # apply operator.yaml 39 | kubectl apply -f https://raw.githubusercontent.com/DeluxeOwl/subdomain-mapper-operator/main/operator.yaml 40 | 41 | # example service and pod 42 | kubectl apply -f https://raw.githubusercontent.com/DeluxeOwl/subdomain-mapper-operator/main/examples/example.yaml 43 | ``` 44 | 45 | ## Development 46 | 47 | 1. Build it yourself (multi-arch): 48 | ```sh 49 | docker buildx ls 50 | docker buildx create --name multi-arch 51 | docker buildx use multi-arch 52 | docker buildx inspect --bootstrap 53 | docker buildx build --platform linux/amd64,linux/arm64,linux/arm/v7 -t username/subdomain-mapper:latest --push . 54 | ``` 55 | 2. Change the image from [operator.yaml](operator.yaml) to use your image. 56 | 57 | 58 | ### todo 59 | - add tests 60 | -------------------------------------------------------------------------------- /examples/example.yaml: -------------------------------------------------------------------------------- 1 | apiVersion: v1 2 | kind: Pod 3 | metadata: 4 | labels: 5 | app: automatic-nginx-subdomain 6 | name: some-nginx-pod 7 | spec: 8 | containers: 9 | - image: nginx:alpine 10 | name: nginx 11 | 12 | --- 13 | apiVersion: v1 14 | kind: Service 15 | metadata: 16 | labels: 17 | app: automatic-nginx-subdomain 18 | name: automatic-nginx-subdomain 19 | annotations: 20 | subdomain-mapper/ingress: "main-ingress" 21 | subdomain-mapper/domain: "andreisurugiu.com" 22 | spec: 23 | ports: 24 | - name: 80-80 25 | port: 80 26 | protocol: TCP 27 | targetPort: 80 28 | selector: 29 | app: automatic-nginx-subdomain 30 | type: ClusterIP 31 | -------------------------------------------------------------------------------- /hooks/service-hook.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | ARRAY_COUNT=`jq -r '. | length-1' $BINDING_CONTEXT_PATH` 4 | 5 | 6 | 7 | if [[ $1 == "--config" ]] ; then 8 | cat <