17
17
package api
18
18
19
19
import (
20
- "bytes"
21
20
"context"
22
21
b64 "encoding/base64"
23
22
"encoding/json"
24
23
"errors"
25
24
"fmt"
26
25
"net/http"
27
- "strings"
26
+ "net/url"
27
+ "os"
28
28
"time"
29
29
30
+ "github.com/minio/console/pkg/logger"
30
31
"github.com/minio/console/pkg/utils"
31
32
32
- "github.com/klauspost/compress/gzip"
33
- xhttp "github.com/minio/console/pkg/http"
34
33
subnet "github.com/minio/console/pkg/subnet"
35
34
"github.com/minio/madmin-go/v3"
35
+ mc "github.com/minio/mc/cmd"
36
36
"github.com/minio/websocket"
37
37
)
38
38
@@ -63,7 +63,7 @@ func startHealthInfo(ctx context.Context, conn WSConn, client MinioAdmin, deadli
63
63
return err
64
64
}
65
65
66
- compressedDiag , err := tarGZ (healthInfo , version )
66
+ compressedDiag , err := mc . TarGZHealthInfo (healthInfo , version )
67
67
if err != nil {
68
68
return err
69
69
}
@@ -75,11 +75,11 @@ func startHealthInfo(ctx context.Context, conn WSConn, client MinioAdmin, deadli
75
75
}
76
76
77
77
ctx = context .WithValue (ctx , utils .ContextClientIP , conn .remoteAddress ())
78
- subnetResp , err : = sendHealthInfoToSubnet (ctx , healthInfo , client )
78
+ err = sendHealthInfoToSubnet (ctx , healthInfo , client )
79
79
report := messageReport {
80
80
Encoded : encodedDiag ,
81
81
ServerHealthInfo : healthInfo ,
82
- SubnetResponse : subnetResp ,
82
+ SubnetResponse : mc . SubnetBaseURL () + "/health" ,
83
83
}
84
84
if err != nil {
85
85
report .SubnetResponse = fmt .Sprintf ("Error: %s" , err .Error ())
@@ -94,31 +94,6 @@ func startHealthInfo(ctx context.Context, conn WSConn, client MinioAdmin, deadli
94
94
return conn .writeMessage (websocket .TextMessage , message )
95
95
}
96
96
97
- // compress and tar MinIO diagnostics output
98
- func tarGZ (healthInfo interface {}, version string ) ([]byte , error ) {
99
- buffer := bytes .NewBuffer (nil )
100
- gzWriter := gzip .NewWriter (buffer )
101
-
102
- enc := json .NewEncoder (gzWriter )
103
-
104
- header := struct {
105
- Version string `json:"version"`
106
- }{Version : version }
107
-
108
- if err := enc .Encode (header ); err != nil {
109
- return nil , err
110
- }
111
-
112
- if err := enc .Encode (healthInfo ); err != nil {
113
- return nil , err
114
- }
115
- err := gzWriter .Close ()
116
- if err != nil {
117
- return nil , err
118
- }
119
- return buffer .Bytes (), nil
120
- }
121
-
122
97
// getHealthInfoOptionsFromReq gets duration for startHealthInfo request
123
98
// path come as : `/health-info?deadline=2h`
124
99
func getHealthInfoOptionsFromReq (req * http.Request ) (* time.Duration , error ) {
@@ -129,49 +104,69 @@ func getHealthInfoOptionsFromReq(req *http.Request) (*time.Duration, error) {
129
104
return & deadlineDuration , nil
130
105
}
131
106
132
- func sendHealthInfoToSubnet (ctx context.Context , healthInfo interface {}, client MinioAdmin ) (string , error ) {
133
- filename := fmt .Sprintf ("health_%d.json" , time .Now ().Unix ())
134
-
135
- clientIP := utils .ClientIPFromContext (ctx )
107
+ func updateMcGlobals (subnetTokenConfig subnet.LicenseTokenConfig ) error {
108
+ mc .GlobalDevMode = getConsoleDevMode ()
109
+ if len (subnetTokenConfig .Proxy ) > 0 {
110
+ proxyURL , e := url .Parse (subnetTokenConfig .Proxy )
111
+ if e != nil {
112
+ return e
113
+ }
114
+ mc .GlobalSubnetProxyURL = proxyURL
115
+ }
116
+ return nil
117
+ }
136
118
137
- subnetUploadURL := subnet . UploadURL ( "health" , filename )
138
- subnetHTTPClient := & xhttp. Client { Client : GetConsoleHTTPClient ( " " , clientIP )}
119
+ func sendHealthInfoToSubnet ( ctx context. Context , healthInfo interface {}, client MinioAdmin ) error {
120
+ filename := fmt . Sprintf ( "health_%d.json.gz " , time . Now (). Unix ())
139
121
subnetTokenConfig , e := GetSubnetKeyFromMinIOConfig (ctx , client )
140
122
if e != nil {
141
- return "" , e
123
+ return e
124
+ }
125
+ e = updateMcGlobals (* subnetTokenConfig )
126
+ if e != nil {
127
+ return e
142
128
}
143
129
var apiKey string
144
130
if len (subnetTokenConfig .APIKey ) != 0 {
145
131
apiKey = subnetTokenConfig .APIKey
146
132
} else {
147
133
apiKey , e = subnet .GetSubnetAPIKeyUsingLicense (subnetTokenConfig .License )
148
134
if e != nil {
149
- return "" , e
135
+ return e
150
136
}
151
137
}
152
- headers := subnet .UploadAuthHeaders (apiKey )
153
- uploadInfo , formDataType , e := subnet .ProcessUploadInfo (healthInfo , "health" , filename )
138
+ compressedHealthInfo , e := mc .TarGZHealthInfo (healthInfo , madmin .HealthInfoVersion )
154
139
if e != nil {
155
- return "" , e
140
+ return e
156
141
}
157
- resp , e := subnet .UploadFileToSubnet (uploadInfo , subnetHTTPClient , subnetUploadURL , headers , formDataType )
158
-
142
+ e = os .WriteFile (filename , compressedHealthInfo , 0o666 )
159
143
if e != nil {
160
- return "" , e
144
+ return e
145
+ }
146
+ headers := mc .SubnetAPIKeyAuthHeaders (apiKey )
147
+ resp , e := (& mc.SubnetFileUploader {
148
+ FilePath : filename ,
149
+ ReqURL : mc .SubnetUploadURL ("health" ),
150
+ Headers : headers ,
151
+ DeleteAfterUpload : true ,
152
+ }).UploadFileToSubnet ()
153
+ if e != nil {
154
+ // file gets deleted only if upload is successful
155
+ // so we delete explicitly here as we already have the bytes
156
+ logger .LogIf (ctx , os .Remove (filename ))
157
+ return e
161
158
}
159
+
162
160
type SubnetResponse struct {
163
- ClusterURL string `json:"cluster_url,omitempty"`
161
+ LicenseV2 string `json:"license_v2,omitempty"`
162
+ APIKey string `json:"api_key,omitempty"`
164
163
}
165
164
166
165
var subnetResp SubnetResponse
167
166
e = json .Unmarshal ([]byte (resp ), & subnetResp )
168
167
if e != nil {
169
- return "" , e
170
- }
171
- if len (subnetResp .ClusterURL ) != 0 {
172
- subnetClusterURL := strings .ReplaceAll (subnetResp .ClusterURL , "%2f" , "/" )
173
- return subnetClusterURL , nil
168
+ return e
174
169
}
175
170
176
- return "" , ErrSubnetUploadFail
171
+ return nil
177
172
}
0 commit comments