Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[TT-11468] Allow bootstrapping hybrid organisation while bootstrapping tyk dashb… #30

Merged
merged 5 commits into from
Mar 11, 2024
Merged
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
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ setting up an organization and an admin user. Additionally, it generates Kuberne
| TYK_K8SBOOTSTRAP_TYK_ORG_NAME | corresponds to the name for your organization that is going to be bootstrapped in Tyk. |
| TYK_K8SBOOTSTRAP_TYK_ORG_CNAME | corresponds to the Organisation CNAME which is going to bind the Portal to. |
| TYK_K8SBOOTSTRAP_TYK_ORG_ID | corresponds to the organisation ID that is being created. |
| TYK_K8SBOOTSTRAP_TYK_ORG_HYBRID_ENABLED | specifies if the Hybrid organisation for MDCB Control Plane is enabled or not |
| TYK_K8SBOOTSTRAP_TYK_ORG_HYBRID_KEYEVENT | corresponds to `key_event` of the event options (optional). |
| TYK_K8SBOOTSTRAP_TYK_ORG_HYBRID_HASHEDKEYEVENT | corresponds to `hashed_key_event` of the event options (optional). |
| TYK_K8SBOOTSTRAP_TYK_DASHBOARDLICENSE | corresponds to the license key of Tyk Dashboard. |

## Bootstrapped Environments
Expand Down
16 changes: 16 additions & 0 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package config

import (
"tyk/tyk/bootstrap/tyk/api"

"github.com/kelseyhightower/envconfig"
)

Expand Down Expand Up @@ -74,6 +76,9 @@ type TykOrg struct {

// ID corresponds to the organisation ID that is being created.
ID string

// Hybrid includes details of hybrid organisation while using MDCB Control Plane
Hybrid *HybridConf
}

type TykConf struct {
Expand All @@ -86,6 +91,17 @@ type TykConf struct {
DashboardLicense string
}

type HybridConf struct {
// Enabled specified if the Hybrid organisation is enabled or not
Enabled bool
// KeyEvent corresponds to `key_event` of the event options which enables key events such as updates and deletes,
// to be propagated to the various instance zones.
KeyEvent *api.EventConfig
// HashedKeyEvent corresponds to `hashed_key_event` of the event options which enables key events such as updates
// and deletes, to be propagated to the various instance zones.
HashedKeyEvent *api.EventConfig `json:",omitempty"`
}

func NewConfig() (*Config, error) {
conf := &Config{}
if err := envconfig.Process(prefix, conf); err != nil {
Expand Down
14 changes: 11 additions & 3 deletions tyk/api/request.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
package api

type EventConfig struct {
Webhook string `json:"webhook,omitempty"`
Email string `json:"email,omitempty"`
Redis bool `json:"redis,omitempty"`
}

type CreateOrgReq struct {
OwnerName string `json:"owner_name"`
CnameEnabled bool `json:"cname_enabled"`
Cname string `json:"cname"`
OwnerName string `json:"owner_name"`
CnameEnabled bool `json:"cname_enabled"`
Cname string `json:"cname"`
HybridEnabled bool `json:"hybrid_enabled"`
EventOptions map[string]EventConfig `json:"event_options,omitempty"`
}

type ResetPasswordReq struct {
Expand Down
45 changes: 45 additions & 0 deletions tyk/organisation.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"io"
"net/http"
"tyk/tyk/bootstrap/pkg/config"
"tyk/tyk/bootstrap/tyk/api"
"tyk/tyk/bootstrap/tyk/internal/constants"

Expand Down Expand Up @@ -79,6 +80,13 @@ func (s *Service) CreateOrganisation() error {
Cname: s.appArgs.Tyk.Org.Cname,
}

// Enable hybrid in the organisation while setting up the MDCB Control Plane.
// For reference: https://tyk.io/docs/tyk-multi-data-centre/setup-controller-data-centre/
if s.appArgs.Tyk.Org.Hybrid != nil && s.appArgs.Tyk.Org.Hybrid.Enabled {
createOrgData.HybridEnabled = true
createOrgData.EventOptions = eventOptions(s.appArgs.Tyk.Org.Hybrid, s.appArgs.Tyk.Admin.EmailAddress)
}

reqBodyBytes, err := json.Marshal(createOrgData)
if err != nil {
return err
Expand Down Expand Up @@ -121,3 +129,40 @@ func (s *Service) CreateOrganisation() error {

return nil
}

func eventOptions(hconf *config.HybridConf, defaultEmail string) map[string]api.EventConfig {
if hconf == nil {
return nil
}

m := make(map[string]api.EventConfig)

const (
hashedKeyEventKey = "hashed_key_event"
keyEventKey = "key_event"
)

hashedKeyEventConf := api.EventConfig{
Email: defaultEmail,
Webhook: hconf.HashedKeyEvent.Webhook,
Redis: hconf.HashedKeyEvent.Redis,
}
keyEventConf := hashedKeyEventConf

if hconf.HashedKeyEvent != nil {
if hconf.HashedKeyEvent.Email != "" {
hashedKeyEventConf.Email = hconf.HashedKeyEvent.Email
}
}

if hconf.KeyEvent != nil {
if hconf.KeyEvent.Email != "" {
keyEventConf.Email = hconf.KeyEvent.Email
}
}

m[hashedKeyEventKey] = hashedKeyEventConf
m[keyEventKey] = keyEventConf

return m
}
Loading