Skip to content

Commit

Permalink
Merge pull request #216 from RedHatInsights/multiple-cachebust-urls
Browse files Browse the repository at this point in the history
Allow multiple cache bust URLs
  • Loading branch information
adamrdrew authored Nov 26, 2024
2 parents cafaa9d + e966a51 commit b364d4f
Show file tree
Hide file tree
Showing 10 changed files with 382 additions and 21 deletions.
5 changes: 4 additions & 1 deletion api/v1alpha1/frontendenvironment_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,11 @@ type FrontendEnvironmentSpec struct {
EnableAkamaiCacheBust bool `json:"enableAkamaiCacheBust,omitempty"`
// Set Akamai Cache Bust Image
AkamaiCacheBustImage string `json:"akamaiCacheBustImage,omitempty"`
// Set Akamai Cache Bust URL that the files will hang off of
// Deprecated: Users should move to AkamaiCacheBustURLs
// Preserving for backwards compatibility
AkamaiCacheBustURL string `json:"akamaiCacheBustURL,omitempty"`
// Set Akamai Cache Bust URL that the files will hang off of
AkamaiCacheBustURLs []string `json:"akamaiCacheBustURLs,omitempty"`
// The name of the secret we will use to get the akamai credentials
AkamaiSecretName string `json:"akamaiSecretName,omitempty"`
// List of namespaces that should receive a copy of the frontend configuration as a config map
Expand Down
5 changes: 5 additions & 0 deletions api/v1alpha1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 8 additions & 1 deletion config/crd/bases/cloud.redhat.com_frontendenvironments.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,16 @@ spec:
description: Set Akamai Cache Bust Image
type: string
akamaiCacheBustURL:
description: |-
Deprecated: Users should move to AkamaiCacheBustURLs
Preserving for backwards compatibility
type: string
akamaiCacheBustURLs:
description: Set Akamai Cache Bust URL that the files will hang off
of
type: string
items:
type: string
type: array
akamaiSecretName:
description: The name of the secret we will use to get the akamai
credentials
Expand Down
66 changes: 50 additions & 16 deletions controllers/reconcile.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,30 +216,64 @@ func makeAkamaiEdgercFileFromSecret(secret *v1.Secret) string {
}

func createCachePurgePathList(frontend *crd.Frontend, frontendEnvironment *crd.FrontendEnvironment) []string {
// Set purgeHost by ensuring the URL begins with https:// and has no trailing /
purgeHost := strings.TrimSuffix(fmt.Sprintf("https://%s", strings.TrimPrefix(frontendEnvironment.Spec.AkamaiCacheBustURL, "https://")), "/")
var purgePaths []string

// Initialize with a default path if AkamaiCacheBustPaths is nil
purgePaths := []string{fmt.Sprintf("%s/apps/%s/fed-mods.json", purgeHost, frontend.Name)}
// Helper function to check if a path is already in the list
contains := func(slice []string, item string) bool {
for _, existing := range slice {
if existing == item {
return true
}
}
return false
}

if frontend.Spec.AkamaiCacheBustPaths == nil {
cacheBustUrls := frontendEnvironment.Spec.AkamaiCacheBustURLs

if frontendEnvironment.Spec.AkamaiCacheBustURL != "" {
cacheBustUrls = append(cacheBustUrls, frontendEnvironment.Spec.AkamaiCacheBustURL)
}

// Return early if we have no cache bust URLs of any kind to process
if len(cacheBustUrls) == 0 {
return purgePaths
}

purgePaths = make([]string, 0, len(frontend.Spec.AkamaiCacheBustPaths))
for _, path := range frontend.Spec.AkamaiCacheBustPaths {
// Check if path is a full URL (starts with "http://" or "https://")
if strings.HasPrefix(path, "http://") || strings.HasPrefix(path, "https://") {
// Add full URL path directly
purgePaths = append(purgePaths, path)
} else {
// Ensure each path has a leading slash but no double slashes
if !strings.HasPrefix(path, "/") {
path = "/" + path
for _, cacheBustURL := range cacheBustUrls {
// Ensure the URL begins with https:// and has no trailing /
purgeHost := strings.TrimSuffix(fmt.Sprintf("https://%s", strings.TrimPrefix(cacheBustURL, "https://")), "/")

// Add default path if AkamaiCacheBustPaths is nil
if frontend.Spec.AkamaiCacheBustPaths == nil {
defaultPath := fmt.Sprintf("%s/apps/%s/fed-mods.json", purgeHost, frontend.Name)
if !contains(purgePaths, defaultPath) {
purgePaths = append(purgePaths, defaultPath)
}
continue
}

// Append paths based on AkamaiCacheBustPaths
for _, path := range frontend.Spec.AkamaiCacheBustPaths {
var fullPath string

if strings.HasPrefix(path, "http://") || strings.HasPrefix(path, "https://") {
// Add full URL path directly
fullPath = path
} else {
// Ensure each path has a leading slash but no double slashes
if !strings.HasPrefix(path, "/") {
path = "/" + path
}
fullPath = purgeHost + path
}

// Append the fullPath only if it doesn't already exist in purgePaths
if !contains(purgePaths, fullPath) {
purgePaths = append(purgePaths, fullPath)
}
purgePaths = append(purgePaths, purgeHost+path)
}
}

return purgePaths
}

Expand Down
9 changes: 8 additions & 1 deletion deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -256,9 +256,16 @@ objects:
description: Set Akamai Cache Bust Image
type: string
akamaiCacheBustURL:
description: 'Deprecated: Users should move to AkamaiCacheBustURLs
Preserving for backwards compatibility'
type: string
akamaiCacheBustURLs:
description: Set Akamai Cache Bust URL that the files will hang
off of
type: string
items:
type: string
type: array
akamaiSecretName:
description: The name of the secret we will use to get the akamai
credentials
Expand Down
4 changes: 3 additions & 1 deletion docs/antora/modules/ROOT/pages/api_reference.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -458,7 +458,9 @@ parts should be generated for the bundles. We want to do +
do this in epehemeral environments but not in production + | |
| *`enableAkamaiCacheBust`* __boolean__ | Enable Akamai Cache Bust + | |
| *`akamaiCacheBustImage`* __string__ | Set Akamai Cache Bust Image + | |
| *`akamaiCacheBustURL`* __string__ | Set Akamai Cache Bust URL that the files will hang off of + | |
| *`akamaiCacheBustURL`* __string__ | Deprecated: Users should move to AkamaiCacheBustURLs +
Preserving for backwards compatibility + | |
| *`akamaiCacheBustURLs`* __string array__ | Set Akamai Cache Bust URL that the files will hang off of + | |
| *`akamaiSecretName`* __string__ | The name of the secret we will use to get the akamai credentials + | |
| *`targetNamespaces`* __string array__ | List of namespaces that should receive a copy of the frontend configuration as a config map +
By configurations we mean the fed-modules.json, navigation files, etc. + | |
Expand Down
21 changes: 21 additions & 0 deletions tests/e2e/cachebust-multiple-urls/00-create-namespace.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
---
apiVersion: v1
kind: Namespace
metadata:
name: test-cachebust-multiple-urls
spec:
finalizers:
- kubernetes
---
kind: Secret
apiVersion: v1
metadata:
name: akamai
namespace: test-cachebust-multiple-urls
data:
access_token: "YWNjZXNzX3Rva2Vu"
client_secret: "Y2xpZW50X3NlY3JldA=="
client_token: "Y2xpZW50X3Rva2Vu"
host: "aG9zdA=="
type: Opaque

83 changes: 83 additions & 0 deletions tests/e2e/cachebust-multiple-urls/01-create-resources.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
---
apiVersion: cloud.redhat.com/v1alpha1
kind: FrontendEnvironment
metadata:
name: test-cachebust-multiple-urls-environment
spec:
generateNavJSON: false
ssl: false
hostname: foo.redhat.com
sso: https://sso.foo.redhat.com
enableAkamaiCacheBust: true
akamaiCacheBustImage: "quay.io/rh_ee_addrew/hi_true_bye:add_alias"
akamaiCacheBustURLs:
- "console.doesntexist.redhat.com"
- "us.console.doesntexist.redhat.com"
---
apiVersion: cloud.redhat.com/v1alpha1
kind: Frontend
metadata:
name: chrome-test-filelist
namespace: test-cachebust-multiple-urls
spec:
API:
versions:
- v1
frontend:
paths:
- /
akamaiCacheBustPaths:
- /config/chrome/fed-modules.json
- apps/chrome/index.html
- https://app.company.com
deploymentRepo: https://github.com/RedHatInsights/insights-chrome
envName: test-cachebust-multiple-urls-environment
image: quay.io/cloudservices/insights-chrome-frontend:720317c
module:
config:
ssoUrl: 'https://'
manifestLocation: /apps/chrome/js/fed-mods.json
title: Chrome
---
apiVersion: cloud.redhat.com/v1alpha1
kind: Frontend
metadata:
name: chrome-test-defaults
namespace: test-cachebust-multiple-urls
spec:
API:
versions:
- v1
frontend:
paths:
- /chrome/defaults
deploymentRepo: https://github.com/RedHatInsights/insights-chrome
envName: test-cachebust-multiple-urls-environment
image: quay.io/cloudservices/insights-chrome-frontend:720317c
module:
config:
ssoUrl: 'https://'
manifestLocation: /apps/chrome/js/fed-mods.json
title: Chrome
---
apiVersion: cloud.redhat.com/v1alpha1
kind: Frontend
metadata:
name: chrome-test-optout
namespace: test-cachebust-multiple-urls
spec:
akamaiCacheBustDisable: true
API:
versions:
- v1
frontend:
paths:
- /chrome2
deploymentRepo: https://github.com/RedHatInsights/insights-chrome
envName: test-cachebust-multiple-urls-environment
image: quay.io/cloudservices/insights-chrome-frontend:720317c
module:
config:
ssoUrl: 'https://'
manifestLocation: /apps/chrome/js/fed-mods.json
title: Chrome
Loading

0 comments on commit b364d4f

Please sign in to comment.