This repository has been archived by the owner on Jul 18, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
admission.go
156 lines (133 loc) · 4.98 KB
/
admission.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
package jenkinsbootstrapper
import (
"fmt"
"io"
"net/http"
"github.com/golang/glog"
"k8s.io/kubernetes/pkg/admission"
kapi "k8s.io/kubernetes/pkg/api"
kapierrors "k8s.io/kubernetes/pkg/api/errors"
"k8s.io/kubernetes/pkg/api/meta"
"k8s.io/kubernetes/pkg/apimachinery/registered"
clientset "k8s.io/kubernetes/pkg/client/clientset_generated/internalclientset"
coreclient "k8s.io/kubernetes/pkg/client/clientset_generated/internalclientset/typed/core/unversioned"
"k8s.io/kubernetes/pkg/client/restclient"
kclient "k8s.io/kubernetes/pkg/client/unversioned"
"k8s.io/kubernetes/pkg/kubectl/resource"
"k8s.io/kubernetes/pkg/runtime"
kutilerrors "k8s.io/kubernetes/pkg/util/errors"
"github.com/openshift/origin/pkg/api/latest"
authenticationclient "github.com/openshift/origin/pkg/auth/client"
buildapi "github.com/openshift/origin/pkg/build/api"
jenkinscontroller "github.com/openshift/origin/pkg/build/controller/jenkins"
"github.com/openshift/origin/pkg/client"
configapi "github.com/openshift/origin/pkg/cmd/server/api"
"github.com/openshift/origin/pkg/config/cmd"
)
func init() {
admission.RegisterPlugin("openshift.io/JenkinsBootstrapper", func(c clientset.Interface, config io.Reader) (admission.Interface, error) {
return NewJenkinsBootstrapper(c.Core()), nil
})
}
type jenkinsBootstrapper struct {
*admission.Handler
privilegedRESTClientConfig restclient.Config
serviceClient coreclient.ServicesGetter
openshiftClient client.Interface
jenkinsConfig configapi.JenkinsPipelineConfig
}
// NewJenkinsBootstrapper returns an admission plugin that will create required jenkins resources as the user if they are needed.
func NewJenkinsBootstrapper(serviceClient coreclient.ServicesGetter) admission.Interface {
return &jenkinsBootstrapper{
Handler: admission.NewHandler(admission.Create),
serviceClient: serviceClient,
}
}
func (a *jenkinsBootstrapper) Admit(attributes admission.Attributes) error {
if a.jenkinsConfig.AutoProvisionEnabled != nil && !*a.jenkinsConfig.AutoProvisionEnabled {
return nil
}
if len(attributes.GetSubresource()) != 0 {
return nil
}
if attributes.GetResource().GroupResource() != buildapi.Resource("buildconfigs") && attributes.GetResource().GroupResource() != buildapi.Resource("builds") {
return nil
}
if !needsJenkinsTemplate(attributes.GetObject()) {
return nil
}
namespace := attributes.GetNamespace()
svcName := a.jenkinsConfig.ServiceName
if len(svcName) == 0 {
return nil
}
// TODO pull this from a cache.
if _, err := a.serviceClient.Services(namespace).Get(svcName); !kapierrors.IsNotFound(err) {
// if it isn't a "not found" error, return the error. Either its nil and there's nothing to do or something went really wrong
return err
}
glog.V(3).Infof("Adding new jenkins service %q to the project %q", svcName, namespace)
jenkinsTemplate := jenkinscontroller.NewPipelineTemplate(namespace, a.jenkinsConfig, a.openshiftClient)
objects, errs := jenkinsTemplate.Process()
if len(errs) > 0 {
return kutilerrors.NewAggregate(errs)
}
if !jenkinsTemplate.HasJenkinsService(objects) {
return fmt.Errorf("template %s/%s does not contain required service %q", a.jenkinsConfig.TemplateNamespace, a.jenkinsConfig.TemplateName, a.jenkinsConfig.ServiceName)
}
impersonatingConfig := a.privilegedRESTClientConfig
oldWrapTransport := impersonatingConfig.WrapTransport
impersonatingConfig.WrapTransport = func(rt http.RoundTripper) http.RoundTripper {
return authenticationclient.NewImpersonatingRoundTripper(attributes.GetUserInfo(), oldWrapTransport(rt))
}
var bulkErr error
bulk := &cmd.Bulk{
Mapper: &resource.Mapper{
RESTMapper: registered.RESTMapper(),
ObjectTyper: kapi.Scheme,
ClientMapper: resource.ClientMapperFunc(func(mapping *meta.RESTMapping) (resource.RESTClient, error) {
if latest.OriginKind(mapping.GroupVersionKind) {
return client.New(&impersonatingConfig)
}
return kclient.New(&impersonatingConfig)
}),
},
Op: cmd.Create,
After: func(info *resource.Info, err error) bool {
if kapierrors.IsAlreadyExists(err) {
return false
}
if err != nil {
bulkErr = err
return true
}
return false
},
}
// we're intercepting the error we care about using After
bulk.Run(objects, namespace)
if bulkErr != nil {
return bulkErr
}
glog.V(1).Infof("Jenkins Pipeline service %q created", svcName)
return nil
}
func needsJenkinsTemplate(obj runtime.Object) bool {
switch t := obj.(type) {
case *buildapi.Build:
return t.Spec.Strategy.JenkinsPipelineStrategy != nil
case *buildapi.BuildConfig:
return t.Spec.Strategy.JenkinsPipelineStrategy != nil
default:
return false
}
}
func (a *jenkinsBootstrapper) SetJenkinsPipelineConfig(jenkinsConfig configapi.JenkinsPipelineConfig) {
a.jenkinsConfig = jenkinsConfig
}
func (a *jenkinsBootstrapper) SetRESTClientConfig(restClientConfig restclient.Config) {
a.privilegedRESTClientConfig = restClientConfig
}
func (a *jenkinsBootstrapper) SetOpenshiftClient(oclient client.Interface) {
a.openshiftClient = oclient
}