/
create_lease.go
71 lines (62 loc) · 2.12 KB
/
create_lease.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
package api
import (
"encoding/base64"
"encoding/json"
"io"
"time"
"github.com/deis/k8s-claimer/k8s"
)
// CreateLeaseReq is the encoding/json compatible struct that represents the POST /lease
// request body
type CreateLeaseReq struct {
MaxTimeSec int `json:"max_time"`
ClusterRegex string `json:"cluster_regex"`
ClusterVersion string `json:"cluster_version"`
}
// MaxTimeDur returns the maximum time specified in c as a time.Duration
func (c CreateLeaseReq) MaxTimeDur() time.Duration {
return time.Duration(c.MaxTimeSec) * time.Second
}
// ExpirationTime is a convenience function for start.Add(c.MaxTimeDur())
func (c CreateLeaseReq) ExpirationTime(start time.Time) time.Time {
return start.Add(c.MaxTimeDur())
}
// CreateLeaseResp is the encoding/json compatible struct that represents the POST /lease
// response body
type CreateLeaseResp struct {
KubeConfigStr string `json:"kubeconfig"`
IP string `json:"ip"`
Token string `json:"uuid"`
ClusterName string `json:"cluster_name"`
ClusterVersion string `json:"cluster_version"`
}
// DecodeCreateLeaseResp decodes rdr from its JSON representation into a CreateLeaseResp.
// If there was any error reading rdr or it had malformed JSON, returns nil and the error
func DecodeCreateLeaseResp(rdr io.Reader) (*CreateLeaseResp, error) {
ret := new(CreateLeaseResp)
if err := json.NewDecoder(rdr).Decode(ret); err != nil {
return nil, err
}
return ret, nil
}
// KubeConfigBytes decodes c.KubeConfig by the RFC 4648 standard.
// See http://tools.ietf.org/html/rfc4648 for more information
func (c CreateLeaseResp) KubeConfigBytes() ([]byte, error) {
kubeConfigBytes, err := base64.StdEncoding.DecodeString(c.KubeConfigStr)
if err != nil {
return nil, err
}
return kubeConfigBytes, nil
}
// KubeConfig returns decoded and unmarshalled Kubernetes client configuration
func (c CreateLeaseResp) KubeConfig() (*k8s.KubeConfig, error) {
configBytes, err := c.KubeConfigBytes()
if err != nil {
return nil, err
}
kubeConfig := &k8s.KubeConfig{}
if err := json.Unmarshal(configBytes, kubeConfig); err != nil {
return nil, err
}
return kubeConfig, nil
}