@@ -21,6 +21,7 @@ import (
21
21
"context"
22
22
"encoding/json"
23
23
"errors"
24
+ "io"
24
25
"log"
25
26
"net/http"
26
27
"path/filepath"
@@ -30,17 +31,19 @@ import (
30
31
"sigs.k8s.io/controller-runtime/pkg/client"
31
32
32
33
experimentv1beta1 "github.com/kubeflow/katib/pkg/apis/controller/experiments/v1beta1"
33
- trialsv1beta1 "github.com/kubeflow/katib/pkg/apis/controller/trials/v1beta1"
34
34
suggestionv1beta1 "github.com/kubeflow/katib/pkg/apis/controller/suggestions/v1beta1"
35
+ trialsv1beta1 "github.com/kubeflow/katib/pkg/apis/controller/trials/v1beta1"
35
36
api_pb_v1beta1 "github.com/kubeflow/katib/pkg/apis/manager/v1beta1"
36
37
consts "github.com/kubeflow/katib/pkg/controller.v1beta1/consts"
37
38
"github.com/kubeflow/katib/pkg/util/v1beta1/katibclient"
38
39
corev1 "k8s.io/api/core/v1"
39
40
41
+ common "github.com/kubeflow/katib/pkg/apis/controller/common/v1beta1"
42
+ mccommon "github.com/kubeflow/katib/pkg/metricscollector/v1beta1/common"
40
43
apiv1 "k8s.io/api/core/v1"
41
44
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
42
45
"k8s.io/apimachinery/pkg/types"
43
- corev1 "k8s.io/client-go/kubernetes/typed/core/v1 "
46
+ "k8s.io/client-go/kubernetes"
44
47
"sigs.k8s.io/controller-runtime/pkg/client/config"
45
48
)
46
49
@@ -586,11 +589,19 @@ func (k *KatibUIHandler) FetchTrial(w http.ResponseWriter, r *http.Request) {
586
589
587
590
// FetchTrialLogs fetches logs for a trial in specific namespace.
588
591
func (k * KatibUIHandler ) FetchTrialLogs (w http.ResponseWriter , r * http.Request ) {
589
- log .Printf ("Requesting logs" )
590
-
591
592
trialName := r .URL .Query ()["trialName" ][0 ]
592
593
namespace := r .URL .Query ()["namespace" ][0 ]
593
- log .Printf ("Requesting logs" )
594
+
595
+ user , err := IsAuthorized (consts .ActionTypeGet , namespace , consts .PluralTrial , "" , trialName , trialsv1beta1 .SchemeGroupVersion , k .katibClient .GetClient (), r )
596
+ if user == "" && err != nil {
597
+ log .Printf ("No user provided in kubeflow-userid header." )
598
+ http .Error (w , err .Error (), http .StatusUnauthorized )
599
+ return
600
+ } else if err != nil {
601
+ log .Printf ("The user: %s is not authorized to get trial: %s in namespace: %s \n " , user , trialName , namespace )
602
+ http .Error (w , err .Error (), http .StatusForbidden )
603
+ return
604
+ }
594
605
595
606
logs , err := getTrialLogs (k , trialName , namespace )
596
607
if err != nil {
@@ -613,50 +624,48 @@ func (k *KatibUIHandler) FetchTrialLogs(w http.ResponseWriter, r *http.Request)
613
624
614
625
// GetTrialLogs returns logs of a master Pod for the given job name and namespace
615
626
func getTrialLogs (k * KatibUIHandler , trialName string , namespace string ) (string , error ) {
616
- cfg , err := config .GetConfig ()
617
- if err != nil {
618
- return "" , err
619
- }
620
-
621
- clientset , err := corev1 .NewForConfig (cfg )
622
- if err != nil {
623
- return "" , err
624
- }
625
-
626
627
trial := & trialsv1beta1.Trial {}
627
628
if err := k .katibClient .GetClient ().Get (context .TODO (), types.NamespacedName {Name : trialName , Namespace : namespace }, trial ); err != nil {
628
629
return "" , err
629
630
}
630
631
631
- selectionLabel := "training.kubeflow.org/job-name=" + trialName
632
-
632
+ selectionLabel := consts .LabelTrialName + "=" + trialName
633
633
for primaryKey , primaryValue := range trial .Spec .PrimaryPodLabels {
634
634
selectionLabel = selectionLabel + "," + primaryKey + "=" + primaryValue
635
635
}
636
636
637
- podList , err := clientset .Pods (namespace ).List (context .Background (), metav1.ListOptions {LabelSelector : selectionLabel })
637
+ cfg , err := config .GetConfig ()
638
+ if err != nil {
639
+ return "" , err
640
+ }
641
+ clientset , err := kubernetes .NewForConfig (cfg )
642
+ if err != nil {
643
+ return "" , err
644
+ }
645
+
646
+ podList , err := clientset .CoreV1 ().Pods (namespace ).List (context .Background (), metav1.ListOptions {LabelSelector : selectionLabel })
638
647
if err != nil {
639
648
return "" , err
640
649
}
641
650
642
651
if len (podList .Items ) == 0 {
643
652
message := `Logs for the trial could not be found.
644
653
Was 'retain: true' specified in the Experiment definition?
645
- An example can be found here: https://github.com/kubeflow/katib/blob/master /examples/v1beta1/argo/argo-workflow.yaml#L33`
654
+ An example can be found here: https://github.com/kubeflow/katib/blob/7bf39225f7235ee4ba6cf285ecc2c455c6471234 /examples/v1beta1/argo/argo-workflow.yaml#L33`
646
655
647
656
return message , nil
648
657
}
658
+ if len (podList .Items ) > 1 {
659
+ return "" , errors .New ("More than one master replica found" )
660
+ }
649
661
650
662
podLogOpts := apiv1.PodLogOptions {}
651
663
podLogOpts .Container = trial .Spec .PrimaryContainerName
652
- for container := range podList .Items [0 ].Spec .Containers {
653
- if podList .Items [0 ].Spec .Containers [container ].Name == "metrics-logger-and-collector" {
654
- podLogOpts .Container = "metrics-logger-and-collector"
655
- break
656
- }
664
+ if trial .Spec .MetricsCollector .Collector .Kind == common .StdOutCollector {
665
+ podLogOpts .Container = mccommon .MetricLoggerCollectorContainerName
657
666
}
658
667
659
- req := clientset .Pods (namespace ).GetLogs (podList .Items [0 ].Name , & podLogOpts )
668
+ req := clientset .CoreV1 (). Pods (namespace ).GetLogs (podList .Items [0 ].Name , & podLogOpts )
660
669
podLogs , err := req .Stream (context .Background ())
661
670
if err != nil {
662
671
return "" , err
0 commit comments