Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
jakubkulhan committed Sep 26, 2018
1 parent af529a8 commit bee5ea3
Show file tree
Hide file tree
Showing 13 changed files with 884 additions and 0 deletions.
21 changes: 21 additions & 0 deletions .circleci/config.yml
@@ -0,0 +1,21 @@
version: 2
jobs:
build:
docker:
- image: circleci/golang
working_directory: /go/src/github.com/jakubkulhan/ingress-merge
steps:
- checkout
- setup_remote_docker
- run:
command: |
docker build -t jakubkulhan/ingress-merge:latest .
if [ "$CIRCLE_BRANCH" == "master" ]; then
docker login --username $DOCKER_USERNAME --password $DOCKER_PASSWORD
docker push jakubkulhan/ingress-merge:latest
fi
workflows:
version: 2
wf:
jobs:
- build
1 change: 1 addition & 0 deletions .gitignore
@@ -0,0 +1 @@
/go.sum
12 changes: 12 additions & 0 deletions Dockerfile
@@ -0,0 +1,12 @@
FROM golang:1.11

COPY . /src

RUN set -ex \
&& cd /src \
&& CGO_ENABLED=0 GOOS=linux go build -ldflags '-extldflags "-static"' -o /bin/ingress-merge ./cmd/ingress-merge

FROM scratch
COPY --from=0 /bin/ingress-merge /ingress-merge
ENTRYPOINT ["/ingress-merge"]
CMD ["--logtostderr"]
22 changes: 22 additions & 0 deletions LICENSE
@@ -0,0 +1,22 @@
Copyright (c) 2018 Jakub Kulhan <jakub.kulhan@gmail.com>

Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following
conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
126 changes: 126 additions & 0 deletions README.md
@@ -0,0 +1,126 @@
# Merge Ingress Controller

