/
processor_database.go
211 lines (191 loc) Β· 5.79 KB
/
processor_database.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
// Copyright 2022 The kubegems.io Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package application
import (
"context"
"errors"
"fmt"
"gorm.io/gorm"
"kubegems.io/kubegems/pkg/log"
"kubegems.io/kubegems/pkg/service/models"
)
type DatabseProcessor struct {
DB *gorm.DB
}
type EnvironmentDetails struct {
ClusterName string `json:"clusterName,omitempty"`
ClusterKubeConfig []byte `json:"clusterKubeConfig,omitempty"`
Namespace string `json:"namespace,omitempty"`
EnvironmentName string `json:"environmentName,omitempty"`
EnvironmentType string `json:"environmentType,omitempty"`
// TenantName string `json:"tenantName,omitempty"`
// ProjectName string `json:"projectName,omitempty"`
}
func (p *DatabseProcessor) GetEnvironmentWithCluster(ref PathRef) (*EnvironmentDetails, error) {
env := &models.Environment{
EnvironmentName: ref.Env,
Project: &models.Project{
ProjectName: ref.Project,
Tenant: &models.Tenant{
TenantName: ref.Tenant,
},
},
}
if err := p.DB.Where(env).Preload("Cluster").Take(env).Error; err != nil {
return nil, err
}
if len(env.Cluster.KubeConfig) == 0 {
if env.Cluster.AgentAddr != models.SelfClusterAgentAddress {
return nil, fmt.Errorf("cluster %s kubeconfig is empty", env.Cluster.ClusterName)
}
}
return &EnvironmentDetails{
ClusterName: env.Cluster.ClusterName,
ClusterKubeConfig: env.Cluster.KubeConfig,
Namespace: env.Namespace,
EnvironmentName: env.EnvironmentName,
EnvironmentType: env.MetaType,
}, nil
}
const (
statusDeploy = 0b10
statusApp = 0b01
)
type syncStatus struct {
deploy DeploiedManifest
app *models.Application
}
func (p *DatabseProcessor) RemoveDeploy(ctx context.Context, ref PathRef, deploy DeploiedManifest) error {
exampleapp := &models.Application{
ApplicationName: deploy.Name,
Environment: &models.Environment{EnvironmentName: ref.Env},
Project: &models.Project{
ProjectName: ref.Project,
Tenant: &models.Tenant{TenantName: ref.Tenant},
},
}
exist := &models.Application{}
if err := p.DB.Where(exampleapp).Delete(&exist).Error; err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return nil
}
// remove existing
return err
}
return nil
}
func (p *DatabseProcessor) SyncDeploy(ctx context.Context, ref PathRef, deploy DeploiedManifest) error {
log := log.FromContextOrDiscard(ctx)
if ref.IsEmpty() || deploy.Name == "" {
return fmt.Errorf("invalid ref or deploy name")
}
exampleapp := &models.Application{
ApplicationName: deploy.Name,
Environment: &models.Environment{EnvironmentName: ref.Env},
Project: &models.Project{
ProjectName: ref.Project,
Tenant: &models.Tenant{TenantName: ref.Tenant},
},
}
exist := &models.Application{}
if err := p.DB.Where(exampleapp).Take(&exist).Error; err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
// create a new record
updateDatabaseRecord(exampleapp, deploy)
log.V(5).Info("create new record", "app", exampleapp)
if err := p.DB.Create(exampleapp).Error; err != nil {
return err
}
}
return err
}
// update if necessary
if updateDatabaseRecord(exist, deploy) {
log.V(5).Info("update record", "app", exist)
if err := p.DB.Save(exist).Error; err != nil {
return err
}
}
return nil
}
// SyncDeploies full sync current deploies to database
// Use git store as database instead of orm database on application, but sometimes database as data source from other component, so we need to sync it
func (p *DatabseProcessor) SyncDeploies(ctx context.Context, ref PathRef, deploies []DeploiedManifest) error {
log := log.FromContextOrDiscard(ctx)
depsmap := map[string]syncStatus{}
for _, v := range deploies {
depsmap[v.Name] = syncStatus{deploy: v}
}
// sync all apps
apps := []models.Application{}
if err := p.DB.Where(models.Application{
Environment: &models.Environment{EnvironmentName: ref.Env},
Project: &models.Project{
ProjectName: ref.Project,
Tenant: &models.Tenant{TenantName: ref.Tenant},
},
}).Find(&apps).Error; err != nil {
return err
}
for i, app := range apps {
if dep, ok := depsmap[app.ApplicationName]; !ok {
// remove existing
log.Info("remove existing", "app", app)
if err := p.DB.Delete(&app).Error; err != nil {
return err
}
} else {
// mark app existing
dep.app = &apps[i]
}
}
for _, dep := range depsmap {
if dep.app != nil {
// update existing
if updateDatabaseRecord(dep.app, dep.deploy) {
if err := p.DB.Save(dep.app).Error; err != nil {
return err
}
}
} else {
// create app
newapp := &models.Application{
ApplicationName: dep.deploy.Name,
Environment: &models.Environment{EnvironmentName: ref.Env},
Project: &models.Project{ProjectName: ref.Project, Tenant: &models.Tenant{TenantName: ref.Tenant}},
}
updateDatabaseRecord(newapp, dep.deploy)
if err := p.DB.Create(newapp).Error; err != nil {
return err
}
}
}
return nil
}
func updateDatabaseRecord(app *models.Application, deploy DeploiedManifest) bool {
updated := false
if app.Kind != deploy.Kind {
app.Kind = deploy.Kind
updated = true
}
if app.Remark != deploy.Description {
app.Remark = deploy.Description
updated = true
}
if app.ApplicationName != deploy.Name {
app.ApplicationName = deploy.Name
updated = true
}
return updated
}