Skip to content
This repository was archived by the owner on May 24, 2023. It is now read-only.

Commit 6b8e5b3

Browse files
committed
Add extraLabels support
1 parent 7a76f38 commit 6b8e5b3

File tree

6 files changed

+30
-7
lines changed

6 files changed

+30
-7
lines changed

deploy/crds/k8s.nginx.org_nginxingresscontrollers_crd.yaml

+5
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,11 @@ spec:
8383
description: Enable TLS Passthrough on port 443. Requires enableCRDs
8484
set to true.
8585
type: boolean
86+
extraLabels:
87+
additionalProperties:
88+
type: string
89+
description: Specifies extra labels of the service.
90+
type: object
8691
globalConfiguration:
8792
description: The GlobalConfiguration resource for global configuration
8893
of the Ingress Controller. Format is namespace/name. Requires enableCRDs

pkg/apis/k8s/v1alpha1/nginxingresscontroller_types.go

+5-1
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,17 @@ type NginxIngressControllerSpec struct {
4040
// +kubebuilder:validation:Optional
4141
// +operator-sdk:gen-csv:customresourcedefinitions.specDescriptors=true
4242
EnableSnippets bool `json:"enableSnippets"`
43-
// +kubebuilder:validation:Optional
4443
// A class of the Ingress controller. The Ingress controller only processes Ingress resources that belong to its
4544
// class (in other words, have the annotation “kubernetes.io/ingress.class”).
4645
// Additionally, the Ingress controller processes Ingress resources that do not have that annotation,
4746
// which can be disabled by setting UseIngressClassOnly to true. Default is `nginx`.
47+
// +kubebuilder:validation:Optional
4848
// +operator-sdk:gen-csv:customresourcedefinitions.specDescriptors=true
4949
IngressClass string `json:"ingressClass"`
50+
// Specifies extra labels of the service.
51+
// +kubebuilder:validation:Optional
52+
// +operator-sdk:gen-csv:customresourcedefinitions.specDescriptors=true
53+
ExtraLabels map[string]string `json:"extraLabels,omitempty"`
5054
// Ignore Ingress resources without the “kubernetes.io/ingress.class” annotation.
5155
// +kubebuilder:validation:Optional
5256
// +operator-sdk:gen-csv:customresourcedefinitions.specDescriptors=true

pkg/apis/k8s/v1alpha1/zz_generated.deepcopy.go

+7
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/controller/nginxingresscontroller/nginxingresscontroller_controller.go

+5-3
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,11 @@ import (
3232

3333
var log = logf.Log.WithName("controller_nginxingresscontroller")
3434

35-
const clusterRoleName = "nginx-ingress-role"
36-
const sccName = "nginx-ingress-scc"
37-
const finalizer = "finalizer.nginxingresscontroller.k8s.nginx.org"
35+
const (
36+
clusterRoleName = "nginx-ingress-role"
37+
sccName = "nginx-ingress-scc"
38+
finalizer = "finalizer.nginxingresscontroller.k8s.nginx.org"
39+
)
3840

3941
// Add creates a new NginxIngressController Controller and adds it to the Manager. The Manager will set fields on the Controller
4042
// and Start it when the Manager is Started.

pkg/controller/nginxingresscontroller/service.go

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ func serviceForNginxIngressController(instance *k8sv1alpha1.NginxIngressControll
1212
ObjectMeta: v1.ObjectMeta{
1313
Name: instance.Name,
1414
Namespace: instance.Namespace,
15+
Labels: instance.Spec.ExtraLabels,
1516
},
1617
Spec: corev1.ServiceSpec{
1718
Ports: []corev1.ServicePort{

pkg/controller/nginxingresscontroller/service_test.go

+7-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
package nginxingresscontroller
22

33
import (
4-
"reflect"
54
"testing"
65

6+
"github.com/google/go-cmp/cmp"
77
k8sv1alpha1 "github.com/nginxinc/nginx-ingress-operator/pkg/apis/k8s/v1alpha1"
88
corev1 "k8s.io/api/core/v1"
99
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -14,20 +14,24 @@ func TestServiceForNginxIngressController(t *testing.T) {
1414
name := "my-service"
1515
namespace := "my-nginx-ingress"
1616
serviceType := "LoadBalancer"
17+
extraLabels := map[string]string{"app": "my-nginx-ingress"}
1718

1819
instance := &k8sv1alpha1.NginxIngressController{
1920
ObjectMeta: v1.ObjectMeta{
2021
Name: name,
2122
Namespace: namespace,
23+
Labels: extraLabels,
2224
},
2325
Spec: k8sv1alpha1.NginxIngressControllerSpec{
2426
ServiceType: serviceType,
27+
ExtraLabels: extraLabels,
2528
},
2629
}
2730
expected := &corev1.Service{
2831
ObjectMeta: v1.ObjectMeta{
2932
Name: name,
3033
Namespace: namespace,
34+
Labels: extraLabels,
3135
},
3236
Spec: corev1.ServiceSpec{
3337
Ports: []corev1.ServicePort{
@@ -56,7 +60,7 @@ func TestServiceForNginxIngressController(t *testing.T) {
5660
}
5761

5862
result := serviceForNginxIngressController(instance)
59-
if !reflect.DeepEqual(result, expected) {
60-
t.Errorf("serviceForNginxIngressController(%v) returned %+v but expected %+v", instance, result, expected)
63+
if diff := cmp.Diff(expected, result); diff != "" {
64+
t.Errorf("serviceForNginxIngressController() mismatch (-want +got):\n%s", diff)
6165
}
6266
}

0 commit comments

Comments
 (0)