Skip to content

Commit

Permalink
Merge pull request #90 from RedHatInsights/conditionally-build-bundles
Browse files Browse the repository at this point in the history
[RHCLOUD-23633] - Conditionally build Chrome config
  • Loading branch information
adamrdrew authored Jan 24, 2023
2 parents 399ce64 + 94bda08 commit 91eed18
Show file tree
Hide file tree
Showing 8 changed files with 77 additions and 35 deletions.
5 changes: 5 additions & 0 deletions api/v1alpha1/frontendenvironment_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ type FrontendEnvironmentSpec struct {
// local will add it to the frontend's namespace
// app-interface will add it to "openshift-customer-monitoring"
Monitoring *MonitoringConfig `json:"monitoring,omitempty"`

// GenerateChromeConfig determines if a chrome configmap will be generated
// If empty or false the chrome nav config in the chrome container will be used
// If true a configmap will be generated and mounted into the chrome container
GenerateChromeConfig bool `json:"generateChromeConfig,omitempty"`
}

type MonitoringConfig struct {
Expand Down
6 changes: 6 additions & 0 deletions config/crd/bases/cloud.redhat.com_frontendenvironments.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,12 @@ spec:
spec:
description: FrontendEnvironmentSpec defines the desired state of FrontendEnvironment
properties:
generateChromeConfig:
description: GenerateChromeConfig determines if a chrome configmap
will be generated If empty or false the chrome nav config in the
chrome container will be used If true a configmap will be generated
and mounted into the chrome container
type: boolean
hostname:
description: Hostname
type: string
Expand Down
5 changes: 5 additions & 0 deletions controllers/frontend_controller_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ var _ = Describe("Frontend controller with image", func() {
Monitoring: &crd.MonitoringConfig{
Mode: "app-interface",
},
GenerateChromeConfig: true,
},
}
Expect(k8sClient.Create(ctx, frontendEnvironment)).Should(Succeed())
Expand Down Expand Up @@ -255,6 +256,7 @@ var _ = Describe("Frontend controller with service", func() {
Monitoring: &crd.MonitoringConfig{
Mode: "local",
},
GenerateChromeConfig: true,
},
}
Expect(k8sClient.Create(ctx, &frontendEnvironment)).Should(Succeed())
Expand Down Expand Up @@ -530,6 +532,7 @@ var _ = Describe("Frontend controller with chrome", func() {
Monitoring: &crd.MonitoringConfig{
Mode: "app-interface",
},
GenerateChromeConfig: true,
},
}
Expect(k8sClient.Create(ctx, frontendEnvironment)).Should(Succeed())
Expand Down Expand Up @@ -678,6 +681,7 @@ var _ = Describe("ServiceMonitor Creation", func() {
Monitoring: &crd.MonitoringConfig{
Mode: "app-interface",
},
GenerateChromeConfig: true,
},
}
Expect(k8sClient.Create(ctx, frontendEnvironment)).Should(Succeed())
Expand Down Expand Up @@ -881,6 +885,7 @@ var _ = Describe("Dependencies", func() {
Monitoring: &crd.MonitoringConfig{
Mode: "app-interface",
},
GenerateChromeConfig: true,
},
}
Expect(k8sClient.Create(ctx, frontendEnvironment)).Should(Succeed())
Expand Down
86 changes: 52 additions & 34 deletions controllers/reconcile.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,18 +41,24 @@ type FrontendReconciliation struct {
}

func (r *FrontendReconciliation) run() error {
hash, err := r.setupConfigMap()
if err != nil {
return err
var annotationHashes []map[string]string

if r.FrontendEnvironment.Spec.GenerateChromeConfig {
configHash, err := r.setupConfigMap()
if err != nil {
return err
}
annotationHashes = append(annotationHashes, map[string]string{"configHash": configHash})
}

ssoHash, err := r.createSSOConfigMap()
if err != nil {
return err
}
annotationHashes = append(annotationHashes, map[string]string{"ssoHash": ssoHash})

if r.Frontend.Spec.Image != "" {
if err := r.createFrontendDeployment(hash, ssoHash); err != nil {
if err := r.createFrontendDeployment(annotationHashes); err != nil {
return err
}
if err := r.createFrontendService(); err != nil {
Expand All @@ -72,6 +78,25 @@ func (r *FrontendReconciliation) run() error {
return nil
}

func populateContainerVolumeMounts(frontendEnvironment *crd.FrontendEnvironment) []v1.VolumeMount {
volumeMounts := []v1.VolumeMount{
{
Name: "sso",
MountPath: "/opt/app-root/src/build/js/sso-url.js",
SubPath: "sso-url.js",
},
}

if frontendEnvironment.Spec.GenerateChromeConfig {
volumeMounts = append(volumeMounts, v1.VolumeMount{
Name: "config",
MountPath: "/opt/app-root/src/build/chrome",
})
}

return volumeMounts
}

func populateContainer(d *apps.Deployment, frontend *crd.Frontend, frontendEnvironment *crd.FrontendEnvironment) {
d.SetOwnerReferences([]metav1.OwnerReference{frontend.MakeOwnerReference()})

Expand All @@ -91,17 +116,7 @@ func populateContainer(d *apps.Deployment, frontend *crd.Frontend, frontendEnvir
Protocol: "TCP",
},
},
VolumeMounts: []v1.VolumeMount{
{
Name: "config",
MountPath: "/opt/app-root/src/build/chrome",
},
{
Name: "sso",
MountPath: "/opt/app-root/src/build/js/sso-url.js",
SubPath: "sso-url.js",
},
},
VolumeMounts: populateContainerVolumeMounts(frontendEnvironment),
Env: []v1.EnvVar{{
Name: "SSO_URL",
Value: frontendEnvironment.Spec.SSO,
Expand All @@ -112,32 +127,41 @@ func populateContainer(d *apps.Deployment, frontend *crd.Frontend, frontendEnvir
}
}

func populateVolumes(d *apps.Deployment, frontend *crd.Frontend) {
d.Spec.Template.Spec.Volumes = []v1.Volume{
func populateVolumes(d *apps.Deployment, frontend *crd.Frontend, frontendEnvironment *crd.FrontendEnvironment) {
// By default we just want the SSO volume
volumes := []v1.Volume{
{
Name: "config",
Name: "sso",
VolumeSource: v1.VolumeSource{
ConfigMap: &v1.ConfigMapVolumeSource{
LocalObjectReference: v1.LocalObjectReference{
Name: frontend.Spec.EnvName,
Name: fmt.Sprintf("%s-sso", frontend.Spec.EnvName),
},
},
},
},
{
Name: "sso",
}

// If we are generating the chrome config, add it to the volumes
if frontendEnvironment.Spec.GenerateChromeConfig {
config := v1.Volume{
Name: "config",
VolumeSource: v1.VolumeSource{
ConfigMap: &v1.ConfigMapVolumeSource{
LocalObjectReference: v1.LocalObjectReference{
Name: fmt.Sprintf("%s-sso", frontend.Spec.EnvName),
Name: frontend.Spec.EnvName,
},
},
},
},
}
volumes = append(volumes, config)
}

// Set the volumes on the deployment
d.Spec.Template.Spec.Volumes = volumes
}

func (r *FrontendReconciliation) createFrontendDeployment(hash, ssoHash string) error {
func (r *FrontendReconciliation) createFrontendDeployment(annotationHashes []map[string]string) error {

// Create new empty struct
d := &apps.Deployment{}
Expand All @@ -162,27 +186,21 @@ func (r *FrontendReconciliation) createFrontendDeployment(hash, ssoHash string)
labeler(d)

populateContainer(d, r.Frontend, r.FrontendEnvironment)
populateVolumes(d, r.Frontend)
populateVolumes(d, r.Frontend, r.FrontendEnvironment)

d.Spec.Template.ObjectMeta.Labels = labels

d.Spec.Selector = &metav1.LabelSelector{MatchLabels: labels}

annotations := d.Spec.Template.GetAnnotations()
if annotations == nil {
annotations = make(map[string]string)
}

annotations["configHash"] = hash
annotations["ssoHash"] = ssoHash

d.Spec.Template.SetAnnotations(annotations)
utils.UpdateAnnotations(&d.Spec.Template, annotationHashes...)

// This is a temporary measure to silence DVO from opening 600 million tickets for each frontend - Issue fix ETA is TBD
deploymentAnnotation := d.ObjectMeta.GetAnnotations()
if deploymentAnnotation == nil {
deploymentAnnotation = make(map[string]string)
}

// Gabor wrote the string "we don't need no any checking" and we will never change it
deploymentAnnotation["kube-linter.io/ignore-all"] = "we don't need no any checking"
d.ObjectMeta.SetAnnotations(deploymentAnnotation)

Expand Down
6 changes: 6 additions & 0 deletions deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,12 @@ objects:
spec:
description: FrontendEnvironmentSpec defines the desired state of FrontendEnvironment
properties:
generateChromeConfig:
description: GenerateChromeConfig determines if a chrome configmap
will be generated If empty or false the chrome nav config in the
chrome container will be used If true a configmap will be generated
and mounted into the chrome container
type: boolean
hostname:
description: Hostname
type: string
Expand Down
1 change: 1 addition & 0 deletions docs/antora/modules/ROOT/pages/api_reference.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,7 @@ FrontendEnvironmentSpec defines the desired state of FrontendEnvironment
| *`hostname`* __string__ | Hostname
| *`whitelist`* __string array__ | Whitelist CIDRs
| *`monitoring`* __xref:{anchor_prefix}-github-com-redhatinsights-frontend-operator-api-v1alpha1-monitoringconfig[$$MonitoringConfig$$]__ | MonitorMode determines where a ServiceMonitor object will be placed local will add it to the frontend's namespace app-interface will add it to "openshift-customer-monitoring"
| *`generateChromeConfig`* __boolean__ | GenerateChromeConfig determines if a chrome configmap will be generated If empty or false the chrome nav config in the chrome container will be used If true a configmap will be generated and mounted into the chrome container
|===


Expand Down
2 changes: 1 addition & 1 deletion examples/chrome.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@ spec:
paths:
- /
- /config/chrome
image: quay.io/cloudservices/insights-chrome-frontend:1ddc0de
image: quay.io/cloudservices/insights-chrome-frontend:2712b0a
1 change: 1 addition & 0 deletions examples/feenvironment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ spec:
monitoring:
mode: "local"
disabled: false
generateChromeConfig: true

0 comments on commit 91eed18

Please sign in to comment.