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

Commit e29cca1

Browse files
Adding extra annotations to service (#150)
1 parent b6f3902 commit e29cca1

8 files changed

+43
-12
lines changed

Diff for: api/v1alpha1/nginxingresscontroller_types.go

+3
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,9 @@ type Service struct {
297297
// Specifies extra labels of the service.
298298
// +kubebuilder:validation:Optional
299299
ExtraLabels map[string]string `json:"extraLabels,omitempty"`
300+
// Specifies extra annotations of the service.
301+
// +kubebuilder:validation:Optional
302+
ExtraAnnotations map[string]string `json:"extraAnnotations,omitempty"`
300303
}
301304

302305
func init() {

Diff for: api/v1alpha1/zz_generated.deepcopy.go

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

Diff for: bundle/manifests/k8s.nginx.org_nginxingresscontrollers.yaml

+5
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,11 @@ spec:
246246
type: string
247247
description: Specifies extra labels of the service.
248248
type: object
249+
extraAnnotations:
250+
additionalProperties:
251+
type: string
252+
description: Specifies extra annotations of the service.
253+
type: object
249254
type: object
250255
serviceType:
251256
description: 'The type of the Service for the Ingress Controller.

Diff for: config/crd/bases/k8s.nginx.org_nginxingresscontrollers.yaml

+5
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,11 @@ spec:
270270
description: The service of the Ingress controller.
271271
nullable: true
272272
properties:
273+
extraAnnotations:
274+
additionalProperties:
275+
type: string
276+
description: Specifies extra annotations of the service.
277+
type: object
273278
extraLabels:
274279
additionalProperties:
275280
type: string

Diff for: controllers/nginxingresscontroller_controller.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -223,10 +223,12 @@ func (r *NginxIngressControllerReconciler) Reconcile(ctx context.Context, req ct
223223
return ctrl.Result{}, err
224224
}
225225
var extraLabels map[string]string
226+
var extraAnnotations map[string]string
226227
if instance.Spec.Service != nil {
227228
extraLabels = instance.Spec.Service.ExtraLabels
229+
extraAnnotations = instance.Spec.Service.ExtraAnnotations
228230
}
229-
res, err := controllerutil.CreateOrUpdate(ctx, r.Client, svc, serviceMutateFn(svc, instance.Spec.ServiceType, extraLabels))
231+
res, err := controllerutil.CreateOrUpdate(ctx, r.Client, svc, serviceMutateFn(svc, instance.Spec.ServiceType, extraLabels, extraAnnotations))
230232
log.V(1).Info(fmt.Sprintf("Service %s %s", svc.Name, res))
231233
if err != nil {
232234
return ctrl.Result{}, err

Diff for: controllers/service.go

+8-4
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,18 @@ import (
1212

1313
func serviceForNginxIngressController(instance *k8sv1alpha1.NginxIngressController, scheme *runtime.Scheme) (*corev1.Service, error) {
1414
extraLabels := map[string]string{}
15+
extraAnnotations := map[string]string{}
1516
if instance.Spec.Service != nil {
1617
extraLabels = instance.Spec.Service.ExtraLabels
18+
extraAnnotations = instance.Spec.Service.ExtraAnnotations
1719
}
1820

1921
svc := &corev1.Service{
2022
ObjectMeta: v1.ObjectMeta{
21-
Name: instance.Name,
22-
Namespace: instance.Namespace,
23-
Labels: extraLabels,
23+
Name: instance.Name,
24+
Namespace: instance.Namespace,
25+
Labels: extraLabels,
26+
Annotations: extraAnnotations,
2427
},
2528
Spec: corev1.ServiceSpec{
2629
Ports: []corev1.ServicePort{
@@ -55,10 +58,11 @@ func serviceForNginxIngressController(instance *k8sv1alpha1.NginxIngressControll
5558
return svc, nil
5659
}
5760

58-
func serviceMutateFn(svc *corev1.Service, serviceType string, labels map[string]string) controllerutil.MutateFn {
61+
func serviceMutateFn(svc *corev1.Service, serviceType string, labels map[string]string, annotations map[string]string) controllerutil.MutateFn {
5962
return func() error {
6063
svc.Spec.Type = corev1.ServiceType(serviceType)
6164
svc.Labels = labels
65+
svc.Annotations = annotations
6266
return nil
6367
}
6468
}

Diff for: controllers/service_test.go

+11-7
Original file line numberDiff line numberDiff line change
@@ -21,25 +21,29 @@ func TestServiceForNginxIngressController(t *testing.T) {
2121
name := "my-service"
2222
namespace := "my-nginx-ingress"
2323
extraLabels := map[string]string{"app": "my-nginx-ingress"}
24+
extraAnnotations := map[string]string{"app": "my-nginx-ingress"}
2425

2526
instance := &k8sv1alpha1.NginxIngressController{
2627
ObjectMeta: v1.ObjectMeta{
27-
Name: name,
28-
Namespace: namespace,
29-
Labels: extraLabels,
28+
Name: name,
29+
Namespace: namespace,
30+
Labels: extraLabels,
31+
Annotations: extraAnnotations,
3032
},
3133
Spec: k8sv1alpha1.NginxIngressControllerSpec{
3234
ServiceType: string(corev1.ServiceTypeLoadBalancer),
3335
Service: &k8sv1alpha1.Service{
34-
ExtraLabels: extraLabels,
36+
ExtraLabels: extraLabels,
37+
ExtraAnnotations: extraAnnotations,
3538
},
3639
},
3740
}
3841
expected := &corev1.Service{
3942
ObjectMeta: v1.ObjectMeta{
40-
Name: name,
41-
Namespace: namespace,
42-
Labels: extraLabels,
43+
Name: name,
44+
Namespace: namespace,
45+
Labels: extraLabels,
46+
Annotations: extraAnnotations,
4347
OwnerReferences: []v1.OwnerReference{
4448
{
4549
APIVersion: "k8s.nginx.org/v1alpha1",

Diff for: docs/nginx-ingress-controller.md

+1
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ spec:
132132
| Field | Type | Description | Required |
133133
| --- | --- | --- | --- |
134134
| `extraLabels` | `map[string]string` | Specifies extra labels of the service. | No |
135+
| `extraAnnotations` | `map[string]string` | Specifies extra annotations of the service. | No |
135136

136137
## NginxIngressController.ReportIngressStatus
137138

0 commit comments

Comments
 (0)