Skip to content

Commit

Permalink
Add metrics calculator for ProxySQL, PgBouncer, XtraDB (#28)
Browse files Browse the repository at this point in the history
* Add metrics calculator for ProxySQL, PgBouncer, XtraDB

Signed-off-by: Tamal Saha <tamal@appscode.com>

* Add tests

Signed-off-by: Tamal Saha <tamal@appscode.com>
  • Loading branch information
tamalsaha committed Apr 4, 2022
1 parent 9880534 commit be02b89
Show file tree
Hide file tree
Showing 6 changed files with 560 additions and 6 deletions.
94 changes: 94 additions & 0 deletions kubedb.com/v1alpha2/perconaxtradb.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/*
Copyright AppsCode Inc. and Contributors
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 v1alpha2

import (
"fmt"

"kmodules.xyz/resource-metrics/api"

core "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime/schema"
)

func init() {
api.Register(schema.GroupVersionKind{
Group: "kubedb.com",
Version: "v1alpha2",
Kind: "PerconaXtraDB",
}, PerconaXtraDB{}.ResourceCalculator())
}

type PerconaXtraDB struct{}

func (r PerconaXtraDB) ResourceCalculator() api.ResourceCalculator {
return &api.ResourceCalculatorFuncs{
AppRoles: []api.PodRole{api.PodRoleDefault},
RuntimeRoles: []api.PodRole{api.PodRoleDefault, api.PodRoleExporter},
RoleReplicasFn: r.roleReplicasFn,
ModeFn: r.modeFn,
UsesTLSFn: r.usesTLSFn,
RoleResourceLimitsFn: r.roleResourceFn(api.ResourceLimits),
RoleResourceRequestsFn: r.roleResourceFn(api.ResourceRequests),
}
}

func (r PerconaXtraDB) roleReplicasFn(obj map[string]interface{}) (api.ReplicaList, error) {
replicas, found, err := unstructured.NestedInt64(obj, "spec", "replicas")
if err != nil {
return nil, fmt.Errorf("failed to read spec.replicas %v: %w", obj, err)
}
if !found {
return api.ReplicaList{api.PodRoleDefault: 1}, nil
}
return api.ReplicaList{api.PodRoleDefault: replicas}, nil
}

func (r PerconaXtraDB) modeFn(obj map[string]interface{}) (string, error) {
replicas, _, err := unstructured.NestedInt64(obj, "spec", "replicas")
if err != nil {
return "", err
}
if replicas > 1 {
return DBModeCluster, nil
}
return DBStandalone, nil
}

func (r PerconaXtraDB) usesTLSFn(obj map[string]interface{}) (bool, error) {
_, found, err := unstructured.NestedFieldNoCopy(obj, "spec", "tls")
return found, err
}

func (r PerconaXtraDB) roleResourceFn(fn func(rr core.ResourceRequirements) core.ResourceList) func(obj map[string]interface{}) (map[api.PodRole]core.ResourceList, error) {
return func(obj map[string]interface{}) (map[api.PodRole]core.ResourceList, error) {
container, replicas, err := api.AppNodeResources(obj, fn, "spec")
if err != nil {
return nil, err
}

exporter, err := api.ContainerResources(obj, fn, "spec", "monitor", "prometheus", "exporter")
if err != nil {
return nil, err
}
return map[api.PodRole]core.ResourceList{
api.PodRoleDefault: api.MulResourceList(container, replicas),
api.PodRoleExporter: api.MulResourceList(exporter, replicas),
}, nil
}
}
147 changes: 147 additions & 0 deletions kubedb.com/v1alpha2/perconaxtradb_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
/*
Copyright AppsCode Inc. and Contributors
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 v1alpha2

import (
"testing"

mt "kmodules.xyz/resource-metrics/testing"

"github.com/google/go-cmp/cmp"
core "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
)

func TestPerconaXtraDB(t *testing.T) {
type want struct {
replicas int64
mode string
totalResources core.ResourceRequirements
appResources core.ResourceRequirements
}
tests := []struct {
name string
want want
}{
{
name: "testdata/kubedb.com/v1alpha2/percona-xtradb/standalone.yaml",
want: want{
replicas: 1,
mode: DBStandalone,
totalResources: core.ResourceRequirements{
Limits: core.ResourceList{
core.ResourceCPU: resource.MustParse("1"),
core.ResourceMemory: resource.MustParse("1152Mi"),
core.ResourceStorage: resource.MustParse("1Gi"),
},
Requests: core.ResourceList{
core.ResourceCPU: resource.MustParse("500m"),
core.ResourceMemory: resource.MustParse("564Mi"),
core.ResourceStorage: resource.MustParse("1Gi"),
},
},
appResources: core.ResourceRequirements{
Limits: core.ResourceList{
core.ResourceCPU: resource.MustParse("500m"),
core.ResourceMemory: resource.MustParse("1Gi"),
core.ResourceStorage: resource.MustParse("1Gi"),
},
Requests: core.ResourceList{
core.ResourceCPU: resource.MustParse("250m"),
core.ResourceMemory: resource.MustParse("500Mi"),
core.ResourceStorage: resource.MustParse("1Gi"),
},
},
},
},
{
name: "testdata/kubedb.com/v1alpha2/percona-xtradb/cluster.yaml",
want: want{
replicas: 3,
mode: "Cluster",
totalResources: core.ResourceRequirements{
Limits: core.ResourceList{
core.ResourceCPU: resource.MustParse("3"),
core.ResourceMemory: resource.MustParse("3456Mi"),
core.ResourceStorage: resource.MustParse("3Gi"),
},
Requests: core.ResourceList{
core.ResourceCPU: resource.MustParse("1.5"),
core.ResourceMemory: resource.MustParse("1692Mi"),
core.ResourceStorage: resource.MustParse("3Gi"),
},
},
appResources: core.ResourceRequirements{
Limits: core.ResourceList{
core.ResourceCPU: resource.MustParse("1.5"),
core.ResourceMemory: resource.MustParse("3Gi"),
core.ResourceStorage: resource.MustParse("3Gi"),
},
Requests: core.ResourceList{
core.ResourceCPU: resource.MustParse("750m"),
core.ResourceMemory: resource.MustParse("1500Mi"),
core.ResourceStorage: resource.MustParse("3Gi"),
},
},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
obj, err := mt.Load(tt.name)
if err != nil {
t.Error(err)
return
}
c := PerconaXtraDB{}.ResourceCalculator()

if got, err := c.Replicas(obj); err != nil {
t.Errorf("Replicas() error = %v", err)
} else if got != tt.want.replicas {
t.Errorf("Replicas found = %v, expected = %v", got, tt.want.replicas)
}

if got, err := c.Mode(obj); err != nil {
t.Errorf("Mode() error = %v", err)
} else if got != tt.want.mode {
t.Errorf("Mode found = %v, expected = %v", got, tt.want.mode)
}

if got, err := c.TotalResourceLimits(obj); err != nil {
t.Errorf("TotalResourceLimits() error = %v", err)
} else if !cmp.Equal(tt.want.totalResources.Limits, got) {
t.Errorf("TotalResourceLimits() difference = %v", cmp.Diff(tt.want.totalResources.Limits, got))
}
if got, err := c.TotalResourceRequests(obj); err != nil {
t.Errorf("TotalResourceRequests() error = %v", err)
} else if !cmp.Equal(tt.want.totalResources.Requests, got) {
t.Errorf("TotalResourceRequests() difference = %v", cmp.Diff(tt.want.totalResources.Requests, got))
}

if got, err := c.AppResourceLimits(obj); err != nil {
t.Errorf("AppResourceLimits() error = %v", err)
} else if !cmp.Equal(tt.want.appResources.Limits, got) {
t.Errorf("AppResourceLimits() difference = %v", cmp.Diff(tt.want.appResources.Limits, got))
}
if got, err := c.AppResourceRequests(obj); err != nil {
t.Errorf("AppResourceRequests() error = %v", err)
} else if !cmp.Equal(tt.want.appResources.Requests, got) {
t.Errorf("AppResourceRequests() difference = %v", cmp.Diff(tt.want.appResources.Requests, got))
}
})
}
}
94 changes: 94 additions & 0 deletions kubedb.com/v1alpha2/pgbouncer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/*
Copyright AppsCode Inc. and Contributors
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 v1alpha2

import (
"fmt"

"kmodules.xyz/resource-metrics/api"

core "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime/schema"
)

func init() {
api.Register(schema.GroupVersionKind{
Group: "kubedb.com",
Version: "v1alpha2",
Kind: "PgBouncer",
}, PgBouncer{}.ResourceCalculator())
}

type PgBouncer struct{}

func (r PgBouncer) ResourceCalculator() api.ResourceCalculator {
return &api.ResourceCalculatorFuncs{
AppRoles: []api.PodRole{api.PodRoleDefault},
RuntimeRoles: []api.PodRole{api.PodRoleDefault, api.PodRoleExporter},
RoleReplicasFn: r.roleReplicasFn,
ModeFn: r.modeFn,
UsesTLSFn: r.usesTLSFn,
RoleResourceLimitsFn: r.roleResourceFn(api.ResourceLimits),
RoleResourceRequestsFn: r.roleResourceFn(api.ResourceRequests),
}
}

func (r PgBouncer) roleReplicasFn(obj map[string]interface{}) (api.ReplicaList, error) {
replicas, found, err := unstructured.NestedInt64(obj, "spec", "replicas")
if err != nil {
return nil, fmt.Errorf("failed to read spec.replicas %v: %w", obj, err)
}
if !found {
return api.ReplicaList{api.PodRoleDefault: 1}, nil
}
return api.ReplicaList{api.PodRoleDefault: replicas}, nil
}

func (r PgBouncer) modeFn(obj map[string]interface{}) (string, error) {
replicas, _, err := unstructured.NestedInt64(obj, "spec", "replicas")
if err != nil {
return "", err
}
if replicas > 1 {
return DBModeCluster, nil
}
return DBStandalone, nil
}

func (r PgBouncer) usesTLSFn(obj map[string]interface{}) (bool, error) {
_, found, err := unstructured.NestedFieldNoCopy(obj, "spec", "tls")
return found, err
}

func (r PgBouncer) roleResourceFn(fn func(rr core.ResourceRequirements) core.ResourceList) func(obj map[string]interface{}) (map[api.PodRole]core.ResourceList, error) {
return func(obj map[string]interface{}) (map[api.PodRole]core.ResourceList, error) {
container, replicas, err := api.AppNodeResources(obj, fn, "spec")
if err != nil {
return nil, err
}

exporter, err := api.ContainerResources(obj, fn, "spec", "monitor", "prometheus", "exporter")
if err != nil {
return nil, err
}
return map[api.PodRole]core.ResourceList{
api.PodRoleDefault: api.MulResourceList(container, replicas),
api.PodRoleExporter: api.MulResourceList(exporter, replicas),
}, nil
}
}
Loading

0 comments on commit be02b89

Please sign in to comment.