mirror of
https://github.com/kubernetes-sigs/kustomize.git
synced 2026-06-30 01:46:23 +00:00
Replace pkger with embed.FS compatibility
This commit is contained in:
@@ -2,11 +2,16 @@
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
// Package main contains an example using the the framework.
|
||||
// The example annotates all resources in the input with the value provided as a flag.
|
||||
// The example annotates all resources in the input with the value provided as a flag,
|
||||
// and adds all resources in the templates/ directory to the list.
|
||||
//
|
||||
// To execute the function, run:
|
||||
//
|
||||
// $ cat input/cm.yaml | go run ./main.go --value=foo
|
||||
// $ cat testdata/basic/input.yaml | go run ./main.go --value=foo
|
||||
//
|
||||
// Alternatively, you can provide the value via a config file instead of a flag:
|
||||
//
|
||||
// $ go run ./main.go testdata/basic/config.yaml testdata/basic/input.yaml
|
||||
//
|
||||
// To generate the Dockerfile for the function image run:
|
||||
//
|
||||
|
||||
@@ -4,29 +4,49 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"embed"
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
"sigs.k8s.io/kustomize/kyaml/fn/framework"
|
||||
"sigs.k8s.io/kustomize/kyaml/fn/framework/command"
|
||||
"sigs.k8s.io/kustomize/kyaml/yaml"
|
||||
"sigs.k8s.io/kustomize/kyaml/fn/framework/parser"
|
||||
)
|
||||
|
||||
func main() {
|
||||
var value string
|
||||
fn := func(rl *framework.ResourceList) error {
|
||||
for i := range rl.Items {
|
||||
// set the annotation on each resource item
|
||||
if err := rl.Items[i].PipeE(yaml.SetAnnotation("value", value)); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
cmd := command.Build(framework.ResourceListProcessorFunc(fn), command.StandaloneEnabled, false)
|
||||
cmd.Flags().StringVar(&value, "value", "", "annotation value")
|
||||
//go:embed templates/*
|
||||
var templateFS embed.FS
|
||||
|
||||
if err := cmd.Execute(); err != nil {
|
||||
var annotationTemplate = `
|
||||
metadata:
|
||||
annotations:
|
||||
value: {{ .Value }}
|
||||
`
|
||||
|
||||
func buildProcessor(value *string) framework.ResourceListProcessor {
|
||||
return framework.TemplateProcessor{
|
||||
ResourceTemplates: []framework.ResourceTemplate{{
|
||||
Templates: parser.TemplateFiles("templates").FromFS(templateFS),
|
||||
}},
|
||||
PatchTemplates: []framework.PatchTemplate{&framework.ResourcePatchTemplate{
|
||||
Templates: parser.TemplateStrings(annotationTemplate),
|
||||
}},
|
||||
// This will be populated from the --value flag if provided,
|
||||
// or the config file's `value` field if provided, with the latter taking precedence.
|
||||
TemplateData: struct {
|
||||
Value *string `yaml:"value"`
|
||||
}{Value: value}}
|
||||
}
|
||||
|
||||
func buildCmd() *cobra.Command {
|
||||
var value string
|
||||
cmd := command.Build(buildProcessor(&value), command.StandaloneEnabled, false)
|
||||
cmd.Flags().StringVar(&value, "value", "", "annotation value")
|
||||
return cmd
|
||||
}
|
||||
|
||||
func main() {
|
||||
if err := buildCmd().Execute(); err != nil {
|
||||
fmt.Println(err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
17
kyaml/fn/framework/example/main_test.go
Normal file
17
kyaml/fn/framework/example/main_test.go
Normal file
@@ -0,0 +1,17 @@
|
||||
// Copyright 2021 The Kubernetes Authors.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"sigs.k8s.io/kustomize/kyaml/fn/framework/frameworktestutil"
|
||||
)
|
||||
|
||||
func TestRun(t *testing.T) {
|
||||
prc := frameworktestutil.CommandResultsChecker{
|
||||
Command: buildCmd,
|
||||
}
|
||||
prc.Assert(t)
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
# Copyright 2021 The Kubernetes Authors.
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
apiVersion: v1
|
||||
kind: ServiceAccount
|
||||
metadata:
|
||||
name: {{ .Value }}
|
||||
6
kyaml/fn/framework/example/testdata/basic/config.yaml
vendored
Normal file
6
kyaml/fn/framework/example/testdata/basic/config.yaml
vendored
Normal file
@@ -0,0 +1,6 @@
|
||||
# Copyright 2019 The Kubernetes Authors.
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
apiVersion: example.com/v1alpha1
|
||||
kind: Tester
|
||||
value: foo
|
||||
21
kyaml/fn/framework/example/testdata/basic/expected.yaml
vendored
Normal file
21
kyaml/fn/framework/example/testdata/basic/expected.yaml
vendored
Normal file
@@ -0,0 +1,21 @@
|
||||
# Copyright 2021 The Kubernetes Authors.
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
kind: ConfigMap
|
||||
apiVersion: v1
|
||||
metadata:
|
||||
name: tester
|
||||
annotations:
|
||||
value: foo
|
||||
data:
|
||||
some: data
|
||||
---
|
||||
# Copyright 2021 The Kubernetes Authors.
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
apiVersion: v1
|
||||
kind: ServiceAccount
|
||||
metadata:
|
||||
name: foo
|
||||
annotations:
|
||||
value: foo
|
||||
Reference in New Issue
Block a user