get.porter.sh/porter@v1.3.0/pkg/cnab/docker.go (about)

     1  package cnab
     2  
     3  import (
     4  	"encoding/json"
     5  	"errors"
     6  	"fmt"
     7  )
     8  
     9  const (
    10  	// DockerExtensionShortHand is the short suffix of the DockerExtensionKey.
    11  	DockerExtensionShortHand = "docker"
    12  
    13  	// DockerExtensionKey represents the full key for the Docker Extension
    14  	DockerExtensionKey = OfficialExtensionsPrefix + DockerExtensionShortHand
    15  
    16  	// DockerExtensionSchema represents the schema for the Docker Extension
    17  	DockerExtensionSchema = "schema/io-cnab-docker.schema.json"
    18  )
    19  
    20  // DockerExtension represents a required extension enabling access to the host Docker daemon
    21  var DockerExtension = RequiredExtension{
    22  	Shorthand: DockerExtensionShortHand,
    23  	Key:       DockerExtensionKey,
    24  	Schema:    "schema/io-cnab-docker.schema.json",
    25  	Reader:    DockerExtensionReader,
    26  }
    27  
    28  // Docker describes the set of custom extension metadata associated with the Docker extension
    29  type Docker struct {
    30  	// Privileged represents whether or not the Docker container should run as --privileged
    31  	Privileged bool `json:"privileged,omitempty"`
    32  }
    33  
    34  // DockerExtensionReader is a Reader for the DockerExtension,
    35  // which reads from the applicable section in the provided bundle and
    36  // returns the raw data in the form of an interface
    37  func DockerExtensionReader(bun ExtendedBundle) (interface{}, error) {
    38  	return bun.DockerExtensionReader()
    39  }
    40  
    41  // DockerExtensionReader is a Reader for the DockerExtension,
    42  // which reads from the applicable section in the provided bundle and
    43  // returns the raw data in the form of an interface
    44  func (b ExtendedBundle) DockerExtensionReader() (interface{}, error) {
    45  	data, ok := b.Custom[DockerExtensionKey]
    46  	if !ok {
    47  		return nil, errors.New("no custom extension configuration found")
    48  	}
    49  
    50  	dataB, err := json.Marshal(data)
    51  	if err != nil {
    52  		return nil, fmt.Errorf("could not marshal the untyped %q extension data %q: %w",
    53  			DockerExtensionKey, string(dataB), err)
    54  	}
    55  
    56  	dha := Docker{}
    57  	err = json.Unmarshal(dataB, &dha)
    58  	if err != nil {
    59  		return nil, fmt.Errorf("could not unmarshal the %q extension %q: %w",
    60  			DockerExtensionKey, string(dataB), err)
    61  	}
    62  
    63  	return dha, nil
    64  }
    65  
    66  // GetDocker checks if the docker extension is present and returns its
    67  // extension configuration.
    68  func (e ProcessedExtensions) GetDocker() (dockerExt Docker, dockerRequired bool, err error) {
    69  	ext, extensionRequired := e[DockerExtensionKey]
    70  
    71  	dockerExt, ok := ext.(Docker)
    72  	if !ok && extensionRequired {
    73  		return Docker{}, extensionRequired, fmt.Errorf("unable to parse Docker extension config: %+v", dockerExt)
    74  	}
    75  
    76  	return dockerExt, extensionRequired, nil
    77  }
    78  
    79  // SupportsDocker checks if the bundle supports docker.
    80  func (b ExtendedBundle) SupportsDocker() bool {
    81  	return b.SupportsExtension(DockerExtensionKey)
    82  }