forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
getbuildconfigs.go
65 lines (54 loc) · 1.48 KB
/
getbuildconfigs.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
package gitserver
import (
"fmt"
"io"
"os"
kapi "k8s.io/kubernetes/pkg/api"
"k8s.io/kubernetes/pkg/client/restclient"
buildapi "github.com/openshift/origin/pkg/build/api"
"github.com/openshift/origin/pkg/client"
)
const gitRepositoryAnnotationKey = "openshift.io/git-repository"
func GetRepositoryBuildConfigs(c client.Interface, name string, out io.Writer) error {
ns := os.Getenv("POD_NAMESPACE")
buildConfigList, err := c.BuildConfigs(ns).List(kapi.ListOptions{})
if err != nil {
return err
}
matchingBuildConfigs := []*buildapi.BuildConfig{}
for i := range buildConfigList.Items {
bc := &buildConfigList.Items[i]
repoAnnotation, hasAnnotation := bc.Annotations[gitRepositoryAnnotationKey]
if hasAnnotation {
if repoAnnotation == name {
matchingBuildConfigs = append(matchingBuildConfigs, bc)
}
continue
}
if bc.Name == name {
matchingBuildConfigs = append(matchingBuildConfigs, bc)
}
}
for _, bc := range matchingBuildConfigs {
var ref string
if bc.Spec.Source.Git != nil {
ref = bc.Spec.Source.Git.Ref
}
if ref == "" {
ref = "master"
}
fmt.Fprintf(out, "%s %s\n", bc.Name, ref)
}
return nil
}
func GetClient() (client.Interface, error) {
clientConfig, err := restclient.InClusterConfig()
if err != nil {
return nil, fmt.Errorf("failed to get client config: %v", err)
}
osClient, err := client.New(clientConfig)
if err != nil {
return nil, fmt.Errorf("error obtaining OpenShift client: %v", err)
}
return osClient, nil
}