Skip to content

Commit

Permalink
simplify
Browse files Browse the repository at this point in the history
  • Loading branch information
martinmaly committed May 5, 2022
1 parent 1cc5b42 commit 3540d0d
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 61 deletions.
74 changes: 22 additions & 52 deletions porch/pkg/engine/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ package engine
import (
"context"
"fmt"
"io"
"io/fs"
"io/ioutil"
"os"
Expand All @@ -35,9 +34,8 @@ import (
"github.com/GoogleContainerTools/kpt/porch/pkg/repository"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/trace"
"sigs.k8s.io/kustomize/kyaml/fn/runtime/runtimeutil"
"sigs.k8s.io/kustomize/kyaml/comments"
"sigs.k8s.io/kustomize/kyaml/kio"
"sigs.k8s.io/kustomize/kyaml/kio/kioutil"
"sigs.k8s.io/kustomize/kyaml/yaml"
)

Expand Down Expand Up @@ -556,69 +554,41 @@ func (m *mutationReplaceResources) Apply(ctx context.Context, resources reposito
}

func healConfig(old, new map[string]string) (map[string]string, error) {
// The processor simply replaces the old resource list with new one,
// matching resources by names and copying their IDs over
ff := runtimeutil.FunctionFilter{
Run: func(reader io.Reader, writer io.Writer) error {
items, err := (&kio.ByteReader{
Reader: reader,
WrappingAPIVersion: kio.ResourceListAPIVersion,
WrappingKind: kio.ResourceListKind,
}).Read()
if err != nil {
return fmt.Errorf("failed to parse old config with resource IDs: %w", err)
}
// Copy comments from old config to new
oldResources, err := (&packageReader{
input: repository.PackageResources{Contents: old},
extra: map[string]string{},
}).Read()
if err != nil {
return nil, fmt.Errorf("failed to read old packge resources: %w", err)
}

var copyID kio.FilterFunc = func(r []*yaml.RNode) ([]*yaml.RNode, error) {
for _, n := range r {
for _, original := range items {
if n.GetNamespace() == original.GetNamespace() &&
n.GetName() == original.GetName() &&
n.GetApiVersion() == original.GetApiVersion() &&
n.GetKind() == original.GetKind() {
id, err := original.Pipe(yaml.GetAnnotation(kioutil.IdAnnotation))
if err != nil {
continue
}
if id == nil {
continue
}

n.Pipe(yaml.SetAnnotation(kioutil.IdAnnotation, id.YNode().Value))
}
}
var filter kio.FilterFunc = func(r []*yaml.RNode) ([]*yaml.RNode, error) {
for _, n := range r {
for _, original := range oldResources {
if n.GetNamespace() == original.GetNamespace() &&
n.GetName() == original.GetName() &&
n.GetApiVersion() == original.GetApiVersion() &&
n.GetKind() == original.GetKind() {
comments.CopyComments(original, n)
}
return r, nil
}

return kio.Pipeline{
Inputs: []kio.Reader{&packageReader{
input: repository.PackageResources{Contents: new},
extra: map[string]string{},
}},
Filters: []kio.Filter{copyID},
Outputs: []kio.Writer{&kio.ByteWriter{
Writer: writer,
KeepReaderAnnotations: true,
WrappingAPIVersion: kio.ResourceListAPIVersion,
WrappingKind: kio.ResourceListKind,
}},
}.Execute()
},
GlobalScope: true,
}
return r, nil
}

out := &packageWriter{
output: repository.PackageResources{
Contents: map[string]string{},
},
}

if err := (kio.Pipeline{
Inputs: []kio.Reader{&packageReader{
input: repository.PackageResources{Contents: old},
input: repository.PackageResources{Contents: new},
extra: map[string]string{},
}},
Filters: []kio.Filter{&ff},
Filters: []kio.Filter{filter},
Outputs: []kio.Writer{out},
ContinueOnEmptyResult: true,
}).Execute(); err != nil {
Expand Down
14 changes: 7 additions & 7 deletions porch/pkg/engine/testdata/replace/Kptfile
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
# top comment
apiVersion: kpt.dev/v1
# Kptfile info
info:
# Kptfile description
description: A Google Cloud Storage bucket
# Kptfile kind
kind: Kptfile
# Kptfile metadata
metadata:
name: simple-bucket
annotations:
blueprints.cloud.google.com/title: Google Cloud Storage Bucket blueprint
# Kptfile info
info:
# Kptfile description
description: A Google Cloud Storage bucket
name: simple-bucket
# Kptfile pipeline
pipeline:
# Kptfile mutators
mutators:
- image: gcr.io/kpt-fn/apply-setters:v0.2.0
configMap:
- configMap:
name: updated-bucket-name
namespace: updated-namespace
project-id: updated-project-id
storage-class: updated-storage-class
image: gcr.io/kpt-fn/apply-setters:v0.2.0
4 changes: 2 additions & 2 deletions porch/pkg/engine/testdata/replace/bucket.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ apiVersion: storage.cnrm.cloud.google.com/v1beta1
kind: StorageBucket
# metadata comment
metadata: # kpt-merge: config-control/blueprints-project-bucket
name: blueprints-project-bucket # kpt-set: ${project-id}-${name}
namespace: config-control # kpt-set: ${namespace}
# annotations comment
annotations:
cnrm.cloud.google.com/force-destroy: "false"
cnrm.cloud.google.com/project-id: blueprints-project # kpt-set: ${project-id}
name: blueprints-project-bucket # kpt-set: ${project-id}-${name}
namespace: config-control # kpt-set: ${namespace}
# spec comment
spec:
storageClass: standard # kpt-set: ${storage-class}
Expand Down

0 comments on commit 3540d0d

Please sign in to comment.