/
system.go
243 lines (206 loc) · 7.08 KB
/
system.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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
package cmd
import (
"context"
"fmt"
"io"
"strings"
"github.com/cloud-native-application/rudrx/pkg/oam"
"github.com/cloud-native-application/rudrx/pkg/builtin/traitdefinition"
"github.com/cloud-native-application/rudrx/pkg/builtin/workloaddefinition"
"github.com/ghodss/yaml"
"github.com/cloud-native-application/rudrx/api/types"
"k8s.io/apimachinery/pkg/runtime"
oamv1 "github.com/crossplane/oam-kubernetes-runtime/apis/core/v1alpha2"
"github.com/spf13/cobra"
"sigs.k8s.io/controller-runtime/pkg/client"
"github.com/pkg/errors"
kubeerrors "k8s.io/apimachinery/pkg/api/errors"
cmdutil "github.com/cloud-native-application/rudrx/pkg/cmd/util"
)
type initCmd struct {
namespace string
ioStreams cmdutil.IOStreams
client client.Client
version string
}
type infoCmd struct {
out io.Writer
}
var (
defaultObject = []interface{}{
&oamv1.WorkloadDefinition{},
&oamv1.ApplicationConfiguration{},
&oamv1.Component{},
&oamv1.TraitDefinition{},
&oamv1.ContainerizedWorkload{},
&oamv1.HealthScope{},
&oamv1.ManualScalerTrait{},
&oamv1.ScopeDefinition{},
}
workloadResource = map[string]string{
"deployments.apps": workloaddefinition.Deployment,
"containerizedworkloads.core.oam.dev": workloaddefinition.ContainerizedWorkload,
}
traitResource = map[string]string{
"manualscalertraits.core.oam.dev": traitdefinition.ManualScaler,
"simplerollouttraits.extend.oam.dev": traitdefinition.SimpleRollout,
}
)
func SystemCommandGroup(c types.Args, ioStream cmdutil.IOStreams) *cobra.Command {
cmd := &cobra.Command{
Use: "system",
Short: "system management utilities",
Long: "system management utilities",
Annotations: map[string]string{
types.TagCommandType: types.TypeSystem,
},
}
cmd.AddCommand(NewAdminInitCommand(c, ioStream), NewAdminInfoCommand(ioStream), NewRefreshCommand(c, ioStream))
return cmd
}
func NewAdminInfoCommand(ioStreams cmdutil.IOStreams) *cobra.Command {
i := &infoCmd{out: ioStreams.Out}
cmd := &cobra.Command{
Use: "info",
Short: "show vela client and cluster version",
Long: "show vela client and cluster version",
RunE: func(cmd *cobra.Command, args []string) error {
return i.run(ioStreams)
},
Annotations: map[string]string{
types.TagCommandType: types.TypeSystem,
},
}
return cmd
}
func (i *infoCmd) run(ioStreams cmdutil.IOStreams) error {
clusterVersion, err := GetOAMReleaseVersion()
if err != nil {
ioStreams.Errorf("fail to get cluster version, err: %v \n", err)
return err
}
ioStreams.Info("Versions:")
ioStreams.Infof("oam-kubernetes-runtime: %s \n", clusterVersion)
// TODO(wonderflow): we should print all helm charts installed by vela, including plugins
return nil
}
func NewAdminInitCommand(c types.Args, ioStreams cmdutil.IOStreams) *cobra.Command {
i := &initCmd{ioStreams: ioStreams}
cmd := &cobra.Command{
Use: "init",
Short: "Initialize vela on both client and server",
Long: "Install OAM runtime and vela builtin capabilities.",
RunE: func(cmd *cobra.Command, args []string) error {
newClient, err := client.New(c.Config, client.Options{Scheme: c.Schema})
if err != nil {
return err
}
i.client = newClient
i.namespace = types.DefaultOAMNS
return i.run(ioStreams)
},
Annotations: map[string]string{
types.TagCommandType: types.TypeSystem,
},
}
flag := cmd.Flags()
flag.StringVarP(&i.version, "version", "v", "", "Override chart version")
return cmd
}
func (i *initCmd) run(ioStreams cmdutil.IOStreams) error {
ioStreams.Info("- Installing OAM Kubernetes Runtime:")
if !cmdutil.IsNamespaceExist(i.client, types.DefaultOAMNS) {
if err := cmdutil.NewNamespace(i.client, types.DefaultOAMNS); err != nil {
return err
}
}
if i.IsOamRuntimeExist() {
i.ioStreams.Info("Vela system along with OAM runtime already exist.")
}
if err := InstallOamRuntime(ioStreams, i.version); err != nil {
return err
}
ioStreams.Info("- Installing builtin capabilities:")
if err := GenNativeResourceDefinition(i.client); err != nil {
return err
}
ioStreams.Info()
if err := RefreshDefinitions(context.Background(), i.client, ioStreams); err != nil {
return err
}
ioStreams.Info("- Finished.")
return nil
}
func (i *initCmd) IsOamRuntimeExist() bool {
for _, object := range defaultObject {
if err := cmdutil.IsCoreCRDExist(context.Background(), i.client, object.(runtime.Object)); err != nil {
return false
}
}
return oam.IsHelmReleaseRunning(types.DefaultOAMReleaseName, types.DefaultOAMRuntimeChartName, i.ioStreams)
}
func InstallOamRuntime(ioStreams cmdutil.IOStreams, version string) error {
return oam.HelmInstall(ioStreams, types.DefaultOAMRepoName, types.DefaultOAMRepoURL, types.DefaultOAMRuntimeChartName, version, types.DefaultOAMReleaseName, nil)
}
func GetOAMReleaseVersion() (string, error) {
results, err := oam.GetHelmRelease()
if err != nil {
return "", err
}
for _, result := range results {
if result.Chart.ChartFullPath() == types.DefaultOAMRuntimeChartName {
return result.Chart.AppVersion(), nil
}
}
return "", errors.New("oam-kubernetes-runtime not found in your kubernetes cluster, try `vela system init` to install")
}
func GenNativeResourceDefinition(c client.Client) error {
var capabilities []string
ctx := context.Background()
for name, manifest := range workloadResource {
wd := NewWorkloadDefinition(manifest)
capabilities = append(capabilities, name)
nwd := &oamv1.WorkloadDefinition{}
err := c.Get(ctx, client.ObjectKey{Name: name}, nwd)
if err != nil && kubeerrors.IsNotFound(err) {
if err := c.Create(context.Background(), &wd); err != nil {
return fmt.Errorf("create workload definition %s hit an issue: %v", name, err)
}
continue
}
wd.ResourceVersion = nwd.ResourceVersion
if err := c.Update(ctx, &wd); err != nil {
return fmt.Errorf("update workload definition %s err %v", wd.Name, err)
}
}
for name, manifest := range traitResource {
td := NewTraitDefinition(manifest)
capabilities = append(capabilities, name)
ntd := &oamv1.TraitDefinition{}
err := c.Get(context.Background(), client.ObjectKey{Name: name}, ntd)
if err != nil && kubeerrors.IsNotFound(err) {
if err := c.Create(context.Background(), &td); err != nil {
return fmt.Errorf("create trait definition %s hit an issue: %v", name, err)
}
continue
}
td.ResourceVersion = ntd.ResourceVersion
if err := c.Update(ctx, &td); err != nil {
return fmt.Errorf("update trait definition %s err %v", td.Name, err)
}
}
fmt.Printf("Successful applied %d kinds of Workloads and Traits: %s.", len(capabilities), strings.Join(capabilities, ","))
return nil
}
func NewWorkloadDefinition(manifest string) oamv1.WorkloadDefinition {
var workloadDefinition oamv1.WorkloadDefinition
// We have tests to make sure built-in resource can always unmarshal succeed
_ = yaml.Unmarshal([]byte(manifest), &workloadDefinition)
return workloadDefinition
}
func NewTraitDefinition(manifest string) oamv1.TraitDefinition {
var traitDefinition oamv1.TraitDefinition
// We have tests to make sure built-in resource can always unmarshal succeed
_ = yaml.Unmarshal([]byte(manifest), &traitDefinition)
return traitDefinition
}