Skip to content

Commit

Permalink
Refactor exposure to have an ability share host among components
Browse files Browse the repository at this point in the history
  • Loading branch information
sleshchenko committed Apr 6, 2021
1 parent 4b003c7 commit a3ca1f1
Show file tree
Hide file tree
Showing 11 changed files with 123 additions and 98 deletions.
9 changes: 6 additions & 3 deletions pkg/controller/che/che_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -845,10 +845,11 @@ func (r *ReconcileChe) Reconcile(request reconcile.Request) (reconcile.Result, e
exposedServiceName := getServerExposingServiceName(instance)
cheHost := ""
if !isOpenShift {
done, err := deploy.SyncIngressToCluster(
_, done, err := deploy.SyncIngressToCluster(
deployContext,
cheFlavor,
instance.Spec.Server.CheHost,
"",
exposedServiceName,
8080,
deployContext.CheCluster.Spec.Server.CheServerIngress,
Expand Down Expand Up @@ -882,6 +883,7 @@ func (r *ReconcileChe) Reconcile(request reconcile.Request) (reconcile.Result, e
deployContext,
cheFlavor,
customHost,
"",
exposedServiceName,
8080,
deployContext.CheCluster.Spec.Server.CheServerRoute,
Expand Down Expand Up @@ -918,7 +920,7 @@ func (r *ReconcileChe) Reconcile(request reconcile.Request) (reconcile.Result, e
}
}

provisioned, err = devfile_registry.SyncDevfileRegistryToCluster(deployContext, cheHost)
provisioned, err = devfile_registry.SyncDevfileRegistryToCluster(deployContext)
if !tests {
if !provisioned {
if err != nil {
Expand All @@ -928,7 +930,7 @@ func (r *ReconcileChe) Reconcile(request reconcile.Request) (reconcile.Result, e
}
}

provisioned, err = plugin_registry.SyncPluginRegistryToCluster(deployContext, cheHost)
provisioned, err = plugin_registry.SyncPluginRegistryToCluster(deployContext)
if !tests {
if !provisioned {
if err != nil {
Expand Down Expand Up @@ -1059,6 +1061,7 @@ func getDefaultCheHost(deployContext *deploy.DeployContext) (string, error) {
deployContext,
cheFlavor,
"",
"",
getServerExposingServiceName(deployContext.CheCluster),
8080,
deployContext.CheCluster.Spec.Server.CheServerRoute,
Expand Down
6 changes: 2 additions & 4 deletions pkg/deploy/devfile-registry/devfile_registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,14 @@ type DevFileRegistryConfigMap struct {
/**
* Create devfile registry resources unless an external registry is used.
*/
func SyncDevfileRegistryToCluster(deployContext *deploy.DeployContext, cheHost string) (bool, error) {
func SyncDevfileRegistryToCluster(deployContext *deploy.DeployContext) (bool, error) {
devfileRegistryURL := deployContext.CheCluster.Spec.Server.DevfileRegistryUrl
if !deployContext.CheCluster.Spec.Server.ExternalDevfileRegistry {
endpoint, done, err := expose.Expose(
deployContext,
cheHost,
deploy.DevfileRegistryName,
deployContext.CheCluster.Spec.Server.DevfileRegistryRoute,
deployContext.CheCluster.Spec.Server.DevfileRegistryIngress,
deploy.DevfileRegistryName)
deployContext.CheCluster.Spec.Server.DevfileRegistryIngress)
if !done {
return false, err
}
Expand Down
123 changes: 67 additions & 56 deletions pkg/deploy/expose/expose.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
package expose

import (
"strings"

orgv1 "github.com/eclipse-che/che-operator/pkg/apis/org/v1"
"github.com/eclipse-che/che-operator/pkg/deploy"
"github.com/eclipse-che/che-operator/pkg/deploy/gateway"
Expand All @@ -20,63 +22,47 @@ import (
extentionsv1beta1 "k8s.io/api/extensions/v1beta1"
)

//Expose exposes the specified component according to the configured exposure strategy rules
func Expose(
deployContext *deploy.DeployContext,
cheHost string,
endpointName string,
componentName string,
routeCustomSettings orgv1.RouteCustomSettings,
ingressCustomSettings orgv1.IngressCustomSettings) (endpointUrl string, done bool, err error) {
//the host and path are empty and will be evaluated for the specified component
return ExposeWithHostPath(deployContext, componentName, "", "", routeCustomSettings, ingressCustomSettings)
}

//Expose exposes the specified component on the specified host and domain.
//Empty host or path will be evaluated according to the configured strategy rules.
//Note: path may be prefixed according to the configured strategy rules.
func ExposeWithHostPath(
deployContext *deploy.DeployContext,
component string,
host string,
path string,
routeCustomSettings orgv1.RouteCustomSettings,
ingressCustomSettings orgv1.IngressCustomSettings,
component string) (endpont string, done bool, err error) {
ingressCustomSettings orgv1.IngressCustomSettings) (endpointUrl string, done bool, err error) {

exposureStrategy := util.GetServerExposureStrategy(deployContext.CheCluster, deploy.DefaultServerExposureStrategy)
var domain string
var endpoint string
var pathPrefix string
var stripPrefix bool

if endpointName == deploy.IdentityProviderName {
pathPrefix = "auth"
stripPrefix = false
} else {
pathPrefix = endpointName
stripPrefix = true
}
if exposureStrategy == "multi-host" {
// this won't get used on openshift, because there we're intentionally let Openshift decide on the domain name
domain = endpointName + "-" + deployContext.CheCluster.Namespace + "." + deployContext.CheCluster.Spec.K8s.IngressDomain
endpoint = domain
} else {
domain = cheHost
if endpointName == deploy.IdentityProviderName {
// legacy
endpoint = domain
} else {
endpoint = domain + "/" + pathPrefix
}
if path != "" && !strings.HasPrefix(path, "/") {
path = "/" + path
}

gatewayConfig := "che-gateway-route-" + endpointName
singleHostExposureType := deploy.GetSingleHostExposureType(deployContext.CheCluster)
useGateway := exposureStrategy == "single-host" && (util.IsOpenShift || singleHostExposureType == "gateway")

gatewayConfig := "che-gateway-route-" + component
if !util.IsOpenShift {
if useGateway {
cfg := gateway.GetGatewayRouteConfig(deployContext, gatewayConfig, "/"+pathPrefix, 10, "http://"+endpointName+":8080", stripPrefix)
done, err := deploy.SyncConfigMapSpecToCluster(deployContext, &cfg)
if !util.IsTestMode() {
if !done {
if err != nil {
logrus.Error(err)
}
return "", false, err
return exposeWithGateway(deployContext, gatewayConfig, component, path, func() {
if _, err = deploy.DeleteNamespacedObject(deployContext, component, &extentionsv1beta1.Ingress{}); err != nil {
logrus.Error(err)
}
}
if _, err = deploy.DeleteNamespacedObject(deployContext, endpointName, &extentionsv1beta1.Ingress{}); err != nil {
logrus.Error(err)
}
})
} else {
done, err := deploy.SyncIngressToCluster(deployContext, endpointName, domain, endpointName, 8080, ingressCustomSettings, component)
endpointUrl, done, err = deploy.SyncIngressToCluster(deployContext, component, host, path, component, 8080, ingressCustomSettings, component)
if !done {
logrus.Infof("Waiting on ingress '%s' to be ready", endpointName)
logrus.Infof("Waiting on ingress '%s' to be ready", component)
if err != nil {
logrus.Error(err)
}
Expand All @@ -85,25 +71,21 @@ func Expose(
if err := gateway.DeleteGatewayRouteConfig(gatewayConfig, deployContext); !util.IsTestMode() && err != nil {
logrus.Error(err)
}

return endpointUrl, true, nil
}
} else {
if useGateway {
cfg := gateway.GetGatewayRouteConfig(deployContext, gatewayConfig, "/"+pathPrefix, 10, "http://"+endpointName+":8080", stripPrefix)
done, err := deploy.SyncConfigMapSpecToCluster(deployContext, &cfg)
if !done {
if err != nil {
return exposeWithGateway(deployContext, gatewayConfig, component, path, func() {
if err := deploy.DeleteRouteIfExists(component, deployContext); !util.IsTestMode() && err != nil {
logrus.Error(err)
}
return "", false, err
}
if err := deploy.DeleteRouteIfExists(endpointName, deployContext); !util.IsTestMode() && err != nil {
logrus.Error(err)
}
})
} else {
// the empty string for a host is intentional here - we let OpenShift decide on the hostname
route, err := deploy.SyncRouteToCluster(deployContext, endpointName, "", endpointName, 8080, routeCustomSettings, component)
route, err := deploy.SyncRouteToCluster(deployContext, component, host, path, component, 8080, routeCustomSettings, component)
if route == nil {
logrus.Infof("Waiting on route '%s' to be ready", endpointName)
logrus.Infof("Waiting on route '%s' to be ready", component)
if err != nil {
logrus.Error(err)
}
Expand All @@ -114,8 +96,37 @@ func Expose(
logrus.Error(err)
}

endpoint = route.Spec.Host
return route.Spec.Host + route.Spec.Path, true, nil
}
}
}

func exposeWithGateway(deployContext *deploy.DeployContext,
gatewayConfig string,
component string,
path string,
cleanUpRouting func()) (endpointUrl string, done bool, err error) {

var pathPrefix string
var stripPrefix bool
if component == deploy.IdentityProviderName {
pathPrefix = "auth" + path
stripPrefix = false
} else {
pathPrefix = component + path
stripPrefix = true
}

cfg := gateway.GetGatewayRouteConfig(deployContext, component, gatewayConfig, "/"+pathPrefix, 10, "http://"+component+":8080", stripPrefix)
done, err = deploy.SyncConfigMapSpecToCluster(deployContext, &cfg)
if !done {
if err != nil {
logrus.Error(err)
}
return "", false, err
}
return endpoint, true, nil

cleanUpRouting()

return "", true, err
}
8 changes: 4 additions & 4 deletions pkg/deploy/gateway/gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ func delete(clusterAPI deploy.ClusterAPI, obj metav1.Object) error {
// GetGatewayRouteConfig creates a config map with traefik configuration for a single new route.
// `serviceName` is an arbitrary name identifying the configuration. This should be unique within operator. Che server only creates
// new configuration for workspaces, so the name should not resemble any of the names created by the Che server.
func GetGatewayRouteConfig(deployContext *deploy.DeployContext, serviceName string, pathPrefix string, priority int, internalUrl string, stripPrefix bool) corev1.ConfigMap {
func GetGatewayRouteConfig(deployContext *deploy.DeployContext, component string, serviceName string, pathPrefix string, priority int, internalUrl string, stripPrefix bool) corev1.ConfigMap {
pathRewrite := pathPrefix != "/" && stripPrefix

data := `---
Expand Down Expand Up @@ -225,14 +225,14 @@ http:
Kind: "ConfigMap",
},
ObjectMeta: metav1.ObjectMeta{
Name: serviceName,
Name: component,
Namespace: deployContext.CheCluster.Namespace,
Labels: util.MergeMaps(
deploy.GetLabels(deployContext.CheCluster, gatewayConfigComponentName),
util.GetMapValue(deployContext.CheCluster.Spec.Server.SingleHostGatewayConfigMapLabels, deploy.DefaultSingleHostGatewayConfigMapLabels)),
},
Data: map[string]string{
serviceName + ".yml": data,
component + ".yml": data,
},
}

Expand All @@ -255,7 +255,7 @@ func DeleteGatewayRouteConfig(serviceName string, deployContext *deploy.DeployCo
// below functions declare the desired states of the various objects required for the gateway

func getGatewayServerConfigSpec(deployContext *deploy.DeployContext) corev1.ConfigMap {
return GetGatewayRouteConfig(deployContext, gatewayServerConfigName, "/", 1, "http://"+deploy.CheServiceName+":8080", false)
return GetGatewayRouteConfig(deployContext, gatewayServerConfigName, gatewayServerConfigName, "/", 1, "http://"+deploy.CheServiceName+":8080", false)
}

func getGatewayServiceAccountSpec(instance *orgv1.CheCluster) corev1.ServiceAccount {
Expand Down
4 changes: 1 addition & 3 deletions pkg/deploy/identity-provider/identity_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,9 @@ func syncExposure(deployContext *deploy.DeployContext) (bool, error) {
false: "http"})[cr.Spec.Server.TlsSupport]
endpoint, done, err := expose.Expose(
deployContext,
cr.Spec.Server.CheHost,
deploy.IdentityProviderName,
cr.Spec.Auth.IdentityProviderRoute,
cr.Spec.Auth.IdentityProviderIngress,
deploy.IdentityProviderName)
cr.Spec.Auth.IdentityProviderIngress)
if !done {
return false, err
}
Expand Down
9 changes: 6 additions & 3 deletions pkg/deploy/ingres_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ func TestIngressSpec(t *testing.T) {
name string
ingressName string
ingressHost string
ingressPath string
ingressComponent string
serviceName string
servicePort int
Expand All @@ -57,6 +58,7 @@ func TestIngressSpec(t *testing.T) {
ingressName: "test",
ingressComponent: "test-component",
ingressHost: "test-host",
ingressPath: "",
serviceName: "che",
servicePort: 8080,
ingressCustomSettings: orgv1.IngressCustomSettings{
Expand Down Expand Up @@ -124,9 +126,10 @@ func TestIngressSpec(t *testing.T) {
},
}

actualIngress := GetIngressSpec(deployContext,
actualIngress, _ := GetIngressSpec(deployContext,
testCase.ingressName,
testCase.ingressHost,
testCase.ingressPath,
testCase.serviceName,
testCase.servicePort,
testCase.ingressCustomSettings,
Expand Down Expand Up @@ -157,12 +160,12 @@ func TestSyncIngressToCluster(t *testing.T) {
},
}

done, err := SyncIngressToCluster(deployContext, "test", "host-1", "service-1", 8080, orgv1.IngressCustomSettings{}, "component")
_, done, err := SyncIngressToCluster(deployContext, "test", "host-1", "", "service-1", 8080, orgv1.IngressCustomSettings{}, "component")
if !done || err != nil {
t.Fatalf("Failed to sync ingress: %v", err)
}

done, err = SyncIngressToCluster(deployContext, "test", "host-2", "service-2", 8080, orgv1.IngressCustomSettings{}, "component")
_, done, err = SyncIngressToCluster(deployContext, "test", "host-2", "", "service-2", 8080, orgv1.IngressCustomSettings{}, "component")
if !done || err != nil {
t.Fatalf("Failed to sync ingress: %v", err)
}
Expand Down
Loading

0 comments on commit a3ca1f1

Please sign in to comment.