forked from kptdev/kpt
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcmdrender.go
More file actions
148 lines (134 loc) · 4.75 KB
/
Copy pathcmdrender.go
File metadata and controls
148 lines (134 loc) · 4.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
// Copyright 2021,2026 The kpt Authors
//
// 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 cmdrender contains the render command
package render
import (
"bytes"
"context"
"fmt"
"io"
"os"
docs "github.com/kptdev/kpt/internal/docs/generated/fndocs"
"github.com/kptdev/kpt/internal/util/argutil"
"github.com/kptdev/kpt/internal/util/pathutil"
"github.com/kptdev/kpt/internal/util/render"
"github.com/kptdev/kpt/pkg/lib/runneroptions"
"github.com/kptdev/kpt/pkg/lib/util/cmdutil"
"github.com/kptdev/kpt/pkg/printer"
"github.com/spf13/cobra"
"sigs.k8s.io/kustomize/kyaml/filesys"
)
// NewRunner returns a command runner
func NewRunner(ctx context.Context, parent string) *Runner {
r := &Runner{ctx: ctx}
r.InitDefaults()
c := &cobra.Command{
Use: "render [PKG_PATH] [flags]",
Short: docs.RenderShort,
Long: docs.RenderShort + "\n" + docs.RenderLong,
Example: docs.RenderExamples,
RunE: r.runE,
PreRunE: r.preRunE,
}
c.Flags().StringVar(&r.resultsDirPath, "results-dir", "",
"path to a directory to save function results")
c.Flags().StringVarP(&r.dest, "output", "o", "",
fmt.Sprintf("output resources are written to provided location. Allowed values: %s|%s|<OUT_DIR_PATH>", cmdutil.Stdout, cmdutil.Unwrap))
c.Flags().Var(&r.RunnerOptions.ImagePullPolicy, "image-pull-policy",
"pull image before running the container "+r.RunnerOptions.ImagePullPolicy.HelpAllowedValues())
_ = c.RegisterFlagCompletionFunc("image-pull-policy", func(_ *cobra.Command, _ []string, _ string) ([]string, cobra.ShellCompDirective) {
return r.RunnerOptions.ImagePullPolicy.AllStrings(), cobra.ShellCompDirectiveDefault
})
c.Flags().BoolVar(&r.RunnerOptions.AllowExec, "allow-exec", r.RunnerOptions.AllowExec,
"allow binary executable to be run during pipeline execution.")
c.Flags().BoolVar(&r.RunnerOptions.AllowNetwork, "allow-network", false,
"allow functions to access network during pipeline execution.")
c.Flags().BoolVar(&r.RunnerOptions.AllowWasm, "allow-alpha-wasm", r.RunnerOptions.AllowWasm,
"allow wasm to be used during pipeline execution.")
cmdutil.FixDocs("kpt", parent, c)
r.Command = c
return r
}
func NewCommand(ctx context.Context, parent string) *cobra.Command {
return NewRunner(ctx, parent).Command
}
// Runner contains the run function pipeline run command
type Runner struct {
pkgPath string
resultsDirPath string
dest string
Command *cobra.Command
ctx context.Context
RunnerOptions runneroptions.RunnerOptions
}
func (r *Runner) InitDefaults() {
r.RunnerOptions.InitDefaults(runneroptions.GHCRImagePrefix)
// Initialize CEL environment for condition evaluation
// Ignore error as conditions are optional; if CEL init fails, conditions will error at runtime
_ = r.RunnerOptions.InitCELEnvironment()
}
func (r *Runner) preRunE(_ *cobra.Command, args []string) error {
if len(args) == 0 {
// no pkg path specified, default to current working dir
wd, err := os.Getwd()
if err != nil {
return err
}
r.pkgPath = wd
} else {
// resolve and validate the provided path
r.pkgPath = args[0]
}
var err error
r.pkgPath, err = argutil.ResolveSymlink(r.ctx, r.pkgPath)
if err != nil {
return err
}
if r.dest != "" && r.dest != cmdutil.Stdout && r.dest != cmdutil.Unwrap {
if err := cmdutil.CheckDirectoryNotPresent(r.dest); err != nil {
return err
}
}
if r.resultsDirPath != "" {
err := os.MkdirAll(r.resultsDirPath, 0755)
if err != nil {
return fmt.Errorf("cannot read or create results dir %q: %w", r.resultsDirPath, err)
}
}
return nil
}
func (r *Runner) runE(_ *cobra.Command, _ []string) error {
var output io.Writer
outContent := bytes.Buffer{}
if r.dest != "" {
// this means the output should be written to another destination
// capture the content to be written
output = &outContent
}
absPkgPath, _, err := pathutil.ResolveAbsAndRelPaths(r.pkgPath)
if err != nil {
return err
}
executor := render.Renderer{
PkgPath: absPkgPath,
ResultsDirPath: r.resultsDirPath,
Output: output,
RunnerOptions: r.RunnerOptions,
FileSystem: filesys.FileSystemOrOnDisk{},
}
if _, err := executor.Execute(r.ctx); err != nil {
return err
}
return cmdutil.WriteFnOutput(r.dest, outContent.String(), false, printer.FromContextOrDie(r.ctx).OutStream())
}