11package controller
22
33import (
4- "errors "
4+ "regexp "
55 "strings"
66
7+ "github.com/pdok/mapserver-operator/internal/controller/utils"
8+
79 pdoknlv3 "github.com/pdok/mapserver-operator/api/v3"
810 smoothoperatorutils "github.com/pdok/smooth-operator/pkg/util"
911 traefikiov1alpha1 "github.com/traefik/traefik/v3/pkg/provider/kubernetes/crd/traefikio/v1alpha1"
@@ -12,6 +14,12 @@ import (
1214 ctrl "sigs.k8s.io/controller-runtime"
1315)
1416
17+ var setUptimeOperatorAnnotations = true
18+
19+ func SetUptimeOperatorAnnotations (set bool ) {
20+ setUptimeOperatorAnnotations = set
21+ }
22+
1523func getBareIngressRoute [O pdoknlv3.WMSWFS ](obj O ) * traefikiov1alpha1.IngressRoute {
1624 return & traefikiov1alpha1.IngressRoute {
1725 ObjectMeta : metav1.ObjectMeta {
@@ -29,23 +37,24 @@ func mutateIngressRoute[R Reconciler, O pdoknlv3.WMSWFS](r R, obj O, ingressRout
2937 return err
3038 }
3139
32- var uptimeURL string
33- switch any (obj ).(type ) {
34- case * pdoknlv3.WFS :
35- uptimeURL = any (obj ).(* pdoknlv3.WFS ).Spec .Service .URL // TODO add healthcheck query
36- case * pdoknlv3.WMS :
37- uptimeURL = any (obj ).(* pdoknlv3.WMS ).Spec .Service .URL // TODO add healthcheck query
38- }
40+ annotations := smoothoperatorutils .CloneOrEmptyMap (obj .GetAnnotations ())
41+ if setUptimeOperatorAnnotations {
42+ tags := []string {"public-stats" , strings .ToLower (string (obj .Type ()))}
3943
40- uptimeName , err := makeUptimeName (obj )
41- if err != nil {
42- return err
44+ if obj .Inspire () != nil {
45+ tags = append (tags , "inspire" )
46+ }
47+
48+ queryString , err := obj .ReadinessQueryString ()
49+ if err != nil {
50+ return err
51+ }
52+
53+ annotations ["uptime.pdok.nl/id" ] = utils .Sha1Hash (obj .TypedName ())
54+ annotations ["uptime.pdok.nl/name" ] = getUptimeName (obj )
55+ annotations ["uptime.pdok.nl/url" ] = obj .URLPath () + "?" + queryString
56+ annotations ["uptime.pdok.nl/tags" ] = strings .Join (tags , "," )
4357 }
44- annotations := smoothoperatorutils .CloneOrEmptyMap (obj .GetAnnotations ())
45- annotations ["uptime.pdok.nl/id" ] = obj .ID ()
46- annotations ["uptime.pdok.nl/name" ] = uptimeName
47- annotations ["uptime.pdok.nl/url" ] = uptimeURL
48- annotations ["uptime.pdok.nl/tags" ] = strings .Join (makeUptimeTags (obj ), "," )
4958 ingressRoute .SetAnnotations (annotations )
5059
5160 mapserverService := traefikiov1alpha1.Service {
@@ -108,90 +117,33 @@ func mutateIngressRoute[R Reconciler, O pdoknlv3.WMSWFS](r R, obj O, ingressRout
108117 }}
109118 }
110119
111- // Add finalizers
112- ingressRoute .Finalizers = []string {"uptime.pdok.nl/finalizer" }
113-
114120 if err := smoothoperatorutils .EnsureSetGVK (reconcilerClient , ingressRoute , ingressRoute ); err != nil {
115121 return err
116122 }
117123 return ctrl .SetControllerReference (obj , ingressRoute , getReconcilerScheme (r ))
118124}
119125
120- func makeUptimeTags [O pdoknlv3.WMSWFS ](obj O ) []string {
121- tags := []string {"public-stats" , strings .ToLower (string (obj .Type ()))}
122-
123- switch any (obj ).(type ) {
124- case * pdoknlv3.WFS :
125- wfs , _ := any (obj ).(* pdoknlv3.WFS )
126- if wfs .Spec .Service .Inspire != nil {
127- tags = append (tags , "inspire" )
128- }
129- case * pdoknlv3.WMS :
130- wms , _ := any (obj ).(* pdoknlv3.WMS )
131- if wms .Spec .Service .Inspire != nil {
132- tags = append (tags , "inspire" )
133- }
134- }
135-
136- return tags
137- }
138-
139- func makeUptimeName [O pdoknlv3.WMSWFS ](obj O ) (string , error ) {
140- var parts []string
141-
142- inspire := false
143- switch any (obj ).(type ) {
144- case * pdoknlv3.WFS :
145- inspire = any (obj ).(* pdoknlv3.WFS ).Spec .Service .Inspire != nil
146- case * pdoknlv3.WMS :
147- inspire = any (obj ).(* pdoknlv3.WMS ).Spec .Service .Inspire != nil
148- }
149-
150- ownerID , ok := obj .GetLabels ()["dataset-owner" ]
151- if ! ok {
152- ownerID , ok = obj .GetLabels ()["pdok.nl/owner-id" ]
153- if ! ok {
154- return "" , errors .New ("dataset-owner and pdok.nl/owner-id labels are not found in object" )
155- }
156- }
157- parts = append (parts , strings .ToUpper (strings .ReplaceAll (ownerID , "-" , "" )))
158-
159- datasetID , ok := obj .GetLabels ()["dataset" ]
160- if ! ok {
161- // V3 label
162- datasetID , ok = obj .GetLabels ()["pdok.nl/dataset-id" ]
163- if ! ok {
164- return "" , errors .New ("dataset label not found in object" )
165- }
166- }
167- parts = append (parts , strings .ReplaceAll (datasetID , "-" , "" ))
126+ // getUptimeName transforms the CR name into a uptime.pdok.nl/name value
127+ // owner-dataset-v1-0 -> OWNER dataset v1_0 [INSPIRE] [WMS|WFS]
128+ func getUptimeName [O pdoknlv3.WMSWFS ](obj O ) string {
129+ // Extract the version from the CR name, owner-dataset-v1-0 -> owner-dataset + v1-0
130+ versionMatcher := regexp .MustCompile ("^(.*)(?:-(v?[1-9](?:-[0-9])?))?$" )
131+ match := versionMatcher .FindStringSubmatch (obj .GetName ())
168132
169- theme , ok := obj .GetLabels ()["theme" ]
170- if ! ok {
171- // V3 label
172- theme , ok = obj .GetLabels ()["pdok.nl/tag" ]
173- }
133+ nameParts := strings .Split (match [1 ], "-" )
134+ nameParts [0 ] = strings .ToUpper (nameParts [0 ])
174135
175- if ok {
176- parts = append (parts , strings .ReplaceAll (theme , "-" , "" ))
136+ // Add service version if found
137+ if len (match ) > 2 && len (match [2 ]) > 0 {
138+ nameParts = append (nameParts , strings .ReplaceAll (match [2 ], "-" , "_" ))
177139 }
178140
179- version , ok := obj .GetLabels ()["service-version" ]
180- if ! ok {
181- version , ok = obj .GetLabels ()["pdok.nl/service-version" ]
182- if ! ok {
183- return "" , errors .New ("service-version label not found in object" )
184- }
185- }
186- parts = append (parts , version )
187-
188- if inspire {
189- parts = append (parts , "INSPIRE" )
141+ // Add inspire
142+ if obj .Inspire () != nil {
143+ nameParts = append (nameParts , "INSPIRE" )
190144 }
191145
192- parts = append (parts , string (obj .Type ()))
193-
194- return strings .Join (parts , " " ), nil
146+ return strings .Join (append (nameParts , string (obj .Type ())), " " )
195147}
196148
197149func getMatchRule [O pdoknlv3.WMSWFS ](obj O ) string {
0 commit comments