This repository has been archived by the owner on Jun 13, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
/
caching.go
181 lines (164 loc) · 4.5 KB
/
caching.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
package caching
import (
"fmt"
"os"
"strings"
"sync"
"code.cloudfoundry.org/lager"
cfclient "github.com/cloudfoundry-community/go-cfclient"
)
type AppInfo struct {
Name string `json:"name"`
Org string `json:"org"`
OrgID string `json:"orgId"`
Space string `json:"space"`
SpaceID string `json:"spaceId"`
Monitored bool `json:"monitored"`
}
type Caching struct {
cfClientConfig *cfclient.Config
appInfosByGuid map[string]AppInfo
spaceWhiteList map[string]bool
appInfoLock sync.RWMutex
logger lager.Logger
instanceName string
environment string
}
type CachingClient interface {
GetAppInfo(string) AppInfo
GetInstanceName() string
GetEnvironmentName() string
Initialize(bool)
}
func NewCaching(config *cfclient.Config, logger lager.Logger, environment string, spaceFilter string) CachingClient {
var spaceWhiteList map[string]bool
if len(spaceFilter) > 0 {
logger.Info("config", lager.Data{"SPACE_FILTER": spaceFilter})
spaceWhiteList = make(map[string]bool)
spaceFilters := strings.Split(spaceFilter, ",")
for _, v := range spaceFilters {
v = strings.TrimSuffix(strings.Trim(v, " "), ".*")
spaceWhiteList[v] = true
logger.Debug("adding app space filter", lager.Data{"filter-content": v})
}
} else {
logger.Info("config SPACE_FILTER is nil, all apps will be monitored")
}
return &Caching{
cfClientConfig: config,
appInfosByGuid: make(map[string]AppInfo),
spaceWhiteList: spaceWhiteList,
logger: logger,
environment: environment,
}
}
func (c *Caching) addAppinfoRecord(app cfclient.App) {
var appInfo = AppInfo{
Name: app.Name,
Org: app.SpaceData.Entity.OrgData.Entity.Name,
OrgID: app.SpaceData.Entity.OrgData.Entity.Guid,
Space: app.SpaceData.Entity.Name,
SpaceID: app.SpaceData.Entity.Guid,
}
if c.spaceWhiteList == nil ||
c.spaceWhiteList[app.SpaceData.Entity.OrgData.Entity.Name] ||
c.spaceWhiteList[app.SpaceData.Entity.OrgData.Entity.Name+"."+app.SpaceData.Entity.Name] ||
c.spaceWhiteList[app.SpaceData.Entity.OrgData.Entity.Name+"."+app.SpaceData.Entity.Name+"."+app.Name] {
appInfo.Monitored = true
} else {
appInfo.Monitored = false
}
func() {
c.appInfoLock.Lock()
defer c.appInfoLock.Unlock()
c.appInfosByGuid[app.Guid] = appInfo
}()
c.logger.Debug("adding to app info cache",
lager.Data{"guid": app.Guid},
lager.Data{"info": appInfo},
)
return
}
func (c *Caching) Initialize(loadApps bool) {
c.setInstanceName()
if !loadApps {
return
}
cfClient, err := cfclient.NewClient(c.cfClientConfig)
if err != nil {
c.logger.Fatal("error creating cfclient", err)
}
apps, err := cfClient.ListApps()
if err != nil {
c.logger.Fatal("error getting app list", err)
}
for _, app := range apps {
c.addAppinfoRecord(app)
}
c.logger.Info("Cache initialize completed",
lager.Data{"cache size": len(c.appInfosByGuid)})
}
func (c *Caching) GetAppInfo(appGuid string) AppInfo {
var appInfo AppInfo
var ok bool
func() {
c.appInfoLock.RLock()
defer c.appInfoLock.RUnlock()
appInfo, ok = c.appInfosByGuid[appGuid]
}()
if ok {
return appInfo
} else {
c.logger.Info("App info not found for GUID",
lager.Data{"guid": appGuid})
// call the client api to get the name for this app
// purposely create a new client due to issue in using a single client
cfClient, err := cfclient.NewClient(c.cfClientConfig)
if err != nil {
c.logger.Error("error creating cfclient", err)
return AppInfo{
Name: "",
Org: "",
OrgID: "",
Space: "",
SpaceID: "",
Monitored: false,
}
}
app, err := cfClient.AppByGuid(appGuid)
if err != nil {
c.logger.Error("error getting app info", err, lager.Data{"guid": appGuid})
return AppInfo{
Name: "",
Org: "",
OrgID: "",
Space: "",
SpaceID: "",
Monitored: false,
}
} else {
// store app info in map
c.addAppinfoRecord(app)
// return App Info
return appInfo
}
}
}
func (c *Caching) setInstanceName() error {
// instance id to track multiple nozzles, used for logging
hostName, err := os.Hostname()
if err != nil {
c.logger.Error("failed to get hostname for nozzle instance", err)
c.instanceName = fmt.Sprintf("pid-%d", os.Getpid())
} else {
c.instanceName = fmt.Sprintf("pid-%d@%s", os.Getpid(), hostName)
}
c.logger.Info("getting nozzle instance name", lager.Data{"name": c.instanceName})
return err
}
func (c *Caching) GetInstanceName() string {
return c.instanceName
}
func (c *Caching) GetEnvironmentName() string {
return c.environment
}