mirror of
https://github.com/containers/podman.git
synced 2025-06-13 19:52:11 +08:00
Add functions to return image informations
Signed-off-by: Daniel J Walsh <dwalsh@redhat.com>
This commit is contained in:
@ -867,22 +867,77 @@ func (i *Image) Intermediate(ctx context.Context) (bool, error) {
|
|||||||
return false, nil
|
return false, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// User returns the image's user
|
||||||
|
func (i *Image) User(ctx context.Context) (string, error) {
|
||||||
|
imgInspect, err := i.inspect(ctx, false)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
return imgInspect.Config.User, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// StopSignal returns the image's StopSignal
|
||||||
|
func (i *Image) StopSignal(ctx context.Context) (string, error) {
|
||||||
|
imgInspect, err := i.inspect(ctx, false)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
return imgInspect.Config.StopSignal, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// WorkingDir returns the image's WorkingDir
|
||||||
|
func (i *Image) WorkingDir(ctx context.Context) (string, error) {
|
||||||
|
imgInspect, err := i.inspect(ctx, false)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
return imgInspect.Config.WorkingDir, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Cmd returns the image's cmd
|
||||||
|
func (i *Image) Cmd(ctx context.Context) ([]string, error) {
|
||||||
|
imgInspect, err := i.inspect(ctx, false)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return imgInspect.Config.Cmd, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Entrypoint returns the image's entrypoint
|
||||||
|
func (i *Image) Entrypoint(ctx context.Context) ([]string, error) {
|
||||||
|
imgInspect, err := i.inspect(ctx, false)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return imgInspect.Config.Entrypoint, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Env returns the image's env
|
||||||
|
func (i *Image) Env(ctx context.Context) ([]string, error) {
|
||||||
|
imgInspect, err := i.imageInspectInfo(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return imgInspect.Env, nil
|
||||||
|
}
|
||||||
|
|
||||||
// Labels returns the image's labels
|
// Labels returns the image's labels
|
||||||
func (i *Image) Labels(ctx context.Context) (map[string]string, error) {
|
func (i *Image) Labels(ctx context.Context) (map[string]string, error) {
|
||||||
imgInspect, err := i.imageInspectInfo(ctx)
|
imgInspect, err := i.imageInspectInfo(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, nil
|
return nil, err
|
||||||
}
|
}
|
||||||
return imgInspect.Labels, nil
|
return imgInspect.Labels, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetLabel Returns a case-insensitive match of a given label
|
// GetLabel Returns a case-insensitive match of a given label
|
||||||
func (i *Image) GetLabel(ctx context.Context, label string) (string, error) {
|
func (i *Image) GetLabel(ctx context.Context, label string) (string, error) {
|
||||||
imageLabels, err := i.Labels(ctx)
|
labels, err := i.Labels(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
for k, v := range imageLabels {
|
|
||||||
|
for k, v := range labels {
|
||||||
if strings.ToLower(k) == strings.ToLower(label) {
|
if strings.ToLower(k) == strings.ToLower(label) {
|
||||||
return v, nil
|
return v, nil
|
||||||
}
|
}
|
||||||
|
@ -20,17 +20,27 @@ func CompleteSpec(ctx context.Context, r *libpod.Runtime, s *specgen.SpecGenerat
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Image stop signal
|
// Image stop signal
|
||||||
if s.StopSignal == nil && newImage.Config != nil {
|
if s.StopSignal == nil {
|
||||||
sig, err := signal.ParseSignalNameOrNumber(newImage.Config.StopSignal)
|
stopSignal, err := newImage.StopSignal(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
sig, err := signal.ParseSignalNameOrNumber(stopSignal)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
s.StopSignal = &sig
|
s.StopSignal = &sig
|
||||||
}
|
}
|
||||||
|
|
||||||
// Image envs from the image if they don't exist
|
// Image envs from the image if they don't exist
|
||||||
// already
|
// already
|
||||||
if newImage.Config != nil && len(newImage.Config.Env) > 0 {
|
env, err := newImage.Env(ctx)
|
||||||
envs, err := envLib.ParseSlice(newImage.Config.Env)
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(env) > 0 {
|
||||||
|
envs, err := envLib.ParseSlice(env)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -41,14 +51,17 @@ func CompleteSpec(ctx context.Context, r *libpod.Runtime, s *specgen.SpecGenerat
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
labels, err := newImage.Labels(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
// labels from the image that dont exist already
|
// labels from the image that dont exist already
|
||||||
if config := newImage.Config; config != nil {
|
for k, v := range labels {
|
||||||
for k, v := range config.Labels {
|
|
||||||
if _, exists := s.Labels[k]; !exists {
|
if _, exists := s.Labels[k]; !exists {
|
||||||
s.Labels[k] = v
|
s.Labels[k] = v
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
// annotations
|
// annotations
|
||||||
// in the event this container is in a pod, and the pod has an infra container
|
// in the event this container is in a pod, and the pod has an infra container
|
||||||
@ -75,20 +88,30 @@ func CompleteSpec(ctx context.Context, r *libpod.Runtime, s *specgen.SpecGenerat
|
|||||||
}
|
}
|
||||||
|
|
||||||
// entrypoint
|
// entrypoint
|
||||||
if config := newImage.Config; config != nil {
|
entrypoint, err := newImage.Entrypoint(ctx)
|
||||||
if len(s.Entrypoint) < 1 && len(config.Entrypoint) > 0 {
|
if err != nil {
|
||||||
s.Entrypoint = config.Entrypoint
|
return err
|
||||||
}
|
}
|
||||||
if len(s.Command) < 1 && len(config.Cmd) > 0 {
|
if len(s.Entrypoint) < 1 && len(entrypoint) > 0 {
|
||||||
s.Command = config.Cmd
|
s.Entrypoint = entrypoint
|
||||||
|
}
|
||||||
|
command, err := newImage.Cmd(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if len(s.Command) < 1 && len(command) > 0 {
|
||||||
|
s.Command = command
|
||||||
}
|
}
|
||||||
if len(s.Command) < 1 && len(s.Entrypoint) < 1 {
|
if len(s.Command) < 1 && len(s.Entrypoint) < 1 {
|
||||||
return errors.Errorf("No command provided or as CMD or ENTRYPOINT in this image")
|
return errors.Errorf("No command provided or as CMD or ENTRYPOINT in this image")
|
||||||
}
|
}
|
||||||
// workdir
|
// workdir
|
||||||
if len(s.WorkDir) < 1 && len(config.WorkingDir) > 1 {
|
workingDir, err := newImage.WorkingDir(ctx)
|
||||||
s.WorkDir = config.WorkingDir
|
if err != nil {
|
||||||
|
return err
|
||||||
}
|
}
|
||||||
|
if len(s.WorkDir) < 1 && len(workingDir) > 1 {
|
||||||
|
s.WorkDir = workingDir
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(s.SeccompProfilePath) < 1 {
|
if len(s.SeccompProfilePath) < 1 {
|
||||||
@ -99,15 +122,17 @@ func CompleteSpec(ctx context.Context, r *libpod.Runtime, s *specgen.SpecGenerat
|
|||||||
s.SeccompProfilePath = p
|
s.SeccompProfilePath = p
|
||||||
}
|
}
|
||||||
|
|
||||||
if user := s.User; len(user) == 0 {
|
if len(s.User) == 0 {
|
||||||
switch {
|
s.User, err = newImage.User(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
// TODO This should be enabled when namespaces actually work
|
// TODO This should be enabled when namespaces actually work
|
||||||
//case usernsMode.IsKeepID():
|
//case usernsMode.IsKeepID():
|
||||||
// user = fmt.Sprintf("%d:%d", rootless.GetRootlessUID(), rootless.GetRootlessGID())
|
// user = fmt.Sprintf("%d:%d", rootless.GetRootlessUID(), rootless.GetRootlessGID())
|
||||||
case newImage.Config == nil || (newImage.Config != nil && len(newImage.Config.User) == 0):
|
if len(s.User) == 0 {
|
||||||
s.User = "0"
|
s.User = "0"
|
||||||
default:
|
|
||||||
s.User = newImage.Config.User
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if err := finishThrottleDevices(s); err != nil {
|
if err := finishThrottleDevices(s); err != nil {
|
||||||
|
Reference in New Issue
Block a user