forked from knative/serving
/
service.go
200 lines (173 loc) · 5.64 KB
/
service.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
/*
Copyright 2019 The Knative Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package v1
import (
"context"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"knative.dev/pkg/ptr"
v1 "knative.dev/serving/pkg/apis/serving/v1"
presources "knative.dev/serving/pkg/resources"
)
// ServiceOption enables further configuration of a Service.
type ServiceOption func(*v1.Service)
// Service creates a service with ServiceOptions
func Service(name, namespace string, so ...ServiceOption) *v1.Service {
s := &v1.Service{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Namespace: namespace,
},
}
for _, opt := range so {
opt(s)
}
s.SetDefaults(context.Background())
return s
}
// ServiceWithoutNamespace creates a service with ServiceOptions but without a specific namespace
func ServiceWithoutNamespace(name string, so ...ServiceOption) *v1.Service {
s := &v1.Service{
ObjectMeta: metav1.ObjectMeta{
Name: name,
},
}
for _, opt := range so {
opt(s)
}
s.SetDefaults(context.Background())
return s
}
// WithInlineConfigSpec confgures the Service to use the given config spec
func WithInlineConfigSpec(config v1.ConfigurationSpec) ServiceOption {
return func(svc *v1.Service) {
svc.Spec.ConfigurationSpec = config
}
}
// WithNamedPort sets the name on the Service's port to the provided name
func WithNamedPort(name string) ServiceOption {
return func(svc *v1.Service) {
c := &svc.Spec.Template.Spec.Containers[0]
if len(c.Ports) == 1 {
c.Ports[0].Name = name
} else {
c.Ports = []corev1.ContainerPort{{
Name: name,
}}
}
}
}
// WithNumberedPort sets the Service's port number to what's provided.
func WithNumberedPort(number int32) ServiceOption {
return func(svc *v1.Service) {
c := &svc.Spec.Template.Spec.Containers[0]
if len(c.Ports) == 1 {
c.Ports[0].ContainerPort = number
} else {
c.Ports = []corev1.ContainerPort{{
ContainerPort: number,
}}
}
}
}
// WithResourceRequirements attaches resource requirements to the service
func WithResourceRequirements(resourceRequirements corev1.ResourceRequirements) ServiceOption {
return func(svc *v1.Service) {
svc.Spec.Template.Spec.Containers[0].Resources = resourceRequirements
}
}
// WithServiceAnnotation adds the given annotation to the service.
func WithServiceAnnotation(k, v string) ServiceOption {
return func(svc *v1.Service) {
svc.Annotations = presources.UnionMaps(svc.Annotations, map[string]string{
k: v,
})
}
}
// WithServiceAnnotationRemoved adds the given annotation to the service.
func WithServiceAnnotationRemoved(k string) ServiceOption {
return func(svc *v1.Service) {
svc.Annotations = presources.FilterMap(svc.Annotations, func(s string) bool {
return k == s
})
}
}
// WithServiceImage sets the container image to be the provided string.
func WithServiceImage(img string) ServiceOption {
return func(svc *v1.Service) {
svc.Spec.Template.Spec.Containers[0].Image = img
}
}
// WithServiceTemplateMeta sets the container image to be the provided string.
func WithServiceTemplateMeta(m metav1.ObjectMeta) ServiceOption {
return func(svc *v1.Service) {
svc.Spec.Template.ObjectMeta = m
}
}
// WithRevisionTimeoutSeconds sets revision timeout
func WithRevisionTimeoutSeconds(revisionTimeoutSeconds int64) ServiceOption {
return func(service *v1.Service) {
service.Spec.Template.Spec.TimeoutSeconds = ptr.Int64(revisionTimeoutSeconds)
}
}
// WithServiceAccountName sets revision service account name
func WithServiceAccountName(serviceAccountName string) ServiceOption {
return func(service *v1.Service) {
service.Spec.Template.Spec.ServiceAccountName = serviceAccountName
}
}
// WithContainerConcurrency sets the given Service's concurrency.
func WithContainerConcurrency(cc int64) ServiceOption {
return func(svc *v1.Service) {
svc.Spec.Template.Spec.ContainerConcurrency = &cc
}
}
// WithVolume adds a volume to the service
func WithVolume(name, mountPath string, volumeSource corev1.VolumeSource) ServiceOption {
return func(svc *v1.Service) {
rt := &svc.Spec.ConfigurationSpec.Template.Spec
rt.Containers[0].VolumeMounts = append(rt.Containers[0].VolumeMounts,
corev1.VolumeMount{
Name: name,
MountPath: mountPath,
})
rt.Volumes = append(rt.Volumes, corev1.Volume{
Name: name,
VolumeSource: volumeSource,
})
}
}
// WithEnv configures the Service to use the provided environment variables.
func WithEnv(evs ...corev1.EnvVar) ServiceOption {
return func(s *v1.Service) {
s.Spec.Template.Spec.Containers[0].Env = evs
}
}
// WithEnvFrom configures the Service to use the provided environment variables.
func WithEnvFrom(evs ...corev1.EnvFromSource) ServiceOption {
return func(s *v1.Service) {
s.Spec.Template.Spec.Containers[0].EnvFrom = evs
}
}
// WithSecurityContext configures the Service to use the provided security context.
func WithSecurityContext(sc *corev1.SecurityContext) ServiceOption {
return func(s *v1.Service) {
s.Spec.Template.Spec.Containers[0].SecurityContext = sc
}
}
// WithWorkingDir configures the Service to use the provided working directory.
func WithWorkingDir(wd string) ServiceOption {
return func(s *v1.Service) {
s.Spec.Template.Spec.Containers[0].WorkingDir = wd
}
}