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

Commit

Permalink
Merge pull request #19 from flanksource/for-each
Browse files Browse the repository at this point in the history
For_each field
  • Loading branch information
moshloop authored Jan 18, 2021
2 parents 8fa866e + 375f87b commit 029fa99
Show file tree
Hide file tree
Showing 11 changed files with 420 additions and 17 deletions.
1 change: 1 addition & 0 deletions api/v1/template_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ type TemplateStatus struct {
}

type ResourceSelector struct {
ForEach string `json:"forEach,omitempty"`
LabelSelector metav1.LabelSelector `json:"labelSelector,omitempty"`
NamespaceSelector metav1.LabelSelector `json:"namespaceSelector,omitempty"`
AnnotationSelector map[string]string `json:"annotationSelector,omitempty"`
Expand Down
4 changes: 4 additions & 0 deletions config/crd/bases/templating.flanksource.com_templates.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ spec:
type: string
fieldSelector:
type: string
forEach:
type: string
kind:
type: string
labelSelector:
Expand Down Expand Up @@ -206,6 +208,8 @@ spec:
type: string
fieldSelector:
type: string
forEach:
type: string
kind:
type: string
labelSelector:
Expand Down
4 changes: 4 additions & 0 deletions config/deploy/crd.yml
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ spec:
type: string
fieldSelector:
type: string
forEach:
type: string
kind:
type: string
labelSelector:
Expand Down Expand Up @@ -151,6 +153,8 @@ spec:
type: string
fieldSelector:
type: string
forEach:
type: string
kind:
type: string
labelSelector:
Expand Down
4 changes: 4 additions & 0 deletions config/deploy/operator.yml
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ spec:
type: string
fieldSelector:
type: string
forEach:
type: string
kind:
type: string
labelSelector:
Expand Down Expand Up @@ -158,6 +160,8 @@ spec:
type: string
fieldSelector:
type: string
forEach:
type: string
kind:
type: string
labelSelector:
Expand Down
16 changes: 16 additions & 0 deletions examples/for-each-test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
apiVersion: abcd.flanksource.com/v1
kind: ABCD
metadata:
name: test
namespace: default
spec:
topics:
- a
- b
- c
- d
topicsMap:
a1: a2
b1: b2
c1: c2
d1: d2
89 changes: 89 additions & 0 deletions examples/for-each.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
apiVersion: apiextensions.k8s.io/v1beta1
kind: CustomResourceDefinition
metadata:
name: abcds.abcd.flanksource.com
spec:
group: abcd.flanksource.com
names:
kind: ABCD
listKind: ABCDList
plural: abcds
singular: abcd
scope: Namespaced
subresources:
status: {}
version: v1
versions:
- name: v1
served: true
storage: true
validation:
openAPIV3Schema:
description: Schema validation for the ABCD CRDs
type: object
properties:
spec:
type: object
---
apiVersion: apiextensions.k8s.io/v1beta1
kind: CustomResourceDefinition
metadata:
name: abcdtopics.abcd.flanksource.com
spec:
group: abcd.flanksource.com
names:
kind: ABCDTopic
listKind: ABCDTopicList
plural: abcdtopics
singular: abcdtopic
scope: Namespaced
subresources:
status: {}
version: v1
versions:
- name: v1
served: true
storage: true
validation:
openAPIV3Schema:
description: Schema validation for the ABCD Topic CRDs
type: object
properties:
spec:
type: object
---
apiVersion: templating.flanksource.com/v1
kind: Template
metadata:
name: abcd-topic
spec:
source:
apiVersion: abcd.flanksource.com/v1
kind: ABCD
resources:
- forEach: "{{.spec.topics}}"
apiVersion: abcd.flanksource.com/v1
kind: ABCDTopic
metadata:
name: "{{.metadata.name}}-{{.each}}"
namespace: "{{.metadata.namespace}}"
spec:
topicName: "{{.each}}"
---
apiVersion: templating.flanksource.com/v1
kind: Template
metadata:
name: abcd-topic-map
spec:
source:
apiVersion: abcd.flanksource.com/v1
kind: ABCD
resources:
- forEach: "{{.spec.topicsMap}}"
apiVersion: abcd.flanksource.com/v1
kind: ABCDTopic
metadata:
name: "{{.metadata.name}}-{{.each.key}}"
namespace: "{{.metadata.namespace}}"
spec:
"{{.each.key}}": "{{.each.value}}"
22 changes: 12 additions & 10 deletions k8s/schema_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"strconv"
"strings"

"github.com/flanksource/commons/logger"
"github.com/go-logr/logr"
"github.com/go-openapi/jsonpointer"
"github.com/go-openapi/spec"
"github.com/pkg/errors"
Expand All @@ -25,6 +25,7 @@ type SchemaManager struct {
serverResources []*metav1.APIResourceList
clientset *kubernetes.Clientset
crdClient extapi.ApiextensionsV1beta1Interface
log logr.Logger
}

type TypedField struct {
Expand All @@ -34,7 +35,7 @@ type TypedField struct {

type MapInterface map[string]interface{}

func NewSchemaManagerWithCache(clientset *kubernetes.Clientset, crdClient extapi.ApiextensionsV1beta1Interface, cache *SchemaCache) (*SchemaManager, error) {
func NewSchemaManagerWithCache(clientset *kubernetes.Clientset, crdClient extapi.ApiextensionsV1beta1Interface, cache *SchemaCache, log logr.Logger) (*SchemaManager, error) {
s, err := cache.FetchSchema()
if err != nil {
return nil, err
Expand All @@ -50,11 +51,12 @@ func NewSchemaManagerWithCache(clientset *kubernetes.Clientset, crdClient extapi
serverResources: serverResources,
clientset: clientset,
crdClient: crdClient,
log: log,
}
return mgr, nil
}

func NewSchemaManager(clientset *kubernetes.Clientset, crdClient extapi.ApiextensionsV1beta1Interface) (*SchemaManager, error) {
func NewSchemaManager(clientset *kubernetes.Clientset, crdClient extapi.ApiextensionsV1beta1Interface, log logr.Logger) (*SchemaManager, error) {
bs, err := clientset.RESTClient().Get().AbsPath("openapi", "v2").DoRaw(context.TODO())
if err != nil {
return nil, err
Expand All @@ -75,6 +77,7 @@ func NewSchemaManager(clientset *kubernetes.Clientset, crdClient extapi.Apiexten
serverResources: serverResources,
clientset: clientset,
crdClient: crdClient,
log: log,
}
return mgr, nil
}
Expand All @@ -97,7 +100,6 @@ 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
}
Expand All @@ -109,7 +111,7 @@ func (m *SchemaManager) duckType(schema *spec.Schema, object interface{}, prefix
if ok {
fieldType, err := m.FindTypeForKeyFromSchema(schema, prefix)
if err != nil {
logger.Errorf("failed to find type for key %s: %v", prefix, err)
m.log.V(2).Info("failed to find type for", "key", prefix, "error", err)
return bytes, nil
}
return transformBytesToType(bytes, fieldType)
Expand Down Expand Up @@ -145,7 +147,7 @@ func (m *SchemaManager) duckType(schema *spec.Schema, object interface{}, prefix
value := object.(string)
fieldType, err := m.FindTypeForKeyFromSchema(schema, prefix)
if err != nil {
logger.Errorf("failed to find type for key %s: %v", prefix, err)
m.log.V(2).Info("failed to find type for", "key", prefix, "error", err)
return value, nil
}
newValue, err := transformStringToType(value, fieldType)
Expand All @@ -157,7 +159,7 @@ func (m *SchemaManager) duckType(schema *spec.Schema, object interface{}, prefix
value := v.Int()
fieldType, err := m.FindTypeForKeyFromSchema(schema, prefix)
if err != nil {
logger.Errorf("failed to find type for key %s: %v", prefix, err)
m.log.V(2).Info("failed to find type for", "key", prefix, "error", err)
return value, nil
}
newValue, err := transformInt64ToType(value, fieldType)
Expand All @@ -169,7 +171,7 @@ func (m *SchemaManager) duckType(schema *spec.Schema, object interface{}, prefix
value := v.Uint()
fieldType, err := m.FindTypeForKeyFromSchema(schema, prefix)
if err != nil {
logger.Errorf("failed to find type for key %s: %v", prefix, err)
m.log.V(2).Info("failed to find type for", "key", prefix, "error", err)
return value, nil
}
newValue, err := transformUint64ToType(value, fieldType)
Expand All @@ -181,7 +183,7 @@ func (m *SchemaManager) duckType(schema *spec.Schema, object interface{}, prefix
value := int64(v.Float())
fieldType, err := m.FindTypeForKeyFromSchema(schema, prefix)
if err != nil {
logger.Errorf("failed to find type for key %s: %v", prefix, err)
m.log.V(2).Info("failed to find type for", "key", prefix, "error", err)
return value, nil
}
newValue, err := transformInt64ToType(value, fieldType)
Expand All @@ -193,7 +195,7 @@ func (m *SchemaManager) duckType(schema *spec.Schema, object interface{}, prefix
value := v.Bool()
fieldType, err := m.FindTypeForKeyFromSchema(schema, prefix)
if err != nil {
logger.Errorf("failed to find type for key %s: %v", prefix, err)
m.log.V(2).Info("failed to find type for", "key", prefix, "error", err)
return value, nil
}
newValue, err := transformBoolToType(value, fieldType)
Expand Down
2 changes: 1 addition & 1 deletion k8s/schema_manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,7 @@ func duckTypeWithValue(template string) (*unstructured.Unstructured, error) {
}

func newSchemaManager() *k8s.SchemaManager {
sm, err := k8s.NewSchemaManager(clientset(), crdClient())
sm, err := k8s.NewSchemaManager(clientset(), crdClient(), testLog)
if err != nil {
logger.Fatalf("failed to get schema manager: %v", err)
}
Expand Down
Loading

0 comments on commit 029fa99

Please sign in to comment.