From a22638f0b6867001e51643f4f10341bdacdaa31f Mon Sep 17 00:00:00 2001 From: Sanskar Jaiswal Date: Tue, 22 Mar 2022 15:12:08 +0530 Subject: [PATCH] add annotation to enable appmesh logs Signed-off-by: Sanskar Jaiswal Co-authored-by: wucg --- pkg/apis/appmesh/register.go | 4 ++++ pkg/router/appmesh.go | 13 +++++++++++++ pkg/router/appmesh_test.go | 1 + pkg/router/appmesh_v1beta2.go | 13 +++++++++++++ pkg/router/appmesh_v1beta2_test.go | 1 + pkg/router/router_test.go | 4 ++++ 6 files changed, 36 insertions(+) diff --git a/pkg/apis/appmesh/register.go b/pkg/apis/appmesh/register.go index 2e54ff10f..ffcf13884 100644 --- a/pkg/apis/appmesh/register.go +++ b/pkg/apis/appmesh/register.go @@ -3,3 +3,7 @@ package appmesh const ( GroupName = "appmesh.k8s.aws" ) + +const AccessLogAnnotation = "appmesh.flagger.app/accesslog" + +const EnabledValue = "enabled" diff --git a/pkg/router/appmesh.go b/pkg/router/appmesh.go index 296aaf843..78f986d3d 100644 --- a/pkg/router/appmesh.go +++ b/pkg/router/appmesh.go @@ -31,6 +31,7 @@ import ( "k8s.io/apimachinery/pkg/runtime/schema" "k8s.io/client-go/kubernetes" + appmesh "github.com/fluxcd/flagger/pkg/apis/appmesh" appmeshv1 "github.com/fluxcd/flagger/pkg/apis/appmesh/v1beta1" flaggerv1 "github.com/fluxcd/flagger/pkg/apis/flagger/v1beta1" clientset "github.com/fluxcd/flagger/pkg/client/clientset/versioned" @@ -114,6 +115,18 @@ func (ar *AppMeshRouter) reconcileVirtualNode(canary *flaggerv1.Canary, name str }, } + //get annotation to enable the access log + val, _ := canary.ObjectMeta.GetAnnotations()[appmesh.AccessLogAnnotation] + if val == appmesh.EnabledValue { + vnSpec.Logging = &appmeshv1.Logging{ + AccessLog: &appmeshv1.AccessLog{ + File: &appmeshv1.FileAccessLog{ + Path: "/dev/stdout", + }, + }, + } + } + backends := make([]appmeshv1.Backend, len(canary.Spec.Service.Backends)) for i, b := range canary.Spec.Service.Backends { backends[i] = appmeshv1.Backend{ diff --git a/pkg/router/appmesh_test.go b/pkg/router/appmesh_test.go index b983935ff..ec12df861 100644 --- a/pkg/router/appmesh_test.go +++ b/pkg/router/appmesh_test.go @@ -63,6 +63,7 @@ func TestAppmeshRouter_Reconcile(t *testing.T) { vnName := mocks.appmeshCanary.Spec.TargetRef.Name vn, err := router.appmeshClient.AppmeshV1beta1().VirtualNodes("default").Get(context.TODO(), vnName, metav1.GetOptions{}) require.NoError(t, err) + require.NotNil(t, vn.Spec.Logging) primaryDNS := fmt.Sprintf("%s-primary.%s", mocks.appmeshCanary.Spec.TargetRef.Name, mocks.appmeshCanary.Namespace) assert.Equal(t, primaryDNS, vn.Spec.ServiceDiscovery.Dns.HostName) diff --git a/pkg/router/appmesh_v1beta2.go b/pkg/router/appmesh_v1beta2.go index 61e4fb971..73346c733 100644 --- a/pkg/router/appmesh_v1beta2.go +++ b/pkg/router/appmesh_v1beta2.go @@ -31,6 +31,7 @@ import ( "k8s.io/apimachinery/pkg/runtime/schema" "k8s.io/client-go/kubernetes" + appmesh "github.com/fluxcd/flagger/pkg/apis/appmesh" appmeshv1 "github.com/fluxcd/flagger/pkg/apis/appmesh/v1beta2" flaggerv1 "github.com/fluxcd/flagger/pkg/apis/flagger/v1beta1" clientset "github.com/fluxcd/flagger/pkg/client/clientset/versioned" @@ -118,6 +119,18 @@ func (ar *AppMeshv1beta2Router) reconcileVirtualNode(canary *flaggerv1.Canary, n }, } + //get annotation to enable the access log + val, _ := canary.ObjectMeta.GetAnnotations()[appmesh.AccessLogAnnotation] + if val == appmesh.EnabledValue { + vnSpec.Logging = &appmeshv1.Logging{ + AccessLog: &appmeshv1.AccessLog{ + File: &appmeshv1.FileAccessLog{ + Path: "/dev/stdout", + }, + }, + } + } + backends := make([]appmeshv1.Backend, 0) for i := range canary.Spec.Service.Backends { if strings.HasPrefix(canary.Spec.Service.Backends[i], "arn:aws") { diff --git a/pkg/router/appmesh_v1beta2_test.go b/pkg/router/appmesh_v1beta2_test.go index b41162aeb..6815b0229 100644 --- a/pkg/router/appmesh_v1beta2_test.go +++ b/pkg/router/appmesh_v1beta2_test.go @@ -67,6 +67,7 @@ func TestAppmeshv1beta2Router_Reconcile(t *testing.T) { // check primary virtual node vnPrimary, err := router.appmeshClient.AppmeshV1beta2().VirtualNodes("default").Get(context.TODO(), primaryName, metav1.GetOptions{}) require.NoError(t, err) + require.NotNil(t, vnPrimary.Spec.Logging) // check FQDN primaryDNS := fmt.Sprintf("%s.%s.svc.cluster.local.", primaryName, mocks.appmeshCanary.Namespace) diff --git a/pkg/router/router_test.go b/pkg/router/router_test.go index 0561b40b2..917282bfd 100644 --- a/pkg/router/router_test.go +++ b/pkg/router/router_test.go @@ -26,6 +26,7 @@ import ( "k8s.io/client-go/kubernetes" "k8s.io/client-go/kubernetes/fake" + appmesh "github.com/fluxcd/flagger/pkg/apis/appmesh" flaggerv1 "github.com/fluxcd/flagger/pkg/apis/flagger/v1beta1" "github.com/fluxcd/flagger/pkg/apis/gatewayapi/v1alpha2" istiov1alpha1 "github.com/fluxcd/flagger/pkg/apis/istio/common/v1alpha1" @@ -164,6 +165,9 @@ func newTestCanaryAppMesh() *flaggerv1.Canary { ObjectMeta: metav1.ObjectMeta{ Namespace: "default", Name: "appmesh", + Annotations: map[string]string{ + appmesh.AccessLogAnnotation: appmesh.EnabledValue, + }, }, Spec: flaggerv1.CanarySpec{ TargetRef: flaggerv1.CrossNamespaceObjectReference{