...

Source file src/github.com/google/go-containerregistry/pkg/v1/config.go

Documentation: github.com/google/go-containerregistry/pkg/v1

     1  // Copyright 2018 Google LLC All Rights Reserved.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //    http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package v1
    16  
    17  import (
    18  	"encoding/json"
    19  	"io"
    20  	"time"
    21  )
    22  
    23  // ConfigFile is the configuration file that holds the metadata describing
    24  // how to launch a container. See:
    25  // https://github.com/opencontainers/image-spec/blob/master/config.md
    26  //
    27  // docker_version and os.version are not part of the spec but included
    28  // for backwards compatibility.
    29  type ConfigFile struct {
    30  	Architecture  string    `json:"architecture"`
    31  	Author        string    `json:"author,omitempty"`
    32  	Container     string    `json:"container,omitempty"`
    33  	Created       Time      `json:"created,omitempty"`
    34  	DockerVersion string    `json:"docker_version,omitempty"`
    35  	History       []History `json:"history,omitempty"`
    36  	OS            string    `json:"os"`
    37  	RootFS        RootFS    `json:"rootfs"`
    38  	Config        Config    `json:"config"`
    39  	OSVersion     string    `json:"os.version,omitempty"`
    40  	Variant       string    `json:"variant,omitempty"`
    41  	OSFeatures    []string  `json:"os.features,omitempty"`
    42  }
    43  
    44  // Platform attempts to generates a Platform from the ConfigFile fields.
    45  func (cf *ConfigFile) Platform() *Platform {
    46  	if cf.OS == "" && cf.Architecture == "" && cf.OSVersion == "" && cf.Variant == "" && len(cf.OSFeatures) == 0 {
    47  		return nil
    48  	}
    49  	return &Platform{
    50  		OS:           cf.OS,
    51  		Architecture: cf.Architecture,
    52  		OSVersion:    cf.OSVersion,
    53  		Variant:      cf.Variant,
    54  		OSFeatures:   cf.OSFeatures,
    55  	}
    56  }
    57  
    58  // History is one entry of a list recording how this container image was built.
    59  type History struct {
    60  	Author     string `json:"author,omitempty"`
    61  	Created    Time   `json:"created,omitempty"`
    62  	CreatedBy  string `json:"created_by,omitempty"`
    63  	Comment    string `json:"comment,omitempty"`
    64  	EmptyLayer bool   `json:"empty_layer,omitempty"`
    65  }
    66  
    67  // Time is a wrapper around time.Time to help with deep copying
    68  type Time struct {
    69  	time.Time
    70  }
    71  
    72  // DeepCopyInto creates a deep-copy of the Time value.  The underlying time.Time
    73  // type is effectively immutable in the time API, so it is safe to
    74  // copy-by-assign, despite the presence of (unexported) Pointer fields.
    75  func (t *Time) DeepCopyInto(out *Time) {
    76  	*out = *t
    77  }
    78  
    79  // RootFS holds the ordered list of file system deltas that comprise the
    80  // container image's root filesystem.
    81  type RootFS struct {
    82  	Type    string `json:"type"`
    83  	DiffIDs []Hash `json:"diff_ids"`
    84  }
    85  
    86  // HealthConfig holds configuration settings for the HEALTHCHECK feature.
    87  type HealthConfig struct {
    88  	// Test is the test to perform to check that the container is healthy.
    89  	// An empty slice means to inherit the default.
    90  	// The options are:
    91  	// {} : inherit healthcheck
    92  	// {"NONE"} : disable healthcheck
    93  	// {"CMD", args...} : exec arguments directly
    94  	// {"CMD-SHELL", command} : run command with system's default shell
    95  	Test []string `json:",omitempty"`
    96  
    97  	// Zero means to inherit. Durations are expressed as integer nanoseconds.
    98  	Interval    time.Duration `json:",omitempty"` // Interval is the time to wait between checks.
    99  	Timeout     time.Duration `json:",omitempty"` // Timeout is the time to wait before considering the check to have hung.
   100  	StartPeriod time.Duration `json:",omitempty"` // The start period for the container to initialize before the retries starts to count down.
   101  
   102  	// Retries is the number of consecutive failures needed to consider a container as unhealthy.
   103  	// Zero means inherit.
   104  	Retries int `json:",omitempty"`
   105  }
   106  
   107  // Config is a submessage of the config file described as:
   108  //
   109  //	The execution parameters which SHOULD be used as a base when running
   110  //	a container using the image.
   111  //
   112  // The names of the fields in this message are chosen to reflect the JSON
   113  // payload of the Config as defined here:
   114  // https://git.io/vrAET
   115  // and
   116  // https://github.com/opencontainers/image-spec/blob/master/config.md
   117  type Config struct {
   118  	AttachStderr    bool                `json:"AttachStderr,omitempty"`
   119  	AttachStdin     bool                `json:"AttachStdin,omitempty"`
   120  	AttachStdout    bool                `json:"AttachStdout,omitempty"`
   121  	Cmd             []string            `json:"Cmd,omitempty"`
   122  	Healthcheck     *HealthConfig       `json:"Healthcheck,omitempty"`
   123  	Domainname      string              `json:"Domainname,omitempty"`
   124  	Entrypoint      []string            `json:"Entrypoint,omitempty"`
   125  	Env             []string            `json:"Env,omitempty"`
   126  	Hostname        string              `json:"Hostname,omitempty"`
   127  	Image           string              `json:"Image,omitempty"`
   128  	Labels          map[string]string   `json:"Labels,omitempty"`
   129  	OnBuild         []string            `json:"OnBuild,omitempty"`
   130  	OpenStdin       bool                `json:"OpenStdin,omitempty"`
   131  	StdinOnce       bool                `json:"StdinOnce,omitempty"`
   132  	Tty             bool                `json:"Tty,omitempty"`
   133  	User            string              `json:"User,omitempty"`
   134  	Volumes         map[string]struct{} `json:"Volumes,omitempty"`
   135  	WorkingDir      string              `json:"WorkingDir,omitempty"`
   136  	ExposedPorts    map[string]struct{} `json:"ExposedPorts,omitempty"`
   137  	ArgsEscaped     bool                `json:"ArgsEscaped,omitempty"`
   138  	NetworkDisabled bool                `json:"NetworkDisabled,omitempty"`
   139  	MacAddress      string              `json:"MacAddress,omitempty"`
   140  	StopSignal      string              `json:"StopSignal,omitempty"`
   141  	Shell           []string            `json:"Shell,omitempty"`
   142  }
   143  
   144  // ParseConfigFile parses the io.Reader's contents into a ConfigFile.
   145  func ParseConfigFile(r io.Reader) (*ConfigFile, error) {
   146  	cf := ConfigFile{}
   147  	if err := json.NewDecoder(r).Decode(&cf); err != nil {
   148  		return nil, err
   149  	}
   150  	return &cf, nil
   151  }
   152  

View as plain text