Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

up2date with networking.k8s.io/v1 and go 1.19 #14

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
Open
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM golang:1.11
FROM golang:1.19

COPY . /src

Expand Down
32 changes: 21 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,15 @@ resource that will be managed by different controller.
Install via [Helm](https://www.helm.sh/):

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

## Example

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

```yaml
apiVersion: extensions/v1beta1
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: foo-ingress
Expand All @@ -43,12 +43,15 @@ spec:
http:
paths:
- path: /
pathType: Prefix
backend:
serviceName: foo-svc
servicePort: 80
service:
name: foo-svc
port:
number: 80

---
apiVersion: extensions/v1beta1
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: bar-ingress
Expand All @@ -61,9 +64,12 @@ spec:
http:
paths:
- path: /
pathType: Prefix
backend:
serviceName: bar-svc
servicePort: 80
service:
name: bar-svc
port:
number: 80

---
apiVersion: v1
Expand Down Expand Up @@ -91,16 +97,20 @@ spec:
paths:
- path: /
backend:
serviceName: bar-svc
servicePort: 80
service:
name: bar-svc
port:
number: 80

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

## Annotations
Expand Down
2 changes: 1 addition & 1 deletion cmd/ingress-merge/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"syscall"

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

Expand Down
46 changes: 23 additions & 23 deletions controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ import (
"github.com/ghodss/yaml"
"github.com/golang/glog"
"github.com/pkg/errors"
"k8s.io/api/core/v1"
extensionsV1beta1 "k8s.io/api/extensions/v1beta1"
v1 "k8s.io/api/core/v1"
networkingV1 "k8s.io/api/networking/v1"
metaV1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/client-go/kubernetes"
Expand Down Expand Up @@ -80,15 +80,15 @@ func (c *Controller) Run(ctx context.Context) (err error) {

c.ingressesIndex, c.ingressesInformer = cache.NewIndexerInformer(
cache.NewFilteredListWatchFromClient(
c.client.ExtensionsV1beta1().RESTClient(),
c.client.NetworkingV1().RESTClient(),
"ingresses",
"",
func(options *metaV1.ListOptions) {
options.LabelSelector = c.IngressSelector
duration := int64(math.MaxInt32)
options.TimeoutSeconds = &duration
}),
&extensionsV1beta1.Ingress{},
&networkingV1.Ingress{},
0,
c,
cache.Indexers{},
Expand Down Expand Up @@ -162,7 +162,7 @@ func (c *Controller) Run(ctx context.Context) (err error) {
func (c *Controller) isIgnored(obj interface{}) bool {

switch object := obj.(type) {
case *extensionsV1beta1.Ingress:
case *networkingV1.Ingress:
for _, val := range c.IngressWatchIgnore {
if _, exists := object.Annotations[val]; exists {
return true
Expand Down Expand Up @@ -211,12 +211,12 @@ func (c *Controller) Process(ctx context.Context) {
glog.Infof("Processing ingress resources")

var (
mergeMap = make(map[*v1.ConfigMap][]*extensionsV1beta1.Ingress)
orphaned = make(map[string]*extensionsV1beta1.Ingress)
mergeMap = make(map[*v1.ConfigMap][]*networkingV1.Ingress)
orphaned = make(map[string]*networkingV1.Ingress)
)

for _, ingressIface := range c.ingressesIndex.List() {
ingress := ingressIface.(*extensionsV1beta1.Ingress)
ingress := ingressIface.(*networkingV1.Ingress)

ingressClass := ingress.Annotations[IngressClassAnnotation]
if ingressClass != c.IngressClass {
Expand Down Expand Up @@ -302,13 +302,13 @@ func (c *Controller) Process(ctx context.Context) {

var (
ownerReferences []metaV1.OwnerReference
tls []extensionsV1beta1.IngressTLS
rules []extensionsV1beta1.IngressRule
tls []networkingV1.IngressTLS
rules []networkingV1.IngressRule
)

for _, ingress := range ingresses {
ownerReferences = append(ownerReferences, metaV1.OwnerReference{
APIVersion: "extensions/v1beta1",
APIVersion: "networking.k8s.io/v1",
Kind: "Ingress",
Name: ingress.Name,
UID: ingress.UID,
Expand Down Expand Up @@ -338,7 +338,7 @@ func (c *Controller) Process(ctx context.Context) {
name string
labels map[string]string
annotations map[string]string
backend *extensionsV1beta1.IngressBackend
backend *networkingV1.IngressBackend
)

if dataName, exists := configMap.Data[NameConfigKey]; exists {
Expand Down Expand Up @@ -384,27 +384,27 @@ func (c *Controller) Process(ctx context.Context) {
}
}

mergedIngres := &extensionsV1beta1.Ingress{
mergedIngres := &networkingV1.Ingress{
ObjectMeta: metaV1.ObjectMeta{
Namespace: configMap.Namespace,
Name: name,
Labels: labels,
Annotations: annotations,
OwnerReferences: ownerReferences,
},
Spec: extensionsV1beta1.IngressSpec{
Backend: backend,
TLS: tls,
Rules: rules,
Spec: networkingV1.IngressSpec{
DefaultBackend: backend,
TLS: tls,
Rules: rules,
},
}

if existingMergedIngressIface, exists, _ := c.ingressesIndex.Get(mergedIngres); exists {
existingMergedIngress := existingMergedIngressIface.(*extensionsV1beta1.Ingress)
existingMergedIngress := existingMergedIngressIface.(*networkingV1.Ingress)

if hasIngressChanged(existingMergedIngress, mergedIngres) {
changed = true
ret, err := c.client.ExtensionsV1beta1().Ingresses(mergedIngres.Namespace).Update(mergedIngres)
ret, err := c.client.NetworkingV1().Ingresses(mergedIngres.Namespace).Update(ctx, mergedIngres, metaV1.UpdateOptions{})
if err != nil {
glog.Errorf("Could not update ingress [%s/%s]: %v", mergedIngres.Namespace, mergedIngres.Name, err)
continue
Expand All @@ -417,7 +417,7 @@ func (c *Controller) Process(ctx context.Context) {

} else {
changed = true
ret, err := c.client.ExtensionsV1beta1().Ingresses(mergedIngres.Namespace).Create(mergedIngres)
ret, err := c.client.NetworkingV1().Ingresses(mergedIngres.Namespace).Create(ctx, mergedIngres, metaV1.CreateOptions{})
if err != nil {
glog.Errorf("Could not create ingress [%s/%s]: %v", mergedIngres.Namespace, mergedIngres.Name, err)
continue
Expand All @@ -437,7 +437,7 @@ func (c *Controller) Process(ctx context.Context) {
mergedIngres.Status.DeepCopyInto(&ingress.Status)

changed = true
ret, err := c.client.ExtensionsV1beta1().Ingresses(ingress.Namespace).UpdateStatus(ingress)
ret, err := c.client.NetworkingV1().Ingresses(ingress.Namespace).UpdateStatus(ctx, ingress, metaV1.UpdateOptions{})
if err != nil {
glog.Errorf("Could not update status of ingress [%s/%s]: %v", ingress.Namespace, ingress.Name, err)
continue
Expand All @@ -458,7 +458,7 @@ func (c *Controller) Process(ctx context.Context) {

for _, ingress := range orphaned {
changed = true
err := c.client.ExtensionsV1beta1().Ingresses(ingress.Namespace).Delete(ingress.Name, nil)
err := c.client.NetworkingV1().Ingresses(ingress.Namespace).Delete(ctx, ingress.Name, metaV1.DeleteOptions{})
if err != nil {
glog.Errorf("Could not delete ingress [%s/%s]: %v", ingress.Namespace, ingress.Name, err)
continue
Expand All @@ -474,7 +474,7 @@ func (c *Controller) Process(ctx context.Context) {
}
}

func hasIngressChanged(old, new *extensionsV1beta1.Ingress) bool {
func hasIngressChanged(old, new *networkingV1.Ingress) bool {
if new.Namespace != old.Namespace {
return true
}
Expand Down
79 changes: 50 additions & 29 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,37 +1,58 @@
module github.com/jakubkulhan/ingress-merge

go 1.19

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/ghodss/yaml v1.0.0
github.com/gogo/protobuf v1.1.1 // indirect
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b
github.com/golang/groupcache v0.0.0-20180924190550-6f2cf27854a4 // indirect
github.com/golang/protobuf v1.2.0 // indirect
github.com/google/btree v0.0.0-20180813153112-4030bb1f1f0c // indirect
github.com/google/gofuzz v0.0.0-20170612174753-24818f796faf // indirect
github.com/googleapis/gnostic v0.2.0 // indirect
github.com/gregjones/httpcache v0.0.0-20180305231024-9cad4c3443a7 // indirect
github.com/hashicorp/golang-lru v0.5.0 // indirect
k8s.io/apimachinery v0.25.4
)

require (
github.com/PuerkitoBio/purell v1.1.1 // indirect
github.com/PuerkitoBio/urlesc v0.0.0-20170810143723-de5bf2ad4578 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/emicklei/go-restful/v3 v3.8.0 // indirect
github.com/go-logr/logr v1.2.3 // indirect
github.com/go-openapi/jsonpointer v0.19.5 // indirect
github.com/go-openapi/jsonreference v0.19.5 // indirect
github.com/go-openapi/swag v0.19.14 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/google/gnostic v0.5.7-v3refs // indirect
github.com/google/go-cmp v0.5.8 // indirect
github.com/google/gofuzz v1.1.0 // indirect
github.com/imdario/mergo v0.3.6 // indirect
github.com/inconshreveable/mousetrap v1.0.0 // indirect
github.com/json-iterator/go v1.1.5 // indirect
github.com/inconshreveable/mousetrap v1.0.1 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/mailru/easyjson v0.7.6 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.1 // indirect
github.com/peterbourgon/diskv v2.0.1+incompatible // indirect
github.com/pkg/errors v0.8.1
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/spf13/cobra v0.0.3
github.com/spf13/pflag v1.0.2 // indirect
github.com/stretchr/testify v1.2.2 // indirect
golang.org/x/crypto v0.0.0-20180910181607-0e37d006457b // indirect
golang.org/x/net v0.0.0-20180921000356-2f5d2388922f // indirect
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f // indirect
golang.org/x/sys v0.0.0-20180920110915-d641721ec2de // indirect
golang.org/x/text v0.3.0 // indirect
golang.org/x/time v0.0.0-20180412165947-fbb02b2291d2 // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
github.com/spf13/pflag v1.0.5 // indirect
golang.org/x/net v0.0.0-20220722155237-a158d28d115b // indirect
golang.org/x/oauth2 v0.0.0-20211104180415-d3ed0bb246c8 // indirect
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f // indirect
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211 // indirect
golang.org/x/text v0.3.7 // indirect
golang.org/x/time v0.0.0-20220210224613-90d013bbcef8 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/protobuf v1.28.0 // indirect
gopkg.in/inf.v0 v0.9.1 // indirect
gopkg.in/yaml.v2 v2.2.1 // indirect
k8s.io/api v0.0.0-20180913155108-f456898a08e4
k8s.io/apimachinery v0.0.0-20180621070125-103fd098999d
k8s.io/client-go v8.0.0+incompatible
gopkg.in/yaml.v3 v3.0.1 // indirect
k8s.io/klog/v2 v2.70.1 // indirect
k8s.io/kube-openapi v0.0.0-20220803162953-67bda5d908f1 // indirect
k8s.io/utils v0.0.0-20220728103510-ee6ede2d64ed // indirect
sigs.k8s.io/json v0.0.0-20220713155537-f223a00ba0e2 // indirect
sigs.k8s.io/structured-merge-diff/v4 v4.2.3 // indirect
sigs.k8s.io/yaml v1.2.0 // indirect
)

require (
github.com/golang/glog v1.0.0
github.com/pkg/errors v0.9.1
github.com/spf13/cobra v1.6.1
gopkg.in/yaml.v2 v2.4.0 // indirect
k8s.io/api v0.25.4
k8s.io/client-go v0.25.4
)
6 changes: 3 additions & 3 deletions helm/templates/01_rbac.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ metadata:
heritage: {{ .Release.Service }}
release: {{ .Release.Name }}
---
apiVersion: rbac.authorization.k8s.io/v1beta1
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
name: {{ include "ingress-merge.fullname" . }}
Expand All @@ -28,7 +28,7 @@ rules:
- list
- watch
- apiGroups:
- extensions
- networking.k8s.io
resources:
- ingresses
- ingresses/status
Expand All @@ -41,7 +41,7 @@ rules:
- patch
- delete
---
apiVersion: rbac.authorization.k8s.io/v1beta1
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
name: {{ include "ingress-merge.fullname" . }}
Expand Down
2 changes: 1 addition & 1 deletion helm/templates/02_deployment.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
apiVersion: apps/v1beta2
apiVersion: apps/v1
kind: Deployment
metadata:
name: {{ include "ingress-merge.fullname" . }}
Expand Down