-
Notifications
You must be signed in to change notification settings - Fork 787
/
hooks.go
86 lines (79 loc) · 3.21 KB
/
hooks.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
package extensions
import (
jenkinsv1 "github.com/jenkins-x/jx/pkg/apis/jenkins.io/v1"
jenkinsv1client "github.com/jenkins-x/jx/pkg/client/clientset/versioned"
"github.com/jenkins-x/jx/pkg/expose"
"github.com/jenkins-x/jx/pkg/helm"
"github.com/jenkins-x/jx/pkg/kube"
certclient "github.com/jetstack/cert-manager/pkg/client/clientset/versioned"
"github.com/pkg/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
)
// OnApply examines the currently installs apps to perform any post-install actions
func OnApply(jxClient jenkinsv1client.Interface, kubeClient kubernetes.Interface, certClient certclient.Interface, ns string, helmer helm.Helmer,
installTimeout string, versionsDir string) error {
appList, err := jxClient.JenkinsV1().Apps(ns).List(metav1.ListOptions{})
if err != nil {
return err
}
for _, app := range appList.Items {
err = OnInstall(&app, kubeClient, certClient, ns, helmer, installTimeout, versionsDir)
if err != nil {
return err
}
}
return nil
}
// OnInstallFromName uses the App CRD installed by appName to perform any post-install actions.
func OnInstallFromName(appName string, jxClient jenkinsv1client.Interface, kubeClient kubernetes.Interface, certClient certclient.Interface,
ns string, helmer helm.Helmer,
installTimeout string, versionsDir string) error {
app, err := jxClient.JenkinsV1().Apps(ns).Get(appName, metav1.GetOptions{})
if err != nil {
return err
}
return OnInstall(app, kubeClient, certClient, ns, helmer, installTimeout, versionsDir)
}
// OnInstall uses the App CRD installed by appName to perform any post-install actions.
func OnInstall(app *jenkinsv1.App, kubeClient kubernetes.Interface, certClient certclient.Interface, ns string,
helmer helm.Helmer, installTimeout string, versionsDir string) error {
// Specific hooks go here
err := exposeOnInstall(app, kubeClient, certClient, ns, helmer, installTimeout, versionsDir)
if err != nil {
return err
}
return nil
}
func exposeOnInstall(app *jenkinsv1.App, kubeClient kubernetes.Interface, certClient certclient.Interface, ns string,
helmer helm.Helmer, installTimeout string, versionsDir string) error {
for _, svc := range app.Spec.ExposedServices {
err := exposeSvc(svc, kubeClient, certClient, ns, helmer, installTimeout, versionsDir)
if err != nil {
return err
}
}
return nil
}
func exposeSvc(svcName string, kubeClient kubernetes.Interface, certClient certclient.Interface, ns string,
helmer helm.Helmer, installTimeout string, versionsDir string) error {
svc, err := kubeClient.CoreV1().Services(ns).Get(svcName, metav1.GetOptions{})
if err != nil {
return errors.Wrapf(err, "getting the addon service: %s", svc)
}
if svc.Annotations == nil {
svc.Annotations = map[string]string{}
}
if svc.Annotations[kube.AnnotationExpose] == "" {
svc.Annotations[kube.AnnotationExpose] = "true"
svc, err = kubeClient.CoreV1().Services(ns).Update(svc)
if err != nil {
return errors.Wrap(err, "updating the service annotations")
}
}
devNamespace, _, err := kube.GetDevNamespace(kubeClient, ns)
if err != nil {
return errors.Wrap(err, "retrieving the dev namespace")
}
return expose.Expose(kubeClient, certClient, devNamespace, ns, "", helmer, installTimeout, versionsDir)
}