github.com/zntrio/harp/v2@v2.0.9/pkg/tasks/bundle/patch.go (about)

     1  // Licensed to Elasticsearch B.V. under one or more contributor
     2  // license agreements. See the NOTICE file distributed with
     3  // this work for additional information regarding copyright
     4  // ownership. Elasticsearch B.V. licenses this file to you under
     5  // the Apache License, Version 2.0 (the "License"); you may
     6  // not use this file except in compliance with the License.
     7  // You may obtain a copy of the License at
     8  //
     9  //     http://www.apache.org/licenses/LICENSE-2.0
    10  //
    11  // Unless required by applicable law or agreed to in writing,
    12  // software distributed under the License is distributed on an
    13  // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    14  // KIND, either express or implied.  See the License for the
    15  // specific language governing permissions and limitations
    16  // under the License.
    17  
    18  package bundle
    19  
    20  import (
    21  	"context"
    22  	"errors"
    23  	"fmt"
    24  
    25  	"github.com/zntrio/harp/v2/pkg/bundle"
    26  	"github.com/zntrio/harp/v2/pkg/bundle/patch"
    27  	"github.com/zntrio/harp/v2/pkg/sdk/types"
    28  	"github.com/zntrio/harp/v2/pkg/tasks"
    29  )
    30  
    31  // PatchTask implements secret container patching task.
    32  type PatchTask struct {
    33  	PatchReader     tasks.ReaderProvider
    34  	ContainerReader tasks.ReaderProvider
    35  	OutputWriter    tasks.WriterProvider
    36  	Values          map[string]interface{}
    37  	Options         []patch.OptionFunc
    38  }
    39  
    40  // Run the task.
    41  func (t *PatchTask) Run(ctx context.Context) error {
    42  	// Check arguments
    43  	if types.IsNil(t.ContainerReader) {
    44  		return errors.New("unable to run task with a nil containerReader provider")
    45  	}
    46  	if types.IsNil(t.PatchReader) {
    47  		return errors.New("unable to run task with a nil patchReader provider")
    48  	}
    49  	if types.IsNil(t.OutputWriter) {
    50  		return errors.New("unable to run task with a nil outputWriter provider")
    51  	}
    52  
    53  	// Retrieve the container reader
    54  	containerReader, err := t.ContainerReader(ctx)
    55  	if err != nil {
    56  		return fmt.Errorf("unable to retrieve patch reader: %w", err)
    57  	}
    58  
    59  	// Load bundle
    60  	b, err := bundle.FromContainerReader(containerReader)
    61  	if err != nil {
    62  		return fmt.Errorf("unable to load bundle content: %w", err)
    63  	}
    64  
    65  	// Retrieve the patch reader
    66  	patchReader, err := t.PatchReader(ctx)
    67  	if err != nil {
    68  		return fmt.Errorf("unable to retrieve patch reader: %w", err)
    69  	}
    70  
    71  	// Parse the input specification
    72  	spec, err := patch.YAML(patchReader)
    73  	if err != nil {
    74  		return fmt.Errorf("unable to parse patch file: %w", err)
    75  	}
    76  
    77  	// Apply the patch speicification to generate an output bundle
    78  	patchedBundle, err := patch.Apply(ctx, spec, b, t.Values, t.Options...)
    79  	if err != nil {
    80  		return fmt.Errorf("unable to generate output bundle from patch: %w", err)
    81  	}
    82  
    83  	// Retrieve the container reader
    84  	outputWriter, err := t.OutputWriter(ctx)
    85  	if err != nil {
    86  		return fmt.Errorf("unable to retrieve output writer: %w", err)
    87  	}
    88  
    89  	// Dump all content
    90  	if err = bundle.ToContainerWriter(outputWriter, patchedBundle); err != nil {
    91  		return fmt.Errorf("unable to dump bundle content: %w", err)
    92  	}
    93  
    94  	// No error
    95  	return nil
    96  }