-
Notifications
You must be signed in to change notification settings - Fork 605
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add healthy server for liveness probe and isolate file.
Signed-off-by: huozhixin.hzx <huozhixin.hzx@alibaba-inc.com>
- Loading branch information
huozhixin.hzx
committed
Mar 29, 2024
1 parent
21ca876
commit ad934c1
Showing
15 changed files
with
1,212 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
package resource | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"io/ioutil" | ||
"log" | ||
"net/http" | ||
"os" | ||
"strconv" | ||
"time" | ||
) | ||
|
||
const ( | ||
DevicePluginConfigPath = "/etc/nvidia-device-plugin/" | ||
IsolatedDevicesFilePath = "/etc/nvidia-device-plugin/unhealthyDevices.json" | ||
HealthyServerPort = "7123" | ||
) | ||
|
||
type UnhealthyDevices struct { | ||
GPUIndex []string `json:"index"` | ||
GPUUuid []string `json:"uuid"` | ||
} | ||
|
||
type HealthServer struct { | ||
httpServer *http.Server | ||
mux *http.ServeMux | ||
} | ||
|
||
func NewHealthServer(portString string) (*HealthServer, error) { | ||
port, err := strconv.Atoi(portString) | ||
if err != nil { | ||
log.Println("Port set for health server is invalid.") | ||
return nil, err | ||
} | ||
if port > 65535 || port < 1 { | ||
return nil, fmt.Errorf("port set for health server is invalid, it should be in [1, 65535]") | ||
} | ||
|
||
healthServer := &HealthServer{ | ||
httpServer: &http.Server{ | ||
Addr: fmt.Sprintf(":%v", port), | ||
}, | ||
mux: http.NewServeMux(), | ||
} | ||
healthServer.init() | ||
|
||
return healthServer, nil | ||
} | ||
|
||
func (h *HealthServer) init() { | ||
h.mux.HandleFunc("/health", h.serveHealthyHandler) | ||
h.httpServer.Handler = h.mux | ||
} | ||
|
||
func (h *HealthServer) Serve() error { | ||
return h.httpServer.ListenAndServe() | ||
} | ||
|
||
func (h *HealthServer) serveHealthyHandler(w http.ResponseWriter, r *http.Request) { | ||
w.WriteHeader(http.StatusOK) | ||
} | ||
|
||
func FindUnhealthyDevices() (*UnhealthyDevices, error) { | ||
_, err := os.Stat(IsolatedDevicesFilePath) | ||
if os.IsNotExist(err) { | ||
return nil, nil | ||
} | ||
|
||
unhealthyDevices := UnhealthyDevices{} | ||
// To wait for write file | ||
time.Sleep(3 * time.Second) | ||
|
||
jsonData, err := ioutil.ReadFile(IsolatedDevicesFilePath) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to read file %s", IsolatedDevicesFilePath) | ||
} | ||
|
||
err = json.Unmarshal(jsonData, &unhealthyDevices) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to unmarshal json file %s", IsolatedDevicesFilePath) | ||
} | ||
|
||
return &unhealthyDevices, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.