Skip to content

Commit 0445fcb

Browse files
Merge pull request #787 from fmount/cors
Automatically configure CORS section based on Horizon CR
2 parents 75c41fb + 3f9b5d4 commit 0445fcb

File tree

9 files changed

+64
-4
lines changed

9 files changed

+64
-4
lines changed

api/go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ require (
4343
github.com/prometheus/common v0.51.1 // indirect
4444
github.com/prometheus/procfs v0.13.0 // indirect
4545
github.com/spf13/pflag v1.0.5 // indirect
46+
github.com/stretchr/testify v1.10.0 // indirect
4647
golang.org/x/exp v0.0.0-20240719175910-8a7402abbf56 // indirect
4748
golang.org/x/net v0.28.0 // indirect
4849
golang.org/x/oauth2 v0.18.0 // indirect

api/go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,8 @@ github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
9999
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
100100
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
101101
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
102-
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
103-
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
102+
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
103+
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
104104
github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
105105
github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
106106
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=

config/rbac/role.yaml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,14 @@ rules:
176176
- get
177177
- patch
178178
- update
179+
- apiGroups:
180+
- horizon.openstack.org
181+
resources:
182+
- horizons
183+
verbs:
184+
- get
185+
- list
186+
- watch
179187
- apiGroups:
180188
- k8s.cni.cncf.io
181189
resources:

controllers/glanceapi_controller.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ import (
4444
glancev1 "github.com/openstack-k8s-operators/glance-operator/api/v1beta1"
4545
"github.com/openstack-k8s-operators/glance-operator/pkg/glance"
4646
"github.com/openstack-k8s-operators/glance-operator/pkg/glanceapi"
47+
horizonv1 "github.com/openstack-k8s-operators/horizon-operator/api/v1beta1"
4748
memcachedv1 "github.com/openstack-k8s-operators/infra-operator/apis/memcached/v1beta1"
4849
topologyv1 "github.com/openstack-k8s-operators/infra-operator/apis/topology/v1beta1"
4950
keystonev1 "github.com/openstack-k8s-operators/keystone-operator/api/v1beta1"
@@ -83,6 +84,7 @@ func (r *GlanceAPIReconciler) GetLogger(ctx context.Context) logr.Logger {
8384
// +kubebuilder:rbac:groups=glance.openstack.org,resources=glanceapis/status,verbs=get;update;patch
8485
// +kubebuilder:rbac:groups=glance.openstack.org,resources=glanceapis/finalizers,verbs=update;patch
8586
// +kubebuilder:rbac:groups=cinder.openstack.org,resources=cinders,verbs=get;list;watch
87+
// +kubebuilder:rbac:groups=horizon.openstack.org,resources=horizons,verbs=get;list;watch
8688
// +kubebuilder:rbac:groups=core,resources=secrets,verbs=get;list;watch;create;update;patch;delete;
8789
// +kubebuilder:rbac:groups=apps,resources=statefulsets,verbs=get;list;watch;create;update;patch;delete
8890
// +kubebuilder:rbac:groups=apps,resources=statefulsets/status,verbs=get;list;watch
@@ -333,6 +335,7 @@ func (r *GlanceAPIReconciler) SetupWithManager(ctx context.Context, mgr ctrl.Man
333335
return nil
334336
}
335337

338+
// Watch for changes in memcached used by Glance
336339
memcachedFn := func(_ context.Context, o client.Object) []reconcile.Request {
337340
result := []reconcile.Request{}
338341

@@ -385,6 +388,9 @@ func (r *GlanceAPIReconciler) SetupWithManager(ctx context.Context, mgr ctrl.Man
385388
Watches(&keystonev1.KeystoneAPI{},
386389
handler.EnqueueRequestsFromMapFunc(r.findObjectForSrc),
387390
builder.WithPredicates(keystonev1.KeystoneAPIStatusChangedPredicate)).
391+
Watches(&horizonv1.Horizon{},
392+
handler.EnqueueRequestsFromMapFunc(r.findObjectForSrc),
393+
builder.WithPredicates(horizonv1.HorizonEndpointChangedPredicate)).
388394
Complete(r)
389395
}
390396

@@ -1278,6 +1284,11 @@ func (r *GlanceAPIReconciler) generateServiceConfig(
12781284
templateParameters["TransportURL"] = string(notificationBusSecret.Data["transport_url"])
12791285
}
12801286

1287+
// Try to get Horizon endpoint and setup CORS section if the CR is found
1288+
if horizonEndpoint, err := r.GetHorizonEndpoint(ctx, h, instance); err == nil {
1289+
templateParameters["HorizonEndpoint"] = horizonEndpoint
1290+
}
1291+
12811292
// 00-default.conf will be regenerated as we have a ln -s of the
12821293
// templates/glance/config directory
12831294
// Do not generate -scripts as they are inherited from the top-level CR
@@ -1633,3 +1644,26 @@ func (r *GlanceAPIReconciler) glanceAPIRefresh(
16331644
}
16341645
return nil
16351646
}
1647+
1648+
// GetHorizonEndpoint - Returns the horizon endpoint set in the CR .Status
1649+
func (r *GlanceAPIReconciler) GetHorizonEndpoint(
1650+
ctx context.Context,
1651+
h *helper.Helper,
1652+
instance *glancev1.GlanceAPI,
1653+
) (string, error) {
1654+
Log := r.GetLogger(ctx)
1655+
1656+
horizon, err := horizonv1.GetHorizon(ctx, h, instance.Namespace)
1657+
if err != nil {
1658+
Log.Info("Error getting Horizon CR")
1659+
return "", err
1660+
}
1661+
1662+
ep, err := horizon.GetEndpoint()
1663+
if err != nil {
1664+
Log.Info("No endpoint present in the Status")
1665+
return "", err
1666+
}
1667+
1668+
return ep, nil
1669+
}

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ require (
1111
github.com/onsi/gomega v1.34.1
1212
github.com/openstack-k8s-operators/cinder-operator/api v0.6.1-0.20250819151225-552086a0c13a
1313
github.com/openstack-k8s-operators/glance-operator/api v0.0.0-00010101000000-000000000000
14+
github.com/openstack-k8s-operators/horizon-operator/api v0.6.1-0.20250823131712-0d9426060c76
1415
github.com/openstack-k8s-operators/infra-operator/apis v0.6.1-0.20250821143610-c8ef7b9a21ec
1516
github.com/openstack-k8s-operators/keystone-operator/api v0.6.1-0.20250818180001-057253e3d233
1617
github.com/openstack-k8s-operators/lib-common/modules/common v0.6.1-0.20250730071847-837b07f8d72f

go.sum

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,8 @@ github.com/openshift/api v0.0.0-20240830023148-b7d0481c9094 h1:J1wuGhVxpsHykZBa6
8080
github.com/openshift/api v0.0.0-20240830023148-b7d0481c9094/go.mod h1:CxgbWAlvu2iQB0UmKTtRu1YfepRg1/vJ64n2DlIEVz4=
8181
github.com/openstack-k8s-operators/cinder-operator/api v0.6.1-0.20250819151225-552086a0c13a h1:jxe0C5g4IOiucwSlcWiZeX7D+QHM9lg9MdaLSJ+XiMc=
8282
github.com/openstack-k8s-operators/cinder-operator/api v0.6.1-0.20250819151225-552086a0c13a/go.mod h1:Hx0ceOhQjOw3XermRVwO3OUpVeF2eifbi8Z3in5Ulk4=
83+
github.com/openstack-k8s-operators/horizon-operator/api v0.6.1-0.20250823131712-0d9426060c76 h1:Z94YiZtnUBrkl7NurhbLHtbk21LXFIN3XvRtmtiAz3U=
84+
github.com/openstack-k8s-operators/horizon-operator/api v0.6.1-0.20250823131712-0d9426060c76/go.mod h1:oFuhdOnGHrtdJrEdW1TUthTnAX2skVvm2zC9B0zLfrc=
8385
github.com/openstack-k8s-operators/infra-operator/apis v0.6.1-0.20250821143610-c8ef7b9a21ec h1:Jvz2BuTWCvjeCbDzpPzlPMrUQpEe04Rzi8LPNxeW0Ts=
8486
github.com/openstack-k8s-operators/infra-operator/apis v0.6.1-0.20250821143610-c8ef7b9a21ec/go.mod h1:Dv8qpmBIQy3Jv/EyQnOyc0w61X8vyfxpjcIQONP5CwY=
8587
github.com/openstack-k8s-operators/keystone-operator/api v0.6.1-0.20250818180001-057253e3d233 h1:1Kuny36wIpijE4RsFu8e+b0uUK8Gh0PgvlEVOOhG+uo=
@@ -114,8 +116,8 @@ github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
114116
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
115117
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
116118
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
117-
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
118-
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
119+
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
120+
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
119121
github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
120122
github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
121123
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=

main.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ import (
4343
cinderv1 "github.com/openstack-k8s-operators/cinder-operator/api/v1beta1"
4444
glancev1 "github.com/openstack-k8s-operators/glance-operator/api/v1beta1"
4545
"github.com/openstack-k8s-operators/glance-operator/controllers"
46+
horizonv1 "github.com/openstack-k8s-operators/horizon-operator/api/v1beta1"
4647
memcachedv1 "github.com/openstack-k8s-operators/infra-operator/apis/memcached/v1beta1"
4748
rabbitmqv1 "github.com/openstack-k8s-operators/infra-operator/apis/rabbitmq/v1beta1"
4849
topologyv1 "github.com/openstack-k8s-operators/infra-operator/apis/topology/v1beta1"
@@ -65,6 +66,7 @@ func init() {
6566
utilruntime.Must(mariadbv1.AddToScheme(scheme))
6667
utilruntime.Must(keystonev1.AddToScheme(scheme))
6768
utilruntime.Must(cinderv1.AddToScheme(scheme))
69+
utilruntime.Must(horizonv1.AddToScheme(scheme))
6870
utilruntime.Must(topologyv1.AddToScheme(scheme))
6971
utilruntime.Must(networkv1.AddToScheme(scheme))
7072
utilruntime.Must(rabbitmqv1.AddToScheme(scheme))

templates/common/config/00-config.conf

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,3 +124,8 @@ auth_endpoint={{ .KeystoneInternalURL }}
124124

125125
{{/* not "MinimalConfig" */ -}}
126126
{{ end -}}
127+
128+
{{ if (index . "HorizonEndpoint") }}
129+
[cors]
130+
allowed_origin={{ .HorizonEndpoint }}
131+
{{ end }}

test/functional/suite_test.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ import (
5454
cinderv1 "github.com/openstack-k8s-operators/cinder-operator/api/v1beta1"
5555
glancev1 "github.com/openstack-k8s-operators/glance-operator/api/v1beta1"
5656
"github.com/openstack-k8s-operators/glance-operator/controllers"
57+
horizonv1 "github.com/openstack-k8s-operators/horizon-operator/api/v1beta1"
5758
rabbitmqv1beta1 "github.com/openstack-k8s-operators/infra-operator/apis/rabbitmq/v1beta1"
5859
infra_test "github.com/openstack-k8s-operators/infra-operator/apis/test/helpers"
5960
keystonev1 "github.com/openstack-k8s-operators/keystone-operator/api/v1beta1"
@@ -113,6 +114,9 @@ var _ = BeforeSuite(func() {
113114
cinderCRDs, err := test.GetCRDDirFromModule(
114115
"github.com/openstack-k8s-operators/cinder-operator/api", gomod, "bases")
115116
Expect(err).ShouldNot(HaveOccurred())
117+
horizonCRDs, err := test.GetCRDDirFromModule(
118+
"github.com/openstack-k8s-operators/horizon-operator/api", gomod, "bases")
119+
Expect(err).ShouldNot(HaveOccurred())
116120
infraCRDs, err := test.GetCRDDirFromModule(
117121
"github.com/openstack-k8s-operators/infra-operator/apis", "../../go.mod", "bases")
118122
Expect(err).ShouldNot(HaveOccurred())
@@ -125,6 +129,7 @@ var _ = BeforeSuite(func() {
125129
keystoneCRDs,
126130
infraCRDs,
127131
cinderCRDs,
132+
horizonCRDs,
128133
},
129134
CRDInstallOptions: envtest.CRDInstallOptions{
130135
Paths: []string{
@@ -162,6 +167,8 @@ var _ = BeforeSuite(func() {
162167
Expect(err).NotTo(HaveOccurred())
163168
err = cinderv1.AddToScheme(scheme.Scheme)
164169
Expect(err).NotTo(HaveOccurred())
170+
err = horizonv1.AddToScheme(scheme.Scheme)
171+
Expect(err).NotTo(HaveOccurred())
165172

166173
//+kubebuilder:scaffold:scheme
167174

0 commit comments

Comments
 (0)