-
Notifications
You must be signed in to change notification settings - Fork 226
/
applycmd.go
76 lines (69 loc) · 2.9 KB
/
applycmd.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
// Copyright 2020 The Kubernetes Authors.
// SPDX-License-Identifier: Apache-2.0
package commands
import (
"fmt"
"github.com/GoogleContainerTools/kpt/pkg/live"
"github.com/GoogleContainerTools/kpt/thirdparty/cli-utils/apply"
"github.com/spf13/cobra"
"k8s.io/cli-runtime/pkg/genericclioptions"
cmdutil "k8s.io/kubectl/pkg/cmd/util"
"sigs.k8s.io/cli-utils/pkg/manifestreader"
"sigs.k8s.io/cli-utils/pkg/provider"
)
// Get ApplyRunner returns a wrapper around the cli-utils apply command ApplyRunner. Sets
// up the Run on this wrapped runner to be the ApplyRunnerWrapper run.
func GetApplyRunner(provider provider.Provider, loader manifestreader.ManifestLoader, ioStreams genericclioptions.IOStreams) *ApplyRunnerWrapper {
applyRunner := apply.GetApplyRunner(provider, loader, ioStreams)
w := &ApplyRunnerWrapper{
applyRunner: applyRunner,
factory: provider.Factory(),
ioStreams: ioStreams,
installCRD: false,
}
cmd := applyRunner.Command
cmd.Flags().BoolVar(&w.installCRD, "install-resource-group", false,
"If true, install the inventory ResourceGroup CRD before applying.")
// Set the wrapper run to be the RunE function for the wrapped command.
applyRunner.Command.RunE = w.RunE
return w
}
// ApplyRunnerWrapper encapsulates the cli-utils apply command ApplyRunner as well
// as structures necessary to run.
type ApplyRunnerWrapper struct {
applyRunner *apply.ApplyRunner
factory cmdutil.Factory
ioStreams genericclioptions.IOStreams
installCRD bool // Install the ResourceGroup CRD before applying
}
// Command returns the wrapped ApplyRunner cobraCommand structure.
func (w *ApplyRunnerWrapper) Command() *cobra.Command {
return w.applyRunner.Command
}
// RunE delegates to the stored applyRunner. Before the delegation, this
// function either applies the inventory ResourceGroup CRD
// (--apply-inventory-crd flag), or checks if the inventory
// ResourceGroup CRD is available. Returns an error if one occurs in the
// delegation.
func (w *ApplyRunnerWrapper) RunE(cmd *cobra.Command, args []string) error {
// Install the inventory ResourceGroup CRD prior to applying if
// the flag/option is present. Otherwise, check if the CRD exists
// and report a failure.
if w.installCRD {
fmt.Fprint(w.ioStreams.Out, "installing inventory ResourceGroup CRD...")
err := live.InstallResourceGroupCRD(w.factory)
if err == nil {
fmt.Fprintln(w.ioStreams.Out, "success")
} else {
fmt.Fprintln(w.ioStreams.Out, "failed")
fmt.Fprintln(w.ioStreams.Out, "run 'kpt live install-resource-group' to try again")
return err
}
} else if !live.ResourceGroupCRDApplied(w.factory) {
// Otherwise, report the inventory ResourceGroup if missing.
fmt.Fprintln(w.ioStreams.Out, "inventory ResourceGroup CRD is missing")
fmt.Fprintln(w.ioStreams.Out, "run 'kpt live install-resource-group' to remedy")
// Do NOT return here, since it breaks legacy ConfigMap applies.
}
return w.applyRunner.RunE(cmd, args)
}