-
Notifications
You must be signed in to change notification settings - Fork 1
/
application.go
210 lines (171 loc) · 4.95 KB
/
application.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
package application
import (
"context"
"encoding/json"
"fmt"
"github.com/khulnasoft/meshkit/models/oam/core/v1alpha1"
meshkube "github.com/khulnasoft/meshkit/utils/kubernetes"
"github.com/sirupsen/logrus"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
)
type serviceMesh string
type rolloutEngine string
const (
argo rolloutEngine = "argo"
)
type PatternSetting struct {
Replicas int `json:"replicas,omitempty"`
Mesh serviceMesh `json:"mesh,omitempty"`
Containers []RolloutEngineContainer `json:"containers,omitempty"`
Advanced PatternSettingAdvanced `json:"advanced,omitempty"`
}
type PatternSettingAdvanced struct {
CreateService *bool `json:"create_service,omitempty"`
}
type PatternConfiguration struct {
RolloutStrategy *PatternConfigurationRolloutStrategy `json:"rollout_strategy,omitempty"`
}
type PatternConfigurationRolloutStrategy struct {
}
// Deploy will deploy the application
func Deploy(
kubeClient *meshkube.Client,
oamComp v1alpha1.Component,
oamConfig v1alpha1.Configuration,
isDel bool,
) error {
if oamComp.Spec.Type == "Application" {
settings, err := getApplicationPatternSettings(oamComp)
if err != nil {
return err
}
config, err := getApplicationPatternConfiguration(oamComp.Name, oamConfig)
if err != nil {
return nil
}
mesh := settings.Mesh
if mesh == "" {
mesh = detectServiceMesh(kubeClient)
}
var engineName rolloutEngine
detectedEngine := detectRolloutEngine(kubeClient)
if detectedEngine == "" {
engineName = argo
} else {
engineName = detectedEngine
}
engine, err := NewRolloutEngine(kubeClient, string(engineName))
if err != nil {
return err
}
// If no engine was detected then proceed to install the default one
if detectedEngine == "" {
if err := engine.Install(); err != nil {
return err
}
}
setupDefaults(&settings)
if config.RolloutStrategy == nil {
return engine.Native(RolloutEngineGenericOptions{
Name: oamComp.Name,
Namespace: oamComp.Namespace,
ServiceMesh: string(mesh),
Replicas: settings.Replicas,
Containers: settings.Containers,
Metadata: RolloutEngineGenericOptionsMetadata{
Labels: oamComp.ObjectMeta.Labels,
Annotations: oamComp.ObjectMeta.Annotations,
},
Delete: isDel,
Advanced: settings.Advanced,
})
}
return fmt.Errorf("strategy not supported")
}
return fmt.Errorf("%s is not an application pattern", oamComp.Spec.Type)
}
func getApplicationPatternSettings(oamComp v1alpha1.Component) (PatternSetting, error) {
var settings PatternSetting
jsonByt, err := json.Marshal(oamComp.Spec.Settings)
if err != nil {
return PatternSetting{}, err
}
if err := json.Unmarshal(jsonByt, &settings); err != nil {
return settings, err
}
return settings, nil
}
func getApplicationPatternConfiguration(compName string, oamConfig v1alpha1.Configuration) (PatternConfiguration, error) {
var config PatternConfiguration
for _, cfg := range oamConfig.Spec.Components {
if cfg.ComponentName == compName {
// Find the "RolloutStrategy" trait
for _, trait := range cfg.Traits {
if trait.Name == "RolloutStrategy" {
jsonByt, err := json.Marshal(trait.Properties)
if err != nil {
return config, err
}
if err := json.Unmarshal(jsonByt, &config); err != nil {
return config, err
}
break
}
}
}
}
return config, nil
}
// detectServiceMesh will detect available service mesh in the k8s cluster
func detectServiceMesh(_ *meshkube.Client) serviceMesh {
return ""
}
// detectRolloutEngine will detect available rollout engine in the k8s cluster
func detectRolloutEngine(kubeClient *meshkube.Client) rolloutEngine {
if detectArgoRollout(kubeClient) {
return argo
}
return ""
}
// detectArgoRollout returns true if argo rollout is present in the cluster
func detectArgoRollout(kubeClient *meshkube.Client) bool {
deployments := []string{"argo-rollouts"}
nsList := findNamespaces(kubeClient)
for _, deployment := range deployments {
for _, ns := range nsList {
if _, err := kubeClient.
DynamicKubeClient.
Resource(schema.GroupVersionResource{
Group: "apps",
Version: "v1",
Resource: "deployments",
}).
Namespace(ns).
Get(context.TODO(), deployment, v1.GetOptions{}); err == nil {
logrus.Debug("found argo rollout in the cluster")
return true
}
logrus.Debugf("%s not found in namespace %s", deployment, ns)
}
}
return false
}
func findNamespaces(kubeClient *meshkube.Client) []string {
list, err := kubeClient.KubeClient.CoreV1().Namespaces().List(context.TODO(), v1.ListOptions{})
if err != nil {
return []string{}
}
res := []string{}
for _, ns := range list.Items {
res = append(res, ns.Name)
}
return res
}
func setupDefaults(s *PatternSetting) {
// If nothing is specified then set the value to true
if s.Advanced.CreateService == nil {
csvc := true
s.Advanced.CreateService = &csvc
}
}