Skip to main content

Labels and Selectors

This page describes how Traefik Hub uses Labels and Selectors for resource management and selections.


Introduction

In Kubernetes, Labels and Selectors are essential concepts used for organizing and managing resources in Kubernetes.

They're used to identifying and group resources, allowing for efficient resource management and flexible resource selection.


Traefik Hub

Traefik Hub utilizes Labels and Selectors for API management.

They're used for granting API access, rate limiting, etc.


Labels

In Traefik Hub they're used to categorizing resources based on attributes such as environment, application, or component type.

For example, you could label an API with "area:customers" or "module:crm" to differentiate between different parts of your application.

Labels allow you to logically organize and query resources based on specific characteristics, enabling you to apply operations or configurations to many resources at once.

Example

YAML
---
apiVersion: hub.traefik.io/v1alpha1
kind: API
metadata:
name: customer-api
namespace: apps
labels:
area: customers
module: crm
spec:
pathPrefix: "/customers"
service:
name: customer-app
port:
number: 3000
openApiSpec:
path: /openapi.yaml

Selectors

Selectors helps to filter the items/objects which have Labels attached to them.

They allow selecting resources based on their Labels. When you need to target a specific set of resources, you can use a selector to filter them based on the Labels attached to those resources.

Traefik Hub uses equality-based and set-based Selectors.

Example

YAML
---
apiVersion: hub.traefik.io/v1alpha1
kind: APIAccess
metadata:
name: my-api-access
spec:
groups:
- support
apis:
- name: my-api-1
namespace: my-ns
- name: my-api-2
namespace: my-ns
apiSelector:
matchLabels:
area: product
matchExpressions:
- operator: in
key: audience
value: ["dev", "admin"]

The selected APIs will be my-api-1, my-api-2 because of explicitly listing their names.

Further, all APIs, which match the condition that their area label is set to product and their audience is either dev or admin.

The hard-coded list of APIs can be seen as a logical OR, while matchLabels and matchExpressions can be seen as a logical AND.


Summary

Labels provide metadata to resources, while Selectors use that metadata to filter and target specific resources for various operations.

Labels are flexible and can be assigned to resources as per your requirements, while Selectors provide a way to identify resources based on the labels' key-value pairs.