-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
implement HTTP server for task processing
Signed-off-by: Dmitry Shmulevich <[email protected]>
- Loading branch information
Showing
9 changed files
with
320 additions
and
36 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
/* | ||
* Copyright (c) 2024, NVIDIA CORPORATION. All rights reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package main | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"flag" | ||
"fmt" | ||
"io" | ||
"net/http" | ||
"net/url" | ||
"os" | ||
|
||
"k8s.io/klog/v2" | ||
|
||
"github.com/NVIDIA/knavigator/pkg/config" | ||
) | ||
|
||
func mainInternal() error { | ||
var addr, taskCfg string | ||
flag.StringVar(&addr, "addr", "", "server address") | ||
flag.StringVar(&taskCfg, "tasks", "", "comma-separated list of task config files and dirs") | ||
flag.Parse() | ||
|
||
taskconfigs, err := config.NewFromPaths(taskCfg) | ||
if err != nil { | ||
return err | ||
} | ||
if len(taskconfigs) == 0 { | ||
return fmt.Errorf("missing 'tasks' argument") | ||
} | ||
|
||
taskURL, err := url.JoinPath(addr, "task") | ||
if err != nil { | ||
return err | ||
} | ||
resetURL, err := url.JoinPath(addr, "reset") | ||
if err != nil { | ||
return err | ||
} | ||
|
||
for _, taskconfig := range taskconfigs { | ||
fmt.Printf("Starting test %s\n", taskconfig.Name) | ||
if err := execTaskConfig(taskURL, resetURL, taskconfig); err != nil { | ||
return err | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func execTaskConfig(taskURL, resetURL string, taskconfig *config.TaskConfig) error { | ||
var err error | ||
for _, task := range taskconfig.Tasks { | ||
if err = execTask(taskURL, task); err != nil { | ||
break | ||
} | ||
} | ||
|
||
if errReset := execReset(resetURL); errReset != nil { | ||
if err == nil { | ||
err = errReset | ||
} | ||
} | ||
|
||
return err | ||
} | ||
|
||
func execTask(urlPath string, task *config.Task) error { | ||
jsonData, err := json.Marshal(task) | ||
if err != nil { | ||
return fmt.Errorf("failed to marshal to JSON: %v", err) | ||
} | ||
|
||
resp, err := http.Post(urlPath, "application/json", bytes.NewBuffer(jsonData)) // #nosec G107 | ||
if err != nil { | ||
return fmt.Errorf("failed to create request: %v", err) | ||
} | ||
|
||
defer resp.Body.Close() //nolint:errcheck | ||
body, err := io.ReadAll(resp.Body) | ||
if err != nil { | ||
return fmt.Errorf("failed to read response: %v", err) | ||
} | ||
|
||
if resp.StatusCode != http.StatusOK { | ||
fmt.Printf("Error response: %s: %s\n", resp.Status, body) | ||
return fmt.Errorf("%s", resp.Status) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func execReset(urlPath string) error { | ||
resp, err := http.Get(urlPath) // #nosec G107 | ||
if err != nil { | ||
return fmt.Errorf("failed to create request: %v", err) | ||
} | ||
|
||
defer func() { _ = resp.Body.Close() }() | ||
body, err := io.ReadAll(resp.Body) | ||
if err != nil { | ||
return fmt.Errorf("failed to read response: %v", err) | ||
} | ||
|
||
if resp.StatusCode != http.StatusOK { | ||
fmt.Printf("Error response: %s: %s\n", resp.Status, body) | ||
return fmt.Errorf("%s", resp.Status) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func main() { | ||
defer klog.Flush() | ||
if err := mainInternal(); err != nil { | ||
klog.Error(err.Error()) | ||
os.Exit(1) | ||
} | ||
} |
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
Oops, something went wrong.