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

[chore] finalizer removal & teardown logic cleanup #1522

Merged
merged 19 commits into from
Jul 12, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions pkg/store/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,46 @@ func NewCacheStoresFromObjs(objs ...runtime.Object) (CacheStores, error) {
return c, nil
}

// Get checks whether or not there's already some version of the provided object present in the cache.
func (c CacheStores) Get(obj runtime.Object) (item interface{}, exists bool, err error) {
switch obj := obj.(type) {
// ----------------------------------------------------------------------------
// Kubernetes Core API Support
// ----------------------------------------------------------------------------
case *extensions.Ingress:
return c.IngressV1beta1.Get(obj)
case *networkingv1.Ingress:
return c.IngressV1.Get(obj)
case *corev1.Service:
return c.Service.Get(obj)
case *corev1.Secret:
return c.Secret.Get(obj)
case *corev1.Endpoints:
return c.Endpoint.Get(obj)
// ----------------------------------------------------------------------------
// Kong API Support
// ----------------------------------------------------------------------------
case *kongv1.KongPlugin:
return c.Plugin.Get(obj)
case *kongv1.KongClusterPlugin:
return c.ClusterPlugin.Get(obj)
case *kongv1.KongConsumer:
return c.Consumer.Get(obj)
case *kongv1.KongIngress:
return c.KongIngress.Get(obj)
case *kongv1beta1.TCPIngress:
return c.TCPIngress.Get(obj)
case *kongv1beta1.UDPIngress:
return c.UDPIngress.Get(obj)
// ----------------------------------------------------------------------------
// 3rd Party API Support
// ----------------------------------------------------------------------------
case *knative.Ingress:
return c.KnativeIngress.Get(obj)
}
return nil, false, fmt.Errorf("%T is not a supported cache object type", obj)
}

