|
| 1 | +package utilities |
| 2 | + |
| 3 | +import ( |
| 4 | + "crypto/tls" |
| 5 | + "crypto/x509" |
| 6 | + "errors" |
| 7 | + "fmt" |
| 8 | + "io" |
| 9 | + "log" |
| 10 | + "net/http" |
| 11 | + "net/url" |
| 12 | + "os" |
| 13 | + "strings" |
| 14 | +) |
| 15 | + |
| 16 | +// GetCert |
| 17 | +// @Description get certificate for URL request |
| 18 | +// @Create jianl Oct 22 2025 |
| 19 | +// @Param kubeConfigFolder string The certificate folder which contains key.crt, client.crt, ca.ct |
| 20 | +// @Return (Instance of http.Transport, error) |
| 21 | +func GetCert(kubeConfigFolder string) (*http.Transport, error) { |
| 22 | + // 1. Load the client certificate and private key |
| 23 | + clientCert, err := tls.LoadX509KeyPair(kubeConfigFolder+"/client.crt", kubeConfigFolder+"/key.crt") |
| 24 | + if err != nil { |
| 25 | + log.Fatalf("Error loading client certificate and key: %v", err) |
| 26 | + return nil, errors.New("Error loading client certificate and key") |
| 27 | + } |
| 28 | + |
| 29 | + // 2. Load the CA certificate(s) to trust a custom CA |
| 30 | + caCert, err := os.ReadFile(kubeConfigFolder + "/ca.ct") |
| 31 | + if err != nil { |
| 32 | + log.Fatalf("Error loading CA certificate: %v.", err) |
| 33 | + return nil, errors.New("Error loading CA certificate") |
| 34 | + } |
| 35 | + caCertPool := x509.NewCertPool() |
| 36 | + if caCert != nil { |
| 37 | + caCertPool.AppendCertsFromPEM(caCert) |
| 38 | + } |
| 39 | + |
| 40 | + // 3. Create a tls.Config with the client certificate and trusted CAs |
| 41 | + tlsConfig := &tls.Config{ |
| 42 | + Certificates: []tls.Certificate{clientCert}, |
| 43 | + RootCAs: caCertPool, // Only needed if you have a custom CA |
| 44 | + InsecureSkipVerify: true, |
| 45 | + } |
| 46 | + |
| 47 | + transport := &http.Transport{ |
| 48 | + TLSClientConfig: tlsConfig, |
| 49 | + } |
| 50 | + return transport, nil |
| 51 | +} |
| 52 | + |
| 53 | +// GetResorce |
| 54 | +// @Description get resources from an Openshift cluster |
| 55 | +// @Create jianl Oct 22 2025 |
| 56 | +// @Param server string API host |
| 57 | +// @Param transport string Instance of http.Transport which used to create http.Client |
| 58 | +// @Param api string The api endpoint |
| 59 | +// @Return (resource, error) |
| 60 | +func GetResorce(server string, transport *http.Transport, api string) (string, error) { |
| 61 | + finalUrl, err := url.JoinPath(server, api) |
| 62 | + if err != nil { |
| 63 | + fmt.Printf("Error failed to concatenate URL for request: %v\n", err) |
| 64 | + return "", err |
| 65 | + } |
| 66 | + |
| 67 | + // Create a new GET request |
| 68 | + client := &http.Client{ |
| 69 | + Transport: transport, |
| 70 | + } |
| 71 | + |
| 72 | + resp, err := client.Get(finalUrl) // Replace with your URL |
| 73 | + if err != nil { |
| 74 | + fmt.Printf("Error creating request: %v\n", err) |
| 75 | + return "", err |
| 76 | + } |
| 77 | + defer func() { |
| 78 | + if err := resp.Body.Close(); err != nil { |
| 79 | + log.Printf("Error closing response body: %v", err) |
| 80 | + } |
| 81 | + }() |
| 82 | + |
| 83 | + // Send the request |
| 84 | + body, err := io.ReadAll(resp.Body) |
| 85 | + if err != nil { |
| 86 | + fmt.Printf("Error reading response body: %v\n", err) |
| 87 | + return "", err |
| 88 | + } |
| 89 | + return string(body), nil |
| 90 | +} |
| 91 | + |
| 92 | +// PatchResorce |
| 93 | +// @Description Patch update resource |
| 94 | +// @Create jianl Oct 22 2025 |
| 95 | +// @Param server string API host |
| 96 | +// @Param transport string Instance of http.Transport which used to create http.Client |
| 97 | +// @Param api string The api endpoint |
| 98 | +// @Param jsonObject string The part of resources which will be updated |
| 99 | +// |
| 100 | +// (Now only support json format string, for example: "{\"spec\":{\"capabilities\":{\"baselineCapabilitySet\":\"None\"}}}") |
| 101 | +// |
| 102 | +// @Return (resource, error) |
| 103 | +func PatchResorce(server string, transport *http.Transport, api string, jsonObject string) (string, error) { |
| 104 | + finalUrl, err := url.JoinPath(server, api) |
| 105 | + if err != nil { |
| 106 | + fmt.Printf("Error failed to concatenate URL for request: %v\n", err) |
| 107 | + return "", err |
| 108 | + } |
| 109 | + // Create a new GET request |
| 110 | + client := &http.Client{ |
| 111 | + Transport: transport, |
| 112 | + } |
| 113 | + |
| 114 | + req, err := http.NewRequest(http.MethodPatch, finalUrl, strings.NewReader(jsonObject)) |
| 115 | + if err != nil { |
| 116 | + log.Fatal(err) |
| 117 | + } |
| 118 | + req.Header.Set("Content-Type", "application/merge-patch+json") |
| 119 | + resp, err := client.Do(req) // Replace with your URL |
| 120 | + if err != nil { |
| 121 | + fmt.Printf("Error creating request: %v\n", err) |
| 122 | + return "", err |
| 123 | + } |
| 124 | + defer func() { |
| 125 | + if err := resp.Body.Close(); err != nil { |
| 126 | + log.Printf("Error closing response body: %v", err) |
| 127 | + } |
| 128 | + }() |
| 129 | + |
| 130 | + // Send the request |
| 131 | + body, err := io.ReadAll(resp.Body) |
| 132 | + if err != nil { |
| 133 | + fmt.Printf("Error reading response body: %v\n", err) |
| 134 | + return "", err |
| 135 | + } |
| 136 | + return string(body), nil |
| 137 | +} |
0 commit comments