forked from istio/istio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
manifest-generate.go
200 lines (180 loc) · 7.21 KB
/
manifest-generate.go
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
// Copyright Istio 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 mesh
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
"github.com/spf13/cobra"
"istio.io/istio/operator/pkg/helm"
"istio.io/istio/operator/pkg/helmreconciler"
"istio.io/istio/operator/pkg/manifest"
"istio.io/istio/operator/pkg/name"
"istio.io/istio/operator/pkg/object"
"istio.io/istio/operator/pkg/util/clog"
"istio.io/pkg/log"
)
type manifestGenerateArgs struct {
// inFilenames is an array of paths to the input IstioOperator CR files.
inFilename []string
// outFilename is the path to the generated output directory.
outFilename string
// set is a string with element format "path=value" where path is an IstioOperator path and the value is a
// value to set the node at that path to.
set []string
// force proceeds even if there are validation errors
force bool
// manifestsPath is a path to a charts and profiles directory in the local filesystem, or URL with a release tgz.
manifestsPath string
// revision is the Istio control plane revision the command targets.
revision string
// components is a list of strings specifying which component's manifests to be generated.
components []string
}
func addManifestGenerateFlags(cmd *cobra.Command, args *manifestGenerateArgs) {
cmd.PersistentFlags().StringSliceVarP(&args.inFilename, "filename", "f", nil, filenameFlagHelpStr)
cmd.PersistentFlags().StringVarP(&args.outFilename, "output", "o", "", "Manifest output directory path.")
cmd.PersistentFlags().StringArrayVarP(&args.set, "set", "s", nil, setFlagHelpStr)
cmd.PersistentFlags().BoolVar(&args.force, "force", false, ForceFlagHelpStr)
cmd.PersistentFlags().StringVarP(&args.manifestsPath, "charts", "", "", ChartsDeprecatedStr)
cmd.PersistentFlags().StringVarP(&args.manifestsPath, "manifests", "d", "", ManifestsFlagHelpStr)
cmd.PersistentFlags().StringVarP(&args.revision, "revision", "r", "", revisionFlagHelpStr)
cmd.PersistentFlags().StringSliceVar(&args.components, "component", nil, ComponentFlagHelpStr)
}
func manifestGenerateCmd(rootArgs *rootArgs, mgArgs *manifestGenerateArgs, logOpts *log.Options) *cobra.Command {
return &cobra.Command{
Use: "generate",
Short: "Generates an Istio install manifest",
Long: "The generate subcommand generates an Istio install manifest and outputs to the console by default.",
// nolint: lll
Example: ` # Generate a default Istio installation
istioctl manifest generate
# Enable Tracing
istioctl manifest generate --set meshConfig.enableTracing=true
# Generate the demo profile
istioctl manifest generate --set profile=demo
# To override a setting that includes dots, escape them with a backslash (\). Your shell may require enclosing quotes.
istioctl manifest generate --set "values.sidecarInjectorWebhook.injectedAnnotations.container\.apparmor\.security\.beta\.kubernetes\.io/istio-proxy=runtime/default"
`,
Args: func(cmd *cobra.Command, args []string) error {
if len(args) != 0 {
return fmt.Errorf("generate accepts no positional arguments, got %#v", args)
}
return nil
},
RunE: func(cmd *cobra.Command, args []string) error {
l := clog.NewConsoleLogger(cmd.OutOrStdout(), cmd.ErrOrStderr(), installerScope)
return manifestGenerate(rootArgs, mgArgs, logOpts, l)
},
}
}
func manifestGenerate(args *rootArgs, mgArgs *manifestGenerateArgs, logopts *log.Options, l clog.Logger) error {
if err := configLogs(logopts); err != nil {
return fmt.Errorf("could not configure logs: %s", err)
}
manifests, _, err := manifest.GenManifests(mgArgs.inFilename, applyFlagAliases(mgArgs.set, mgArgs.manifestsPath, mgArgs.revision), mgArgs.force, nil, l)
if err != nil {
return err
}
if len(mgArgs.components) != 0 {
filteredManifests := name.ManifestMap{}
for _, cArg := range mgArgs.components {
componentName := name.ComponentName(cArg)
if cManifests, ok := manifests[componentName]; ok {
filteredManifests[componentName] = cManifests
} else {
return fmt.Errorf("incorrect component name: %s. Valid options: %v", cArg, name.AllComponentNames)
}
}
manifests = filteredManifests
}
if mgArgs.outFilename == "" {
ordered, err := orderedManifests(manifests)
if err != nil {
return fmt.Errorf("failed to order manifests: %v", err)
}
for _, m := range ordered {
l.Print(m + object.YAMLSeparator)
}
} else {
if err := os.MkdirAll(mgArgs.outFilename, os.ModePerm); err != nil {
return err
}
if err := RenderToDir(manifests, mgArgs.outFilename, args.dryRun, l); err != nil {
return err
}
}
return nil
}
// orderedManifests generates a list of manifests from the given map sorted by the default object order
// This allows
func orderedManifests(mm name.ManifestMap) ([]string, error) {
var rawOutput []string
var output []string
for _, mfs := range mm {
rawOutput = append(rawOutput, mfs...)
}
objects, err := object.ParseK8sObjectsFromYAMLManifest(strings.Join(rawOutput, helm.YAMLSeparator))
if err != nil {
return nil, err
}
// For a given group of objects, sort in order to avoid missing dependencies, such as creating CRDs first
objects.Sort(object.DefaultObjectOrder())
for _, obj := range objects {
yml, err := obj.YAML()
if err != nil {
return nil, err
}
output = append(output, string(yml))
}
return output, nil
}
// RenderToDir writes manifests to a local filesystem directory tree.
func RenderToDir(manifests name.ManifestMap, outputDir string, dryRun bool, l clog.Logger) error {
l.LogAndPrintf("Component dependencies tree: \n%s", helmreconciler.InstallTreeString())
l.LogAndPrintf("Rendering manifests to output dir %s", outputDir)
return renderRecursive(manifests, helmreconciler.InstallTree, outputDir, dryRun, l)
}
func renderRecursive(manifests name.ManifestMap, installTree helmreconciler.ComponentTree, outputDir string, dryRun bool, l clog.Logger) error {
for k, v := range installTree {
componentName := string(k)
// In cases (like gateways) where multiple instances can exist, concatenate the manifests and apply as one.
ym := strings.Join(manifests[k], helm.YAMLSeparator)
l.LogAndPrintf("Rendering: %s", componentName)
dirName := filepath.Join(outputDir, componentName)
if !dryRun {
if err := os.MkdirAll(dirName, os.ModePerm); err != nil {
return fmt.Errorf("could not create directory %s; %s", outputDir, err)
}
}
fname := filepath.Join(dirName, componentName) + ".yaml"
l.LogAndPrintf("Writing manifest to %s", fname)
if !dryRun {
if err := ioutil.WriteFile(fname, []byte(ym), 0644); err != nil {
return fmt.Errorf("could not write manifest config; %s", err)
}
}
kt, ok := v.(helmreconciler.ComponentTree)
if !ok {
// Leaf
return nil
}
if err := renderRecursive(manifests, kt, dirName, dryRun, l); err != nil {
return err
}
}
return nil
}