Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c5e45d3
commit f5b0348
Showing
6 changed files
with
176 additions
and
18 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,101 @@ | ||
package main | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"flag" | ||
"fmt" | ||
"math/rand" | ||
"net/http" | ||
"os" | ||
"time" | ||
) | ||
|
||
var listOnly bool | ||
|
||
type NodeList struct { | ||
Items []Node `json:"items"` | ||
} | ||
|
||
type Node struct { | ||
Metadata Metadata `json:"metadata"` | ||
} | ||
|
||
type Metadata struct { | ||
Name string `json:"name,omitempty"` | ||
Annotations map[string]string `json:"annotations"` | ||
} | ||
|
||
func main() { | ||
flag.BoolVar(&listOnly, "l", false, "List current annotations and exist") | ||
flag.Parse() | ||
|
||
prices := []string{"0.05", "0.10", "0.20", "0.40", "0.80", "1.60"} | ||
resp, err := http.Get("http://127.0.0.1:8001/api/v1/nodes") | ||
if err != nil { | ||
fmt.Println(err) | ||
os.Exit(1) | ||
} | ||
if resp.StatusCode != 200 { | ||
fmt.Println("Invalid status code", resp.Status) | ||
os.Exit(1) | ||
} | ||
|
||
var nodes NodeList | ||
decoder := json.NewDecoder(resp.Body) | ||
err = decoder.Decode(&nodes) | ||
if err != nil { | ||
fmt.Println(err) | ||
os.Exit(1) | ||
} | ||
|
||
if listOnly { | ||
for _, node := range nodes.Items { | ||
price := node.Metadata.Annotations["hightower.com/cost"] | ||
fmt.Printf("%s %s\n", node.Metadata.Name, price) | ||
} | ||
os.Exit(0) | ||
} | ||
|
||
rand.Seed(time.Now().Unix()) | ||
for _, node := range nodes.Items { | ||
price := prices[rand.Intn(len(prices))] | ||
annotations := map[string]string{ | ||
"hightower.com/cost": price, | ||
} | ||
patch := Node{ | ||
Metadata{ | ||
Annotations: annotations, | ||
}, | ||
} | ||
|
||
var b []byte | ||
body := bytes.NewBuffer(b) | ||
err := json.NewEncoder(body).Encode(patch) | ||
if err != nil { | ||
fmt.Println(err) | ||
os.Exit(1) | ||
} | ||
|
||
url := "http://127.0.0.1:8001/api/v1/nodes/" + node.Metadata.Name | ||
request, err := http.NewRequest("PATCH", url, body) | ||
if err != nil { | ||
fmt.Println(err) | ||
os.Exit(1) | ||
} | ||
request.Header.Set("Content-Type", "application/strategic-merge-patch+json") | ||
request.Header.Set("Accept", "application/json, */*") | ||
|
||
resp, err := http.DefaultClient.Do(request) | ||
if err != nil { | ||
fmt.Println(err) | ||
os.Exit(1) | ||
} | ||
|
||
if resp.StatusCode != 200 { | ||
fmt.Println(err) | ||
os.Exit(1) | ||
} | ||
fmt.Printf("%s %s\n", node.Metadata.Name, price) | ||
} | ||
} |
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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
GOOS=linux bash build | ||
docker build -t kelseyhightower/scheduler:0.2.0 . | ||
docker push kelseyhightower/scheduler:0.2.0 | ||
docker build -t kelseyhightower/scheduler:0.4.0 . | ||
docker push kelseyhightower/scheduler:0.4.0 | ||
rm scheduler |
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