forked from jenkins-x/jx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
git_services.go
140 lines (130 loc) · 3.77 KB
/
git_services.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
package kube
import (
"fmt"
"io"
"net/url"
"strings"
"github.com/jenkins-x/jx/pkg/apis/jenkins.io/v1"
"github.com/jenkins-x/jx/pkg/client/clientset/versioned"
"github.com/jenkins-x/jx/pkg/gits"
"gopkg.in/yaml.v2"
"k8s.io/client-go/kubernetes"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
// EnsureGitServiceExistsForHost ensures that there is a GitService CRD for the given host and kind
func EnsureGitServiceExistsForHost(jxClient *versioned.Clientset, devNs string, kind string, name string, gitUrl string, out io.Writer) error {
if kind == "" || kind == "github" || gitUrl == "" {
return nil
}
gitServices := jxClient.JenkinsV1().GitServices(devNs)
list, err := gitServices.List(metav1.ListOptions{})
if err != nil {
return err
}
for _, gs := range list.Items {
if gs.Spec.URL == gitUrl {
oldKind := gs.Spec.GitKind
if oldKind != kind {
fmt.Fprintf(out, "Updating GitService %s as the kind has changed from %s to %s\n", gs.Name, oldKind, kind)
gs.Spec.GitKind = kind
_, err = gitServices.Update(&gs)
if err != nil {
return fmt.Errorf("Failed to update kind on GitService with name %s: %s", gs.Name, err)
}
return err
} else {
return nil
}
}
}
if name == "" {
u, err := url.Parse(gitUrl)
if err != nil {
return fmt.Errorf("No name supplied and could not parse URL %s due to %s", u, err)
}
name = u.Host
}
// not found so lets create a new GitService
gitSvc := &v1.GitService{
ObjectMeta: metav1.ObjectMeta{
Name: ToValidNameWithDots(name),
},
Spec: v1.GitServiceSpec{
Name: name,
URL: gitUrl,
GitKind: kind,
},
}
current, err := gitServices.Get(name, metav1.GetOptions{})
if err != nil {
_, err = gitServices.Create(gitSvc)
if err != nil {
return fmt.Errorf("Failed to create GitService with name %s: %s", gitSvc.Name, err)
}
} else if current != nil {
if current.Spec.URL != gitSvc.Spec.URL || current.Spec.GitKind != gitSvc.Spec.GitKind {
current.Spec.URL = gitSvc.Spec.URL
current.Spec.GitKind = gitSvc.Spec.GitKind
_, err = gitServices.Update(current)
if err != nil {
return fmt.Errorf("Failed to update GitService with name %s: %s", gitSvc.Name, err)
}
}
}
return nil
}
// GetGitServiceKind returns the kind of the given host if one can be found or ""
func GetGitServiceKind(jxClient *versioned.Clientset, kubeClient *kubernetes.Clientset, devNs string, gitServiceUrl string) (string, error) {
answer := gits.SaasGitKind(gitServiceUrl)
if answer != "" {
return answer, nil
}
cm, err := kubeClient.CoreV1().ConfigMaps(devNs).Get(ConfigMapJenkinsXGitKinds, metav1.GetOptions{})
if err == nil {
answer = GetGitServiceKindFromConfigMap(cm, gitServiceUrl)
if answer != "" {
return answer, nil
}
}
gitServices := jxClient.JenkinsV1().GitServices(devNs)
list, err := gitServices.List(metav1.ListOptions{})
if err == nil {
for _, gs := range list.Items {
if gs.Spec.URL == gitServiceUrl {
return gs.Spec.GitKind, nil
}
}
}
// TODO should we default to github?
return answer, nil
}
func GetGitServiceKindFromConfigMap(cm *corev1.ConfigMap, gitServiceUrl string) string {
gitServiceUrl = strings.TrimSuffix(gitServiceUrl, "/")
for k, v := range cm.Data {
if strings.TrimSpace(v) != "" {
m := map[string]string{}
err := yaml.Unmarshal([]byte(v), &m)
if err != nil {
fmt.Printf("Warning could not parse %s YAML %s: %s due to: %s", ConfigMapJenkinsXGitKinds, k, v, err)
} else {
for _, u := range m {
if u == gitServiceUrl {
return k
}
if k == "github" {
// lets trim /api/ paths from github repos
idx := strings.LastIndex(u, "/api/")
if idx > 0 {
u2 := u[0:idx]
if gitServiceUrl == u2 {
return k
}
}
}
}
}
}
}
return ""
}