-
Notifications
You must be signed in to change notification settings - Fork 13
/
exec.go
142 lines (127 loc) · 3.77 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
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
// Copyright Contributors to the Open Cluster Management project
package acm
import (
"context"
"fmt"
"time"
apiextensionsclient "k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset"
"k8s.io/client-go/dynamic"
"k8s.io/client-go/rest"
"github.com/spf13/cobra"
"github.com/stolostron/applier/pkg/apply"
"github.com/stolostron/cm-cli/pkg/cmd/install/acm/scenario"
"github.com/stolostron/cm-cli/pkg/helpers"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/wait"
)
func (o *Options) complete(cmd *cobra.Command, args []string) (err error) {
return nil
}
func (o *Options) validate() error {
return nil
}
func (o *Options) run() (err error) {
restConfig, err := o.CMFlags.KubectlFactory.ToRESTConfig()
if err != nil {
return err
}
dynamicClient, err := o.CMFlags.KubectlFactory.DynamicClient()
if err != nil {
return err
}
apiextensionsClient, err := apiextensionsclient.NewForConfig(restConfig)
if err != nil {
return err
}
return o.runWithClient(restConfig, apiextensionsClient, dynamicClient)
}
func (o *Options) runWithClient(restConfig *rest.Config,
apiextensionsClient apiextensionsclient.Interface,
dynamicClient dynamic.Interface) (err error) {
_, err = dynamicClient.Resource(helpers.GvrMCH).Namespace(o.namespace).Get(context.TODO(), "multiclusterhub", metav1.GetOptions{})
if err == nil {
return errors.NewUnauthorized("acm already installed")
}
output := make([]string, 0)
reader := scenario.GetScenarioResourcesReader()
files := []string{
"install/namespace.yaml",
}
approval := "Automatic"
if o.manualApproval {
approval = "Manual"
}
values := struct {
Channel string
Namespace string
OperatorGroup string
Approval string
}{
Channel: o.channel,
Namespace: o.namespace,
OperatorGroup: o.operatorGroup,
Approval: approval,
}
applierBuilder := apply.NewApplierBuilder()
applier := applierBuilder.WithRestConfig(restConfig).Build()
out, err := applier.ApplyDirectly(reader, values, o.CMFlags.DryRun, "", files...)
if err != nil {
return err
}
output = append(output, out...)
files = []string{
"install/operator-group.yaml",
"install/subscription.yaml",
}
out, err = applier.ApplyCustomResources(reader, values, o.CMFlags.DryRun, "", files...)
if err != nil {
return err
}
output = append(output, out...)
//Wait MCH CRD to be created
if !o.CMFlags.DryRun {
err = wait.PollImmediate(10*time.Second, time.Duration(3)*time.Minute, func() (bool, error) {
_, err := apiextensionsClient.ApiextensionsV1().CustomResourceDefinitions().Get(context.TODO(), "multiclusterhubs.operator.open-cluster-management.io", metav1.GetOptions{})
if err != nil {
fmt.Printf("%s, waiting...\n", err)
return false, nil
}
return true, nil
})
if err != nil {
return err
}
}
files = []string{
"install/multiclusterhub.yaml",
}
out, err = applier.ApplyCustomResources(reader, values, o.CMFlags.DryRun, "", files...)
if err != nil {
return err
}
output = append(output, out...)
if o.wait {
i := 0
wait.PollImmediate(1*time.Minute, time.Duration(o.timeout)*time.Minute, func() (bool, error) {
mchu, err := dynamicClient.Resource(helpers.GvrMCH).Namespace(o.namespace).Get(context.TODO(), "multiclusterhub", metav1.GetOptions{})
if err != nil {
fmt.Printf("%s, waiting...\n", err)
return false, nil
}
i += 1
if statusu, ok := mchu.Object["status"]; ok {
status := statusu.(map[string]interface{})
if phaseu, ok := status["phase"]; ok {
phase := phaseu.(string)
if phase != "Running" {
fmt.Printf("(%d/%d), phase is %s, waiting for Running", i, o.timeout, phase)
return false, nil
}
}
}
return true, nil
})
}
return apply.WriteOutput(o.outputFile, output)
}