@@ -2,26 +2,19 @@ package v1
22
33import (
44 "fmt"
5- "math"
6- "net/http"
75 "strconv"
86 "time"
97
108 "bytetrade.io/web3os/bfl/internal/log"
119 "bytetrade.io/web3os/bfl/pkg/api"
1210 "bytetrade.io/web3os/bfl/pkg/api/response"
1311 "bytetrade.io/web3os/bfl/pkg/apis"
14- "bytetrade.io/web3os/bfl/pkg/apis/backend/v1/metrics"
15- iamV1alpha1 "bytetrade.io/web3os/bfl/pkg/apis/iam/v1alpha1"
1612 "bytetrade.io/web3os/bfl/pkg/apis/iam/v1alpha1/operator"
17- monitov1alpha1 "bytetrade.io/web3os/bfl/pkg/apis/monitor/v1alpha1"
1813 "bytetrade.io/web3os/bfl/pkg/apiserver/runtime"
1914 "bytetrade.io/web3os/bfl/pkg/app_service/v1"
2015 "bytetrade.io/web3os/bfl/pkg/client/clientset/v1alpha1"
2116 "bytetrade.io/web3os/bfl/pkg/constants"
22- "bytetrade.io/web3os/bfl/pkg/utils"
2317 "bytetrade.io/web3os/bfl/pkg/utils/certmanager"
24- "bytetrade.io/web3os/bfl/pkg/utils/k8sutil"
2518
2619 iamV1alpha2 "github.com/beclab/api/iam/v1alpha2"
2720 "github.com/emicklei/go-restful/v3"
@@ -42,53 +35,6 @@ func New() *Handler {
4235 }
4336}
4437
45- func (h * Handler ) handleGetIPAddress (req * restful.Request , resp * restful.Response ) {
46- ctx := req .Request .Context ()
47- master := req .QueryParameter ("master" )
48-
49- var masterInternalIP string
50-
51- if master == "true" {
52- ip , err := k8sutil .GetL4ProxyNodeIP (ctx , 10 * time .Second )
53- if err != nil {
54- log .Warnf ("no master hostIP: %v" , err )
55- }
56-
57- if ip != nil && * ip != "" {
58- masterInternalIP = * ip
59- }
60-
61- resp .Write ([]byte (masterInternalIP ))
62- return
63- }
64-
65- ipAddr := IPAddress {
66- IsNatted : constants .IsNatted ,
67- Internal : utils .RemoteIp (req .Request ),
68- }
69-
70- if masterInternalIP != "" {
71- ipAddr .MasterInternalIP = masterInternalIP
72- }
73-
74- // master external ip
75- masterExternalIP := k8sutil .GetMasterExternalIP (ctx )
76- if masterExternalIP == nil {
77- response .HandleError (resp , errors .New ("no master external ip" ))
78- return
79- }
80- ipAddr .MasterExternalIP = * masterExternalIP
81-
82- external := utils .GetMyExternalIPAddr ()
83- if external == "" {
84- response .HandleInternalError (resp , errors .New ("no external ip address" ))
85- return
86- }
87- ipAddr .External = external
88-
89- response .Success (resp , ipAddr )
90- }
91-
9238func (h * Handler ) handleUserInfo (req * restful.Request , resp * restful.Response ) {
9339 var (
9440 isEphemeral bool
@@ -154,50 +100,6 @@ func (h *Handler) handleUserInfo(req *restful.Request, resp *restful.Response) {
154100 response .Success (resp , uInfo )
155101}
156102
157- func (h * Handler ) handleVerifyUserPassword (req * restful.Request , resp * restful.Response ) {
158- var u iamV1alpha1.UserPassword
159- err := req .ReadEntity (& u )
160- if err != nil {
161- response .HandleBadRequest (resp , errors .Errorf ("verify password: %v" , err ))
162- return
163- }
164-
165- log .Info ("verify user password" )
166-
167- if u .UserName == "" {
168- u .UserName = constants .Username
169- }
170-
171- // the user's password must be provided
172- if u .Password == "" {
173- response .HandleBadRequest (resp , errors .New ("verify password: no password provided" ))
174- return
175- }
176-
177- data := map [string ]string {
178- "username" : u .UserName ,
179- "password" : u .Password ,
180- "client_id" : constants .KubeSphereClientID ,
181- "client_secret" : constants .KubeSphereClientSecret ,
182- "grant_type" : "password" ,
183- }
184-
185- token , code , err := iamV1alpha1 .RequestToken ("" , data )
186- if err != nil {
187- resp .WriteHeaderAndEntity (http .StatusOK , response.Header {
188- Code : code ,
189- Message : err .Error (),
190- })
191- return
192- }
193-
194- if token .AccessToken != "" {
195- response .SuccessNoData (resp )
196- return
197- }
198- response .HandleUnauthorized (resp , errors .New (response .UnexpectedError ))
199- }
200-
201103func (h * Handler ) handleReDownloadCert (req * restful.Request , resp * restful.Response ) {
202104 var (
203105 ctx = req .Request .Context ()
@@ -469,68 +371,3 @@ func (h *Handler) myapps(req *restful.Request, resp *restful.Response) {
469371 response .Success (resp , api .NewListResult (list ))
470372
471373}
472-
473- func (h * Handler ) getClusterMetric (req * restful.Request , resp * restful.Response ) {
474- prome , err := metrics .NewPrometheus (metrics .PrometheusEndpoint )
475- if err != nil {
476- response .HandleError (resp , err )
477- return
478- }
479-
480- opts := metrics.QueryOptions {
481- Level : metrics .LevelCluster ,
482- }
483-
484- metricsResult := prome .GetNamedMetrics (req .Request .Context (), []string {
485- "cluster_cpu_usage" ,
486- "cluster_cpu_total" ,
487- "cluster_disk_size_usage" ,
488- "cluster_disk_size_capacity" ,
489- "cluster_memory_total" ,
490- "cluster_memory_usage_wo_cache" ,
491- "cluster_net_bytes_transmitted" ,
492- "cluster_net_bytes_received" ,
493- }, time .Now (), opts )
494-
495- var clusterMetrics monitov1alpha1.ClusterMetrics
496- for _ , m := range metricsResult {
497- switch m .MetricName {
498- case "cluster_cpu_usage" :
499- clusterMetrics .CPU .Usage = metrics .GetValue (& m )
500- case "cluster_cpu_total" :
501- clusterMetrics .CPU .Total = metrics .GetValue (& m )
502-
503- case "cluster_disk_size_usage" :
504- clusterMetrics .Disk .Usage = metrics .GetValue (& m )
505- case "cluster_disk_size_capacity" :
506- clusterMetrics .Disk .Total = metrics .GetValue (& m )
507-
508- case "cluster_memory_total" :
509- clusterMetrics .Memory .Total = metrics .GetValue (& m )
510- case "cluster_memory_usage_wo_cache" :
511- clusterMetrics .Memory .Usage = metrics .GetValue (& m )
512-
513- case "cluster_net_bytes_transmitted" :
514- clusterMetrics .Net .Transmitted = metrics .GetValue (& m )
515-
516- case "cluster_net_bytes_received" :
517- clusterMetrics .Net .Received = metrics .GetValue (& m )
518- }
519- }
520-
521- roundToGB := func (v float64 ) float64 { return math .Round ((v / 1000000000.00 )* 100.00 ) / 100.00 }
522- fmtMetricsValue (& clusterMetrics .CPU , "Cores" , func (v float64 ) float64 { return v })
523- fmtMetricsValue (& clusterMetrics .Memory , "GB" , roundToGB )
524- fmtMetricsValue (& clusterMetrics .Disk , "GB" , roundToGB )
525-
526- response .Success (resp , clusterMetrics )
527-
528- }
529-
530- func fmtMetricsValue (v * monitov1alpha1.MetricV , unit string , unitFunc func (float64 ) float64 ) {
531- v .Unit = unit
532-
533- v .Usage = unitFunc (v .Usage )
534- v .Total = unitFunc (v .Total )
535- v .Ratio = math .Round ((v .Usage / v .Total ) * 100 )
536- }
0 commit comments