Skip to content

Commit b628e5e

Browse files
committed
novncproxy cert and routes cleanup
1 parent 49734b3 commit b628e5e

2 files changed

Lines changed: 102 additions & 0 deletions

File tree

pkg/openstack/common.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package openstack
33
import (
44
"context"
55
"fmt"
6+
"strings"
67
"time"
78

89
certmgrv1 "github.com/cert-manager/cert-manager/pkg/apis/certmanager/v1"
@@ -33,6 +34,7 @@ import (
3334
novav1 "github.com/openstack-k8s-operators/nova-operator/api/v1beta1"
3435
octaviav1 "github.com/openstack-k8s-operators/octavia-operator/api/v1beta1"
3536
corev1 "github.com/openstack-k8s-operators/openstack-operator/apis/core/v1beta1"
37+
corev1beta1 "github.com/openstack-k8s-operators/openstack-operator/apis/core/v1beta1"
3638
ovnv1 "github.com/openstack-k8s-operators/ovn-operator/api/v1beta1"
3739
placementv1 "github.com/openstack-k8s-operators/placement-operator/api/v1beta1"
3840
swiftv1 "github.com/openstack-k8s-operators/swift-operator/api/v1beta1"
@@ -815,3 +817,21 @@ func DeleteCertificate(
815817
helper.GetLogger().Info(fmt.Sprintf("Deleting cert %s", certName))
816818
return cert.Delete(ctx, helper)
817819
}
820+
821+
// this checks if cert can be deleted on the basis of cellName
822+
// Note: this is specific for certs/Route Name where cellName comes at index 2 - [0, 1, 2]
823+
// ex: nova-novncproxy-cell1-public-svc
824+
// but not: rabbitmq-cell1-svc
825+
func ShouldCertRouteBeDeleted(
826+
strObject string,
827+
prefixSubstring string,
828+
instance *corev1beta1.OpenStackControlPlane,
829+
) bool {
830+
if strings.HasPrefix(strObject, prefixSubstring) {
831+
cell := strings.Split(strObject, "-")[2]
832+
if _, exists := (instance.Spec.Nova.Template.CellTemplates)[cell]; !exists {
833+
return true
834+
}
835+
}
836+
return false
837+
}

pkg/openstack/nova.go

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,20 @@ package openstack
1818

1919
import (
2020
"context"
21+
"errors"
2122
"fmt"
2223

24+
routev1 "github.com/openshift/api/route/v1"
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"
@@ -40,6 +44,70 @@ import (
4044
ctrl "sigs.k8s.io/controller-runtime"
4145
)
4246

47+
func deleteUndefinedNoVNCProxyCertsByCellName(
48+
ctx context.Context,
49+
instance *corev1beta1.OpenStackControlPlane,
50+
helper *helper.Helper,
51+
) (ctrl.Result, error) {
52+
53+
log := GetLogger(ctx)
54+
// Fetch the list of certificates
55+
allCerts := &certmgrv1.CertificateList{}
56+
listOpts := []client.ListOption{
57+
client.InNamespace(instance.GetNamespace()),
58+
}
59+
err := helper.GetClient().List(ctx, allCerts, listOpts...)
60+
if err != nil {
61+
return ctrl.Result{}, fmt.Errorf("could not get certs %w", err)
62+
}
63+
64+
allRoutes := &routev1.RouteList{}
65+
listOpts = []client.ListOption{
66+
client.InNamespace(instance.GetNamespace()),
67+
}
68+
err = helper.GetClient().List(ctx, allRoutes, listOpts...)
69+
if err != nil {
70+
return ctrl.Result{}, fmt.Errorf("could not get routes %w", err)
71+
}
72+
73+
novncproxyPrefix := "nova-novncproxy-cell"
74+
75+
var delErrs []error
76+
for _, cert := range allCerts.Items {
77+
shouldDelete := ShouldCertRouteBeDeleted(cert.Name, novncproxyPrefix, instance)
78+
if shouldDelete {
79+
if object.CheckOwnerRefExist(instance.GetUID(), cert.OwnerReferences) {
80+
log.Info("Deleting novncproxy cert %s", "", cert.Name)
81+
err = DeleteCertificate(ctx, helper, instance.Namespace, cert.Name)
82+
if err != nil {
83+
delErrs = append(delErrs, fmt.Errorf("novncproxy cert deletion for '%s' failed, because: %w", cert.Name, err))
84+
}
85+
}
86+
87+
}
88+
}
89+
90+
for _, route := range allRoutes.Items {
91+
shouldDelete := ShouldCertRouteBeDeleted(route.Name, novncproxyPrefix, instance)
92+
if shouldDelete {
93+
if object.CheckOwnerRefExist(instance.GetUID(), route.OwnerReferences) {
94+
log.Info("Deleting novncproxy route for %s", "", route.Name)
95+
_, err := EnsureDeleted(ctx, helper, &route)
96+
if err != nil {
97+
delErrs = append(delErrs, fmt.Errorf("novncproxy route deletion for '%s' failed, because: %w", route.Name, err))
98+
}
99+
}
100+
}
101+
}
102+
103+
if len(delErrs) > 0 {
104+
delErrs := errors.Join(delErrs...)
105+
return ctrl.Result{}, delErrs
106+
}
107+
108+
return ctrl.Result{}, nil
109+
}
110+
43111
// ReconcileNova -
44112
func ReconcileNova(ctx context.Context, instance *corev1beta1.OpenStackControlPlane, version *corev1beta1.OpenStackVersion, helper *helper.Helper) (ctrl.Result, error) {
45113
nova := &novav1.Nova{
@@ -209,6 +277,7 @@ func ReconcileNova(ctx context.Context, instance *corev1beta1.OpenStackControlPl
209277
}
210278

211279
// cell Metadata and NoVNCProxy
280+
Log.Info("", "", "XXX ReconcileNova iterating cells")
212281
for cellName, cellTemplate := range instance.Spec.Nova.Template.CellTemplates {
213282
// create certificate for Metadata agend if internal TLS and Metadata per cell is enabled
214283
if instance.Spec.TLS.PodLevel.Enabled &&
@@ -232,6 +301,7 @@ func ReconcileNova(ctx context.Context, instance *corev1beta1.OpenStackControlPl
232301

233302
// NoVNCProxy check for/creating route if service is enabled
234303
if noVNCProxyEnabled(cellTemplate.NoVNCProxyServiceTemplate) {
304+
Log.Info("", "XXX ReconcileNova VNC enabled cell name", cellName)
235305
if cellTemplate.NoVNCProxyServiceTemplate.Override.Service == nil {
236306
cellTemplate.NoVNCProxyServiceTemplate.Override.Service = &service.RoutedOverrideSpec{}
237307
}
@@ -247,6 +317,7 @@ func ReconcileNova(ctx context.Context, instance *corev1beta1.OpenStackControlPl
247317
}
248318

249319
// make sure to get to EndpointConfig when all service got created
320+
Log.Info("", "XXX vnc services", svcs.Items)
250321
if len(svcs.Items) == 1 {
251322
endpointDetails, ctrlResult, err := EnsureEndpointConfig(
252323
ctx,
@@ -394,6 +465,17 @@ func ReconcileNova(ctx context.Context, instance *corev1beta1.OpenStackControlPl
394465
corev1beta1.OpenStackControlPlaneNovaReadyRunningMessage))
395466
}
396467

468+
_, errs := deleteUndefinedNoVNCProxyCertsByCellName(ctx, instance, helper)
469+
if errs != nil {
470+
instance.Status.Conditions.Set(condition.FalseCondition(
471+
corev1beta1.OpenStackControlPlaneNovaReadyCondition,
472+
condition.ErrorReason,
473+
condition.SeverityWarning,
474+
corev1beta1.OpenStackControlPlaneNovaReadyErrorMessage,
475+
errs))
476+
return ctrl.Result{}, errs
477+
}
478+
397479
return ctrl.Result{}, nil
398480
}
399481

0 commit comments

Comments
 (0)