Skip to content

Commit 4fd5487

Browse files
committed
remove time.sleep
1 parent 331f4d1 commit 4fd5487

File tree

2 files changed

+108
-3
lines changed

2 files changed

+108
-3
lines changed

main.go

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
package main
2+
3+
import (
4+
"bytes"
5+
"encoding/json"
6+
"fmt"
7+
"io"
8+
"net/http"
9+
"time"
10+
11+
"k8s.io/klog"
12+
)
13+
14+
const (
15+
ClusterIP = "http://34.8.181.238" // Replace with your actual cluster IP
16+
ClusterID = "af7863cd-3097-434e-9325-c1011a2c9366" // Replace with your actual cluster ID
17+
ClusterSecret = "HZn4ktklT8ux2xab41hH" // Replace with your actual cluster secret
18+
)
19+
20+
func CallSBCLI(method, path string, args interface{}) (interface{}, error) {
21+
22+
data := []byte(`{}`)
23+
var err error
24+
25+
if args != nil {
26+
data, err = json.Marshal(args)
27+
if err != nil {
28+
return nil, fmt.Errorf("%s: %w", method, err)
29+
}
30+
} else {
31+
data = nil
32+
}
33+
34+
requestURL := fmt.Sprintf("%s/%s", ClusterIP, path)
35+
klog.Infof("Calling Simplyblock API: Method: %s: RequestURL: %s: Body: %s\n", method, requestURL, string(data))
36+
37+
req, err := http.NewRequest("GET", requestURL, bytes.NewReader(data))
38+
if err != nil {
39+
return nil, fmt.Errorf("%s: %w", method, err)
40+
}
41+
42+
// authHeader := fmt.Sprintf("%s %s", ClusterID, ClusterSecret)
43+
44+
// req.Header.Add("Authorization", authHeader)
45+
req.Header.Set("Content-Type", "application/json")
46+
47+
for key, values := range req.Header {
48+
for _, value := range values {
49+
fmt.Printf("Header: %s: %s\n", key, value)
50+
}
51+
}
52+
53+
httpClient := &http.Client{Timeout: 10 * time.Second}
54+
//print request object
55+
fmt.Printf("Request: %s %s, Headers: %v, Body: %s", method, requestURL, req.Header, string(data))
56+
// resp, err := httpClient.Get(requestURL)
57+
// if err != nil {
58+
// return nil, fmt.Errorf("%s: %w", method, err)
59+
// }
60+
61+
resp, err := httpClient.Do(req)
62+
if err != nil {
63+
return nil, fmt.Errorf("%s: %w", method, err)
64+
}
65+
66+
defer resp.Body.Close()
67+
if resp.StatusCode >= http.StatusInternalServerError {
68+
return nil, fmt.Errorf("%s: HTTP error code: %d", method, resp.StatusCode)
69+
}
70+
71+
var response struct {
72+
Result any `json:"result"`
73+
Results any `json:"results"`
74+
Error string `json:"error"`
75+
}
76+
77+
// copy resp.Body object
78+
copiedBody, err := io.ReadAll(resp.Body)
79+
if err != nil {
80+
return nil, fmt.Errorf("%s: HTTP error code: %d Error: %w", method, resp.StatusCode, err)
81+
}
82+
fmt.Println("copiedBody:", string(copiedBody), "for request:", requestURL)
83+
resp.Body = io.NopCloser(bytes.NewBuffer(copiedBody))
84+
85+
err = json.NewDecoder(resp.Body).Decode(&response)
86+
if err != nil {
87+
return nil, fmt.Errorf("%s: HTTP error code: %d Error: %w", method, resp.StatusCode, err)
88+
}
89+
90+
if resp.StatusCode >= http.StatusBadRequest {
91+
return nil, fmt.Errorf("%s: HTTP error code: %d Error: %s", method, resp.StatusCode, response.Error)
92+
}
93+
94+
if response.Result != nil {
95+
return response.Result, nil
96+
}
97+
return response.Results, nil
98+
}
99+
100+
func main() {
101+
resp, err := CallSBCLI("GET", "lvol/"+"ce6cb6a1-0c63-43e9-a095-66db8f2f4997/", nil)
102+
if err != nil {
103+
klog.Errorf("Error calling SBCLI: %v", err)
104+
} else {
105+
klog.Info("Successfully called SBCLI")
106+
}
107+
fmt.Printf("Response: %v\n", resp)
108+
}

pkg/spdk/controllerserver.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ import (
2222
"fmt"
2323
"strconv"
2424
"strings"
25-
"time"
2625

2726
"github.com/container-storage-interface/spec/lib/go/csi"
2827
"google.golang.org/grpc/codes"
@@ -373,8 +372,6 @@ func (cs *controllerServer) publishVolume(volumeID string) (map[string]string, e
373372
return nil, err
374373
}
375374
fmt.Println("spdkVol:", spdkVol)
376-
time.Sleep(30 * time.Second) // simulate some delay for debugging
377-
fmt.Println("slept for 30secs")
378375
err = cs.spdkNode.PublishVolume(spdkVol.lvolID)
379376
if err != nil {
380377
return nil, err

0 commit comments

Comments
 (0)