Skip to content

Commit f7fcba5

Browse files
committed
Support calling into executor pods directly
If suitable function pods are already running, we can call into them directly. This is essentially a proof-of-concept; in practice we will want to launch these pods ourselves / manage them etc.
1 parent 9defc18 commit f7fcba5

File tree

8 files changed

+214
-8
lines changed

8 files changed

+214
-8
lines changed

porch/apiserver/pkg/apiserver/apiserver.go

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -168,13 +168,21 @@ func (c completedConfig) New() (*PorchServer, error) {
168168
renderer := kpt.NewRenderer()
169169

170170
cache := cache.NewCache(c.ExtraConfig.CacheDirectory, credentialResolver)
171-
cad, err := engine.NewCaDEngine(
171+
options := []engine.EngineOption{
172172
engine.WithCache(cache),
173-
engine.WithGRPCFunctionRuntime(c.ExtraConfig.FunctionRunnerAddress),
174173
engine.WithCredentialResolver(credentialResolver),
175174
engine.WithRenderer(renderer),
176175
engine.WithReferenceResolver(referenceResolver),
177-
)
176+
}
177+
178+
if c.ExtraConfig.FunctionRunnerAddress != "" {
179+
options = append(options, engine.WithGRPCFunctionRuntime(c.ExtraConfig.FunctionRunnerAddress))
180+
} else {
181+
ns := "porch-functions-system"
182+
options = append(options, engine.WithKubeFunctionRuntime(coreClient, ns))
183+
}
184+
185+
cad, err := engine.NewCaDEngine(options...)
178186
if err != nil {
179187
return nil, err
180188
}

porch/config/deploy/3-porch-server.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ spec:
5353
- mountPath: /cache
5454
name: cache-volume
5555
args:
56-
- --function-runner=function-runner:9445
56+
#- --function-runner=function-runner:9445
5757
- --cache-directory=/cache
5858

5959
---

porch/config/deploy/5-rbac.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,7 @@ rules:
3030
- apiGroups: ["flowcontrol.apiserver.k8s.io"]
3131
resources: ["flowschemas", "prioritylevelconfigurations"]
3232
verbs: ["get", "watch", "list"]
33+
# Needed to launch / read executor pods
34+
- apiGroups: [""]
35+
resources: ["pods"]
36+
verbs: ["get", "watch", "list"]

porch/engine/go.mod

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,10 @@ require (
1313
github.com/go-git/go-git/v5 v5.4.3-0.20220119145113-935af59cf64f
1414
github.com/google/go-cmp v0.5.7
1515
google.golang.org/grpc v1.44.0
16+
k8s.io/api v0.23.2
1617
k8s.io/apimachinery v0.23.2
1718
k8s.io/klog/v2 v2.40.1
19+
sigs.k8s.io/controller-runtime v0.11.0
1820
sigs.k8s.io/kustomize/kyaml v0.13.3
1921
)
2022

@@ -103,7 +105,6 @@ require (
103105
gopkg.in/warnings.v0 v0.1.2 // indirect
104106
gopkg.in/yaml.v2 v2.4.0 // indirect
105107
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect
106-
k8s.io/api v0.23.2 // indirect
107108
k8s.io/cli-runtime v0.23.2 // indirect
108109
k8s.io/client-go v0.23.2 // indirect
109110
k8s.io/component-base v0.23.2 // indirect

porch/engine/go.sum

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ github.com/benbjohnson/clock v1.1.0/go.mod h1:J11/hYXuz8f4ySSvYwY0FKfm+ezbsZBKZx
133133
github.com/beorn7/perks v0.0.0-20160804104726-4c0e84591b9a/go.mod h1:Dwedo/Wpr24TaqPxmxbtue+5NUziq4I4S80YR8gNf3Q=
134134
github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24TaqPxmxbtue+5NUziq4I4S80YR8gNf3Q=
135135
github.com/beorn7/perks v1.0.0/go.mod h1:KWe93zE9D1o94FZ5RNwFwVgaQK1VOXiVxmqh+CedLV8=
136+
github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM=
136137
github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw=
137138
github.com/bgentry/speakeasy v0.1.0/go.mod h1:+zsyZBPWlz7T6j88CTgSN5bM796AkVf0kBD4zp0CCIs=
138139
github.com/bitly/go-simplejson v0.5.0/go.mod h1:cXHtHw4XUPsvGaxgjIAn8PhEWG9NfngEKAMDJEczWVA=
@@ -152,8 +153,10 @@ github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA
152153
github.com/census-instrumentation/opencensus-proto v0.3.0/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
153154
github.com/certifi/gocertifi v0.0.0-20191021191039-0944d244cd40/go.mod h1:sGbDF6GwGcLpkNXPUTkMRoywsNa/ol15pxFe6ERfguA=
154155
github.com/certifi/gocertifi v0.0.0-20200922220541-2c3bb06c6054/go.mod h1:sGbDF6GwGcLpkNXPUTkMRoywsNa/ol15pxFe6ERfguA=
156+
github.com/cespare/xxhash v1.1.0 h1:a6HrQnmkObjyL+Gs60czilIUGqrzKutQD6XZog3p+ko=
155157
github.com/cespare/xxhash v1.1.0/go.mod h1:XrSqR1VqqWfGrhpAt58auRo0WTKS1nRRg3ghfAqPWnc=
156158
github.com/cespare/xxhash/v2 v2.1.1/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
159+
github.com/cespare/xxhash/v2 v2.1.2 h1:YRXhKfTDauu4ajMg1TPgFO5jnlC2HCbmLXMcTG5cbYE=
157160
github.com/cespare/xxhash/v2 v2.1.2/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
158161
github.com/chai2010/gettext-go v0.0.0-20160711120539-c6fed771bfd5 h1:7aWHqerlJ41y6FOsEUvknqgXnGmJyJSbjhAWq5pO4F8=
159162
github.com/chai2010/gettext-go v0.0.0-20160711120539-c6fed771bfd5/go.mod h1:/iP1qXHoty45bqomnu2LM+VVyAEdWN+vtSHGlQgyxbw=
@@ -401,6 +404,7 @@ github.com/go-logr/logr v0.2.0/go.mod h1:z6/tIYblkpsD+a4lm/fGIIU9mZ+XfAiaFtq7xTg
401404
github.com/go-logr/logr v1.2.0/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A=
402405
github.com/go-logr/logr v1.2.1 h1:DX7uPQ4WgAWfoh+NGGlbJQswnYIVvz0SRlLS3rPZQDA=
403406
github.com/go-logr/logr v1.2.1/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A=
407+
github.com/go-logr/zapr v1.2.0 h1:n4JnPI1T3Qq1SFEi/F8rwLrZERp2bso19PJZDB9dayk=
404408
github.com/go-logr/zapr v1.2.0/go.mod h1:Qa4Bsj2Vb+FAVeAKsLD8RLQ+YRJB8YDmOAKxaBQf7Ro=
405409
github.com/go-openapi/jsonpointer v0.19.2/go.mod h1:3akKfEdA7DF1sugOqz1dVQHBcuDBPKZGEoHC/NkiQRg=
406410
github.com/go-openapi/jsonpointer v0.19.3/go.mod h1:Pl9vOtqEWErmShwVjC8pYs9cog34VGT37dQOVbmoatg=
@@ -676,6 +680,7 @@ github.com/mattn/go-runewidth v0.0.2/go.mod h1:LwmH8dsx7+W8Uxz3IHJYH5QSwggIsqBzp
676680
github.com/mattn/go-runewidth v0.0.7/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI=
677681
github.com/mattn/go-shellwords v1.0.3/go.mod h1:3xCvwCdWdlDJUrvuMn7Wuy9eWs4pE8vqg+NOMyg4B2o=
678682
github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0=
683+
github.com/matttproud/golang_protobuf_extensions v1.0.2-0.20181231171920-c182affec369 h1:I0XW9+e1XWDxdcEniV4rQAIOPUGDq67JSCiRCgGCZLI=
679684
github.com/matttproud/golang_protobuf_extensions v1.0.2-0.20181231171920-c182affec369/go.mod h1:BSXmuO+STAnVfrANrmjBb36TMTDstsz7MSK+HVaYKv4=
680685
github.com/miekg/dns v1.0.14/go.mod h1:W1PPwlIAgtquWBMBEV9nkV9Cazfe8ScdGz/Lj7v3Nrg=
681686
github.com/miekg/dns v1.1.26/go.mod h1:bPDLeHnStXmXAq1m/Ch/hvfNHr14JKNPMBo3VZKjuso=
@@ -812,11 +817,13 @@ github.com/prometheus/client_golang v1.0.0/go.mod h1:db9x61etRT2tGnBNRi70OPL5Fsn
812817
github.com/prometheus/client_golang v1.1.0/go.mod h1:I1FGZT9+L76gKKOs5djB6ezCbFQP1xR9D75/vuwEF3g=
813818
github.com/prometheus/client_golang v1.4.0/go.mod h1:e9GMxYsXl05ICDXkRhurwBS4Q3OK1iX/F2sw+iXX5zU=
814819
github.com/prometheus/client_golang v1.7.1/go.mod h1:PY5Wy2awLA44sXw4AOSfFBetzPP4j5+D6mVACh+pe2M=
820+
github.com/prometheus/client_golang v1.11.0 h1:HNkLOAEQMIDv/K+04rukrLx6ch7msSRwf3/SASFAGtQ=
815821
github.com/prometheus/client_golang v1.11.0/go.mod h1:Z6t4BnS23TR94PD6BsDNk8yVqroYurpAkEiz0P2BEV0=
816822
github.com/prometheus/client_model v0.0.0-20171117100541-99fa1f4be8e5/go.mod h1:MbSGuTsp3dbXC40dX6PRTWyKYBIrTGTE9sqQNg2J8bo=
817823
github.com/prometheus/client_model v0.0.0-20180712105110-5c3871d89910/go.mod h1:MbSGuTsp3dbXC40dX6PRTWyKYBIrTGTE9sqQNg2J8bo=
818824
github.com/prometheus/client_model v0.0.0-20190129233127-fd36f4220a90/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
819825
github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
826+
github.com/prometheus/client_model v0.2.0 h1:uq5h0d+GuxiXLJLNABMgp2qUWDPiLvgCzz2dUR+/W/M=
820827
github.com/prometheus/client_model v0.2.0/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
821828
github.com/prometheus/common v0.0.0-20180110214958-89604d197083/go.mod h1:daVV7qP5qjZbuso7PdcryaAu0sAZbrN9i7WWcTMWvro=
822829
github.com/prometheus/common v0.0.0-20181113130724-41aa239b4cce/go.mod h1:daVV7qP5qjZbuso7PdcryaAu0sAZbrN9i7WWcTMWvro=
@@ -826,6 +833,7 @@ github.com/prometheus/common v0.6.0/go.mod h1:eBmuwkDJBwy6iBfxCBob6t6dR6ENT/y+J+
826833
github.com/prometheus/common v0.9.1/go.mod h1:yhUN8i9wzaXS3w1O07YhxHEBxD+W35wd8bs7vj7HSQ4=
827834
github.com/prometheus/common v0.10.0/go.mod h1:Tlit/dnDKsSWFlCLTWaA1cyBgKHSMdTB80sz/V91rCo=
828835
github.com/prometheus/common v0.26.0/go.mod h1:M7rCNAaPfAosfx8veZJCuw84e35h3Cfd9VFqTh1DIvc=
836+
github.com/prometheus/common v0.28.0 h1:vGVfV9KrDTvWt5boZO0I19g2E3CsWfpPPKZM9dt3mEw=
829837
github.com/prometheus/common v0.28.0/go.mod h1:vu+V0TpY+O6vW9J44gczi3Ap/oXXR10b+M/gUGO4Hls=
830838
github.com/prometheus/procfs v0.0.0-20180125133057-cb4147076ac7/go.mod h1:c3At6R/oaqEKCNdg8wHV1ftS6bRYblBhIjjI8uT2IGk=
831839
github.com/prometheus/procfs v0.0.0-20181005140218-185b4288413d/go.mod h1:c3At6R/oaqEKCNdg8wHV1ftS6bRYblBhIjjI8uT2IGk=
@@ -837,6 +845,7 @@ github.com/prometheus/procfs v0.0.5/go.mod h1:4A/X28fw3Fc593LaREMrKMqOKvUAntwMDa
837845
github.com/prometheus/procfs v0.0.8/go.mod h1:7Qr8sr6344vo1JqZ6HhLceV9o3AJ1Ff+GxbHq6oeK9A=
838846
github.com/prometheus/procfs v0.1.3/go.mod h1:lV6e/gmhEcM9IjHGsFOCxxuZ+z1YqCvr4OA4YeYWdaU=
839847
github.com/prometheus/procfs v0.2.0/go.mod h1:lV6e/gmhEcM9IjHGsFOCxxuZ+z1YqCvr4OA4YeYWdaU=
848+
github.com/prometheus/procfs v0.6.0 h1:mxy4L2jP6qMonqmq+aTtOx1ifVWUgG/TAmntgbh3xv4=
840849
github.com/prometheus/procfs v0.6.0/go.mod h1:cz+aTbrPOrUb4q7XlbU9ygM+/jj0fzG6c1xBZuNvfVA=
841850
github.com/prometheus/tsdb v0.7.1/go.mod h1:qhTCs0VvXwvX/y3TZrWD7rabWM+ijKTux40TwIPHuXU=
842851
github.com/rogpeppe/fastuuid v0.0.0-20150106093220-6724a57986af/go.mod h1:XWv6SoW27p1b0cqNHllgS5HIMJraePCO15w5zCzIWYg=
@@ -1001,15 +1010,18 @@ go.starlark.net v0.0.0-20200306205701-8dd3e2ee1dd5 h1:+FNtrFTmVw0YZGpBGX56XDee33
10011010
go.starlark.net v0.0.0-20200306205701-8dd3e2ee1dd5/go.mod h1:nmDLcffg48OtT/PSW0Hg7FvpRQsQh5OSqIylirxKC7o=
10021011
go.uber.org/atomic v1.3.2/go.mod h1:gD2HeocX3+yG+ygLZcrzQJaqmWj9AIm7n08wl/qW/PE=
10031012
go.uber.org/atomic v1.4.0/go.mod h1:gD2HeocX3+yG+ygLZcrzQJaqmWj9AIm7n08wl/qW/PE=
1013+
go.uber.org/atomic v1.7.0 h1:ADUqmZGgLDDfbSL9ZmPxKTybcoEYHgpYfELNoN+7hsw=
10041014
go.uber.org/atomic v1.7.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc=
10051015
go.uber.org/goleak v1.1.10/go.mod h1:8a7PlsEVH3e/a/GLqe5IIrQx6GzcnRmZEufDUTk4A7A=
10061016
go.uber.org/goleak v1.1.11-0.20210813005559-691160354723/go.mod h1:cwTWslyiVhfpKIDGSZEM2HlOvcqm+tG4zioyIeLoqMQ=
10071017
go.uber.org/goleak v1.1.12/go.mod h1:cwTWslyiVhfpKIDGSZEM2HlOvcqm+tG4zioyIeLoqMQ=
10081018
go.uber.org/multierr v1.1.0/go.mod h1:wR5kodmAFQ0UK8QlbwjlSNy0Z68gJhDJUG5sjR94q/0=
1019+
go.uber.org/multierr v1.6.0 h1:y6IPFStTAIT5Ytl7/XYmHvzXQ7S3g/IeZW9hyZ5thw4=
10091020
go.uber.org/multierr v1.6.0/go.mod h1:cdWPpRnG4AhwMwsgIHip0KRBQjJy5kYEpYjJxpXp9iU=
10101021
go.uber.org/zap v1.10.0/go.mod h1:vwi/ZaCAaUcBkycHslxD9B2zi4UTXhF60s6SWpuDF0Q=
10111022
go.uber.org/zap v1.17.0/go.mod h1:MXVU+bhUf/A7Xi2HNOnopQOrmycQ5Ih87HtOu4q5SSo=
10121023
go.uber.org/zap v1.19.0/go.mod h1:xg/QME4nWcxGxrpdeYfq7UvYrLh66cuVKdrbD1XF/NI=
1024+
go.uber.org/zap v1.19.1 h1:ue41HOKd1vGURxrmeKIgELGb3jPW9DMUDGtsinblHwI=
10131025
go.uber.org/zap v1.19.1/go.mod h1:j3DNczoxDZroyBnOT1L/Q79cfUMGZxlv/9dzN7SM1rI=
10141026
golang.org/x/crypto v0.0.0-20171113213409-9f005a07e0d3/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
10151027
golang.org/x/crypto v0.0.0-20180904163835-0709b304e793/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
@@ -1364,6 +1376,7 @@ golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8T
13641376
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
13651377
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 h1:go1bK/D/BFZV2I8cIQd1NKEZ+0owSTG1fDTci4IqFcE=
13661378
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
1379+
gomodules.xyz/jsonpatch/v2 v2.2.0 h1:4pT439QV83L+G9FkcCriY6EkpcK6r6bK+A5FBUMI7qY=
13671380
gomodules.xyz/jsonpatch/v2 v2.2.0/go.mod h1:WXp+iVDkoLQqPudfQ9GBlwB2eZ5DKOnjQZCYdOS8GPY=
13681381
google.golang.org/api v0.0.0-20160322025152-9bf6e6e569ff/go.mod h1:4mhQ8q/RsB7i+udVvVy5NUi08OU8ZlA0gRVgrF7VFY0=
13691382
google.golang.org/api v0.4.0/go.mod h1:8k5glujaEP+g9n7WNsDg8QP6cUVNI86fCNMcbazEtwE=
@@ -1587,6 +1600,7 @@ k8s.io/api v0.23.0/go.mod h1:8wmDdLBHBNxtOIytwLstXt5E9PddnZb0GaMcqsvDBpg=
15871600
k8s.io/api v0.23.2 h1:62cpzreV3dCuj0hqPi8r4dyWh48ogMcyh+ga9jEGij4=
15881601
k8s.io/api v0.23.2/go.mod h1:sYuDb3flCtRPI8ghn6qFrcK5ZBu2mhbElxRE95qpwlI=
15891602
k8s.io/apiextensions-apiserver v0.23.0/go.mod h1:xIFAEEDlAZgpVBl/1VSjGDmLoXAWRG40+GsWhKhAxY4=
1603+
k8s.io/apiextensions-apiserver v0.23.2 h1:N6CIVAhmF0ahgFKUMDdV/AUyckhUb4nIyVPohPtdUPk=
15901604
k8s.io/apiextensions-apiserver v0.23.2/go.mod h1:9cs7avT6+GfzbB0pambTvH11wcaR85QQg4ovl9s15UU=
15911605
k8s.io/apimachinery v0.20.1/go.mod h1:WlLqWAHZGg07AeltaI0MV5uk1Omp8xaN0JGLY6gkRpU=
15921606
k8s.io/apimachinery v0.20.4/go.mod h1:WlLqWAHZGg07AeltaI0MV5uk1Omp8xaN0JGLY6gkRpU=
@@ -1651,6 +1665,7 @@ sigs.k8s.io/apiserver-network-proxy/konnectivity-client v0.0.25/go.mod h1:Mlj9PN
16511665
sigs.k8s.io/apiserver-network-proxy/konnectivity-client v0.0.27/go.mod h1:tq2nT0Kx7W+/f2JVE+zxYtUhdjuELJkVpNz+x/QN5R4=
16521666
sigs.k8s.io/cli-utils v0.29.2 h1:SaYo2C1xd0MVv65NQXZ6tIqT1W1iWy8CGmC+VnxQGWs=
16531667
sigs.k8s.io/cli-utils v0.29.2/go.mod h1:WDVRa5/eQBKntG++uyKdyT+xU7MLdCR4XsgseqL5uX4=
1668+
sigs.k8s.io/controller-runtime v0.11.0 h1:DqO+c8mywcZLFJWILq4iktoECTyn30Bkj0CwgqMpZWQ=
16541669
sigs.k8s.io/controller-runtime v0.11.0/go.mod h1:KKwLiTooNGu+JmLZGn9Sl3Gjmfj66eMbCQznLP5zcqA=
16551670
sigs.k8s.io/json v0.0.0-20211020170558-c049b76a60c6/go.mod h1:p4QtZmO4uMYipTQNzagwnNoseA6OxSUutVw05NhYDRs=
16561671
sigs.k8s.io/json v0.0.0-20211208200746-9f7c6b3444d2 h1:kDi4JBNAsJWfz1aEXhO8Jg87JJaPNLh5tIzYHgStQ9Y=
Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
// Copyright 2022 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package engine
16+
17+
import (
18+
"context"
19+
"fmt"
20+
"io"
21+
"io/ioutil"
22+
"strings"
23+
24+
v1 "github.com/GoogleContainerTools/kpt/pkg/api/kptfile/v1"
25+
"github.com/GoogleContainerTools/kpt/pkg/fn"
26+
"github.com/GoogleContainerTools/kpt/porch/engine/pkg/kpt"
27+
"github.com/GoogleContainerTools/kpt/porch/func/evaluator"
28+
"google.golang.org/grpc"
29+
"google.golang.org/grpc/credentials/insecure"
30+
corev1 "k8s.io/api/core/v1"
31+
"k8s.io/klog/v2"
32+
"sigs.k8s.io/controller-runtime/pkg/client"
33+
)
34+
35+
func NewKubeFunctionRuntime(kubeClient client.Client, namespace string) (*kubeFunctionRuntime, error) {
36+
return &kubeFunctionRuntime{
37+
kubeClient: kubeClient,
38+
namespace: namespace,
39+
}, nil
40+
}
41+
42+
type kubeFunctionRuntime struct {
43+
// kubeClient is the kubernetes client
44+
kubeClient client.Client
45+
46+
// namespace holds the namespace where the executors run
47+
namespace string
48+
}
49+
50+
var _ kpt.FunctionRuntime = &kubeFunctionRuntime{}
51+
52+
func (k *kubeFunctionRuntime) GetRunner(ctx context.Context, fn *v1.Function) (fn.FunctionRunner, error) {
53+
image := fn.Image
54+
55+
tokens := strings.SplitN(image, ":", 2)
56+
if len(tokens) != 2 {
57+
// TODO: Assume latest?
58+
return nil, fmt.Errorf("expected version in image %q", image)
59+
}
60+
61+
functionName := lastComponent(tokens[0])
62+
63+
matchLabels := map[string]string{
64+
"functions.porch.kpt.dev/" + functionName: "",
65+
}
66+
// Work around limits of annotation/label keys/values
67+
matchAnnotations := map[string]string{
68+
"functions.porch.kpt.dev/" + functionName: image,
69+
}
70+
71+
var pods corev1.PodList
72+
73+
var options []client.ListOption
74+
options = append(options, client.InNamespace(k.namespace))
75+
options = append(options, client.MatchingLabels(matchLabels))
76+
if err := k.kubeClient.List(ctx, &pods, options...); err != nil {
77+
return nil, fmt.Errorf("error listing pods: %w", err)
78+
}
79+
80+
// TODO: we should launch/manage the pods, rather than trusting the labels
81+
for i := range pods.Items {
82+
pod := &pods.Items[i]
83+
if pod.Status.Phase != "Running" {
84+
continue
85+
}
86+
87+
matchesAnnotations := true
88+
for k, v := range matchAnnotations {
89+
if pod.Annotations[k] != v {
90+
matchesAnnotations = false
91+
break
92+
}
93+
}
94+
if !matchesAnnotations {
95+
break
96+
}
97+
return &kubeFunctionRunner{
98+
pod: pod,
99+
image: image,
100+
}, nil
101+
}
102+
103+
return nil, fmt.Errorf("could not find pod to run function %q", image)
104+
}
105+
106+
func lastComponent(s string) string {
107+
lastSlash := strings.LastIndex(s, "/")
108+
return s[lastSlash+1:]
109+
}
110+
111+
func (k *kubeFunctionRuntime) Close() error {
112+
return nil
113+
}
114+
115+
type kubeFunctionRunner struct {
116+
pod *corev1.Pod
117+
image string
118+
}
119+
120+
var _ fn.FunctionRunner = &kubeFunctionRunner{}
121+
122+
func (k *kubeFunctionRunner) Run(r io.Reader, w io.Writer) error {
123+
// We shouldn't be putting this into the runner
124+
ctx := context.TODO()
125+
126+
address := k.pod.Status.PodIP
127+
if address == "" {
128+
return fmt.Errorf("pod did not have podIP")
129+
}
130+
address += ":8888"
131+
132+
klog.Infof("dialing grpc function runner %q", address)
133+
134+
// TODO: pool connections
135+
cc, err := grpc.Dial(address, grpc.WithTransportCredentials(insecure.NewCredentials()))
136+
if err != nil {
137+
return fmt.Errorf("failed to dial grpc function evaluator on %q for pod %s/%s: %w", address, k.pod.Namespace, k.pod.Name, err)
138+
}
139+
defer func() {
140+
if err := cc.Close(); err != nil {
141+
klog.Warningf("failed to close grpc connection: %v", err)
142+
}
143+
}()
144+
145+
client := evaluator.NewFunctionEvaluatorClient(cc)
146+
147+
in, err := ioutil.ReadAll(r)
148+
if err != nil {
149+
return fmt.Errorf("failed to read function runner input: %w", err)
150+
}
151+
152+
res, err := client.EvaluateFunction(ctx, &evaluator.EvaluateFunctionRequest{
153+
ResourceList: in,
154+
Image: k.image,
155+
})
156+
if err != nil {
157+
return fmt.Errorf("func eval failed: %w", err)
158+
}
159+
if _, err := w.Write(res.ResourceList); err != nil {
160+
return fmt.Errorf("failed to write function runner output: %w", err)
161+
}
162+
return nil
163+
}

porch/engine/pkg/engine/options.go

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525
"google.golang.org/grpc"
2626
"google.golang.org/grpc/credentials/insecure"
2727
"k8s.io/klog/v2"
28+
"sigs.k8s.io/controller-runtime/pkg/client"
2829
)
2930

3031
type EngineOption interface {
@@ -48,11 +49,24 @@ func WithCache(cache *cache.Cache) EngineOption {
4849

4950
func WithGRPCFunctionRuntime(address string) EngineOption {
5051
return EngineOptionFunc(func(engine *cadEngine) error {
51-
runtime, err := createFunctionRuntime(address)
52+
runtime, err := createGRPCFunctionRuntime(address)
5253
if err != nil {
5354
return fmt.Errorf("failed to create function runtime: %w", err)
5455
}
55-
engine.runtime = runtime
56+
withBuiltins := &builtinFunctionRuntime{fallback: runtime}
57+
engine.runtime = withBuiltins
58+
return nil
59+
})
60+
}
61+
62+
func WithKubeFunctionRuntime(coreClient client.Client, ns string) EngineOption {
63+
return EngineOptionFunc(func(engine *cadEngine) error {
64+
runtime, err := NewKubeFunctionRuntime(coreClient, ns)
65+
if err != nil {
66+
return fmt.Errorf("failed to create function runtime: %w", err)
67+
}
68+
withBuiltins := &builtinFunctionRuntime{fallback: runtime}
69+
engine.runtime = withBuiltins
5670
return nil
5771
})
5872
}
@@ -92,7 +106,7 @@ func WithReferenceResolver(resolver ReferenceResolver) EngineOption {
92106
})
93107
}
94108

95-
func createFunctionRuntime(address string) (kpt.FunctionRuntime, error) {
109+
func createGRPCFunctionRuntime(address string) (kpt.FunctionRuntime, error) {
96110
if address == "" {
97111
return nil, fmt.Errorf("address is required to instantiate gRPC function runtime")
98112
}

porch/hack/Dockerfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ COPY porch/controllers/go.mod porch/controllers/go.sum porch/controllers/
2727
COPY porch/engine/go.mod porch/engine/go.mod porch/engine/
2828
COPY porch/repository/go.mod porch/repository/go.sum porch/repository/
2929
COPY porch/func/go.mod porch/func/go.sum porch/func/
30+
COPY porch/functions/go.mod porch/functions/go.sum porch/functions/
3031

3132
RUN echo "Downloading root modules ..." \
3233
&& go mod download

0 commit comments

Comments
 (0)