Merge [Ingress](https://kubernetes.io/docs/concepts/services-networking/ingress/) Controller combines multiple ingress
resources into a new one.

## Motivation

Different ingress controllers [behave differently](https://github.com/kubernetes/ingress-nginx/issues/1539#issue-266008311)
when creating _services_/_load balancers_ satisfying the ingress resources of the managed class. Some create single service/LB
for all ingress resources, some merge resources according to hosts or TLS certificates, other create separate service/LB
per ingress resource.

E.g. AWS ALB Ingress Controller creates a new load balancer for each ingress resource. This can become quite costly.
There is [an issue](https://github.com/kubernetes-sigs/aws-alb-ingress-controller/issues/298) to support reusing ALBs
across ingress resources, however, it won't be implemented anytime soon.

Merge Ingress Controller allows you to create ingress resources that will be combined together to create a new ingress
resource that will be managed by different controller.

## Setup

Install via [Helm](https://www.helm.sh/):

```sh
helm install --namespace kube-system --name ingress-merge ./helm
```

## Example

Create multiple ingresses & one config map that will provide parameters for the result ingress:

```yaml
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
name: foo-ingress
annotations:
kubernetes.io/ingress.class: merge
merge.ingress.kubernetes.io/config: merged-ingress
spec:
rules:
- host: foo.example.com
http:
paths:
- path: /
backend:
serviceName: foo-svc
servicePort: 80

---
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
name: bar-ingress
annotations:
kubernetes.io/ingress.class: merge
merge.ingress.kubernetes.io/config: merged-ingress
spec:
rules:
- host: bar.example.com
http:
paths:
- path: /
backend:
serviceName: bar-svc
servicePort: 80

---
apiVersion: v1
kind: ConfigMap
metadata:
name: merged-ingress
data:
annotations: |
kubernetes.io/ingress.class: other
```

Merge Ingress Controller will create new ingress resource named by the config map with rules combined together:

```yaml
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
name: merged-ingress
annotations:
kubernetes.io/ingress.class: other
spec:
rules:
- host: bar.example.com
http:
paths:
- path: /
backend:
serviceName: bar-svc
servicePort: 80

- host: foo.example.com
http:
paths:
- path: /
backend:
serviceName: foo-svc
servicePort: 80
```

## Annotations

| Annotation | Default Value | Description | Example |
|------------|---------------|-------------|---------|
| `kubernetes.io/ingress.class` | | Use `merge` for this controller to take over. | `kubernetes.io/ingress.class: merge` |
| `merge.ingress.kubernetes.io/config` | | Name of the [`ConfigMap`](https://kubernetes.io/docs/tutorials/configuration/) resource that will be used to merge this ingress with others. Because ingresses do not support to reference services across namespaces, neither does this reference. All ingresses to be merged, the config map & the result ingress use the same namespace. | `merge.ingress.kubernetes.io/config: merged-ingress` |
| `merge.ingress.kubernetes.io/priority` | `0` | Rules from ingresses with higher priority come in the result ingress rules first. | `merge.ingress.kubernetes.io/priority: 10` |
| `merge.ingress.kubernetes.io/result` | | Marks ingress created by the controller. If all source ingress resources are deleted, this ingress is deleted as well. | `merge.ingress.kubernetes.io/result: "true"` |

## Configuration keys

| Key | Default Value | Description | Example |
|-----|---------------|-------------|---------|
| `name` | _name of the `ConfigMap`_ | Name of the result ingress resource. | `name: my-merged-ingress` |
| `labels` | | YAML/JSON-serialized labels to be applied to the result ingress. | `labels: '{"app": "loadbalancer", "env": "prod"}'` |
| `annotations` | `{"merge.ingress.kubernetes.io/result":"true"}` | YAML/JSON-serialized labels to be applied to the result ingress. `merge.ingress.kubernetes.io/result` with value `true` will be always added to the annotations. | `annotations: '{"kubernetes.io/ingress.class": "alb"}` |
| `backend` | | Default backend for the result ingress (`spec.backend`). Source ingresses **must not** specify default backend (such ingresses won't be merged). | `backend: '{"serviceName": "default-backend-svc", "servicePort": 80}` |

## License

Licensed under MIT license. See `LICENSE` file.
78 changes: 78 additions & 0 deletions cmd/ingress-merge/main.go
@@ -0,0 +1,78 @@
package main

import (
"context"
goflag "flag"
"log"
"os"
"os/signal"
"syscall"

"github.com/golang/glog"
"github.com/jakubkulhan/ingress-merge"
"github.com/spf13/cobra"
)

func main() {
rootCmd := &cobra.Command{
Use: os.Args[0],
Short: "Merge Ingress Controller",
RunE: func(cmd *cobra.Command, args []string) error {
var (
err error
controller = ingress_merge.NewController()
)

if controller.MasterURL, err = cmd.Flags().GetString("apiserver"); err != nil {
return err
}

if controller.KubeconfigPath, err = cmd.Flags().GetString("kubeconfig"); err != nil {
return err
}

if controller.IngressClass, err = cmd.Flags().GetString("ingress-class"); err != nil {
return err
}

ctx, cancel := context.WithCancel(context.Background())
interrupts := make(chan os.Signal, 1)
go func() {
select {
case <-interrupts:
cancel()
}
}()
signal.Notify(interrupts, syscall.SIGINT, syscall.SIGTERM)

glog.Infoln("Starting controller")

return controller.Run(ctx)
},
}

rootCmd.PersistentFlags().String(
"apiserver",
"",
"Address of Kubernetes API server.",
)

rootCmd.PersistentFlags().String(
"kubeconfig",
"",
"Path to kubeconfig file.",
)

rootCmd.PersistentFlags().AddGoFlagSet(goflag.CommandLine)
goflag.CommandLine.Parse([]string{}) // prevents glog errors

rootCmd.Flags().String(
"ingress-class",
"merge",
"Process ingress resources with this `kubernetes.io/ingress.class` annotation.",
)

if err := rootCmd.Execute(); err != nil {
log.Fatal(err)
}
}

0 comments on commit bee5ea3

Please sign in to comment.