-
Notifications
You must be signed in to change notification settings - Fork 787
/
git_services.go
172 lines (154 loc) · 5.52 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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
package kube
import (
"fmt"
"io"
"net/url"
"strings"
"github.com/jenkins-x/jx-logging/pkg/log"
"github.com/jenkins-x/jx/v2/pkg/auth"
"github.com/jenkins-x/jx/v2/pkg/kube/naming"
"github.com/pkg/errors"
v1 "github.com/jenkins-x/jx-api/pkg/apis/jenkins.io/v1"
"github.com/jenkins-x/jx-api/pkg/client/clientset/versioned"
"github.com/jenkins-x/jx/v2/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 == gits.GitHubURL) || gitUrl == "" {
return nil
}
gitServices := jxClient.JenkinsV1().GitServices(devNs)
list, err := gitServices.List(metav1.ListOptions{})
if err != nil {
return errors.Wrap(err, "failed to list git services")
}
for _, g := range list.Items {
gs := g
if gitUrlsEqual(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.PatchUpdate(&gs)
if err != nil {
return fmt.Errorf("Failed to update kind on GitService with name %s: %s", gs.Name, err)
}
return errors.Wrap(err, "failed to PatchUpdate")
} else {
log.Logger().Infof("already has GitService %s in namespace %s for URL %s", gs.Name, devNs, gitUrl)
return nil
}
}
}
if name == "" {
u, err := url.Parse(gitUrl)
if err != nil {
return errors.Wrapf(err, "no name supplied and could not parse URL %s", u)
}
name = u.Host
}
// not found so lets create a new GitService
gitSvc := &v1.GitService{
ObjectMeta: metav1.ObjectMeta{
Name: naming.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 errors.Wrapf(err, "failed to create GitService with name %s", gitSvc.Name)
}
log.Logger().Infof("GitService %s created in namespace %s for URL %s", gitSvc.Name, devNs, gitUrl)
} 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.PatchUpdate(current)
if err != nil {
return errors.Wrapf(err, "failed to PatchUpdate GitService with name %s", gitSvc.Name)
}
log.Logger().Infof("GitService %s updated in namespace %s for URL %s", gitSvc.Name, devNs, gitUrl)
}
}
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, clusterAuthConfig *auth.AuthConfig, gitServiceURL string) (string, error) {
answer := gits.SaasGitKind(gitServiceURL)
if answer != "" {
return answer, nil
}
if clusterAuthConfig != nil {
clusterServer := clusterAuthConfig.GetServer(gitServiceURL)
if clusterServer != nil {
return clusterServer.Kind, nil
}
}
answer, err := GetServiceKindFromSecrets(kubeClient, devNs, gitServiceURL)
if err == nil && answer != "" {
return answer, nil
}
return getServiceKindFromGitServices(jxClient, devNs, gitServiceURL)
}
// GetServiceKindFromSecrets gets the kind of service from secrets
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")
}
// note sometimes the Git secret is just called 'jx-pipeline-git' if its created as part of
// 'jx create cluster --git-provider-url' - so lets handle the missing - on the name
secretNamePrefix := strings.TrimSuffix(SecretJenkinsPipelineGitCredentials, "-")
for _, secret := range secretList.Items {
if strings.HasPrefix(secret.GetName(), secretNamePrefix) {
annotations := secret.GetAnnotations()
url, ok := annotations[AnnotationURL]
if !ok {
continue
}
if gitUrlsEqual(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)
}
if serviceKind == "" {
kind := labels[LabelKind]
if kind == "git" {
serviceKind = gits.SaasGitKind(gitServiceURL)
if serviceKind == "" {
// lets default to github?
serviceKind = gits.KindGitHub
}
}
}
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 gitUrlsEqual(gs.Spec.URL, gitServiceURL) {
return gs.Spec.GitKind, nil
}
}
}
return "", fmt.Errorf("no Git service resource found with URL '%s' in namespace %s", gitServiceURL, ns)
}
func gitUrlsEqual(url1 string, url2 string) bool {
return url1 == url2 || strings.TrimSuffix(url1, "/") == strings.TrimSuffix(url2, "/")
}