forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.go
179 lines (152 loc) · 6.25 KB
/
util.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
package util
import (
"encoding/json"
"fmt"
"hash/adler32"
"strconv"
"github.com/golang/glog"
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
"github.com/GoogleCloudPlatform/kubernetes/pkg/labels"
"github.com/GoogleCloudPlatform/kubernetes/pkg/runtime"
"github.com/GoogleCloudPlatform/kubernetes/pkg/watch"
deployapi "github.com/openshift/origin/pkg/deploy/api"
)
// LatestDeploymentNameForConfig returns a stable identifier for config based on its version.
func LatestDeploymentNameForConfig(config *deployapi.DeploymentConfig) string {
return config.Name + "-" + strconv.Itoa(config.LatestVersion)
}
func DeployerPodNameForDeployment(deployment *api.ReplicationController) string {
return fmt.Sprintf("deploy-%s", deployment.Name)
}
// StatusForDeployment gets the DeploymentStatus for deployment from its annotations.
func StatusForDeployment(deployment *api.ReplicationController) deployapi.DeploymentStatus {
return deployapi.DeploymentStatus(deployment.Annotations[deployapi.DeploymentStatusAnnotation])
}
// LabelForDeployment builds a string identifier for a Deployment.
func LabelForDeployment(deployment *api.ReplicationController) string {
return fmt.Sprintf("%s/%s", deployment.Namespace, deployment.Name)
}
// LabelForDeploymentConfig builds a string identifier for a DeploymentConfig.
func LabelForDeploymentConfig(config *deployapi.DeploymentConfig) string {
return fmt.Sprintf("%s/%s:%d", config.Namespace, config.Name, config.LatestVersion)
}
// HashPodSpecs hashes a PodSpec into a uint64.
// TODO: Resources are currently ignored due to the formats not surviving encoding/decoding
// in a consistent manner (e.g. 0 is represented sometimes as 0.000)
func HashPodSpec(t api.PodSpec) uint64 {
// Ignore resources by making them uniformly empty
for i := range t.Containers {
t.Containers[i].Resources = api.ResourceRequirements{}
}
jsonString, err := json.Marshal(t)
if err != nil {
glog.Errorf("An error occurred marshalling pod state: %v", err)
return 0
}
hash := adler32.New()
fmt.Fprintf(hash, "%s", jsonString)
return uint64(hash.Sum32())
}
// PodSpecsEqual returns true if the given PodSpecs are the same.
func PodSpecsEqual(a, b api.PodSpec) bool {
return HashPodSpec(a) == HashPodSpec(b)
}
// DecodeDeploymentConfig decodes a DeploymentConfig from controller using codec. An error is returned
// if the controller doesn't contain an encoded config.
func DecodeDeploymentConfig(controller *api.ReplicationController, codec runtime.Codec) (*deployapi.DeploymentConfig, error) {
encodedConfig := []byte(controller.Annotations[deployapi.DeploymentEncodedConfigAnnotation])
if decoded, err := codec.Decode(encodedConfig); err == nil {
if config, ok := decoded.(*deployapi.DeploymentConfig); ok {
return config, nil
} else {
return nil, fmt.Errorf("Decoded deploymentConfig from controller is not a DeploymentConfig: %v", err)
}
} else {
return nil, fmt.Errorf("Failed to decode DeploymentConfig from controller: %v", err)
}
}
// EncodeDeploymentConfig encodes config as a string using codec.
func EncodeDeploymentConfig(config *deployapi.DeploymentConfig, codec runtime.Codec) (string, error) {
if bytes, err := codec.Encode(config); err == nil {
return string(bytes[:]), nil
} else {
return "", err
}
}
// MakeDeployment creates a deployment represented as a ReplicationController and based on the given
// DeploymentConfig. The controller replica count will be zero.
func MakeDeployment(config *deployapi.DeploymentConfig, codec runtime.Codec) (*api.ReplicationController, error) {
var err error
var encodedConfig string
if encodedConfig, err = EncodeDeploymentConfig(config, codec); err != nil {
return nil, err
}
deploymentName := LatestDeploymentNameForConfig(config)
podSpec := api.PodSpec{}
if err := api.Scheme.Convert(&config.Template.ControllerTemplate.Template.Spec, &podSpec); err != nil {
return nil, fmt.Errorf("Couldn't clone podTemplateSpec: %v", err)
}
controllerLabels := make(labels.Set)
for k, v := range config.Labels {
controllerLabels[k] = v
}
// Ensure that pods created by this deployment controller can be safely associated back
// to the controller, and that multiple deployment controllers for the same config don't
// manipulate each others' pods.
selector := map[string]string{}
for k, v := range config.Template.ControllerTemplate.Selector {
selector[k] = v
}
selector[deployapi.DeploymentConfigLabel] = config.Name
selector[deployapi.DeploymentLabel] = deploymentName
podLabels := make(labels.Set)
for k, v := range config.Template.ControllerTemplate.Template.Labels {
podLabels[k] = v
}
podLabels[deployapi.DeploymentConfigLabel] = config.Name
podLabels[deployapi.DeploymentLabel] = deploymentName
podAnnotations := make(labels.Set)
for k, v := range config.Template.ControllerTemplate.Template.Annotations {
podAnnotations[k] = v
}
podAnnotations[deployapi.DeploymentAnnotation] = deploymentName
podAnnotations[deployapi.DeploymentConfigAnnotation] = config.Name
podAnnotations[deployapi.DeploymentVersionAnnotation] = strconv.Itoa(config.LatestVersion)
deployment := &api.ReplicationController{
ObjectMeta: api.ObjectMeta{
Name: deploymentName,
Annotations: map[string]string{
deployapi.DeploymentConfigAnnotation: config.Name,
deployapi.DeploymentStatusAnnotation: string(deployapi.DeploymentStatusNew),
deployapi.DeploymentEncodedConfigAnnotation: encodedConfig,
deployapi.DeploymentVersionAnnotation: strconv.Itoa(config.LatestVersion),
},
Labels: controllerLabels,
},
Spec: api.ReplicationControllerSpec{
// The deployment should be inactive initially
Replicas: 0,
Selector: selector,
Template: &api.PodTemplateSpec{
ObjectMeta: api.ObjectMeta{
Labels: podLabels,
Annotations: podAnnotations,
},
Spec: podSpec,
},
},
}
return deployment, nil
}
// ListWatcherImpl is a pluggable ListWatcher.
// TODO: This has been incorporated upstream; replace during a future rebase.
type ListWatcherImpl struct {
ListFunc func() (runtime.Object, error)
WatchFunc func(resourceVersion string) (watch.Interface, error)
}
func (lw *ListWatcherImpl) List() (runtime.Object, error) {
return lw.ListFunc()
}
func (lw *ListWatcherImpl) Watch(resourceVersion string) (watch.Interface, error) {
return lw.WatchFunc(resourceVersion)
}