forked from jenkins-x/jx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
common_team_settings.go
112 lines (96 loc) · 2.6 KB
/
common_team_settings.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
package cmd
import (
"fmt"
"github.com/jenkins-x/jx/pkg/apis/jenkins.io/v1"
"github.com/jenkins-x/jx/pkg/kube"
)
type BranchPatterns struct {
DefaultBranchPattern string
ForkBranchPattern string
}
const (
defaultBuildPackRef = "2.1"
defaultHelmBin = "helm"
)
// TeamSettings returns the team settings
func (o *CommonOptions) TeamSettings() (*v1.TeamSettings, error) {
jxClient, ns, err := o.JXClientAndDevNamespace()
if err != nil {
return nil, err
}
err = o.registerEnvironmentCRD()
if err != nil {
return nil, err
}
env, err := kube.EnsureDevEnvironmentSetup(jxClient, ns)
if err != nil {
return nil, err
}
if env == nil {
return nil, fmt.Errorf("No Development environment found for namespace %s", ns)
}
teamSettings := &env.Spec.TeamSettings
if teamSettings.BuildPackURL == "" {
teamSettings.BuildPackURL = JenkinsBuildPackURL
}
if teamSettings.BuildPackRef == "" {
teamSettings.BuildPackRef = defaultBuildPackRef
}
return teamSettings, nil
}
// TeamBranchPatterns returns the team branch patterns used to enable CI/CD on branches when creating/importing projects
func (o *CommonOptions) TeamBranchPatterns() (*BranchPatterns, error) {
teamSettings, err := o.TeamSettings()
if err != nil {
return nil, err
}
branchPatterns := teamSettings.BranchPatterns
if branchPatterns == "" {
branchPatterns = defaultBranchPatterns
}
forkBranchPatterns := teamSettings.ForkBranchPatterns
if forkBranchPatterns == "" {
forkBranchPatterns = defaultForkBranchPatterns
}
return &BranchPatterns{
DefaultBranchPattern: branchPatterns,
ForkBranchPattern: forkBranchPatterns,
}, nil
}
// TeamHelmBin returns the helm binary used for a team
func (o *CommonOptions) TeamHelmBin() (string, error) {
helmBin := defaultHelmBin
teamSettings, err := o.TeamSettings()
if err != nil {
return helmBin, err
}
helmBin = teamSettings.HelmBinary
if helmBin == "" {
helmBin = defaultHelmBin
}
return helmBin, nil
}
// ModifyDevEnvironment modifies the development environment settings
func (o *CommonOptions) ModifyDevEnvironment(callback func(env *v1.Environment) error) error {
apisClient, err := o.CreateApiExtensionsClient()
if err != nil {
return err
}
kube.RegisterEnvironmentCRD(apisClient)
jxClient, ns, err := o.JXClientAndDevNamespace()
if err != nil {
return err
}
err = o.registerEnvironmentCRD()
if err != nil {
return err
}
env, err := kube.EnsureDevEnvironmentSetup(jxClient, ns)
if err != nil {
return err
}
if env == nil {
return fmt.Errorf("No Development environment found for namespace %s", ns)
}
return o.modifyDevEnvironment(jxClient, ns, callback)
}