This is an automated email from the ASF dual-hosted git repository.
zhongxjian pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/dubbo-kubernetes.git
The following commit(s) were added to refs/heads/master by this push:
new def124bb [horus] Kubernetes multi cluster initialization (#330)
def124bb is described below
commit def124bbf974e203243afd6614a381e13655f45f
Author: mfordjody <[email protected]>
AuthorDate: Tue Sep 10 13:01:39 2024 +0800
[horus] Kubernetes multi cluster initialization (#330)
License Check
---
app/horus/basic/config/file.go | 10 +++--
app/horus/{core => basic}/db/db.go | 0
app/horus/{core => basic}/db/sql.tpl | 0
app/horus/cmd/main.go | 2 +-
app/horus/core/horuser/horuser.go | 71 ++++++++++++++++++++++++++++++++++++
5 files changed, 78 insertions(+), 5 deletions(-)
diff --git a/app/horus/basic/config/file.go b/app/horus/basic/config/file.go
index 5b2767a6..93b37c2b 100644
--- a/app/horus/basic/config/file.go
+++ b/app/horus/basic/config/file.go
@@ -16,10 +16,12 @@
package config
type Config struct {
- Address string `yaml:"address"`
- Mysql *MysqlConfiguration `yaml:"mysql"`
- DingTalk *DingTalkConfiguration `yaml:"dingTalk"`
- Slack *SlackConfiguration `yaml:"slack"`
+ Address string `yaml:"address"`
+ Mysql *MysqlConfiguration `yaml:"mysql"`
+ DingTalk *DingTalkConfiguration `yaml:"dingTalk"`
+ Slack *SlackConfiguration `yaml:"slack"`
+ KubeMultiple map[string]string `yaml:"kubeMultiple"`
+ KubeTimeSecond int64
}
type MysqlConfiguration struct {
diff --git a/app/horus/core/db/db.go b/app/horus/basic/db/db.go
similarity index 100%
rename from app/horus/core/db/db.go
rename to app/horus/basic/db/db.go
diff --git a/app/horus/core/db/sql.tpl b/app/horus/basic/db/sql.tpl
similarity index 100%
rename from app/horus/core/db/sql.tpl
rename to app/horus/basic/db/sql.tpl
diff --git a/app/horus/cmd/main.go b/app/horus/cmd/main.go
index f24dd8f8..b5c05a70 100644
--- a/app/horus/cmd/main.go
+++ b/app/horus/cmd/main.go
@@ -19,7 +19,7 @@ import (
"context"
"flag"
"github.com/apache/dubbo-kubernetes/app/horus/basic/config"
- "github.com/apache/dubbo-kubernetes/app/horus/core/db"
+ "github.com/apache/dubbo-kubernetes/app/horus/basic/db"
"github.com/prometheus/client_golang/prometheus/promhttp"
"k8s.io/klog"
"net/http"
diff --git a/app/horus/core/horuser/horuser.go
b/app/horus/core/horuser/horuser.go
new file mode 100644
index 00000000..a5afff12
--- /dev/null
+++ b/app/horus/core/horuser/horuser.go
@@ -0,0 +1,71 @@
+// Licensed to the Apache Software Foundation (ASF) under one or more
+// contributor license agreements. See the NOTICE file distributed with
+// this work for additional information regarding copyright ownership.
+// The ASF licenses this file to You under the Apache License, Version 2.0
+// (the "License"); you may not use this file except in compliance with
+// the License. You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package horuser
+
+import (
+ "context"
+ "github.com/apache/dubbo-kubernetes/app/horus/basic/config"
+ "k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset"
+ "k8s.io/client-go/rest"
+ "k8s.io/client-go/tools/clientcmd"
+ "k8s.io/klog/v2"
+ "time"
+)
+
+type Horuser struct {
+ cc *config.Config
+ kubeClientMap map[string]*clientset.Clientset
+}
+
+func NewHoruser(c *config.Config) *Horuser {
+ hr := &Horuser{
+ cc: c,
+ kubeClientMap: map[string]*clientset.Clientset{},
+ }
+ i := 1
+ n := len(c.KubeMultiple)
+ for clusterName, km := range c.KubeMultiple {
+ kcfg, err := k8sBuildConfig(km)
+ if err != nil {
+ klog.Errorf("NewHoruser k8sBuildConfig err:%v\n
name:%v\n", err, clusterName)
+ }
+ km := clientset.NewForConfigOrDie(kcfg)
+ hr.kubeClientMap[clusterName] = km
+ klog.Infof("NewHoruser k8sBuildConfig success.%d/%d
KubeMultipleCluster: %v", n, i, clusterName)
+ i++
+ }
+ return hr
+}
+
+func k8sBuildConfig(kubeconfig string) (*rest.Config, error) {
+ if kubeconfig != "" {
+ cfg, err := clientcmd.BuildConfigFromFlags("", kubeconfig)
+ if err != nil {
+ return nil, err
+ }
+ return cfg, err
+ }
+ cfg, err := rest.InClusterConfig()
+ if err != nil {
+ return nil, err
+ }
+ return cfg, err
+}
+
+func (h *Horuser) GetK8sContext() (context.Context, context.CancelFunc) {
+ return context.WithTimeout(context.Background(),
time.Duration(h.cc.KubeTimeSecond)*time.Second)
+
+}