@@ -295,7 +295,7 @@ func TestTailscaleIngressWithProxyClass(t *testing.T) {
295295 pc := & tsapi.ProxyClass {
296296 ObjectMeta : metav1.ObjectMeta {Name : "custom-metadata" },
297297 Spec : tsapi.ProxyClassSpec {StatefulSet : & tsapi.StatefulSet {
298- Labels : map [ string ] string {"foo" : "bar" },
298+ Labels : tsapi. Labels {"foo" : "bar" },
299299 Annotations : map [string ]string {"bar.io/foo" : "some-val" },
300300 Pod : & tsapi.Pod {Annotations : map [string ]string {"foo.io/bar" : "some-val" }}}},
301301 }
@@ -424,45 +424,14 @@ func TestTailscaleIngressWithProxyClass(t *testing.T) {
424424func TestTailscaleIngressWithServiceMonitor (t * testing.T ) {
425425 pc := & tsapi.ProxyClass {
426426 ObjectMeta : metav1.ObjectMeta {Name : "metrics" , Generation : 1 },
427- Spec : tsapi.ProxyClassSpec {
428- Metrics : & tsapi.Metrics {
429- Enable : true ,
430- ServiceMonitor : & tsapi.ServiceMonitor {Enable : true },
431- },
432- },
427+ Spec : tsapi.ProxyClassSpec {},
433428 Status : tsapi.ProxyClassStatus {
434429 Conditions : []metav1.Condition {{
435430 Status : metav1 .ConditionTrue ,
436431 Type : string (tsapi .ProxyClassReady ),
437432 ObservedGeneration : 1 ,
438433 }}},
439434 }
440- crd := & apiextensionsv1.CustomResourceDefinition {ObjectMeta : metav1.ObjectMeta {Name : serviceMonitorCRD }}
441- tsIngressClass := & networkingv1.IngressClass {ObjectMeta : metav1.ObjectMeta {Name : "tailscale" }, Spec : networkingv1.IngressClassSpec {Controller : "tailscale.com/ts-ingress" }}
442- fc := fake .NewClientBuilder ().
443- WithScheme (tsapi .GlobalScheme ).
444- WithObjects (pc , tsIngressClass ).
445- WithStatusSubresource (pc ).
446- Build ()
447- ft := & fakeTSClient {}
448- fakeTsnetServer := & fakeTSNetServer {certDomains : []string {"foo.com" }}
449- zl , err := zap .NewDevelopment ()
450- if err != nil {
451- t .Fatal (err )
452- }
453- ingR := & IngressReconciler {
454- Client : fc ,
455- ssr : & tailscaleSTSReconciler {
456- Client : fc ,
457- tsClient : ft ,
458- tsnetServer : fakeTsnetServer ,
459- defaultTags : []string {"tag:k8s" },
460- operatorNamespace : "operator-ns" ,
461- proxyImage : "tailscale/tailscale" ,
462- },
463- logger : zl .Sugar (),
464- }
465- // 1. Enable metrics- expect metrics Service to be created
466435 ing := & networkingv1.Ingress {
467436 TypeMeta : metav1.TypeMeta {Kind : "Ingress" , APIVersion : "networking.k8s.io/v1" },
468437 ObjectMeta : metav1.ObjectMeta {
@@ -491,8 +460,7 @@ func TestTailscaleIngressWithServiceMonitor(t *testing.T) {
491460 },
492461 },
493462 }
494- mustCreate (t , fc , ing )
495- mustCreate (t , fc , & corev1.Service {
463+ svc := & corev1.Service {
496464 ObjectMeta : metav1.ObjectMeta {
497465 Name : "test" ,
498466 Namespace : "default" ,
@@ -504,11 +472,38 @@ func TestTailscaleIngressWithServiceMonitor(t *testing.T) {
504472 Name : "http" },
505473 },
506474 },
507- })
508-
475+ }
476+ crd := & apiextensionsv1.CustomResourceDefinition {ObjectMeta : metav1.ObjectMeta {Name : serviceMonitorCRD }}
477+ tsIngressClass := & networkingv1.IngressClass {ObjectMeta : metav1.ObjectMeta {Name : "tailscale" }, Spec : networkingv1.IngressClassSpec {Controller : "tailscale.com/ts-ingress" }}
478+ fc := fake .NewClientBuilder ().
479+ WithScheme (tsapi .GlobalScheme ).
480+ WithObjects (pc , tsIngressClass , ing , svc ).
481+ WithStatusSubresource (pc ).
482+ Build ()
483+ ft := & fakeTSClient {}
484+ fakeTsnetServer := & fakeTSNetServer {certDomains : []string {"foo.com" }}
485+ zl , err := zap .NewDevelopment ()
486+ if err != nil {
487+ t .Fatal (err )
488+ }
489+ ingR := & IngressReconciler {
490+ Client : fc ,
491+ ssr : & tailscaleSTSReconciler {
492+ Client : fc ,
493+ tsClient : ft ,
494+ tsnetServer : fakeTsnetServer ,
495+ defaultTags : []string {"tag:k8s" },
496+ operatorNamespace : "operator-ns" ,
497+ proxyImage : "tailscale/tailscale" ,
498+ },
499+ logger : zl .Sugar (),
500+ }
509501 expectReconciled (t , ingR , "default" , "test" )
510-
511502 fullName , shortName := findGenName (t , fc , "default" , "test" , "ingress" )
503+ serveConfig := & ipn.ServeConfig {
504+ TCP : map [uint16 ]* ipn.TCPPortHandler {443 : {HTTPS : true }},
505+ Web : map [ipn.HostPort ]* ipn.WebServerConfig {"${TS_CERT_DOMAIN}:443" : {Handlers : map [string ]* ipn.HTTPHandler {"/" : {Proxy : "http://1.2.3.4:8080/" }}}},
506+ }
512507 opts := configOpts {
513508 stsName : shortName ,
514509 secretName : fullName ,
@@ -517,27 +512,51 @@ func TestTailscaleIngressWithServiceMonitor(t *testing.T) {
517512 parentType : "ingress" ,
518513 hostname : "default-test" ,
519514 app : kubetypes .AppIngressResource ,
520- enableMetrics : true ,
521515 namespaced : true ,
522516 proxyType : proxyTypeIngressResource ,
517+ serveConfig : serveConfig ,
518+ resourceVersion : "1" ,
523519 }
524- serveConfig := & ipn.ServeConfig {
525- TCP : map [uint16 ]* ipn.TCPPortHandler {443 : {HTTPS : true }},
526- Web : map [ipn.HostPort ]* ipn.WebServerConfig {"${TS_CERT_DOMAIN}:443" : {Handlers : map [string ]* ipn.HTTPHandler {"/" : {Proxy : "http://1.2.3.4:8080/" }}}},
527- }
528- opts .serveConfig = serveConfig
529520
530- expectEqual (t , fc , expectedSecret (t , fc , opts ), nil )
531- expectEqual (t , fc , expectedHeadlessService (shortName , "ingress" ), nil )
521+ // 1. Enable metrics- expect metrics Service to be created
522+ mustUpdate (t , fc , "" , "metrics" , func (proxyClass * tsapi.ProxyClass ) {
523+ proxyClass .Spec .Metrics = & tsapi.Metrics {Enable : true }
524+ })
525+ opts .enableMetrics = true
526+
527+ expectReconciled (t , ingR , "default" , "test" )
528+
532529 expectEqual (t , fc , expectedMetricsService (opts ), nil )
533- expectEqual ( t , fc , expectedSTSUserspace ( t , fc , opts ), removeHashAnnotation )
530+
534531 // 2. Enable ServiceMonitor - should not error when there is no ServiceMonitor CRD in cluster
535532 mustUpdate (t , fc , "" , "metrics" , func (pc * tsapi.ProxyClass ) {
536- pc .Spec .Metrics .ServiceMonitor = & tsapi.ServiceMonitor {Enable : true }
533+ pc .Spec .Metrics .ServiceMonitor = & tsapi.ServiceMonitor {Enable : true , Labels : tsapi. Labels { "foo" : "bar" } }
537534 })
538535 expectReconciled (t , ingR , "default" , "test" )
536+ expectEqual (t , fc , expectedMetricsService (opts ), nil )
537+
539538 // 3. Create ServiceMonitor CRD and reconcile- ServiceMonitor should get created
540539 mustCreate (t , fc , crd )
541540 expectReconciled (t , ingR , "default" , "test" )
541+ opts .serviceMonitorLabels = tsapi.Labels {"foo" : "bar" }
542+ expectEqual (t , fc , expectedMetricsService (opts ), nil )
543+ expectEqualUnstructured (t , fc , expectedServiceMonitor (t , opts ))
544+
545+ // 4. Update ServiceMonitor CRD and reconcile- ServiceMonitor should get updated
546+ mustUpdate (t , fc , pc .Namespace , pc .Name , func (proxyClass * tsapi.ProxyClass ) {
547+ proxyClass .Spec .Metrics .ServiceMonitor .Labels = nil
548+ })
549+ expectReconciled (t , ingR , "default" , "test" )
550+ opts .serviceMonitorLabels = nil
551+ opts .resourceVersion = "2"
552+ expectEqual (t , fc , expectedMetricsService (opts ), nil )
542553 expectEqualUnstructured (t , fc , expectedServiceMonitor (t , opts ))
554+
555+ // 5. Disable metrics - metrics resources should get deleted.
556+ mustUpdate (t , fc , pc .Namespace , pc .Name , func (proxyClass * tsapi.ProxyClass ) {
557+ proxyClass .Spec .Metrics = nil
558+ })
559+ expectReconciled (t , ingR , "default" , "test" )
560+ expectMissing [corev1.Service ](t , fc , "operator-ns" , metricsResourceName (shortName ))
561+ // ServiceMonitor gets garbage collected when the Service is deleted - we cannot test that here.
543562}
0 commit comments