Skip to content
This repository has been archived by the owner on Apr 18, 2024. It is now read-only.

Commit

Permalink
Add reflection for bool and nil fix postgresql template
Browse files Browse the repository at this point in the history
  • Loading branch information
teodor-pripoae committed Nov 26, 2020
1 parent b443789 commit a4750ee
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 2 deletions.
3 changes: 1 addition & 2 deletions examples/postgres-operator.yml
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,6 @@ spec:
configMap:
name: postgres-exporter-config
allowedSourceRanges: null
clone: null
dockerImage: docker.io/flanksource/spilo:1.6-p2.flanksource
enableShmVolume: true
env:
Expand Down Expand Up @@ -133,6 +132,7 @@ spec:
value: "{{.spec.backup.bucket}}"
- name: CLONE_WAL_BUCKET_SCOPE_SUFFIX
value: /
numberOfInstances: "{{ .spec.replicas }}"
patroni:
initdb:
data-checksums: "true"
Expand Down Expand Up @@ -192,7 +192,6 @@ spec:
requests:
cpu: 10m
memory: 128Mi
standby: null
teamId: postgres
tls: null
users:
Expand Down
16 changes: 16 additions & 0 deletions examples/postgresqldb.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
apiVersion: db.flanksource.com/v1
kind: PostgresqlDB
metadata:
name: test1
namespace: postgres-operator
spec:
replicas: 2
parameters:
max_connections: "1024"
shared_buffers: 4759MB
work_mem: 475MB
maintenance_work_mem: 634MB
storage:
storageClass: vsan
backup:
bucket: foo
41 changes: 41 additions & 0 deletions k8s/schema_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,9 @@ func (m *SchemaManager) DuckType(gvk schema.GroupVersionKind, object *unstructur

func (m *SchemaManager) duckType(schema *spec.Schema, object interface{}, prefix string) (interface{}, error) {
// fmt.Printf("Prefix: %s\n", prefix)
if isNil(object) {
return nil, nil
}

v := reflect.ValueOf(object)
switch v.Kind() {
Expand Down Expand Up @@ -166,6 +169,18 @@ func (m *SchemaManager) duckType(schema *spec.Schema, object interface{}, prefix
return nil, errors.Wrapf(err, "failed to transform string to type %v", fieldType)
}
return newValue, nil
case reflect.Bool:
value := v.Bool()
fieldType, err := m.FindTypeForKeyFromSchema(schema, prefix)
if err != nil {
logger.Errorf("failed to find type for key %s: %v", prefix, err)
return value, nil
}
newValue, err := transformBoolToType(value, fieldType)
if err != nil {
return nil, errors.Wrapf(err, "failed to transform bool to type %v", fieldType)
}
return newValue, nil
default:
return nil, errors.Errorf("Value for field %s for type %T could not be transformed: %d", prefix, v, v.Kind())
}
Expand Down Expand Up @@ -478,6 +493,21 @@ func transformBytesToType(value []byte, fieldType *TypedField) (interface{}, err
return nil, errors.Errorf("could not transform []byte value to types %v format %s", fieldType.Types, fieldType.Format)
}

func transformBoolToType(value bool, fieldType *TypedField) (interface{}, error) {
if contains(fieldType.Types, "boolean") {
return value, nil
}

if contains(fieldType.Types, "string") {
if value {
return "true", nil
}
return "false", nil
}

return nil, errors.Errorf("could not transform bool value to types %v format %s", fieldType.Types, fieldType.Format)
}

func contains(slice []string, value string) bool {
for _, k := range slice {
if k == value {
Expand All @@ -490,3 +520,14 @@ func contains(slice []string, value string) bool {
func escapeDot(s string) string {
return strings.ReplaceAll(s, ".", "_")
}

func isNil(i interface{}) bool {
if i == nil {
return true
}
switch reflect.TypeOf(i).Kind() {
case reflect.Ptr, reflect.Map, reflect.Array, reflect.Chan, reflect.Slice:
return reflect.ValueOf(i).IsNil()
}
return false
}
4 changes: 4 additions & 0 deletions k8s/template_manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,10 @@ metadata:
namespace: postgres-operator
spec:
numberOfInstances: "{{ .spec.replicas }}"
clone: null
postgresql:
parameters: "{{ .spec.parameters | data.ToJSON }}"
synchronous_mode: false
`
templateJSON, err := yaml.YAMLToJSON([]byte(template))
Expect(err).ToNot(HaveOccurred())
Expand All @@ -56,13 +58,15 @@ metadata:
name: postgres-test1
namespace: postgres-operator
spec:
clone: null
numberOfInstances: 2
postgresql:
parameters:
maintenance_work_mem: 634MB
max_connections: "1024"
shared_buffers: 4759MB
work_mem: 475MB
synchronous_mode: false
`

templateManager, err := k8s.NewTemplateManager(kommonsClient(), testLog)
Expand Down

0 comments on commit a4750ee

Please sign in to comment.