-
Notifications
You must be signed in to change notification settings - Fork 248
/
version.go
175 lines (155 loc) · 4.73 KB
/
version.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
/*
* Tencent is pleased to support the open source community by making Blueking Container Service available.
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
* Licensed under the MIT License (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
* http://opensource.org/licenses/MIT
* 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 etcd
import (
"sort"
"strconv"
"time"
"github.com/Tencent/bk-bcs/bcs-common/common/blog"
"github.com/Tencent/bk-bcs/bcs-common/pkg/scheduler/schetypes"
schStore "github.com/Tencent/bk-bcs/bcs-mesos/bcs-scheduler/src/manager/store"
"github.com/Tencent/bk-bcs/bcs-mesos/kubebkbcsv2/apis/bkbcs/v2"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
const VersionIdKey = "VersionId"
//create version, produce version id
func (store *managerStore) SaveVersion(version *types.Version) error {
version.Name = strconv.FormatInt(time.Now().UnixNano(), 10)
runAs := version.RunAs
if "" == runAs {
runAs = defaultRunAs
version.RunAs = defaultRunAs
}
err := store.checkNamespace(runAs)
if err != nil {
return err
}
client := store.BkbcsClient.Versions(runAs)
v2Version := &v2.Version{
TypeMeta: metav1.TypeMeta{
Kind: CrdVersion,
APIVersion: ApiversionV2,
},
ObjectMeta: metav1.ObjectMeta{
Name: version.Name,
Namespace: runAs,
},
Spec: v2.VersionSpec{
Version: *version,
},
}
_, err = client.Create(v2Version)
if err != nil {
return err
}
saveCacheVersion(version.RunAs, version.ID, version)
return err
}
func (store *managerStore) UpdateVersion(version *types.Version) error {
runAs := version.RunAs
if "" == runAs {
runAs = defaultRunAs
version.RunAs = defaultRunAs
}
err := store.checkNamespace(runAs)
if err != nil {
return err
}
client := store.BkbcsClient.Versions(runAs)
v2Version := &v2.Version{
TypeMeta: metav1.TypeMeta{
Kind: CrdVersion,
APIVersion: ApiversionV2,
},
ObjectMeta: metav1.ObjectMeta{
Name: version.Name,
Namespace: runAs,
},
Spec: v2.VersionSpec{
Version: *version,
},
}
_, err = client.Create(v2Version)
if err != nil {
return err
}
saveCacheVersion(version.RunAs, version.ID, version)
return err
}
func (store *managerStore) ListVersions(runAs, versionID string) ([]string, error) {
versions, _ := listCacheVersions(runAs, versionID)
nodes := make([]string, 0, len(versions))
for _, version := range versions {
nodes = append(nodes, version.Name)
}
if len(nodes) == 0 {
blog.Warnf("fetch version(%s.%s) is empty", runAs, versionID)
return nil, nil
}
return nodes, nil
}
func (store *managerStore) listClusterVersions() ([]*types.Version, error) {
client := store.BkbcsClient.Versions("")
v2Versions, err := client.List(metav1.ListOptions{})
if err != nil {
return nil, err
}
nodes := make([]*types.Version, 0, len(v2Versions.Items))
for _, version := range v2Versions.Items {
obj := version.Spec.Version
nodes = append(nodes, &obj)
}
return nodes, nil
}
func (store *managerStore) FetchVersion(runAs, versionId, versionNo string) (*types.Version, error) {
version, _ := getCacheVersion(runAs, versionId, versionNo)
if version == nil {
return nil, schStore.ErrNoFound
}
return version, nil
}
func (store *managerStore) DeleteVersion(runAs, versionId, versionNo string) error {
if "" == runAs {
runAs = defaultRunAs
}
client := store.BkbcsClient.Versions(runAs)
err := client.Delete(versionNo, &metav1.DeleteOptions{})
if err != nil && !errors.IsNotFound(err) {
return err
}
return nil
}
//zk stores this method by removing the version path, and etcd stores in order to be consistent
//with the store interface, this method does not implement anything
func (store *managerStore) DeleteVersionNode(runAs, versionId string) error {
deleteCacheVersion(runAs, versionId)
return nil
}
func (store *managerStore) GetVersion(runAs, appId string) (*types.Version, error) {
versions, err := store.ListVersions(runAs, appId)
if err != nil {
blog.Error("fail to list versions. err:%s", err.Error())
return nil, err
}
if len(versions) != 0 {
sort.Strings(versions)
newestVersion, err := store.FetchVersion(runAs, appId, versions[len(versions)-1])
if err != nil {
blog.Error("fail to fetch version by runAs(%s), appId(%s), versionNo(%s). err:%s", runAs, appId, versions[len(versions)-1], err.Error())
return nil, err
}
return newestVersion, nil
}
return nil, nil
}