Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add labels to envoyService config #4427

Merged
merged 2 commits into from
Oct 13, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions api/v1alpha1/shared_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,12 @@ type KubernetesServiceSpec struct {
// +optional
Annotations map[string]string `json:"annotations,omitempty"`

// Labels that should be appended to the service.
// By default, no labels are appended.
//
// +optional
Labels map[string]string `json:"labels,omitempty"`

// Type determines how the Service is exposed. Defaults to LoadBalancer.
// Valid options are ClusterIP, LoadBalancer and NodePort.
// "LoadBalancer" means a service will be exposed via an external load balancer (if the cloud provider supports it).
Expand Down
7 changes: 7 additions & 0 deletions api/v1alpha1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 16 additions & 6 deletions internal/infrastructure/kubernetes/proxy/resource_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,10 @@ func (r *ResourceRender) Service() (*corev1.Service, error) {
}
}

// Set the labels based on the owning gatewayclass name.
labels := envoyLabels(r.infra.GetProxyMetadata().Labels)
if OwningGatewayLabelsAbsent(labels) {
return nil, fmt.Errorf("missing owning gateway labels")
// Set the infraLabels based on the owning gatewayclass name.
infraLabels := envoyLabels(r.infra.GetProxyMetadata().Labels)
if OwningGatewayLabelsAbsent(infraLabels) {
return nil, fmt.Errorf("missing owning gateway infraLabels")
}

// Get annotations
Expand All @@ -120,10 +120,20 @@ func (r *ResourceRender) Service() (*corev1.Service, error) {
annotations = nil
}

// Get service-specific labels
svcLabels := map[string]string{}
maps.Copy(svcLabels, infraLabels)
if envoyServiceConfig.Labels != nil {
maps.Copy(svcLabels, envoyServiceConfig.Labels)
}
if len(svcLabels) == 0 {
svcLabels = nil
}

// Set the spec of gateway service
serviceSpec := resource.ExpectedServiceSpec(envoyServiceConfig)
serviceSpec.Ports = ports
serviceSpec.Selector = resource.GetSelector(labels).MatchLabels
serviceSpec.Selector = resource.GetSelector(infraLabels).MatchLabels

if (*envoyServiceConfig.Type) == egv1a1.ServiceTypeClusterIP {
if len(r.infra.Addresses) > 0 {
Expand All @@ -144,7 +154,7 @@ func (r *ResourceRender) Service() (*corev1.Service, error) {
},
ObjectMeta: metav1.ObjectMeta{
Namespace: r.Namespace,
Labels: labels,
Labels: svcLabels,
Annotations: annotations,
},
Spec: serviceSpec,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1051,6 +1051,9 @@ func TestService(t *testing.T) {
caseName: "custom",
infra: newTestInfra(),
service: &egv1a1.KubernetesServiceSpec{
Labels: map[string]string{
"key1": "value1",
},
Annotations: map[string]string{
"key1": "value1",
},
Expand Down Expand Up @@ -1079,6 +1082,31 @@ func TestService(t *testing.T) {
},
},
},
{
caseName: "with-svc-labels",
infra: newTestInfra(),
service: &egv1a1.KubernetesServiceSpec{
Labels: map[string]string{
"label1": "value1",
"label2": "value2",
},
},
},
{
caseName: "override-labels",
infra: newTestInfraWithAnnotationsAndLabels(map[string]string{
"anno1": "value1",
"anno2": "value2",
}, map[string]string{
"label1": "value1",
"label2": "value2",
}),
service: &egv1a1.KubernetesServiceSpec{
Labels: map[string]string{
"label1": "value1-override",
},
},
},
{
caseName: "clusterIP-custom-addresses",
infra: newTestInfraWithAddresses([]string{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ metadata:
annotations:
key1: value1
labels:
key1: value1
app.kubernetes.io/name: envoy
app.kubernetes.io/component: proxy
app.kubernetes.io/managed-by: envoy-gateway
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
apiVersion: v1
kind: Service
metadata:
annotations:
anno1: value1
anno2: value2
labels:
app.kubernetes.io/name: envoy
app.kubernetes.io/component: proxy
app.kubernetes.io/managed-by: envoy-gateway
gateway.envoyproxy.io/owning-gateway-name: default
gateway.envoyproxy.io/owning-gateway-namespace: default
label1: value1-override
label2: value2
name: envoy-default-37a8eec1
namespace: envoy-gateway-system
spec:
externalTrafficPolicy: Local
ports:
- name: EnvoyHTTPPort
port: 0
protocol: TCP
targetPort: 8080
- name: EnvoyHTTPSPort
port: 0
protocol: TCP
targetPort: 8443
selector:
app.kubernetes.io/name: envoy
app.kubernetes.io/component: proxy
app.kubernetes.io/managed-by: envoy-gateway
gateway.envoyproxy.io/owning-gateway-name: default
gateway.envoyproxy.io/owning-gateway-namespace: default
label1: value1
label2: value2
sessionAffinity: None
type: LoadBalancer
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
apiVersion: v1
kind: Service
metadata:
labels:
label1: value1
label2: value2
app.kubernetes.io/name: envoy
app.kubernetes.io/component: proxy
app.kubernetes.io/managed-by: envoy-gateway
gateway.envoyproxy.io/owning-gateway-name: default
gateway.envoyproxy.io/owning-gateway-namespace: default
name: envoy-default-37a8eec1
namespace: envoy-gateway-system
spec:
externalTrafficPolicy: Local
ports:
- name: EnvoyHTTPPort
port: 0
protocol: TCP
targetPort: 8080
- name: EnvoyHTTPSPort
port: 0
protocol: TCP
targetPort: 8443
selector:
app.kubernetes.io/name: envoy
app.kubernetes.io/component: proxy
app.kubernetes.io/managed-by: envoy-gateway
gateway.envoyproxy.io/owning-gateway-name: default
gateway.envoyproxy.io/owning-gateway-namespace: default
sessionAffinity: None
type: LoadBalancer
1 change: 1 addition & 0 deletions site/content/en/latest/api/extension_types.md
Original file line number Diff line number Diff line change
Expand Up @@ -2564,6 +2564,7 @@ _Appears in:_
| Field | Type | Required | Description |
| --- | --- | --- | --- |
| `annotations` | _object (keys:string, values:string)_ | false | Annotations that should be appended to the service.<br />By default, no annotations are appended. |
| `labels` | _object (keys:string, values:string)_ | false | Labels that should be appended to the service.<br />By default, no labels are appended. |
| `type` | _[ServiceType](#servicetype)_ | false | Type determines how the Service is exposed. Defaults to LoadBalancer.<br />Valid options are ClusterIP, LoadBalancer and NodePort.<br />"LoadBalancer" means a service will be exposed via an external load balancer (if the cloud provider supports it).<br />"ClusterIP" means a service will only be accessible inside the cluster, via the cluster IP.<br />"NodePort" means a service will be exposed on a static Port on all Nodes of the cluster. |
| `loadBalancerClass` | _string_ | false | LoadBalancerClass, when specified, allows for choosing the LoadBalancer provider<br />implementation if more than one are available or is otherwise expected to be specified |
| `allocateLoadBalancerNodePorts` | _boolean_ | false | AllocateLoadBalancerNodePorts defines if NodePorts will be automatically allocated for<br />services with type LoadBalancer. Default is "true". It may be set to "false" if the cluster<br />load-balancer does not rely on NodePorts. If the caller requests specific NodePorts (by specifying a<br />value), those requests will be respected, regardless of this field. This field may only be set for<br />services with type LoadBalancer and will be cleared if the type is changed to any other type. |
Expand Down
1 change: 1 addition & 0 deletions site/content/zh/latest/api/extension_types.md
Original file line number Diff line number Diff line change
Expand Up @@ -2564,6 +2564,7 @@ _Appears in:_
| Field | Type | Required | Description |
| --- | --- | --- | --- |
| `annotations` | _object (keys:string, values:string)_ | false | Annotations that should be appended to the service.<br />By default, no annotations are appended. |
| `labels` | _object (keys:string, values:string)_ | false | Labels that should be appended to the service.<br />By default, no labels are appended. |
| `type` | _[ServiceType](#servicetype)_ | false | Type determines how the Service is exposed. Defaults to LoadBalancer.<br />Valid options are ClusterIP, LoadBalancer and NodePort.<br />"LoadBalancer" means a service will be exposed via an external load balancer (if the cloud provider supports it).<br />"ClusterIP" means a service will only be accessible inside the cluster, via the cluster IP.<br />"NodePort" means a service will be exposed on a static Port on all Nodes of the cluster. |
| `loadBalancerClass` | _string_ | false | LoadBalancerClass, when specified, allows for choosing the LoadBalancer provider<br />implementation if more than one are available or is otherwise expected to be specified |
| `allocateLoadBalancerNodePorts` | _boolean_ | false | AllocateLoadBalancerNodePorts defines if NodePorts will be automatically allocated for<br />services with type LoadBalancer. Default is "true". It may be set to "false" if the cluster<br />load-balancer does not rely on NodePorts. If the caller requests specific NodePorts (by specifying a<br />value), those requests will be respected, regardless of this field. This field may only be set for<br />services with type LoadBalancer and will be cleared if the type is changed to any other type. |
Expand Down
Loading