// Add stores a provided runtime.Object into the CacheStore if it's of a supported type.
// The CacheStore must be initialized (see NewCacheStores()) or this will panic.
func (c CacheStores) Add(obj runtime.Object) error {
Expand Down
94 changes: 85 additions & 9 deletions pkg/store/store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,16 @@ package store

import (
"reflect"
"strings"
"testing"

"github.com/sirupsen/logrus"
core "k8s.io/api/core/v1"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
extensions "k8s.io/api/extensions/v1beta1"
netv1 "k8s.io/api/networking/v1"
networking "k8s.io/api/networking/v1beta1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/intstr"
Expand All @@ -31,11 +36,11 @@ func Test_networkingIngressV1Beta1(t *testing.T) {
{
name: "returns nil if a non-ingress object is passed in",
args: args{
&core.Service{
Spec: core.ServiceSpec{
Type: core.ServiceTypeClusterIP,
&corev1.Service{
Spec: corev1.ServiceSpec{
Type: corev1.ServiceTypeClusterIP,
ClusterIP: "1.1.1.1",
Ports: []core.ServicePort{
Ports: []corev1.ServicePort{
{
Name: "default",
TargetPort: intstr.FromString("port-1"),
Expand Down Expand Up @@ -75,8 +80,8 @@ func Test_networkingIngressV1Beta1(t *testing.T) {
},
},
Status: extensions.IngressStatus{
LoadBalancer: core.LoadBalancerStatus{
Ingress: []core.LoadBalancerIngress{{IP: "1.2.3.4"}},
LoadBalancer: corev1.LoadBalancerStatus{
Ingress: []corev1.LoadBalancerIngress{{IP: "1.2.3.4"}},
},
},
},
Expand Down Expand Up @@ -107,8 +112,8 @@ func Test_networkingIngressV1Beta1(t *testing.T) {
},
},
Status: networking.IngressStatus{
LoadBalancer: core.LoadBalancerStatus{
Ingress: []core.LoadBalancerIngress{{IP: "1.2.3.4"}},
LoadBalancer: corev1.LoadBalancerStatus{
Ingress: []corev1.LoadBalancerIngress{{IP: "1.2.3.4"}},
},
},
},
Expand All @@ -125,3 +130,74 @@ func Test_networkingIngressV1Beta1(t *testing.T) {
})
}
}

func TestCacheStoresGet(t *testing.T) {
t.Log("configuring some yaml objects to store in the cache")
svcYAML := []byte(`---
apiVersion: v1
kind: Service
metadata:
name: httpbin-deployment
namespace: default
labels:
app: httpbin
spec:
ports:
- port: 80
protocol: TCP
targetPort: 80
selector:
app: httpbin
type: ClusterIP
`)
ingYAML := []byte(`---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: httpbin-ingress
namespace: default
annotations:
httpbin.ingress.kubernetes.io/rewrite-target: /
kubernetes.io/ingress.class: "kong"
spec:
rules:
- http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: httpbin-deployment
port:
number: 80
`)

t.Log("creating a new cache store from object yaml files")
cs, err := NewCacheStoresFromObjYAML(svcYAML, ingYAML)
require.NoError(t, err)

t.Log("verifying that the cache store doesnt try to retrieve unsupported object types")
_, exists, err := cs.Get(new(appsv1.Deployment))
assert.Error(t, err)
assert.True(t, strings.Contains(err.Error(), "Deployment is not a supported cache object type"))
assert.False(t, exists)

t.Log("verifying the integrity of the cache store")
assert.Len(t, cs.IngressV1.List(), 1)
assert.Len(t, cs.Service.List(), 1)
assert.Len(t, cs.IngressV1beta1.List(), 0)
assert.Len(t, cs.KongIngress.List(), 0)
_, exists, err = cs.Get(&corev1.Service{ObjectMeta: metav1.ObjectMeta{Namespace: "doesntexist", Name: "doesntexist"}})
assert.NoError(t, err)
assert.False(t, exists)

t.Log("ensuring that we can Get() the objects back out of the cache store")
svc := &corev1.Service{ObjectMeta: metav1.ObjectMeta{Namespace: "default", Name: "httpbin-deployment"}}
ing := &netv1.Ingress{ObjectMeta: metav1.ObjectMeta{Namespace: "default", Name: "httpbin-ingress"}}
_, exists, err = cs.Get(svc)
assert.NoError(t, err)
assert.True(t, exists)
_, exists, err = cs.Get(ing)
assert.NoError(t, err)
assert.True(t, exists)
}
10 changes: 6 additions & 4 deletions pkg/util/debug_logging_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,18 +133,20 @@ func TestDebugLoggerThreadSafety(t *testing.T) {

// spam the logger concurrently across several goroutines to ensure no dataraces
wg := &sync.WaitGroup{}
for i := 0; i < 100; i++ {
wg.Add(1)
total := 100
wg.Add(total)
for i := 0; i < total; i++ {
go func() {
defer wg.Done()
log.Debug("unique")
}()
}
wg.Wait()
assert.Contains(t, buf.String(), "unique")
lines := strings.Split(buf.String(), "\n")

// Ensure that _some_ lines have been stifled. The actual number is not deterministic.
assert.True(t, len(lines) < 100)
lines := strings.Split(buf.String(), "\n")
assert.True(t, len(lines) < total)
}

// -----------------------------------------------------------------------------
Expand Down
6 changes: 3 additions & 3 deletions railgun/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -166,18 +166,18 @@ test.integration: test.integration.dbless test.integration.postgres
.PHONY: test.integration.dbless
test.integration.dbless:
@./scripts/setup-integration-tests.sh
@TEST_DATABASE_MODE="off" GOFLAGS="-tags=integration_tests" go test -timeout 20m -race -v -count=1 -covermode=atomic -coverpkg=$(PKG_LIST) -coverprofile=$(COVERAGE_INTEGRATION_PROFILE) ./test/integration/
@TEST_DATABASE_MODE="off" GOFLAGS="-tags=integration_tests" go test -timeout 15m -race -v -count=1 -covermode=atomic -coverpkg=$(PKG_LIST) -coverprofile=$(COVERAGE_INTEGRATION_PROFILE) ./test/integration/
rainest marked this conversation as resolved.
Show resolved Hide resolved

# Our integration tests using the postgres backend, with verbose output
# TODO: race checking has been temporarily turned off because of race conditions found with deck. This will be resolved in an upcoming Alpha release of KIC 2.0.
# See: https://github.com/Kong/kubernetes-ingress-controller/issues/1324
.PHONY: test.integration.postgres
test.integration.postgres:
@./scripts/setup-integration-tests.sh
@TEST_DATABASE_MODE="postgres" GOFLAGS="-tags=integration_tests" go test -timeout 20m -v -count=1 -covermode=atomic -coverpkg=$(PKG_LIST) -coverprofile=$(COVERAGE_INTEGRATION_PROFILE) ./test/integration/
@TEST_DATABASE_MODE="postgres" GOFLAGS="-tags=integration_tests" go test -timeout 15m -v -count=1 -covermode=atomic -coverpkg=$(PKG_LIST) -coverprofile=$(COVERAGE_INTEGRATION_PROFILE) ./test/integration/

# Our integration tests using the legacy v1 controller manager
.PHONY: test.integration.legacy
test.integration.legacy:
@./scripts/setup-integration-tests.sh
@KONG_LEGACY_CONTROLLER=1 GOFLAGS="-tags=integration_tests" go test -timeout 20m -race -v -count=1 -covermode=atomic -coverpkg=$(PKG_LIST) -coverprofile=$(COVERAGE_INTEGRATION_PROFILE) ./test/integration/
@KONG_LEGACY_CONTROLLER=1 GOFLAGS="-tags=integration_tests" go test -timeout 15m -race -v -count=1 -covermode=atomic -coverpkg=$(PKG_LIST) -coverprofile=$(COVERAGE_INTEGRATION_PROFILE) ./test/integration/
Loading