Skip to content

Commit 7fae058

Browse files
committed
update agent info to annotation
Signed-off-by: renyunkang <rykren1998@gmail.com>
1 parent eb5b319 commit 7fae058

3 files changed

Lines changed: 70 additions & 0 deletions

File tree

cmd/controller/app/controllers.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ func addControllers(mgr manager.Manager, client k8s.Client, informerFactory info
5353
DevOpsClient: devopsClient,
5454
JenkinsCore: jenkinsCore,
5555
PipelineRunDataStore: s.FeatureOptions.PipelineRunDataStore,
56+
Options: s.JenkinsOptions,
5657
}).SetupWithManager(mgr); err != nil {
5758
klog.Errorf("unable to create pipelinerun-controller, err: %v", err)
5859
return

controllers/jenkins/pipelinerun/pipelinerun_controller.go

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
229295
func (r *Reconciler) storePipelineRunData(runResultJSON, nodeDetailsJSON string, pipelineRunCopied *v1alpha3.PipelineRun) (err error) {
230296
if r.PipelineRunDataStore == "" {
231297
if pipelineRunCopied.Annotations == nil {

pkg/api/devops/v1alpha3/groupversion_info.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ const (
4040
PipelineRunSCMRefNameField = "spec.scm.ref-name"
4141
// PipelineRunIdentifierIndexerName is an indexer name of PipelineRun identifier.
4242
PipelineRunIdentifierIndexerName = "pipelinerun.identifier"
43+
44+
JenkinsAgentPodNameAnnoKey = devops.GroupName + "/agent-pod-name"
45+
JenkinsAgentNodeNameAnnoKey = devops.GroupName + "/agent-node-name"
4346
)
4447

4548
var (

0 commit comments

Comments
 (0)