-
Notifications
You must be signed in to change notification settings - Fork 1
/
git_services.go
138 lines (124 loc) · 4.02 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
package kube
import (
"fmt"
"io"
"net/url"
"strings"
"github.com/pkg/errors"
"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"
"k8s.io/client-go/kubernetes"
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.Interface, 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.Interface, kubeClient kubernetes.Interface, devNs string, gitServiceURL string) (string, error) {
answer := gits.SaasGitKind(gitServiceURL)
if answer != "" {
return answer, nil
}
answer, err := getServiceKindFromSecrets(kubeClient, devNs, gitServiceURL)
if err == nil && answer != "" {
return answer, nil
}
return getServiceKindFromGitServices(jxClient, devNs, gitServiceURL)
}
func getServiceKindFromSecrets(kubeClient kubernetes.Interface, ns string, gitServiceURL string) (string, error) {
secretList, err := kubeClient.CoreV1().Secrets(ns).List(metav1.ListOptions{})
if err != nil {
return "", errors.Wrap(err, "failed to list the secrets")
}
for _, secret := range secretList.Items {
if strings.HasPrefix(secret.GetName(), SecretJenkinsPipelineGitCredentials) {
annotations := secret.GetAnnotations()
url, ok := annotations[AnnotationURL]
if !ok {
continue
}
if url == gitServiceURL {
labels := secret.GetLabels()
serviceKind, ok := labels[LabelServiceKind]
if !ok {
return "", fmt.Errorf("no service kind label found on secret '%s' for git service '%s'",
secret.GetName(), gitServiceURL)
}
return serviceKind, nil
}
}
}
return "", fmt.Errorf("no secret found with configuration for '%s' git service", gitServiceURL)
}
func getServiceKindFromGitServices(jxClient versioned.Interface, ns string, gitServiceURL string) (string, error) {
gitServices := jxClient.JenkinsV1().GitServices(ns)
list, err := gitServices.List(metav1.ListOptions{})
if err == nil {
for _, gs := range list.Items {
if gs.Spec.URL == gitServiceURL {
return gs.Spec.GitKind, nil
}
}
}
return "", fmt.Errorf("no git service resource found with URL '%s'", gitServiceURL)
}