...
1 package main
2
3 import (
4 "context"
5 "flag"
6 "fmt"
7 "os"
8 "path/filepath"
9
10 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
11 "k8s.io/client-go/tools/clientcmd"
12
13 v1 "github.com/openshift/api/build/v1"
14 buildv1 "github.com/openshift/client-go/build/clientset/versioned/typed/build/v1"
15 )
16
17 func main() {
18 err := start()
19 if err != nil {
20 fmt.Fprintf(os.Stderr, "error: %v", err)
21 os.Exit(1)
22 }
23 }
24
25 func start() error {
26 var kubeconfig *string
27 if home := homeDir(); home != "" {
28 kubeconfig = flag.String("kubeconfig", filepath.Join(home, ".kube", "config"), "(optional) absolute path to the kubeconfig file")
29 } else {
30 kubeconfig = flag.String("kubeconfig", "", "absolute path to the kubeconfig file")
31 }
32 flag.Parse()
33
34
35 config, err := clientcmd.BuildConfigFromFlags("", *kubeconfig)
36 if err != nil {
37 return err
38 }
39
40 buildV1Client, err := buildv1.NewForConfig(config)
41 if err != nil {
42 return err
43 }
44
45 namespace := "testproject"
46
47 builds, err := buildV1Client.Builds(namespace).List(context.TODO(), metav1.ListOptions{})
48 if err != nil {
49 return err
50 }
51 fmt.Printf("There are %d builds in project %s\n", len(builds.Items), namespace)
52
53 for i, build := range builds.Items {
54 fmt.Printf("index %d: Name of the build: %s", i, build.Name)
55 }
56
57
58 build := "cakephp-ex-1"
59 myBuild, err := buildV1Client.Builds(namespace).Get(context.TODO(), build, metav1.GetOptions{})
60 if err != nil {
61 return err
62 }
63 fmt.Printf("Found build %s in namespace %s\n", build, namespace)
64 fmt.Printf("Raw printout of the build %+v\n", myBuild)
65
66 fmt.Printf("name %s, start time %s, duration (in sec) %.0f, and phase %s\n",
67 myBuild.Name, myBuild.Status.StartTimestamp.String(),
68 myBuild.Status.Duration.Seconds(), myBuild.Status.Phase)
69
70
71 buildConfig := "cakephp-ex"
72 myBuildConfig, err := buildV1Client.BuildConfigs(namespace).Get(context.TODO(), buildConfig, metav1.GetOptions{})
73 if err != nil {
74 return err
75 }
76 fmt.Printf("Found BuildConfig %s in namespace %s\n", myBuildConfig.Name, namespace)
77 buildRequest := v1.BuildRequest{}
78 buildRequest.Kind = "BuildRequest"
79 buildRequest.APIVersion = "build.openshift.io/v1"
80 objectMeta := metav1.ObjectMeta{}
81 objectMeta.Name = "cakephp-ex"
82 buildRequest.ObjectMeta = objectMeta
83 buildTriggerCause := v1.BuildTriggerCause{}
84 buildTriggerCause.Message = "Manually triggered"
85 buildRequest.TriggeredBy = []v1.BuildTriggerCause{buildTriggerCause}
86 myBuild, err = buildV1Client.BuildConfigs(namespace).Instantiate(context.TODO(), objectMeta.Name, &buildRequest, metav1.CreateOptions{})
87
88 if err != nil {
89 return err
90 }
91 fmt.Printf("Name of the triggered build %s\n", myBuild.Name)
92 return nil
93 }
94
95 func homeDir() string {
96 if h := os.Getenv("HOME"); h != "" {
97 return h
98 }
99 return os.Getenv("USERPROFILE")
100 }
101
View as plain text