mirror of
https://github.com/kubernetes-sigs/kustomize.git
synced 2026-07-01 02:11:20 +00:00
Replace pkger with embed.FS compatibility
This commit is contained in:
@@ -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)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user