-
Notifications
You must be signed in to change notification settings - Fork 3
/
pod-rep.go
106 lines (86 loc) · 2.37 KB
/
pod-rep.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
// Copyright 2015 Eryx <evorui аt gmаil dοt cοm>, All rights reserved.
//
// 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 inapi
import (
"fmt"
"sync"
"github.com/lessos/lessgo/types"
"github.com/sysinner/incore/inutils"
)
type PodRep struct {
Meta types.InnerObjectMeta `json:"meta,omitempty" toml:"meta,omitempty"`
// Spec defines the behavior of a pod.
Spec *PodSpecBound `json:"spec,omitempty" toml:"spec,omitempty"`
// Apps represents the information about a collection of applications to deploy.
// this is a module for App Engine
Apps AppInstances `json:"apps,omitempty" toml:"apps,omitempty"`
// Replica
Replica PodOperateReplica `json:"replica,omitempty" toml:"replica,omitempty"`
//
Operate PodOperate `json:"operate,omitempty" toml:"operate,omitempty"`
}
type PodRepItems []*PodRep
func (it *PodRep) RepKey() string {
return NsZonePodOpRepKey(it.Meta.ID, it.Replica.RepId)
}
var (
podRepItemsMu sync.RWMutex
)
func (ls *PodRepItems) Del(repKey string) {
podRepItemsMu.Lock()
defer podRepItemsMu.Unlock()
for i, v := range *ls {
if v.RepKey() == repKey {
*ls = append((*ls)[:i], (*ls)[i+1:]...)
return
}
}
}
func (ls *PodRepItems) Get(podId string) *PodRep {
podRepItemsMu.RLock()
defer podRepItemsMu.RUnlock()
for _, v := range *ls {
if v.RepKey() == podId {
return v
}
}
return nil
}
func (ls *PodRepItems) Set(item *PodRep) {
podRepItemsMu.Lock()
defer podRepItemsMu.Unlock()
for i, v := range *ls {
if v.RepKey() == item.RepKey() {
(*ls)[i] = item
return
}
}
*ls = append(*ls, item)
}
func (ls *PodRepItems) Each(fn func(item *PodRep)) {
podRepItemsMu.RLock()
defer podRepItemsMu.RUnlock()
for _, v := range *ls {
fn(v)
}
}
func PodRepInstanceName(podId string, repId uint32) string {
if repId > 65535 {
repId = 65535
}
return fmt.Sprintf(
"%s-%s",
podId, inutils.Uint16ToHexString(uint16(repId)),
)
}