From fd03ae5587a2d2a86ff7f456d6854315e44c96d7 Mon Sep 17 00:00:00 2001 From: Burak Sekili Date: Wed, 6 Mar 2024 11:40:58 +0300 Subject: [PATCH 1/5] Allow bootstrapping hybrid organisation while bootstrapping tyk dashboard Signed-off-by: Burak Sekili --- README.md | 3 +++ pkg/config/config.go | 15 +++++++++++++++ tyk/api/request.go | 14 +++++++++++--- tyk/organisation.go | 31 +++++++++++++++++++++++++++++++ 4 files changed, 60 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 52b1de0..0ea7d5a 100644 --- a/README.md +++ b/README.md @@ -38,6 +38,9 @@ setting up an organization and an admin user. Additionally, it generates Kuberne | 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_DASHBOARDLICENSE | corresponds to the license key of Tyk Dashboard. | +| TYK_K8SBOOTSTRAP_TYK_HYBRID_ENABLED | specifies if the Hybrid organisation for MDCB Control Plane is enabled or not | +| TYK_K8SBOOTSTRAP_TYK_HYBRID_KEYEVENT | corresponds to `key_event` of the event options (optional). | +| TYK_K8SBOOTSTRAP_TYK_HYBRID_HASHEDKEYEVENT | corresponds to `hashed_key_event` of the event options (optional). | ## Bootstrapped Environments diff --git a/pkg/config/config.go b/pkg/config/config.go index 6b960b1..ca1103d 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -2,6 +2,7 @@ package config import ( "github.com/kelseyhightower/envconfig" + "tyk/tyk/bootstrap/tyk/api" ) const prefix = "TYK_K8SBOOTSTRAP" @@ -84,6 +85,20 @@ type TykConf struct { // DashboardLicense corresponds to the license key of Tyk Dashboard. DashboardLicense string + + // Hybrid includes details of hybrid organisation while using MDCB Control Plane + Hybrid HybridConf +} + +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 } func NewConfig() (*Config, error) { diff --git a/tyk/api/request.go b/tyk/api/request.go index a3a8167..83ecbc0 100644 --- a/tyk/api/request.go +++ b/tyk/api/request.go @@ -1,9 +1,17 @@ package api +type EventConfig struct { + Webhook string `json:"webhook"` + Email string `json:"email"` + Redis bool `json:"redis"` +} + 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"` } type ResetPasswordReq struct { diff --git a/tyk/organisation.go b/tyk/organisation.go index f0be5d0..927e934 100644 --- a/tyk/organisation.go +++ b/tyk/organisation.go @@ -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" @@ -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.Hybrid.Enabled { + createOrgData.HybridEnabled = true + createOrgData.EventOptions = eventOptions(s.appArgs.Tyk.Hybrid, s.appArgs.Tyk.Admin.EmailAddress) + } + reqBodyBytes, err := json.Marshal(createOrgData) if err != nil { return err @@ -121,3 +129,26 @@ func (s *Service) CreateOrganisation() error { return nil } + +func eventOptions(hconf config.HybridConf, defaultEmail string) map[string]api.EventConfig { + m := make(map[string]api.EventConfig) + + const ( + keyEventKey = "key_event" + hashedKeyEventKey = "hashed_key_event" + ) + + if hconf.HashedKeyEvent != nil { + m[hashedKeyEventKey] = *hconf.HashedKeyEvent + } else { + m[hashedKeyEventKey] = api.EventConfig{Email: defaultEmail} + } + + if hconf.KeyEvent != nil { + m[keyEventKey] = *hconf.KeyEvent + } else { + m[keyEventKey] = api.EventConfig{Email: defaultEmail} + } + + return m +} From 521cdf0392a8289b5a27b5de80350f390706939b Mon Sep 17 00:00:00 2001 From: Burak Sekili Date: Wed, 6 Mar 2024 11:45:24 +0300 Subject: [PATCH 2/5] fix linter: run gofumpt to fix import order Signed-off-by: Burak Sekili --- pkg/config/config.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/pkg/config/config.go b/pkg/config/config.go index ca1103d..3215ae7 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -1,8 +1,9 @@ package config import ( - "github.com/kelseyhightower/envconfig" "tyk/tyk/bootstrap/tyk/api" + + "github.com/kelseyhightower/envconfig" ) const prefix = "TYK_K8SBOOTSTRAP" From 345a47e3408d1790861677e76cd65371053eb417 Mon Sep 17 00:00:00 2001 From: Burak Sekili Date: Wed, 6 Mar 2024 11:45:58 +0300 Subject: [PATCH 3/5] fix long line issue in config field description Signed-off-by: Burak Sekili --- pkg/config/config.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pkg/config/config.go b/pkg/config/config.go index 3215ae7..cd922b4 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -97,8 +97,8 @@ type HybridConf struct { // 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 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 } From 2b8131b6980f47dd6fbbeb36081feac62ebb06c1 Mon Sep 17 00:00:00 2001 From: Burak Sekili Date: Wed, 6 Mar 2024 15:49:35 +0300 Subject: [PATCH 4/5] Populate email in events field Signed-off-by: Burak Sekili --- README.md | 6 +++--- hack/load_images.sh | 10 +++------- pkg/config/config.go | 8 ++++---- tyk/api/request.go | 8 ++++---- tyk/organisation.go | 34 ++++++++++++++++++++++++---------- 5 files changed, 38 insertions(+), 28 deletions(-) diff --git a/README.md b/README.md index 0ea7d5a..f78bddc 100644 --- a/README.md +++ b/README.md @@ -37,10 +37,10 @@ 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. | -| TYK_K8SBOOTSTRAP_TYK_HYBRID_ENABLED | specifies if the Hybrid organisation for MDCB Control Plane is enabled or not | -| TYK_K8SBOOTSTRAP_TYK_HYBRID_KEYEVENT | corresponds to `key_event` of the event options (optional). | -| TYK_K8SBOOTSTRAP_TYK_HYBRID_HASHEDKEYEVENT | corresponds to `hashed_key_event` of the event options (optional). | ## Bootstrapped Environments diff --git a/hack/load_images.sh b/hack/load_images.sh index 1302ba2..58dd25d 100755 --- a/hack/load_images.sh +++ b/hack/load_images.sh @@ -2,11 +2,7 @@ make build-all -docker build -t tykio/tyk-k8s-bootstrap-pre-install:testing -f ./.container/image/bootstrap-pre-install/Dockerfile ./bin && - kind load docker-image tykio/tyk-k8s-bootstrap-pre-install:testing +docker build -t tykio/tyk-k8s-bootstrap-pre-install:testing -f ./.container/image/bootstrap-pre-install/Dockerfile ./bin +docker build -t tykio/tyk-k8s-bootstrap-post:testing -f ./.container/image/bootstrap-post/Dockerfile ./bin +docker build -t tykio/tyk-k8s-bootstrap-pre-delete:testing -f ./.container/image/bootstrap-pre-delete/Dockerfile ./bin -docker build -t tykio/tyk-k8s-bootstrap-post:testing -f ./.container/image/bootstrap-post/Dockerfile ./bin && - kind load docker-image tykio/tyk-k8s-bootstrap-post:testing - -docker build -t tykio/tyk-k8s-bootstrap-pre-delete:testing -f ./.container/image/bootstrap-pre-delete/Dockerfile ./bin && - kind load docker-image tykio/tyk-k8s-bootstrap-pre-delete:testing diff --git a/pkg/config/config.go b/pkg/config/config.go index cd922b4..99e649b 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -76,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 { @@ -86,9 +89,6 @@ type TykConf struct { // DashboardLicense corresponds to the license key of Tyk Dashboard. DashboardLicense string - - // Hybrid includes details of hybrid organisation while using MDCB Control Plane - Hybrid HybridConf } type HybridConf struct { @@ -99,7 +99,7 @@ type HybridConf struct { 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 + HashedKeyEvent *api.EventConfig `json:",omitempty"` } func NewConfig() (*Config, error) { diff --git a/tyk/api/request.go b/tyk/api/request.go index 83ecbc0..1f709b8 100644 --- a/tyk/api/request.go +++ b/tyk/api/request.go @@ -1,9 +1,9 @@ package api type EventConfig struct { - Webhook string `json:"webhook"` - Email string `json:"email"` - Redis bool `json:"redis"` + Webhook string `json:"webhook,omitempty"` + Email string `json:"email,omitempty"` + Redis bool `json:"redis,omitempty"` } type CreateOrgReq struct { @@ -11,7 +11,7 @@ type CreateOrgReq struct { CnameEnabled bool `json:"cname_enabled"` Cname string `json:"cname"` HybridEnabled bool `json:"hybrid_enabled"` - EventOptions map[string]EventConfig `json:"event_options"` + EventOptions map[string]EventConfig `json:"event_options,omitempty"` } type ResetPasswordReq struct { diff --git a/tyk/organisation.go b/tyk/organisation.go index 927e934..7dc1d06 100644 --- a/tyk/organisation.go +++ b/tyk/organisation.go @@ -82,9 +82,9 @@ func (s *Service) CreateOrganisation() error { // 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.Hybrid.Enabled { + if s.appArgs.Tyk.Org.Hybrid != nil && s.appArgs.Tyk.Org.Hybrid.Enabled { createOrgData.HybridEnabled = true - createOrgData.EventOptions = eventOptions(s.appArgs.Tyk.Hybrid, s.appArgs.Tyk.Admin.EmailAddress) + createOrgData.EventOptions = eventOptions(s.appArgs.Tyk.Org.Hybrid, s.appArgs.Tyk.Admin.EmailAddress) } reqBodyBytes, err := json.Marshal(createOrgData) @@ -130,25 +130,39 @@ func (s *Service) CreateOrganisation() error { return nil } -func eventOptions(hconf config.HybridConf, defaultEmail string) map[string]api.EventConfig { +func eventOptions(hconf *config.HybridConf, defaultEmail string) map[string]api.EventConfig { + if hconf == nil { + return nil + } + m := make(map[string]api.EventConfig) const ( - keyEventKey = "key_event" 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 { - m[hashedKeyEventKey] = *hconf.HashedKeyEvent - } else { - m[hashedKeyEventKey] = api.EventConfig{Email: defaultEmail} + if hconf.HashedKeyEvent.Email != "" { + hashedKeyEventConf.Email = hconf.HashedKeyEvent.Email + } } if hconf.KeyEvent != nil { - m[keyEventKey] = *hconf.KeyEvent - } else { - m[keyEventKey] = api.EventConfig{Email: defaultEmail} + if hconf.KeyEvent.Email != "" { + keyEventConf.Email = hconf.KeyEvent.Email + } } + m[hashedKeyEventKey] = hashedKeyEventConf + m[keyEventKey] = keyEventConf + return m } From 4e68dcaef280fecfaf53e303fbb8c1168bb15684 Mon Sep 17 00:00:00 2001 From: Burak Sekili Date: Wed, 6 Mar 2024 15:53:44 +0300 Subject: [PATCH 5/5] allow kind cluster to load local images Signed-off-by: Burak Sekili --- hack/load_images.sh | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/hack/load_images.sh b/hack/load_images.sh index 58dd25d..1302ba2 100755 --- a/hack/load_images.sh +++ b/hack/load_images.sh @@ -2,7 +2,11 @@ make build-all -docker build -t tykio/tyk-k8s-bootstrap-pre-install:testing -f ./.container/image/bootstrap-pre-install/Dockerfile ./bin -docker build -t tykio/tyk-k8s-bootstrap-post:testing -f ./.container/image/bootstrap-post/Dockerfile ./bin -docker build -t tykio/tyk-k8s-bootstrap-pre-delete:testing -f ./.container/image/bootstrap-pre-delete/Dockerfile ./bin +docker build -t tykio/tyk-k8s-bootstrap-pre-install:testing -f ./.container/image/bootstrap-pre-install/Dockerfile ./bin && + kind load docker-image tykio/tyk-k8s-bootstrap-pre-install:testing +docker build -t tykio/tyk-k8s-bootstrap-post:testing -f ./.container/image/bootstrap-post/Dockerfile ./bin && + kind load docker-image tykio/tyk-k8s-bootstrap-post:testing + +docker build -t tykio/tyk-k8s-bootstrap-pre-delete:testing -f ./.container/image/bootstrap-pre-delete/Dockerfile ./bin && + kind load docker-image tykio/tyk-k8s-bootstrap-pre-delete:testing