Skip to content

Commit 640a72a

Browse files
authored
Add raw deployment deployer (#3075)
* Add kubernetes deployment deployer * Run update-codegen.sh * Remove unneeded function * Address build issues * Run make schema-generate * Get logs by selector in integration_test This allows to get the logs of the deployment too * Add implementations for Lister, Describer and Remover for raw deployments * Use correct deploytype in integration test * tmp: Use http scheme for url in describer * Fix receiving pod logs correctly in test * Fix deployer integration test * Wait for pods of deployment to be ready too * Move Deployer, Describer, Lister and Remover int tests to correct package * Log receiver also check existing pods * Revert "Log receiver also check existing pods" This reverts commit a9bf274. * tmp: disable cleanup to get pod logs / status and add more logs * Revert "tmp: disable cleanup to get pod logs / status and add more logs" This reverts commit b498724. * Give a bit more time to collect logs * Add multi deployer, -lister, -remover * Move deployer, lister, describer and removers in separate packages * Use integration tests in k8s packages too * Fix import cycle issue * Fix integration tests * Remove unneeded test * Run all deployer integration tests on both deployer implementations * Add small delay in test * Revert "Add small delay in test" This reverts commit 2aec3f4. * Wait for deployments to be up * wait for ready pods instead of checking only if containers are running * tmp: collect pod status and logs * Revert "tmp: collect pod status and logs" This reverts commit 8bc2a0b. * Add deploy-type information in description * Poll for pod logs instead of time.sleep * Switch to PollUntilContextTimeout in wait functions * Make sure no pods of old replicas of the deployment are running anymore in WaitForDeploymentsAvailable functions * Change clients WithLister|Describer|Remover methods to variadic arguments * Move Deployers, Listers, Describers and Removers back into knative/k8s package * [WIP] Resolve some cyclic dependencies * Remove subscribe functionality from raw deployer to remove dependencies * Some more cleanup and import alias fixes * Rename deploy-type arg to deployer to align with other arg names * Fix build issues in cmd unit tests * Adjust integration tests to not test func subscribe by default and skip test for kubernetes deployer * Add shell completion for deployer selection * Undo unneeded changes in imports ordering * Move SynchronizedBuffer to k8s package * Readd Test_processValue unit test * Use constants for waiting timeout * Use onClusterFix for raw deployer too * Remove unneeded build tags * Use TestInt_ pattern for integration tests (and helper functions) * Use dot import for pkg/testing * Use dot import for pkg/testing/k8s * Use fn alias for pkg/functions import for consistency * Update outdated comment * Some more TestInt_xyz replaces * Mark test helper functions with t.Helper() * Removed build tags in other int_test_helpers.go * Use dot imports for some testing packages * Move testing/k8s to testing package * Revert "Move testing/k8s to testing package" This reverts commit 9782f41. * Update describe to return a ErrNotHandled error * Update remover to return a ErrNotHandled error * Update lister to only return resultlist and error (remove handled/bool return value) * Move IsCRDNotFoundError() to knative package and make specific to Knative * Add +build integration tag on integration test files * Remove misleading comment * Fix build issues * Add a few more comments to the Describe, Remove and List usage about responsibility * Remove outdated package comment * Remove need to specify deployer in function programmatic declaration * create Trigger in test only for Knative Service as we don't support it yet for the raw deployer anyhow * Use correct API version for triggers subscriber ref * Ignore golang-lint dot import errors in testing helpers
1 parent af45a47 commit 640a72a

Some content is hidden

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

56 files changed

+3381
-1997
lines changed

cmd/client.go

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,16 +58,16 @@ func NewClient(cfg ClientConfig, options ...fn.Option) (*fn.Client, func()) {
5858
var (
5959
t = newTransport(cfg.InsecureSkipVerify) // may provide a custom impl which proxies
6060
c = newCredentialsProvider(config.Dir(), t) // for accessing registries
61-
d = newKnativeDeployer(cfg.Verbose)
61+
d = newKnativeDeployer(cfg.Verbose) // default deployer (can be overridden via options)
6262
pp = newTektonPipelinesProvider(c, cfg.Verbose)
6363
o = []fn.Option{ // standard (shared) options for all commands
6464
fn.WithVerbose(cfg.Verbose),
6565
fn.WithTransport(t),
6666
fn.WithRepositoriesPath(config.RepositoriesPath()),
6767
fn.WithBuilder(buildpacks.NewBuilder(buildpacks.WithVerbose(cfg.Verbose))),
68-
fn.WithRemover(knative.NewRemover(cfg.Verbose)),
69-
fn.WithDescriber(knative.NewDescriber(cfg.Verbose)),
70-
fn.WithLister(knative.NewLister(cfg.Verbose)),
68+
fn.WithRemovers(knative.NewRemover(cfg.Verbose), k8s.NewRemover(cfg.Verbose)),
69+
fn.WithDescribers(knative.NewDescriber(cfg.Verbose), k8s.NewDescriber(cfg.Verbose)),
70+
fn.WithListers(knative.NewLister(cfg.Verbose), k8s.NewLister(cfg.Verbose)),
7171
fn.WithDeployer(d),
7272
fn.WithPipelinesProvider(pp),
7373
fn.WithPusher(docker.NewPusher(
@@ -135,6 +135,15 @@ func newKnativeDeployer(verbose bool) fn.Deployer {
135135
return knative.NewDeployer(options...)
136136
}
137137

138+
func newK8sDeployer(verbose bool) fn.Deployer {
139+
options := []k8s.DeployerOpt{
140+
k8s.WithDeployerVerbose(verbose),
141+
k8s.WithDeployerDecorator(deployDecorator{}),
142+
}
143+
144+
return k8s.NewDeployer(options...)
145+
}
146+
138147
type deployDecorator struct {
139148
oshDec k8s.OpenshiftMetadataDecorator
140149
}

cmd/client_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,10 @@ func Test_NewTestClient(t *testing.T) {
2121
)
2222

2323
// Factory constructor options which should be used when invoking later
24-
clientFn := NewTestClient(fn.WithRemover(remover))
24+
clientFn := NewTestClient(fn.WithRemovers(remover))
2525

2626
// Factory should ignore options provided when invoking
27-
client, _ := clientFn(ClientConfig{}, fn.WithDescriber(describer))
27+
client, _ := clientFn(ClientConfig{}, fn.WithDescribers(describer))
2828

2929
// Trigger an invocation of the mocks by running the associated client
3030
// methods which depend on them

cmd/completion_util.go

Lines changed: 37 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,28 @@ import (
99
"strings"
1010

1111
"github.com/spf13/cobra"
12-
1312
fn "knative.dev/func/pkg/functions"
13+
"knative.dev/func/pkg/k8s"
1414
"knative.dev/func/pkg/knative"
1515
)
1616

1717
func CompleteFunctionList(cmd *cobra.Command, args []string, toComplete string) (strings []string, directive cobra.ShellCompDirective) {
18-
lister := knative.NewLister(false)
18+
listers := []fn.Lister{
19+
knative.NewLister(false),
20+
k8s.NewLister(false),
21+
}
1922

20-
list, err := lister.List(cmd.Context(), "")
21-
if err != nil {
22-
directive = cobra.ShellCompDirectiveError
23-
return
23+
items := []fn.ListItem{}
24+
for _, lister := range listers {
25+
list, err := lister.List(cmd.Context(), "")
26+
if err != nil {
27+
directive = cobra.ShellCompDirectiveError
28+
return
29+
}
30+
items = append(items, list...)
2431
}
2532

26-
for _, item := range list {
33+
for _, item := range items {
2734
strings = append(strings, item.Name)
2835
}
2936
directive = cobra.ShellCompDirectiveDefault
@@ -158,3 +165,26 @@ func CompleteBuilderList(cmd *cobra.Command, args []string, complete string) (ma
158165

159166
return
160167
}
168+
169+
func CompleteDeployerList(cmd *cobra.Command, args []string, complete string) (matches []string, d cobra.ShellCompDirective) {
170+
deployers := []string{
171+
knative.KnativeDeployerName,
172+
k8s.KubernetesDeployerName,
173+
}
174+
175+
d = cobra.ShellCompDirectiveNoFileComp
176+
matches = []string{}
177+
178+
if len(complete) == 0 {
179+
matches = deployers
180+
return
181+
}
182+
183+
for _, b := range deployers {
184+
if strings.HasPrefix(b, complete) {
185+
matches = append(matches, b)
186+
}
187+
}
188+
189+
return
190+
}

cmd/delete_test.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ func TestDelete_Default(t *testing.T) {
5353
t.Fatal(err)
5454
}
5555

56-
cmd := NewDeleteCmd(NewTestClient(fn.WithRemover(remover)))
56+
cmd := NewDeleteCmd(NewTestClient(fn.WithRemovers(remover)))
5757
cmd.SetArgs([]string{})
5858
if err := cmd.Execute(); err != nil {
5959
t.Fatal(err)
@@ -104,7 +104,7 @@ func TestDelete_ByName(t *testing.T) {
104104

105105
// Create a command with a client constructor fn that instantiates a client
106106
// with a mocked remover.
107-
cmd := NewDeleteCmd(NewTestClient(fn.WithRemover(remover)))
107+
cmd := NewDeleteCmd(NewTestClient(fn.WithRemovers(remover)))
108108
cmd.SetArgs([]string{testname}) // run: func delete <name>
109109
if err := cmd.Execute(); err != nil {
110110
t.Fatal(err)
@@ -132,7 +132,7 @@ func TestDelete_Namespace(t *testing.T) {
132132
return nil
133133
}
134134

135-
cmd := NewDeleteCmd(NewTestClient(fn.WithRemover(remover)))
135+
cmd := NewDeleteCmd(NewTestClient(fn.WithRemovers(remover)))
136136
cmd.SetArgs([]string{testname, "--namespace", namespace})
137137
if err := cmd.Execute(); err != nil {
138138
t.Fatal(err)
@@ -177,7 +177,7 @@ func TestDelete_NamespaceFlagPriority(t *testing.T) {
177177
t.Fatal(err)
178178
}
179179

180-
cmd := NewDeleteCmd(NewTestClient(fn.WithRemover(remover)))
180+
cmd := NewDeleteCmd(NewTestClient(fn.WithRemovers(remover)))
181181
cmd.SetArgs([]string{testname, "--namespace", namespace2})
182182
if err := cmd.Execute(); err != nil {
183183
t.Fatal(err)
@@ -233,7 +233,7 @@ created: 2021-01-01T00:00:00+00:00
233233

234234
// Command with a Client constructor that returns client with the
235235
// mocked remover.
236-
cmd := NewDeleteCmd(NewTestClient(fn.WithRemover(remover)))
236+
cmd := NewDeleteCmd(NewTestClient(fn.WithRemovers(remover)))
237237
cmd.SetArgs([]string{}) // Do not use test command args
238238

239239
// Execute the command simulating no arguments.
@@ -277,7 +277,7 @@ func TestDelete_ByPath(t *testing.T) {
277277
}
278278

279279
// Command with a Client constructor using the mock remover.
280-
cmd := NewDeleteCmd(NewTestClient(fn.WithRemover(remover)))
280+
cmd := NewDeleteCmd(NewTestClient(fn.WithRemovers(remover)))
281281

282282
// Execute the command only with the path argument
283283
cmd.SetArgs([]string{"-p", root})
@@ -303,7 +303,7 @@ func TestDelete_NameAndPathExclusivity(t *testing.T) {
303303
remover := mock.NewRemover()
304304

305305
// Command with a Client constructor using the mock remover.
306-
cmd := NewDeleteCmd(NewTestClient(fn.WithRemover(remover)))
306+
cmd := NewDeleteCmd(NewTestClient(fn.WithRemovers(remover)))
307307

308308
// Capture command output for inspection
309309
buf := new(bytes.Buffer)

cmd/deploy.go

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@ import (
1515
"github.com/spf13/cobra"
1616
"k8s.io/apimachinery/pkg/api/resource"
1717
"knative.dev/client/pkg/util"
18-
1918
"knative.dev/func/pkg/builders"
2019
"knative.dev/func/pkg/config"
2120
fn "knative.dev/func/pkg/functions"
2221
"knative.dev/func/pkg/k8s"
22+
"knative.dev/func/pkg/knative"
2323
"knative.dev/func/pkg/utils"
2424
)
2525

@@ -132,7 +132,7 @@ EXAMPLES
132132
PreRunE: bindEnv("build", "build-timestamp", "builder", "builder-image",
133133
"base-image", "confirm", "domain", "env", "git-branch", "git-dir",
134134
"git-url", "image", "namespace", "path", "platform", "push", "pvc-size",
135-
"service-account", "registry", "registry-insecure", "remote",
135+
"service-account", "deployer", "registry", "registry-insecure", "remote",
136136
"username", "password", "token", "verbose", "remote-storage-class"),
137137
RunE: func(cmd *cobra.Command, args []string) error {
138138
return runDeploy(cmd, newClient)
@@ -193,6 +193,8 @@ EXAMPLES
193193
"When triggering a remote deployment, set a custom volume size to allocate for the build operation ($FUNC_PVC_SIZE)")
194194
cmd.Flags().String("service-account", f.Deploy.ServiceAccountName,
195195
"Service account to be used in the deployed function ($FUNC_SERVICE_ACCOUNT)")
196+
cmd.Flags().String("deployer", f.Deploy.Deployer,
197+
fmt.Sprintf("Type of deployment to use: '%s' for Knative Service (default) or '%s' for Kubernetes Deployment ($FUNC_DEPLOY_TYPE)", knative.KnativeDeployerName, k8s.KubernetesDeployerName))
196198
// Static Flags:
197199
// Options which have static defaults only (not globally configurable nor
198200
// persisted with the function)
@@ -234,6 +236,10 @@ EXAMPLES
234236
fmt.Println("internal: error while calling RegisterFlagCompletionFunc: ", err)
235237
}
236238

239+
if err := cmd.RegisterFlagCompletionFunc("deployer", CompleteDeployerList); err != nil {
240+
fmt.Println("internal: error while calling RegisterFlagCompletionFunc: ", err)
241+
}
242+
237243
return cmd
238244
}
239245

@@ -660,6 +666,9 @@ type deployConfig struct {
660666
//Service account to be used in deployed function
661667
ServiceAccountName string
662668

669+
// Deployer specifies the type of deployment: "knative" or "raw"
670+
Deployer string
671+
663672
// Remote indicates the deployment (and possibly build) process are to
664673
// be triggered in a remote environment rather than run locally.
665674
Remote bool
@@ -693,6 +702,7 @@ func newDeployConfig(cmd *cobra.Command) deployConfig {
693702
PVCSize: viper.GetString("pvc-size"),
694703
Timestamp: viper.GetBool("build-timestamp"),
695704
ServiceAccountName: viper.GetString("service-account"),
705+
Deployer: viper.GetString("deployer"),
696706
}
697707
// NOTE: .Env should be viper.GetStringSlice, but this returns unparsed
698708
// results and appears to be an open issue since 2017:
@@ -727,6 +737,7 @@ func (c deployConfig) Configure(f fn.Function) (fn.Function, error) {
727737
f.Build.Git.Revision = c.GitBranch // TODO: should match; perhaps "refSpec"
728738
f.Build.RemoteStorageClass = c.RemoteStorageClass
729739
f.Deploy.ServiceAccountName = c.ServiceAccountName
740+
f.Deploy.Deployer = c.Deployer
730741
f.Local.Remote = c.Remote
731742

732743
// PVCSize
@@ -899,6 +910,32 @@ func (c deployConfig) Validate(cmd *cobra.Command) (err error) {
899910
return
900911
}
901912

913+
// clientOptions returns client options specific to deploy, including the appropriate deployer
914+
func (c deployConfig) clientOptions() ([]fn.Option, error) {
915+
// Start with build config options
916+
o, err := c.buildConfig.clientOptions()
917+
if err != nil {
918+
return o, err
919+
}
920+
921+
// Add the appropriate deployer based on deploy type
922+
deployer := c.Deployer
923+
if deployer == "" {
924+
deployer = knative.KnativeDeployerName // default to knative for backwards compatibility
925+
}
926+
927+
switch deployer {
928+
case knative.KnativeDeployerName:
929+
o = append(o, fn.WithDeployer(newKnativeDeployer(c.Verbose)))
930+
case k8s.KubernetesDeployerName:
931+
o = append(o, fn.WithDeployer(newK8sDeployer(c.Verbose)))
932+
default:
933+
return o, fmt.Errorf("unsupported deploy type: %s (supported: %s, %s)", deployer, knative.KnativeDeployerName, k8s.KubernetesDeployerName)
934+
}
935+
936+
return o, nil
937+
}
938+
902939
// printDeployMessages to the output. Non-error deployment messages.
903940
func printDeployMessages(out io.Writer, f fn.Function) {
904941
digest, err := isDigested(f.Image)

cmd/deploy_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1959,7 +1959,7 @@ func TestDeploy_NoErrorOnOldFunctionNotFound(t *testing.T) {
19591959
}
19601960
clientFn := NewTestClient(
19611961
fn.WithDeployer(mock.NewDeployer()),
1962-
fn.WithRemover(remover),
1962+
fn.WithRemovers(remover),
19631963
)
19641964

19651965
// Create a basic go Function

cmd/describe.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,9 @@ func (i info) Human(w io.Writer) error {
179179
fmt.Fprintf(w, " %v\n", route)
180180
}
181181

182+
fmt.Fprintln(w, "Deployer:")
183+
fmt.Fprintf(w, " %v\n", i.Deployer)
184+
182185
if len(i.Subscriptions) > 0 {
183186
fmt.Fprintln(w, "Subscriptions (Source, Type, Broker):")
184187
for _, s := range i.Subscriptions {
@@ -204,6 +207,8 @@ func (i info) Plain(w io.Writer) error {
204207
fmt.Fprintf(w, "Route %v\n", route)
205208
}
206209

210+
fmt.Fprintf(w, "Deployer %v\n", i.Deployer)
211+
207212
if len(i.Subscriptions) > 0 {
208213
for _, s := range i.Subscriptions {
209214
fmt.Fprintf(w, "Subscription %v %v %v\n", s.Source, s.Type, s.Broker)

cmd/describe_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ func TestDescribe_Default(t *testing.T) {
1717
_ = FromTempDirectory(t)
1818
describer := mock.NewDescriber()
1919

20-
cmd := NewDescribeCmd(NewTestClient(fn.WithDescriber(describer)))
20+
cmd := NewDescribeCmd(NewTestClient(fn.WithDescribers(describer)))
2121
cmd.SetArgs([]string{})
2222
err := cmd.Execute()
2323

@@ -52,7 +52,7 @@ func TestDescribe_Undeployed(t *testing.T) {
5252

5353
describer := mock.NewDescriber()
5454

55-
cmd := NewDescribeCmd(NewTestClient(fn.WithDescriber(describer)))
55+
cmd := NewDescribeCmd(NewTestClient(fn.WithDescribers(describer)))
5656
cmd.SetArgs([]string{})
5757
if err := cmd.Execute(); err != nil {
5858
t.Fatal(err)
@@ -78,7 +78,7 @@ func TestDescribe_ByName(t *testing.T) {
7878
return fn.Instance{}, nil
7979
}
8080

81-
cmd := NewDescribeCmd(NewTestClient(fn.WithDescriber(describer)))
81+
cmd := NewDescribeCmd(NewTestClient(fn.WithDescribers(describer)))
8282
cmd.SetArgs([]string{testname})
8383
if err := cmd.Execute(); err != nil {
8484
t.Fatal(err)
@@ -111,9 +111,9 @@ func TestDescribe_ByProject(t *testing.T) {
111111
if name != expected {
112112
t.Fatalf("expected describer to receive name %q, got %q", expected, name)
113113
}
114-
return
114+
return fn.Instance{}, nil
115115
}
116-
cmd := NewDescribeCmd(NewTestClient(fn.WithDescriber(describer)))
116+
cmd := NewDescribeCmd(NewTestClient(fn.WithDescribers(describer)))
117117
cmd.SetArgs([]string{})
118118
if err := cmd.Execute(); err != nil {
119119
t.Fatal(err)
@@ -124,7 +124,7 @@ func TestDescribe_ByProject(t *testing.T) {
124124
// and a path will generate an error.
125125
func TestDescribe_NameAndPathExclusivity(t *testing.T) {
126126
d := mock.NewDescriber()
127-
cmd := NewDescribeCmd(NewTestClient(fn.WithDescriber(d)))
127+
cmd := NewDescribeCmd(NewTestClient(fn.WithDescribers(d)))
128128
cmd.SetArgs([]string{"-p", "./testpath", "testname"})
129129
if err := cmd.Execute(); err == nil {
130130
t.Fatalf("expected error on conflicting flags not received")

cmd/list.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -187,9 +187,9 @@ func (items listItems) Plain(w io.Writer) error {
187187
tabWriter := tabwriter.NewWriter(w, 0, 8, 2, ' ', 0)
188188
defer tabWriter.Flush()
189189

190-
fmt.Fprintf(tabWriter, "%s\t%s\t%s\t%s\t%s\n", "NAME", "NAMESPACE", "RUNTIME", "URL", "READY")
190+
fmt.Fprintf(tabWriter, "%s\t%s\t%s\t%s\t%s\t%s\n", "NAME", "NAMESPACE", "RUNTIME", "DEPLOYER", "URL", "READY")
191191
for _, item := range items {
192-
fmt.Fprintf(tabWriter, "%s\t%s\t%s\t%s\t%s\n", item.Name, item.Namespace, item.Runtime, item.URL, item.Ready)
192+
fmt.Fprintf(tabWriter, "%s\t%s\t%s\t%s\t%s\t%s\n", item.Name, item.Namespace, item.Runtime, item.Deployer, item.URL, item.Ready)
193193
}
194194
return nil
195195
}

cmd/list_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ func TestList_Namespace(t *testing.T) {
7171

7272
// Create an instance of the command which sets the flags
7373
// according to the test case
74-
cmd := NewListCmd(NewTestClient(fn.WithLister(lister)))
74+
cmd := NewListCmd(NewTestClient(fn.WithListers(lister)))
7575
args := []string{}
7676
if test.namespace != "" {
7777
args = append(args, "--namespace", test.namespace)

0 commit comments

Comments
 (0)