GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — func/cmd/environment.go at main · stackbuilders/func

Expand file tree

/

environment.go

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
223 lines (193 loc) · 5.7 KB
/

environment.go

Copy path

File metadata and controls

223 lines (193 loc) · 5.7 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
package cmd
import (
"context"
"encoding/json"
"fmt"
"os"
"strings"
"github.com/ory/viper"
"github.com/spf13/cobra"
"gopkg.in/yaml.v2"
"knative.dev/func/pkg/buildpacks"
"knative.dev/func/pkg/config"
"knative.dev/func/pkg/functions"
"knative.dev/func/pkg/k8s"
"knative.dev/func/pkg/pipelines/tekton"
"knative.dev/func/pkg/s2i"
)
var format string = "json"
func NewEnvironmentCmd(newClient ClientFactory, version *Version) *cobra.Command {
cmd := &cobra.Command{
Use: "environment",
Aliases: []string{"env"},
Short: "Display function execution environment information",
Long: `
NAME
{{rootCmdUse}} environment (env) - display function execution environment information
SYNOPSIS
{{rootCmdUse}} environment [-f|--format] [-v|--verbose] [-p|--path]
DESCRIPTION
Display information about the function execution environment, including
the version of func, the version of the function spec, the default builder,
available runtimes, and available templates.
`,
PreRunE: bindEnv("verbose", "format", "path"),
RunE: func(cmd *cobra.Command, args []string) error {
return runEnvironment(cmd, newClient, version)
},
}
cfg, err := config.NewDefault()
if err != nil {
fmt.Fprintf(cmd.OutOrStdout(), "error loading config at '%v'. %v\n", config.File(), err)
}
cmd.Flags().StringP("format", "f", format, "Format of output environment information, 'json' or 'yaml'. ($FUNC_FORMAT)")
addPathFlag(cmd)
addVerboseFlag(cmd, cfg.Verbose)
return cmd
}
type Environment struct {
Version string
GitRevision string
SpecVersion string
SocatImage string
TarImage string
FuncUtilsImage string
DeployerImage string
ScaffoldImage string
S2IImage string
Languages []string
DefaultImageBuilders map[string]map[string]string
Templates map[string][]string
Environment []string
Cluster string
Defaults config.Global
Function *functions.Function `json:",omitempty" yaml:",omitempty"`
Instance *functions.Instance `json:",omitempty" yaml:",omitempty"`
}
func runEnvironment(cmd *cobra.Command, newClient ClientFactory, v *Version) (err error) {
cfg, err := newEnvironmentConfig()
if err != nil {
return
}
// Create a client to get runtimes and templates
client := functions.New(functions.WithVerbose(cfg.Verbose))
r, err := getRuntimes(client)
if err != nil {
return
}
t, err := getTemplates(client, r)
if err != nil {
return
}
// Get all environment variables that start with FUNC_
var envs []string
for _, e := range os.Environ() {
if strings.HasPrefix(e, "FUNC_") {
envs = append(envs, e)
}
}
// If no environment variables are set, make sure we return an empty array
// otherwise the output is "null" instead of "[]"
if len(envs) == 0 {
envs = make([]string, 0)
}
// Get global defaults
defaults, err := config.NewDefault()
if err != nil {
return
}
// Gets the cluster host
var host string
cc, err := k8s.GetClientConfig().ClientConfig()
if err != nil {
fmt.Printf("error getting client config %v\n", err)
} else {
host = cc.Host
}
//Get default image builders
builderimagesdefault := make(map[string]map[string]string)
builderimagesdefault["s2i"] = s2i.DefaultBuilderImages
builderimagesdefault["buildpacks"] = buildpacks.DefaultBuilderImages
environment := Environment{
Version: v.String(),
GitRevision: v.Hash,
SpecVersion: functions.LastSpecVersion(),
SocatImage: k8s.SocatImage,
TarImage: k8s.TarImage,
FuncUtilsImage: tekton.FuncUtilImage,
DeployerImage: tekton.DeployerImage,
ScaffoldImage: tekton.ScaffoldImage,
S2IImage: tekton.S2IImage,
Languages: r,
DefaultImageBuilders: builderimagesdefault,
Templates: t,
Environment: envs,
Cluster: host,
Defaults: defaults,
}
function, instance := describeFuncInformation(cmd.Context(), newClient, cfg)
if function != nil {
environment.Function = function
}
if instance != nil {
environment.Instance = instance
}
var s []byte
switch cfg.Format {
case "json":
s, err = json.MarshalIndent(environment, "", " ")
case "yaml":
s, err = yaml.Marshal(&environment)
default:
err = fmt.Errorf("unsupported format: %s", cfg.Format)
}
if err != nil {
return err
}
fmt.Fprintln(cmd.OutOrStdout(), string(s))
return nil
}
func getRuntimes(client *functions.Client) ([]string, error) {
runtimes, err := client.Runtimes()
if err != nil {
return nil, err
}
return runtimes, nil
}
func getTemplates(client *functions.Client, runtimes []string) (map[string][]string, error) {
templateMap := make(map[string][]string)
for _, runtime := range runtimes {
templates, err := client.Templates().List(runtime)
if err != nil {
return nil, err
}
templateMap[runtime] = templates
}
return templateMap, nil
}
func describeFuncInformation(context context.Context, newClient ClientFactory, cfg environmentConfig) (*functions.Function, *functions.Instance) {
function, err := functions.NewFunction(cfg.Path)
if err != nil || !function.Initialized() {
return nil, nil
}
client, done := newClient(ClientConfig{Verbose: cfg.Verbose})
defer done()
instance, err := client.Describe(context, function.Name, function.Deploy.Namespace, function)
if err != nil {
return &function, nil
}
return &function, &instance
}
type environmentConfig struct {
Verbose bool
Format string
Path string
}
func newEnvironmentConfig() (cfg environmentConfig, err error) {
cfg = environmentConfig{
Verbose: viper.GetBool("verbose"),
Format: viper.GetString("format"),
Path: viper.GetString("path"),
}
return
}
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.