@@ -20,7 +20,9 @@ import (
2020 "context"
2121 "encoding/json"
2222 "fmt"
23+ "net/http"
2324 "reflect"
25+ "strings"
2426 "time"
2527
2628 "github.com/go-logr/logr"
@@ -37,6 +39,7 @@ import (
3739
3840 "github.com/kubesphere/ks-devops/pkg/api/devops/v1alpha3"
3941 devopsClient "github.com/kubesphere/ks-devops/pkg/client/devops"
42+ "github.com/kubesphere/ks-devops/pkg/client/devops/jenkins"
4043 cmstore "github.com/kubesphere/ks-devops/pkg/store/configmap"
4144 storeInter "github.com/kubesphere/ks-devops/pkg/store/store"
4245 "github.com/kubesphere/ks-devops/pkg/utils/k8sutil"
@@ -52,6 +55,7 @@ type Reconciler struct {
5255 ctx context.Context
5356 log logr.Logger
5457 Scheme * runtime.Scheme
58+ Options * jenkins.Options
5559 DevOpsClient devopsClient.Interface
5660 JenkinsCore core.JenkinsCore
5761 recorder record.EventRecorder
@@ -170,6 +174,12 @@ func (r *Reconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Resu
170174 return ctrl.Result {}, err
171175 }
172176
177+ if err := r .getAgentInfo (ctx , pipelineRunCopied ); err != nil {
178+ log .Error (err , "unable to get agent info" )
179+ r .recorder .Eventf (pipelineRunCopied , corev1 .EventTypeWarning , v1alpha3 .RetrieveFailed , "Failed to retrieve agent info from Jenkins, and error was %v" , err )
180+ return ctrl.Result {}, err
181+ }
182+
173183 r .recorder .Eventf (pipelineRunCopied , corev1 .EventTypeNormal , v1alpha3 .Updated , "Updated running data for PipelineRun %s" , req .NamespacedName )
174184 // until the status is okay
175185 // TODO make the RequeueAfter configurable
@@ -226,6 +236,62 @@ func (r *Reconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Resu
226236 return ctrl.Result {}, nil
227237}
228238
239+ // match /blue/rest/organizations/jenkins/pipelines/{devops}/{pipeline}/runs/{run}/log/?start=0
240+ // match /blue/rest/organizations/jenkins/pipelines/%s/pipelines/%s/branches/%s/runs/%s/log/?
241+ func (r * Reconciler ) getAgentInfo (ctx context.Context , pr * v1alpha3.PipelineRun ) error {
242+ if pr .Annotations == nil {
243+ pr .Annotations = make (map [string ]string )
244+ }
245+
246+ podName , podNameExist := pr .Annotations [v1alpha3 .JenkinsAgentPodNameAnnoKey ]
247+ _ , nodeNameExist := pr .Annotations [v1alpha3 .JenkinsAgentNodeNameAnnoKey ]
248+ if podNameExist && nodeNameExist || (pr .Status .Phase != v1alpha3 .Running && pr .Status .Phase != v1alpha3 .Pending ) {
249+ return nil
250+ }
251+
252+ if ! podNameExist {
253+ runID , _ := pr .GetPipelineRunID ()
254+ logUrl := jenkins .GetRunLogUrl
255+ api := fmt .Sprintf (logUrl , pr .Namespace , pr .Spec .PipelineRef .Name , runID )
256+ if pr .Spec .PipelineSpec .Type == v1alpha3 .MultiBranchPipelineType {
257+ logUrl = jenkins .GetBranchRunLogUrl
258+ api = fmt .Sprintf (logUrl , pr .Namespace , pr .Spec .PipelineRef .Name , pr .Spec .SCM .RefName , runID )
259+ }
260+
261+ _ , res , err := r .JenkinsCore .Request (http .MethodGet , api , map [string ]string {"Content-Type" : "application/json" }, nil )
262+ if err != nil {
263+ return err
264+ }
265+
266+ lines := strings .Split (string (res ), "\n " )
267+ for _ , line := range lines {
268+ if strings .HasPrefix (line , "Agent" ) && strings .Contains (line , "is provisioned from template" ) {
269+ parts := strings .Fields (line )
270+ if len (parts ) > 2 {
271+ pr .Annotations [v1alpha3 .JenkinsAgentPodNameAnnoKey ] = parts [1 ]
272+ klog .Infof ("get agent pod name: %s" , parts [1 ])
273+ }
274+ break
275+ }
276+ }
277+ }
278+
279+ if ! nodeNameExist {
280+ agentPod := & corev1.Pod {}
281+ err := r .Client .Get (ctx , client.ObjectKey {Namespace : r .Options .WorkerNamespace , Name : podName }, agentPod )
282+ if err != nil {
283+ return err
284+ }
285+
286+ if agentPod .Spec .NodeName != "" {
287+ pr .Annotations [v1alpha3 .JenkinsAgentNodeNameAnnoKey ] = agentPod .Spec .NodeName
288+ klog .Info ("get agent pod running node name: " , agentPod .Spec .NodeName )
289+ }
290+ }
291+
292+ return r .updateLabelsAndAnnotations (ctx , pr )
293+ }
294+
229295func (r * Reconciler ) storePipelineRunData (runResultJSON , nodeDetailsJSON string , pipelineRunCopied * v1alpha3.PipelineRun ) (err error ) {
230296 if r .PipelineRunDataStore == "" {
231297 if pipelineRunCopied .Annotations == nil {
0 commit comments