Skip to content

Commit

Permalink
Add UsesTLS method (#21)
Browse files Browse the repository at this point in the history
Signed-off-by: Tamal Saha <tamal@appscode.com>
  • Loading branch information
tamalsaha authored Jan 14, 2022
1 parent 9938b07 commit 0b13ba5
Show file tree
Hide file tree
Showing 11 changed files with 79 additions and 0 deletions.
9 changes: 9 additions & 0 deletions api/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
}
Expand Down Expand Up @@ -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 {
Expand Down
8 changes: 8 additions & 0 deletions calculator.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
6 changes: 6 additions & 0 deletions eval.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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{}))
Expand Down
6 changes: 6 additions & 0 deletions kubedb.com/v1alpha2/elasticsearch.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
}
Expand Down Expand Up @@ -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")
Expand Down
6 changes: 6 additions & 0 deletions kubedb.com/v1alpha2/mariadb.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
}
Expand Down Expand Up @@ -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")
Expand Down
6 changes: 6 additions & 0 deletions kubedb.com/v1alpha2/memcached.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
}
Expand Down Expand Up @@ -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")
Expand Down
6 changes: 6 additions & 0 deletions kubedb.com/v1alpha2/mongodb.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
}
Expand Down Expand Up @@ -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")
Expand Down
6 changes: 6 additions & 0 deletions kubedb.com/v1alpha2/mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
}
Expand Down Expand Up @@ -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")
Expand Down
6 changes: 6 additions & 0 deletions kubedb.com/v1alpha2/postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
}
Expand Down Expand Up @@ -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")
Expand Down
6 changes: 6 additions & 0 deletions kubedb.com/v1alpha2/redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
}
Expand Down Expand Up @@ -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")
Expand Down
14 changes: 14 additions & 0 deletions template.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 0b13ba5

Please sign in to comment.