cluster.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. "context"
  17. "sync"
  18. "time"
  19. "k8s.io/client-go/kubernetes"
  20. "yunion.io/x/jsonutils"
  21. "yunion.io/x/log"
  22. "yunion.io/x/onecloud/pkg/mcclient/auth"
  23. kubeserver "yunion.io/x/onecloud/pkg/mcclient/modules/k8s"
  24. )
  25. type SKubeClusterManager struct {
  26. k8sConfigLock *sync.RWMutex
  27. k8sConfig string
  28. interval time.Duration
  29. region string
  30. }
  31. func NewKubeClusterManager(region string, interval time.Duration) *SKubeClusterManager {
  32. return &SKubeClusterManager{
  33. k8sConfigLock: new(sync.RWMutex),
  34. interval: interval,
  35. region: region,
  36. }
  37. }
  38. func (man *SKubeClusterManager) GetK8sConfig() string {
  39. man.k8sConfigLock.RLock()
  40. defer man.k8sConfigLock.RUnlock()
  41. return man.k8sConfig
  42. }
  43. func (man *SKubeClusterManager) GetK8sClient() (*kubernetes.Clientset, error) {
  44. cli, err := NewClientByContent([]byte(man.GetK8sConfig()), nil)
  45. if err != nil {
  46. log.Warningf("Init kubernetes client error: %v", err)
  47. return nil, err
  48. }
  49. return cli, nil
  50. }
  51. func (man *SKubeClusterManager) Start() {
  52. go man.startRefreshKubeConfig()
  53. }
  54. func (man *SKubeClusterManager) setK8sConfig(conf string) {
  55. man.k8sConfigLock.Lock()
  56. defer man.k8sConfigLock.Unlock()
  57. man.k8sConfig = conf
  58. }
  59. func (man *SKubeClusterManager) isK8sHealthy() bool {
  60. if man.GetK8sConfig() == "" {
  61. return false
  62. }
  63. cli, err := man.GetK8sClient()
  64. if err != nil {
  65. return false
  66. }
  67. _, err = cli.Discovery().ServerVersion()
  68. if err != nil {
  69. log.Errorf("Discovery k8s version: %v", err)
  70. return false
  71. }
  72. return true
  73. }
  74. func (man *SKubeClusterManager) startRefreshKubeConfig() {
  75. man.refreshKubeConfig()
  76. tick := time.Tick(man.interval)
  77. for {
  78. select {
  79. case <-tick:
  80. man.refreshKubeConfig()
  81. }
  82. }
  83. }
  84. func (man *SKubeClusterManager) refreshKubeConfig() {
  85. if man.isK8sHealthy() {
  86. return
  87. }
  88. kubeConfig, err := man.getKubeClusterConfig()
  89. if err != nil {
  90. log.Errorf("Get default k8s config from kube server error: %v", err)
  91. }
  92. man.setK8sConfig(kubeConfig)
  93. }
  94. func (man *SKubeClusterManager) getKubeClusterConfig() (string, error) {
  95. session := auth.GetAdminSession(context.Background(), man.region)
  96. params := jsonutils.NewDict()
  97. params.Add(jsonutils.JSONTrue, "directly")
  98. ret, err := kubeserver.KubeClusters.PerformAction(session, "default", "generate-kubeconfig", params)
  99. if err != nil {
  100. return "", err
  101. }
  102. return ret.GetString("kubeconfig")
  103. }