diff --git a/docs/commands/rhoas_generate-config.md b/docs/commands/rhoas_generate-config.md index 3c8d6a702..a40d22be8 100644 --- a/docs/commands/rhoas_generate-config.md +++ b/docs/commands/rhoas_generate-config.md @@ -11,7 +11,7 @@ You must specify an output format into which the credentials will be stored: - env (default): Store configurations in an env file as environment variables - json: Store configurations in a JSON file - properties: Store configurations in a properties file, which is typically used in Java-related technologies -- secret: Store configurations in a Kubernetes secret file +- configmap: Store configurations in a Kubernetes ConfigMap file ``` @@ -27,8 +27,8 @@ $ rhoas generate-config --type json ## Generate configurations for the current service context in env format and save it in specified path $ rhoas generate-config --type env --output-file ./configs/.env -## Generate configurations for a specified context as Kubernetes secret -$ rhoas generate-config --name qaprod --type secret +## Generate configurations for a specified context as Kubernetes ConfigMap +$ rhoas generate-config --name qaprod --type configmap ``` diff --git a/pkg/cmd/generate/build-configs.go b/pkg/cmd/generate/build-configs.go index 2dfb85150..4f07f854d 100644 --- a/pkg/cmd/generate/build-configs.go +++ b/pkg/cmd/generate/build-configs.go @@ -2,53 +2,22 @@ package generate import ( "fmt" - "time" - "github.com/redhat-developer/app-services-cli/pkg/cmd/serviceaccount/svcaccountcmdutil" "github.com/redhat-developer/app-services-cli/pkg/core/ioutil/icon" "github.com/redhat-developer/app-services-cli/pkg/core/localize" "github.com/redhat-developer/app-services-cli/pkg/core/servicecontext" "github.com/redhat-developer/app-services-cli/pkg/shared/contextutil" "github.com/redhat-developer/app-services-cli/pkg/shared/factory" - - kafkamgmtclient "github.com/redhat-developer/app-services-sdk-go/kafkamgmt/apiv1/client" ) type configValues struct { - KafkaHost string - RegistryURL string - ClientID string - ClientSecret string - TokenURL string + KafkaHost string + RegistryURL string // Optional Name string } -func createServiceAccount(opts *options, shortDescription string) (*kafkamgmtclient.ServiceAccount, error) { - conn, err := opts.Connection() - if err != nil { - return nil, err - } - serviceAccountPayload := kafkamgmtclient.ServiceAccountRequest{Name: shortDescription} - - serviceacct, httpRes, err := conn.API(). - ServiceAccountMgmt(). - CreateServiceAccount(opts.Context). - ServiceAccountRequest(serviceAccountPayload). - Execute() - - if httpRes != nil { - defer httpRes.Body.Close() - } - - if err != nil { - return nil, err - } - - return &serviceacct, nil -} - // BuildConfiguration builds the configs for the service context func BuildConfiguration(svcConfig *servicecontext.ServiceConfig, opts *options) error { @@ -64,6 +33,7 @@ func BuildConfiguration(svcConfig *servicecontext.ServiceConfig, opts *options) configurations := &configValues{} var serviceAvailable bool + var err error if svcConfig.KafkaID != "" { kafkaInstance, newErr := contextutil.GetCurrentKafkaInstance(factory) @@ -88,30 +58,8 @@ func BuildConfiguration(svcConfig *servicecontext.ServiceConfig, opts *options) if !serviceAvailable { return opts.localizer.MustLocalizeError("generate.log.info.noServices") } - configInstanceName := fmt.Sprintf("%s-%v", opts.name, time.Now().Unix()) - serviceAccount, err := createServiceAccount(opts, configInstanceName) - if err != nil { - return err - } - - opts.Logger.Info( - icon.SuccessPrefix(), - opts.localizer.MustLocalize("serviceAccount.create.log.info.createdSuccessfully", localize.NewEntry("ID", serviceAccount.GetId())), - ) - - conn, err := opts.Connection() - if err != nil { - return err - } - - providerUrls, err := svcaccountcmdutil.GetProvidersDetails(conn, opts.Context) - if err != nil { - return err - } + configInstanceName := fmt.Sprintf("%s-configuration", opts.name) - configurations.ClientID = serviceAccount.GetClientId() - configurations.ClientSecret = serviceAccount.GetClientSecret() - configurations.TokenURL = providerUrls.GetTokenUrl() configurations.Name = configInstanceName var fileName string diff --git a/pkg/cmd/generate/configurations.go b/pkg/cmd/generate/configurations.go index c68a9ac5e..804444c9c 100644 --- a/pkg/cmd/generate/configurations.go +++ b/pkg/cmd/generate/configurations.go @@ -15,16 +15,16 @@ const ( envFormat = "env" jsonFormat = "json" propertiesFormat = "properties" - secretFormat = "secret" + configmapFormat = "configmap" ) -var configurationTypes = []string{envFormat, jsonFormat, propertiesFormat, secretFormat} +var configurationTypes = []string{envFormat, jsonFormat, propertiesFormat, configmapFormat} var ( - envConfig = template.Must(template.New(envFormat).Parse(templateEnv)) - jsonConfig = template.Must(template.New(jsonFormat).Parse(templateJSON)) - propertiesConfig = template.Must(template.New(propertiesFormat).Parse(templateProperties)) - secretTemplateConfig = template.Must(template.New(secretFormat).Parse(templateSecret)) + envConfig = template.Must(template.New(envFormat).Parse(templateEnv)) + jsonConfig = template.Must(template.New(jsonFormat).Parse(templateJSON)) + propertiesConfig = template.Must(template.New(propertiesFormat).Parse(templateProperties)) + configMapTemplateConfig = template.Must(template.New(configmapFormat).Parse(templateConfigMap)) ) // WriteConfig saves the configurations to a file @@ -57,13 +57,13 @@ func WriteConfig(opts *options, config *configValues) (string, error) { func getDefaultPath(configType string) (filePath string) { switch configType { case envFormat: - filePath = ".env" + filePath = "rhoas.env" case propertiesFormat: filePath = "rhoas.properties" case jsonFormat: filePath = "rhoas.json" - case secretFormat: - filePath = "rhoas-services-secret.yaml" + case configmapFormat: + filePath = "rhoas-services.yaml" } pwd, err := os.Getwd() @@ -84,8 +84,8 @@ func getFileFormat(configType string) (template *template.Template) { template = propertiesConfig case jsonFormat: template = jsonConfig - case secretFormat: - template = secretTemplateConfig + case configmapFormat: + template = configMapTemplateConfig } return template diff --git a/pkg/cmd/generate/generate-config.go b/pkg/cmd/generate/generate-config.go index c489defb4..febf61070 100644 --- a/pkg/cmd/generate/generate-config.go +++ b/pkg/cmd/generate/generate-config.go @@ -63,7 +63,8 @@ func NewGenerateCommand(f *factory.Factory) *cobra.Command { flags.AddContextName(&opts.name) flags.StringVar(&opts.configType, "type", "", opts.localizer.MustLocalize("generate.flag.type")) cmd.Flags().BoolVar(&opts.overwrite, "overwrite", false, opts.localizer.MustLocalize("generate.flag.overwrite.description")) - cmd.Flags().StringVar(&opts.fileName, "output-file", "", opts.localizer.MustLocalize("generate.common.flag.fileLocation.description")) + flags.StringVar(&opts.fileName, "output-file", "", opts.localizer.MustLocalize("generate.common.flag.fileLocation.description")) + _ = cmd.MarkFlagRequired("type") flagutil.EnableStaticFlagCompletion(cmd, "type", configurationTypes) diff --git a/pkg/cmd/generate/templates.go b/pkg/cmd/generate/templates.go index 6f796f3f9..ac59235e3 100644 --- a/pkg/cmd/generate/templates.go +++ b/pkg/cmd/generate/templates.go @@ -15,10 +15,6 @@ var ( SERVICE_REGISTRY_CORE_PATH=` + registrycmdutil.REGISTRY_CORE_PATH + ` SERVICE_REGISTRY_COMPAT_PATH=` + registrycmdutil.REGISTRY_COMPAT_PATH + ` {{end}} - ## Authentication Configuration - RHOAS_CLIENT_ID={{.ClientID}} - RHOAS_CLIENT_SECRET={{.ClientSecret}} - RHOAS_OAUTH_TOKEN_URL={{.TokenURL}} `) templateJSON = heredoc.Doc(` @@ -26,10 +22,7 @@ var ( {{if .KafkaHost}}"kafkaHost":"{{.KafkaHost}}", {{end}}{{if .RegistryURL}}"serviceRegistryUrl":"{{.RegistryURL}}", "serviceRegistryCorePath":"` + registrycmdutil.REGISTRY_CORE_PATH + `", - "serviceRegistryCompatPath":"` + registrycmdutil.REGISTRY_COMPAT_PATH + `", - {{end}}"rhoasClientID":"{{.ClientID}}", - "rhoasClientSecret":"{{.ClientSecret}}", - "rhoasOauthTokenUrl":"{{.TokenURL}}" + "serviceRegistryCompatPath":"` + registrycmdutil.REGISTRY_COMPAT_PATH + `"{{end}} } `) @@ -37,35 +30,24 @@ var ( ## Generated by rhoas cli {{if .KafkaHost}}## Kafka Configuration kafkaHost={{.KafkaHost}} - {{end}}{{if .RegistryURL}} ## Service Registry Configuration + {{end}}{{if .RegistryURL}}## Service Registry Configuration serviceRegistryUrl={{.RegistryURL}} serviceRegistryCorePath=` + registrycmdutil.REGISTRY_CORE_PATH + ` - serviceRegistryCompatPath=` + registrycmdutil.REGISTRY_COMPAT_PATH + ` - {{end}} - ## Authentication Configuration - rhoasClientID={{.ClientID}} - rhoasClientSecret={{.ClientSecret}} - rhoasOauthTokenUrl={{.TokenURL}} + serviceRegistryCompatPath=` + registrycmdutil.REGISTRY_COMPAT_PATH + `{{end}} `) - templateSecret = heredoc.Doc(` + templateConfigMap = heredoc.Doc(` apiVersion: v1 - kind: Secret + kind: ConfigMap metadata: - name: {{.Name}} - type: Opaque - stringData: + name: {{.Name}} + data: {{if .KafkaHost}}## Kafka Configuration - KAFKA_HOST: {{.KafkaHost}} + kafka_host: {{.KafkaHost}} {{end}} {{if .RegistryURL}}## Service Registry Configuration - SERVICE_REGISTRY_URL: {{.RegistryURL}} - SERVICE_REGISTRY_COMPAT_PATH: ` + registrycmdutil.REGISTRY_COMPAT_PATH + ` - SERVICE_REGISTRY_CORE_PATH: ` + registrycmdutil.REGISTRY_CORE_PATH + ` - {{end}} - ## Authentication Configuration - RHOAS_CLIENT_ID: {{.ClientID}} - RHOAS_CLIENT_SECRET: {{.ClientSecret}} - RHOAS_OAUTH_TOKEN_URL: {{.TokenURL}} + service_registry_url: {{.RegistryURL}} + service_registry_compat_path: ` + registrycmdutil.REGISTRY_COMPAT_PATH + ` + service_registry_core_path: ` + registrycmdutil.REGISTRY_CORE_PATH + `{{end}} `) ) diff --git a/pkg/core/localize/locales/en/cmd/generate_config.en.toml b/pkg/core/localize/locales/en/cmd/generate_config.en.toml index 089b65cce..692ecc4a8 100644 --- a/pkg/core/localize/locales/en/cmd/generate_config.en.toml +++ b/pkg/core/localize/locales/en/cmd/generate_config.en.toml @@ -10,7 +10,7 @@ You must specify an output format into which the credentials will be stored: - env (default): Store configurations in an env file as environment variables - json: Store configurations in a JSON file - properties: Store configurations in a properties file, which is typically used in Java-related technologies -- secret: Store configurations in a Kubernetes secret file +- configmap: Store configurations in a Kubernetes ConfigMap file ''' [generate.cmd.example] @@ -21,8 +21,8 @@ $ rhoas generate-config --type json ## Generate configurations for the current service context in env format and save it in specified path $ rhoas generate-config --type env --output-file ./configs/.env -## Generate configurations for a specified context as Kubernetes secret -$ rhoas generate-config --name qaprod --type secret +## Generate configurations for a specified context as Kubernetes ConfigMap +$ rhoas generate-config --name qaprod --type configmap ''' [generate.flag.type] @@ -43,4 +43,11 @@ one = 'file {{.FilePath}} already exists. Use --overwrite to overwrite the file, one='No services available to generate configurations' [generate.log.info.credentialsSaved] -one='Configurations successfully saved to "{{.FilePath}}"' \ No newline at end of file +one=''' +Configurations successfully saved to "{{.FilePath}}" + +You can now use existing service accounts or create new to connect to the services. +To create new service account, run this command: + + $ rhoas service-account create +''' \ No newline at end of file