forked from kubernetes/ingress-nginx
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Bootstrap initial crossplane work (kubernetes#11653)
* Bootstrap initial crossplane work * Disable some tests to speed up stuff
- Loading branch information
Showing
14 changed files
with
646 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,7 +25,6 @@ linters: | |
- ginkgolinter | ||
- gocheckcompilerdirectives | ||
- goconst | ||
- gocritic | ||
- gocyclo | ||
- godox | ||
- gofmt | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/* | ||
Copyright 2024 The Kubernetes Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package crossplane | ||
|
||
import ( | ||
ngx_crossplane "github.com/nginxinc/nginx-go-crossplane" | ||
) | ||
|
||
func (c *crossplaneTemplate) buildConfig() { | ||
// Write basic directives | ||
config := &ngx_crossplane.Config{ | ||
Parsed: ngx_crossplane.Directives{ | ||
buildDirective("pid", c.tplConfig.PID), | ||
buildDirective("daemon", "off"), | ||
buildDirective("worker_processes", c.tplConfig.Cfg.WorkerProcesses), | ||
buildDirective("worker_rlimit_nofile", c.tplConfig.Cfg.MaxWorkerOpenFiles), | ||
buildDirective("worker_shutdown_timeout", c.tplConfig.Cfg.WorkerShutdownTimeout), | ||
}, | ||
} | ||
if c.tplConfig.Cfg.WorkerCPUAffinity != "" { | ||
config.Parsed = append(config.Parsed, buildDirective("worker_cpu_affinity", c.tplConfig.Cfg.WorkerCPUAffinity)) | ||
} | ||
c.config = config | ||
} |
70 changes: 70 additions & 0 deletions
70
internal/ingress/controller/template/crossplane/crossplane.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
/* | ||
Copyright 2024 The Kubernetes Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package crossplane | ||
|
||
import ( | ||
"bytes" | ||
|
||
ngx_crossplane "github.com/nginxinc/nginx-go-crossplane" | ||
|
||
"k8s.io/ingress-nginx/internal/ingress/controller/config" | ||
) | ||
|
||
/* | ||
Unsupported directives: | ||
- opentelemetry | ||
- modsecurity | ||
- any stream directive (TCP/UDP forwarding) | ||
- geoip2 | ||
*/ | ||
|
||
// On this case we will try to use the go ngx_crossplane to write the template instead of the template renderer | ||
|
||
type crossplaneTemplate struct { | ||
options *ngx_crossplane.BuildOptions | ||
config *ngx_crossplane.Config | ||
tplConfig *config.TemplateConfig | ||
} | ||
|
||
func NewCrossplaneTemplate() *crossplaneTemplate { | ||
lua := ngx_crossplane.Lua{} | ||
return &crossplaneTemplate{ | ||
options: &ngx_crossplane.BuildOptions{ | ||
Builders: []ngx_crossplane.RegisterBuilder{ | ||
lua.RegisterBuilder(), | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func (c *crossplaneTemplate) Write(conf *config.TemplateConfig) ([]byte, error) { | ||
c.tplConfig = conf | ||
|
||
// build root directives | ||
c.buildConfig() | ||
|
||
// build events directive | ||
c.buildEvents() | ||
|
||
// build http directive | ||
c.buildHTTP() | ||
|
||
var buf bytes.Buffer | ||
|
||
err := ngx_crossplane.Build(&buf, *c.config, &ngx_crossplane.BuildOptions{}) | ||
return buf.Bytes(), err | ||
} |
90 changes: 90 additions & 0 deletions
90
internal/ingress/controller/template/crossplane/crossplane_internal_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
/* | ||
Copyright 2024 The Kubernetes Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package crossplane | ||
|
||
import ( | ||
"testing" | ||
|
||
ngx_crossplane "github.com/nginxinc/nginx-go-crossplane" | ||
"github.com/stretchr/testify/require" | ||
|
||
"k8s.io/ingress-nginx/internal/ingress/controller/config" | ||
) | ||
|
||
// THIS FILE SHOULD BE USED JUST FOR INTERNAL TESTS - Private functions | ||
|
||
func Test_Internal_buildEvents(t *testing.T) { | ||
t.Run("should fill correctly events directives with defaults", func(t *testing.T) { | ||
c := ngx_crossplane.Config{} | ||
tplConfig := &config.TemplateConfig{ | ||
Cfg: config.NewDefault(), | ||
} | ||
|
||
expectedEvents := &ngx_crossplane.Config{ | ||
File: "", | ||
Parsed: ngx_crossplane.Directives{ | ||
{ | ||
Directive: "events", | ||
Block: ngx_crossplane.Directives{ | ||
buildDirective("worker_connections", 16384), | ||
buildDirective("use", "epool"), | ||
buildDirective("multi_accept", true), | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
cplane := NewCrossplaneTemplate() | ||
cplane.config = &c | ||
cplane.tplConfig = tplConfig | ||
cplane.buildEvents() | ||
require.Equal(t, expectedEvents, cplane.config) | ||
}) | ||
|
||
t.Run("should fill correctly events directives with specific values", func(t *testing.T) { | ||
c := ngx_crossplane.Config{} | ||
tplConfig := &config.TemplateConfig{ | ||
Cfg: config.Configuration{ | ||
MaxWorkerConnections: 50, | ||
EnableMultiAccept: false, | ||
DebugConnections: []string{"127.0.0.1/32", "192.168.0.10"}, | ||
}, | ||
} | ||
|
||
expectedEvents := &ngx_crossplane.Config{ | ||
File: "", | ||
Parsed: ngx_crossplane.Directives{ | ||
{ | ||
Directive: "events", | ||
Block: ngx_crossplane.Directives{ | ||
buildDirective("worker_connections", 50), | ||
buildDirective("use", "epool"), | ||
buildDirective("multi_accept", false), | ||
buildDirective("debug_connection", "127.0.0.1/32"), | ||
buildDirective("debug_connection", "192.168.0.10"), | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
cplane := NewCrossplaneTemplate() | ||
cplane.config = &c | ||
cplane.tplConfig = tplConfig | ||
cplane.buildEvents() | ||
require.Equal(t, expectedEvents, cplane.config) | ||
}) | ||
} |
58 changes: 58 additions & 0 deletions
58
internal/ingress/controller/template/crossplane/crossplane_internal_utils_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
/* | ||
Copyright 2024 The Kubernetes Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package crossplane | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
"k8s.io/ingress-nginx/internal/ingress/controller/config" | ||
) | ||
|
||
// THIS FILE SHOULD BE USED JUST FOR INTERNAL TESTS - Private functions | ||
|
||
func Test_Internal_buildDirectives(t *testing.T) { | ||
t.Run("should be able to run a directive with a single argument", func(t *testing.T) { | ||
directive := buildDirective("somedirective", "bla") | ||
require.Equal(t, directive.Directive, "somedirective", []string{"bla"}) | ||
}) | ||
t.Run("should be able to run a directive with multiple different arguments", func(t *testing.T) { | ||
directive := buildDirective("somedirective", "bla", 5, true, seconds(10), []string{"xpto", "bla"}) | ||
require.Equal(t, directive.Directive, "somedirective", []string{"bla", "5", "on", "10s", "xpto", "bla"}) | ||
}) | ||
} | ||
|
||
func Test_Internal_boolToStr(t *testing.T) { | ||
require.Equal(t, boolToStr(true), "on") | ||
require.Equal(t, boolToStr(false), "off") | ||
} | ||
|
||
func Test_Internal_buildLuaDictionaries(t *testing.T) { | ||
t.Skip("Maps are not sorted, need to fix this") | ||
cfg := &config.Configuration{ | ||
LuaSharedDicts: map[string]int{ | ||
"somedict": 1024, | ||
"otherdict": 1025, | ||
}, | ||
} | ||
directives := buildLuaSharedDictionaries(cfg) | ||
require.Len(t, directives, 2) | ||
require.Equal(t, "lua_shared_dict", directives[0].Directive) | ||
require.Equal(t, []string{"somedict", "1M"}, directives[0].Args) | ||
require.Equal(t, "lua_shared_dict", directives[1].Directive) | ||
require.Equal(t, []string{"otherdict", "1025K"}, directives[1].Args) | ||
} |
Oops, something went wrong.