-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix panic issue in resource-metrics (#420)
Signed-off-by: Arnob kumar saha <[email protected]>
- Loading branch information
1 parent
baf126c
commit df8e417
Showing
10 changed files
with
511 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
92 changes: 92 additions & 0 deletions
92
vendor/kmodules.xyz/resource-metrics/kafka.kubedb.com/v1alpha1/connectcluster.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
/* | ||
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 v1alpha1 | ||
|
||
import ( | ||
"fmt" | ||
|
||
core "k8s.io/api/core/v1" | ||
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" | ||
"k8s.io/apimachinery/pkg/runtime/schema" | ||
"kmodules.xyz/resource-metrics/api" | ||
) | ||
|
||
func init() { | ||
api.Register(schema.GroupVersionKind{ | ||
Group: "kafka.kubedb.com", | ||
Version: "v1alpha1", | ||
Kind: "ConnectCluster", | ||
}, ConnectCluster{}.ResourceCalculator()) | ||
} | ||
|
||
type ConnectCluster struct{} | ||
|
||
func (r ConnectCluster) 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 ConnectCluster) 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 ConnectCluster) modeFn(obj map[string]interface{}) (string, error) { | ||
replicas, _, err := unstructured.NestedInt64(obj, "spec", "replicas") | ||
if err != nil { | ||
return "", err | ||
} | ||
if replicas > 1 { | ||
return ConnectClusterModeDistributed, nil | ||
} | ||
return ConnectClusterModeStandalone, nil | ||
} | ||
|
||
func (r ConnectCluster) usesTLSFn(obj map[string]interface{}) (bool, error) { | ||
_, found, err := unstructured.NestedFieldNoCopy(obj, "spec", "enableSSL") | ||
return found, err | ||
} | ||
|
||
func (r ConnectCluster) 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) { | ||
exporter, err := api.ContainerResources(obj, fn, "spec", "monitor", "prometheus", "exporter") | ||
if err != nil { | ||
return nil, err | ||
} | ||
container, replicas, err := api.AppNodeResourcesV2(obj, fn, ConnectClusterContainerName, "spec") | ||
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 | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
vendor/kmodules.xyz/resource-metrics/kafka.kubedb.com/v1alpha1/constants.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
/* | ||
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 v1alpha1 | ||
|
||
const ( | ||
ConnectClusterModeStandalone = "Standalone" | ||
ConnectClusterModeDistributed = "Distributed" | ||
) | ||
|
||
const ( | ||
ConnectClusterContainerName = "connect-cluster" | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
139 changes: 139 additions & 0 deletions
139
vendor/kmodules.xyz/resource-metrics/kubedb.com/v1alpha2/druid.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
/* | ||
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" | ||
"reflect" | ||
|
||
"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: "Druid", | ||
}, Druid{}.ResourceCalculator()) | ||
} | ||
|
||
type Druid struct{} | ||
|
||
func (r Druid) 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 Druid) roleReplicasFn(obj map[string]interface{}) (api.ReplicaList, error) { | ||
result := api.ReplicaList{} | ||
|
||
topology, found, err := unstructured.NestedMap(obj, "spec", "topology") | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
if found && topology != nil { | ||
var replicas int64 = 0 | ||
for role, roleSpec := range topology { | ||
roleReplicas, found, err := unstructured.NestedInt64(roleSpec.(map[string]interface{}), "replicas") | ||
if err != nil { | ||
return nil, err | ||
} | ||
if found { | ||
result[api.PodRole(role)] = roleReplicas | ||
replicas += roleReplicas | ||
} | ||
} | ||
result[api.PodRoleDefault] = replicas | ||
} else { | ||
// Combined mode | ||
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 { | ||
result[api.PodRoleDefault] = 1 | ||
} else { | ||
result[api.PodRoleDefault] = replicas | ||
} | ||
} | ||
|
||
return result, nil | ||
} | ||
|
||
func (r Druid) modeFn(obj map[string]interface{}) (string, error) { | ||
topology, found, err := unstructured.NestedFieldNoCopy(obj, "spec", "topology") | ||
if err != nil { | ||
return "", err | ||
} | ||
if found && !reflect.ValueOf(topology).IsNil() { | ||
return DBModeDedicated, nil | ||
} | ||
return DBModeCombined, nil | ||
} | ||
|
||
func (r Druid) usesTLSFn(obj map[string]interface{}) (bool, error) { | ||
_, found, err := unstructured.NestedFieldNoCopy(obj, "spec", "enableSSL") | ||
return found, err | ||
} | ||
|
||
func (r Druid) 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) { | ||
exporter, err := api.ContainerResources(obj, fn, "spec", "monitor", "prometheus", "exporter") | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
topology, found, err := unstructured.NestedMap(obj, "spec", "topology") | ||
if err != nil { | ||
return nil, err | ||
} | ||
if found && topology != nil { | ||
var replicas int64 = 0 | ||
var totalResources core.ResourceList | ||
result := map[api.PodRole]core.ResourceList{} | ||
|
||
for role, roleSpec := range topology { | ||
rolePerReplicaResources, roleReplicas, err := api.AppNodeResourcesV2(roleSpec.(map[string]interface{}), fn, DruidContainerName) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
roleResources := api.MulResourceList(rolePerReplicaResources, roleReplicas) | ||
result[api.PodRole(role)] = roleResources | ||
totalResources = api.AddResourceList(totalResources, roleResources) | ||
} | ||
|
||
result[api.PodRoleDefault] = totalResources | ||
result[api.PodRoleExporter] = api.MulResourceList(exporter, replicas) | ||
return result, nil | ||
} | ||
|
||
return nil, nil | ||
} | ||
} |
94 changes: 94 additions & 0 deletions
94
vendor/kmodules.xyz/resource-metrics/kubedb.com/v1alpha2/pgpool.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: "Pgpool", | ||
}, Pgpool{}.ResourceCalculator()) | ||
} | ||
|
||
type Pgpool struct{} | ||
|
||
func (r Pgpool) 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 Pgpool) 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 Pgpool) 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 DBModeStandalone, nil | ||
} | ||
|
||
func (r Pgpool) usesTLSFn(obj map[string]interface{}) (bool, error) { | ||
_, found, err := unstructured.NestedFieldNoCopy(obj, "spec", "tls") | ||
return found, err | ||
} | ||
|
||
func (r Pgpool) 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.AppNodeResourcesV2(obj, fn, PgpoolContainerName, "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 | ||
} | ||
} |
Oops, something went wrong.