...

Source file src/edge-infra.dev/pkg/f8n/warehouse/lift/unpack/options.go

Documentation: edge-infra.dev/pkg/f8n/warehouse/lift/unpack

     1  package unpack
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"edge-infra.dev/pkg/f8n/warehouse/cluster"
     7  	"edge-infra.dev/pkg/f8n/warehouse/oci/layer"
     8  )
     9  
    10  // Option is a functional option for unpacking pallets.
    11  type Option func(*options)
    12  
    13  func makeOptions(opts ...Option) (*options, error) {
    14  	o := &options{}
    15  	for _, opt := range opts {
    16  		opt(o)
    17  	}
    18  
    19  	if len(o.keys) > 0 && len(o.types) > 0 {
    20  		return nil, fmt.Errorf("options ForLayerKeys and ForLayerTypes are mutually " +
    21  			"exclusive")
    22  	}
    23  
    24  	if o.infraNamespace != "" && !o.hasInfra() {
    25  		return nil, fmt.Errorf("if WithInfraNamespace is provided, unpacking the " +
    26  			"infrastructure layer must be enabled via ForLayerTypes or ForLayerKeys")
    27  	}
    28  
    29  	return o, nil
    30  }
    31  
    32  type options struct {
    33  	infraNamespace string
    34  	keys           []string
    35  	types          []layer.Type
    36  	provider       cluster.Provider
    37  	render         bool
    38  	parameters     []map[string]string
    39  }
    40  
    41  func (o *options) hasInfra() bool {
    42  	for _, k := range o.keys {
    43  		if k == layer.Infra.String() {
    44  			return true
    45  		}
    46  	}
    47  	for _, t := range o.types {
    48  		if t == layer.Infra {
    49  			return true
    50  		}
    51  	}
    52  	return false
    53  }
    54  
    55  // WithInfraNamespace sets metadata.namespace field for all infrastructure
    56  // objects.
    57  func WithInfraNamespace(ns string) Option {
    58  	return func(o *options) {
    59  		o.infraNamespace = ns
    60  	}
    61  }
    62  
    63  // ForProvider sets the cluster provider to unpack manifests for. This option
    64  // must be provided if the artifact passed to Unpack isn't already a v1.Image or
    65  // unwrap into one.
    66  func ForProvider(p cluster.Provider) Option {
    67  	return func(o *options) {
    68  		o.provider = p
    69  	}
    70  }
    71  
    72  // RenderWith merges the provided parameters with the already set parameters,
    73  // last-in wins for duplicated keys. If this option is provided for unpacking a
    74  // Pallet that isn't renderable, no action is taken.
    75  func RenderWith(parameters ...map[string]string) Option {
    76  	return func(o *options) {
    77  		o.render = true
    78  		o.parameters = parameters
    79  	}
    80  }
    81  
    82  // ForLayerTypes returns the layers associated with the input layer types.
    83  func ForLayerTypes(t ...layer.Type) Option {
    84  	return func(o *options) {
    85  		o.types = t
    86  	}
    87  }
    88  
    89  // ForLayerKeys returns the layers with matching keys. See layer.Key().
    90  func ForLayerKeys(s ...string) Option {
    91  	return func(o *options) {
    92  		o.keys = s
    93  	}
    94  }
    95  

View as plain text