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 3b5bad2
Show file tree
Hide file tree
Showing 12 changed files with 544 additions and 76 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,4 @@ require (
google.golang.org/protobuf v1.26.0 // indirect
)

replace github.com/gophercloud/gophercloud => github.com/gophercloud/gophercloud v0.24.1-0.20220209210756-c2c9a5564161
replace github.com/gophercloud/gophercloud => github.com/sapcc/gophercloud v0.0.0-20220215140845-a2d0ea7cf216
8 changes: 2 additions & 6 deletions 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 Expand Up @@ -159,8 +158,6 @@ github.com/google/renameio v0.1.0/go.mod h1:KWCgfxg9yswjAJkECMjeO8J8rahYeXnNhOm4
github.com/google/uuid v1.1.2/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/googleapis/gax-go/v2 v2.0.4/go.mod h1:0Wqv26UfaUD9n4G6kQubkQ+KchISgw+vpHVxEJEs9eg=
github.com/googleapis/gax-go/v2 v2.0.5/go.mod h1:DWXyrwAJ9X0FpwwEdw+IPEYBICEFu5mhpdKc/us6bOk=
github.com/gophercloud/gophercloud v0.24.1-0.20220209210756-c2c9a5564161 h1:RYxDqNUwFVXLZ4i02nbFu0/+1O4w4kQm9MHCpS/lGys=
github.com/gophercloud/gophercloud v0.24.1-0.20220209210756-c2c9a5564161/go.mod h1:Q8fZtyi5zZxPS/j9aj3sSxtvj41AdQMDwyo1myduD5c=
github.com/gophercloud/utils v0.0.0-20210323225332-7b186010c04f/go.mod h1:wx8HMD8oQD0Ryhz6+6ykq75PJ79iPyEqYHfwZ4l7OsA=
github.com/gorilla/context v1.1.1/go.mod h1:kBGZzfjB9CEq2AlWe17Uuf7NDRt0dE0s8S51q0aT7Yg=
github.com/gorilla/mux v1.6.2/go.mod h1:1lud6UwP+6orDFRuTfBEV8e9/aOM/c4fVVCaMa2zaAs=
Expand Down Expand Up @@ -306,6 +303,8 @@ github.com/sapcc/go-bits v0.0.0-20180828115508-ed29938ce01c/go.mod h1:KeP5OOqQIg
github.com/sapcc/go-bits v0.0.0-20190910095551-3863bd6b4943/go.mod h1:YpW83RbRSHPedMBfuvdfYJMQbejxSxzK1Kj/39szShw=
github.com/sapcc/go-bits v0.0.0-20220204120448-40f0b29218db h1:yeuVT13U5thlzCdTj1/7CnUf//si3+5bPxQr5HjjTyI=
github.com/sapcc/go-bits v0.0.0-20220204120448-40f0b29218db/go.mod h1:Q3azgrNnSmcGHuqrMmo8+M+WHlU9CEK8cSh2rBVe+8M=
github.com/sapcc/gophercloud v0.0.0-20220215140845-a2d0ea7cf216 h1:v5cZWrHpjUAHDDhruPxIxAmV3Nh3sN9owmTePt3S9Wo=
github.com/sapcc/gophercloud v0.0.0-20220215140845-a2d0ea7cf216/go.mod h1:Q8fZtyi5zZxPS/j9aj3sSxtvj41AdQMDwyo1myduD5c=
github.com/sapcc/hermes v0.0.0-20190722215605-d772f40f9fc6/go.mod h1:WM5CVlqFCGM+AUE7lQgkLmPE89PWqJS3i+oRXaSztR8=
github.com/sapcc/hermes v0.0.0-20211212013121-4426e1b79ccc h1:YBTayXeNDOtEMoavKVgv6G51Vi/tiHZ9wJI9OrNcHS0=
github.com/sapcc/hermes v0.0.0-20211212013121-4426e1b79ccc/go.mod h1:1fxCUJjYnq0n63o61aXG+5G1oCKApPb667aiGawNvHQ=
Expand Down Expand Up @@ -495,19 +494,16 @@ golang.org/x/sys v0.0.0-20210124154548-22da62e12c0c/go.mod h1:h1NjWce9XRLGQEsW7w
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210603081109-ebe580a85c40/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220114195835-da31bd327af9 h1:XfKQ4OlFl8okEOr5UvAqFRVj8pY/4yfcXrddB8qAbU0=
golang.org/x/sys v0.0.0-20220114195835-da31bd327af9/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.1-0.20180807135948-17ff2d5776d2/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.7 h1:olpwvP2KacW1ZWvsR7uQhoyTYvKAupfQrRGBFM352Gk=
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
golang.org/x/time v0.0.0-20181108054448-85acf8d2951c/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
golang.org/x/time v0.0.0-20190308202827-9d24e82272b4/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
golang.org/x/time v0.0.0-20191024005414-555d28b269f0/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
Expand Down
100 changes: 33 additions & 67 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 @@ -104,25 +107,33 @@ func (p *designatePlugin) Scrape(provider *gophercloud.ProviderClient, eo gopher
//query "recordsets per zone" usage by counting recordsets in each zone
//individually (we could count all recordsets over the all project at once,
//but that won't help since the quota applies per individual zone)
maxRecordsetsPerZone := uint64(0)
maxRecordsetsPerZone := 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,
Usage: maxRecordsetsPerZone,
Quota: int64(quotas.ZoneRecordsets),
Usage: uint64(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,38 +182,9 @@ 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{})
pager.Headers = map[string]string{
client.MoreHeaders = map[string]string{
"X-Auth-All-Projects": "false",
"X-Auth-Sudo-Project-Id": projectUUID,
}
Expand All @@ -213,26 +202,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 3b5bad2

Please sign in to comment.