Skip to content

Commit

Permalink
update ingress api to networking/v1 (#249)
Browse files Browse the repository at this point in the history
This brings feed in line with newer versions of kubernetes which (optionally, and then obligatorily) use this API in preference to the extensions/v1beta1 one.
  • Loading branch information
aecay authored Aug 1, 2022
1 parent 765bd1c commit 56994e8
Show file tree
Hide file tree
Showing 10 changed files with 188 additions and 184 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ mocks: k8s/mocks/mocks.go

k8s/mocks/mocks.go:
go run github.com/golang/mock/[email protected] -package mocks -destination $@ \
k8s.io/client-go/kubernetes/typed/extensions/v1beta1 IngressInterface,IngressesGetter
k8s.io/client-go/kubernetes/typed/networking/v1 IngressInterface,IngressesGetter

test : build fakenginx mocks
@echo "== run tests"
Expand Down
14 changes: 7 additions & 7 deletions controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ import (
log "github.com/sirupsen/logrus"
"github.com/sky-uk/feed/k8s"
"github.com/sky-uk/feed/util"
v1 "k8s.io/api/core/v1"
"k8s.io/api/extensions/v1beta1"
corev1 "k8s.io/api/core/v1"
networkingv1 "k8s.io/api/networking/v1"
)

// Deprecated: retained to maintain backwards compatibility.
Expand Down Expand Up @@ -188,7 +188,7 @@ func (c *controller) updateIngresses() (err error) {
}()

// Get ingresses
var ingresses []*v1beta1.Ingress
var ingresses []*networkingv1.Ingress

if c.namespaceSelectors == nil {
ingresses, err = c.client.GetAllIngresses()
Expand Down Expand Up @@ -231,7 +231,7 @@ func (c *controller) updateIngresses() (err error) {
if rule.HTTP != nil {
for _, path := range rule.HTTP.Paths {

serviceName := serviceName{namespace: ingress.Namespace, name: path.Backend.ServiceName}
serviceName := serviceName{namespace: ingress.Namespace, name: path.Backend.Service.Name}

if address := serviceMap[serviceName]; address == "" {
skipped = append(skipped, fmt.Sprintf("%s/%s (service doesn't exist)", ingress.Namespace, ingress.Name))
Expand All @@ -245,7 +245,7 @@ func (c *controller) updateIngresses() (err error) {
Host: rule.Host,
Path: path.Path,
ServiceAddress: address,
ServicePort: int32(path.Backend.ServicePort.IntValue()),
ServicePort: path.Backend.Service.Port.Number,
Allow: c.defaultAllow,
StripPaths: c.defaultStripPath,
ExactPath: c.defaultExactPath, BackendTimeoutSeconds: c.defaultBackendTimeout,
Expand Down Expand Up @@ -381,7 +381,7 @@ func (c *controller) updateIngresses() (err error) {
return nil
}

func (c *controller) ingressClassSupported(ingress *v1beta1.Ingress) bool {
func (c *controller) ingressClassSupported(ingress *networkingv1.Ingress) bool {

isValid := false

Expand All @@ -399,7 +399,7 @@ type serviceName struct {
name string
}

func serviceNamesToClusterIPs(services []*v1.Service) map[serviceName]string {
func serviceNamesToClusterIPs(services []*corev1.Service) map[serviceName]string {
m := make(map[serviceName]string)

for _, svc := range services {
Expand Down
92 changes: 49 additions & 43 deletions controller/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,9 @@ import (
fake "github.com/sky-uk/feed/util/test"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
v1 "k8s.io/api/core/v1"
"k8s.io/api/extensions/v1beta1"
corev1 "k8s.io/api/core/v1"
networkingv1 "k8s.io/api/networking/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/intstr"
)

const smallWaitTime = time.Millisecond * 50
Expand Down Expand Up @@ -82,9 +81,9 @@ func createDefaultStubs() (*fakeUpdater, *fake.FakeClient) {
serviceWatcher, _ := createFakeWatcher()
namespaceWatcher, _ := createFakeWatcher()

client.On("GetAllIngresses").Return([]*v1beta1.Ingress{}, nil)
client.On("GetIngresses", mock.Anything, mock.AnythingOfType("bool")).Return([]*v1beta1.Ingress{}, nil)
client.On("GetServices").Return([]*v1.Service{}, nil)
client.On("GetAllIngresses").Return([]*networkingv1.Ingress{}, nil)
client.On("GetIngresses", mock.Anything, mock.AnythingOfType("bool")).Return([]*networkingv1.Ingress{}, nil)
client.On("GetServices").Return([]*corev1.Service{}, nil)
client.On("WatchIngresses").Return(ingressWatcher)
client.On("WatchServices").Return(serviceWatcher)
client.On("WatchNamespaces").Return(namespaceWatcher)
Expand Down Expand Up @@ -333,9 +332,9 @@ func defaultConfig() Config {

type testSpec struct {
description string
ingresses []*v1beta1.Ingress
services []*v1.Service
namespaces []*v1.Namespace
ingresses []*networkingv1.Ingress
services []*corev1.Service
namespaces []*corev1.Namespace
entries IngressEntries
config Config
}
Expand Down Expand Up @@ -389,13 +388,13 @@ func TestUpdaterIsUpdatedForIngressWithoutCorrespondingService(t *testing.T) {
runAndAssertUpdates(t, expectGetAllIngresses, testSpec{
"ingress without corresponding service",
createDefaultIngresses(),
[]*v1.Service{
[]*corev1.Service{
{
ObjectMeta: metav1.ObjectMeta{
Name: "non-default-name",
Namespace: "non-default-namespace",
},
Spec: v1.ServiceSpec{
Spec: corev1.ServiceSpec{
ClusterIP: "non-default-ip",
},
},
Expand Down Expand Up @@ -1228,7 +1227,7 @@ func TestNamespaceSelectorsIsUsedToGetIngresses(t *testing.T) {
updater.On("Health").Return(nil)

// The purpose of this test is to ensure that the NamespaceSelectors is passed to GetIngresses
client.On("GetIngresses", config.NamespaceSelectors, config.MatchAllNamespaceSelectors).Return([]*v1beta1.Ingress{}, nil)
client.On("GetIngresses", config.NamespaceSelectors, config.MatchAllNamespaceSelectors).Return([]*networkingv1.Ingress{}, nil)

ingressWatcher, ingressCh := createFakeWatcher()
serviceWatcher, serviceCh := createFakeWatcher()
Expand Down Expand Up @@ -1287,13 +1286,13 @@ func TestUpdaterIsUpdatedForIngressWithoutRulesDefinition(t *testing.T) {
})
}

type clientExpectation func(client *fake.FakeClient, ingresses []*v1beta1.Ingress)
type clientExpectation func(client *fake.FakeClient, ingresses []*networkingv1.Ingress)

var expectGetAllIngresses = func(client *fake.FakeClient, ingresses []*v1beta1.Ingress) {
var expectGetAllIngresses = func(client *fake.FakeClient, ingresses []*networkingv1.Ingress) {
client.On("GetAllIngresses").Return(ingresses, nil)
}

var expectGetIngresses = func(client *fake.FakeClient, ingresses []*v1beta1.Ingress) {
var expectGetIngresses = func(client *fake.FakeClient, ingresses []*networkingv1.Ingress) {
client.On("GetIngresses", &k8s.NamespaceSelector{LabelName: "team", LabelValue: "theteam"}).Return(ingresses, nil)
}

Expand Down Expand Up @@ -1345,7 +1344,7 @@ func runAndAssertUpdates(t *testing.T, clientExpectation clientExpectation, test
client.AssertExpectations(t)
}

func addIngresses(ingresses []*v1beta1.Ingress, entries IngressEntries) IngressEntries {
func addIngresses(ingresses []*networkingv1.Ingress, entries IngressEntries) IngressEntries {
if len(ingresses) != len(entries) {
return entries
}
Expand Down Expand Up @@ -1387,7 +1386,7 @@ const (
defaultMaxConnections = 0
)

func createDefaultIngresses() []*v1beta1.Ingress {
func createDefaultIngresses() []*networkingv1.Ingress {
return createIngressesFixture(ingressNamespace, ingressHost, ingressSvcName, ingressSvcPort, map[string]string{
ingressAllowAnnotation: ingressAllow,
backendTimeoutSeconds: "10",
Expand All @@ -1396,7 +1395,7 @@ func createDefaultIngresses() []*v1beta1.Ingress {
}, ingressPath)
}

func createIngressesFromNonELBAnnotation(legacyAnnotations bool) []*v1beta1.Ingress {
func createIngressesFromNonELBAnnotation(legacyAnnotations bool) []*networkingv1.Ingress {
var timeoutAnnotation, schemeAnnotation string
if legacyAnnotations {
timeoutAnnotation = legacyBackendKeepaliveSeconds
Expand All @@ -1414,8 +1413,8 @@ func createIngressesFromNonELBAnnotation(legacyAnnotations bool) []*v1beta1.Ingr
}, ingressPath)
}

func createIngressWithoutRules() []*v1beta1.Ingress {
return []*v1beta1.Ingress{
func createIngressWithoutRules() []*networkingv1.Ingress {
return []*networkingv1.Ingress{
{
ObjectMeta: metav1.ObjectMeta{
Name: ingressName,
Expand All @@ -1424,19 +1423,26 @@ func createIngressWithoutRules() []*v1beta1.Ingress {
ingressClassAnnotation: defaultIngressClass,
},
},
Spec: v1beta1.IngressSpec{},
Spec: networkingv1.IngressSpec{},
},
}

}

func createIngressesFixture(namespace string, host string, serviceName string, servicePort int, ingressAnnotations map[string]string, path string) []*v1beta1.Ingress {
func createIngressesFixture(namespace string, host string, serviceName string, servicePort int, ingressAnnotations map[string]string, path string) []*networkingv1.Ingress {

paths := []v1beta1.HTTPIngressPath{{
Path: path,
Backend: v1beta1.IngressBackend{
ServiceName: serviceName,
ServicePort: intstr.FromInt(servicePort),
pathType := networkingv1.PathTypeImplementationSpecific
paths := []networkingv1.HTTPIngressPath{{
Path: path,
PathType: &pathType,
Backend: networkingv1.IngressBackend{
Service: &networkingv1.IngressServiceBackend{
Name: serviceName,
Port: networkingv1.ServiceBackendPort{
Name: "",
Number: int32(servicePort),
},
},
},
}}

Expand Down Expand Up @@ -1474,14 +1480,14 @@ func createIngressesFixture(namespace string, host string, serviceName string, s
}

ingressDefinition := createIngressWithoutRules()
var ingressRules []v1beta1.IngressRule
var ingressRules []networkingv1.IngressRule

if host != "" {
ingressRuleValue := v1beta1.IngressRuleValue{}
ingressRuleValue := networkingv1.IngressRuleValue{}
if path != "" {
ingressRuleValue.HTTP = &v1beta1.HTTPIngressRuleValue{Paths: paths}
ingressRuleValue.HTTP = &networkingv1.HTTPIngressRuleValue{Paths: paths}
}
ingressRule := v1beta1.IngressRule{
ingressRule := networkingv1.IngressRule{
Host: host,
IngressRuleValue: ingressRuleValue,
}
Expand All @@ -1496,30 +1502,30 @@ func createIngressesFixture(namespace string, host string, serviceName string, s
return ingressDefinition
}

func createDefaultServices() []*v1.Service {
func createDefaultServices() []*corev1.Service {
return createServiceFixture(ingressSvcName, ingressNamespace, serviceIP)
}

func createServiceFixture(name string, namespace string, clusterIP string) []*v1.Service {
return []*v1.Service{
func createServiceFixture(name string, namespace string, clusterIP string) []*corev1.Service {
return []*corev1.Service{
{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Namespace: namespace,
},
Spec: v1.ServiceSpec{
Spec: corev1.ServiceSpec{
ClusterIP: clusterIP,
},
},
}
}

func createDefaultNamespaces() []*v1.Namespace {
func createDefaultNamespaces() []*corev1.Namespace {
return createNamespaceFixture(ingressNamespace, map[string]string{})
}

func createNamespaceFixture(name string, labels map[string]string) []*v1.Namespace {
return []*v1.Namespace{
func createNamespaceFixture(name string, labels map[string]string) []*corev1.Namespace {
return []*corev1.Namespace{
{
TypeMeta: metav1.TypeMeta{
Kind: "Namespace",
Expand All @@ -1529,8 +1535,8 @@ func createNamespaceFixture(name string, labels map[string]string) []*v1.Namespa
Name: name,
Labels: labels,
},
Spec: v1.NamespaceSpec{},
Status: v1.NamespaceStatus{},
Spec: corev1.NamespaceSpec{},
Status: corev1.NamespaceStatus{},
},
}
}
Expand Down Expand Up @@ -1562,7 +1568,7 @@ func TestUpdateFailsWhenK8sClientReturnsNoIngresses(t *testing.T) {
updater.On("Health").Return(nil)

// This is the call we are testing (by returning an empty array of ingresses)
client.On("GetAllIngresses").Return([]*v1beta1.Ingress{}, nil)
client.On("GetAllIngresses").Return([]*networkingv1.Ingress{}, nil)

ingressWatcher, ingressCh := createFakeWatcher()
serviceWatcher, serviceCh := createFakeWatcher()
Expand Down Expand Up @@ -1624,7 +1630,7 @@ func TestUpdateFailsWhenK8sClientReturnsNoNamespaceIngresses(t *testing.T) {
updater.On("Health").Return(nil)

// This is the call we are testing (by returning an empty array of ingresses)
client.On("GetIngresses", namespaceSelectors, false).Return([]*v1beta1.Ingress{}, nil)
client.On("GetIngresses", namespaceSelectors, false).Return([]*networkingv1.Ingress{}, nil)

ingressWatcher, ingressCh := createFakeWatcher()
serviceWatcher, serviceCh := createFakeWatcher()
Expand Down Expand Up @@ -1680,7 +1686,7 @@ func TestUpdateFailsWhenK8sClientReturnsNoServices(t *testing.T) {
// We have to return ingresses successfully first
client.On("GetAllIngresses").Return(test.ingresses, nil)
// This is the call we are testing (by returning an empty array of services)
client.On("GetServices").Return([]*v1.Service{}, nil)
client.On("GetServices").Return([]*corev1.Service{}, nil)

ingressWatcher, ingressCh := createFakeWatcher()
serviceWatcher, serviceCh := createFakeWatcher()
Expand Down
4 changes: 2 additions & 2 deletions controller/ingress_entry.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"strings"
"time"

"k8s.io/api/extensions/v1beta1"
v1 "k8s.io/api/networking/v1"
)

// IngressEntries type
Expand Down Expand Up @@ -49,7 +49,7 @@ type IngressEntry struct {
// Ingress creation time
CreationTimestamp time.Time
// Ingress resource
Ingress *v1beta1.Ingress
Ingress *v1.Ingress
// Size of the buffer used for reading the first part of the response received from the proxied server.
ProxyBufferSize int
// Number of buffers used for reading a response from the proxied server, for a single connection.
Expand Down
Loading

0 comments on commit 56994e8

Please sign in to comment.