-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrelease.go
81 lines (73 loc) · 1.89 KB
/
release.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
package api
import (
"fmt"
"k8s.io/client-go/1.4/kubernetes"
v1types "k8s.io/client-go/1.4/pkg/api/v1"
"k8s.io/client-go/1.4/rest"
)
var (
ErrNoBuildToPublish = &ReleaseError{"no build to publish with this release"}
)
type ReleaseError struct {
Message string
}
func (r *ReleaseError) Error() string {
return fmt.Sprintf("could not publish release: %s", r.Message)
}
// Release represents a snapshot of an application's build and config artifacts, which is
// immediately ready for execution in the execution environment.
//
// Releases are an append-only ledger and a release cannot be mutated once it is created.
// Any change must create a new release.
type Release struct {
App *App `json:"-"`
Build *Build `json:"-"`
Config *Config `json:"-"`
Version int `json:"version"`
}
func (r *Release) String() string {
return fmt.Sprintf("%s_v%d", r.App.ID, r.Version)
}
// Publish publishes the release to kubernetes.
func (r *Release) Publish() error {
if r.Build == nil {
return ErrNoBuildToPublish
}
config, err := rest.InClusterConfig()
if err != nil {
return err
}
clientset, err := kubernetes.NewForConfig(config)
if err != nil {
return err
}
for typ, command := range r.Build.Procfile {
podName := fmt.Sprintf("%s_%s", r.String(), typ)
pod := &v1types.Pod{
ObjectMeta: v1types.ObjectMeta{
Name: podName,
Namespace: r.App.ID,
Labels: map[string]string{
"heritage": "deis",
},
},
Spec: v1types.PodSpec{
RestartPolicy: v1types.RestartPolicyAlways,
Containers: []v1types.Container{
v1types.Container{
Name: podName,
Image: r.Build.Image,
ImagePullPolicy: v1types.PullAlways,
Command: command,
Env: r.Config.Values,
},
},
},
}
// Schedule the pod
if _, err := clientset.Pods(r.App.ID).Create(pod); err != nil {
return err
}
}
return nil
}