Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
99 changes: 55 additions & 44 deletions api/gen/go/harp/bundle/v1/patch.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions api/proto/harp/bundle/v1/patch.proto
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@ message PatchPackage {
PatchOperation labels = 3;
// Secret data operations.
PatchSecret data = 4;
// Flag as remove.
bool remove = 5;
}

// PatchSecret represents secret data operations.
Expand Down
7 changes: 5 additions & 2 deletions cmd/harp/internal/cmd/bundle_diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ var bundleDiffCmd = func() *cobra.Command {
var (
sourcePath string
destinationPath string
generatePatch bool
)

cmd := &cobra.Command{
Expand All @@ -47,6 +48,7 @@ var bundleDiffCmd = func() *cobra.Command {
SourceReader: cmdutil.FileReader(sourcePath),
DestinationReader: cmdutil.FileReader(destinationPath),
OutputWriter: cmdutil.StdoutWriter(),
GeneratePatch: generatePatch,
}

// Run the task
Expand All @@ -57,9 +59,10 @@ var bundleDiffCmd = func() *cobra.Command {
}

// Parameters
cmd.Flags().StringVar(&sourcePath, "src", "", "Container path ('-' for stdin or filename)")
cmd.Flags().StringVar(&destinationPath, "dst", "", "Container path")
cmd.Flags().StringVar(&sourcePath, "old", "", "Container path ('-' for stdin or filename)")
cmd.Flags().StringVar(&destinationPath, "new", "", "Container path ('-' for stdin or filename)")
log.CheckErr("unable to mark 'dst' flag as required.", cmd.MarkFlagRequired("dst"))
cmd.Flags().BoolVar(&generatePatch, "patch", false, "Output as a bundle patch")

return cmd
}
18 changes: 0 additions & 18 deletions pkg/bundle/codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ import (
"google.golang.org/protobuf/proto"

bundlev1 "github.com/elastic/harp/api/gen/go/harp/bundle/v1"
"github.com/elastic/harp/pkg/bundle/compare"
"github.com/elastic/harp/pkg/bundle/secret"
csov1 "github.com/elastic/harp/pkg/cso/v1"
"github.com/elastic/harp/pkg/sdk/security"
Expand Down Expand Up @@ -378,20 +377,3 @@ func JSON(w io.Writer, b *bundlev1.Bundle) error {
// No error
return nil
}

// Diff calculates bundle differences.
func Diff(src, dst *bundlev1.Bundle) (string, error) {
diffs, err := compare.Diff(src, dst)
if err != nil {
return "", fmt.Errorf("unable to compute bundle differences: %w", err)
}

// Serialize as json
jsonPatch, err := json.Marshal(diffs)
if err != nil {
return "", fmt.Errorf("unable to serialize diff result: %w", err)
}

// No error
return string(jsonPatch), nil
}
115 changes: 115 additions & 0 deletions pkg/bundle/compare/patch.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
// 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 compare

import (
"fmt"
"strings"

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

// ToPatch convert oplog to a bundle patch.
func ToPatch(oplog []DiffItem) (*bundlev1.Patch, error) {
// Check arguments
if len(oplog) == 0 {
return nil, fmt.Errorf("unable to generate a patch with an empty oplog")
}

res := &bundlev1.Patch{
ApiVersion: "harp.elastic.co/v1",
Kind: "BundlePatch",
Meta: &bundlev1.PatchMeta{
Name: "autogenerated-patch",
Description: "Patch generated from oplog",
},
Spec: &bundlev1.PatchSpec{
Rules: []*bundlev1.PatchRule{},
},
}

secretMap := map[string]*bundlev1.PatchRule{}

// Generate patch rules
for _, op := range oplog {
if op.Type == "package" {
if op.Operation == Remove {
res.Spec.Rules = append(res.Spec.Rules, &bundlev1.PatchRule{
Selector: &bundlev1.PatchSelector{
MatchPath: &bundlev1.PatchSelectorMatchPath{
Strict: op.Path,
},
},
Package: &bundlev1.PatchPackage{
Remove: true,
},
})
}
continue
}
if op.Type == "secret" {
pathParts := strings.SplitN(op.Path, "#", 2)
pkgRule, ok := secretMap[pathParts[0]]
if !ok {
secretMap[pathParts[0]] = &bundlev1.PatchRule{
Selector: &bundlev1.PatchSelector{
MatchPath: &bundlev1.PatchSelectorMatchPath{
Strict: pathParts[0],
},
},
Package: &bundlev1.PatchPackage{
Data: &bundlev1.PatchSecret{
Kv: &bundlev1.PatchOperation{},
},
},
}
pkgRule = secretMap[pathParts[0]]
}

switch op.Operation {
case Add:
if pkgRule.Package.Data.Kv.Add == nil {
pkgRule.Package.Data.Kv.Add = map[string]string{}
}
pkgRule.Package.Data.Kv.Add[pathParts[1]] = op.Value
case Replace:
if pkgRule.Package.Data.Kv.Update == nil {
pkgRule.Package.Data.Kv.Update = map[string]string{}
}
pkgRule.Package.Data.Kv.Update[pathParts[1]] = op.Value
case Remove:
if pkgRule.Package.Data.Kv.Remove == nil {
pkgRule.Package.Data.Kv.Remove = []string{}
}
pkgRule.Package.Data.Kv.Remove = append(pkgRule.Package.Data.Kv.Remove, pathParts[1])
}
}
}

// Add grouped secret patches
for _, r := range secretMap {
if r == nil {
continue
}

res.Spec.Rules = append(res.Spec.Rules, r)
}

// No error
return res, nil
}
Loading