Skip to content

Instantly share code, notes, and snippets.

@sidneyw
Last active May 2, 2018 07:00
Show Gist options
  • Save sidneyw/86503f992857f553bff77db16ac8b021 to your computer and use it in GitHub Desktop.
Save sidneyw/86503f992857f553bff77db16ac8b021 to your computer and use it in GitHub Desktop.
type Plugin struct {
ID string `json:"id"`
Label string `json:"label"`
Description string `json:"description,omitempty"`
Interfaces []string `json:"interfaces"`
APIVersion int `json:"api_version,omitempty"`
Report WeaveReport
// This is a concurrently accessed data structure
// acquire the lock before mutating
sync sync.Mutex
}
// Execute a list of Kubernetes queries and add link data
// to the report
func (p *Plugin) GenerateReport(queries []K8sQuery) {
client := GetK8sClient()
done := make(chan bool)
// Execute queries concurrently
for _, k8sQuery := range queries {
// Calls syncAdd with each kubernetes resource object
go queryWorker(client, k8sQuery, p.syncAdd, done)
}
// Wait for all the queries to exit
for range queries {
<-done
}
}
func (p *Plugin) poll(ticker *time.Ticker) {
// Get the report before waiting
p.WeaveReportInit()
p.GenerateReport([]K8sQuery{MapDeployments})
for range ticker.C {
p.GenerateReport([]K8sQuery{MapDeployments})
Debug(func() { p.LogReport("pollK8s") })
}
}
func (p *Plugin) HandleReport(w http.ResponseWriter, r *http.Request) {
p.sync.Lock()
raw, err := json.Marshal(&p.Report)
p.sync.Unlock()
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
log.Fatalf("JSON Marshall Error %v", err)
return
}
w.WriteHeader(http.StatusOK)
w.Write(raw)
}
// Acquire the mutex and mutate the report
func (p *Plugin) syncAdd(obj K8sObject) {
// Get the relevant topology from the report
top := SelectTopology(&p.Report, obj)
weaveID, _ := GetWeaveID(obj)
metaID, metaTemplate := GetMetaTemplate()
// Get the link data from the kubernetes object
metaLatestID, metaLatest := GetMetaLatest(obj)
p.sync.Lock()
defer p.sync.Unlock()
top.AddMetadataTemplate(metaID, metaTemplate)
top.AddLatest(weaveID, metaLatestID, metaLatest)
}
@ibreakthecloud
Copy link

I am getting WeaveReport is undefined!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment