-
Notifications
You must be signed in to change notification settings - Fork 784
/
step_expose.go
362 lines (319 loc) · 11.2 KB
/
step_expose.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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
package expose
import (
"bytes"
"fmt"
"path/filepath"
"reflect"
"strconv"
"text/template"
"github.com/ghodss/yaml"
"github.com/pkg/errors"
"github.com/spf13/cobra"
corev1 "k8s.io/api/core/v1"
"k8s.io/api/extensions/v1beta1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/helm/pkg/chartutil"
"github.com/jenkins-x/jx/v2/pkg/cmd/helper"
"github.com/jenkins-x/jx/v2/pkg/cmd/opts"
"github.com/jenkins-x/jx/v2/pkg/cmd/templates"
"github.com/jenkins-x/jx/v2/pkg/config"
"github.com/jenkins-x/jx/v2/pkg/helm"
"github.com/jenkins-x/jx/v2/pkg/kube"
"github.com/jenkins-x/jx/v2/pkg/kube/services"
"github.com/jenkins-x/jx/v2/pkg/log"
"github.com/jenkins-x/jx/v2/pkg/util"
)
var (
// APIServicePathAnnotationKey the standard annotation to indicate the exposed URL should include an API path in addition to the domain name
APIServicePathAnnotationKey = "api.service.kubernetes.io/path"
// ExposeLabelKeys the labels used to indicate if a Service should be exposed
ExposeLabelKeys = []string{"fabric8.io/expose", "jenkins-x.io/expose"}
// ExposeAnnotationKey annotation on a Service to store its exposed URL
ExposeAnnotationKey = "fabric8.io/exposeUrl"
// ExposeHostNameAsAnnotationKey annotation to indicate the annotation name to expose the service host name
ExposeHostNameAsAnnotationKey = "fabric8.io/exposeHostNameAs"
// ExposePortAnnotationKey annotation to expose the service port
ExposePortAnnotationKey = "fabric8.io/exposePort"
ingressTemplateFileName = "ingress.tmpl.yaml"
)
// StepExposeOptions contains the command line flags
type StepExposeOptions struct {
*opts.CommonOptions
Dir string
IngressTemplate string
Namespace string
LabelSelector string
}
var (
stepExposeLong = templates.LongDesc(`
"This step generates Ingress resources for exposed services
`)
stepExposeExample = templates.Examples(`
# runs the expose step as part of the Staging/Production pipeline
# looking in the env folder for the ingress template 'ingress.tmpl.yaml'
jx step expose -d env
`)
)
// NewCmdStepExpose creates the command
func NewCmdStepExpose(commonOpts *opts.CommonOptions) *cobra.Command {
o := StepExposeOptions{
CommonOptions: commonOpts,
}
cmd := &cobra.Command{
Use: "expose",
Short: "This step generates Ingress resources for exposed services",
Long: stepExposeLong,
Example: stepExposeExample,
Run: func(cmd *cobra.Command, args []string) {
o.Cmd = cmd
o.Args = args
err := o.Run()
helper.CheckErr(err)
},
}
cmd.Flags().StringVarP(&o.IngressTemplate, "template", "t", "", "the go template to generate the Ingress YAML for each service")
cmd.Flags().StringVarP(&o.Dir, "dir", "d", ".", "the directory to look for the install requirements file")
cmd.Flags().StringVarP(&o.Namespace, "namespace", "", "", "the namespace that Jenkins X will be booted into. If not specified it defaults to $DEPLOY_NAMESPACE")
cmd.Flags().StringVarP(&o.LabelSelector, "selector", "s", "", "the optional label selector to only process a subset of the Service resources")
return cmd
}
// Run runs the command
func (o *StepExposeOptions) Run() error {
info := util.ColorInfo
if o.IngressTemplate == "" {
o.IngressTemplate = filepath.Join(o.Dir, ingressTemplateFileName)
}
ns, err := o.GetDeployNamespace(o.Namespace)
if err != nil {
return err
}
requirements, _, err := config.LoadRequirementsConfig(o.Dir, config.DefaultFailOnValidationError)
if err != nil {
return errors.Wrapf(err, "failed to load requirements YAML")
}
if requirements == nil {
return errors.Errorf("unable to find requirements file in '%s'", o.Dir)
}
if requirements.Cluster.Namespace == "" {
requirements.Cluster.Namespace = ns
}
// if we have no domain lets look for the old exposecontroller chart configuration in the `expose` sub chart in the
// values.yaml file
err = o.loadExposeControllerConfigFromValuesYAML(requirements)
if err != nil {
return errors.Wrapf(err, "failed to load exposecontroller configuration from helm values.yaml")
}
if requirements.Ingress.Domain == "" {
return fmt.Errorf("could not detect the domain name to expose via a 'jx-requirements.yml' file in %s or a parent directory nor in the values.yaml file in expose.config.domain", o.Dir)
}
kubeClient, err := o.KubeClient()
if err != nil {
return errors.Wrapf(err, "failed to create kubernetes client")
}
serviceInterface := kubeClient.CoreV1().Services(ns)
serviceList, err := serviceInterface.List(metav1.ListOptions{
LabelSelector: o.LabelSelector,
})
if err != nil {
if apierrors.IsNotFound(err) {
log.Logger().Warnf("No services found in namespace %s: %s", ns, err.Error())
return nil
}
return errors.Wrapf(err, "failed to query services in namespace %s", ns)
}
for _, svc := range serviceList.Items {
if !IsExposedService(&svc) {
continue
}
oldAnnotations := map[string]string{}
for k, v := range svc.Annotations {
oldAnnotations[k] = v
}
ingress, err := o.createIngress(requirements, kubeClient, ns, &svc, o.IngressTemplate)
if err != nil {
return err
}
if ingress == nil {
continue
}
exposedURL, err := o.applyIngressAndUpdateService(kubeClient, ns, &svc, ingress)
if err != nil {
return errors.Wrapf(err, "failed to modify the Service %s in namespace %s", svc.Name, ns)
}
if reflect.DeepEqual(svc.Annotations, oldAnnotations) {
log.Logger().Infof("did not need to annotate Service %s in namespace %s with the exposed URL", info(svc.Name), info(ns))
} else {
// lets save the modified service due to updating of the label
_, err = serviceInterface.Update(&svc)
if err != nil {
return errors.Wrapf(err, "failed to modify the annotated Service %s to namespace %s", svc.Name, ns)
}
log.Logger().Infof("annotated Service %s in namespace %s with the exposed URL %s", info(svc.Name), info(ns), info(exposedURL))
}
}
return nil
}
// creates an Ingres resource from a template if it exists
func (o *StepExposeOptions) createIngress(requirements *config.RequirementsConfig, kubeClient kubernetes.Interface, ns string, service *corev1.Service, fileName string) (*v1beta1.Ingress, error) {
exists, err := util.FileExists(fileName)
if err != nil {
return nil, errors.Wrapf(err, "failed to check if file exists %s", fileName)
}
if !exists {
log.Logger().Warnf("failed to find file %s\n", fileName)
return nil, nil
}
data, err := readYamlTemplate(fileName, requirements, service)
if err != nil {
return nil, errors.Wrapf(err, "failed to load vault ingress template file %s", fileName)
}
answer := &v1beta1.Ingress{}
err = yaml.Unmarshal(data, answer)
if err != nil {
return nil, errors.Wrapf(err, "failed to load Ingress from result of template file %s", fileName)
}
return answer, nil
}
// readYamlTemplate evaluates the given go template file and returns the output data
func readYamlTemplate(templateFile string, requirements *config.RequirementsConfig, svc *corev1.Service) ([]byte, error) {
_, name := filepath.Split(templateFile)
funcMap := helm.NewFunctionMap()
tmpl, err := template.New(name).Option("missingkey=error").Funcs(funcMap).ParseFiles(templateFile)
if err != nil {
return nil, errors.Wrapf(err, "failed to parse Secrets template: %s", templateFile)
}
requirementsMap, err := requirements.ToMap()
if err != nil {
return nil, errors.Wrapf(err, "failed turn requirements into a map: %v", requirements)
}
svcMap, err := createServiceMap(svc)
if err != nil {
return nil, errors.Wrapf(err, "failed turn Service into a map: %v", svc)
}
templateData := map[string]interface{}{
"Requirements": chartutil.Values(requirementsMap),
"Environments": chartutil.Values(requirements.EnvironmentMap()),
"Service": chartutil.Values(svcMap),
}
var buf bytes.Buffer
err = tmpl.Execute(&buf, templateData)
if err != nil {
return nil, errors.Wrapf(err, "failed to execute Secrets template: %s", templateFile)
}
data := buf.Bytes()
return data, nil
}
func createServiceMap(service *corev1.Service) (map[string]interface{}, error) {
m, err := util.ToObjectMap(service)
if err != nil {
return m, err
}
m["name"] = service.Name
port := 0
exposePort := service.Annotations[ExposePortAnnotationKey]
if exposePort != "" {
port, err = strconv.Atoi(exposePort)
if err != nil {
port = 0
}
}
if port == 0 {
for _, p := range service.Spec.Ports {
port = int(p.Port)
break
}
}
m["port"] = port
return m, nil
}
func (o *StepExposeOptions) applyIngressAndUpdateService(client kubernetes.Interface, ns string, svc *corev1.Service, ingress *v1beta1.Ingress) (string, error) {
if ingress.Labels == nil {
ingress.Labels = map[string]string{}
}
ingress.Labels["provider"] = "jenkins-x"
hasOwner := false
for _, o := range ingress.OwnerReferences {
if o.UID == svc.UID {
hasOwner = true
break
}
}
if !hasOwner {
ingress.OwnerReferences = append(ingress.OwnerReferences, kube.ServiceOwnerRef(svc))
}
name := ingress.Name
_, err := client.ExtensionsV1beta1().Ingresses(ns).Get(name, metav1.GetOptions{})
createIngress := false
if err != nil {
if apierrors.IsNotFound(err) {
createIngress = true
} else {
return "", errors.Wrapf(err, "could not check for existing ingress %s/%s", ns, name)
}
}
info := util.ColorInfo
if createIngress {
_, err = client.ExtensionsV1beta1().Ingresses(ns).Create(ingress)
if err != nil {
return "", errors.Wrapf(err, "failed to create ingress %s/%s", ns, ingress.Name)
}
log.Logger().Infof("created Ingress %s in namespace %s", info(name), info(ns))
} else {
_, err = client.ExtensionsV1beta1().Ingresses(ns).Update(ingress)
if err != nil {
return "", errors.Wrapf(err, "failed to update ingress %s/%s", ns, ingress.Name)
}
log.Logger().Infof("updated Ingress %s in namespace %s", info(name), info(ns))
}
if svc.Annotations == nil {
svc.Annotations = map[string]string{}
}
hostName := services.IngressHost(ingress)
exposeURL := services.IngressURL(ingress)
path := svc.Annotations[APIServicePathAnnotationKey]
if path == "" {
path = svc.Annotations["fabric8.io/ingress.path"]
}
if len(path) > 0 {
exposeURL = util.UrlJoin(exposeURL, path)
}
svc.Annotations[ExposeAnnotationKey] = exposeURL
if key := svc.Annotations[ExposeHostNameAsAnnotationKey]; len(key) > 0 {
svc.Annotations[key] = hostName
}
return exposeURL, nil
}
func (o *StepExposeOptions) loadExposeControllerConfigFromValuesYAML(requirements *config.RequirementsConfig) error {
values, err := helm.LoadValuesFile(filepath.Join(o.Dir, helm.ValuesFileName))
if err != nil {
return err
}
ic := &requirements.Ingress
if ic.Domain == "" {
ic.Domain = util.GetMapValueAsStringViaPath(values, "expose.config.domain")
}
// lets detect TLS
if util.GetMapValueAsStringViaPath(values, "expose.config.tlsacme") == "true" || util.GetMapValueAsStringViaPath(values, "expose.config.http") == "false" {
ic.TLS.Enabled = true
}
// lets populate the sub domain if its missing
if ic.NamespaceSubDomain == "" {
ic.NamespaceSubDomain = "-" + requirements.Cluster.Namespace + "."
}
return nil
}
// IsExposedService returns true if this service should be exposed
func IsExposedService(svc *corev1.Service) bool {
labels := svc.Labels
if labels == nil {
labels = map[string]string{}
}
for _, l := range ExposeLabelKeys {
if labels[l] == "true" {
return true
}
}
return false
}