Skip to content

Commit 480b775

Browse files
committed
novncproxy cert and routes cleanup
Add tests for novncproxy certs and routes cleanup Closes: OSPRH-10549
1 parent 49734b3 commit 480b775

4 files changed

Lines changed: 298 additions & 49 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

tests/functional/ctlplane/base_test.go

Lines changed: 56 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -44,47 +44,50 @@ import (
4444
)
4545

4646
type Names struct {
47-
Namespace string
48-
OpenStackControlplaneName types.NamespacedName
49-
OpenStackVersionName types.NamespacedName
50-
KeystoneAPIName types.NamespacedName
51-
MemcachedName types.NamespacedName
52-
MemcachedCertName types.NamespacedName
53-
CinderName types.NamespacedName
54-
ManilaName types.NamespacedName
55-
GlanceName types.NamespacedName
56-
NeutronName types.NamespacedName
57-
HorizonName types.NamespacedName
58-
HeatName types.NamespacedName
59-
TelemetryName types.NamespacedName
60-
DBName types.NamespacedName
61-
DBCertName types.NamespacedName
62-
DBCell1Name types.NamespacedName
63-
DBCell1CertName types.NamespacedName
64-
RabbitMQName types.NamespacedName
65-
RabbitMQCertName types.NamespacedName
66-
RabbitMQCell1Name types.NamespacedName
67-
RabbitMQCell1CertName types.NamespacedName
68-
ServiceAccountName types.NamespacedName
69-
RoleName types.NamespacedName
70-
RoleBindingName types.NamespacedName
71-
RootCAPublicName types.NamespacedName
72-
RootCAInternalName types.NamespacedName
73-
RootCAOvnName types.NamespacedName
74-
RootCALibvirtName types.NamespacedName
75-
SelfSignedIssuerName types.NamespacedName
76-
CustomIssuerName types.NamespacedName
77-
CustomServiceCertSecretName types.NamespacedName
78-
CABundleName types.NamespacedName
79-
OpenStackClientName types.NamespacedName
80-
OVNNorthdName types.NamespacedName
81-
OVNNorthdCertName types.NamespacedName
82-
OVNControllerName types.NamespacedName
83-
OVNControllerCertName types.NamespacedName
84-
OVNDbServerNBName types.NamespacedName
85-
OVNDbServerSBName types.NamespacedName
86-
NeutronOVNCertName types.NamespacedName
87-
OpenStackTopology []types.NamespacedName
47+
Namespace string
48+
OpenStackControlplaneName types.NamespacedName
49+
OpenStackVersionName types.NamespacedName
50+
KeystoneAPIName types.NamespacedName
51+
MemcachedName types.NamespacedName
52+
MemcachedCertName types.NamespacedName
53+
CinderName types.NamespacedName
54+
ManilaName types.NamespacedName
55+
GlanceName types.NamespacedName
56+
NeutronName types.NamespacedName
57+
HorizonName types.NamespacedName
58+
HeatName types.NamespacedName
59+
TelemetryName types.NamespacedName
60+
DBName types.NamespacedName
61+
DBCertName types.NamespacedName
62+
DBCell1Name types.NamespacedName
63+
DBCell1CertName types.NamespacedName
64+
RabbitMQName types.NamespacedName
65+
RabbitMQCertName types.NamespacedName
66+
RabbitMQCell1Name types.NamespacedName
67+
RabbitMQCell1CertName types.NamespacedName
68+
NoVNCProxyCell1CertPublicRouteName types.NamespacedName
69+
NoVNCProxyCell1CertPublicSvcName types.NamespacedName
70+
NoVNCProxyCell1CertVencryptName types.NamespacedName
71+
ServiceAccountName types.NamespacedName
72+
RoleName types.NamespacedName
73+
RoleBindingName types.NamespacedName
74+
RootCAPublicName types.NamespacedName
75+
RootCAInternalName types.NamespacedName
76+
RootCAOvnName types.NamespacedName
77+
RootCALibvirtName types.NamespacedName
78+
SelfSignedIssuerName types.NamespacedName
79+
CustomIssuerName types.NamespacedName
80+
CustomServiceCertSecretName types.NamespacedName
81+
CABundleName types.NamespacedName
82+
OpenStackClientName types.NamespacedName
83+
OVNNorthdName types.NamespacedName
84+
OVNNorthdCertName types.NamespacedName
85+
OVNControllerName types.NamespacedName
86+
OVNControllerCertName types.NamespacedName
87+
OVNDbServerNBName types.NamespacedName
88+
OVNDbServerSBName types.NamespacedName
89+
NeutronOVNCertName types.NamespacedName
90+
OpenStackTopology []types.NamespacedName
8891
}
8992

9093
func CreateNames(openstackControlplaneName types.NamespacedName) Names {
@@ -200,6 +203,18 @@ func CreateNames(openstackControlplaneName types.NamespacedName) Names {
200203
Namespace: openstackControlplaneName.Namespace,
201204
Name: "cert-rabbitmq-cell1-svc",
202205
},
206+
NoVNCProxyCell1CertPublicRouteName: types.NamespacedName{
207+
Name: "cert-nova-novncproxy-cell1-public-route",
208+
Namespace: openstackControlplaneName.Namespace,
209+
},
210+
NoVNCProxyCell1CertPublicSvcName: types.NamespacedName{
211+
Name: "cert-nova-novncproxy-cell1-public-svc",
212+
Namespace: openstackControlplaneName.Namespace,
213+
},
214+
NoVNCProxyCell1CertVencryptName: types.NamespacedName{
215+
Name: "cert-nova-novncproxy-cell1-vencrypt",
216+
Namespace: openstackControlplaneName.Namespace,
217+
},
203218
OpenStackClientName: types.NamespacedName{
204219
Namespace: openstackControlplaneName.Namespace,
205220
Name: "openstackclient",

0 commit comments

Comments
 (0)