Skip to content

Commit d9a2e1b

Browse files
authored
Refactor function-pythonic render to enable unit tests to call it, (#38)
and add unit tests which run all examples. Signed-off-by: Patrick J. McNerthney <pat@mcnerthney.com>
1 parent 29ad09a commit d9a2e1b

60 files changed

Lines changed: 3130 additions & 1059 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ kind: Function
5757
metadata:
5858
name: function-pythonic
5959
spec:
60-
package: xpkg.upbound.io/crossplane-contrib/function-pythonic:v0.4.1
60+
package: xpkg.upbound.io/crossplane-contrib/function-pythonic:v0.4.2
6161
```
6262
6363
### Crossplane V1
@@ -69,7 +69,7 @@ kind: Function
6969
metadata:
7070
name: function-pythonic
7171
spec:
72-
package: xpkg.upbound.io/crossplane-contrib/function-pythonic:v0.4.1
72+
package: xpkg.upbound.io/crossplane-contrib/function-pythonic:v0.4.2
7373
runtimeConfigRef:
7474
name: function-pythonic
7575
--
@@ -215,6 +215,7 @@ The following functions are provided to create Protobuf structures:
215215
| List | Create a new Protobuf list |
216216
| Unknown | Create a new Protobuf unknown placeholder |
217217
| Yaml | Create a new Protobuf structure from a yaml string |
218+
| YamlAll | Create a new Protobuf list from a yaml string |
218219
| Json | Create a new Protobuf structure from a json string |
219220
| B64Encode | Encode a string into base 64 |
220221
| B64Decode | Decode a string from base 64 |
@@ -590,7 +591,7 @@ kind: Function
590591
metadata:
591592
name: function-pythonic
592593
spec:
593-
package: xpkg.upbound.io/crossplane-contrib/function-pythonic:v0.4.1
594+
package: xpkg.upbound.io/crossplane-contrib/function-pythonic:v0.4.2
594595
runtimeConfigRef:
595596
name: function-pythonic
596597
---

crossplane/pythonic/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11

22

33
from .composite import BaseComposite
4-
from .protobuf import append, Map, List, Unknown, Yaml, Json, B64Encode, B64Decode
4+
from .protobuf import append, Map, List, Unknown, Yaml, YamlAll, Json, B64Encode, B64Decode
55

66
__all__ = [
77
'BaseComposite',
@@ -10,6 +10,7 @@
1010
'List',
1111
'Unknown',
1212
'Yaml',
13+
'YamlAll',
1314
'Json',
1415
'B64Encode',
1516
'B64Decode',

crossplane/pythonic/command.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ def add_function_arguments(cls, parser):
6363
help='Enable Crossplane V1 compatibility mode',
6464
)
6565

66-
def __init__(self, args):
66+
def __init__(self, args=None):
6767
self.args = args
6868
self.initialize()
6969

@@ -79,6 +79,7 @@ def initialize_function(self):
7979
logger.setLevel(logging.DEBUG if self.args.debug else logging.INFO)
8080
# Suppress noisy libraries, these can be overriden using --logger-level
8181
logging.getLogger('asyncio').setLevel(logging.INFO)
82+
logging.getLogger('grpc').setLevel(logging.INFO)
8283
logging.getLogger('httpcore').setLevel(logging.INFO)
8384
logging.getLogger('httpx').setLevel(logging.WARNING)
8485
logging.getLogger('kr8s').setLevel(logging.INFO)

crossplane/pythonic/function.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,8 @@
1717
class FunctionRunner(grpcv1.FunctionRunnerService):
1818
"""A FunctionRunner handles gRPC RunFunctionRequests."""
1919

20-
def __init__(self, debug=False, renderUnknowns=False, crossplane_v1=False):
20+
def __init__(self, renderUnknowns=False, crossplane_v1=False):
2121
"""Create a new FunctionRunner."""
22-
self.debug = debug
2322
self.renderUnknowns = renderUnknowns
2423
self.crossplane_v1 = crossplane_v1
2524
self.clazzes = {}
@@ -182,7 +181,7 @@ def process_usages(self, composite):
182181
for _, resource in sorted(entry for entry in composite.resources):
183182
dependencies = resource.desired._getDependencies
184183
if dependencies:
185-
if self.debug:
184+
if composite.logger.isEnabledFor(logging.DEBUG):
186185
for destination, source in sorted(dependencies.items()):
187186
destination = self.trimFullName(destination)
188187
source = self.trimFullName(source)
@@ -278,7 +277,7 @@ def process_unknowns(self, composite):
278277
if resource.unknownsFatal or (resource.unknownsFatal is None and composite.unknownsFatal):
279278
fatalResources.append(name)
280279
fatal = True
281-
if self.debug:
280+
if composite.logger.isEnabledFor(logging.DEBUG):
282281
for destination, source in sorted(unknowns.items()):
283282
destination = self.trimFullName(destination)
284283
source = self.trimFullName(source)
@@ -319,7 +318,7 @@ def process_unknowns(self, composite):
319318
message = 'All resources are composed'
320319
status = True
321320
result = None
322-
if not self.debug and level:
321+
if not composite.logger.isEnabledFor(logging.DEBUG) and level:
323322
level(message)
324323
composite.conditions.ResourcesComposed(reason, message, status)
325324
if result:
@@ -378,6 +377,7 @@ def __init__(self):
378377
self.List = pythonic.List
379378
self.Unknown = pythonic.Unknown
380379
self.Yaml = pythonic.Yaml
380+
self.YamlAll = pythonic.YamlAll
381381
self.Json = pythonic.Json
382382
self.B64Encode = pythonic.B64Encode
383383
self.B64Decode = pythonic.B64Decode

crossplane/pythonic/grpc.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ def initialize(self):
8888

8989
async def run(self):
9090
grpc.aio.init_grpc_aio()
91-
grpc_runner = function.FunctionRunner(self.args.debug, self.args.render_unknowns, self.args.crossplane_v1)
91+
grpc_runner = function.FunctionRunner(self.args.render_unknowns, self.args.crossplane_v1)
9292
grpc_server = grpc.aio.server()
9393
grpcv1.add_FunctionRunnerServiceServicer_to_server(grpc_runner, grpc_server)
9494
if self.args.insecure:

crossplane/pythonic/protobuf.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,13 @@ def Yaml(string, readOnly=None):
4242
string = str(string)
4343
return Value(None, None, yaml.safe_load(string), readOnly)
4444

45+
def YamlAll(string, readOnly=None):
46+
if isinstance(string, (FieldMessage, Value)):
47+
if not string:
48+
return string
49+
string = str(string)
50+
return Value(None, None, [document for document in yaml.safe_load_all(string)], readOnly)
51+
4552
def Json(string, readOnly=None):
4653
if isinstance(string, (FieldMessage, Value)):
4754
if not string:

0 commit comments

Comments
 (0)