cluster.go 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825
  1. // Copyright 2019 Yunion
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package k8s
  15. import (
  16. "fmt"
  17. "io/ioutil"
  18. "os"
  19. "strings"
  20. "yunion.io/x/jsonutils"
  21. "yunion.io/x/pkg/errors"
  22. "yunion.io/x/onecloud/pkg/mcclient/options"
  23. )
  24. type ClusterListOptions struct {
  25. options.BaseListOptions
  26. // federated resource keyword, e.g: federatednamespace
  27. FederatedKeyword string `json:"federated_keyword"`
  28. FederatedResourceId string `json:"federated_resource_id"`
  29. FederatedUsed *bool `json:"-"`
  30. FederatedUnused *bool `json:"-"`
  31. ManagerId string `json:"manager_id"`
  32. CloudRegionId string `json:"cloudregion_id"`
  33. }
  34. func (o *ClusterListOptions) Params() (jsonutils.JSONObject, error) {
  35. params, err := options.ListStructToParams(o)
  36. if err != nil {
  37. return nil, err
  38. }
  39. if o.FederatedUnused != nil {
  40. params.Add(jsonutils.JSONFalse, "federated_used")
  41. }
  42. if o.FederatedUsed != nil {
  43. params.Add(jsonutils.JSONTrue, "federated_used")
  44. }
  45. return params, nil
  46. }
  47. type AddMachineOptions struct {
  48. Machine []string `help:"Machine create desc, e.g. host01:baremetal:controlplane"`
  49. MachineNet string `help:"Machine net config"`
  50. MachineDisk string `help:"Machine root disk size, e.g. 100G"`
  51. MachineCpu int `help:"Machine cpu count"`
  52. MachineMemory string `help:"Machine memory size, e.g. 1G"`
  53. MachineSku string `help:"Machine sku, e.g. 'ecs.c6.large'"`
  54. MachineHypervisor string `help:"Machine hypervisor, e.g. kvm, openstack"`
  55. }
  56. type K8SClusterCreateOptions struct {
  57. NAME string `help:"Name of cluster"`
  58. File string `help:"JSON file of create body"`
  59. // ClusterType string `help:"Cluster cluster type" choices:"default|serverless"`
  60. ResourceType string `help:"Cluster cluster type" choices:"host|guest"`
  61. // CloudType string `help:"Cluster cloud type" choices:"private|public|hybrid"`
  62. Mode string `help:"Cluster mode type" choices:"customize|import"`
  63. Provider string `help:"Cluster provider" choices:"onecloud|system"`
  64. ServiceCidr string `help:"Cluster service CIDR, e.g. 10.43.0.0/16"`
  65. ServiceDomain string `help:"Cluster service domain, e.g. cluster.local"`
  66. Vip string `help:"Cluster api server static loadbalancer vip"`
  67. Version string `help:"Cluster kubernetes version" choices:"v1.17.0|v1.19.0|v1.22.9"`
  68. ImageRepo string `help:"Image repository, e.g. registry-1.docker.io/yunion"`
  69. ImageRepoInsecure bool `help:"Image repostiory is insecure"`
  70. Vpc string `help:"Cluster nodes network vpc"`
  71. // AddMachineOptions include create machine options
  72. AddMachineOptions
  73. // Addons options
  74. EnableNativeIPAlloc bool `help:"Calico CNI plugin enable native ip allocation"`
  75. }
  76. func parseMachineDesc(
  77. desc string,
  78. disk string,
  79. netConf string,
  80. ncpu int,
  81. memorySize string,
  82. sku string,
  83. hypervisor string,
  84. ) (*MachineCreateOptions, error) {
  85. matchType := func(p string) bool {
  86. switch p {
  87. case "baremetal", "vm":
  88. return true
  89. default:
  90. return false
  91. }
  92. }
  93. matchRole := func(p string) bool {
  94. switch p {
  95. case "controlplane", "node":
  96. return true
  97. default:
  98. return false
  99. }
  100. }
  101. mo := new(MachineCreateOptions)
  102. for _, part := range strings.Split(desc, ":") {
  103. switch {
  104. case matchType(part):
  105. mo.Type = part
  106. case matchRole(part):
  107. mo.ROLE = part
  108. default:
  109. mo.Instance = part
  110. }
  111. }
  112. if mo.ROLE == "" {
  113. return nil, fmt.Errorf("Machine role is empty")
  114. }
  115. if mo.Type == "" {
  116. return nil, fmt.Errorf("Machine type is empty")
  117. }
  118. mo.Disk = disk
  119. mo.Cpu = ncpu
  120. mo.Memory = memorySize
  121. mo.Sku = sku
  122. mo.Net = netConf
  123. mo.Hypervisor = hypervisor
  124. return mo, nil
  125. }
  126. type K8SClusterAddonNetworkConfig struct {
  127. EnableNativeIPAlloc bool `json:"enable_native_ip_alloc"`
  128. }
  129. type K8SClusterAddonConfig struct {
  130. Network K8SClusterAddonNetworkConfig `json:"network"`
  131. }
  132. func (o K8SClusterCreateOptions) getAddonsConfig() (jsonutils.JSONObject, error) {
  133. conf := &K8SClusterAddonConfig{
  134. Network: K8SClusterAddonNetworkConfig{
  135. EnableNativeIPAlloc: o.EnableNativeIPAlloc,
  136. },
  137. }
  138. return jsonutils.Marshal(conf), nil
  139. }
  140. func (o K8SClusterCreateOptions) Params() (jsonutils.JSONObject, error) {
  141. if o.File != "" {
  142. content, err := os.ReadFile(o.File)
  143. if err != nil {
  144. return nil, errors.Wrapf(err, "read file: %s", o.File)
  145. }
  146. obj, err := jsonutils.Parse(content)
  147. if err != nil {
  148. return nil, errors.Wrapf(err, "parse file %s content: %s", o.File, content)
  149. }
  150. return obj, nil
  151. }
  152. params := jsonutils.NewDict()
  153. params.Add(jsonutils.NewString(o.NAME), "name")
  154. /*
  155. * if o.ClusterType != "" {
  156. * params.Add(jsonutils.NewString(o.ClusterType), "cluster_type")
  157. * }
  158. */
  159. if o.ResourceType != "" {
  160. params.Add(jsonutils.NewString(o.ResourceType), "resource_type")
  161. }
  162. /*
  163. * if o.CloudType != "" {
  164. * params.Add(jsonutils.NewString(o.CloudType), "cloud_type")
  165. * }
  166. */
  167. if o.Mode != "" {
  168. params.Add(jsonutils.NewString(o.Mode), "mode")
  169. }
  170. if o.Provider != "" {
  171. params.Add(jsonutils.NewString(o.Provider), "provider")
  172. }
  173. if o.ServiceCidr != "" {
  174. params.Add(jsonutils.NewString(o.ServiceCidr), "service_cidr")
  175. }
  176. if o.ServiceDomain != "" {
  177. params.Add(jsonutils.NewString(o.ServiceDomain), "service_domain")
  178. }
  179. if o.Vip != "" {
  180. params.Add(jsonutils.NewString(o.Vip), "vip")
  181. }
  182. if o.Vpc != "" {
  183. params.Add(jsonutils.NewString(o.Vpc), "vpc_id")
  184. }
  185. if o.Version != "" {
  186. params.Add(jsonutils.NewString(o.Version), "version")
  187. }
  188. imageRepo := jsonutils.NewDict()
  189. if o.ImageRepo != "" {
  190. imageRepo.Add(jsonutils.NewString(o.ImageRepo), "url")
  191. }
  192. if o.ImageRepoInsecure {
  193. imageRepo.Add(jsonutils.JSONTrue, "insecure")
  194. }
  195. machineObjs, err := o.AddMachineOptions.Params()
  196. if err != nil {
  197. return nil, err
  198. }
  199. params.Add(machineObjs, "machines")
  200. params.Add(imageRepo, "image_repository")
  201. addonsConf, err := o.getAddonsConfig()
  202. if err != nil {
  203. return nil, errors.Wrap(err, "get addons config")
  204. }
  205. params.Add(addonsConf, "addons_config")
  206. return params, nil
  207. }
  208. type KubeClusterImportOptions struct {
  209. NAME string `help:"Name of cluster"`
  210. KUBECONFIG string `help:"Cluster kubeconfig file path"`
  211. Distro string `help:"Kubernetes distribution, e.g. openshift"`
  212. Provider string `help:"Provider type" choices:"external|aliyun|qcloud|azure"`
  213. ResourceType string `help:"Node resource type" choices:"unknown|guest"`
  214. CloudKubeCluster string `help:"Cloud kube cluster id or name"`
  215. }
  216. func (o KubeClusterImportOptions) Params() (jsonutils.JSONObject, error) {
  217. kubeconfig, err := ioutil.ReadFile(o.KUBECONFIG)
  218. if err != nil {
  219. return nil, fmt.Errorf("Read kube config %q error: %v", o.KUBECONFIG, err)
  220. }
  221. params := jsonutils.NewDict()
  222. params.Add(jsonutils.NewString(o.NAME), "name")
  223. params.Add(jsonutils.NewString("import"), "mode")
  224. params.Add(jsonutils.NewString(o.Provider), "provider")
  225. if o.ResourceType == "" {
  226. o.ResourceType = "unknown"
  227. }
  228. params.Add(jsonutils.NewString(o.ResourceType), "resource_type")
  229. if o.CloudKubeCluster != "" {
  230. params.Add(jsonutils.NewString(o.CloudKubeCluster), "external_cluster_id")
  231. }
  232. importData := jsonutils.NewDict()
  233. importData.Add(jsonutils.NewString(string(kubeconfig)), "kubeconfig")
  234. if o.Distro != "" {
  235. importData.Add(jsonutils.NewString(o.Distro), "distribution")
  236. }
  237. params.Add(importData, "import_data")
  238. return params, nil
  239. }
  240. type ClusterGCOpts struct{}
  241. func (o ClusterGCOpts) Params() (jsonutils.JSONObject, error) {
  242. return nil, nil
  243. }
  244. type IdentOptions struct {
  245. ID string `help:"ID or name of the model"`
  246. }
  247. func (o IdentOptions) Params() (jsonutils.JSONObject, error) {
  248. return nil, nil
  249. }
  250. func (o IdentOptions) GetId() string {
  251. return o.ID
  252. }
  253. type ClusterPurgeOptions struct {
  254. IdentOptions
  255. Force bool `help:"force purge"`
  256. }
  257. func (o ClusterPurgeOptions) Params() (jsonutils.JSONObject, error) {
  258. params := jsonutils.NewDict()
  259. force := jsonutils.JSONFalse
  260. if o.Force {
  261. force = jsonutils.JSONTrue
  262. }
  263. params.Add(force, "force")
  264. return params, nil
  265. }
  266. type ClusterSyncOptions struct {
  267. IdentOptions
  268. Force bool `help:"force sync"`
  269. }
  270. func (o ClusterSyncOptions) Params() (jsonutils.JSONObject, error) {
  271. param := jsonutils.NewDict()
  272. if o.Force {
  273. param.Add(jsonutils.JSONTrue, "force")
  274. }
  275. return param, nil
  276. }
  277. type ClusterDeployOptions struct {
  278. IdentOptions
  279. Force bool `help:"force deploy"`
  280. Action string `help:"deploy action" choices:"run|upgrade-master-config"`
  281. }
  282. func (o ClusterDeployOptions) Params() (jsonutils.JSONObject, error) {
  283. param := jsonutils.NewDict()
  284. if o.Force {
  285. param.Add(jsonutils.JSONTrue, "force")
  286. }
  287. if o.Action != "" {
  288. param.Add(jsonutils.NewString(o.Action), "action")
  289. }
  290. return param, nil
  291. }
  292. type ClusterK8sVersions struct {
  293. PROVIDER string `help:"cluster provider" choices:"system|onecloud"`
  294. }
  295. type ClusterCheckOptions struct{}
  296. type IdentsOptions struct {
  297. ID []string `help:"ID of models to operate"`
  298. }
  299. func (o IdentsOptions) GetIds() []string {
  300. return o.ID
  301. }
  302. func (o IdentsOptions) Params() (jsonutils.JSONObject, error) {
  303. return nil, nil
  304. }
  305. type ClusterDeleteOptions struct {
  306. IdentsOptions
  307. }
  308. type KubeClusterAddMachinesOptions struct {
  309. IdentOptions
  310. AddMachineOptions
  311. }
  312. func (o AddMachineOptions) Params() (jsonutils.JSONObject, error) {
  313. machineObjs := jsonutils.NewArray()
  314. if len(o.Machine) == 0 {
  315. return machineObjs, nil
  316. }
  317. for _, m := range o.Machine {
  318. machine, err := parseMachineDesc(m, o.MachineDisk, o.MachineNet, o.MachineCpu, o.MachineMemory, o.MachineSku, o.MachineHypervisor)
  319. if err != nil {
  320. return nil, err
  321. }
  322. params, err := machine.Params()
  323. if err != nil {
  324. return nil, err
  325. }
  326. machineObjs.Add(params)
  327. }
  328. return machineObjs, nil
  329. }
  330. func (o KubeClusterAddMachinesOptions) Params() (jsonutils.JSONObject, error) {
  331. params := jsonutils.NewDict()
  332. machinesArray, err := o.AddMachineOptions.Params()
  333. if err != nil {
  334. return nil, err
  335. }
  336. params.Add(machinesArray, "machines")
  337. return params, nil
  338. }
  339. type KubeClusterDeleteMachinesOptions struct {
  340. IdentOptions
  341. Machines []string `help:"Machine id or name"`
  342. }
  343. func (o KubeClusterDeleteMachinesOptions) Params() (jsonutils.JSONObject, error) {
  344. params := jsonutils.NewDict()
  345. machinesArray := jsonutils.NewArray()
  346. for _, m := range o.Machines {
  347. machinesArray.Add(jsonutils.NewString(m))
  348. }
  349. params.Add(machinesArray, "machines")
  350. return params, nil
  351. }
  352. type ClusterComponentOptions struct {
  353. IdentOptions
  354. }
  355. func (o ClusterComponentOptions) Params(typ string) *jsonutils.JSONDict {
  356. params := jsonutils.NewDict()
  357. params.Add(jsonutils.NewString(typ), "type")
  358. return params
  359. }
  360. type ClusterComponentType struct {
  361. ClusterComponentOptions
  362. TYPE string `help:"Component type"`
  363. }
  364. type ClusterComponentTypeOptions struct {
  365. ClusterComponentType
  366. AsHelmValues bool `help:"As helm values config" json:"as_helm_values"`
  367. }
  368. type ClusterEnableComponentCephCSIOpt struct {
  369. ClusterComponentOptions
  370. ClusterId string `help:"Ceph cluster id"`
  371. Monitor []string `help:"Ceph monitor, format is 'ip:port'"`
  372. }
  373. func (o ClusterEnableComponentCephCSIOpt) Params() (*jsonutils.JSONDict, error) {
  374. params := o.ClusterComponentOptions.Params("cephCSI")
  375. conf := jsonutils.NewDict()
  376. clusterConfs := jsonutils.NewArray()
  377. clusterConf := jsonutils.NewDict()
  378. clusterConf.Add(jsonutils.NewString(o.ClusterId), "clusterId")
  379. mons := jsonutils.NewArray()
  380. for _, m := range o.Monitor {
  381. mons.Add(jsonutils.NewString(m))
  382. }
  383. clusterConf.Add(mons, "monitors")
  384. clusterConfs.Add(clusterConf)
  385. conf.Add(clusterConfs, "config")
  386. params.Add(conf, "cephCSI")
  387. return params, nil
  388. }
  389. type ClusterComponentStorage struct {
  390. Enabled bool `help:"Enable this storage" json:"enabled"`
  391. SizeMB int `help:"Persistent storage size MB" json:"sizeMB"`
  392. ClassName string `help:"Storage class name" json:"storageClassName"`
  393. }
  394. type ClusterComponentMonitorGrafanaTlsOpt struct {
  395. CertificateFile string `help:"TLS certificate file" json:"-"`
  396. KeyFile string `help:"TLS key file" json:"-"`
  397. }
  398. type ClusterComponentMonitorGrafanaOAuth struct {
  399. Enabled bool `help:"Enable oauth setting" json:"enabled"`
  400. ClientId string `help:"Client id" json:"clientId"`
  401. ClientSecret string `help:"Client secret" json:"clientSecret"`
  402. Scopes string `help:"Client scopes" json:"scopes"`
  403. AuthURL string `help:"Auth url" json:"authURL"`
  404. TokenURL string `help:"Token url" json:"tokenURL"`
  405. ApiURL string `help:"API URL" json:"apiURL"`
  406. AllowedDomains string `help:"Allowed domains" json:"allowedDomains"`
  407. AllowSignUp bool `help:"Allow sign up" json:"allowSignUp"`
  408. RoleAttributePath string `help:"Role attribute path" json:"roleAttributePath"`
  409. }
  410. type ClusterComponentMonitorGrafanaDB struct {
  411. Host string `help:"db host" json:"host"`
  412. Port int `help:"db port" json:"port"`
  413. Database string `help:"db name" json:"database"`
  414. Username string `help:"db username" json:"username"`
  415. Password string `help:"db password" json:"password"`
  416. }
  417. type ClusterComponentMonitorGrafana struct {
  418. Disable bool `help:"Disable grafana component" json:"disable"`
  419. AdminUser string `help:"Grafana admin user" default:"admin" json:"adminUser"`
  420. AdminPassword string `help:"Grafana admin user password" json:"adminPassword"`
  421. Storage ClusterComponentStorage `help:"Storage setting"`
  422. PublicAddress string `help:"Grafana expose public IP address or domain hostname" json:"publicAddress"`
  423. Host string `help:"Grafana ingress host domain name" json:"host"`
  424. EnforceDomain bool `help:"Enforce use domain" json:"enforceDomain"`
  425. Tls ClusterComponentMonitorGrafanaTlsOpt `help:"TLS setting"`
  426. DisableSubpath bool `help:"Disable grafana subpath" json:"disableSubpath"`
  427. Subpath string `help:"Grafana subpath" default:"grafana" json:"subpath"`
  428. EnableThanosQuery bool `help:"Enable thanos query datasource" json:"enableThanosQueryDataSource"`
  429. Oauth ClusterComponentMonitorGrafanaOAuth `help:"OAuth config" json:"oauth"`
  430. Db ClusterComponentMonitorGrafanaDB `help:"db config" json:"db"`
  431. }
  432. type ObjectStoreConfig struct {
  433. Bucket string `json:"bucket"`
  434. Endpoint string `json:"endpoint"`
  435. AccessKey string `json:"access_key"`
  436. SecretKey string `json:"secret_key"`
  437. Insecure bool `json:"insecure"`
  438. }
  439. type ClusterComponentMonitorLoki struct {
  440. Disable bool `help:"Disable loki component" json:"disable"`
  441. Storage ClusterComponentStorage `help:"Storage setting" json:"storage"`
  442. ObjectStoreConfig ObjectStoreConfig `json:"objectStoreConfig"`
  443. }
  444. type MonitorPrometheusThanosSidecar struct {
  445. ObjectStoreConfig ObjectStoreConfig `json:"objectStoreConfig"`
  446. }
  447. type ClusterComponentMonitorPrometheus struct {
  448. Disable bool `help:"Disable prometheus component" json:"disable"`
  449. Storage ClusterComponentStorage `help:"Storage setting" json:"storage"`
  450. Thanos MonitorPrometheusThanosSidecar `json:"thanosSidecar"`
  451. }
  452. type ClusterComponentMonitorPromtail struct {
  453. Disable bool `help:"Disable promtail component" json:"disable"`
  454. }
  455. type ClusterComponentMonitorSetting struct {
  456. Grafana ClusterComponentMonitorGrafana `help:"Grafana setting" json:"grafana"`
  457. Loki ClusterComponentMonitorLoki `help:"Loki setting" json:"loki"`
  458. Prometheus ClusterComponentMonitorPrometheus `help:"Prometheus setting" json:"prometheus"`
  459. Promtail ClusterComponentMonitorPromtail `help:"Promtail setting" json:"promtail"`
  460. }
  461. type ClusterEnableComponentMonitorOpt struct {
  462. ClusterComponentOptions
  463. ClusterComponentMonitorSetting
  464. }
  465. func (o ClusterEnableComponentMonitorOpt) Params() (jsonutils.JSONObject, error) {
  466. params := o.ClusterComponentOptions.Params("monitor")
  467. setting := jsonutils.Marshal(o.ClusterComponentMonitorSetting)
  468. params.Add(setting, "monitor")
  469. certFile := o.Grafana.Tls.CertificateFile
  470. keyFile := o.Grafana.Tls.KeyFile
  471. if certFile != "" {
  472. cert, err := ioutil.ReadFile(certFile)
  473. if err != nil {
  474. return nil, errors.Wrap(err, "read grafana tls certFile")
  475. }
  476. key, err := ioutil.ReadFile(keyFile)
  477. if err != nil {
  478. return nil, errors.Wrap(err, "read grafana tls keyFile")
  479. }
  480. params.Add(jsonutils.NewString(string(cert)), "monitor", "grafana", "tlsKeyPair", "certificate")
  481. params.Add(jsonutils.NewString(string(key)), "monitor", "grafana", "tlsKeyPair", "key")
  482. }
  483. return params, nil
  484. }
  485. type ClusterComponentFluentBitBackendCommon struct {
  486. Enabled bool `help:"Enable this component"`
  487. }
  488. func (o ClusterComponentFluentBitBackendCommon) Params() (*jsonutils.JSONDict, error) {
  489. ret := jsonutils.NewDict()
  490. if o.Enabled {
  491. ret.Add(jsonutils.JSONTrue, "enabled")
  492. }
  493. return ret, nil
  494. }
  495. type ClusterComponentFluentBitBackendTLS struct {
  496. Tls bool `help:"Enable TLS support"`
  497. TlsVerify bool `help:"Enable TLS validation"`
  498. }
  499. func (o ClusterComponentFluentBitBackendTLS) Params() (*jsonutils.JSONDict, error) {
  500. ret := jsonutils.NewDict()
  501. if o.Tls {
  502. ret.Add(jsonutils.JSONTrue, "tls")
  503. }
  504. if o.TlsVerify {
  505. ret.Add(jsonutils.JSONTrue, "tlsVerify")
  506. }
  507. return ret, nil
  508. }
  509. type ClusterComponentFluentBitBackendES struct {
  510. ClusterComponentFluentBitBackendCommon
  511. ClusterComponentFluentBitBackendTLS
  512. Host string `help:"IP address or hostname of the target Elasticsearch instance"`
  513. Port int `help:"TCP port of the target Elasticsearch instance" default:"9200"`
  514. Index string `help:"Elastic idnex name" default:"fluentbit"`
  515. Type string `help:"Elastic type name" default:"flb_type"`
  516. HTTPUser string `help:"Optional username credential for Elastic X-Pack access"`
  517. HTTPPasswd string `help:"Password for user defined in HTTPUser"`
  518. }
  519. func (o ClusterComponentFluentBitBackendES) Params() (*jsonutils.JSONDict, error) {
  520. ret, err := o.ClusterComponentFluentBitBackendCommon.Params()
  521. if err != nil {
  522. return nil, err
  523. }
  524. if o.Host == "" {
  525. return nil, fmt.Errorf("host must specified")
  526. }
  527. ret.Add(jsonutils.NewString(o.Host), "host")
  528. ret.Add(jsonutils.NewInt(int64(o.Port)), "port")
  529. ret.Add(jsonutils.NewString(o.Index), "index")
  530. ret.Add(jsonutils.NewString(o.Type), "type")
  531. if o.HTTPUser != "" {
  532. ret.Add(jsonutils.NewString(o.HTTPUser), "httpUser")
  533. ret.Add(jsonutils.NewString(o.HTTPPasswd), "httpPassword")
  534. }
  535. tlsParams, err := o.ClusterComponentFluentBitBackendTLS.Params()
  536. if err != nil {
  537. return nil, err
  538. }
  539. ret.Update(tlsParams)
  540. return ret, nil
  541. }
  542. type ClusterComponentFluentBitBackendKafka struct {
  543. ClusterComponentFluentBitBackendCommon
  544. Brokers string `help:"Single of multiple list of Kafka Brokers, e.g: 192.168.1.3:9092, 192.168.1.4:9092"`
  545. Topics string `help:"Single entry or list of topics separated by comma (,) that Fluent Bit will use to send messages to Kafka" default:"fluent-bit"`
  546. TopicKey string `help:"If multiple Topics exists, the value of TopicKey in the record will indicate the topic to use."`
  547. }
  548. func (o ClusterComponentFluentBitBackendKafka) Params() (*jsonutils.JSONDict, error) {
  549. if o.Brokers == "" {
  550. return nil, fmt.Errorf("brokers is empty")
  551. }
  552. brokers := strings.Split(o.Brokers, ",")
  553. if len(brokers) == 0 {
  554. return nil, fmt.Errorf("invaild brokers %s", o.Brokers)
  555. }
  556. ret, err := o.ClusterComponentFluentBitBackendCommon.Params()
  557. if err != nil {
  558. return nil, err
  559. }
  560. ret.Add(jsonutils.NewStringArray(brokers), "brokers")
  561. topics := strings.Split(o.Topics, ",")
  562. if len(topics) == 0 {
  563. return nil, fmt.Errorf("invalid topics %s", o.Topics)
  564. }
  565. ret.Add(jsonutils.NewStringArray(topics), "topics")
  566. if len(o.TopicKey) != 0 {
  567. ret.Add(jsonutils.NewString(o.TopicKey), "topicKey")
  568. }
  569. return ret, nil
  570. }
  571. type ClusterComponentFluentBitBackend struct {
  572. Es ClusterComponentFluentBitBackendES `help:"Elasticsearch setting"`
  573. Kafka ClusterComponentFluentBitBackendKafka `help:"Kafka setting"`
  574. }
  575. func (o ClusterComponentFluentBitBackend) Params() (*jsonutils.JSONDict, error) {
  576. ret := jsonutils.NewDict()
  577. if o.Es.Enabled {
  578. es, err := o.Es.Params()
  579. if err != nil {
  580. return nil, errors.Wrap(err, "es config")
  581. }
  582. ret.Add(es, "es")
  583. }
  584. if o.Kafka.Enabled {
  585. kafka, err := o.Kafka.Params()
  586. if err != nil {
  587. return nil, errors.Wrap(err, "kafka config")
  588. }
  589. ret.Add(kafka, "kafka")
  590. }
  591. return ret, nil
  592. }
  593. type ClusterComponentFluentBitSetting struct {
  594. Backend ClusterComponentFluentBitBackend
  595. }
  596. func (o ClusterComponentFluentBitSetting) Params() (*jsonutils.JSONDict, error) {
  597. backend, err := o.Backend.Params()
  598. if err != nil {
  599. return nil, errors.Wrap(err, "backend config")
  600. }
  601. ret := jsonutils.NewDict()
  602. ret.Add(backend, "backend")
  603. return ret, nil
  604. }
  605. type ClusterEnableComponentFluentBitOpt struct {
  606. ClusterComponentOptions
  607. ClusterComponentFluentBitSetting
  608. }
  609. func (o ClusterEnableComponentFluentBitOpt) Params() (*jsonutils.JSONDict, error) {
  610. params := o.ClusterComponentOptions.Params("fluentbit")
  611. setting, err := o.ClusterComponentFluentBitSetting.Params()
  612. if err != nil {
  613. return nil, err
  614. }
  615. params.Add(setting, "fluentbit")
  616. return params, nil
  617. }
  618. type ClusterDisableComponent struct {
  619. ClusterComponentOptions
  620. TYPE string `help:"component type"`
  621. }
  622. func (o ClusterDisableComponent) Params() *jsonutils.JSONDict {
  623. p := o.ClusterComponentOptions.Params(o.TYPE)
  624. return p
  625. }
  626. type ClusterUpdateComponentCephCSIOpt struct {
  627. ClusterComponentOptions
  628. ClusterId string `help:"Ceph cluster id"`
  629. Monitor []string `help:"Ceph monitor, format is 'ip:port'"`
  630. }
  631. func (o ClusterUpdateComponentCephCSIOpt) Params() (*jsonutils.JSONDict, error) {
  632. params := o.ClusterComponentOptions.Params("cephCSI")
  633. conf := jsonutils.NewDict()
  634. clusterConfs := jsonutils.NewArray()
  635. clusterConf := jsonutils.NewDict()
  636. if o.ClusterId != "" {
  637. clusterConf.Add(jsonutils.NewString(o.ClusterId), "clusterId")
  638. }
  639. mons := jsonutils.NewArray()
  640. for _, m := range o.Monitor {
  641. mons.Add(jsonutils.NewString(m))
  642. }
  643. if mons.Length() != 0 {
  644. clusterConf.Add(mons, "monitors")
  645. }
  646. clusterConfs.Add(clusterConf)
  647. conf.Add(clusterConfs, "config")
  648. params.Add(conf, "cephCSI")
  649. return params, nil
  650. }
  651. type ClusterComponentMinioSetting struct {
  652. Mode string `help:"MinIO mode" choices:"standalone|distributed" json:"mode"`
  653. Replicas int `help:"MinIO replicas" default:"1" json:"replicas"`
  654. DrivesPerNode int `help:"MinIO drives per node" default:"1" json:"drivesPerNode"`
  655. AccessKey string `help:"MinIO admin access key" json:"accessKey"`
  656. SecretKey string `help:"MinIO admin secret key" json:"secretKey"`
  657. MountPath string `help:"MinIO export mount path" json:"mountPath"`
  658. Storage ClusterComponentStorage `help:"Storage setting" json:"storage"`
  659. }
  660. type ClusterEnableComponentMinioBaseOpt struct {
  661. ClusterComponentOptions
  662. ClusterComponentMinioSetting
  663. }
  664. func (o ClusterEnableComponentMinioBaseOpt) Params(typ string) (jsonutils.JSONObject, error) {
  665. params := o.ClusterComponentOptions.Params(typ)
  666. setting := jsonutils.Marshal(o.ClusterComponentMinioSetting)
  667. params.Add(setting, typ)
  668. return params, nil
  669. }
  670. type ClusterEnableComponentMinioOpt struct {
  671. ClusterEnableComponentMinioBaseOpt
  672. }
  673. func (o ClusterEnableComponentMinioOpt) Params() (jsonutils.JSONObject, error) {
  674. return o.ClusterEnableComponentMinioBaseOpt.Params("minio")
  675. }
  676. type ClusterEnableComponentMonitorMinioOpt struct {
  677. ClusterEnableComponentMinioBaseOpt
  678. }
  679. func (o ClusterEnableComponentMonitorMinioOpt) Params() (jsonutils.JSONObject, error) {
  680. return o.ClusterEnableComponentMinioBaseOpt.Params("monitorMinio")
  681. }
  682. type ComponentThanosDnsDiscovery struct {
  683. SidecarsService string `help:"Sidecars service name to discover them using DNS discovery" default:"prometheus-operated" json:"sidecarsService"`
  684. SidecarsNamespace string `help:"Sidecars namespace to discover them using DNS discovery" default:"onecloud-monitoring" json:"sidecarsNamespace"`
  685. }
  686. type ComponentThanosQuery struct {
  687. DnsDiscovery ComponentThanosDnsDiscovery `json:"dnsDiscovery"`
  688. Stores []string `help:"Statically configure store APIs to connect with Thanos" json:"stores"`
  689. }
  690. type ComponentThanosCompactor struct {
  691. Storage ClusterComponentStorage `json:"storage"`
  692. }
  693. type ComponentThanosStoregateway struct {
  694. Storage ClusterComponentStorage `json:"storage"`
  695. }
  696. type ClusterComponentThanosSetting struct {
  697. ClusterDomain string `json:"clusterDomain"`
  698. ObjectStoreConfig ObjectStoreConfig `json:"objectStoreConfig"`
  699. Query ComponentThanosQuery `json:"query"`
  700. Store ComponentThanosStoregateway `json:"store"`
  701. Compactor ComponentThanosCompactor `json:"compactor"`
  702. }
  703. type ClusterEnableComponentThanosOpt struct {
  704. ClusterComponentOptions
  705. ClusterComponentThanosSetting
  706. }
  707. func (o ClusterEnableComponentThanosOpt) Params() (jsonutils.JSONObject, error) {
  708. params := o.ClusterComponentOptions.Params("thanos")
  709. setting := jsonutils.Marshal(o.ClusterComponentThanosSetting)
  710. params.Add(setting, "thanos")
  711. return params, nil
  712. }
  713. type ClusterGetAddonsOpt struct {
  714. IdentOptions
  715. EnableNativeIPAlloc bool `json:"enable_native_ip_alloc"`
  716. }
  717. func (o ClusterGetAddonsOpt) Params() (jsonutils.JSONObject, error) {
  718. params := jsonutils.NewDict()
  719. params.Add(jsonutils.NewBool(o.EnableNativeIPAlloc), "enable_native_ip_alloc")
  720. return params, nil
  721. }
  722. type ClusterSetExtraConfigOpt struct {
  723. IdentOptions
  724. DockerRegistryMirrors []string `json:"docker_registry_mirrors"`
  725. DockerInsecureRegistries []string `json:"docker_insecure_registries"`
  726. }
  727. func (o ClusterSetExtraConfigOpt) Params() (jsonutils.JSONObject, error) {
  728. return jsonutils.Marshal(o), nil
  729. }