Skip to content

Commit

Permalink
Propagate App tags through a annotation with "tsuru.io/custom-tag-" p…
Browse files Browse the repository at this point in the history
…refix
  • Loading branch information
wpjunior committed Oct 18, 2024
1 parent 714a1c2 commit 9a684a4
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 3 deletions.
30 changes: 27 additions & 3 deletions kubernetes/ingress.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/validation"
typedV1 "k8s.io/client-go/kubernetes/typed/core/v1"
networkingTypedV1 "k8s.io/client-go/kubernetes/typed/networking/v1"
)
Expand Down Expand Up @@ -192,7 +193,7 @@ func (k *IngressService) Ensure(ctx context.Context, id router.InstanceID, o rou
},
Spec: buildIngressSpec(vhosts, o.Opts.Route, backendServices, k),
}
k.fillIngressMeta(ingress, o.Opts, id, o.Team)
k.fillIngressMeta(ingress, o.Opts, id, o.Team, o.Tags)
if o.Opts.Acme {
k.fillIngressTLS(ingress, id)
ingress.ObjectMeta.Annotations[AnnotationsACMEKey] = "true"
Expand All @@ -218,6 +219,7 @@ func (k *IngressService) Ensure(ctx context.Context, id router.InstanceID, o rou
certIssuer: o.CertIssuers[cname],
service: backendServices["default"],
routerOpts: o.Opts,
tags: o.Tags,
})
if err != nil {
err = errors.Wrapf(err, "could not ensure CName: %q", cname)
Expand Down Expand Up @@ -328,6 +330,7 @@ type ensureCNameBackendOpts struct {
certIssuer string
service *v1.Service
routerOpts router.Opts
tags []string
}

func (k *IngressService) ensureCNameBackend(ctx context.Context, opts ensureCNameBackendOpts) error {
Expand Down Expand Up @@ -377,7 +380,7 @@ func (k *IngressService) ensureCNameBackend(ctx context.Context, opts ensureCNam
Spec: buildIngressSpec(map[string]string{"ensureCnameBackend": opts.cname}, opts.routerOpts.Route, map[string]*v1.Service{"ensureCnameBackend": opts.service}, k),
}

k.fillIngressMeta(ingress, opts.routerOpts, opts.id, opts.team)
k.fillIngressMeta(ingress, opts.routerOpts, opts.id, opts.team, opts.tags)
if opts.routerOpts.AcmeCName {
k.fillIngressTLS(ingress, opts.id)
ingress.ObjectMeta.Annotations[AnnotationsACMEKey] = "true"
Expand Down Expand Up @@ -775,7 +778,7 @@ func (s *IngressService) SupportedOptions(ctx context.Context) map[string]string
return opts
}

func (s *IngressService) fillIngressMeta(i *networkingV1.Ingress, routerOpts router.Opts, id router.InstanceID, team string) {
func (s *IngressService) fillIngressMeta(i *networkingV1.Ingress, routerOpts router.Opts, id router.InstanceID, team string, tags []string) {
if i.ObjectMeta.Labels == nil {
i.ObjectMeta.Labels = map[string]string{}
}
Expand Down Expand Up @@ -814,6 +817,27 @@ func (s *IngressService) fillIngressMeta(i *networkingV1.Ingress, routerOpts rou
i.ObjectMeta.Annotations[labelName] = optValue
}
}

for _, tag := range tags {
parts := strings.SplitN(tag, "=", 2)
var key, value string
if len(parts) != 2 {
continue
}

key = parts[0]
value = parts[1]

if key == "" {
continue
}
labelName := customTagPrefixLabel + key
if len(validation.IsQualifiedName(labelName)) > 0 {
// Ignoring tags that are not valid identifiers for labels or annotations
continue
}
i.ObjectMeta.Labels[labelName] = value
}
}

func (s *IngressService) validateCustomIssuer(ctx context.Context, resource CertManagerIssuerData, ns string) error {
Expand Down
49 changes: 49 additions & 0 deletions kubernetes/ingress_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -526,6 +526,55 @@ func TestIngressEnsureWithCNames(t *testing.T) {
assert.Equal(t, foundIngress.Annotations[AnnotationsCNames], "")
}

func TestIngressEnsureWithTags(t *testing.T) {
svc := createFakeService()

Check failure on line 530 in kubernetes/ingress_test.go

View workflow job for this annotation

GitHub Actions / Test

not enough arguments in call to createFakeService

Check failure on line 530 in kubernetes/ingress_test.go

View workflow job for this annotation

GitHub Actions / Lint

not enough arguments in call to createFakeService
svc.Labels = map[string]string{"controller": "my-controller", "XPTO": "true"}
svc.Annotations = map[string]string{"ann1": "val1", "ann2": "val2"}
err := svc.Ensure(ctx, idForApp("test"), router.EnsureBackendOpts{
Opts: router.Opts{
Route: "/admin",
AdditionalOpts: map[string]string{
"tsuru.io/some-annotation": "true",
"cert-manager.io/cluster-issuer": "letsencrypt-prod",
},
},
Tags: []string{"test.io", "product=myproduct"},
Team: "default",
Prefixes: []router.BackendPrefix{
{
Target: router.BackendTarget{
Service: "test-web",
Namespace: "default",
},
},
{
Prefix: "subscriber",
Target: router.BackendTarget{
Service: "test-subscriber",
Namespace: "default",
},
},
},
})
require.NoError(t, err)
foundIngress, err := svc.Client.NetworkingV1().Ingresses(svc.Namespace).Get(ctx, "kubernetes-router-test-ingress", metav1.GetOptions{})
require.NoError(t, err)

expectedIngress := defaultIngress("test", "default")

expectedIngress.Spec.Rules[0].HTTP.Paths[0].Path = "/admin"
expectedIngress.Labels["controller"] = "my-controller"
expectedIngress.Labels["XPTO"] = "true"
expectedIngress.Labels["tsuru.io/app-name"] = "test"
expectedIngress.Labels["tsuru.io/app-team"] = "default"
expectedIngress.Labels["tsuru.io/custom-tag-product"] = "myproduct"
expectedIngress.Annotations["ann1"] = "val1"
expectedIngress.Annotations["ann2"] = "val2"
expectedIngress.Annotations["tsuru.io/some-annotation"] = "true"

assert.Equal(t, expectedIngress, foundIngress)
}

func TestEnsureCertManagerIssuer(t *testing.T) {
svc := createFakeService(false)

Expand Down
2 changes: 2 additions & 0 deletions kubernetes/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ const (
processLabel = "tsuru.io/app-process"
appPoolLabel = "tsuru.io/app-pool"

customTagPrefixLabel = "tsuru.io/custom-tag-"

appCRDName = "apps.tsuru.io"
)

Expand Down
1 change: 1 addition & 0 deletions router/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ type EnsureBackendOpts struct {
Opts Opts `json:"opts"`
CNames []string `json:"cnames"`
Team string `json:"team"`
Tags []string `json:"tags,omitempty"`
CertIssuers map[string]string `json:"certIssuers"`
Prefixes []BackendPrefix `json:"prefixes"`
}
Expand Down

0 comments on commit 9a684a4

Please sign in to comment.