Debian Codegraph @

cmd/config/internal/commands/e2e/e2econtainerconfig/main.go

raw symbols SCIP
1 // Copyright 2019 The Kubernetes Authors.
2 // SPDX-License-Identifier: Apache-2.0
3
4 package main
5
6 import (
7 "fmt"
8 "os"
9 "strconv"
10
11 "sigs.k8s.io/kustomize/kyaml/fn/framework"
12 "sigs.k8s.io/kustomize/kyaml/fn/framework/command"
13 "sigs.k8s.io/kustomize/kyaml/kio"
14 "sigs.k8s.io/kustomize/kyaml/yaml"
15 )
16
17 // Config defines the ResourceList.functionConfig schema.
18 type Config struct {
19 // Data contains configuration data for the Example
20 // Nest values under Data so that the function can accept a ConfigMap as its
21 // functionConfig (`run` generates a ConfigMap for the functionConfig when run with --)
22 // e.g. `config run DIR/ --image my-image -- a-string-value=foo` will create the input
23 // with ResourceList.functionConfig.data.a-string-value=foo
24 Data map[string]string `yaml:"data,omitempty"`
25 }
26
27 func main() {
28 config := &Config{}
29
30 fn := func(items []*yaml.RNode) ([]*yaml.RNode, error) {
31 var newNodes []*yaml.RNode
32 for i := range items {
33 // set the annotation on each resource item
34 if err := items[i].PipeE(yaml.SetAnnotation("a-string-value", config.Data["stringValue"])); err != nil {
35 return nil, fmt.Errorf("%w", err)
36 }
37 intValue, err := strconv.Atoi(config.Data["intValue"])
38 if config.Data["intValue"] == "" {
39 // default value
40 intValue = 0
41 } else if err != nil {
42 return nil, fmt.Errorf("intValue convert error: %w", err)
43
44 }
45 if err := items[i].PipeE(yaml.SetAnnotation("a-int-value", strconv.Itoa(intValue))); err != nil {
46 return nil, fmt.Errorf("%w", err)
47 }
48 boolValue, err := strconv.ParseBool(config.Data["boolValue"])
49 if config.Data["boolValue"] == "" {
50 // default value
51 boolValue = false
52 } else if err != nil {
53 return nil, fmt.Errorf("boolValue convert error: %w", err)
54
55 }
56 if err := items[i].PipeE(yaml.SetAnnotation("a-bool-value", strconv.FormatBool(boolValue))); err != nil {
57 return nil, fmt.Errorf("%w", err)
58 }
59
60 newNodes = append(newNodes, items[i])
61 }
62 return newNodes, nil
63 }
64
65 p := framework.SimpleProcessor{Config: config, Filter: kio.FilterFunc(fn)}
66 cmd := command.Build(p, command.StandaloneDisabled, false)
67 command.AddGenerateDockerfile(cmd)
68 if err := cmd.Execute(); err != nil {
69 os.Exit(1)
70 }
71 }