forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jenkins.go
109 lines (100 loc) · 3.58 KB
/
jenkins.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
package jenkins
import (
"fmt"
"github.com/golang/glog"
kapi "k8s.io/kubernetes/pkg/api"
kerrs "k8s.io/kubernetes/pkg/api/errors"
"k8s.io/kubernetes/pkg/api/meta"
"k8s.io/kubernetes/pkg/api/unversioned"
"k8s.io/kubernetes/pkg/runtime"
"github.com/openshift/origin/pkg/client"
serverapi "github.com/openshift/origin/pkg/cmd/server/api"
"github.com/openshift/origin/pkg/template"
templateapi "github.com/openshift/origin/pkg/template/api"
)
// PipelineTemplate stores the configuration of the
// PipelineStrategy template, used to instantiate the Jenkins service in
// given namespace.
type PipelineTemplate struct {
Config serverapi.JenkinsPipelineConfig
Namespace string
osClient client.Interface
}
// NewPipelineTemplate returns a new PipelineTemplate.
func NewPipelineTemplate(ns string, conf serverapi.JenkinsPipelineConfig, osClient client.Interface) *PipelineTemplate {
return &PipelineTemplate{
Config: conf,
Namespace: ns,
osClient: osClient,
}
}
// Process processes the Jenkins template. If an error occurs
func (t *PipelineTemplate) Process() (*kapi.List, []error) {
var errors []error
jenkinsTemplate, err := t.osClient.Templates(t.Config.TemplateNamespace).Get(t.Config.TemplateName)
if err != nil {
if kerrs.IsNotFound(err) {
errors = append(errors, fmt.Errorf("Jenkins pipeline template %s/%s not found", t.Config.TemplateNamespace, t.Config.TemplateName))
} else {
errors = append(errors, err)
}
return nil, errors
}
errors = append(errors, substituteTemplateParameters(t.Config.Parameters, jenkinsTemplate)...)
pTemplate, err := t.osClient.TemplateConfigs(t.Namespace).Create(jenkinsTemplate)
if err != nil {
errors = append(errors, fmt.Errorf("processing Jenkins template %s/%s failed: %v", t.Config.TemplateNamespace, t.Config.TemplateName, err))
return nil, errors
}
var items []runtime.Object
for _, obj := range pTemplate.Objects {
if unknownObj, ok := obj.(*runtime.Unknown); ok {
decodedObj, err := runtime.Decode(kapi.Codecs.UniversalDecoder(), unknownObj.Raw)
if err != nil {
errors = append(errors, err)
}
items = append(items, decodedObj)
}
}
glog.V(4).Infof("Processed Jenkins pipeline jenkinsTemplate %s/%s", pTemplate.Namespace, pTemplate.Namespace)
return &kapi.List{ListMeta: unversioned.ListMeta{}, Items: items}, errors
}
// HasJenkinsService searches the template items and return true if the expected
// Jenkins service is contained in template.
func (t *PipelineTemplate) HasJenkinsService(items *kapi.List) bool {
accessor := meta.NewAccessor()
for _, item := range items.Items {
kind, _, err := kapi.Scheme.ObjectKind(item)
if err != nil {
glog.Infof("Error checking Jenkins service kind: %v", err)
return false
}
name, err := accessor.Name(item)
if err != nil {
glog.Infof("Error checking Jenkins service name: %v", err)
return false
}
glog.Infof("Jenkins Pipeline template object %q with name %q", name, kind.Kind)
if name == t.Config.ServiceName && kind.Kind == "Service" {
return true
}
}
return false
}
// substituteTemplateParameters injects user specified parameter values into the Template
func substituteTemplateParameters(params map[string]string, t *templateapi.Template) []error {
var errors []error
for name, value := range params {
if len(name) == 0 {
errors = append(errors, fmt.Errorf("template parameter name cannot be empty (%q)", value))
continue
}
if v := template.GetParameterByName(t, name); v != nil {
v.Value = value
v.Generate = ""
} else {
errors = append(errors, fmt.Errorf("unknown parameter %q specified for template", name))
}
}
return errors
}