Skip to content

Commit

Permalink
- created k8s package for all k8s-related operations
Browse files Browse the repository at this point in the history
- created tyk package for all tyk-releated operations
- add checks to identify existing organisations
- moved license package content to pkg/
- delete preinstallation package since it only runs one function.
- moved all configs to pkg/config

Signed-off-by: Burak Sekili <buraksekili@gmail.com>
  • Loading branch information
buraksekili committed Nov 6, 2023
1 parent ec848fe commit d4bdace
Show file tree
Hide file tree
Showing 25 changed files with 1,109 additions and 973 deletions.
93 changes: 47 additions & 46 deletions cmd/bootstrap-post/main.go
Original file line number Diff line number Diff line change
@@ -1,80 +1,81 @@
package main

import (
"crypto/tls"
"errors"
"fmt"
"net/http"
"os"
"tyk/tyk/bootstrap/data"
"tyk/tyk/bootstrap/helpers"
"tyk/tyk/bootstrap/readiness"
"tyk/tyk/bootstrap/k8s"
"tyk/tyk/bootstrap/pkg/config"
"tyk/tyk/bootstrap/tyk"
)

func main() {
err := data.InitPostInstall()
conf, err := config.NewConfig()
if err != nil {
fmt.Println(err)
os.Exit(1)
exit(err)
}

err = readiness.CheckIfRequiredDeploymentsAreReady()
k8sClient, err := k8s.NewClient(conf)
if err != nil {
fmt.Println(err)
os.Exit(1)
exit(err)
}

tp := &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: data.BootstrapConf.InsecureSkipVerify},
if err = k8sClient.CheckIfRequiredDeploymentsAreReady(); err != nil {
exit(err)
}
client := http.Client{Transport: tp}

fmt.Println("Started creating dashboard org")
orgExists := false

