sigs.k8s.io/prow@v0.0.0-20240503223140-c5e374dc7eb1/pkg/spyglass/podlogartifact_fetcher.go (about)

     1  /*
     2  Copyright 2018 The Kubernetes Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package spyglass
    18  
    19  import (
    20  	"context"
    21  	"fmt"
    22  	"strings"
    23  
    24  	"sigs.k8s.io/prow/pkg/kube"
    25  	"sigs.k8s.io/prow/pkg/spyglass/api"
    26  	"sigs.k8s.io/prow/pkg/spyglass/lenses/common"
    27  )
    28  
    29  const singleLogName = "build-log.txt"
    30  
    31  // PodLogArtifactFetcher is used to fetch artifacts from k8s apiserver
    32  type PodLogArtifactFetcher struct {
    33  	jobAgent
    34  }
    35  
    36  // NewPodLogArtifactFetcher returns a PodLogArtifactFetcher using the given job agent as storage
    37  func NewPodLogArtifactFetcher(ja jobAgent) *PodLogArtifactFetcher {
    38  	return &PodLogArtifactFetcher{jobAgent: ja}
    39  }
    40  
    41  // artifact constructs an artifact handle for the given job build
    42  func (af *PodLogArtifactFetcher) Artifact(_ context.Context, key, artifactName string, sizeLimit int64) (api.Artifact, error) {
    43  	jobName, buildID, err := common.KeyToJob(key)
    44  	if err != nil {
    45  		return nil, fmt.Errorf("could not derive job: %w", err)
    46  	}
    47  	containerName := containerName(artifactName)
    48  	podLog, err := NewPodLogArtifact(jobName, buildID, artifactName, containerName, sizeLimit, af.jobAgent)
    49  	if err != nil {
    50  		return nil, fmt.Errorf("error accessing pod log from given source: %w", err)
    51  	}
    52  	return podLog, nil
    53  }
    54  
    55  func containerName(artifactName string) string {
    56  	if artifactName == singleLogName {
    57  		return kube.TestContainerName
    58  	}
    59  	return strings.TrimSuffix(artifactName, fmt.Sprintf("-%s", singleLogName))
    60  }