Skip to content

Commit

Permalink
Merge pull request #719 from Hyperkid123/consume-config-map
Browse files Browse the repository at this point in the history
Enable consuming generating fed-modules.json from FEO env variables
  • Loading branch information
Hyperkid123 authored Nov 14, 2024
2 parents e9c79b6 + dde1020 commit 8c70b73
Show file tree
Hide file tree
Showing 8 changed files with 150 additions and 3 deletions.
6 changes: 6 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,9 @@ KAFKA_JMX_PORT=9999
KAFKA_CLIENT_LISTEN_PORT=9092
KAFKA_CLIENT_LISTEN_PORT_2=29092

# Values to generate chrome UI configuration
FEO_SEARCH_INDEX=[{"id":"landing-env-boot-landing","href":"/","title":"Landing","description":"Landing page description","alt_title":["HCC Home page","Home"]},{"id":"landing-env-boot-landing-widgets","href":"/widgets","title":"Widget fantastic","description":"Widget"}]
FEO_FED_MODULES={"landing":{"manifestLocation":"/apps/landing/fed-mods.json","modules":[{"id":"landing","module":"./RootApp","routes":[{"pathname":"/","exact":true}]}],"fullProfile":false}}
FEO_SERVICE_TILES=[{"id":"automation","title":"Automation","groups":[{"id":"ansible","title":"Ansible","tiles":[{"section":"automation","group":"ansible","id":"ansible-link","href":"/ansible/foo","title":"Ansible FOO","description":"Ansible FOO description thing","icon":"AnsibleIcon"}]},{"id":"rhel","title":"Red Hat Enterprise Linux","tiles":[]}]},{"id":"iam","title":"Identity and Access Management","groups":[{"id":"iam","title":"IAM","tiles":[{"section":"iam","group":"iam","id":"iam-link","href":"/iam","title":"IAM FOO","description":"Some Iam thing","icon":"IAMIcon"}]}]}]
FEO_WIDGET_REGISTRY=[{"scope":"landing","module":"./RandomWidget","config":{"icon":"CogIcon","title":"Random Widget","headerLink":{"title":"","href":""}},"defaults":{"sm":{"w":1,"h":1,"maxH":1,"minH":1},"md":{"w":1,"h":1,"maxH":1,"minH":1},"lg":{"w":1,"h":1,"maxH":1,"minH":1},"xl":{"w":1,"h":1,"maxH":1,"minH":1}}}]