err = helpers.CheckForExistingOrganisation(client)
if err != nil {
fmt.Println(err)
os.Exit(1)
tykSvc := tyk.NewService(conf)
if err = tykSvc.OrgExists(); err != nil {
if !errors.Is(err, tyk.ErrOrgExists) {
exit(err)
}

orgExists = true
}

fmt.Println("Finished creating dashboard org")
fmt.Println("Generating dashboard credentials")
if !orgExists {
if err = tykSvc.CreateOrganisation(); err != nil {
exit(err)
}

err = helpers.GenerateDashboardCredentials(client)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
if err = tykSvc.CreateAdmin(); err != nil {
exit(err)
}

fmt.Println("Finished generating dashboard credentials")
fmt.Println("Started bootstrapping operator secret")
if conf.BootstrapPortal {
if err = tykSvc.BootstrapClassicPortal(); err != nil {
exit(err)
}
}

if data.BootstrapConf.OperatorKubernetesSecretName != "" {
err = helpers.BootstrapTykOperatorSecret()
if err != nil {
fmt.Println(err)
os.Exit(1)
if err = k8sClient.RestartDashboard(); err != nil {
exit(err)
}
}

fmt.Println("Finished bootstrapping operator secret\nStarted bootstrapping portal secret")

if data.BootstrapConf.DevPortalKubernetesSecretName != "" {
err = helpers.BootstrapTykPortalSecret()
if conf.OperatorKubernetesSecretName != "" {
err = k8sClient.BootstrapTykOperatorSecret()
if err != nil {
fmt.Println(err)
os.Exit(1)
exit(err)
}
}

fmt.Println("Started bootstrapping portal with requests to dashboard")

if data.BootstrapConf.BootstrapPortal {
err = helpers.BoostrapPortal(client)
if conf.DevPortalKubernetesSecretName != "" {
err = k8sClient.BootstrapTykPortalSecret()
if err != nil {
fmt.Println(err)
os.Exit(1)
exit(err)
}
}

fmt.Println("Finished bootstrapping portal")
}

func exit(err error) {
if err == nil {
os.Exit(0)
}

fmt.Printf("[ERROR]: %v", err)
os.Exit(1)
}
14 changes: 10 additions & 4 deletions cmd/bootstrap-pre-delete/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,24 @@ package main
import (
"fmt"
"os"
"tyk/tyk/bootstrap/data"
"tyk/tyk/bootstrap/predelete"
"tyk/tyk/bootstrap/k8s"
"tyk/tyk/bootstrap/pkg/config"
)

func main() {
err := data.InitBootstrapConf()
conf, err := config.NewConfig()
if err != nil {
fmt.Println(err)
os.Exit(1)
}

err = predelete.ExecutePreDeleteOperations()
k8sClient, err := k8s.NewClient(conf)
if err != nil {
fmt.Println(err)
os.Exit(1)
}

err = k8sClient.ExecutePreDeleteOperations()
if err != nil {
fmt.Println(err)
os.Exit(1)
Expand Down
15 changes: 9 additions & 6 deletions cmd/bootstrap-pre-install/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,23 @@ package main
import (
"fmt"
"os"
"tyk/tyk/bootstrap/data"
"tyk/tyk/bootstrap/preinstallation"
"tyk/tyk/bootstrap/pkg"
"tyk/tyk/bootstrap/pkg/config"
)

func main() {
err := data.InitBootstrapConf()
conf, err := config.NewConfig()
if err != nil {
fmt.Printf("Failed to parse bootstrap environment variables, err: %v", err)
os.Exit(1)
}

err = preinstallation.PreHookInstall()
licenseIsValid, err := pkg.ValidateDashboardLicense(conf.Tyk.DashboardLicense)
if err != nil {
fmt.Printf("Failed to run pre-hook job, err: %v", err)
os.Exit(1)
}

if !licenseIsValid {
//return errors.New("provided license is invalid")
os.Exit(1)
}

Expand Down
8 changes: 5 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ go 1.17

require (
github.com/golang-jwt/jwt v3.2.2+incompatible
github.com/kelseyhightower/envconfig v1.4.0
k8s.io/api v0.24.3
k8s.io/apimachinery v0.24.3
k8s.io/client-go v0.24.3
)
Expand All @@ -22,18 +24,19 @@ require (
github.com/google/gnostic v0.5.7-v3refs // indirect
github.com/google/go-cmp v0.5.6 // indirect
github.com/google/gofuzz v1.1.0 // indirect
github.com/imdario/mergo v0.3.5 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/kelseyhightower/envconfig v1.4.0 // indirect
github.com/mailru/easyjson v0.7.6 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
github.com/onsi/ginkgo v1.16.4 // indirect
github.com/onsi/gomega v1.13.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
golang.org/x/net v0.0.0-20220127200216-cd36cc0744dd // indirect
golang.org/x/oauth2 v0.0.0-20211104180415-d3ed0bb246c8 // indirect
golang.org/x/sys v0.0.0-20220209214540-3681064d5158 // indirect
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 // indirect
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211 // indirect
golang.org/x/text v0.3.7 // indirect
golang.org/x/time v0.0.0-20220210224613-90d013bbcef8 // indirect
Expand All @@ -43,7 +46,6 @@ require (
gopkg.in/inf.v0 v0.9.1 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect
k8s.io/api v0.24.3 // indirect
k8s.io/klog/v2 v2.60.1 // indirect
k8s.io/kube-openapi v0.0.0-20220328201542-3ee0da9b0b42 // indirect
k8s.io/utils v0.0.0-20220210201930-3a6ce19ff2f9 // indirect
Expand Down
4 changes: 3 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@ github.com/hashicorp/golang-lru v0.5.1/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ
github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
github.com/ianlancetaylor/demangle v0.0.0-20181102032728-5e5cf60278f6/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
github.com/ianlancetaylor/demangle v0.0.0-20200824232613-28f6c0f3b639/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
github.com/imdario/mergo v0.3.5 h1:JboBksRwiiAJWvIYJVo46AfV+IAIKZpfrSzVKj42R4Q=
github.com/imdario/mergo v0.3.5/go.mod h1:2EnlNZ0deacrJVfApfmtdGgDfMuh/nq6Ok1EcJh5FfA=
github.com/josharian/intern v1.0.0 h1:vlS4z54oSdjm0bgjRigI+G1HpF+tI+9rE5LLzOg8HmY=
github.com/josharian/intern v1.0.0/go.mod h1:5DoeVV0s6jJacbCEi61lwdGj/aVlrQvzHFFd8Hwg//Y=
Expand Down Expand Up @@ -415,8 +416,9 @@ golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7w
golang.org/x/sys v0.0.0-20210510120138-977fb7262007/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220209214540-3681064d5158 h1:rm+CHSpPEEW2IsXUib1ThaHIjuBVZjxNgSKmBLFfD4c=
golang.org/x/sys v0.0.0-20220209214540-3681064d5158/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 h1:0A+M6Uqn+Eje4kHMK80dtF3JCXC4ykBgQG4Fe06QRhQ=
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211 h1:JGgROgKl9N8DuW20oFS5gxc+lE67/N3FcwmBPMe7ArY=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
Expand Down
150 changes: 0 additions & 150 deletions helpers/operator.go

This file was deleted.

Loading

0 comments on commit d4bdace

Please sign in to comment.