/
main.go
128 lines (119 loc) · 2.73 KB
/
main.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
package main
import (
"context"
"log"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
core_oam_dev "github.com/oam-dev/kubevela-core-api/apis/core.oam.dev"
"github.com/oam-dev/kubevela-core-api/apis/core.oam.dev/common"
core_v1beta1 "github.com/oam-dev/kubevela-core-api/apis/core.oam.dev/v1beta1"
"github.com/oam-dev/kubevela-core-api/pkg/oam/util"
)
var scheme = runtime.NewScheme()
func init() {
_ = core_oam_dev.AddToScheme(scheme)
}
func main() {
k8sClient, err := client.New(ctrl.GetConfigOrDie(), client.Options{Scheme: scheme})
if err != nil {
log.Fatal(err)
}
err = k8sClient.Create(context.Background(), &core_v1beta1.ComponentDefinition{
TypeMeta: metav1.TypeMeta{
Kind: "ComponentDefinition",
APIVersion: "core.oam.dev/v1beta1",
},
ObjectMeta: metav1.ObjectMeta{
Name: "test-comp",
Namespace: "vela-system",
},
Spec: core_v1beta1.ComponentDefinitionSpec{
Workload: common.WorkloadTypeDescriptor{
Definition: common.WorkloadGVK{
Kind: "Deployment",
APIVersion: "apps/v1",
},
},
Schematic: &common.Schematic{
CUE: &common.CUE{
Template: webServiceTemplate,
},
},
},
})
if err != nil {
log.Fatal(err)
}
err = k8sClient.Create(context.Background(), &core_v1beta1.Application{
TypeMeta: metav1.TypeMeta{
Kind: "Application",
APIVersion: "core.oam.dev/v1beta1",
},
ObjectMeta: metav1.ObjectMeta{
Name: "test-app",
Namespace: "default",
},
Spec: core_v1beta1.ApplicationSpec{
Components: []common.ApplicationComponent{
{
Name: "web",
Type: "webservice",
Properties: util.Object2RawExtension(map[string]interface{}{
"image": "nginx:1.14.0",
}),
Traits: []common.ApplicationTrait{
{
Type: "labels",
Properties: util.Object2RawExtension(map[string]interface{}{
"hello": "world",
}),
},
},
},
},
},
})
if err != nil {
log.Fatal(err)
}
}
var webServiceTemplate = `output: {
apiVersion: "apps/v1"
kind: "Deployment"
metadata: labels: {
"componentdefinition.oam.dev/version": "v1"
}
spec: {
selector: matchLabels: {
"app.oam.dev/component": context.name
}
template: {
metadata: labels: {
"app.oam.dev/component": context.name
}
spec: {
containers: [{
name: context.name
image: parameter.image
if parameter["cmd"] != _|_ {
command: parameter.cmd
}
if context["config"] != _|_ {
env: context.config
}
ports: [{
containerPort: parameter.port
}]
}]
}
}
}
}
parameter: {
image: string
cmd?: [...string]
port: *80 | int
}
`