Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions cmd/migrate/migration/system/1724396388009_migrate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package system

import (
"runtime"

"github.com/mss-boot-io/mss-boot/pkg/migration"
"gorm.io/gorm"

"github.com/mss-boot-io/mss-boot-admin/models"
)

func init() {
_, fileName, _, _ := runtime.Caller(0)
migration.Migrate.SetVersion(migration.GetFilename(fileName), _1724396388009Migrate)
}

func _1724396388009Migrate(db *gorm.DB, version string) error {
return db.Transaction(func(tx *gorm.DB) error {

err := tx.Migrator().AutoMigrate(
new(models.Task),
)
if err != nil {
return err
}

return migration.Migrate.CreateVersion(tx, version)
})
}
161 changes: 161 additions & 0 deletions config/clusters.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
package config

import (
"context"
"log/slog"
"os"
"path/filepath"
"time"

"github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/service/eks"
"k8s.io/client-go/dynamic"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
clientcmdapi "k8s.io/client-go/tools/clientcmd/api"
"k8s.io/client-go/util/homedir"

"github.com/mss-boot-io/mss-boot-admin/pkg"
)

/*
* @Author: lwnmengjing<lwnmengjing@qq.com>
* @Date: 2024/8/22 16:59:33
* @Last Modified by: lwnmengjing<lwnmengjing@qq.com>
* @Last Modified time: 2024/8/22 16:59:33
*/

type Clusters []*Cluster

func (e Clusters) Init() {
for i := range e {
(e)[i].Init()
}
}

func (e Clusters) GetDynamicClient(name string) *dynamic.DynamicClient {
for _, cluster := range e {
if cluster.Name == name {
return cluster.GetDynamicClient()
}
}
return nil
}

func (e Clusters) GetClientSet(name string) *kubernetes.Clientset {
for _, cluster := range e {
if cluster.Name == name {
return cluster.GetClientSet()
}
}
return nil
}

func (e Clusters) GetConfig(name string) *rest.Config {
for _, cluster := range e {
if cluster.Name == name {
return cluster.GetConfig()
}
}
return nil
}

type Cluster struct {
Name string `yaml:"name" json:"name"`
KubeConfig string `yaml:"kubeConfig" json:"kubeConfig"`
KubeConfigPath string `yaml:"kubeConfigPath" json:"kubeConfigPath"`
EKS *EKSCluster `yaml:"eks" json:"eks"`
clientSet *kubernetes.Clientset
config *rest.Config
dynamicClient *dynamic.DynamicClient
}

func (e *Cluster) GetDynamicClient() *dynamic.DynamicClient {
return e.dynamicClient
}

func (e *Cluster) GetClientSet() *kubernetes.Clientset {
return e.clientSet
}

func (e *Cluster) GetConfig() *rest.Config {
return e.config
}

func (e *Cluster) Init() {
var err error
var apiConfig *clientcmdapi.Config
if e.EKS != nil {
apiConfig, err = e.EKS.GetKubeconfig()
if err != nil {
slog.Error("Failed to get kubeconfig", "err", err)
os.Exit(-1)
}
if e.Name == "" {
e.Name = e.EKS.Name
}
}
if apiConfig == nil && e.KubeConfigPath == "" && e.KubeConfig == "" {
e.config, err = rest.InClusterConfig()
if err != nil {
slog.Error("Failed to get in cluster config", "err", err)
os.Exit(-1)
}
}
if apiConfig == nil && e.config == nil {
if e.KubeConfig != "" {
apiConfig, err = clientcmd.Load([]byte(e.KubeConfig))
if err != nil {
slog.Error("Failed to load kube config", "err", err)
os.Exit(-1)
}
} else {
if e.KubeConfigPath == "" {
e.KubeConfigPath = filepath.Join(homedir.HomeDir(), ".kube", "config")
}
apiConfig, err = clientcmd.LoadFromFile(e.KubeConfigPath)
if err != nil {
slog.Error("Failed to load kube config", "err", err)
os.Exit(-1)
}
}
}
if e.config == nil {
// 创建一个 rest.Config 对象
e.config, err = clientcmd.NewDefaultClientConfig(*apiConfig, &clientcmd.ConfigOverrides{}).ClientConfig()
if err != nil {
slog.Error("Failed to create rest config", "err", err)
os.Exit(-1)
}
}

e.clientSet, err = kubernetes.NewForConfig(e.config)
if err != nil {
slog.Error("Failed to create clientset", "err", err)
os.Exit(-1)
}
e.dynamicClient, err = dynamic.NewForConfig(e.config)
if err != nil {
slog.Error("Failed to create dynamic client", "err", err)
os.Exit(-1)
}
}

type EKSCluster struct {
Name string `yaml:"name" json:"name"`
Region string `yaml:"region" json:"region"`
}

func (e *EKSCluster) GetKubeconfig() (*clientcmdapi.Config, error) {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
cfg, err := config.LoadDefaultConfig(ctx, config.WithRegion(e.Region))
if err != nil {
return nil, err
}

svc := eks.NewFromConfig(cfg)

return pkg.FetchEKSKubeconfig(ctx, svc, e.Region, e.Name)
}
4 changes: 4 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ type Config struct {
Locker *Locker `yaml:"locker" json:"locker"`
Secret *Secret `yaml:"secret" json:"secret"`
Storage *config.Storage `yaml:"storage" json:"storage"`
Clusters Clusters `yaml:"clusters" json:"clusters"`
}

