Skip to content

Commit 66de663

Browse files
Zhuxiaoyangks-ci-bot
Zhuxiaoyang
authored andcommitted
add Annotation to pod (#11)
Signed-off-by: soulseen <[email protected]>
1 parent 76569e5 commit 66de663

File tree

2 files changed

+29
-21
lines changed

2 files changed

+29
-21
lines changed

pkg/api/types.go

-6
Original file line numberDiff line numberDiff line change
@@ -55,12 +55,6 @@ const (
5555

5656
// Default source branch.
5757
DefaultBranch = "master"
58-
59-
//S2iRun Namespace which s2i job run.
60-
S2iRunNamespace = "S2iRunNamespace"
61-
62-
//S2iRunJobName is the job name in k8s.
63-
S2iRunJobName = "S2iRunJobName"
6458
)
6559

6660
// Config contains essential fields for performing build.

pkg/outputresult/output.go

+29-15
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,29 @@ package outputresult
22

33
import (
44
"encoding/json"
5-
dockertypes "github.com/docker/docker/api/types"
5+
"os"
6+
"time"
7+
68
"github.com/kubesphere/s2irun/pkg/api"
7-
utilglog "github.com/kubesphere/s2irun/pkg/utils/glog"
89
"k8s.io/apimachinery/pkg/apis/meta/v1"
10+
"k8s.io/apimachinery/pkg/util/wait"
911
"k8s.io/client-go/kubernetes"
1012
"k8s.io/client-go/util/retry"
11-
"os"
1213
"sigs.k8s.io/controller-runtime/pkg/client/config"
14+
15+
dockertypes "github.com/docker/docker/api/types"
16+
utilglog "github.com/kubesphere/s2irun/pkg/utils/glog"
1317
)
1418

15-
var glog = utilglog.StderrLog
19+
var (
20+
Retry = wait.Backoff{
21+
Steps: 10,
22+
Duration: 10 * time.Millisecond,
23+
Factor: 1.0,
24+
Jitter: 0.1,
25+
}
26+
glog = utilglog.StderrLog
27+
)
1628

1729
func OutputResult(builderConfig *api.Config, imageInspect *dockertypes.ImageInspect, result *api.Result) {
1830
// build result info.
@@ -49,10 +61,10 @@ func OutputResult(builderConfig *api.Config, imageInspect *dockertypes.ImageInsp
4961
}
5062

5163
func addBuildResultToAnnotation(buildResult *api.Result) error {
52-
namespace := os.Getenv(api.S2iRunNamespace)
53-
jobName := os.Getenv(api.S2iRunJobName)
54-
if namespace == "" || jobName == "" {
55-
glog.Warning("failed to get env S2iRunJobName and S2iRunNamespace")
64+
namespace := os.Getenv("POD_NAMESPACE")
65+
podName := os.Getenv("POD_NAME")
66+
if namespace == "" || podName == "" {
67+
glog.Warning("failed to get env S2iRun PodName and S2iRun Namespace")
5668
return nil
5769
}
5870
cfg, err := config.GetConfig()
@@ -62,20 +74,22 @@ func addBuildResultToAnnotation(buildResult *api.Result) error {
6274
}
6375
k8sClient := kubernetes.NewForConfigOrDie(cfg)
6476

65-
retryErr := retry.RetryOnConflict(retry.DefaultRetry, func() error {
66-
job, err := k8sClient.BatchV1().Jobs(namespace).Get(jobName, v1.GetOptions{})
77+
retryErr := retry.RetryOnConflict(Retry, func() error {
78+
pod, err := k8sClient.CoreV1().Pods(namespace).Get(podName, v1.GetOptions{})
6779
if err != nil {
68-
glog.Errorf("failed to get job %s in namespace %s, reason: %s", jobName, namespace, err)
80+
glog.Errorf("failed to get pod %s in namespace %s, reason: %s", podName, namespace, err)
6981
return err
7082
}
7183

72-
//update job annotations
84+
//update pod annotations
7385
result, _ := json.Marshal(buildResult.ResultInfo)
7486
source, _ := json.Marshal(buildResult.SourceInfo)
75-
job.Annotations[api.AnnotationBuildResultKey] = string(result)
76-
job.Annotations[api.AnnotationBuildSourceKey] = string(source)
87+
pod.Annotations = map[string]string{
88+
api.AnnotationBuildResultKey: string(result),
89+
api.AnnotationBuildSourceKey: string(source),
90+
}
7791

78-
_, err = k8sClient.BatchV1().Jobs(namespace).Update(job)
92+
_, err = k8sClient.CoreV1().Pods(namespace).Update(pod)
7993
return err
8094
})
8195

0 commit comments

Comments
 (0)