Skip to content

Commit

Permalink
feat(patch: rule evaluation stopper flags. (#112)
Browse files Browse the repository at this point in the history
* feat(patch: rule evaluation stopper flags.

* feat(patch): ignore rule evaluation flags.

* doc(changelog): update changelog.
  • Loading branch information
Zenithar authored Feb 10, 2022
1 parent 26438fc commit 35e0698
Show file tree
Hide file tree
Showing 11 changed files with 423 additions and 77 deletions.
8 changes: 6 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,13 @@

FEATURES:

* bundle/patch:
* support `--stop-at-rule-index=<int>` and `--stop-at-rule-id=<string>` flags for `bundle patch` to stop patch
evaluation before requested rule identifier or index. [#112](https://github.com/elastic/harp/pull/112)
* `--ignore-rule-id` and `--ignore-rule-index` flags to ignore matching rules during bundle patch evaluation. [#112](https://github.com/elastic/harp/pull/112)
* bundle/selector:
* support `regoFile` to load a Rego filter policy from a file.
* `cel` query language
* support `regoFile` to load a Rego filter policy from a file. [#111](https://github.com/elastic/harp/pull/111)
* `cel` query language [#111](https://github.com/elastic/harp/pull/111)
* `p.match_label(globstring, globstring)` can be used to match label key and value
* `p.match_annotation(globstring, globstring)` can be used to match annotation key and value

Expand Down
65 changes: 42 additions & 23 deletions cmd/harp/internal/cmd/bundle_patch.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,24 +21,30 @@ import (
"github.com/spf13/cobra"
"go.uber.org/zap"

"github.com/elastic/harp/pkg/bundle/patch"
"github.com/elastic/harp/pkg/sdk/cmdutil"
"github.com/elastic/harp/pkg/sdk/log"
"github.com/elastic/harp/pkg/tasks/bundle"
tplcmdutil "github.com/elastic/harp/pkg/template/cmdutil"
)

// -----------------------------------------------------------------------------
type bundlePatchParams struct {
inputPath string
outputPath string
patchPath string
valueFiles []string
values []string
stringValues []string
fileValues []string
stopAtRuleIndex int
stopAtRuleID string
ignoreRuleIDs []string
ignoreRuleIndexes []int
}

var bundlePatchCmd = func() *cobra.Command {
var (
inputPath string
outputPath string
patchPath string
valueFiles []string
values []string
stringValues []string
fileValues []string
)
params := &bundlePatchParams{}

cmd := &cobra.Command{
Use: "patch",
Expand All @@ -50,22 +56,31 @@ var bundlePatchCmd = func() *cobra.Command {

// Load values
valueOpts := tplcmdutil.ValueOptions{
ValueFiles: valueFiles,
Values: values,
StringValues: stringValues,
FileValues: fileValues,
ValueFiles: params.valueFiles,
Values: params.values,
StringValues: params.stringValues,
FileValues: params.fileValues,
}
values, err := valueOpts.MergeValues()
if err != nil {
log.For(ctx).Fatal("unable to process values", zap.Error(err))
}

// Prepare patch options.
opts := []patch.OptionFunc{
patch.WithStopAtRuleID(params.stopAtRuleID),
patch.WithStopAtRuleIndex(params.stopAtRuleIndex),
patch.WithIgnoreRuleIDs(params.ignoreRuleIDs...),
patch.WithIgnoreRuleIndexes(params.ignoreRuleIndexes...),
}

// Prepare task
t := &bundle.PatchTask{
ContainerReader: cmdutil.FileReader(inputPath),
PatchReader: cmdutil.FileReader(patchPath),
OutputWriter: cmdutil.FileWriter(outputPath),
ContainerReader: cmdutil.FileReader(params.inputPath),
PatchReader: cmdutil.FileReader(params.patchPath),
OutputWriter: cmdutil.FileWriter(params.outputPath),
Values: values,
Options: opts,
}

// Run the task
Expand All @@ -76,14 +91,18 @@ var bundlePatchCmd = func() *cobra.Command {
}

// Parameters
cmd.Flags().StringVar(&inputPath, "in", "-", "Container input ('-' for stdin or filename)")
cmd.Flags().StringVar(&outputPath, "out", "", "Container output ('-' for stdout or a filename)")
cmd.Flags().StringVar(&patchPath, "spec", "", "Patch specification path ('-' for stdin or filename)")
cmd.Flags().StringVar(&params.inputPath, "in", "-", "Container input ('-' for stdin or filename)")
cmd.Flags().StringVar(&params.outputPath, "out", "", "Container output ('-' for stdout or a filename)")
cmd.Flags().StringVar(&params.patchPath, "spec", "", "Patch specification path ('-' for stdin or filename)")
log.CheckErr("unable to mark 'spec' flag as required.", cmd.MarkFlagRequired("spec"))
cmd.Flags().StringArrayVar(&valueFiles, "values", []string{}, "Specifies value files to load")
cmd.Flags().StringArrayVar(&values, "set", []string{}, "Specifies value (k=v)")
cmd.Flags().StringArrayVar(&stringValues, "set-string", []string{}, "Specifies value (k=string)")
cmd.Flags().StringArrayVar(&fileValues, "set-file", []string{}, "Specifies value (k=filepath)")
cmd.Flags().StringArrayVar(&params.valueFiles, "values", []string{}, "Specifies value files to load")
cmd.Flags().StringArrayVar(&params.values, "set", []string{}, "Specifies value (k=v)")
cmd.Flags().StringArrayVar(&params.stringValues, "set-string", []string{}, "Specifies value (k=string)")
cmd.Flags().StringArrayVar(&params.fileValues, "set-file", []string{}, "Specifies value (k=filepath)")
cmd.Flags().StringVar(&params.stopAtRuleID, "stop-at-rule-id", "", "Stop patch evaluation before the given rule ID")
cmd.Flags().IntVar(&params.stopAtRuleIndex, "stop-at-rule-index", -1, "Stop patch evaluation before the given rule index (0 for first rule)")
cmd.Flags().StringArrayVar(&params.ignoreRuleIDs, "ignore-rule-id", []string{}, "List of Rule identifier to ignore during evaluation")
cmd.Flags().IntSliceVar(&params.ignoreRuleIndexes, "ignore-rule-index", []int{}, "List of Rule index to ignore during evaluation")

return cmd
}
20 changes: 12 additions & 8 deletions docs/cmd/harp_bundle_patch.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,18 @@ harp bundle patch [flags]
### Options

```
-h, --help help for patch
--in string Container input ('-' for stdin or filename) (default "-")
--out string Container output ('-' for stdout or a filename)
--set stringArray Specifies value (k=v)
--set-file stringArray Specifies value (k=filepath)
--set-string stringArray Specifies value (k=string)
--spec string Patch specification path ('-' for stdin or filename)
--values stringArray Specifies value files to load
-h, --help help for patch
--ignore-rule-id stringArray List of Rule identifier to ignore during evaluation
--ignore-rule-index ints List of Rule index to ignore during evaluation
--in string Container input ('-' for stdin or filename) (default "-")
--out string Container output ('-' for stdout or a filename)
--set stringArray Specifies value (k=v)
--set-file stringArray Specifies value (k=filepath)
--set-string stringArray Specifies value (k=string)
--spec string Patch specification path ('-' for stdin or filename)
--stop-at-rule-id string Stop patch evaluation before the given rule ID
--stop-at-rule-index int Stop patch evaluation before the given rule index (0 for first rule) (default -1)
--values stringArray Specifies value files to load
```

### SEE ALSO
Expand Down
57 changes: 57 additions & 0 deletions pkg/bundle/patch/options.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// Licensed to Elasticsearch B.V. under one or more contributor
// license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright
// ownership. Elasticsearch B.V. licenses this file to you under
// the Apache License, Version 2.0 (the "License"); you may
// not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

package patch

type options struct {
stopAtRuleID string
stopAtRuleIndex int
ignoreRuleIDs []string
ignoreRuleIndexes []int
}

type OptionFunc func(o *options)

// -----------------------------------------------------------------------------

// WithStopAtRuleID sets the id of the rule to stop evaluation at.
func WithStopAtRuleID(value string) OptionFunc {
return func(o *options) {
o.stopAtRuleID = value
}
}

// WithStopAtRuleIndex sets the index of the rule to stop evaluation at.
func WithStopAtRuleIndex(value int) OptionFunc {
return func(o *options) {
o.stopAtRuleIndex = value
}
}

// WithIgnoreRuleIDs sets the rule identifiers to ignore.
func WithIgnoreRuleIDs(values ...string) OptionFunc {
return func(o *options) {
o.ignoreRuleIDs = values
}
}

// WithIgnoreRuleIndexes sets the rule indexes to ignore.
func WithIgnoreRuleIndexes(values ...int) OptionFunc {
return func(o *options) {
o.ignoreRuleIndexes = values
}
}
72 changes: 70 additions & 2 deletions pkg/bundle/patch/package.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,14 @@ import (
"encoding/base64"
"fmt"
"sort"
"strings"

"golang.org/x/crypto/blake2b"
"google.golang.org/protobuf/proto"

bundlev1 "github.com/elastic/harp/api/gen/go/harp/bundle/v1"
"github.com/elastic/harp/pkg/bundle"
"github.com/elastic/harp/pkg/sdk/types"
)

// Validate bundle patch.
Expand Down Expand Up @@ -77,8 +79,8 @@ func Checksum(spec *bundlev1.Patch) (string, error) {
}

// Apply given patch to the given bundle.
//nolint:interfacer,gocyclo // Explicit type restriction
func Apply(spec *bundlev1.Patch, b *bundlev1.Bundle, values map[string]interface{}) (*bundlev1.Bundle, error) {
//nolint:interfacer,gocyclo,funlen // Explicit type restriction
func Apply(spec *bundlev1.Patch, b *bundlev1.Bundle, values map[string]interface{}, o ...OptionFunc) (*bundlev1.Bundle, error) {
// Validate spec
if err := Validate(spec); err != nil {
return nil, fmt.Errorf("unable to validate spec: %w", err)
Expand All @@ -101,12 +103,36 @@ func Apply(spec *bundlev1.Patch, b *bundlev1.Bundle, values map[string]interface
bCopy.Packages = []*bundlev1.Package{}
}

// Default evaluation options
dopts := &options{
stopAtRuleID: "",
stopAtRuleIndex: -1,
ignoreRuleIDs: []string{},
ignoreRuleIndexes: []int{},
}

// Apply functions
for _, opt := range o {
opt(dopts)
}

// Process all creation rule first
for i, r := range spec.Spec.Rules {
// Ignore nil rule
if r == nil {
continue
}

// Ignore non creation rules and non strict matcher
if !r.Package.Create || r.Selector.MatchPath.Strict == "" {
continue
}
if shouldIgnoreThisRule(i, r.Id, dopts) {
continue
}
if shouldStopAtThisRule(i, r.Id, dopts) {
break
}

// Create a package
p := &bundlev1.Package{
Expand All @@ -123,6 +149,17 @@ func Apply(spec *bundlev1.Patch, b *bundlev1.Bundle, values map[string]interface
}

for ri, r := range spec.Spec.Rules {
// Ignore nil rule
if r == nil {
continue
}
if shouldIgnoreThisRule(ri, r.Id, dopts) {
continue
}
if shouldStopAtThisRule(ri, r.Id, dopts) {
break
}

// Process all packages
for i, p := range bCopy.Packages {
action, err := executeRule(r, p, values)
Expand Down Expand Up @@ -155,3 +192,34 @@ func Apply(spec *bundlev1.Patch, b *bundlev1.Bundle, values map[string]interface
// No error
return bCopy, nil
}

func shouldStopAtThisRule(idx int, id string, opts *options) bool {
// Stop at index
if opts.stopAtRuleIndex > 0 && idx >= opts.stopAtRuleIndex {
return true
}
// Stop at rule id
if opts.stopAtRuleID != "" && strings.EqualFold(id, opts.stopAtRuleID) {
return true
}

return false
}

func shouldIgnoreThisRule(idx int, id string, opts *options) bool {
// Ignore using index
if len(opts.ignoreRuleIndexes) > 0 {
for _, v := range opts.ignoreRuleIndexes {
if v == idx {
return true
}
}
}

// Ignore using id
if len(opts.ignoreRuleIDs) > 0 {
return types.StringArray(opts.ignoreRuleIDs).Contains(id)
}

return false
}
Loading

0 comments on commit 35e0698

Please sign in to comment.