github.com/kubeshop/testkube@v1.17.23/pkg/imageinspector/skopeofetcher.go (about)

     1  package imageinspector
     2  
     3  import (
     4  	"context"
     5  	"strconv"
     6  	"strings"
     7  	"time"
     8  
     9  	corev1 "k8s.io/api/core/v1"
    10  
    11  	"github.com/kubeshop/testkube/pkg/skopeo"
    12  )
    13  
    14  type skopeoFetcher struct {
    15  }
    16  
    17  func NewSkopeoFetcher() InfoFetcher {
    18  	return &skopeoFetcher{}
    19  }
    20  
    21  func (s *skopeoFetcher) Fetch(ctx context.Context, registry, image string, pullSecrets []corev1.Secret) (*Info, error) {
    22  	client, err := skopeo.NewClientFromSecrets(pullSecrets, registry)
    23  	if err != nil {
    24  		return nil, err
    25  	}
    26  	info, err := client.Inspect(registry, image) // TODO: Support passing context
    27  	if err != nil {
    28  		return nil, err
    29  	}
    30  	user, group := determineUserGroupPair(info.Config.User)
    31  	return &Info{
    32  		FetchedAt:  time.Now(),
    33  		Entrypoint: info.Config.Entrypoint,
    34  		Cmd:        info.Config.Cmd,
    35  		Shell:      info.Shell,
    36  		WorkingDir: info.Config.WorkingDir,
    37  		User:       user,
    38  		Group:      group,
    39  	}, nil
    40  }
    41  
    42  func determineUserGroupPair(userGroupStr string) (int64, int64) {
    43  	if userGroupStr == "" {
    44  		userGroupStr = "0"
    45  	}
    46  	userStr, groupStr, _ := strings.Cut(userGroupStr, ":")
    47  	if groupStr == "" {
    48  		groupStr = "0"
    49  	}
    50  	user, _ := strconv.Atoi(userStr)
    51  	group, _ := strconv.Atoi(groupStr)
    52  	return int64(user), int64(group)
    53  }