Skip to content

Commit

Permalink
Adding a new User Tag to App Gateway with the Last Update from AGIC (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
draychev authored and akshaysngupta committed Oct 23, 2019
1 parent cdd4f21 commit de9d025
Show file tree
Hide file tree
Showing 16 changed files with 65 additions and 17 deletions.
10 changes: 5 additions & 5 deletions functional_tests/functional_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,16 @@ import (
"k8s.io/client-go/tools/record"

"github.com/Azure/application-gateway-kubernetes-ingress/pkg/annotations"
. "github.com/Azure/application-gateway-kubernetes-ingress/pkg/appgw"
"github.com/Azure/application-gateway-kubernetes-ingress/pkg/crd_client/agic_crd_client/clientset/versioned/fake"
istio_fake "github.com/Azure/application-gateway-kubernetes-ingress/pkg/crd_client/istio_crd_client/clientset/versioned/fake"
"github.com/Azure/application-gateway-kubernetes-ingress/pkg/environment"
"github.com/Azure/application-gateway-kubernetes-ingress/pkg/k8scontext"
"github.com/Azure/application-gateway-kubernetes-ingress/pkg/tests"
"github.com/Azure/application-gateway-kubernetes-ingress/pkg/version"

. "github.com/Azure/application-gateway-kubernetes-ingress/pkg/appgw"
"github.com/Azure/application-gateway-kubernetes-ingress/pkg/metricstore"
"github.com/Azure/application-gateway-kubernetes-ingress/pkg/tests"
"github.com/Azure/application-gateway-kubernetes-ingress/pkg/tests/mocks"
"github.com/Azure/application-gateway-kubernetes-ingress/pkg/utils"
"github.com/Azure/application-gateway-kubernetes-ingress/pkg/version"
)

func TestFunctional(t *testing.T) {
Expand Down Expand Up @@ -345,7 +345,7 @@ var _ = ginkgo.Describe("Tests `appgw.ConfigBuilder`", func() {
ApplicationGatewayPropertiesFormat: NewAppGwyConfigFixture(),
}

configBuilder = NewConfigBuilder(ctxt, &appGwIdentifier, appGwy, record.NewFakeRecorder(100))
configBuilder = NewConfigBuilder(ctxt, &appGwIdentifier, appGwy, record.NewFakeRecorder(100), mocks.Clock{})
})

ginkgo.AfterEach(func() {
Expand Down
1 change: 1 addition & 0 deletions functional_tests/one_ingress_slash_nothing.json
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@
},
"tags": {
"ingress-for-aks-cluster-id": "/subscriptions/subid/resourcegroups/aksresgp/providers/Microsoft.ContainerService/managedClusters/aksname",
"last-updated-by-k8s-ingress": "2009-11-17 20:34:58.651387237 +0000 UTC",
"managed-by-k8s-ingress": "a/b/c"
}
}
1 change: 1 addition & 0 deletions functional_tests/one_ingress_slash_slashnothing.json
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,7 @@
},
"tags": {
"ingress-for-aks-cluster-id": "/subscriptions/subid/resourcegroups/aksresgp/providers/Microsoft.ContainerService/managedClusters/aksname",
"last-updated-by-k8s-ingress": "2009-11-17 20:34:58.651387237 +0000 UTC",
"managed-by-k8s-ingress": "a/b/c"
}
}
1 change: 1 addition & 0 deletions functional_tests/three_ingresses.json
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,7 @@
},
"tags": {
"ingress-for-aks-cluster-id": "/subscriptions/subid/resourcegroups/aksresgp/providers/Microsoft.ContainerService/managedClusters/aksname",
"last-updated-by-k8s-ingress": "2009-11-17 20:34:58.651387237 +0000 UTC",
"managed-by-k8s-ingress": "a/b/c"
}
}
1 change: 1 addition & 0 deletions functional_tests/two_ingresses_same_domain_tls_notls.json
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,7 @@
},
"tags": {
"ingress-for-aks-cluster-id": "/subscriptions/subid/resourcegroups/aksresgp/providers/Microsoft.ContainerService/managedClusters/aksname",
"last-updated-by-k8s-ingress": "2009-11-17 20:34:58.651387237 +0000 UTC",
"managed-by-k8s-ingress": "a/b/c"
}
}
1 change: 1 addition & 0 deletions functional_tests/two_ingresses_slash_slashsomething.json
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,7 @@
},
"tags": {
"ingress-for-aks-cluster-id": "/subscriptions/subid/resourcegroups/aksresgp/providers/Microsoft.ContainerService/managedClusters/aksname",
"last-updated-by-k8s-ingress": "2009-11-17 20:34:58.651387237 +0000 UTC",
"managed-by-k8s-ingress": "a/b/c"
}
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ require (
github.com/spf13/pflag v1.0.3
go.opencensus.io v0.22.0 // indirect
golang.org/x/crypto v0.0.0-20190611184440-5c40567a22f8 // indirect
golang.org/x/lint v0.0.0-20190909230951-414d861bb4ac // indirect
golang.org/x/time v0.0.0-20190308202827-9d24e82272b4 // indirect
google.golang.org/api v0.7.0 // indirect
google.golang.org/appengine v1.6.1 // indirect
Expand Down
3 changes: 3 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,8 @@ golang.org/x/lint v0.0.0-20190227174305-5b3e6a55c961/go.mod h1:wehouNa3lNwaWXcvx
golang.org/x/lint v0.0.0-20190301231843-5614ed5bae6f/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE=
golang.org/x/lint v0.0.0-20190313153728-d0100b6bd8b3/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc=
golang.org/x/lint v0.0.0-20190409202823-959b441ac422/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc=
golang.org/x/lint v0.0.0-20190909230951-414d861bb4ac h1:8R1esu+8QioDxo4E4mX6bFztO+dMTM49DNAaWfO5OeY=
golang.org/x/lint v0.0.0-20190909230951-414d861bb4ac/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc=
golang.org/x/net v0.0.0-20170114055629-f2499483f923/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
Expand Down Expand Up @@ -322,6 +324,7 @@ golang.org/x/time v0.0.0-20161028155119-f51c12702a4d/go.mod h1:tRJNPiyCQ0inRvYxb
golang.org/x/time v0.0.0-20181108054448-85acf8d2951c/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
golang.org/x/time v0.0.0-20190308202827-9d24e82272b4 h1:SvFZT6jyqRaOeXpc5h/JSfZenJ2O330aBsf7JfSUXmQ=
golang.org/x/time v0.0.0-20190308202827-9d24e82272b4/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
golang.org/x/tools v0.0.0-20190313210603-aa82965741a9 h1:7Pf/N3ln54fsGsAPsSwSfFhxXGKWHMIRUI/T5x1GP90=
golang.org/x/tools v0.0.0-20190313210603-aa82965741a9/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
google.golang.org/api v0.3.1/go.mod h1:6wY9I6uQWHQ8EM57III9mq/AjF+i8G65rmVagqKMtkk=
google.golang.org/api v0.4.0/go.mod h1:8k5glujaEP+g9n7WNsDg8QP6cUVNI86fCNMcbazEtwE=
Expand Down
10 changes: 6 additions & 4 deletions pkg/appgw/appgw_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import (
"github.com/Azure/application-gateway-kubernetes-ingress/pkg/k8scontext"
"github.com/Azure/application-gateway-kubernetes-ingress/pkg/metricstore"
"github.com/Azure/application-gateway-kubernetes-ingress/pkg/tests"
"github.com/Azure/application-gateway-kubernetes-ingress/pkg/tests/mocks"
"github.com/Azure/application-gateway-kubernetes-ingress/pkg/utils"
"github.com/Azure/application-gateway-kubernetes-ingress/pkg/version"
)
Expand Down Expand Up @@ -379,10 +380,11 @@ var _ = Describe("Tests `appgw.ConfigBuilder`", func() {
}

// Check tags
Expect(len(appGW.Tags)).To(Equal(2))
Expect(len(appGW.Tags)).To(Equal(3))
expected := map[string]*string{
tags.ManagedByK8sIngress: to.StringPtr("a/b/c"),
tags.IngressForAKSClusterID: to.StringPtr("/subscriptions/subid/resourcegroups/aksresgp/providers/Microsoft.ContainerService/managedClusters/aksname"),
tags.ManagedByK8sIngress: to.StringPtr("a/b/c"),
tags.IngressForAKSClusterID: to.StringPtr("/subscriptions/subid/resourcegroups/aksresgp/providers/Microsoft.ContainerService/managedClusters/aksname"),
tags.LastUpdatedByK8sIngress: to.StringPtr("2009-11-17 20:34:58.651387237 +0000 UTC"),
}
Expect(appGW.Tags).To(Equal(expected))
}
Expand Down Expand Up @@ -446,7 +448,7 @@ var _ = Describe("Tests `appgw.ConfigBuilder`", func() {
appGw := &n.ApplicationGateway{
ApplicationGatewayPropertiesFormat: NewAppGwyConfigFixture(),
}
configBuilder = NewConfigBuilder(ctxt, &appGwIdentifier, appGw, record.NewFakeRecorder(100))
configBuilder = NewConfigBuilder(ctxt, &appGwIdentifier, appGw, record.NewFakeRecorder(100), mocks.Clock{})

_, ok := configBuilder.(*appGwConfigBuilder)
Expect(ok).Should(BeTrue(), "Unable to get the more specific configBuilder implementation")
Expand Down
12 changes: 11 additions & 1 deletion pkg/appgw/configbuilder.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package appgw

import (
"fmt"
"time"

n "github.com/Azure/azure-sdk-for-go/services/network/mgmt/2019-06-01/network"
"github.com/Azure/go-autorest/autorest/to"
Expand All @@ -22,6 +23,12 @@ import (
"github.com/Azure/application-gateway-kubernetes-ingress/pkg/version"
)

// Clock is an interface, which allows you to implement your own Time.
type Clock interface {
Now() time.Time

}

// ConfigBuilder is a builder for application gateway configuration
type ConfigBuilder interface {
PreBuildValidate(cbCtx *ConfigBuilderContext) error
Expand Down Expand Up @@ -52,15 +59,17 @@ type appGwConfigBuilder struct {
appGw n.ApplicationGateway
recorder record.EventRecorder
mem memoization
clock Clock
}

// NewConfigBuilder construct a builder
func NewConfigBuilder(context *k8scontext.Context, appGwIdentifier *Identifier, original *n.ApplicationGateway, recorder record.EventRecorder) ConfigBuilder {
func NewConfigBuilder(context *k8scontext.Context, appGwIdentifier *Identifier, original *n.ApplicationGateway, recorder record.EventRecorder, clock Clock) ConfigBuilder {
return &appGwConfigBuilder{
k8sContext: context,
appGwIdentifier: *appGwIdentifier,
appGw: *original,
recorder: recorder,
clock: clock,
}
}

Expand Down Expand Up @@ -202,4 +211,5 @@ func (c *appGwConfigBuilder) addTags() {
} else {
glog.V(5).Infof("Error while parsing cluster resource ID for tagging: %s", err)
}
c.appGw.Tags[tags.LastUpdatedByK8sIngress] = to.StringPtr(c.clock.Now().String())
}
5 changes: 4 additions & 1 deletion pkg/appgw/configbuilder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (
"github.com/Azure/application-gateway-kubernetes-ingress/pkg/k8scontext"
"github.com/Azure/application-gateway-kubernetes-ingress/pkg/metricstore"
"github.com/Azure/application-gateway-kubernetes-ingress/pkg/tests"
"github.com/Azure/application-gateway-kubernetes-ingress/pkg/tests/mocks"
"github.com/Azure/application-gateway-kubernetes-ingress/pkg/version"
)

Expand Down Expand Up @@ -206,7 +207,7 @@ var _ = Describe("Tests `appgw.ConfigBuilder`", func() {
ApplicationGatewayPropertiesFormat: NewAppGwyConfigFixture(),
}

configBuilder = NewConfigBuilder(ctxt, &appGwIdentifier, appGwy, record.NewFakeRecorder(100))
configBuilder = NewConfigBuilder(ctxt, &appGwIdentifier, appGwy, record.NewFakeRecorder(100), mocks.Clock{})
})

AfterEach(func() {
Expand Down Expand Up @@ -413,6 +414,7 @@ var _ = Describe("Tests `appgw.ConfigBuilder`", func() {
-- },
-- "tags": {
-- "ingress-for-aks-cluster-id": "/subscriptions/subid/resourcegroups/aksresgp/providers/Microsoft.ContainerService/managedClusters/aksname",
-- "last-updated-by-k8s-ingress": "2009-11-17 20:34:58.651387237 +0000 UTC",
-- "managed-by-k8s-ingress": "a/b/c"
-- }
--}`
Expand Down Expand Up @@ -641,6 +643,7 @@ var _ = Describe("Tests `appgw.ConfigBuilder`", func() {
-- },
-- "tags": {
-- "ingress-for-aks-cluster-id": "/subscriptions/subid/resourcegroups/aksresgp/providers/Microsoft.ContainerService/managedClusters/aksname",
-- "last-updated-by-k8s-ingress": "2009-11-17 20:34:58.651387237 +0000 UTC",
-- "managed-by-k8s-ingress": "a/b/c"
-- }
--}`
Expand Down
4 changes: 3 additions & 1 deletion pkg/appgw/public_and_private_ip_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (
"github.com/Azure/application-gateway-kubernetes-ingress/pkg/k8scontext"
"github.com/Azure/application-gateway-kubernetes-ingress/pkg/metricstore"
"github.com/Azure/application-gateway-kubernetes-ingress/pkg/tests"
"github.com/Azure/application-gateway-kubernetes-ingress/pkg/tests/mocks"
"github.com/Azure/application-gateway-kubernetes-ingress/pkg/utils"
"github.com/Azure/application-gateway-kubernetes-ingress/pkg/version"
)
Expand Down Expand Up @@ -275,7 +276,7 @@ var _ = Describe("Tests `appgw.ConfigBuilder`", func() {
}

// Initialize the `ConfigBuilder`
configBuilder := NewConfigBuilder(ctxt, &appGwIdentifier, appGwy, record.NewFakeRecorder(100))
configBuilder := NewConfigBuilder(ctxt, &appGwIdentifier, appGwy, record.NewFakeRecorder(100), mocks.Clock{})

Context("Tests Application Gateway config creation", func() {
cbCtx := &ConfigBuilderContext{
Expand Down Expand Up @@ -517,6 +518,7 @@ var _ = Describe("Tests `appgw.ConfigBuilder`", func() {
-- },
-- "tags": {
-- "ingress-for-aks-cluster-id": "/subscriptions/subid/resourcegroups/aksresgp/providers/Microsoft.ContainerService/managedClusters/aksname",
-- "last-updated-by-k8s-ingress": "2009-11-17 20:34:58.651387237 +0000 UTC",
-- "managed-by-k8s-ingress": "a/b/c"
-- }
--}`
Expand Down
5 changes: 3 additions & 2 deletions pkg/azure/tags/consts.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package tags

// An App Gateway tag: Resources tagged with this are exclusively managed by a Kubernetes Ingress.
const (
ManagedByK8sIngress = "managed-by-k8s-ingress"
IngressForAKSClusterID = "ingress-for-aks-cluster-id"
ManagedByK8sIngress = "managed-by-k8s-ingress"
IngressForAKSClusterID = "ingress-for-aks-cluster-id"
LastUpdatedByK8sIngress = "last-updated-by-k8s-ingress"
)
7 changes: 6 additions & 1 deletion pkg/controller/process.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ import (
"github.com/Azure/application-gateway-kubernetes-ingress/pkg/k8scontext"
)

type realClock struct{}

func (realClock) Now() time.Time { return time.Now() }
func (realClock) After(d time.Duration) <-chan time.Time { return time.After(d) }

// Process is the callback function that will be executed for every event
// in the EventQueue.
func (c AppGwIngressController) Process(event events.Event) error {
Expand Down Expand Up @@ -103,7 +108,7 @@ func (c AppGwIngressController) Process(event events.Event) error {
}

// Create a configbuilder based on current appgw config
configBuilder := appgw.NewConfigBuilder(c.k8sContext, &c.appGwIdentifier, &appGw, c.recorder)
configBuilder := appgw.NewConfigBuilder(c.k8sContext, &c.appGwIdentifier, &appGw, c.recorder, realClock{})

// Run validations on the Kubernetes resources which can suggest misconfiguration.
if err = configBuilder.PreBuildValidate(cbCtx); err != nil {
Expand Down
4 changes: 2 additions & 2 deletions pkg/k8scontext/k8scontext_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ var _ = ginkgo.Describe("K8scontext", func() {
// Search Pod list with a subset matching filter
service := v1.Service{
ObjectMeta: metav1.ObjectMeta{
Name: "service",
Name: "service",
Namespace: "test-ingress-controller",
},
Spec: v1.ServiceSpec{
Expand Down Expand Up @@ -268,7 +268,7 @@ var _ = ginkgo.Describe("K8scontext", func() {
// Filter with a same pod label but different namespace
service = v1.Service{
ObjectMeta: metav1.ObjectMeta{
Name: "service",
Name: "service",
Namespace: "different-namespace",
},
Spec: v1.ServiceSpec{
Expand Down
16 changes: 16 additions & 0 deletions pkg/tests/mocks/clock.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package mocks

import (
"time"
)

// Clock is a custom implementation of Time.
type Clock struct{}

// Now is a static never changing Time.
func (Clock) Now() time.Time {
return time.Date(2009, 11, 17, 20, 34, 58, 651387237, time.UTC)
}

// After is what it was before.
func (Clock) After(d time.Duration) <-chan time.Time { return time.After(d) }

0 comments on commit de9d025

Please sign in to comment.