Skip to content

Commit

Permalink
Add new frontend custom config
Browse files Browse the repository at this point in the history
  • Loading branch information
psav committed Sep 5, 2022
1 parent 0928c45 commit 0c9f7d8
Show file tree
Hide file tree
Showing 7 changed files with 152 additions and 54 deletions.
29 changes: 16 additions & 13 deletions api/v1alpha1/frontend_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"fmt"

errors "github.com/RedHatInsights/clowder/controllers/cloud.redhat.com/errors"
apiextensions "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
Expand All @@ -37,16 +38,17 @@ type FrontendInfo struct {

// FrontendSpec defines the desired state of Frontend
type FrontendSpec struct {
EnvName string `json:"envName" yaml:"envName"`
Title string `json:"title" yaml:"title"`
DeploymentRepo string `json:"deploymentRepo" yaml:"deploymentRepo"`
API ApiInfo `json:"API" yaml:"API"`
Frontend FrontendInfo `json:"frontend" yaml:"frontend"`
Image string `json:"image,omitempty" yaml:"image,omitempty"`
Service string `json:"service,omitempty" yaml:"service,omitempty"`
Module *FedModule `json:"module,omitempty" yaml:"module,omitempty"`
NavItems []*BundleNavItem `json:"navItems,omitempty" yaml:"navItems,omitempty"`
AssetsPrefix string `json:"assetsPrefix,omitempty" yaml:"assetsPrefix,omitempty"`
EnvName string `json:"envName" yaml:"envName"`
Title string `json:"title" yaml:"title"`
DeploymentRepo string `json:"deploymentRepo" yaml:"deploymentRepo"`
API ApiInfo `json:"API" yaml:"API"`
Frontend FrontendInfo `json:"frontend" yaml:"frontend"`
Image string `json:"image,omitempty" yaml:"image,omitempty"`
Service string `json:"service,omitempty" yaml:"service,omitempty"`
Module *FedModule `json:"module,omitempty" yaml:"module,omitempty"`
NavItems []*BundleNavItem `json:"navItems,omitempty" yaml:"navItems,omitempty"`
AssetsPrefix string `json:"assetsPrefix,omitempty" yaml:"assetsPrefix,omitempty"`
CustomConfig *apiextensions.JSON `json:"customConfig,omitempty" yaml:"customConfig,omitempty"`
}

var ReconciliationSuccessful clusterv1.ConditionType = "ReconciliationSuccessful"
Expand All @@ -66,9 +68,10 @@ type FrontendDeployments struct {
}

type FedModule struct {
ManifestLocation string `json:"manifestLocation" yaml:"manifestLocation"`
Modules []Module `json:"modules,omitempty" yaml:"modules,omitempty"`
ModuleID string `json:"moduleID,omitempty" yaml:"moduleID,omitempty"`
ManifestLocation string `json:"manifestLocation" yaml:"manifestLocation"`
Modules []Module `json:"modules,omitempty" yaml:"modules,omitempty"`
ModuleID string `json:"moduleID,omitempty" yaml:"moduleID,omitempty"`
Config *apiextensions.JSON `json:"config,omitempty" yaml:"config,omitempty"`
}

type Module struct {
Expand Down
11 changes: 11 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.

4 changes: 4 additions & 0 deletions config/crd/bases/cloud.redhat.com_frontends.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ spec:
type: object
assetsPrefix:
type: string
customConfig:
x-kubernetes-preserve-unknown-fields: true
deploymentRepo:
type: string
envName:
Expand All @@ -78,6 +80,8 @@ spec:
type: string
module:
properties:
config:
x-kubernetes-preserve-unknown-fields: true
manifestLocation:
type: string
moduleID:
Expand Down
62 changes: 55 additions & 7 deletions controllers/frontend_controller_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,20 @@ import (
apps "k8s.io/api/apps/v1"
v1 "k8s.io/api/core/v1"
networking "k8s.io/api/networking/v1"
apiextensions "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
)

var _ = Describe("Frontend controller with image", func() {
const (
FrontendName = "test-frontend"
FrontendNamespace = "default"
FrontendEnvName = "test-env"
BundleName = "test-bundle"
FrontendName = "test-frontend"
FrontendNamespace = "default"
FrontendEnvName = "test-env"
FrontendName2 = "test-frontend2"
FrontendNamespace2 = "default"
FrontendEnvName2 = "test-env"
BundleName = "test-bundle"

timeout = time.Second * 10
duration = time.Second * 10
Expand All @@ -32,6 +36,9 @@ var _ = Describe("Frontend controller with image", func() {
By("By creating a new Frontend")
ctx := context.Background()

var customConfig apiextensions.JSON
customConfig.UnmarshalJSON([]byte(`{"apple": "pie"}`))

frontend := &crd.Frontend{
TypeMeta: metav1.TypeMeta{
APIVersion: "cloud.redhat.com/v1",
Expand Down Expand Up @@ -67,10 +74,51 @@ var _ = Describe("Frontend controller with image", func() {
}},
}},
},
CustomConfig: &customConfig,
},
}
Expect(k8sClient.Create(ctx, frontend)).Should(Succeed())

frontend2 := &crd.Frontend{
TypeMeta: metav1.TypeMeta{
APIVersion: "cloud.redhat.com/v1",
Kind: "Frontend",
},
ObjectMeta: metav1.ObjectMeta{
Name: FrontendName2,
Namespace: FrontendNamespace,
},
Spec: crd.FrontendSpec{
EnvName: FrontendEnvName,
Title: "",
DeploymentRepo: "",
API: crd.ApiInfo{
Versions: []string{"v1"},
},
Frontend: crd.FrontendInfo{
Paths: []string{"/things/test"},
},
Image: "my-image:version",
NavItems: []*crd.BundleNavItem{{
Title: "Test",
GroupID: "",
Href: "/test/href",
}},
Module: &crd.FedModule{
ManifestLocation: "/apps/inventory/fed-mods.json",
Modules: []crd.Module{{
Id: "test",
Module: "./RootApp",
Routes: []crd.Route{{
Pathname: "/test/href",
}},
}},
},
CustomConfig: &customConfig,
},
}
Expect(k8sClient.Create(ctx, frontend2)).Should(Succeed())

frontendEnvironment := &crd.FrontendEnvironment{
TypeMeta: metav1.TypeMeta{
APIVersion: "cloud.redhat.com/v1",
Expand Down Expand Up @@ -99,7 +147,7 @@ var _ = Describe("Frontend controller with image", func() {
Spec: crd.BundleSpec{
ID: BundleName,
Title: "",
AppList: []string{FrontendName},
AppList: []string{FrontendName, FrontendName2},
EnvName: FrontendEnvName,
},
}
Expand Down Expand Up @@ -149,8 +197,8 @@ var _ = Describe("Frontend controller with image", func() {
}, timeout, interval).Should(BeTrue())
Expect(createdConfigMap.Name).Should(Equal(FrontendEnvName))
Expect(createdConfigMap.Data).Should(Equal(map[string]string{
"fed-modules.json": "{\"testFrontend\":{\"manifestLocation\":\"/apps/inventory/fed-mods.json\",\"modules\":[{\"id\":\"test\",\"module\":\"./RootApp\",\"routes\":[{\"pathname\":\"/test/href\"}]}]}}",
"test-env.json": "{\"id\":\"test-bundle\",\"title\":\"\",\"navItems\":[{\"title\":\"Test\",\"href\":\"/test/href\"}]}",
"fed-modules.json": "{\"testFrontend\":{\"manifestLocation\":\"/apps/inventory/fed-mods.json\",\"modules\":[{\"id\":\"test\",\"module\":\"./RootApp\",\"routes\":[{\"pathname\":\"/test/href\"}]}],\"config\":{\"apple\":\"pie\"}},\"testFrontend2\":{\"manifestLocation\":\"/apps/inventory/fed-mods.json\",\"modules\":[{\"id\":\"test\",\"module\":\"./RootApp\",\"routes\":[{\"pathname\":\"/test/href\"}]}],\"config\":{\"apple\":\"pie\"}}}",
"test-env.json": "{\"id\":\"test-bundle\",\"title\":\"\",\"navItems\":[{\"title\":\"Test\",\"href\":\"/test/href\"},{\"title\":\"Test\",\"href\":\"/test/href\"}]}",
}))
Expect(createdConfigMap.ObjectMeta.OwnerReferences[0].Name).Should(Equal(FrontendEnvName))
createdSSOConfigMap := &v1.ConfigMap{}
Expand Down
62 changes: 39 additions & 23 deletions controllers/reconcile.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,28 +50,7 @@ func runReconciliation(context context.Context, pClient client.Client, frontend
return nil
}

func createFrontendDeployment(context context.Context, pClient client.Client, frontend *crd.Frontend, frontendEnvironment *crd.FrontendEnvironment, hash string, ssoHash string, cache *resCache.ObjectCache) error {
sso := frontendEnvironment.Spec.SSO

// Create new empty struct
d := &apps.Deployment{}

// Define name of resource
nn := types.NamespacedName{
Name: frontend.Name,
Namespace: frontend.Namespace,
}

// Create object in cache (will populate cache if exists)
if err := cache.Create(CoreDeployment, nn, d); err != nil {
return err
}

// Label with the right labels
labels := frontend.GetLabels()

labeler := utils.GetCustomLabeler(labels, nn, frontend)
labeler(d)
func populateContainer(d *apps.Deployment, frontend *crd.Frontend, frontendEnvironment *crd.FrontendEnvironment) {
d.SetOwnerReferences([]metav1.OwnerReference{frontend.MakeOwnerReference()})

// Modify the obejct to set the things we care about
Expand Down Expand Up @@ -103,10 +82,13 @@ func createFrontendDeployment(context context.Context, pClient client.Client, fr
},
Env: []v1.EnvVar{{
Name: "SSO_URL",
Value: sso,
Value: frontendEnvironment.Spec.SSO,
}},
}}

}

func populateVolumes(d *apps.Deployment, frontend *crd.Frontend) {
d.Spec.Template.Spec.Volumes = []v1.Volume{
{
Name: "config",
Expand All @@ -129,6 +111,32 @@ func createFrontendDeployment(context context.Context, pClient client.Client, fr
},
},
}
}

func createFrontendDeployment(context context.Context, pClient client.Client, frontend *crd.Frontend, frontendEnvironment *crd.FrontendEnvironment, hash string, ssoHash string, cache *resCache.ObjectCache) error {

// Create new empty struct
d := &apps.Deployment{}

// Define name of resource
nn := types.NamespacedName{
Name: frontend.Name,
Namespace: frontend.Namespace,
}

// Create object in cache (will populate cache if exists)
if err := cache.Create(CoreDeployment, nn, d); err != nil {
return err
}

// Label with the right labels
labels := frontend.GetLabels()

labeler := utils.GetCustomLabeler(labels, nn, frontend)
labeler(d)

populateContainer(d, frontend, frontendEnvironment)
populateVolumes(d, frontend)

d.Spec.Template.ObjectMeta.Labels = labels

Expand Down Expand Up @@ -477,6 +485,14 @@ func createConfigConfigMap(ctx context.Context, pClient client.Client, frontend
modName = frontend.Spec.Module.ModuleID
}
fedModules[modName] = *frontend.Spec.Module
if frontend.Spec.CustomConfig != nil {
module := fedModules[modName]
fmt.Printf("--%v\n", frontend.Spec.CustomConfig)
fmt.Printf("--%v\n", fedModules[modName].Config)
fmt.Printf("--%v\n", fedModules[modName])
module.Config = frontend.Spec.CustomConfig
fedModules[modName] = module
}
}
}

Expand Down
4 changes: 4 additions & 0 deletions deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -450,6 +450,8 @@ objects:
type: object
assetsPrefix:
type: string
customConfig:
x-kubernetes-preserve-unknown-fields: true
deploymentRepo:
type: string
envName:
Expand All @@ -467,6 +469,8 @@ objects:
type: string
module:
properties:
config:
x-kubernetes-preserve-unknown-fields: true
manifestLocation:
type: string
moduleID:
Expand Down
Loading

0 comments on commit 0c9f7d8

Please sign in to comment.