@@ -5,9 +5,13 @@ import (
55 "errors"
66 "fmt"
77 "github.com/onepanelio/cli/cloud/storage"
8+ "github.com/sethvargo/go-password/password"
89 "golang.org/x/crypto/bcrypt"
910 "io/ioutil"
11+ v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1012 "k8s.io/apimachinery/pkg/util/rand"
13+ "k8s.io/client-go/kubernetes"
14+ "k8s.io/client-go/tools/clientcmd"
1115 "log"
1216 "os"
1317 "path/filepath"
@@ -51,8 +55,17 @@ var generateCmd = &cobra.Command{
5155
5256 kustomizeTemplate := TemplateFromSimpleOverlayedComponents (config .GetOverlayComponents ("" ))
5357
58+ databaseConfig , err := GetDatabaseConfigurationFromCluster ()
59+ if err != nil {
60+ fmt .Printf ("[error] %v" , err .Error ())
61+ return
62+ }
63+
5464 log .Printf ("Building..." )
55- result , err := GenerateKustomizeResult (* config , kustomizeTemplate )
65+ result , err := GenerateKustomizeResult (kustomizeTemplate , & GenerateKustomizeResultOptions {
66+ Config : config ,
67+ Database : databaseConfig ,
68+ })
5669 if err != nil {
5770 fmt .Printf ("%s\n " , HumanizeKustomizeError (err ))
5871 return
@@ -67,10 +80,112 @@ func init() {
6780 generateCmd .Flags ().BoolVarP (& Dev , "latest" , "" , false , "Sets conditions to allow development testing." )
6881}
6982
83+ // GenerateKustomizeResultOptions is configuration for the GenerateKustomizeResult function
84+ type GenerateKustomizeResultOptions struct {
85+ Database * opConfig.Database
86+ Config * opConfig.Config
87+ }
88+
89+ // generateDatabaseConfiguration checks to see if database configuration is already present
90+ // if not, it'll randomly generate some.
91+ func generateDatabaseConfiguration (yaml * util.DynamicYaml , database * opConfig.Database ) error {
92+ if yaml .HasKey ("database" ) {
93+ return nil
94+ }
95+
96+ if database == nil {
97+ dbPath := filepath .Join (".onepanel" , "manifests" , "cache" , "common" , "onepanel" , "base" , "vars.yaml" )
98+ data , err := ioutil .ReadFile (dbPath )
99+ if err != nil {
100+ log .Fatal (err )
101+ }
102+
103+ wrapper := & opConfig.DatabaseWrapper {}
104+ if err := yaml2 .Unmarshal (data , wrapper ); err != nil {
105+ log .Fatal (err )
106+ }
107+
108+ database = wrapper .Database
109+
110+ pass , err := password .Generate (16 , 6 , 0 , false , false )
111+ if err != nil {
112+ return err
113+ }
114+ database .Password .Value = pass
115+
116+ username , err := password .Generate (8 , 6 , 0 , false , false )
117+ if err != nil {
118+ return err
119+ }
120+ database .Username .Value = "onepanel" + username
121+ }
122+
123+ yaml .Put ("database.host" , database .Host .Value )
124+ yaml .Put ("database.username" , database .Username .Value )
125+ yaml .Put ("database.password" , database .Password .Value )
126+ yaml .Put ("database.port" , fmt .Sprintf (`"%s"` , database .Port .Value ))
127+ yaml .Put ("database.databaseName" , database .DatabaseName .Value )
128+ yaml .Put ("database.driverName" , database .DriverName .Value )
129+
130+ return nil
131+ }
132+
133+ // GetDatabaseConfigurationFromCluster attempts to load the database configuration from a deployed cluster
134+ // If there is no configuration (not found) no error is returned
135+ func GetDatabaseConfigurationFromCluster () (database * opConfig.Database , err error ) {
136+ config , err := clientcmd .NewNonInteractiveDeferredLoadingClientConfig (
137+ clientcmd .NewDefaultClientConfigLoadingRules (), & clientcmd.ConfigOverrides {}).ClientConfig ()
138+ if err != nil {
139+ return
140+ }
141+ // check if there is any, and load that. Otherwise don't.
142+ c , err := kubernetes .NewForConfig (config )
143+ if err != nil {
144+ return
145+ }
146+
147+ secret , err := c .CoreV1 ().Secrets ("onepanel" ).Get ("onepanel" , v1.GetOptions {})
148+ if err != nil {
149+ if strings .Contains (err .Error (), "not found" ) {
150+ return nil , nil
151+ }
152+ return
153+ }
154+
155+ databaseUsername := string (secret .Data ["databaseUsername" ])
156+ databasePassword := string (secret .Data ["databasePassword" ])
157+
158+ configMap , err := c .CoreV1 ().ConfigMaps ("onepanel" ).Get ("onepanel" , v1.GetOptions {})
159+ if err != nil {
160+ if strings .Contains (err .Error (), "not found" ) {
161+ return nil , nil
162+ }
163+ return
164+ }
165+
166+ driverName := configMap .Data ["databaseDriverName" ]
167+ databaseHost := configMap .Data ["databaseHost" ]
168+ databaseName := configMap .Data ["databaseName" ]
169+ databasePort := configMap .Data ["databasePort" ]
170+
171+ database = & opConfig.Database {
172+ Host : opConfig .RequiredManifestVar (databaseHost ),
173+ Username : opConfig .RequiredManifestVar (databaseUsername ),
174+ Password : opConfig .RequiredManifestVar (databasePassword ),
175+ Port : opConfig .RequiredManifestVar (databasePort ),
176+ DatabaseName : opConfig .RequiredManifestVar (databaseName ),
177+ DriverName : opConfig .RequiredManifestVar (driverName ),
178+ }
179+
180+ return
181+ }
182+
70183// GenerateKustomizeResult Given the path to the manifests, and a kustomize config, creates the final kustomization file.
71184// It does this by copying the manifests into a temporary directory, inserting the kustomize template
72185// and running the kustomize command
73- func GenerateKustomizeResult (config opConfig.Config , kustomizeTemplate template.Kustomize ) (string , error ) {
186+ func GenerateKustomizeResult (kustomizeTemplate template.Kustomize , options * GenerateKustomizeResultOptions ) (string , error ) {
187+ config := * options .Config
188+
74189 yamlFile , err := util .LoadDynamicYamlFromFile (config .Spec .Params )
75190 if err != nil {
76191 return "" , err
@@ -269,6 +384,10 @@ func GenerateKustomizeResult(config opConfig.Config, kustomizeTemplate template.
269384 yamlFile .Put ("workflowEngineContainerRuntimeExecutor" , valueNode .Value )
270385 }
271386
387+ if err := generateDatabaseConfiguration (yamlFile , options .Database ); err != nil {
388+ return "" , err
389+ }
390+
272391 flatMap := yamlFile .FlattenToKeyValue (util .LowerCamelCaseFlatMapKeyFormatter )
273392 if err := mapLinkedVars (flatMap , localManifestsCopyPath , & config , true ); err != nil {
274393 return "" , err
0 commit comments