...

Source file src/edge-infra.dev/pkg/f8n/warehouse/k8s/apis/v1alpha1/options.go

Documentation: edge-infra.dev/pkg/f8n/warehouse/k8s/apis/v1alpha1

     1  package v1alpha1
     2  
     3  import (
     4  	"time"
     5  
     6  	v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
     7  
     8  	"edge-infra.dev/pkg/f8n/warehouse/cluster"
     9  	"edge-infra.dev/pkg/k8s/meta"
    10  )
    11  
    12  // Option describes options for constructing Warehouse K8s objects. If a specific
    13  // option only applies to a single type of Warehouse K8s object, it will be
    14  // ignored by the constructor. See individual option documentation for details.
    15  // +kubebuilder:object:generate=false
    16  type Option func(*options)
    17  
    18  // +kubebuilder:object:generate=false
    19  type options struct {
    20  	apply              ApplyOptions
    21  	unpack             UnpackOptions
    22  	packagePullOptions PackagePullOptions
    23  	labels             map[string]string
    24  	parameters         map[string]string
    25  	repo               string
    26  	artifact           *Artifact
    27  
    28  	// Shipment specific options
    29  	artifacts []BaseArtifact
    30  
    31  	// UnpackedPallet specific options
    32  	owningShipment string
    33  	deps           []meta.LocalObjectReference
    34  }
    35  
    36  func makeOptions(opts ...Option) *options {
    37  	o := &options{
    38  		apply:              ApplyOptions{},
    39  		unpack:             UnpackOptions{},
    40  		packagePullOptions: PackagePullOptions{},
    41  		parameters:         make(map[string]string),
    42  	}
    43  	for _, opt := range opts {
    44  		opt(o)
    45  	}
    46  	return o
    47  }
    48  
    49  // WithPrune enables object pruning.
    50  func WithPrune() Option {
    51  	return func(o *options) {
    52  		o.apply.Prune = true
    53  	}
    54  }
    55  
    56  // WithRetryInterval configures how often failed reconciles are retried.
    57  func WithRetryInterval(t time.Duration) Option {
    58  	return func(o *options) {
    59  		o.apply.RetryInterval = &v1.Duration{Duration: t}
    60  	}
    61  }
    62  
    63  // WithTimeout configures how long the controller will wait for the applied objects to
    64  // reconcile
    65  func WithTimeout(t time.Duration) Option {
    66  	return func(o *options) {
    67  		o.apply.Timeout = &v1.Duration{Duration: t}
    68  	}
    69  }
    70  
    71  // WithInfra enables unpacking infra layers.
    72  func WithInfra() Option {
    73  	return func(o *options) {
    74  		o.unpack.Infra = true
    75  	}
    76  }
    77  
    78  // WithRuntime enables unpacking runtime layers.
    79  func WithRuntime() Option {
    80  	return func(o *options) {
    81  		o.unpack.Runtime = true
    82  	}
    83  }
    84  
    85  // WithCapabilities configures the runtime capabilities to unpack.
    86  func WithCapabilities(caps ...string) Option {
    87  	return func(o *options) {
    88  		o.unpack.Capabilities = caps
    89  	}
    90  }
    91  
    92  // ForProvider controls the K8s cluster provider that we will unpack for.
    93  func ForProvider(p cluster.Provider) Option {
    94  	return func(o *options) {
    95  		o.unpack.Provider = p
    96  	}
    97  }
    98  
    99  // WithUnpackOpts sets the entirety of spec.unpack
   100  func WithUnpackOpts(u UnpackOptions) Option {
   101  	return func(o *options) {
   102  		o.unpack = u
   103  	}
   104  }
   105  
   106  // WithImagePullOpts sets the entirety of spec.imagePull
   107  func WithImagePullOpts(p PackagePullOptions) Option {
   108  	return func(o *options) {
   109  		o.packagePullOptions = p
   110  	}
   111  }
   112  
   113  // WithParameters sets the inlined rendering parameters for both Shipments and
   114  // UnpackedPallets
   115  func WithParameters(parameters map[string]string) Option {
   116  	return func(o *options) {
   117  		o.parameters = parameters
   118  	}
   119  }
   120  
   121  // WithLabels sets the K8s labels on the result object.
   122  func WithLabels(labels map[string]string) Option {
   123  	return func(o *options) {
   124  		o.labels = labels
   125  	}
   126  }
   127  
   128  // FromRepo sets the repository the package will be pulled from. For
   129  // UnpackedPallets, this will override the repository provided via WithArtifact.
   130  func FromRepo(r string) Option {
   131  	return func(o *options) {
   132  		o.repo = r
   133  	}
   134  }
   135  
   136  // WithArtifact sets spec.artifact for UnpackedPallets and will populate
   137  // spec.pallets with a single value and spec.repository for Shipments.
   138  //
   139  // FromRepo and WithArtifacts take precedence, allowing a partially specified
   140  // artifact to be provided to WithArtifact if FromRepo is also provided.
   141  func WithArtifact(a Artifact) Option {
   142  	return func(o *options) {
   143  		o.artifact = &a
   144  	}
   145  }
   146  
   147  // ForShipment sets status.shipment for UnpackedPallets
   148  func ForShipment(shipment string) Option {
   149  	return func(o *options) {
   150  		o.owningShipment = shipment
   151  	}
   152  }
   153  
   154  // WithDeps sets spec.dependsOn for UnpackedPallets
   155  func WithDeps(unpackedPallets ...string) Option {
   156  	return func(o *options) {
   157  		o.deps = make([]meta.LocalObjectReference, len(unpackedPallets))
   158  		for i, d := range unpackedPallets {
   159  			o.deps[i] = meta.LocalObjectReference{Name: d}
   160  		}
   161  	}
   162  }
   163  
   164  // WithArtifacts sets spec.pallets for Shipments.
   165  func WithArtifacts(artifacts ...BaseArtifact) Option {
   166  	return func(o *options) {
   167  		o.artifacts = artifacts
   168  	}
   169  }
   170  

View as plain text