Skip to content

Commit de574ed

Browse files
committed
Dynamic control plane configuration support (nginx#943)
Problem: We want to be able to change control plane settings dynamically, to avoid having to restart NKG and lose valuable state. Solution: Introducing a new CRD that is initialized and created on startup, NginxGateway. This CRD can be updated by the user to dynamically change the state of the control plane. Right now we simply support changing the logging level. The controller will revert to using default values if the CRD is not detected.
1 parent 51b6873 commit de574ed

Some content is hidden

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

45 files changed

+1238
-68
lines changed

.github/workflows/ci.yml

+3
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,9 @@ jobs:
5555
- name: Check if generated go files are up to date
5656
run: make generate && git diff --exit-code
5757

58+
- name: Check if generated CRDs and types are up to date
59+
run: make generate-crds && git diff --exit-code
60+
5861
- name: Check if generated manifests are up to date
5962
run: make generate-manifests && git diff --exit-code
6063

Makefile

+6-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ KIND_KUBE_CONFIG=$${HOME}/.kube/kind/config## The location of the kind kubeconfi
2121
OUT_DIR ?= $(shell pwd)/build/out## The folder where the binary will be stored
2222
ARCH ?= amd64## The architecture of the image and/or binary. For example: amd64 or arm64
2323
override HELM_TEMPLATE_COMMON_ARGS += --set creator=template --set nameOverride=nginx-gateway## The common options for the Helm template command.
24-
override HELM_TEMPLATE_EXTRA_ARGS_FOR_ALL_MANIFESTS_FILE += --set service.create=false## The options to be passed to the full Helm templating command only.
24+
override HELM_TEMPLATE_EXTRA_ARGS_FOR_ALL_MANIFESTS_FILE += --include-crds --set service.create=false## The options to be passed to the full Helm templating command only.
2525
override DOCKER_BUILD_OPTIONS += --build-arg VERSION=$(VERSION) --build-arg GIT_COMMIT=$(GIT_COMMIT) --build-arg DATE=$(DATE)## The options for the docker build command. For example, --pull
2626
override NGINX_DOCKER_BUILD_OPTIONS += --build-arg NJS_DIR=$(NJS_DIR) --build-arg NGINX_CONF_DIR=$(NGINX_CONF_DIR)
2727
.DEFAULT_GOAL := help
@@ -62,6 +62,11 @@ build-goreleaser: ## Build the binary using GoReleaser
6262
generate: ## Run go generate
6363
go generate ./...
6464

65+
.PHONY: generate-crds
66+
generate-crds: ## Generate CRDs and Go types using kubebuilder
67+
go run sigs.k8s.io/controller-tools/cmd/controller-gen crd paths=./apis/... output:crd:dir=deploy/helm-chart/crds
68+
go run sigs.k8s.io/controller-tools/cmd/controller-gen object paths=./apis/...
69+
6570
.PHONY: clean
6671
clean: ## Clean the build
6772
-rm -r $(OUT_DIR)

apis/doc.go

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
// Package apis stores the API definitions for NGINX Kubernetes Gateway configuration.
2+
package apis

apis/v1alpha1/doc.go

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// Package v1alpha1 contains API Schema definitions for the
2+
// gateway.nginx.org API group.
3+
//
4+
// +kubebuilder:object:generate=true
5+
// +groupName=gateway.nginx.org
6+
package v1alpha1

apis/v1alpha1/nginxgateway_types.go

+90
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
package v1alpha1
2+
3+
import metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
4+
5+
// +kubebuilder:object:root=true
6+
// +kubebuilder:storageversion
7+
// +kubebuilder:subresource:status
8+
9+
// NginxGateway represents the dynamic configuration for an NGINX Kubernetes Gateway control plane.
10+
type NginxGateway struct {
11+
metav1.TypeMeta `json:",inline"`
12+
metav1.ObjectMeta `json:"metadata,omitempty"`
13+
14+
// NginxGatewaySpec defines the desired state of the NginxGateway.
15+
Spec NginxGatewaySpec `json:"spec"`
16+
17+
// NginxGatewayStatus defines the state of the NginxGateway.
18+
Status NginxGatewayStatus `json:"status,omitempty"`
19+
}
20+
21+
// +kubebuilder:object:root=true
22+
23+
// NginxGatewayList contains a list of NginxGateways.
24+
type NginxGatewayList struct {
25+
metav1.TypeMeta `json:",inline"`
26+
metav1.ListMeta `json:"metadata,omitempty"`
27+
Items []NginxGateway `json:"items"`
28+
}
29+
30+
// NginxGatewaySpec defines the desired state of the NginxGateway.
31+
type NginxGatewaySpec struct {
32+
// Logging defines logging related settings for the control plane.
33+
//
34+
// +optional
35+
Logging *Logging `json:"logging,omitempty"`
36+
}
37+
38+
// Logging defines logging related settings for the control plane.
39+
type Logging struct {
40+
// Level defines the logging level.
41+
//
42+
// +optional
43+
// +kubebuilder:default=info
44+
Level *ControllerLogLevel `json:"level,omitempty"`
45+
}
46+
47+
// ControllerLogLevel type defines the logging level for the control plane.
48+
//
49+
// +kubebuilder:validation:Enum=info;debug;error
50+
type ControllerLogLevel string
51+
52+
const (
53+
// ControllerLogLevelInfo is the info level for control plane logging.
54+
ControllerLogLevelInfo ControllerLogLevel = "info"
55+
56+
// ControllerLogLevelDebug is the debug level for control plane logging.
57+
ControllerLogLevelDebug ControllerLogLevel = "debug"
58+
59+
// ControllerLogLevelError is the error level for control plane logging.
60+
ControllerLogLevelError ControllerLogLevel = "error"
61+
)
62+
63+
// NginxGatewayStatus defines the state of the NginxGateway.
64+
type NginxGatewayStatus struct {
65+
// +optional
66+
// +listType=map
67+
// +listMapKey=type
68+
// +kubebuilder:validation:MaxItems=8
69+
Conditions []metav1.Condition `json:"conditions,omitempty"`
70+
}
71+
72+
// NginxGatewayConditionType is a type of condition associated with an
73+
// NginxGateway. This type should be used with the NginxGatewayStatus.Conditions field.
74+
type NginxGatewayConditionType string
75+
76+
// NginxGatewayConditionReason defines the set of reasons that explain why a
77+
// particular NginxGateway condition type has been raised.
78+
type NginxGatewayConditionReason string
79+
80+
const (
81+
// NginxGatewayConditionValid is a condition that is true when the NginxGateway
82+
// configuration is syntactically and semantically valid.
83+
NginxGatewayConditionValid NginxGatewayConditionType = "Valid"
84+
85+
// NginxGatewayReasonValid is a reason that is used with the "Valid" condition when the condition is True.
86+
NginxGatewayReasonValid NginxGatewayConditionReason = "Valid"
87+
88+
// NginxGatewayReasonInvalid is a reason that is used with the "Valid" condition when the condition is False.
89+
NginxGatewayReasonInvalid NginxGatewayConditionReason = "Invalid"
90+
)

apis/v1alpha1/register.go

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package v1alpha1
2+
3+
import (
4+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
5+
"k8s.io/apimachinery/pkg/runtime"
6+
"k8s.io/apimachinery/pkg/runtime/schema"
7+
)
8+
9+
// GroupName specifies the group name used to register the objects.
10+
const GroupName = "gateway.nginx.org"
11+
12+
// SchemeGroupVersion is group version used to register these objects
13+
var SchemeGroupVersion = schema.GroupVersion{Group: GroupName, Version: "v1alpha1"}
14+
15+
// Resource takes an unqualified resource and returns a Group qualified GroupResource
16+
func Resource(resource string) schema.GroupResource {
17+
return SchemeGroupVersion.WithResource(resource).GroupResource()
18+
}
19+
20+
var (
21+
// SchemeBuilder collects functions that add things to a scheme. It's to allow
22+
// code to compile without explicitly referencing generated types. You should
23+
// declare one in each package that will have generated deep copy or conversion
24+
// functions.
25+
SchemeBuilder = runtime.NewSchemeBuilder(addKnownTypes)
26+
27+
// AddToScheme applies all the stored functions to the scheme. A non-nil error
28+
// indicates that one function failed and the attempt was abandoned.
29+
AddToScheme = SchemeBuilder.AddToScheme
30+
)
31+
32+
// Adds the list of known types to Scheme.
33+
func addKnownTypes(scheme *runtime.Scheme) error {
34+
scheme.AddKnownTypes(SchemeGroupVersion,
35+
&NginxGateway{},
36+
&NginxGatewayList{},
37+
)
38+
// AddToGroupVersion allows the serialization of client types like ListOptions.
39+
metav1.AddToGroupVersion(scheme, SchemeGroupVersion)
40+
return nil
41+
}

apis/v1alpha1/zz_generated.deepcopy.go

+132
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)