forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
getbuildconfigs.go
66 lines (55 loc) · 1.6 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
66
package gitserver
import (
"fmt"
"io"
"os"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
restclient "k8s.io/client-go/rest"
buildapi "github.com/openshift/origin/pkg/build/apis/build"
buildclient "github.com/openshift/origin/pkg/build/generated/internalclientset"
)
const gitRepositoryAnnotationKey = "openshift.io/git-repository"
func GetRepositoryBuildConfigs(c buildclient.Interface, name string, out io.Writer) error {
ns := os.Getenv("POD_NAMESPACE")
buildConfigList, err := c.Build().BuildConfigs(ns).List(metav1.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
}
// GetClient returns a build client.
func GetClient() (buildclient.Interface, error) {
clientConfig, err := restclient.InClusterConfig()
if err != nil {
return nil, fmt.Errorf("failed to get client config: %v", err)
}
buildClient, err := buildclient.NewForConfig(clientConfig)
if err != nil {
return nil, fmt.Errorf("error obtaining OpenShift client: %v", err)
}
return buildClient, nil
}