-
Notifications
You must be signed in to change notification settings - Fork 16
/
deploy_builder.go
127 lines (110 loc) · 3.68 KB
/
deploy_builder.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
package akkacluster
import (
"encoding/json"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
rbac "k8s.io/api/rbac/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
appv1alpha1 "github.com/lightbend/akka-cluster-operator/pkg/apis/app/v1alpha1"
)
// GenericResource have both meta and runtime interfaces
type GenericResource interface {
metav1.Object
runtime.Object
}
// allGeneratedResourceTypes returns list of GenericResources that might be generated in future.
// This allows the controller to get a list of secondary resource types to watch.
func allPossibleGeneratedResourceTypes() []GenericResource {
return []GenericResource{
&appsv1.Deployment{},
&rbac.RoleBinding{},
&rbac.Role{},
&corev1.ServiceAccount{},
}
}
// generateResources produces a list of rbac and deployment resources suitable for akkaCluster.
// If akkaCluster resource does not specify needed options, we provide defaults. Note that these
// objects are used as a subset reference for testing cluster object correctness, so be careful
// not to fill in ephemeral fields here like timestamps, uuids. Return the expected reference objects.
func generateResources(akkaCluster *appv1alpha1.AkkaCluster) []GenericResource {
resources := []GenericResource{}
// if akkaCluster has no serviceAccount, generate rbac resources
if akkaCluster.Spec.Template.Spec.ServiceAccountName == "" {
// serviceAccount
serviceAccount := &corev1.ServiceAccount{}
serviceAccount.Name = akkaCluster.Name
serviceAccount.Namespace = akkaCluster.Namespace
// role
role := &rbac.Role{}
role.Name = akkaCluster.Name
role.Namespace = akkaCluster.Namespace
role.Rules = []rbac.PolicyRule{
{
APIGroups: []string{""},
Resources: []string{"pods"},
Verbs: []string{"get", "watch", "list"},
},
}
// rolebinding
roleBinding := &rbac.RoleBinding{}
roleBinding.Name = akkaCluster.Name
roleBinding.Namespace = akkaCluster.Namespace
roleBinding.RoleRef = rbac.RoleRef{
APIGroup: "rbac.authorization.k8s.io",
Kind: "Role",
Name: role.Name,
}
roleBinding.Subjects = []rbac.Subject{
{
Kind: "ServiceAccount",
Name: serviceAccount.Name,
},
}
// connect to pod spec
akkaCluster.Spec.Template.Spec.ServiceAccountName = serviceAccount.Name
// enqueue rbac resources for creation later
resources = append(resources, serviceAccount, role, roleBinding)
}
// default label selector, if none given
if akkaCluster.Spec.Selector == nil {
selectorKey := "app"
akkaCluster.Spec.Selector = &metav1.LabelSelector{
MatchLabels: map[string]string{
selectorKey: akkaCluster.Name,
},
}
if akkaCluster.Spec.Template.Labels == nil {
akkaCluster.Spec.Template.Labels = make(map[string]string)
}
akkaCluster.Spec.Template.Labels[selectorKey] = akkaCluster.Name
}
// default strategy, if none given
if akkaCluster.Spec.Strategy.Type == "" {
// use json serializer here as easier to read
json.Unmarshal([]byte(`{
"type": "`+appsv1.RollingUpdateDeploymentStrategyType+`",
"rollingUpdate": {
"maxSurge": 1,
"maxUnavailable": 0
}
}`), &akkaCluster.Spec.Strategy)
}
// env settings
for i := range akkaCluster.Spec.Template.Spec.Containers {
akkaCluster.Spec.Template.Spec.Containers[i].Env = append(akkaCluster.Spec.Template.Spec.Containers[i].Env,
corev1.EnvVar{
Name: "AKKA_CLUSTER_BOOTSTRAP_SERVICE_NAME",
Value: akkaCluster.Name,
},
// TODO CONTACT_PT_NR
)
}
// set up deployment spec
deployment := &appsv1.Deployment{}
deployment.Name = akkaCluster.Name
deployment.Namespace = akkaCluster.Namespace
deployment.Spec = akkaCluster.Spec
resources = append(resources, deployment)
return resources
}