/
exec.go
113 lines (98 loc) · 3.09 KB
/
exec.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
// Copyright Contributors to the Open Cluster Management project
package addons
import (
"fmt"
apiextensionsclient "k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset"
"k8s.io/client-go/dynamic"
"k8s.io/client-go/kubernetes"
clusteradmhelpers "open-cluster-management.io/clusteradm/pkg/helpers"
"github.com/spf13/cobra"
"github.com/stolostron/applier/pkg/apply"
attachscenario "github.com/stolostron/cm-cli/pkg/cmd/attach/cluster/scenario"
"github.com/stolostron/cm-cli/pkg/cmd/enable/addons/scenario"
"github.com/stolostron/cm-cli/pkg/helpers"
)
func (o *Options) complete(cmd *cobra.Command, args []string) (err error) {
//Check if default values must be used
if o.valuesPath == "" {
if len(args) > 0 {
o.clusterName = args[0]
}
if len(o.clusterName) == 0 {
return fmt.Errorf("values or name are missing")
}
reader := scenario.GetScenarioResourcesReader()
o.values, err = helpers.ConvertReaderFileToValuesMap(valuesDefaultPath, reader)
if err != nil {
return err
}
mc := o.values["managedCluster"].(map[string]interface{})
mc["name"] = o.clusterName
} else {
//Read values
o.values, err = helpers.ConvertValuesFileToValuesMap(o.valuesPath, "")
if err != nil {
return err
}
}
imc, ok := o.values["managedCluster"]
if !ok || imc == nil {
return fmt.Errorf("managedCluster is missing")
}
return nil
}
func (o *Options) validate() error {
kubeClient, err := o.CMFlags.KubectlFactory.KubernetesClientSet()
if err != nil {
return err
}
dynamicClient, err := o.CMFlags.KubectlFactory.DynamicClient()
if err != nil {
return err
}
return o.validateWithClient(kubeClient, dynamicClient)
}
func (o *Options) validateWithClient(kubeClient kubernetes.Interface, dynamicClient dynamic.Interface) error {
imc, ok := o.values["managedCluster"]
if !ok || imc == nil {
return fmt.Errorf("managedCluster is missing")
}
mc := imc.(map[string]interface{})
if o.clusterName == "" {
iname, ok := mc["name"]
if !ok || iname == nil {
return fmt.Errorf("cluster name is missing")
}
o.clusterName = iname.(string)
if len(o.clusterName) == 0 {
return fmt.Errorf("managedCluster.name not specified")
}
}
mc["name"] = o.clusterName
return nil
}
func (o *Options) run() (err error) {
kubeClient, apiExtensionsClient, dynamicClient, err := clusteradmhelpers.GetClients(o.CMFlags.KubectlFactory)
if err != nil {
return err
}
return o.runWithClient(kubeClient, apiExtensionsClient, dynamicClient)
}
func (o *Options) runWithClient(
kubeClient kubernetes.Interface,
apiExtensionsClient apiextensionsclient.Interface,
dynamicClient dynamic.Interface) (err error) {
output := make([]string, 0)
reader := attachscenario.GetScenarioResourcesReader()
files := []string{
"attach/hub/klusterlet_addon_config_cr.yaml",
}
applierBuilder := apply.NewApplierBuilder()
applier := applierBuilder.WithClient(kubeClient, apiExtensionsClient, dynamicClient).Build()
out, err := applier.ApplyCustomResources(reader, o.values, o.CMFlags.DryRun, "", files...)
if err != nil {
return err
}
output = append(output, out...)
return apply.WriteOutput(o.outputFile, output)
}