Skip to content

Commit

Permalink
fix: create ui SECRET_KEY (#264)
Browse files Browse the repository at this point in the history
Signed-off-by: 张启航 <[email protected]>
  • Loading branch information
ZhangSetSail authored Nov 27, 2024
1 parent 0ffe83d commit fe8e6e9
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 4 deletions.
70 changes: 67 additions & 3 deletions controllers/handler/app-ui.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,24 @@ package handler

import (
"context"
"crypto/md5"
"encoding/hex"
"fmt"
"os"
"strconv"

rainbondv1alpha1 "github.com/goodrain/rainbond-operator/api/v1alpha1"
checksqllite "github.com/goodrain/rainbond-operator/util/check-sqllite"
"github.com/goodrain/rainbond-operator/util/commonutil"
"github.com/goodrain/rainbond-operator/util/probeutil"
"github.com/goodrain/rainbond-operator/util/rbdutil"
"github.com/shirou/gopsutil/cpu"
"github.com/shirou/gopsutil/mem"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/intstr"
"os"
"runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"strconv"
)

// AppUIName name for rbd-app-ui resources.
Expand Down Expand Up @@ -133,6 +137,10 @@ func (a *appui) deploymentForAppUI() client.Object {
Name: "IMAGE_REPO",
Value: a.cluster.Spec.ImageHub.Domain,
},
{
Name: "SECRET_KEY",
Value: getHashMac(),
},
}
if !checksqllite.IsSQLLite() {
mysqlEnvs := []corev1.EnvVar{
Expand Down Expand Up @@ -280,3 +288,59 @@ func (a *appui) serviceForAppUI(port int32) client.Object {

return svc
}

func getSystemInfo() string {
// 获取操作系统信息
os := runtime.GOOS

// 获取系统架构
arch := runtime.GOARCH

// 获取 CPU 核心数
cpuCount := runtime.NumCPU()

// 获取 CPU 相关信息
cpus, err := cpu.Info()
if err != nil {
fmt.Println("Error getting CPU info:", err)
return ""
}

// 获取内存信息
vmStat, err := mem.VirtualMemory()
if err != nil {
fmt.Println("Error getting memory info:", err)
return ""
}

// 获取总内存(GB)
totalMemory := vmStat.Total / (1024 * 1024 * 1024) // 转换为 GB

// 将系统信息拼接成一个字符串
systemInfo := fmt.Sprintf("OS:%s-Arch:%s-CPUs:%d-Mem:%dGB-CPU:%s",
os,
arch,
cpuCount,
totalMemory,
cpus[0].ModelName)

return systemInfo
}

// 计算MD5值
func calculateMD5(input string) string {
hash := md5.New()
hash.Write([]byte(input))
hashInBytes := hash.Sum(nil)
return hex.EncodeToString(hashInBytes)
}

// 获取系统信息并计算 MD5 值
func getHashMac() string {
// 获取系统信息
systemInfo := getSystemInfo()
// 计算系统信息的 MD5 值
md5Value := calculateMD5(systemInfo)
// 返回一个包含 "SECRET_KEY" 和 MD5 值的映射
return md5Value
}
7 changes: 6 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,15 @@ require (
github.com/juju/errors v0.0.0-20200330140219-3fe23663418f
github.com/pkg/errors v0.9.1
github.com/pquerna/ffjson v0.0.0-20190930134022-aa0246cd15f7
github.com/shirou/gopsutil v3.21.11+incompatible
github.com/sirupsen/logrus v1.8.1
github.com/stretchr/testify v1.9.0
github.com/twinj/uuid v1.0.0
k8s.io/api v0.20.6
k8s.io/apimachinery v0.20.6
k8s.io/client-go v0.20.6
k8s.io/kube-aggregator v0.20.1
k8s.io/utils v0.0.0-20201110183641-67b214c5f920
sigs.k8s.io/controller-runtime v0.7.0
)

Expand All @@ -44,6 +46,7 @@ require (
github.com/fsnotify/fsnotify v1.4.9 // indirect
github.com/go-ini/ini v1.67.0 // indirect
github.com/go-logr/zapr v0.2.0 // indirect
github.com/go-ole/go-ole v1.2.6 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/groupcache v0.0.0-20200121045136-8c9f03a8e57e // indirect
github.com/golang/protobuf v1.5.0 // indirect
Expand Down Expand Up @@ -71,6 +74,9 @@ require (
github.com/prometheus/common v0.10.0 // indirect
github.com/prometheus/procfs v0.6.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/tklauser/go-sysconf v0.3.14 // indirect
github.com/tklauser/numcpus v0.8.0 // indirect
github.com/yusufpapurcu/wmi v1.2.4 // indirect
go.uber.org/atomic v1.6.0 // indirect
go.uber.org/multierr v1.5.0 // indirect
go.uber.org/zap v1.15.0 // indirect
Expand All @@ -95,7 +101,6 @@ require (
k8s.io/component-base v0.20.6 // indirect
k8s.io/klog/v2 v2.4.0 // indirect
k8s.io/kube-openapi v0.0.0-20201113171705-d219536bb9fd // indirect
k8s.io/utils v0.0.0-20201110183641-67b214c5f920 // indirect
sigs.k8s.io/structured-merge-diff/v4 v4.0.3 // indirect
sigs.k8s.io/yaml v1.2.0 // indirect
)
Expand Down
10 changes: 10 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,8 @@ github.com/go-logr/logr v0.3.0 h1:q4c+kbcR0d5rSurhBR8dIgieOaYpXtsdTYfx22Cu6rs=
github.com/go-logr/logr v0.3.0/go.mod h1:z6/tIYblkpsD+a4lm/fGIIU9mZ+XfAiaFtq7xTgseGU=
github.com/go-logr/zapr v0.2.0 h1:v6Ji8yBW77pva6NkJKQdHLAJKrIJKRHz0RXwPqCHSR4=
github.com/go-logr/zapr v0.2.0/go.mod h1:qhKdvif7YF5GI9NWEpyxTSSBdGmzkNguibrdCNVPunU=
github.com/go-ole/go-ole v1.2.6 h1:/Fpf6oFPoeFik9ty7siob0G6Ke8QvQEuVcuChpwXzpY=
github.com/go-ole/go-ole v1.2.6/go.mod h1:pprOEPIfldk/42T2oK7lQ4v4JSDwmV0As9GaiUsvbm0=
github.com/go-openapi/analysis v0.0.0-20180825180245-b006789cd277/go.mod h1:k70tL6pCuVxPJOHXQ+wIac1FUrvNkHolPie/cLEU6hI=
github.com/go-openapi/analysis v0.17.0/go.mod h1:IowGgpVeD0vNm45So8nr+IcQ3pxVtpRoBWb8PVZO0ik=
github.com/go-openapi/analysis v0.18.0/go.mod h1:IowGgpVeD0vNm45So8nr+IcQ3pxVtpRoBWb8PVZO0ik=
Expand Down Expand Up @@ -706,6 +708,8 @@ github.com/satori/go.uuid v1.2.0/go.mod h1:dA0hQrYB0VpLJoorglMZABFdXlWrHn1NEOzdh
github.com/sean-/seed v0.0.0-20170313163322-e2103e2c3529/go.mod h1:DxrIzT+xaE7yg65j358z/aeFdxmN0P9QXhEzd20vsDc=
github.com/seccomp/libseccomp-golang v0.9.1/go.mod h1:GbW5+tmTXfcxTToHLXlScSlAvWlF4P2Ca7zGrPiEpWo=
github.com/sergi/go-diff v1.0.0/go.mod h1:0CfEIISq7TuYL3j771MWULgwwjU+GofnZX9QAmXWZgo=
github.com/shirou/gopsutil v3.21.11+incompatible h1:+1+c1VGhc88SSonWP6foOcLhvnKlUeu/erjjvaPEYiI=
github.com/shirou/gopsutil v3.21.11+incompatible/go.mod h1:5b4v6he4MtMOwMlS0TUMTu2PcXUg8+E1lC7eC3UO/RA=
github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc=
github.com/sirupsen/logrus v1.0.4-0.20170822132746-89742aefa4b2/go.mod h1:pMByvHTf9Beacp5x1UXfOR9xyW/9antXMhjMPG0dEzc=
github.com/sirupsen/logrus v1.0.6/go.mod h1:pMByvHTf9Beacp5x1UXfOR9xyW/9antXMhjMPG0dEzc=
Expand Down Expand Up @@ -757,6 +761,10 @@ github.com/syndtr/gocapability v0.0.0-20180916011248-d98352740cb2/go.mod h1:hkRG
github.com/syndtr/gocapability v0.0.0-20200815063812-42c35b437635/go.mod h1:hkRG7XYTFWNJGYcbNJQlaLq0fg1yr4J4t/NcTQtrfww=
github.com/tchap/go-patricia v2.2.6+incompatible/go.mod h1:bmLyhP68RS6kStMGxByiQ23RP/odRBOTVjwp2cDyi6I=
github.com/tidwall/pretty v1.0.0/go.mod h1:XNkn88O1ChpSDQmQeStsy+sBenx6DDtFZJxhVysOjyk=
github.com/tklauser/go-sysconf v0.3.14 h1:g5vzr9iPFFz24v2KZXs/pvpvh8/V9Fw6vQK5ZZb78yU=
github.com/tklauser/go-sysconf v0.3.14/go.mod h1:1ym4lWMLUOhuBOPGtRcJm7tEGX4SCYNEEEtghGG/8uY=
github.com/tklauser/numcpus v0.8.0 h1:Mx4Wwe/FjZLeQsK/6kt2EOepwwSl7SmJrK5bV/dXYgY=
github.com/tklauser/numcpus v0.8.0/go.mod h1:ZJZlAY+dmR4eut8epnzf0u/VwodKmryxR8txiloSqBE=
github.com/tmc/grpc-websocket-proxy v0.0.0-20170815181823-89b8d40f7ca8/go.mod h1:ncp9v5uamzpCO7NfCPTXjqaC+bZgJeR0sMTm6dMHP7U=
github.com/tmc/grpc-websocket-proxy v0.0.0-20190109142713-0ad062ec5ee5/go.mod h1:ncp9v5uamzpCO7NfCPTXjqaC+bZgJeR0sMTm6dMHP7U=
github.com/twinj/uuid v1.0.0 h1:fzz7COZnDrXGTAOHGuUGYd6sG+JMq+AoE7+Jlu0przk=
Expand All @@ -782,6 +790,8 @@ github.com/xiang90/probing v0.0.0-20190116061207-43a291ad63a2/go.mod h1:UETIi67q
github.com/xordataexchange/crypt v0.0.3-0.20170626215501-b2862e3d0a77/go.mod h1:aYKd//L2LvnjZzWKhF00oedf4jCCReLcmhLdhm1A27Q=
github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
github.com/yusufpapurcu/wmi v1.2.4 h1:zFUKzehAFReQwLys1b/iSMl+JQGSCSjtVqQn9bBrPo0=
github.com/yusufpapurcu/wmi v1.2.4/go.mod h1:SBZ9tNy3G9/m5Oi98Zks0QjeHVDvuK0qfxQmPyzfmi0=
github.com/yvasiyarov/go-metrics v0.0.0-20140926110328-57bccd1ccd43/go.mod h1:aX5oPXxHm3bOH+xeAttToC8pqch2ScQN/JoXYupl6xs=
github.com/yvasiyarov/gorelic v0.0.0-20141212073537-a9bba5b9ab50/go.mod h1:NUSPSUX/bi6SeDMUh6brw0nXpxHnc96TguQh0+r/ssA=
github.com/yvasiyarov/newrelic_platform_go v0.0.0-20140908184405-b21fdbd4370f/go.mod h1:GlGEuHIJweS1mbCqG+7vt2nvWLzLLnRHbXz5JKd/Qbg=
Expand Down

0 comments on commit fe8e6e9

Please sign in to comment.