Skip to content

Commit 06bb18d

Browse files
authored
✨ feat: support task k8s provider (#173)
1 parent 7897d9a commit 06bb18d

File tree

7 files changed

+496
-110
lines changed

7 files changed

+496
-110
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package system
2+
3+
import (
4+
"runtime"
5+
6+
"github.com/mss-boot-io/mss-boot/pkg/migration"
7+
"gorm.io/gorm"
8+
9+
"github.com/mss-boot-io/mss-boot-admin/models"
10+
)
11+
12+
func init() {
13+
_, fileName, _, _ := runtime.Caller(0)
14+
migration.Migrate.SetVersion(migration.GetFilename(fileName), _1724396388009Migrate)
15+
}
16+
17+
func _1724396388009Migrate(db *gorm.DB, version string) error {
18+
return db.Transaction(func(tx *gorm.DB) error {
19+
20+
err := tx.Migrator().AutoMigrate(
21+
new(models.Task),
22+
)
23+
if err != nil {
24+
return err
25+
}
26+
27+
return migration.Migrate.CreateVersion(tx, version)
28+
})
29+
}

config/clusters.go

Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
package config
2+
3+
import (
4+
"context"
5+
"log/slog"
6+
"os"
7+
"path/filepath"
8+
"time"
9+
10+
"github.com/aws/aws-sdk-go-v2/config"
11+
"github.com/aws/aws-sdk-go-v2/service/eks"
12+
"k8s.io/client-go/dynamic"
13+
"k8s.io/client-go/kubernetes"
14+
"k8s.io/client-go/rest"
15+
"k8s.io/client-go/tools/clientcmd"
16+
clientcmdapi "k8s.io/client-go/tools/clientcmd/api"
17+
"k8s.io/client-go/util/homedir"
18+
19+
"github.com/mss-boot-io/mss-boot-admin/pkg"
20+
)
21+
22+
/*
23+
* @Author: lwnmengjing<lwnmengjing@qq.com>
24+
* @Date: 2024/8/22 16:59:33
25+
* @Last Modified by: lwnmengjing<lwnmengjing@qq.com>
26+
* @Last Modified time: 2024/8/22 16:59:33
27+
*/
28+
29+
type Clusters []*Cluster
30+
31+
func (e Clusters) Init() {
32+
for i := range e {
33+
(e)[i].Init()
34+
}
35+
}
36+
37+
func (e Clusters) GetDynamicClient(name string) *dynamic.DynamicClient {
38+
for _, cluster := range e {
39+
if cluster.Name == name {
40+
return cluster.GetDynamicClient()
41+
}
42+
}
43+
return nil
44+
}
45+
46+
func (e Clusters) GetClientSet(name string) *kubernetes.Clientset {
47+
for _, cluster := range e {
48+
if cluster.Name == name {
49+
return cluster.GetClientSet()
50+
}
51+
}
52+
return nil
53+
}
54+
55+
func (e Clusters) GetConfig(name string) *rest.Config {
56+
for _, cluster := range e {
57+
if cluster.Name == name {
58+
return cluster.GetConfig()
59+
}
60+
}
61+
return nil
62+
}
63+
64+
type Cluster struct {
65+
Name string `yaml:"name" json:"name"`
66+
KubeConfig string `yaml:"kubeConfig" json:"kubeConfig"`
67+
KubeConfigPath string `yaml:"kubeConfigPath" json:"kubeConfigPath"`
68+
EKS *EKSCluster `yaml:"eks" json:"eks"`
69+
clientSet *kubernetes.Clientset
70+
config *rest.Config
71+
dynamicClient *dynamic.DynamicClient
72+
}
73+
74+
func (e *Cluster) GetDynamicClient() *dynamic.DynamicClient {
75+
return e.dynamicClient
76+
}
77+
78+
func (e *Cluster) GetClientSet() *kubernetes.Clientset {
79+
return e.clientSet
80+
}
81+
82+
func (e *Cluster) GetConfig() *rest.Config {
83+
return e.config
84+
}
85+
86+
func (e *Cluster) Init() {
87+
var err error
88+
var apiConfig *clientcmdapi.Config
89+
if e.EKS != nil {
90+
apiConfig, err = e.EKS.GetKubeconfig()
91+
if err != nil {
92+
slog.Error("Failed to get kubeconfig", "err", err)
93+
os.Exit(-1)
94+
}
95+
if e.Name == "" {
96+
e.Name = e.EKS.Name
97+
}
98+
}
99+
if apiConfig == nil && e.KubeConfigPath == "" && e.KubeConfig == "" {
100+
e.config, err = rest.InClusterConfig()
101+
if err != nil {
102+
slog.Error("Failed to get in cluster config", "err", err)
103+
os.Exit(-1)
104+
}
105+
}
106+
if apiConfig == nil && e.config == nil {
107+
if e.KubeConfig != "" {
108+
apiConfig, err = clientcmd.Load([]byte(e.KubeConfig))
109+
if err != nil {
110+
slog.Error("Failed to load kube config", "err", err)
111+
os.Exit(-1)
112+
}
113+
} else {
114+
if e.KubeConfigPath == "" {
115+
e.KubeConfigPath = filepath.Join(homedir.HomeDir(), ".kube", "config")
116+
}
117+
apiConfig, err = clientcmd.LoadFromFile(e.KubeConfigPath)
118+
if err != nil {
119+
slog.Error("Failed to load kube config", "err", err)
120+
os.Exit(-1)
121+
}
122+
}
123+
}
124+
if e.config == nil {
125+
// 创建一个 rest.Config 对象
126+
e.config, err = clientcmd.NewDefaultClientConfig(*apiConfig, &clientcmd.ConfigOverrides{}).ClientConfig()
127+
if err != nil {
128+
slog.Error("Failed to create rest config", "err", err)
129+
os.Exit(-1)
130+
}
131+
}
132+
133+
e.clientSet, err = kubernetes.NewForConfig(e.config)
134+
if err != nil {
135+
slog.Error("Failed to create clientset", "err", err)
136+
os.Exit(-1)
137+
}
138+
e.dynamicClient, err = dynamic.NewForConfig(e.config)
139+
if err != nil {
140+
slog.Error("Failed to create dynamic client", "err", err)
141+
os.Exit(-1)
142+
}
143+
}
144+
145+
type EKSCluster struct {
146+
Name string `yaml:"name" json:"name"`
147+
Region string `yaml:"region" json:"region"`
148+
}
149+
150+
func (e *EKSCluster) GetKubeconfig() (*clientcmdapi.Config, error) {
151+
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
152+
defer cancel()
153+
cfg, err := config.LoadDefaultConfig(ctx, config.WithRegion(e.Region))
154+
if err != nil {
155+
return nil, err
156+
}
157+
158+
svc := eks.NewFromConfig(cfg)
159+
160+
return pkg.FetchEKSKubeconfig(ctx, svc, e.Region, e.Name)
161+
}

config/config.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ type Config struct {
3737
Locker *Locker `yaml:"locker" json:"locker"`
3838
Secret *Secret `yaml:"secret" json:"secret"`
3939
Storage *config.Storage `yaml:"storage" json:"storage"`
40+
Clusters Clusters `yaml:"clusters" json:"clusters"`
4041
}
4142

4243
type SecretConfig struct {
@@ -79,6 +80,9 @@ func (e *Config) Init(opts ...source.Option) {
7980
if e.Storage != nil {
8081
e.Storage.Init()
8182
}
83+
if len(e.Clusters) > 0 {
84+
e.Clusters.Init()
85+
}
8286
}
8387

8488
func (e *Config) OnChange() {

go.mod

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,10 @@ require (
99
github.com/appleboy/gin-jwt/v2 v2.9.2
1010
github.com/aws/aws-msk-iam-sasl-signer-go v1.0.0
1111
github.com/aws/aws-sdk-go-v2 v1.30.4
12-
github.com/aws/aws-sdk-go-v2/config v1.27.28
13-
github.com/aws/aws-sdk-go-v2/service/s3 v1.60.0
14-
github.com/aws/aws-sdk-go-v2/service/secretsmanager v1.32.5
12+
github.com/aws/aws-sdk-go-v2/config v1.27.30
13+
github.com/aws/aws-sdk-go-v2/service/eks v1.48.2
14+
github.com/aws/aws-sdk-go-v2/service/s3 v1.60.1
15+
github.com/aws/aws-sdk-go-v2/service/secretsmanager v1.32.6
1516
github.com/bsm/redislock v0.9.4
1617
github.com/common-nighthawk/go-figure v0.0.0-20210622060536-734e95fb86be
1718
github.com/gin-contrib/cors v1.7.2
@@ -21,8 +22,8 @@ require (
2122
github.com/google/uuid v1.6.0
2223
github.com/gorilla/websocket v1.5.3
2324
github.com/grafana/pyroscope-go v1.1.2
24-
github.com/larksuite/oapi-sdk-go/v3 v3.3.1
25-
github.com/mss-boot-io/mss-boot v0.2.7-0.20240822072236-e542a7a55c43
25+
github.com/larksuite/oapi-sdk-go/v3 v3.3.2
26+
github.com/mss-boot-io/mss-boot v0.2.7
2627
github.com/mss-boot-io/redisqueue/v2 v2.0.0-20240222064111-d36e396df7f9
2728
github.com/nsqio/go-nsq v1.1.0
2829
github.com/redis/go-redis/v9 v9.6.1
@@ -39,6 +40,7 @@ require (
3940
golang.org/x/oauth2 v0.22.0
4041
google.golang.org/grpc v1.65.0
4142
gorm.io/gorm v1.25.11
43+
k8s.io/api v0.31.0
4244
k8s.io/apimachinery v0.31.0
4345
k8s.io/client-go v0.31.0
4446
)
@@ -57,20 +59,20 @@ require (
5759
github.com/andygrunwald/go-jira v1.16.0 // indirect
5860
github.com/armon/go-metrics v0.4.1 // indirect
5961
github.com/aws/aws-sdk-go-v2/aws/protocol/eventstream v1.6.4 // indirect
60-
github.com/aws/aws-sdk-go-v2/credentials v1.17.28 // indirect
62+
github.com/aws/aws-sdk-go-v2/credentials v1.17.29 // indirect
6163
github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.16.12 // indirect
6264
github.com/aws/aws-sdk-go-v2/internal/configsources v1.3.16 // indirect
6365
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.6.16 // indirect
6466
github.com/aws/aws-sdk-go-v2/internal/ini v1.8.1 // indirect
6567
github.com/aws/aws-sdk-go-v2/internal/v4a v1.3.16 // indirect
66-
github.com/aws/aws-sdk-go-v2/service/appconfigdata v1.16.4 // indirect
68+
github.com/aws/aws-sdk-go-v2/service/appconfigdata v1.16.5 // indirect
6769
github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v1.11.4 // indirect
6870
github.com/aws/aws-sdk-go-v2/service/internal/checksum v1.3.18 // indirect
6971
github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.11.18 // indirect
7072
github.com/aws/aws-sdk-go-v2/service/internal/s3shared v1.17.16 // indirect
7173
github.com/aws/aws-sdk-go-v2/service/sso v1.22.5 // indirect
7274
github.com/aws/aws-sdk-go-v2/service/ssooidc v1.26.5 // indirect
73-
github.com/aws/aws-sdk-go-v2/service/sts v1.30.4 // indirect
75+
github.com/aws/aws-sdk-go-v2/service/sts v1.30.5 // indirect
7476
github.com/aws/smithy-go v1.20.4 // indirect
7577
github.com/beorn7/perks v1.0.1 // indirect
7678
github.com/bytedance/sonic v1.12.1 // indirect
@@ -144,16 +146,16 @@ require (
144146
github.com/google/go-querystring v1.1.0 // indirect
145147
github.com/google/gofuzz v1.2.0 // indirect
146148
github.com/gorilla/mux v1.8.1 // indirect
147-
github.com/grafana/dskit v0.0.0-20240820081259-f96e399400b1 // indirect
149+
github.com/grafana/dskit v0.0.0-20240826073544-47b1b6311db3 // indirect
148150
github.com/grafana/gomemcache v0.0.0-20240805133030-fdaf6a95408e // indirect
149151
github.com/grafana/jsonparser v0.0.0-20240425183733-ea80629e1a32 // indirect
150-
github.com/grafana/loki/pkg/push v0.0.0-20240821155943-d43b2de1b4e0 // indirect
152+
github.com/grafana/loki/pkg/push v0.0.0-20240822152437-246a1dfbe24a // indirect
151153
github.com/grafana/loki/v3 v3.1.1 // indirect
152154
github.com/grafana/pyroscope-go/godeltaprof v0.1.8 // indirect
153155
github.com/grafana/regexp v0.0.0-20240518133315-a468a5bfb3bc // indirect
154156
github.com/grpc-ecosystem/go-grpc-middleware/providers/prometheus v1.0.1 // indirect
155157
github.com/grpc-ecosystem/go-grpc-middleware/v2 v2.1.0 // indirect
156-
github.com/hashicorp/consul/api v1.29.2 // indirect
158+
github.com/hashicorp/consul/api v1.29.3 // indirect
157159
github.com/hashicorp/errwrap v1.1.0 // indirect
158160
github.com/hashicorp/go-cleanhttp v0.5.2 // indirect
159161
github.com/hashicorp/go-hclog v1.6.3 // indirect
@@ -181,6 +183,7 @@ require (
181183
github.com/jcmturner/rpc/v2 v2.0.3 // indirect
182184
github.com/jinzhu/inflection v1.0.0 // indirect
183185
github.com/jinzhu/now v1.1.5 // indirect
186+
github.com/jmespath/go-jmespath v0.4.0 // indirect
184187
github.com/josharian/intern v1.0.0 // indirect
185188
github.com/jpillora/backoff v1.0.0 // indirect
186189
github.com/json-iterator/go v1.1.12 // indirect
@@ -213,14 +216,14 @@ require (
213216
github.com/opentracing-contrib/go-grpc v0.0.0-20240724223109-9dec25a38fa8 // indirect
214217
github.com/opentracing-contrib/go-stdlib v1.0.0 // indirect
215218
github.com/opentracing/opentracing-go v1.2.0 // indirect
216-
github.com/pelletier/go-toml/v2 v2.2.2 // indirect
219+
github.com/pelletier/go-toml/v2 v2.2.3 // indirect
217220
github.com/pierrec/lz4/v4 v4.1.21 // indirect
218221
github.com/pires/go-proxyproto v0.7.0 // indirect
219222
github.com/pjbgf/sha1cd v0.3.0 // indirect
220223
github.com/pkg/errors v0.9.1 // indirect
221224
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
222225
github.com/power-devops/perfstat v0.0.0-20240221224432-82ca36839d55 // indirect
223-
github.com/prometheus/client_golang v1.20.1 // indirect
226+
github.com/prometheus/client_golang v1.20.2 // indirect
224227
github.com/prometheus/client_model v0.6.1 // indirect
225228
github.com/prometheus/common v0.55.0 // indirect
226229
github.com/prometheus/exporter-toolkit v0.11.0 // indirect
@@ -261,26 +264,26 @@ require (
261264
go.etcd.io/etcd/client/pkg/v3 v3.5.15 // indirect
262265
go.etcd.io/etcd/client/v3 v3.5.15 // indirect
263266
go.mongodb.org/mongo-driver v1.16.1 // indirect
264-
go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.53.0 // indirect
265-
go.opentelemetry.io/otel v1.28.0 // indirect
266-
go.opentelemetry.io/otel/metric v1.28.0 // indirect
267-
go.opentelemetry.io/otel/trace v1.28.0 // indirect
267+
go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.54.0 // indirect
268+
go.opentelemetry.io/otel v1.29.0 // indirect
269+
go.opentelemetry.io/otel/metric v1.29.0 // indirect
270+
go.opentelemetry.io/otel/trace v1.29.0 // indirect
268271
go.uber.org/atomic v1.11.0 // indirect
269272
go.uber.org/multierr v1.11.0 // indirect
270273
go.uber.org/zap v1.27.0 // indirect
271274
go4.org/netipx v0.0.0-20231129151722-fdeea329fbba // indirect
272275
golang.org/x/arch v0.9.0 // indirect
273276
golang.org/x/crypto v0.26.0 // indirect
274-
golang.org/x/exp v0.0.0-20240808152545-0cdaa3abc0fa // indirect
277+
golang.org/x/exp v0.0.0-20240823005443-9b4947da3948 // indirect
275278
golang.org/x/mod v0.20.0 // indirect
276279
golang.org/x/sync v0.8.0 // indirect
277280
golang.org/x/sys v0.24.0 // indirect
278281
golang.org/x/term v0.23.0 // indirect
279282
golang.org/x/text v0.17.0 // indirect
280283
golang.org/x/time v0.6.0 // indirect
281284
golang.org/x/tools v0.24.0 // indirect
282-
google.golang.org/genproto/googleapis/api v0.0.0-20240820151423-278611b39280 // indirect
283-
google.golang.org/genproto/googleapis/rpc v0.0.0-20240820151423-278611b39280 // indirect
285+
google.golang.org/genproto/googleapis/api v0.0.0-20240823204242-4ba0660f739c // indirect
286+
google.golang.org/genproto/googleapis/rpc v0.0.0-20240823204242-4ba0660f739c // indirect
284287
google.golang.org/protobuf v1.34.2 // indirect
285288
gopkg.in/inf.v0 v0.9.1 // indirect
286289
gopkg.in/warnings.v0 v0.1.2 // indirect
@@ -290,9 +293,8 @@ require (
290293
gorm.io/driver/postgres v1.5.9 // indirect
291294
gorm.io/driver/sqlserver v1.5.3 // indirect
292295
gorm.io/plugin/dbresolver v1.5.2 // indirect
293-
k8s.io/api v0.31.0 // indirect
294296
k8s.io/klog/v2 v2.130.1 // indirect
295-
k8s.io/kube-openapi v0.0.0-20240816214639-573285566f34 // indirect
297+
k8s.io/kube-openapi v0.0.0-20240822171749-76de80e0abd9 // indirect
296298
k8s.io/utils v0.0.0-20240821151609-f90d01438635 // indirect
297299
modernc.org/libc v1.59.9 // indirect
298300
modernc.org/mathutil v1.6.0 // indirect

0 commit comments

Comments
 (0)