Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,30 @@ func makeWebGatewayConfigMap(p *providers.Provider) (string, error) {
}
bopHostname := fmt.Sprintf("%s-%s.%s.svc:8090", p.Env.GetClowdName(), "mbop", p.Env.GetClowdNamespace())

upstreamList, whitelistStrings := buildUpstreamAndWhiteLists(bopHostname, appList)

cmData, err := GenerateConfig(
getCertHostname(p.Env.Status.Hostname),
fmt.Sprintf("http://%s", bopHostname),
whitelistStrings,
upstreamList,
)
if err != nil {
return "", err
}

cm.Data = map[string]string{
"Caddyfile.json": cmData,
}

h := sha256.New()
h.Write([]byte(cmData))
hash := fmt.Sprintf("%x", h.Sum(nil))

return hash, p.Cache.Update(CoreCaddyConfigMap, cm)
}

func buildUpstreamAndWhiteLists(bopHostname string, appList *crd.ClowdAppList) ([]ProxyRoute, []string) {
whitelistStrings := []string{}
upstreamList := []ProxyRoute{{
Upstream: bopHostname,
Expand Down Expand Up @@ -280,41 +304,34 @@ func makeWebGatewayConfigMap(p *providers.Provider) (string, error) {
continue
}

apiPath := innerDeployment.WebServices.Public.APIPath

if apiPath == "" {
apiPath = innerDeployment.Name
}

name := innerApp.GetDeploymentNamespacedName(&innerDeployment).Name
hostname := fmt.Sprintf("%s.%s.svc", name, innerApp.Namespace)

upstreamList = append(upstreamList, ProxyRoute{
Upstream: fmt.Sprintf("%s:%d", hostname, 8000),
Path: fmt.Sprintf("/api/%s/*", apiPath),
})
}
}
if innerDeployment.WebServices.Public.APIPaths != nil {
// apiPaths was defined, use it and ignore 'apiPath'
for _, apiPath := range innerDeployment.WebServices.Public.APIPaths {
upstreamList = append(upstreamList, ProxyRoute{
Upstream: fmt.Sprintf("%s:%d", hostname, 8000),
Path: string(apiPath),
})
}
} else {
apiPath := innerDeployment.WebServices.Public.APIPath

if apiPath == "" {
apiPath = innerDeployment.Name
}

upstreamList = append(upstreamList, ProxyRoute{
Upstream: fmt.Sprintf("%s:%d", hostname, 8000),
Path: fmt.Sprintf("/api/%s/*", apiPath),
})

cmData, err := GenerateConfig(
getCertHostname(p.Env.Status.Hostname),
fmt.Sprintf("http://%s", bopHostname),
whitelistStrings,
upstreamList,
)
if err != nil {
return "", err
}

cm.Data = map[string]string{
"Caddyfile.json": cmData,
}
}
}

h := sha256.New()
h.Write([]byte(cmData))
hash := fmt.Sprintf("%x", h.Sum(nil))

return hash, p.Cache.Update(CoreCaddyConfigMap, cm)
return upstreamList, whitelistStrings
}

func makeWebGatewayDeployment(_ *crd.ClowdEnvironment, o obj.ClowdObject, objMap providers.ObjectMap, _ bool, _ bool) error {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package web

import (
"testing"

crd "github.com/RedHatInsights/clowder/apis/cloud.redhat.com/v1alpha1"
"github.com/stretchr/testify/assert"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

func TestBuildUpstreamAndWhiteLists(t *testing.T) {
bopHostname := "test-bop-hostname"
appList := &crd.ClowdAppList{
Items: []crd.ClowdApp{
{
Spec: crd.ClowdAppSpec{
Deployments: []crd.Deployment{
{
Name: "test-deployment-1",
WebServices: crd.WebServices{
Public: crd.PublicWebService{
Enabled: true,
APIPath: "test-api",
WhitelistPaths: []string{"/whitelist-path-1", "/whitelist-path-2"},
},
},
},
},
},
ObjectMeta: metav1.ObjectMeta{
Name: "test-app-1",
Namespace: "test-namespace-1",
},
},
{
Spec: crd.ClowdAppSpec{
Deployments: []crd.Deployment{
{
Name: "test-deployment-2",
WebServices: crd.WebServices{
Public: crd.PublicWebService{
Enabled: true,
APIPaths: []crd.APIPath{
"/api-path-1",
"/api-path-2",
},
},
},
},
},
},
ObjectMeta: metav1.ObjectMeta{
Name: "test-app-2",
Namespace: "test-namespace-2",
},
},
},
}

// Call the function under test
upstreamList, whitelistStrings := buildUpstreamAndWhiteLists(bopHostname, appList)

expectedUpstreamList := []ProxyRoute{
{Upstream: bopHostname, Path: "/v1/registrations*"},
{Upstream: bopHostname, Path: "/v1/check_registration*"},
{Upstream: "test-app-1-test-deployment-1.test-namespace-1.svc:8000", Path: "/api/test-api/*"},
{Upstream: "test-app-2-test-deployment-2.test-namespace-2.svc:8000", Path: "/api-path-1"},
{Upstream: "test-app-2-test-deployment-2.test-namespace-2.svc:8000", Path: "/api-path-2"},
}

expectedWhitelistStrings := []string{
"/whitelist-path-1",
"/whitelist-path-2",
}

assert.ElementsMatch(t, expectedUpstreamList, upstreamList, "Upstream list does not match expected")
assert.ElementsMatch(t, expectedWhitelistStrings, whitelistStrings, "Whitelist strings do not match expected")
}
Loading