@@ -18,28 +18,184 @@ package openstack
1818
1919import (
2020 "context"
21+ "errors"
2122 "fmt"
23+ "strings"
2224
2325 "github.com/openstack-k8s-operators/lib-common/modules/certmanager"
2426 "github.com/openstack-k8s-operators/lib-common/modules/common/clusterdns"
2527 "github.com/openstack-k8s-operators/lib-common/modules/common/condition"
2628 "github.com/openstack-k8s-operators/lib-common/modules/common/helper"
29+ "github.com/openstack-k8s-operators/lib-common/modules/common/object"
2730 "github.com/openstack-k8s-operators/lib-common/modules/common/service"
2831 "github.com/openstack-k8s-operators/lib-common/modules/common/tls"
2932 "github.com/openstack-k8s-operators/lib-common/modules/common/util"
3033
34+ "sigs.k8s.io/controller-runtime/pkg/client"
3135 "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
3236
3337 certmgrv1 "github.com/cert-manager/cert-manager/pkg/apis/certmanager/v1"
3438 novav1 "github.com/openstack-k8s-operators/nova-operator/api/v1beta1"
3539 corev1beta1 "github.com/openstack-k8s-operators/openstack-operator/apis/core/v1beta1"
40+ corev1 "k8s.io/api/core/v1"
3641 k8s_errors "k8s.io/apimachinery/pkg/api/errors"
3742 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
3843 "k8s.io/apimachinery/pkg/types"
3944 "k8s.io/utils/ptr"
4045 ctrl "sigs.k8s.io/controller-runtime"
4146)
4247
48+ // func deleteUndefinedNoVNCProxyCertsByCellName(
49+ // ctx context.Context,
50+ // instance *corev1beta1.OpenStackControlPlane,
51+ // helper *helper.Helper,
52+ // ) (ctrl.Result, error) {
53+
54+ // log := GetLogger(ctx)
55+ // // Fetch the list of certificates
56+ // allCerts := &certmgrv1.CertificateList{}
57+ // listOpts := []client.ListOption{
58+ // client.InNamespace(instance.GetNamespace()),
59+ // }
60+ // err := helper.GetClient().List(ctx, allCerts, listOpts...)
61+ // if err != nil {
62+ // return ctrl.Result{}, fmt.Errorf("could not get certs %w", err)
63+ // }
64+
65+ // allRoutes := &routev1.RouteList{}
66+ // listOpts = []client.ListOption{
67+ // client.InNamespace(instance.GetNamespace()),
68+ // }
69+ // err = helper.GetClient().List(ctx, allRoutes, listOpts...)
70+ // if err != nil {
71+ // return ctrl.Result{}, fmt.Errorf("could not get routes %w", err)
72+ // }
73+
74+ // novncproxyPrefix := "nova-novncproxy-cell"
75+
76+ // var delErrs []error
77+ // for _, cert := range allCerts.Items {
78+ // shouldDelete := ShouldCertRouteBeDeleted(cert.Name, novncproxyPrefix, instance)
79+ // if shouldDelete {
80+ // if object.CheckOwnerRefExist(instance.GetUID(), cert.OwnerReferences) {
81+ // log.Info("Deleting novncproxy cert %s", "", cert.Name)
82+ // err = DeleteCertificate(ctx, helper, instance.Namespace, cert.Name)
83+ // if err != nil {
84+ // delErrs = append(delErrs, fmt.Errorf("novncproxy cert deletion for '%s' failed, because: %w", cert.Name, err))
85+ // }
86+ // }
87+
88+ // }
89+ // }
90+
91+ // for _, route := range allRoutes.Items {
92+ // shouldDelete := ShouldCertRouteBeDeleted(route.Name, novncproxyPrefix, instance)
93+ // if shouldDelete {
94+ // if object.CheckOwnerRefExist(instance.GetUID(), route.OwnerReferences) {
95+ // log.Info("Deleting novncproxy route for %s", "", route.Name)
96+ // _, err := EnsureDeleted(ctx, helper, &route)
97+ // if err != nil {
98+ // delErrs = append(delErrs, fmt.Errorf("novncproxy route deletion for '%s' failed, because: %w", route.Name, err))
99+ // }
100+ // }
101+ // }
102+ // }
103+
104+ // if len(delErrs) > 0 {
105+ // delErrs := errors.Join(delErrs...)
106+ // return ctrl.Result{}, delErrs
107+ // }
108+
109+ // return ctrl.Result{}, nil
110+ // }
111+
112+ func deleteCertsAndRoutes (
113+ ctx context.Context ,
114+ instance * corev1beta1.OpenStackControlPlane ,
115+ helper * helper.Helper ,
116+ ) (ctrl.Result , error ) {
117+
118+ log := GetLogger (ctx )
119+
120+ novaNovncProxy := "nova-novncproxy"
121+ novncProxyLabelSelector := map [string ]string {
122+ "osctlplane-service" : novaNovncProxy ,
123+ }
124+
125+ routes , err := GetRoutesListWithLabel (
126+ ctx ,
127+ helper ,
128+ instance .Namespace ,
129+ novncProxyLabelSelector ,
130+ )
131+
132+ if err != nil {
133+ return ctrl.Result {}, fmt .Errorf ("could not get routes %w" , err )
134+ }
135+
136+ var delErrs []error
137+ for _ , route := range routes .Items {
138+ log .Info ("" , "xxx- route" , route .Name , "---" , route .Spec .To .Name )
139+
140+ svc := & corev1.Service {}
141+ err := helper .GetClient ().Get (ctx , types.NamespacedName {
142+ Name : route .Spec .To .Name ,
143+ Namespace : instance .Namespace ,
144+ }, svc )
145+
146+ if err != nil {
147+
148+ log .Info ("" , "xxx- can delete" , route .Name )
149+
150+ if ! object .CheckOwnerRefExist (instance .GetUID (), route .OwnerReferences ) {
151+ continue
152+ }
153+ // service not found for route, clean certs and route
154+ certs := & certmgrv1.CertificateList {}
155+ listOpts := []client.ListOption {
156+ client .InNamespace (instance .Namespace ),
157+ }
158+
159+ err = helper .GetClient ().List (ctx , certs , listOpts ... )
160+ if err != nil {
161+ log .Info ("" , "xxx- can't delete" , err )
162+ // return ctrl.Result{}, fmt.Errorf("could not get certs %w", err)
163+ continue
164+ }
165+
166+ for _ , cert := range certs .Items {
167+ // as we have 2 with cell-name and -vencrypt, so this is generic
168+ if strings .Contains (cert .Name , novaNovncProxy ) {
169+ // log.Info("", "t- cert", "-------", "Name", cert.Name)
170+ if object .CheckOwnerRefExist (instance .GetUID (), cert .OwnerReferences ) {
171+ log .Info ("xxx- " , "Deleting novncproxy cert %s" , "" , cert .Name )
172+ err = DeleteCertificate (ctx , helper , instance .Namespace , cert .Name )
173+ if err != nil {
174+ delErrs = append (delErrs , fmt .Errorf ("novncproxy cert deletion for '%s' failed, because: %w" , cert .Name , err ))
175+ }
176+ }
177+ }
178+ }
179+
180+ log .Info ("xxx-" , "Deleting novncproxy route for %s" , "" , route .Name )
181+ _ , err := EnsureDeleted (ctx , helper , & route )
182+ if err != nil {
183+ delErrs = append (delErrs , fmt .Errorf ("novncproxy route deletion for '%s' failed, because: %w" , route .Name , err ))
184+ }
185+
186+ } else {
187+ log .Info ("" , "xxx- can't delete" , route .Name )
188+ }
189+ }
190+
191+ if len (delErrs ) > 0 {
192+ delErrs := errors .Join (delErrs ... )
193+ return ctrl.Result {}, delErrs
194+ }
195+
196+ return ctrl.Result {}, nil
197+ }
198+
43199// ReconcileNova -
44200func ReconcileNova (ctx context.Context , instance * corev1beta1.OpenStackControlPlane , version * corev1beta1.OpenStackVersion , helper * helper.Helper ) (ctrl.Result , error ) {
45201 nova := & novav1.Nova {
@@ -209,6 +365,7 @@ func ReconcileNova(ctx context.Context, instance *corev1beta1.OpenStackControlPl
209365 }
210366
211367 // cell Metadata and NoVNCProxy
368+ Log .Info ("" , "" , "XXX ReconcileNova iterating cells" )
212369 for cellName , cellTemplate := range instance .Spec .Nova .Template .CellTemplates {
213370 // create certificate for Metadata agend if internal TLS and Metadata per cell is enabled
214371 if instance .Spec .TLS .PodLevel .Enabled &&
@@ -232,6 +389,7 @@ func ReconcileNova(ctx context.Context, instance *corev1beta1.OpenStackControlPl
232389
233390 // NoVNCProxy check for/creating route if service is enabled
234391 if noVNCProxyEnabled (cellTemplate .NoVNCProxyServiceTemplate ) {
392+ Log .Info ("" , "XXX ReconcileNova VNC enabled cell name" , cellName )
235393 if cellTemplate .NoVNCProxyServiceTemplate .Override .Service == nil {
236394 cellTemplate .NoVNCProxyServiceTemplate .Override .Service = & service.RoutedOverrideSpec {}
237395 }
@@ -247,6 +405,7 @@ func ReconcileNova(ctx context.Context, instance *corev1beta1.OpenStackControlPl
247405 }
248406
249407 // make sure to get to EndpointConfig when all service got created
408+ Log .Info ("" , "XXX vnc services" , svcs .Items )
250409 if len (svcs .Items ) == 1 {
251410 endpointDetails , ctrlResult , err := EnsureEndpointConfig (
252411 ctx ,
@@ -394,6 +553,18 @@ func ReconcileNova(ctx context.Context, instance *corev1beta1.OpenStackControlPl
394553 corev1beta1 .OpenStackControlPlaneNovaReadyRunningMessage ))
395554 }
396555
556+ // _, errs := deleteUndefinedNoVNCProxyCertsByCellName(ctx, instance, helper)
557+ _ , errs := deleteCertsAndRoutes (ctx , instance , helper )
558+ if errs != nil {
559+ instance .Status .Conditions .Set (condition .FalseCondition (
560+ corev1beta1 .OpenStackControlPlaneNovaReadyCondition ,
561+ condition .ErrorReason ,
562+ condition .SeverityWarning ,
563+ corev1beta1 .OpenStackControlPlaneNovaReadyErrorMessage ,
564+ errs ))
565+ return ctrl.Result {}, errs
566+ }
567+
397568 return ctrl.Result {}, nil
398569}
399570
0 commit comments