From 3fcc8d392d33a77d0b7e0bcf0dee30753b1c888a Mon Sep 17 00:00:00 2001 From: w-mj Date: Tue, 28 Feb 2023 10:07:55 +0800 Subject: [PATCH 1/4] use config class --- main.go | 17 +++--------- src/config.go | 60 ++++++++++++++++++++++++++++++++++++++++++ src/k8s_client.go | 2 +- src/orm.go | 16 +++-------- template/app_view.html | 2 +- 5 files changed, 68 insertions(+), 29 deletions(-) create mode 100644 src/config.go diff --git a/main.go b/main.go index ed1f6c0..92c5963 100644 --- a/main.go +++ b/main.go @@ -21,21 +21,10 @@ import ( //go:embed template/* static/* var f embed.FS -var jwtKey string -var aseKey string - func main() { + m.InitConfig() m.InitClient() - jwtKey = os.Getenv("JWT_KEY") - aseKey = os.Getenv("ASE_KEY") - if jwtKey == "" { - panic("No jwt key") - } - if aseKey == "" { - panic("No ase key") - } - r := gin.Default() fp, _ := fs.Sub(f, "static") @@ -76,10 +65,10 @@ func ParseCGToken(token string) (string, error) { return "", err } - jwtString := AesDecryptECB(aesEncrypt, []byte(aseKey)) + jwtString := AesDecryptECB(aesEncrypt, []byte(m.GetConfig().AseKey)) jwtResult, err := jwt.ParseWithClaims(string(jwtString), &CGJWTClaim{}, func(token *jwt.Token) (i interface{}, err error) { - return []byte(jwtKey), nil + return []byte(m.GetConfig().JwtKey), nil // return hmac.New(sha256.New, []byte(jwtKey)), nil }) if err != nil { diff --git a/src/config.go b/src/config.go new file mode 100644 index 0000000..bf5fff4 --- /dev/null +++ b/src/config.go @@ -0,0 +1,60 @@ +package k8s_manager + +import ( + "fmt" + "os" + "reflect" +) + +type Config struct { + JwtKey string + AseKey string + DBHost string + DBUser string + DBPassword string + NFSHost string + NFSPath string +} + +var config *Config + +func getField(v *Config, field string) string { + r := reflect.ValueOf(v) + f := reflect.Indirect(r).FieldByName(field) + return f.String() +} + +func setField(v *Config, field string, value string) { + r := reflect.ValueOf(v) + f := reflect.Indirect(r).FieldByName(field) + f.SetString(value) +} + +func InitConfig() { + config = &Config{} + config.JwtKey = os.Getenv("JWT_KEY") + config.AseKey = os.Getenv("ASE_KEY") + config.DBHost = os.Getenv("DB_HOST") + config.DBUser = os.Getenv("DB_USER") + config.DBPassword = os.Getenv("DB_PASS") + config.NFSHost = os.Getenv("NFS_HOST") + config.NFSPath = os.Getenv("NFS_PATH") + + mustFields := []string{"JwtKey", "AseKey", "DBHost", "DBUser", "DBPassword"} + optionalFields := map[string]string{} + for _, f := range mustFields { + if getField(config, f) == "" { + panic(fmt.Sprintf("field %s must be set.", f)) + } + } + for f, v := range optionalFields { + if getField(config, f) == "" { + setField(config, f, v) + } + } + +} + +func GetConfig() *Config { + return config +} diff --git a/src/k8s_client.go b/src/k8s_client.go index eea2897..763d284 100644 --- a/src/k8s_client.go +++ b/src/k8s_client.go @@ -362,7 +362,7 @@ func (c *K8sClient) CreatePersistentVolume(app *AppModel) { StorageClassName: app.StorageClassName(), PersistentVolumeSource: v1.PersistentVolumeSource{ NFS: &v1.NFSVolumeSource{ - Server: "192.168.1.1", + Server: "192.168.252.250", Path: app.NFSPath(), ReadOnly: false, }, diff --git a/src/orm.go b/src/orm.go index 2f00370..d395699 100644 --- a/src/orm.go +++ b/src/orm.go @@ -5,7 +5,6 @@ import ( "github.com/sirupsen/logrus" "gorm.io/driver/mysql" "gorm.io/gorm" - "os" "sync" ) @@ -16,18 +15,9 @@ func opendb() *gorm.DB { if instance == nil { lock.Lock() if instance == nil { - db_host, ok := os.LookupEnv("DB_HOST") - if !ok { - panic("DB_HOST NOT SET") - } - db_user, ok := os.LookupEnv("DB_USER") - if !ok { - panic("DB_USER NOT SET") - } - db_pass, ok := os.LookupEnv("DB_PASS") - if !ok { - panic("DB_PASS NOT SET") - } + db_host := GetConfig().DBHost + db_user := GetConfig().DBUser + db_pass := GetConfig().DBPassword dsn := fmt.Sprintf("%s:%s@tcp(%s:3306)/k8s?charset=utf8mb4&parseTime=True&loc=Local", db_user, db_pass, db_host) var err error instance, err = gorm.Open(mysql.Open(dsn), &gorm.Config{}) diff --git a/template/app_view.html b/template/app_view.html index f85c4d1..1f0ef2e 100644 --- a/template/app_view.html +++ b/template/app_view.html @@ -112,7 +112,7 @@
+ placeholder="端口号"/>
From ae28a70c718ed77eead5fee3f24fc06158ab911a Mon Sep 17 00:00:00 2001 From: w-mj Date: Tue, 28 Feb 2023 11:21:23 +0800 Subject: [PATCH 2/4] mount nfs in pod --- install.yaml | 4 ++++ src/app_model.go | 39 ++++++++++++++++++++++++--------------- src/config.go | 2 +- src/k8s_client.go | 32 ++++++++++++++++++++++++++------ template/app_view.html | 7 +++++++ 5 files changed, 62 insertions(+), 22 deletions(-) diff --git a/install.yaml b/install.yaml index 472ced3..574a25d 100644 --- a/install.yaml +++ b/install.yaml @@ -33,6 +33,10 @@ spec: value: "!bd832W0@CG" - name: ASE_KEY value: A5es$&!0GeoEast + - name: NFS_HOST + value: 192.168.252.250 + - name: NFS_PATH + value: /mnt/CGdata/ms --- apiVersion: v1 kind: Service diff --git a/src/app_model.go b/src/app_model.go index a1ed78b..0fd0bcd 100644 --- a/src/app_model.go +++ b/src/app_model.go @@ -8,15 +8,16 @@ import ( ) type AppModel struct { - Id int - AppName string - UserName string - Image string - Command string - Comment string - Env string - SvcName string - Ports string + Id int + AppName string + UserName string + Image string + Command string + Comment string + Env string + SvcName string + Ports string + PersistentPath string } func (*AppModel) TableName() string { @@ -36,21 +37,25 @@ func (a *AppModel) Fill() error { return res.Error } -func (a *AppModel) Update() error { +func (a *AppModel) fillDefaults() { if a.SvcName == "" { a.SvcName = "svc" } a.SvcName = strings.TrimPrefix(a.SvcName, a.NamePrefix()+"-") + if a.PersistentPath == "" { + a.PersistentPath = "/data/" + } +} + +func (a *AppModel) Update() error { + a.fillDefaults() db := DB() db.Save(a) return nil } func (a *AppModel) Create() error { - if a.SvcName == "" { - a.SvcName = "svc" - } - a.SvcName = strings.TrimPrefix(a.SvcName, a.NamePrefix()+"-") + a.fillDefaults() db := DB() db.Create(a) return nil @@ -155,7 +160,7 @@ func (a *AppModel) PersistentVolumeName() string { } func (a *AppModel) NFSPath() string { - return "/data/" + a.UserName + return GetConfig().NFSPath + "/" + a.UserName } func (a *AppModel) PersistentVolumeClaimName() string { @@ -165,3 +170,7 @@ func (a *AppModel) PersistentVolumeClaimName() string { func (a *AppModel) StorageClassName() string { return a.NamePrefix() + "-storage-class" } + +func (a *AppModel) PersistentVolumeMountName() string { + return a.NamePrefix() + "-pvm" +} diff --git a/src/config.go b/src/config.go index bf5fff4..2ec2909 100644 --- a/src/config.go +++ b/src/config.go @@ -40,7 +40,7 @@ func InitConfig() { config.NFSHost = os.Getenv("NFS_HOST") config.NFSPath = os.Getenv("NFS_PATH") - mustFields := []string{"JwtKey", "AseKey", "DBHost", "DBUser", "DBPassword"} + mustFields := []string{"JwtKey", "AseKey", "DBHost", "DBUser", "DBPassword", "NFSHost", "NFSPath"} optionalFields := map[string]string{} for _, f := range mustFields { if getField(config, f) == "" { diff --git a/src/k8s_client.go b/src/k8s_client.go index 763d284..ab38b7b 100644 --- a/src/k8s_client.go +++ b/src/k8s_client.go @@ -72,20 +72,24 @@ func (c *K8sClient) DeploymentExists(deploymentName string) bool { } func (c *K8sClient) UpdateDeployment(app *AppModel) { - var rep int32 = 1 labelKey, labelValue := app.GetLabel() l := map[string]string{labelKey: labelValue} podLabels := map[string]string{ labelKey: labelValue, "cg-deployment": app.DeploymentName(), } + containerPorts := funk.Map(app.GetPorts(), func(port int32) v1.ContainerPort { + return v1.ContainerPort{ + ContainerPort: port, + } + }).([]v1.ContainerPort) deployment := &appv1.Deployment{ ObjectMeta: metav1.ObjectMeta{ Name: app.DeploymentName(), Labels: l, }, Spec: appv1.DeploymentSpec{ - Replicas: &rep, + Replicas: UnnamedPointer(int32(1)), Selector: &metav1.LabelSelector{ MatchLabels: l, }, @@ -102,14 +106,30 @@ func (c *K8sClient) UpdateDeployment(app *AppModel) { Name: "URL_PREFIX", Value: app.ServiceName(), }), - VolumeMounts: nil, - Ports: []v1.ContainerPort{ - {ContainerPort: 80}, + VolumeMounts: []v1.VolumeMount{ + { + Name: app.PersistentVolumeMountName(), + ReadOnly: false, + MountPath: app.PersistentPath, + }, }, + Ports: containerPorts, SecurityContext: &v1.SecurityContext{ RunAsUser: UnnamedPointer(int64(0)), }, }}, + Volumes: []v1.Volume{ + { + Name: app.PersistentVolumeMountName(), + VolumeSource: v1.VolumeSource{ + NFS: &v1.NFSVolumeSource{ + Server: config.NFSHost, + Path: app.NFSPath(), + ReadOnly: false, + }, + }, + }, + }, }, }, }, @@ -362,7 +382,7 @@ func (c *K8sClient) CreatePersistentVolume(app *AppModel) { StorageClassName: app.StorageClassName(), PersistentVolumeSource: v1.PersistentVolumeSource{ NFS: &v1.NFSVolumeSource{ - Server: "192.168.252.250", + Server: GetConfig().NFSHost, Path: app.NFSPath(), ReadOnly: false, }, diff --git a/template/app_view.html b/template/app_view.html index 1f0ef2e..fa10ba3 100644 --- a/template/app_view.html +++ b/template/app_view.html @@ -115,6 +115,12 @@ placeholder="端口号"/>
+
+ + +
+