Skip to content

Commit

Permalink
Merge pull request #408 from zmberg/1.15.x
Browse files Browse the repository at this point in the history
feature: 调整mesos version对象的命名规则 #383
  • Loading branch information
DeveloperJim authored Mar 13, 2020
2 parents c66fde9 + 6fb2562 commit 82df357
Showing 1 changed file with 48 additions and 14 deletions.
62 changes: 48 additions & 14 deletions bcs-mesos/bcs-scheduler/src/manager/store/etcd/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,7 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

func getNamespaceByVersion(runAs, versionId string) string {
return fmt.Sprintf("%s-%s", runAs, versionId)
}
const VersionIdKey = "VersionId"

func (store *managerStore) SaveVersion(version *types.Version) error {
version.Name = strconv.FormatInt(time.Now().UnixNano(), 10)
Expand All @@ -38,21 +36,60 @@ func (store *managerStore) SaveVersion(version *types.Version) error {
runAs = defaultRunAs
version.RunAs = defaultRunAs
}
ns := getNamespaceByVersion(runAs, version.ID)
err := store.checkNamespace(ns)
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,
Labels: map[string]string{
VersionIdKey: version.ID,
},
},
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(ns)
client := store.BkbcsClient.Versions(runAs)
v2Version := &v2.Version{
TypeMeta: metav1.TypeMeta{
Kind: CrdVersion,
APIVersion: ApiversionV2,
},
ObjectMeta: metav1.ObjectMeta{
Name: version.Name,
Namespace: ns,
Namespace: runAs,
Labels: map[string]string{
VersionIdKey: version.ID,
},
},
Spec: v2.VersionSpec{
Version: *version,
Expand Down Expand Up @@ -87,9 +124,8 @@ func (store *managerStore) ListVersions(runAs, versionID string) ([]string, erro
}

func (store *managerStore) listVersions(runAs, versionID string) ([]*types.Version, error) {
ns := getNamespaceByVersion(runAs, versionID)
client := store.BkbcsClient.Versions(ns)
v2Versions, err := client.List(metav1.ListOptions{})
client := store.BkbcsClient.Versions(runAs)
v2Versions, err := client.List(metav1.ListOptions{LabelSelector: fmt.Sprintf("%s=%s", VersionIdKey, versionID)})
if err != nil {
return nil, err
}
Expand All @@ -111,8 +147,7 @@ func (store *managerStore) FetchVersion(runAs, versionId, versionNo string) (*ty
return version, nil
}

ns := getNamespaceByVersion(runAs, versionId)
client := store.BkbcsClient.Versions(ns)
client := store.BkbcsClient.Versions(runAs)
v2Version, err := client.Get(versionNo, metav1.GetOptions{})
if err != nil {
return nil, err
Expand All @@ -125,8 +160,7 @@ func (store *managerStore) DeleteVersion(runAs, versionId, versionNo string) err
if "" == runAs {
runAs = defaultRunAs
}
ns := getNamespaceByVersion(runAs, versionId)
client := store.BkbcsClient.Versions(ns)
client := store.BkbcsClient.Versions(runAs)
err := client.Delete(versionNo, &metav1.DeleteOptions{})
return err
}
Expand Down

0 comments on commit 82df357

Please sign in to comment.