Skip to content

Commit

Permalink
kie-kogito-serverless-operator-536: Adjust cors configuration for the…
Browse files Browse the repository at this point in the history
… dev profile deployments, and JS and DI deployments (apache#537)
  • Loading branch information
wmedvede authored and rgdoliveira committed Oct 3, 2024
1 parent b73588f commit 6246c83
Show file tree
Hide file tree
Showing 8 changed files with 215 additions and 159 deletions.
6 changes: 3 additions & 3 deletions config/manager/manager.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,9 @@ spec:
resources:
limits:
cpu: 500m
memory: 128Mi
memory: 500Mi
requests:
cpu: 10m
memory: 64Mi
cpu: 100m
memory: 200Mi
serviceAccountName: controller-manager
terminationGracePeriodSeconds: 10
4 changes: 2 additions & 2 deletions internal/controller/discovery/discovery_openshift_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@ import (
)

func Test_QueryOpenShiftRoute(t *testing.T) {
doTestQueryOpenShiftRoute(t, false, "http://openshiftroutehost1:80")
doTestQueryOpenShiftRoute(t, false, "http://openshiftroutehost1")
}

func Test_QueryOpenShiftRouteWithTLS(t *testing.T) {
doTestQueryOpenShiftRoute(t, true, "https://openshiftroutehost1:443")
doTestQueryOpenShiftRoute(t, true, "https://openshiftroutehost1")
}

func doTestQueryOpenShiftRoute(t *testing.T, tls bool, expectedUri string) {
Expand Down
5 changes: 2 additions & 3 deletions internal/controller/discovery/openshift_catalog.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,12 +107,11 @@ func (c openShiftServiceCatalog) resolveOpenShiftRouteQuery(ctx context.Context,
return "", err
} else {
scheme := httpProtocol
port := defaultHttpPort
if route.Spec.TLS != nil {
scheme = httpsProtocol
port = defaultHttpsPort
}
return buildURI(scheme, route.Spec.Host, port), nil
// the OpenShift routes are only opened at the http/https standard ports.
return fmt.Sprintf("%s://%s", scheme, route.Spec.Host), nil
}
}

Expand Down
19 changes: 1 addition & 18 deletions internal/controller/platform/services/services.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,14 +197,6 @@ func (d *DataIndexHandler) GetEnvironmentVariables() []corev1.EnvVar {
Name: "KOGITO_DATA_INDEX_QUARKUS_PROFILE",
Value: "http-events-support",
},
{
Name: "QUARKUS_HTTP_CORS",
Value: "true",
},
{
Name: "QUARKUS_HTTP_CORS_ORIGINS",
Value: "/.*/",
},
}
}

Expand Down Expand Up @@ -373,16 +365,7 @@ func (j *JobServiceHandler) GetLocalServiceBaseUrl() string {
}

func (j *JobServiceHandler) GetEnvironmentVariables() []corev1.EnvVar {
return []corev1.EnvVar{
{
Name: "QUARKUS_HTTP_CORS",
Value: "true",
},
{
Name: "QUARKUS_HTTP_CORS_ORIGINS",
Value: "/.*/",
},
}
return []corev1.EnvVar{}
}

func (j *JobServiceHandler) GetPodResourceRequirements() corev1.ResourceRequirements {
Expand Down
3 changes: 3 additions & 0 deletions internal/controller/profiles/common/constants/workflows.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,7 @@ const (
KnativeHealthEnabled = "org.kie.kogito.addons.knative.eventing.health-enabled"
KnativeInjectedEnvVar = "${K_SINK}"
TriggerFinalizer = "trigger-deletion"
QuarkusDevUICorsEnabled = "quarkus.dev-ui.cors.enabled"
QuarkusHttpCors = "quarkus.http.cors"
QuarkusHttpCorsOrigins = "quarkus.http.cors.origins"
)
33 changes: 33 additions & 0 deletions internal/controller/profiles/common/properties/managed.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ import (
"context"
"fmt"

"github.com/apache/incubator-kie-kogito-serverless-operator/internal/controller/profiles"

"github.com/apache/incubator-kie-kogito-serverless-operator/internal/controller/profiles/common/persistence"

"github.com/apache/incubator-kie-kogito-serverless-operator/utils"
Expand Down Expand Up @@ -96,6 +98,10 @@ func (a *managedPropertyHandler) Build() string {
// produce the MicroProfileConfigServiceCatalog properties for the service discovery property values if any.
discoveryProps.Merge(generateDiscoveryProperties(a.ctx, a.catalog, userProps, a.workflow))
}
if profiles.IsDevProfile(a.workflow) && a.requireServiceDiscovery() {
// produce dev profile properties that must be calculated at service discovery time.
setDevProfileDiscoveryProperties(a.ctx, a.catalog, a.defaultManagedProperties, a.workflow)
}
userProps = utils.NewApplicationPropertiesBuilder().
WithInitialProperties(discoveryProps).
WithImmutableProperties(properties.MustLoadString(immutableApplicationProperties)).
Expand Down Expand Up @@ -148,6 +154,9 @@ func NewManagedPropertyHandler(workflow *operatorapi.SonataFlow, platform *opera
platform: platform,
}
props := properties.NewProperties()
if profiles.IsDevProfile(workflow) {
setDevProfileProperties(props)
}
props.Set(constants.KogitoUserTasksEventsEnabled, "false")
if platform != nil {
p, err := resolvePlatformWorkflowProperties(platform)
Expand Down Expand Up @@ -183,6 +192,30 @@ func NewManagedPropertyHandler(workflow *operatorapi.SonataFlow, platform *opera
return handler.withKogitoServiceUrl(), nil
}

func setDevProfileProperties(props *properties.Properties) {
props.Set(constants.QuarkusDevUICorsEnabled, "false")
}

func setDevProfileDiscoveryProperties(ctx context.Context, catalog discovery.ServiceCatalog, props *properties.Properties, workflow *operatorapi.SonataFlow) {
if utils.IsOpenShift() {
// in OpenShift deployments the route is created before the workflow, at this point it can be queried safely.
routeUrl, err := catalog.Query(ctx, *discovery.NewResourceUriBuilder(discovery.OpenshiftScheme).
Kind("routes").
Group("route.openshift.io").
Version("v1").
Namespace(workflow.Namespace).
Name(workflow.Name).
Build(),
discovery.KubernetesDNSAddress)
if err != nil {
klog.V(log.E).ErrorS(err, "An error was produced while getting workflow route url. ", "workflow", workflow.Name)
} else {
props.Set(constants.QuarkusHttpCors, "true")
props.Set(constants.QuarkusHttpCorsOrigins, routeUrl)
}
}
}

// ApplicationManagedProperties immutable default application properties that can be used with any workflow based on Quarkus.
// Alias for NewManagedPropertyHandler(workflow).Build()
func ApplicationManagedProperties(workflow *operatorapi.SonataFlow, platform *operatorapi.SonataFlowPlatform) (string, error) {
Expand Down
Loading

0 comments on commit 6246c83

Please sign in to comment.