type SecretConfig struct {
Expand Down Expand Up @@ -79,6 +80,9 @@ func (e *Config) Init(opts ...source.Option) {
if e.Storage != nil {
e.Storage.Init()
}
if len(e.Clusters) > 0 {
e.Clusters.Init()
}
}

func (e *Config) OnChange() {
Expand Down
46 changes: 24 additions & 22 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@ require (
github.com/appleboy/gin-jwt/v2 v2.9.2
github.com/aws/aws-msk-iam-sasl-signer-go v1.0.0
github.com/aws/aws-sdk-go-v2 v1.30.4
github.com/aws/aws-sdk-go-v2/config v1.27.28
github.com/aws/aws-sdk-go-v2/service/s3 v1.60.0
github.com/aws/aws-sdk-go-v2/service/secretsmanager v1.32.5
github.com/aws/aws-sdk-go-v2/config v1.27.30
github.com/aws/aws-sdk-go-v2/service/eks v1.48.2
github.com/aws/aws-sdk-go-v2/service/s3 v1.60.1
github.com/aws/aws-sdk-go-v2/service/secretsmanager v1.32.6
github.com/bsm/redislock v0.9.4
github.com/common-nighthawk/go-figure v0.0.0-20210622060536-734e95fb86be
github.com/gin-contrib/cors v1.7.2
Expand All @@ -21,8 +22,8 @@ require (
github.com/google/uuid v1.6.0
github.com/gorilla/websocket v1.5.3
github.com/grafana/pyroscope-go v1.1.2
github.com/larksuite/oapi-sdk-go/v3 v3.3.1
github.com/mss-boot-io/mss-boot v0.2.7-0.20240822072236-e542a7a55c43
github.com/larksuite/oapi-sdk-go/v3 v3.3.2
github.com/mss-boot-io/mss-boot v0.2.7
github.com/mss-boot-io/redisqueue/v2 v2.0.0-20240222064111-d36e396df7f9
github.com/nsqio/go-nsq v1.1.0
github.com/redis/go-redis/v9 v9.6.1
Expand All @@ -39,6 +40,7 @@ require (
golang.org/x/oauth2 v0.22.0
google.golang.org/grpc v1.65.0
gorm.io/gorm v1.25.11
k8s.io/api v0.31.0
k8s.io/apimachinery v0.31.0
k8s.io/client-go v0.31.0
)
Expand All @@ -57,20 +59,20 @@ require (
github.com/andygrunwald/go-jira v1.16.0 // indirect
github.com/armon/go-metrics v0.4.1 // indirect
github.com/aws/aws-sdk-go-v2/aws/protocol/eventstream v1.6.4 // indirect
github.com/aws/aws-sdk-go-v2/credentials v1.17.28 // indirect
github.com/aws/aws-sdk-go-v2/credentials v1.17.29 // indirect
github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.16.12 // indirect
github.com/aws/aws-sdk-go-v2/internal/configsources v1.3.16 // indirect
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.6.16 // indirect
github.com/aws/aws-sdk-go-v2/internal/ini v1.8.1 // indirect
github.com/aws/aws-sdk-go-v2/internal/v4a v1.3.16 // indirect
github.com/aws/aws-sdk-go-v2/service/appconfigdata v1.16.4 // indirect
github.com/aws/aws-sdk-go-v2/service/appconfigdata v1.16.5 // indirect
github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v1.11.4 // indirect
github.com/aws/aws-sdk-go-v2/service/internal/checksum v1.3.18 // indirect
github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.11.18 // indirect
github.com/aws/aws-sdk-go-v2/service/internal/s3shared v1.17.16 // indirect
github.com/aws/aws-sdk-go-v2/service/sso v1.22.5 // indirect
github.com/aws/aws-sdk-go-v2/service/ssooidc v1.26.5 // indirect
github.com/aws/aws-sdk-go-v2/service/sts v1.30.4 // indirect
github.com/aws/aws-sdk-go-v2/service/sts v1.30.5 // indirect
github.com/aws/smithy-go v1.20.4 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/bytedance/sonic v1.12.1 // indirect
Expand Down Expand Up @@ -144,16 +146,16 @@ require (
github.com/google/go-querystring v1.1.0 // indirect
github.com/google/gofuzz v1.2.0 // indirect
github.com/gorilla/mux v1.8.1 // indirect
github.com/grafana/dskit v0.0.0-20240820081259-f96e399400b1 // indirect
github.com/grafana/dskit v0.0.0-20240826073544-47b1b6311db3 // indirect
github.com/grafana/gomemcache v0.0.0-20240805133030-fdaf6a95408e // indirect
github.com/grafana/jsonparser v0.0.0-20240425183733-ea80629e1a32 // indirect
github.com/grafana/loki/pkg/push v0.0.0-20240821155943-d43b2de1b4e0 // indirect
github.com/grafana/loki/pkg/push v0.0.0-20240822152437-246a1dfbe24a // indirect
github.com/grafana/loki/v3 v3.1.1 // indirect
github.com/grafana/pyroscope-go/godeltaprof v0.1.8 // indirect
github.com/grafana/regexp v0.0.0-20240518133315-a468a5bfb3bc // indirect
github.com/grpc-ecosystem/go-grpc-middleware/providers/prometheus v1.0.1 // indirect
github.com/grpc-ecosystem/go-grpc-middleware/v2 v2.1.0 // indirect
github.com/hashicorp/consul/api v1.29.2 // indirect
github.com/hashicorp/consul/api v1.29.3 // indirect
github.com/hashicorp/errwrap v1.1.0 // indirect
github.com/hashicorp/go-cleanhttp v0.5.2 // indirect
github.com/hashicorp/go-hclog v1.6.3 // indirect
Expand Down Expand Up @@ -181,6 +183,7 @@ require (
github.com/jcmturner/rpc/v2 v2.0.3 // indirect
github.com/jinzhu/inflection v1.0.0 // indirect
github.com/jinzhu/now v1.1.5 // indirect
github.com/jmespath/go-jmespath v0.4.0 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/jpillora/backoff v1.0.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
Expand Down Expand Up @@ -213,14 +216,14 @@ require (
github.com/opentracing-contrib/go-grpc v0.0.0-20240724223109-9dec25a38fa8 // indirect
github.com/opentracing-contrib/go-stdlib v1.0.0 // indirect
github.com/opentracing/opentracing-go v1.2.0 // indirect
github.com/pelletier/go-toml/v2 v2.2.2 // indirect
github.com/pelletier/go-toml/v2 v2.2.3 // indirect
github.com/pierrec/lz4/v4 v4.1.21 // indirect
github.com/pires/go-proxyproto v0.7.0 // indirect
github.com/pjbgf/sha1cd v0.3.0 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
github.com/power-devops/perfstat v0.0.0-20240221224432-82ca36839d55 // indirect
github.com/prometheus/client_golang v1.20.1 // indirect
github.com/prometheus/client_golang v1.20.2 // indirect
github.com/prometheus/client_model v0.6.1 // indirect
github.com/prometheus/common v0.55.0 // indirect
github.com/prometheus/exporter-toolkit v0.11.0 // indirect
Expand Down Expand Up @@ -261,26 +264,26 @@ require (
go.etcd.io/etcd/client/pkg/v3 v3.5.15 // indirect
go.etcd.io/etcd/client/v3 v3.5.15 // indirect
go.mongodb.org/mongo-driver v1.16.1 // indirect
go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.53.0 // indirect
go.opentelemetry.io/otel v1.28.0 // indirect
go.opentelemetry.io/otel/metric v1.28.0 // indirect
go.opentelemetry.io/otel/trace v1.28.0 // indirect
go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.54.0 // indirect
go.opentelemetry.io/otel v1.29.0 // indirect
go.opentelemetry.io/otel/metric v1.29.0 // indirect
go.opentelemetry.io/otel/trace v1.29.0 // indirect
go.uber.org/atomic v1.11.0 // indirect
go.uber.org/multierr v1.11.0 // indirect
go.uber.org/zap v1.27.0 // indirect
go4.org/netipx v0.0.0-20231129151722-fdeea329fbba // indirect
golang.org/x/arch v0.9.0 // indirect
golang.org/x/crypto v0.26.0 // indirect
golang.org/x/exp v0.0.0-20240808152545-0cdaa3abc0fa // indirect
golang.org/x/exp v0.0.0-20240823005443-9b4947da3948 // indirect
golang.org/x/mod v0.20.0 // indirect
golang.org/x/sync v0.8.0 // indirect
golang.org/x/sys v0.24.0 // indirect
golang.org/x/term v0.23.0 // indirect
golang.org/x/text v0.17.0 // indirect
golang.org/x/time v0.6.0 // indirect
golang.org/x/tools v0.24.0 // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20240820151423-278611b39280 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240820151423-278611b39280 // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20240823204242-4ba0660f739c // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240823204242-4ba0660f739c // indirect
google.golang.org/protobuf v1.34.2 // indirect
gopkg.in/inf.v0 v0.9.1 // indirect
gopkg.in/warnings.v0 v0.1.2 // indirect
Expand All @@ -290,9 +293,8 @@ require (
gorm.io/driver/postgres v1.5.9 // indirect
gorm.io/driver/sqlserver v1.5.3 // indirect
gorm.io/plugin/dbresolver v1.5.2 // indirect
k8s.io/api v0.31.0 // indirect
k8s.io/klog/v2 v2.130.1 // indirect
k8s.io/kube-openapi v0.0.0-20240816214639-573285566f34 // indirect
k8s.io/kube-openapi v0.0.0-20240822171749-76de80e0abd9 // indirect
k8s.io/utils v0.0.0-20240821151609-f90d01438635 // indirect
modernc.org/libc v1.59.9 // indirect
modernc.org/mathutil v1.6.0 // indirect
Expand Down
Loading