1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ static/**/links-storage.json
static/**/services-generated.json
*-services.db
static/**/search-index.json
static/*-generated.json

.tekton/chrome-service-pull-request.bak.yaml
.tekton/chrome-service-push.bak.yaml
3 changes: 3 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ FROM registry.access.redhat.com/ubi9-minimal:latest
RUN mkdir -p /app
RUN chgrp -R 0 /app && \
chmod -R g=u /app
RUN mkdir -p /static
RUN chgrp -R 0 /static && \
chmod -R g=u /static
COPY --from=builder /go/bin/chrome-service-backend /app/chrome-service-backend
COPY --from=builder /go/bin/chrome-migrate /usr/bin
COPY --from=builder /go/bin/chrome-search-index /usr/bin
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ env:
cp .env.example .env

migrate:
go run cmd/migrate/migrate.go
go run cmd/migrate/migrate.go

database:
podman-compose up
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,4 @@ eyJpZGVudGl0eSI6eyJ1c2VyIjp7InVzZXJfaWQiOiIxMiJ9fX0=
`make generate-search-index` will generate search index file
`make parse-services` will generate the `services-generated.json` file
`make parse-services` will generate the `services-generated.json` file
33 changes: 32 additions & 1 deletion deploy/clowdapp.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ objects:
- bash
- -c
- chrome-search-index
inheritEnv: true
livenessProbe:
failureThreshold: 3
httpGet:
Expand All @@ -63,6 +62,31 @@ objects:
successThreshold: 1
timeoutSeconds: 120
env:
- name: FEO_SEARCH_INDEX
valueFrom:
configMapKeyRef:
key: search-index.json
name: ${CHROME_CONFIG_MAP}
# because keys in configmap can be empty
optional: true
- name: FEO_FED_MODULES
valueFrom:
configMapKeyRef:
key: fed-modules.json
name: ${CHROME_CONFIG_MAP}
optional: true
- name: FEO_SERVICE_TILES
valueFrom:
configMapKeyRef:
key: service-tiles.json
name: ${CHROME_CONFIG_MAP}
optional: true
- name: FEO_WIDGET_REGISTRY
valueFrom:
configMapKeyRef:
key: widget-registry.json
name: ${CHROME_CONFIG_MAP}
optional: true
- name: CLOWDER_ENABLED
value: ${CLOWDER_ENABLED}
- name: LOG_LEVEL
Expand Down Expand Up @@ -188,5 +212,12 @@ parameters:
- description: ClowdEnv Name
name: ENV_NAME
required: true
- description: Chrome config map name
name: CHROME_CONFIG_MAP
value: feo-context-cfg
- name: LINT_ANNOTATION
value: 'ignore-check.kube-linter.io/minimum-three-replicas'
- description: Frontend environment
name: FRONTEND_ENVIRONMENT
# fallback to stage environment if not set
value: stage
1 change: 1 addition & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ func init() {
flag.Parse()
database.Init()
util.InitUserIdentitiesCache()
util.CreateChromeConfiguration()
}

func main() {
Expand Down
105 changes: 105 additions & 0 deletions rest/util/createChromeConfiguration.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
package util

import (
"encoding/json"
"fmt"
"os"
"path/filepath"

"github.com/joho/godotenv"
"github.com/sirupsen/logrus"
)

const (
fedModulesPath = "static/fed-modules-generated.json"
staticFedModulesPath = "static/stable/%s/modules/fed-modules.json"
)

func getLegacyConfigFile(path string, env string) ([]byte, error) {
// read the file
file, err := os.ReadFile(fmt.Sprintf(path, env))
if err != nil {
return nil, err
}
return file, nil
}

func parseFedModules(fedModulesConfig string, env string) ([]byte, error) {

fm := make(map[string]interface{})
lfm := make(map[string]interface{})
if fedModulesConfig == "" {
logrus.Warn("FEO_FED_MODULES is not set, using empty configuration")
} else {
err := json.Unmarshal([]byte(fedModulesConfig), &fm)
// do the thing
if err != nil {
return nil, err
}
}

// read legacy configFile
legacyFedModulesFile, err := getLegacyConfigFile(staticFedModulesPath, env)
if err != nil {
return nil, err
}

err = json.Unmarshal(legacyFedModulesFile, &lfm)
if err != nil {
return nil, err
}

for key, value := range fm {
// We will have to wrap this assignment to some kind of if statement to make sure we don't overwrite the values if the frontend resource is not ready to be used
// merge legacy and generated values
lfm[key] = value
}

// parse back to string so it can be written to a file
res, err := json.MarshalIndent(lfm, "", " ")
return res, err
}

func writeConfigFile(config []byte, path string) error {
cwd, err := filepath.Abs(".")
if err != nil {
return err
}
file := fmt.Sprintf("%s/%s", cwd, path)
logrus.Infof("Writing configuration to %s", file)
err = os.WriteFile(file, config, 0644)
return err
}

func CreateChromeConfiguration() {
err := LoadEnv()
if err != nil {
godotenv.Load()
}

// environment type
// Can be one if prod, stage, itless, anything else will cause exception
env := os.Getenv("FRONTEND_ENVIRONMENT")
if env == "" {
logrus.Warn("FRONTEND_ENVIRONMENT is not set, using 'stage'")
env = "stage"
} else if env != "prod" && env != "stage" && env != "itless" {
panic(fmt.Sprintf("Invalid FRONTEND_ENVIRONMENT value: %s", env))
}

fedModulesVar := os.Getenv("FEO_FED_MODULES")
// searchVar := os.Getenv("FEO_SEARCH_INDEX")
// serviceTilesVar := os.Getenv("FEO_SERVICE_TILES")
// widgetRegistryVar := os.Getenv("FEO_WIDGET_REGISTRY")

fedModules, err := parseFedModules(fedModulesVar, env)
if err != nil {
panic(fmt.Sprintf("Error parsing FEO_FED_MODULES: %v", err))
}

fmt.Println("Writing fed-modules.json")
err = writeConfigFile(fedModules, fedModulesPath)
if err != nil {
panic(err)
}
}

0 comments on commit 8c70b73

Please sign in to comment.