github.com/buildpacks/pack@v0.33.3-0.20240516162812-884dd1837311/pkg/client/inspect_builder.go (about)

     1  package client
     2  
     3  import (
     4  	"errors"
     5  
     6  	pubbldr "github.com/buildpacks/pack/builder"
     7  
     8  	"github.com/buildpacks/pack/internal/builder"
     9  	"github.com/buildpacks/pack/pkg/dist"
    10  	"github.com/buildpacks/pack/pkg/image"
    11  )
    12  
    13  // BuilderInfo is a collection of metadata describing a builder created using client.
    14  type BuilderInfo struct {
    15  	// Human readable, description of a builder.
    16  	Description string
    17  
    18  	// Stack name used by the builder.
    19  	Stack string
    20  
    21  	// List of Stack mixins, this information is provided by Stack variable.
    22  	Mixins []string
    23  
    24  	// RunImage provided by the builder.
    25  	RunImages []pubbldr.RunImageConfig
    26  
    27  	// All buildpacks included within the builder.
    28  	Buildpacks []dist.ModuleInfo
    29  
    30  	// Detailed ordering of buildpacks and nested buildpacks where depth is specified.
    31  	Order pubbldr.DetectionOrder
    32  
    33  	// Listing of all buildpack layers in a builder.
    34  	// All elements in the Buildpacks variable are represented in this
    35  	// object.
    36  	BuildpackLayers dist.ModuleLayers
    37  
    38  	// Lifecycle provides the following API versioning information for a builder:
    39  	// - Lifecycle Version used in this builder,
    40  	// - Platform API,
    41  	// - Buildpack API.
    42  	Lifecycle builder.LifecycleDescriptor
    43  
    44  	// Name and Version information from tooling used
    45  	// to produce this builder.
    46  	CreatedBy builder.CreatorMetadata
    47  
    48  	// All extensions included within the builder.
    49  	Extensions []dist.ModuleInfo
    50  
    51  	// Detailed ordering of extensions.
    52  	OrderExtensions pubbldr.DetectionOrder
    53  }
    54  
    55  // BuildpackInfoKey contains all information needed to determine buildpack equivalence.
    56  type BuildpackInfoKey struct {
    57  	ID      string
    58  	Version string
    59  }
    60  
    61  type BuilderInspectionConfig struct {
    62  	OrderDetectionDepth int
    63  }
    64  
    65  type BuilderInspectionModifier func(config *BuilderInspectionConfig)
    66  
    67  func WithDetectionOrderDepth(depth int) BuilderInspectionModifier {
    68  	return func(config *BuilderInspectionConfig) {
    69  		config.OrderDetectionDepth = depth
    70  	}
    71  }
    72  
    73  // InspectBuilder reads label metadata of a local or remote builder image. It initializes a BuilderInfo
    74  // object with this metadata, and returns it. This method will error if the name image cannot be found
    75  // both locally and remotely, or if the found image does not contain the proper labels.
    76  func (c *Client) InspectBuilder(name string, daemon bool, modifiers ...BuilderInspectionModifier) (*BuilderInfo, error) {
    77  	inspector := builder.NewInspector(
    78  		builder.NewImageFetcherWrapper(c.imageFetcher),
    79  		builder.NewLabelManagerProvider(),
    80  		builder.NewDetectionOrderCalculator(),
    81  	)
    82  
    83  	inspectionConfig := BuilderInspectionConfig{OrderDetectionDepth: pubbldr.OrderDetectionNone}
    84  	for _, mod := range modifiers {
    85  		mod(&inspectionConfig)
    86  	}
    87  
    88  	info, err := inspector.Inspect(name, daemon, inspectionConfig.OrderDetectionDepth)
    89  	if err != nil {
    90  		if errors.Is(err, image.ErrNotFound) {
    91  			return nil, nil
    92  		}
    93  		return nil, err
    94  	}
    95  
    96  	return &BuilderInfo{
    97  		Description:     info.Description,
    98  		Stack:           info.StackID,
    99  		Mixins:          info.Mixins,
   100  		RunImages:       info.RunImages,
   101  		Buildpacks:      info.Buildpacks,
   102  		Order:           info.Order,
   103  		BuildpackLayers: info.BuildpackLayers,
   104  		Lifecycle:       info.Lifecycle,
   105  		CreatedBy:       info.CreatedBy,
   106  		Extensions:      info.Extensions,
   107  		OrderExtensions: info.OrderExtensions,
   108  	}, nil
   109  }