Skip to content

Commit

Permalink
Use apply-setters directly (#2719)
Browse files Browse the repository at this point in the history
  • Loading branch information
martinmaly authored Feb 3, 2022
1 parent dd40ec8 commit 08059cf
Show file tree
Hide file tree
Showing 7 changed files with 238 additions and 0 deletions.
1 change: 1 addition & 0 deletions porch/apiserver/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ replace (
require (
cloud.google.com/go v0.99.0 // indirect
github.com/GoogleContainerTools/kpt v0.0.0-00010101000000-000000000000 // indirect
github.com/GoogleContainerTools/kpt-functions-catalog/functions/go/apply-setters v0.2.0 // indirect
github.com/Microsoft/go-winio v0.5.1 // indirect
github.com/NYTimes/gziphandler v1.1.1 // indirect
github.com/ProtonMail/go-crypto v0.0.0-20220113124808-70ae35bab23f // indirect
Expand Down
3 changes: 3 additions & 0 deletions porch/apiserver/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo=
github.com/DataDog/datadog-go v3.2.0+incompatible/go.mod h1:LButxg5PwREeZtORoXG3tL4fMGNddJ+vMq1mwgfaqoQ=
github.com/GoogleContainerTools/kpt-functions-catalog/functions/go/apply-setters v0.2.0 h1:GhM9JLR+vW4/jPuL7bGVAEUsIIp5xhJKhm17wSyQZLY=
github.com/GoogleContainerTools/kpt-functions-catalog/functions/go/apply-setters v0.2.0/go.mod h1:D+1CuvT4BecI7ZokGUVPdjnhT+z0z1/9NB6HGH4cTSI=
github.com/MakeNowJust/heredoc v0.0.0-20170808103936-bb23615498cd/go.mod h1:64YHyfSL2R96J44Nlwm39UHepQbyR5q10x7iYa1ks2E=
github.com/Masterminds/semver v1.5.0/go.mod h1:MB6lktGJrhw8PrUyiEoblNEGEQ+RzHPF078ddwwvV3Y=
github.com/Microsoft/go-winio v0.4.11/go.mod h1:VhR8bwka0BXejwEJY73c50VrPtXAaKcyvVC4A4RozmA=
Expand Down Expand Up @@ -1720,6 +1722,7 @@ sigs.k8s.io/kustomize/api v0.8.11 h1:LzQzlq6Z023b+mBtc6v72N2mSHYmN8x7ssgbf/hv0H8
sigs.k8s.io/kustomize/api v0.8.11/go.mod h1:a77Ls36JdfCWojpUqR6m60pdGY1AYFix4AH83nJtY1g=
sigs.k8s.io/kustomize/cmd/config v0.9.13/go.mod h1:7547FLF8W/lTaDf0BDqFTbZxM9zqwEJqCKN9sSR0xSs=
sigs.k8s.io/kustomize/kustomize/v4 v4.2.0/go.mod h1:MOkR6fmhwG7hEDRXBYELTi5GSFcLwfqwzTRHW3kv5go=
sigs.k8s.io/kustomize/kyaml v0.10.21/go.mod h1:TYWhGwW9vjoRh3rWqBwB/ZOXyEGRVWe7Ggc3+KZIO+c=
sigs.k8s.io/kustomize/kyaml v0.11.0/go.mod h1:GNMwjim4Ypgp/MueD3zXHLRJEjz7RvtPae0AwlvEMFM=
sigs.k8s.io/kustomize/kyaml v0.12.0/go.mod h1:FTJxEZ86ScK184NpGSAQcfEqee0nul8oLCK30D47m4E=
sigs.k8s.io/kustomize/kyaml v0.13.1-0.20211203194734-cd2c6a1ad117/go.mod h1:FTJxEZ86ScK184NpGSAQcfEqee0nul8oLCK30D47m4E=
Expand Down
1 change: 1 addition & 0 deletions porch/engine/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ go 1.17

require (
github.com/GoogleContainerTools/kpt v0.0.0-00010101000000-000000000000
github.com/GoogleContainerTools/kpt-functions-catalog/functions/go/apply-setters v0.2.0
github.com/GoogleContainerTools/kpt/porch/api v0.0.0-00010101000000-000000000000
github.com/GoogleContainerTools/kpt/porch/controllers v0.0.0-00010101000000-000000000000
github.com/GoogleContainerTools/kpt/porch/repository v0.0.0-00010101000000-000000000000
Expand Down
3 changes: 3 additions & 0 deletions porch/engine/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ github.com/Azure/go-autorest/tracing v0.6.0/go.mod h1:+vhtPC754Xsa23ID7GlGsrdKBp
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo=
github.com/DataDog/datadog-go v3.2.0+incompatible/go.mod h1:LButxg5PwREeZtORoXG3tL4fMGNddJ+vMq1mwgfaqoQ=
github.com/GoogleContainerTools/kpt-functions-catalog/functions/go/apply-setters v0.2.0 h1:GhM9JLR+vW4/jPuL7bGVAEUsIIp5xhJKhm17wSyQZLY=
github.com/GoogleContainerTools/kpt-functions-catalog/functions/go/apply-setters v0.2.0/go.mod h1:D+1CuvT4BecI7ZokGUVPdjnhT+z0z1/9NB6HGH4cTSI=
github.com/MakeNowJust/heredoc v0.0.0-20170808103936-bb23615498cd/go.mod h1:64YHyfSL2R96J44Nlwm39UHepQbyR5q10x7iYa1ks2E=
github.com/Masterminds/semver v1.5.0/go.mod h1:MB6lktGJrhw8PrUyiEoblNEGEQ+RzHPF078ddwwvV3Y=
github.com/Microsoft/go-winio v0.4.11/go.mod h1:VhR8bwka0BXejwEJY73c50VrPtXAaKcyvVC4A4RozmA=
Expand Down Expand Up @@ -1619,6 +1621,7 @@ sigs.k8s.io/kustomize/api v0.8.11 h1:LzQzlq6Z023b+mBtc6v72N2mSHYmN8x7ssgbf/hv0H8
sigs.k8s.io/kustomize/api v0.8.11/go.mod h1:a77Ls36JdfCWojpUqR6m60pdGY1AYFix4AH83nJtY1g=
sigs.k8s.io/kustomize/cmd/config v0.9.13/go.mod h1:7547FLF8W/lTaDf0BDqFTbZxM9zqwEJqCKN9sSR0xSs=
sigs.k8s.io/kustomize/kustomize/v4 v4.2.0/go.mod h1:MOkR6fmhwG7hEDRXBYELTi5GSFcLwfqwzTRHW3kv5go=
sigs.k8s.io/kustomize/kyaml v0.10.21/go.mod h1:TYWhGwW9vjoRh3rWqBwB/ZOXyEGRVWe7Ggc3+KZIO+c=
sigs.k8s.io/kustomize/kyaml v0.11.0/go.mod h1:GNMwjim4Ypgp/MueD3zXHLRJEjz7RvtPae0AwlvEMFM=
sigs.k8s.io/kustomize/kyaml v0.12.0/go.mod h1:FTJxEZ86ScK184NpGSAQcfEqee0nul8oLCK30D47m4E=
sigs.k8s.io/kustomize/kyaml v0.13.1-0.20211203194734-cd2c6a1ad117/go.mod h1:FTJxEZ86ScK184NpGSAQcfEqee0nul8oLCK30D47m4E=
Expand Down
37 changes: 37 additions & 0 deletions porch/engine/pkg/internal/apply-setters.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// Copyright 2022 Google LLC
//
// Licensed 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 internal

import (
"fmt"

function "github.com/GoogleContainerTools/kpt-functions-catalog/functions/go/apply-setters/applysetters"
"sigs.k8s.io/kustomize/kyaml/fn/framework"
)

func applySetters(rl *framework.ResourceList) error {
if rl.FunctionConfig == nil {
return nil // nothing to do
}

var fn function.ApplySetters
function.Decode(rl.FunctionConfig, &fn)
if items, err := fn.Filter(rl.Items); err != nil {
return fmt.Errorf("apply-setter evaluation failed: %w", err)
} else {
rl.Items = items
}
return nil
}
1 change: 1 addition & 0 deletions porch/engine/pkg/internal/functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
)

var functions map[string]framework.ResourceListProcessorFunc = map[string]framework.ResourceListProcessorFunc{
"gcr.io/kpt-fn/apply-setters:v0.2.0": applySetters,
"gcr.io/kpt-fn/set-labels:v0.1.5": setLabels,
"gcr.io/kpt-fn/set-namespace:v0.2.0": setNamespace,
}
Expand Down
192 changes: 192 additions & 0 deletions porch/engine/pkg/kpt/render_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,192 @@
// Copyright 2022 Google LLC
//
// Licensed 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 kpt

import (
"testing"

"github.com/GoogleContainerTools/kpt/internal/printer/fake"
"github.com/GoogleContainerTools/kpt/internal/util/render"
"github.com/google/go-cmp/cmp"
)

const (
simpleBucketBucket = `
apiVersion: storage.cnrm.cloud.google.com/v1beta1
kind: StorageBucket
metadata: # kpt-merge: config-control/blueprints-project-bucket
name: blueprints-project-bucket # kpt-set: ${project-id}-${name}
namespace: config-control # kpt-set: ${namespace}
annotations:
cnrm.cloud.google.com/force-destroy: "false"
cnrm.cloud.google.com/project-id: blueprints-project # kpt-set: ${project-id}
spec:
storageClass: standard # kpt-set: ${storage-class}
uniformBucketLevelAccess: true
versioning:
enabled: false
`

simpleBucketKptfile = `
apiVersion: kpt.dev/v1
kind: Kptfile
metadata:
name: simple-bucket
annotations:
blueprints.cloud.google.com/title: Google Cloud Storage Bucket blueprint
info:
description: A Google Cloud Storage bucket
pipeline:
mutators:
- image: gcr.io/kpt-fn/apply-setters:v0.2.0
configPath: setters.yaml
`

simpleBucketSetters = `
apiVersion: v1
kind: ConfigMap
metadata: # kpt-merge: /setters
name: setters
data:
name: updated-bucket-name
namespace: updated-namespace
project-id: updated-project-id
storage-class: updated-storage-class
`
)

func TestRenderWithFunctionConfigFile(t *testing.T) {
t.Skip("kpt renderer does not correctly construct function config")

fs := &memfs{}
if err := fs.MkdirAll("/simple-bucket"); err != nil {
t.Errorf("Failed MkdirAll: %v", err)
}
for k, v := range map[string]string{
"/simple-bucket/bucket.yaml": simpleBucketBucket,
"/simple-bucket/Kptfile": simpleBucketKptfile,
"/simple-bucket/setters.yaml": simpleBucketSetters,
} {
if err := fs.WriteFile(k, []byte(v)); err != nil {
t.Errorf("Failed creating file %q: %v", k, err)
}
}

r := render.Renderer{
PkgPath: "/simple-bucket",
Runner: &runner{},
FileSystem: fs,
}

if err := r.Execute(fake.CtxWithDefaultPrinter()); err != nil {
t.Errorf("Render failed: %v", err)
}

got, err := fs.ReadFile("/simple-bucket/bucket.yaml")
if err != nil {
t.Errorf("Cannot read \"/simple-bucket/bucket.yaml\": %v", err)
}

if diff := cmp.Diff(wantBucketBucket, string(got)); diff != "" {
t.Errorf("Unexpected result (-want, +got): %s", diff)
}
}

const (
inlineBucketBucket = `
apiVersion: storage.cnrm.cloud.google.com/v1beta1
kind: StorageBucket
metadata: # kpt-merge: config-control/blueprints-project-bucket
name: blueprints-project-bucket # kpt-set: ${project-id}-${name}
namespace: config-control # kpt-set: ${namespace}
annotations:
cnrm.cloud.google.com/force-destroy: "false"
cnrm.cloud.google.com/project-id: blueprints-project # kpt-set: ${project-id}
spec:
storageClass: standard # kpt-set: ${storage-class}
uniformBucketLevelAccess: true
versioning:
enabled: false
`

inlineBucketKptfile = `
apiVersion: kpt.dev/v1
kind: Kptfile
metadata:
name: simple-bucket
annotations:
blueprints.cloud.google.com/title: Google Cloud Storage Bucket blueprint
info:
description: A Google Cloud Storage bucket
pipeline:
mutators:
- image: gcr.io/kpt-fn/apply-setters:v0.2.0
configMap:
name: updated-bucket-name
namespace: updated-namespace
project-id: updated-project-id
storage-class: updated-storage-class
`

wantBucketBucket = `apiVersion: storage.cnrm.cloud.google.com/v1beta1
kind: StorageBucket
metadata: # kpt-merge: config-control/blueprints-project-bucket
name: updated-project-id-updated-bucket-name # kpt-set: ${project-id}-${name}
namespace: updated-namespace # kpt-set: ${namespace}
annotations:
cnrm.cloud.google.com/force-destroy: "false"
cnrm.cloud.google.com/project-id: updated-project-id # kpt-set: ${project-id}
cnrm.cloud.google.com/blueprint: 'kpt-fn'
spec:
storageClass: updated-storage-class # kpt-set: ${storage-class}
uniformBucketLevelAccess: true
versioning:
enabled: false
`
)

func TestRenderWithFunctionConfigInline(t *testing.T) {
fs := &memfs{}
if err := fs.MkdirAll("/inline-bucket"); err != nil {
t.Errorf("Failed MkdirAll: %v", err)
}
for k, v := range map[string]string{
"/inline-bucket/bucket.yaml": inlineBucketBucket,
"/inline-bucket/Kptfile": inlineBucketKptfile,
} {
if err := fs.WriteFile(k, []byte(v)); err != nil {
t.Errorf("Failed creating file %q: %v", k, err)
}
}

r := render.Renderer{
PkgPath: "/inline-bucket",
Runner: &runner{},
FileSystem: fs,
}

if err := r.Execute(fake.CtxWithDefaultPrinter()); err != nil {
t.Errorf("Render failed: %v", err)
}

got, err := fs.ReadFile("/inline-bucket/bucket.yaml")
if err != nil {
t.Errorf("Cannot read \"/inline-bucket/bucket.yaml\": %v", err)
}

if diff := cmp.Diff(wantBucketBucket, string(got)); diff != "" {
t.Errorf("Unexpected result (-want, +got): %s", diff)
}
}

0 comments on commit 08059cf

Please sign in to comment.