This repository was archived by the owner on Oct 7, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 80
/
Copy pathmanifest-apply.go
106 lines (95 loc) · 4.18 KB
/
manifest-apply.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
// Copyright 2019 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"
"os"
"strings"
"time"
"github.com/spf13/cobra"
)
type manifestApplyArgs struct {
// inFilename is the path to the input IstioOperator CR.
inFilename string
// kubeConfigPath is the path to kube config file.
kubeConfigPath string
// context is the cluster context in the kube config
context string
// readinessTimeout is maximum time to wait for all Istio resources to be ready.
readinessTimeout time.Duration
// wait is flag that indicates whether to wait resources ready before exiting.
wait bool
// skipConfirmation determines whether the user is prompted for confirmation.
// If set to true, the user is not prompted and a Yes response is assumed in all cases.
skipConfirmation bool
// force proceeds even if there are validation errors
force bool
// 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
}
func addManifestApplyFlags(cmd *cobra.Command, args *manifestApplyArgs) {
cmd.PersistentFlags().StringVarP(&args.inFilename, "filename", "f", "", filenameFlagHelpStr)
cmd.PersistentFlags().StringVarP(&args.kubeConfigPath, "kubeconfig", "c", "", "Path to kube config")
cmd.PersistentFlags().StringVar(&args.context, "context", "", "The name of the kubeconfig context to use")
cmd.PersistentFlags().BoolVar(&args.skipConfirmation, "skip-confirmation", false, skipConfirmationFlagHelpStr)
cmd.PersistentFlags().BoolVar(&args.force, "force", false, "Proceed even with validation errors")
cmd.PersistentFlags().DurationVar(&args.readinessTimeout, "readiness-timeout", 300*time.Second, "Maximum seconds to wait for all Istio resources to be ready."+
" The --wait flag must be set for this flag to apply")
cmd.PersistentFlags().BoolVarP(&args.wait, "wait", "w", false, "Wait, if set will wait until all Pods, Services, and minimum number of Pods "+
"of a Deployment are in a ready state before the command exits. It will wait for a maximum duration of --readiness-timeout seconds")
cmd.PersistentFlags().StringSliceVarP(&args.set, "set", "s", nil, SetFlagHelpStr)
}
func manifestApplyCmd(rootArgs *rootArgs, maArgs *manifestApplyArgs) *cobra.Command {
return &cobra.Command{
Use: "apply",
Short: "Generates and applies an Istio install manifest.",
Long: "The apply subcommand generates an Istio install manifest and applies it to a cluster.",
Args: cobra.ExactArgs(0),
RunE: func(cmd *cobra.Command, args []string) error {
l := NewLogger(rootArgs.logToStdErr, cmd.OutOrStdout(), cmd.ErrOrStderr())
// Warn users before starting to install Istio
if !rootArgs.dryRun && !maArgs.skipConfirmation {
if !confirm("This will install Istio into the cluster. Proceed? (y/N)", cmd.OutOrStdout()) {
cmd.Print("Cancelled.\n")
os.Exit(1)
}
}
return manifestApply(rootArgs, maArgs, l)
}}
}
func manifestApply(args *rootArgs, maArgs *manifestApplyArgs, l *Logger) error {
if err := configLogs(args.logToStdErr); err != nil {
return fmt.Errorf("could not configure logs: %s", err)
}
if err := genApplyManifests(maArgs.set, maArgs.inFilename, maArgs.force, args.dryRun, args.verbose,
maArgs.kubeConfigPath, maArgs.context, maArgs.wait, maArgs.readinessTimeout, l); err != nil {
return fmt.Errorf("failed to generate and apply manifests, error: %v", err)
}
return nil
}
func confirm(msg string, writer io.Writer) bool {
fmt.Fprintf(writer, "%s ", msg)
var response string
_, err := fmt.Scanln(&response)
if err != nil {
return false
}
response = strings.ToUpper(response)
if response == "Y" || response == "YES" {
return true
}
return false
}