From 0b13ba520b68b221c0908db78f7e51c2ec15ed8e Mon Sep 17 00:00:00 2001 From: Tamal Saha Date: Thu, 13 Jan 2022 16:06:27 -0800 Subject: [PATCH] Add UsesTLS method (#21) Signed-off-by: Tamal Saha --- api/interface.go | 9 +++++++++ calculator.go | 8 ++++++++ eval.go | 6 ++++++ kubedb.com/v1alpha2/elasticsearch.go | 6 ++++++ kubedb.com/v1alpha2/mariadb.go | 6 ++++++ kubedb.com/v1alpha2/memcached.go | 6 ++++++ kubedb.com/v1alpha2/mongodb.go | 6 ++++++ kubedb.com/v1alpha2/mysql.go | 6 ++++++ kubedb.com/v1alpha2/postgres.go | 6 ++++++ kubedb.com/v1alpha2/redis.go | 6 ++++++ template.go | 14 ++++++++++++++ 11 files changed, 79 insertions(+) diff --git a/api/interface.go b/api/interface.go index f80c3a42..ed3735f4 100644 --- a/api/interface.go +++ b/api/interface.go @@ -25,6 +25,7 @@ type ResourceCalculator interface { RoleReplicas(obj map[string]interface{}) (ReplicaList, error) Mode(obj map[string]interface{}) (string, error) + UsesTLS(obj map[string]interface{}) (bool, error) TotalResourceLimits(obj map[string]interface{}) (core.ResourceList, error) TotalResourceRequests(obj map[string]interface{}) (core.ResourceList, error) @@ -46,6 +47,7 @@ type ResourceCalculatorFuncs struct { RoleReplicasFn func(obj map[string]interface{}) (ReplicaList, error) ModeFn func(obj map[string]interface{}) (string, error) + UsesTLSFn func(obj map[string]interface{}) (bool, error) RoleResourceLimitsFn func(obj map[string]interface{}) (map[PodRole]core.ResourceList, error) RoleResourceRequestsFn func(obj map[string]interface{}) (map[PodRole]core.ResourceList, error) } @@ -75,6 +77,13 @@ func (c ResourceCalculatorFuncs) Mode(obj map[string]interface{}) (string, error return "", nil } +func (c ResourceCalculatorFuncs) UsesTLS(obj map[string]interface{}) (bool, error) { + if c.UsesTLSFn != nil { + return c.UsesTLSFn(obj) + } + return false, nil +} + func (c ResourceCalculatorFuncs) TotalResourceLimits(obj map[string]interface{}) (core.ResourceList, error) { rr, err := c.RoleResourceLimits(obj) if err != nil { diff --git a/calculator.go b/calculator.go index 3f3eb68a..8c507173 100644 --- a/calculator.go +++ b/calculator.go @@ -51,6 +51,14 @@ func Mode(obj map[string]interface{}) (string, error) { return c.Mode(obj) } +func UsesTLS(obj map[string]interface{}) (bool, error) { + c, err := api.Load(obj) + if err != nil { + return false, err + } + return c.UsesTLS(obj) +} + func TotalResourceLimits(obj map[string]interface{}) (core.ResourceList, error) { c, err := api.Load(obj) if err != nil { diff --git a/eval.go b/eval.go index 465ea9ed..0b5bad1d 100644 --- a/eval.go +++ b/eval.go @@ -29,6 +29,7 @@ func EvalFuncs() map[string]func(arguments ...interface{}) (interface{}, error) return map[string]func(arguments ...interface{}) (interface{}, error){ "resource_replicas": resourceReplicas, "resource_mode": resourceMode, + "resource_uses_tls": resourceUsesTLS, "total_resource_limits": totalResourceLimits, "total_resource_requests": totalResourceRequests, "app_resource_limits": appResourceLimits, @@ -46,6 +47,11 @@ func resourceMode(args ...interface{}) (interface{}, error) { return Mode(args[0].(map[string]interface{})) } +// resourceUsesTLS(resource_obj) +func resourceUsesTLS(args ...interface{}) (interface{}, error) { + return UsesTLS(args[0].(map[string]interface{})) +} + // totalResourceLimits(resource_obj, resource_type) => cpu cores (float64) func totalResourceLimits(args ...interface{}) (interface{}, error) { rr, err := TotalResourceLimits(args[0].(map[string]interface{})) diff --git a/kubedb.com/v1alpha2/elasticsearch.go b/kubedb.com/v1alpha2/elasticsearch.go index 8732b320..36639c9c 100644 --- a/kubedb.com/v1alpha2/elasticsearch.go +++ b/kubedb.com/v1alpha2/elasticsearch.go @@ -45,6 +45,7 @@ func (r Elasticsearch) ResourceCalculator() api.ResourceCalculator { 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), } @@ -96,6 +97,11 @@ func (r Elasticsearch) modeFn(obj map[string]interface{}) (string, error) { return "Combined", nil } +func (r Elasticsearch) usesTLSFn(obj map[string]interface{}) (bool, error) { + _, found, err := unstructured.NestedFieldNoCopy(obj, "spec", "enableSSL") + return found, err +} + func (r Elasticsearch) 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") diff --git a/kubedb.com/v1alpha2/mariadb.go b/kubedb.com/v1alpha2/mariadb.go index faf91b03..50c0eb5f 100644 --- a/kubedb.com/v1alpha2/mariadb.go +++ b/kubedb.com/v1alpha2/mariadb.go @@ -42,6 +42,7 @@ func (r MariaDB) ResourceCalculator() api.ResourceCalculator { 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), } @@ -69,6 +70,11 @@ func (r MariaDB) modeFn(obj map[string]interface{}) (string, error) { return DBStandalone, nil } +func (r MariaDB) usesTLSFn(obj map[string]interface{}) (bool, error) { + _, found, err := unstructured.NestedFieldNoCopy(obj, "spec", "tls") + return found, err +} + func (r MariaDB) 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") diff --git a/kubedb.com/v1alpha2/memcached.go b/kubedb.com/v1alpha2/memcached.go index bf3e64bb..c82cbee0 100644 --- a/kubedb.com/v1alpha2/memcached.go +++ b/kubedb.com/v1alpha2/memcached.go @@ -42,6 +42,7 @@ func (r Memcached) ResourceCalculator() api.ResourceCalculator { 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), } @@ -69,6 +70,11 @@ func (r Memcached) modeFn(obj map[string]interface{}) (string, error) { return DBStandalone, nil } +func (r Memcached) usesTLSFn(obj map[string]interface{}) (bool, error) { + _, found, err := unstructured.NestedFieldNoCopy(obj, "spec", "tls") + return found, err +} + func (r Memcached) 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") diff --git a/kubedb.com/v1alpha2/mongodb.go b/kubedb.com/v1alpha2/mongodb.go index f29f2de2..0f9d9220 100644 --- a/kubedb.com/v1alpha2/mongodb.go +++ b/kubedb.com/v1alpha2/mongodb.go @@ -42,6 +42,7 @@ func (r MongoDB) ResourceCalculator() api.ResourceCalculator { RuntimeRoles: []api.PodRole{api.PodRoleDefault, api.PodRoleTotalShard, api.PodRoleConfigServer, api.PodRoleMongos, api.PodRoleExporter}, RoleReplicasFn: r.roleReplicasFn, ModeFn: r.modeFn, + UsesTLSFn: r.usesTLSFn, RoleResourceLimitsFn: r.roleResourceFn(api.ResourceLimits), RoleResourceRequestsFn: r.roleResourceFn(api.ResourceRequests), } @@ -108,6 +109,11 @@ func (r MongoDB) modeFn(obj map[string]interface{}) (string, error) { return DBStandalone, nil } +func (r MongoDB) usesTLSFn(obj map[string]interface{}) (bool, error) { + _, found, err := unstructured.NestedFieldNoCopy(obj, "spec", "tls") + return found, err +} + func (r MongoDB) 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") diff --git a/kubedb.com/v1alpha2/mysql.go b/kubedb.com/v1alpha2/mysql.go index 8580184e..243e1f0d 100644 --- a/kubedb.com/v1alpha2/mysql.go +++ b/kubedb.com/v1alpha2/mysql.go @@ -42,6 +42,7 @@ func (r MySQL) ResourceCalculator() api.ResourceCalculator { RuntimeRoles: []api.PodRole{api.PodRoleDefault, api.PodRoleExporter, api.PodRoleRouter}, RoleReplicasFn: r.roleReplicasFn, ModeFn: r.modeFn, + UsesTLSFn: r.usesTLSFn, RoleResourceLimitsFn: r.roleResourceFn(api.ResourceLimits), RoleResourceRequestsFn: r.roleResourceFn(api.ResourceRequests), } @@ -92,6 +93,11 @@ func (r MySQL) modeFn(obj map[string]interface{}) (string, error) { return DBStandalone, nil } +func (r MySQL) usesTLSFn(obj map[string]interface{}) (bool, error) { + _, found, err := unstructured.NestedFieldNoCopy(obj, "spec", "tls") + return found, err +} + func (r MySQL) 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") diff --git a/kubedb.com/v1alpha2/postgres.go b/kubedb.com/v1alpha2/postgres.go index ce215e93..3516d124 100644 --- a/kubedb.com/v1alpha2/postgres.go +++ b/kubedb.com/v1alpha2/postgres.go @@ -42,6 +42,7 @@ func (r Postgres) ResourceCalculator() api.ResourceCalculator { 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), } @@ -69,6 +70,11 @@ func (r Postgres) modeFn(obj map[string]interface{}) (string, error) { return DBStandalone, nil } +func (r Postgres) usesTLSFn(obj map[string]interface{}) (bool, error) { + _, found, err := unstructured.NestedFieldNoCopy(obj, "spec", "tls") + return found, err +} + func (r Postgres) 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") diff --git a/kubedb.com/v1alpha2/redis.go b/kubedb.com/v1alpha2/redis.go index 2c2dbc83..4787e3c7 100644 --- a/kubedb.com/v1alpha2/redis.go +++ b/kubedb.com/v1alpha2/redis.go @@ -42,6 +42,7 @@ func (r Redis) ResourceCalculator() api.ResourceCalculator { 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), } @@ -91,6 +92,11 @@ func (r Redis) modeFn(obj map[string]interface{}) (string, error) { return DBStandalone, nil } +func (r Redis) usesTLSFn(obj map[string]interface{}) (bool, error) { + _, found, err := unstructured.NestedFieldNoCopy(obj, "spec", "tls") + return found, err +} + func (r Redis) 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") diff --git a/template.go b/template.go index 374e3459..505b4c8c 100644 --- a/template.go +++ b/template.go @@ -44,6 +44,7 @@ func GenericFuncMap() map[string]interface{} { "k8s_resource_replicas": tplReplicaFn, "k8s_resource_replicas_by_roles": tplRoleReplicaFn, "k8s_resource_mode": tplModeFn, + "k8s_resource_uses_tls": tplUsesTLSFn, "k8s_total_resource_limits": tplTotalResourceLimitsFn, "k8s_total_resource_requests": tplTotalResourceRequestsFn, "k8s_app_resource_limits": tplAppResourceLimitsFn, @@ -112,6 +113,19 @@ func tplModeFn(data interface{}) (string, error) { return c.Mode(obj) } +func tplUsesTLSFn(data interface{}) (bool, error) { + obj, err := toObject(data) + if err != nil { + return false, err + } + + c, err := api.Load(obj) + if err != nil { + return false, err + } + return c.UsesTLS(obj) +} + func tplTotalResourceLimitsFn(data interface{}) (core.ResourceList, error) { obj, err := toObject(data) if err != nil {