-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
99 lines (87 loc) · 2.19 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package main
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"github.com/hashicorp/go-retryablehttp"
"k8s.io/client-go/kubernetes"
"github.com/spf13/viper"
"github.com/tcotav/k8siinv/client"
"github.com/tcotav/k8siinv/types"
)
type ClientConfig struct {
ClusterName string `json:"clusterName"`
RunOutsideCluster bool `json:"runOutsideCluster"`
ConnectInfo struct {
Url string `json:"url"`
Retry int `json:"retry"`
} `json:"connectinfo"`
}
func main() {
viper.SetConfigName("clientconfig")
viper.SetConfigType("yaml")
viper.AddConfigPath("/etc/k8siinv/")
viper.AddConfigPath("$HOME/.k8siinv")
viper.AddConfigPath(".")
err := viper.ReadInConfig()
if err != nil {
log.Fatal("fatal error config file: %w", err)
}
var config ClientConfig
viper.Unmarshal(&config)
var clientset *kubernetes.Clientset
if config.RunOutsideCluster {
// create the clientset
clientset, err = client.GetOutClusterConfig()
if err != nil {
log.Fatal(err.Error())
}
} else {
// this is the expected way to run this -- as a cronjob
// create the clientset for in-cluster
clientset, err = client.GetInClusterConfig()
if err != nil {
log.Fatal(err.Error())
}
}
clusterImages, err := client.GetClusterInventory(clientset, config.ClusterName)
if err != nil {
log.Fatal(err.Error())
}
// convert it to json
b, err := json.Marshal(clusterImages)
if err != nil {
log.Fatal(err.Error())
}
fmt.Printf("%s\n", string(b))
// set up our http client
client := retryablehttp.NewClient()
client.RetryMax = config.ConnectInfo.Retry
req, err := retryablehttp.NewRequest(http.MethodPost, config.ConnectInfo.Url, bytes.NewBuffer(b))
req.Header.Set("Content-Type", "application/json")
if err != nil {
log.Fatal(err)
}
resp, err := client.Do(req)
if err != nil {
log.Fatal(err)
}
defer resp.Body.Close()
// check return code
if resp.StatusCode != 201 {
log.Fatal("Request Problem, ", resp.StatusCode)
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Fatal(err)
}
httpRet := types.HttpReturn{}
json.Unmarshal(body, &httpRet)
if httpRet.Message != "OK" {
log.Fatal("Server returned error: ", httpRet.Message)
}
log.Println("Success")
}