...

Source file src/sigs.k8s.io/kustomize/kyaml/fn/framework/example/main.go

Documentation: sigs.k8s.io/kustomize/kyaml/fn/framework/example

     1  // Copyright 2019 The Kubernetes Authors.
     2  // SPDX-License-Identifier: Apache-2.0
     3  
     4  package main
     5  
     6  import (
     7  	"embed"
     8  	"fmt"
     9  	"os"
    10  
    11  	"github.com/spf13/cobra"
    12  	"sigs.k8s.io/kustomize/kyaml/fn/framework"
    13  	"sigs.k8s.io/kustomize/kyaml/fn/framework/command"
    14  	"sigs.k8s.io/kustomize/kyaml/fn/framework/parser"
    15  )
    16  
    17  //go:embed templates/*
    18  var templateFS embed.FS
    19  
    20  var annotationTemplate = `
    21  metadata:
    22    annotations: 
    23      value: {{ .Value }}
    24  `
    25  
    26  func buildProcessor(value *string) framework.ResourceListProcessor {
    27  	return framework.TemplateProcessor{
    28  		ResourceTemplates: []framework.ResourceTemplate{{
    29  			Templates: parser.TemplateFiles("templates").FromFS(templateFS),
    30  		}},
    31  		PatchTemplates: []framework.PatchTemplate{&framework.ResourcePatchTemplate{
    32  			Templates: parser.TemplateStrings(annotationTemplate),
    33  		}},
    34  		// This will be populated from the --value flag if provided,
    35  		// or the config file's `value` field if provided, with the latter taking precedence.
    36  		TemplateData: &struct {
    37  			Value *string `yaml:"value"`
    38  		}{Value: value}}
    39  }
    40  
    41  func buildCmd() *cobra.Command {
    42  	var value string
    43  	cmd := command.Build(buildProcessor(&value), command.StandaloneEnabled, false)
    44  	cmd.Flags().StringVar(&value, "value", "", "annotation value")
    45  	return cmd
    46  }
    47  
    48  func main() {
    49  	if err := buildCmd().Execute(); err != nil {
    50  		fmt.Println(err)
    51  		os.Exit(1)
    52  	}
    53  }
    54  

View as plain text