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

Adding extra annotations to service #150

Merged
merged 8 commits into from
Jan 12, 2022
Merged
Show file tree
Hide file tree
Changes from 6 commits
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
3 changes: 3 additions & 0 deletions api/v1alpha1/nginxingresscontroller_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,9 @@ type Service struct {
// Specifies extra labels of the service.
// +kubebuilder:validation:Optional
ExtraLabels map[string]string `json:"extraLabels,omitempty"`
// Specifies extra annotations of the service.
// +kubebuilder:validation:Optional
ExtraAnnotations map[string]string `json:"extraAnnotations,omitempty"`
}

func init() {
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.

5 changes: 5 additions & 0 deletions bundle/manifests/k8s.nginx.org_nginxingresscontrollers.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,11 @@ spec:
type: string
description: Specifies extra labels of the service.
type: object
extraAnnotations:
additionalProperties:
type: string
description: Specifies extra annotations of the service.
type: object
type: object
serviceType:
description: 'The type of the Service for the Ingress Controller.
Expand Down
5 changes: 5 additions & 0 deletions config/crd/bases/k8s.nginx.org_nginxingresscontrollers.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,11 @@ spec:
description: The service of the Ingress controller.
nullable: true
properties:
extraAnnotations:
additionalProperties:
type: string
description: Specifies extra annotations of the service.
type: object
extraLabels:
additionalProperties:
type: string
Expand Down
4 changes: 3 additions & 1 deletion controllers/nginxingresscontroller_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,10 +223,12 @@ func (r *NginxIngressControllerReconciler) Reconcile(ctx context.Context, req ct
return ctrl.Result{}, err
}
var extraLabels map[string]string
var extraAnnotations map[string]string
if instance.Spec.Service != nil {
extraLabels = instance.Spec.Service.ExtraLabels
extraAnnotations = instance.Spec.Service.ExtraAnnotations
}
res, err := controllerutil.CreateOrUpdate(ctx, r.Client, svc, serviceMutateFn(svc, instance.Spec.ServiceType, extraLabels))
res, err := controllerutil.CreateOrUpdate(ctx, r.Client, svc, serviceMutateFn(svc, instance.Spec.ServiceType, extraLabels, extraAnnotations))
log.V(1).Info(fmt.Sprintf("Service %s %s", svc.Name, res))
if err != nil {
return ctrl.Result{}, err
Expand Down
12 changes: 8 additions & 4 deletions controllers/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,18 @@ import (

func serviceForNginxIngressController(instance *k8sv1alpha1.NginxIngressController, scheme *runtime.Scheme) (*corev1.Service, error) {
extraLabels := map[string]string{}
extraAnnotations := map[string]string{}
if instance.Spec.Service != nil {
extraLabels = instance.Spec.Service.ExtraLabels
extraAnnotations = instance.Spec.Service.ExtraAnnotations
}

svc := &corev1.Service{
ObjectMeta: v1.ObjectMeta{
Name: instance.Name,
Namespace: instance.Namespace,
Labels: extraLabels,
Name: instance.Name,
Namespace: instance.Namespace,
Labels: extraLabels,
Annotations: extraAnnotations,
},
Spec: corev1.ServiceSpec{
Ports: []corev1.ServicePort{
Expand Down Expand Up @@ -55,10 +58,11 @@ func serviceForNginxIngressController(instance *k8sv1alpha1.NginxIngressControll
return svc, nil
}

func serviceMutateFn(svc *corev1.Service, serviceType string, labels map[string]string) controllerutil.MutateFn {
func serviceMutateFn(svc *corev1.Service, serviceType string, labels map[string]string, annotations map[string]string) controllerutil.MutateFn {
return func() error {
svc.Spec.Type = corev1.ServiceType(serviceType)
svc.Labels = labels
svc.Annotations = annotations
return nil
}
}
18 changes: 11 additions & 7 deletions controllers/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,25 +21,29 @@ func TestServiceForNginxIngressController(t *testing.T) {
name := "my-service"
namespace := "my-nginx-ingress"
extraLabels := map[string]string{"app": "my-nginx-ingress"}
extraAnnotations := map[string]string{"app": "my-nginx-ingress"}

instance := &k8sv1alpha1.NginxIngressController{
ObjectMeta: v1.ObjectMeta{
Name: name,
Namespace: namespace,
Labels: extraLabels,
Name: name,
Namespace: namespace,
Labels: extraLabels,
Annotations: extraAnnotations,
},
Spec: k8sv1alpha1.NginxIngressControllerSpec{
ServiceType: string(corev1.ServiceTypeLoadBalancer),
Service: &k8sv1alpha1.Service{
ExtraLabels: extraLabels,
ExtraLabels: extraLabels,
ExtraAnnotations: extraAnnotations,
},
},
}
expected := &corev1.Service{
ObjectMeta: v1.ObjectMeta{
Name: name,
Namespace: namespace,
Labels: extraLabels,
Name: name,
Namespace: namespace,
Labels: extraLabels,
Annotations: extraAnnotations,
OwnerReferences: []v1.OwnerReference{
{
APIVersion: "k8s.nginx.org/v1alpha1",
Expand Down
1 change: 1 addition & 0 deletions docs/nginx-ingress-controller.md
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ spec:
| Field | Type | Description | Required |
| --- | --- | --- | --- |
| `extraLabels` | `map[string]string` | Specifies extra labels of the service. | No |
| `extraAnnotations` | `map[string]string` | Specifies extra annotations of the service. | No |

## NginxIngressController.ReportIngressStatus

Expand Down