Skip to content

Commit

Permalink
Plugins designate: convert to gophercloud
Browse files Browse the repository at this point in the history
  • Loading branch information
SuperSandro2000 committed Feb 15, 2022
1 parent 0490e46 commit 7f33432
Show file tree
Hide file tree
Showing 11 changed files with 538 additions and 66 deletions.
1 change: 0 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ github.com/beorn7/perks v1.0.0/go.mod h1:KWe93zE9D1o94FZ5RNwFwVgaQK1VOXiVxmqh+Ce
github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM=
github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw=
github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
github.com/cespare/xxhash v1.1.0 h1:a6HrQnmkObjyL+Gs60czilIUGqrzKutQD6XZog3p+ko=
github.com/cespare/xxhash v1.1.0/go.mod h1:XrSqR1VqqWfGrhpAt58auRo0WTKS1nRRg3ghfAqPWnc=
github.com/cespare/xxhash/v2 v2.1.1/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
github.com/cespare/xxhash/v2 v2.1.2 h1:YRXhKfTDauu4ajMg1TPgFO5jnlC2HCbmLXMcTG5cbYE=
Expand Down
97 changes: 32 additions & 65 deletions pkg/plugins/designate.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ import (

"github.com/gophercloud/gophercloud"
"github.com/gophercloud/gophercloud/openstack"
"github.com/gophercloud/gophercloud/openstack/dns/v2/quotas"
"github.com/gophercloud/gophercloud/openstack/dns/v2/recordsets"
"github.com/gophercloud/gophercloud/openstack/dns/v2/zones"
"github.com/gophercloud/gophercloud/pagination"
"github.com/prometheus/client_golang/prometheus"
Expand Down Expand Up @@ -90,7 +92,8 @@ func (p *designatePlugin) Scrape(provider *gophercloud.ProviderClient, eo gopher
}

//query quotas
quotas, err := dnsGetQuota(client, project.UUID)
client.MoreHeaders = map[string]string{"X-Auth-All-Projects": "true"}
quotas, err := quotas.Get(client, project.UUID).Extract()
if err != nil {
return nil, "", err
}
Expand All @@ -106,22 +109,30 @@ func (p *designatePlugin) Scrape(provider *gophercloud.ProviderClient, eo gopher
//but that won't help since the quota applies per individual zone)
maxRecordsetsPerZone := uint64(0)
for _, zoneID := range zoneIDs {
count, err := dnsCountZoneRecordsets(client, project.UUID, zoneID)
client.MoreHeaders = map[string]string{
"X-Auth-All-Projects": "false",
"X-Auth-Sudo-Project-Id": project.UUID,
}
allZones, err := recordsets.ListByZone(client, zoneID, recordsets.ListOpts{Limit: 1}).AllPages()
if err != nil {
return nil, "", err
}
if maxRecordsetsPerZone < count {
maxRecordsetsPerZone = count
allRecordSets, err := recordsets.ExtractRecordSets(allZones)
if err != nil {
return nil, "", err
}
if maxRecordsetsPerZone < allRecordSets[0].Metadata.TotalCount {
maxRecordsetsPerZone = allRecordSets[0].Metadata.TotalCount
}
}

return map[string]core.ResourceData{
"zones": {
Quota: quotas.Zones,
Quota: int64(quotas.Zones),
Usage: uint64(len(zoneIDs)),
},
"recordsets": {
Quota: quotas.ZoneRecordsets,
Quota: int64(quotas.ZoneRecordsets),
Usage: maxRecordsetsPerZone,
},
}, "", nil
Expand All @@ -134,20 +145,27 @@ func (p *designatePlugin) IsQuotaAcceptableForProject(client *gophercloud.Provid
}

//SetQuota implements the core.QuotaPlugin interface.
func (p *designatePlugin) SetQuota(provider *gophercloud.ProviderClient, eo gophercloud.EndpointOpts, project core.KeystoneProject, quotas map[string]uint64) error {
func (p *designatePlugin) SetQuota(provider *gophercloud.ProviderClient, eo gophercloud.EndpointOpts, project core.KeystoneProject, quota map[string]uint64) error {
client, err := openstack.NewDNSV2(provider, eo)
if err != nil {
return err
}

return dnsSetQuota(client, project.UUID, &dnsQuota{
Zones: int64(quotas["zones"]),
ZoneRecordsets: int64(quotas["recordsets"]),
uint2p := func(val uint64) *int {
ret := int(val)
return &ret
}

client.MoreHeaders = map[string]string{"X-Auth-All-Projects": "true"}
_, err = quotas.Update(client, project.UUID, quotas.UpdateOpts{
Zones: uint2p(quota["zones"]),
ZoneRecordsets: uint2p(quota["recordsets"]),
//set ZoneRecords quota to match ZoneRecordsets
//(Designate has a records_per_recordset quota of default 20, so if we set
//ZoneRecords to 20 * ZoneRecordsets, this quota will not disturb us)
ZoneRecords: int64(quotas["recordsets"] * 20),
})
ZoneRecords: uint2p(quota["recordsets"] * 20),
}).Extract()
return err
}

//DescribeMetrics implements the core.QuotaPlugin interface.
Expand All @@ -164,39 +182,11 @@ func (p *designatePlugin) CollectMetrics(ch chan<- prometheus.Metric, clusterID
////////////////////////////////////////////////////////////////////////////////
// API requests to Designate

type dnsQuota struct {
Zones int64 `json:"zones"`
ZoneRecordsets int64 `json:"zone_recordsets"`
ZoneRecords int64 `json:"zone_records"`
}

func dnsGetQuota(client *gophercloud.ServiceClient, projectUUID string) (*dnsQuota, error) {
url := client.ServiceURL("quotas", projectUUID)
opts := gophercloud.RequestOpts{
MoreHeaders: map[string]string{"X-Auth-All-Projects": "true"},
}

var result gophercloud.Result
var data dnsQuota
_, result.Err = client.Get(url, &result.Body, &opts)
err := result.ExtractInto(&data)
return &data, err
}

func dnsSetQuota(client *gophercloud.ServiceClient, projectUUID string, quota *dnsQuota) error {
url := client.ServiceURL("quotas", projectUUID)
opts := gophercloud.RequestOpts{
MoreHeaders: map[string]string{"X-Auth-All-Projects": "true"},
}

_, err := client.Patch(url, quota, nil, &opts)
return err
}

func dnsListZoneIDs(client *gophercloud.ServiceClient, projectUUID string) ([]string, error) {
pager := zones.List(client, zones.ListOpts{})
client.MoreHeaders = map[string]string{"X-Auth-All-Projects": "false"}
pager.Headers = map[string]string{
"X-Auth-All-Projects": "false",
// "X-Auth-All-Projects": "false",
"X-Auth-Sudo-Project-Id": projectUUID,
}

Expand All @@ -213,26 +203,3 @@ func dnsListZoneIDs(client *gophercloud.ServiceClient, projectUUID string) ([]st
})
return ids, err
}

func dnsCountZoneRecordsets(client *gophercloud.ServiceClient, projectUUID, zoneID string) (uint64, error) {
url := client.ServiceURL("zones", zoneID, "recordsets")
opts := gophercloud.RequestOpts{
MoreHeaders: map[string]string{
"X-Auth-All-Projects": "false",
"X-Auth-Sudo-Project-Id": projectUUID,
},
}

//do not need all data about all recordsets, just the total count
url += "?limit=1"

var result gophercloud.Result
var data struct {
Metadata struct {
Count uint64 `json:"total_count"`
} `json:"metadata"`
}
_, result.Err = client.Get(url, &result.Body, &opts)
err := result.ExtractInto(&data)
return data.Metadata.Count, err
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 7f33432

Please sign in to comment.