Skip to content

Commit

Permalink
Enable Ingress to Gslb Owner Reference
Browse files Browse the repository at this point in the history
* Enables proper Ingress -> Gslb CR ownership in case of
annotation based management approach
with pre-existing Ingress as in

https://www.k8gb.io/docs/ingress_annotations.html

* Resolves #361

* Covered by Terratest

Signed-off-by: Yury Tsarev <yury.tsarev@absa.africa>
  • Loading branch information
ytsarev committed Apr 6, 2021
1 parent c105f41 commit c68f4f8
Show file tree
Hide file tree
Showing 7 changed files with 282 additions and 48 deletions.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -434,8 +434,8 @@ endef

define docker-test-build-push
docker build . -t k8gb:$(COMMIT_HASH)
docker tag k8gb:$(COMMIT_HASH) $(REPO):v$(COMMIT_HASH)
docker push $(REPO):v$(COMMIT_HASH)
docker tag k8gb:$(COMMIT_HASH) $(REPO):$(COMMIT_HASH)
docker push $(REPO):$(COMMIT_HASH)
sed -i "s/$(VERSION)/$(COMMIT_HASH)/g" chart/k8gb/Chart.yaml
endef

Expand Down
4 changes: 2 additions & 2 deletions chart/k8gb/Chart.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ apiVersion: v2
name: k8gb
description: A Helm chart for Kubernetes Global Balancer
type: application
version: v0.7.7
appVersion: v0.7.7
version: v0.7.8
appVersion: v0.7.8

dependencies:
- name: coredns
Expand Down
10 changes: 10 additions & 0 deletions controllers/gslb_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import (
types "k8s.io/apimachinery/pkg/types"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
"sigs.k8s.io/controller-runtime/pkg/handler"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
"sigs.k8s.io/controller-runtime/pkg/source"
Expand Down Expand Up @@ -248,12 +249,21 @@ func (r *GslbReconciler) SetupWithManager(mgr ctrl.Manager) error {
}
}

err = controllerutil.SetControllerReference(ingressToReuse, gslb, r.Scheme)
if err != nil {
log.Err(err).
Str("Ingress", ingressToReuse.Name).
Str("Gslb", gslb.Name).
Msg("Cannot set the Ingress as the owner of the Gslb")
}

log.Info().Msgf("Creating new Gslb(%s) out of Ingress annotation", gslb.Name)
err = c.Create(context.Background(), gslb)
if err != nil {
log.Err(err).Msg("Glsb creation failed")
}
}

ingressMapFn := handler.ToRequestsFunc(
func(a handler.MapObject) []reconcile.Request {
for annotationKey, annotationValue := range a.Meta.GetAnnotations() {
Expand Down
280 changes: 240 additions & 40 deletions terratest/test/go.sum

Large diffs are not rendered by default.

18 changes: 18 additions & 0 deletions terratest/test/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,3 +167,21 @@ func assertDNSEndpointLabel(t *testing.T, options *k8s.KubectlOptions, label str
t.Helper()
k8s.RunKubectl(t, options, "get", "dnsendpoint", "-l", label)
}

func assertGslbDeleted(t *testing.T, options *k8s.KubectlOptions, gslbName string) {
t.Helper()
deletionExpected := []string{fmt.Sprintf("Error from server (NotFound): gslbs.k8gb.absa.oss \"%s\" not found", gslbName)}
deletionActual, err := DoWithRetryWaitingForValueE(
t,
"Waiting for Gslb CR to be deleted...",
300,
1*time.Second,
func() ([]string, error) {
out, err := k8s.RunKubectlAndGetOutputE(t, options, "get", "gslb", gslbName)
return []string{out}, err
},
deletionExpected)
require.NoError(t, err)

assert.Equal(t, deletionExpected, deletionActual)
}
7 changes: 5 additions & 2 deletions terratest/test/k8gb_ingress_annotation_failover_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,6 @@ func TestK8gbIngressAnnotationFailover(t *testing.T) {

defer k8s.DeleteNamespace(t, options, namespaceName)

defer k8s.KubectlDelete(t, options, kubeResourcePath)

k8s.KubectlApply(t, options, kubeResourcePath)

ingress := k8s.GetIngress(t, options, "test-gslb-annotation-failover")
Expand All @@ -69,4 +67,9 @@ func TestK8gbIngressAnnotationFailover(t *testing.T) {
err := k8s.RunKubectlE(t, options, "get", "gslb", "broken-test-gslb-annotation-failover")
require.Error(t, err)
})

t.Run("Gslb is getting deleted together with the annotated Ingress", func(t *testing.T) {
k8s.KubectlDelete(t, options, kubeResourcePath)
assertGslbDeleted(t, options, ingress.Name)
})
}
7 changes: 5 additions & 2 deletions terratest/test/k8gb_ingress_annotation_rr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,15 @@ func TestK8gbIngressAnnotationRR(t *testing.T) {

defer k8s.DeleteNamespace(t, options, namespaceName)

defer k8s.KubectlDelete(t, options, kubeResourcePath)

k8s.KubectlApply(t, options, kubeResourcePath)

ingress := k8s.GetIngress(t, options, "test-gslb-annotation")
require.Equal(t, ingress.Name, "test-gslb-annotation")
assertGslbStatus(t, options, "test-gslb-annotation", "notfound.cloud.example.com:NotFound roundrobin.cloud.example.com:NotFound unhealthy.cloud.example.com:NotFound")
assertGslbSpec(t, options, "test-gslb-annotation", ".spec.strategy.type", "roundRobin")

t.Run("Gslb is getting deleted together with the annotated Ingress", func(t *testing.T) {
k8s.KubectlDelete(t, options, kubeResourcePath)
assertGslbDeleted(t, options, ingress.Name)
})
}

0 comments on commit c68f4f8

Please sign in to comment.