Skip to content

Commit 176bf97

Browse files
committed
Fix issue with injector webhook applying bindings from all namespaces instead of selected, update manifests to not use auth-proxy
1 parent 31be652 commit 176bf97

15 files changed

+70
-29
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ Aside from defining simple key/value pairs, Kconfigs can also define and referen
4646

4747
For a target to have its environment variables controlled by Kconfigs, it needs the annotation ```kconfigcontroller.atteg.com/inject=true```.
4848

49+
Add the annotation, ```kconfigcontroller.atteg.com/refresh-template=true``` to have updates to a kconfig to trigger a rolling update for deployments, statefulsets of the selected pods.
50+
4951
Kconfig-controller also has a secondary custom resource, KconfigBinding, that is used by the controllers and should not be created/manipulated directly by users. This resources serve as a target for Kconfigs to update their changes whereafter, the admission-controller can import the contained environment variables directly into pods. Note that there is a one-to-one mapping for each kconfig and kconfigbinding.
5052

5153
Build requires Kustomize (https://github.com/kubernetes-sigs/kustomize) locally and cert-manager (https://github.com/jetstack/cert-manager) installed in the kubernetes cluser for the admission-controller's TLS certificates.

api/v1beta1/kconfig_types.go

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,20 +25,27 @@ import (
2525

2626
// KconfigSpec defines the desired state of Kconfig
2727
type KconfigSpec struct {
28-
Level int `json:"level"`
28+
Level int `json:"level"`
29+
// +kubebuilder:validation:Optional
2930
Selector metav1.LabelSelector `json:"selector"`
3031
EnvConfigs []EnvConfig `json:"envConfigs"`
3132
}
3233

3334
// EnvConfig represents a single environment variable configuration
3435
type EnvConfig struct {
3536
// Type should be immutable
36-
Type string `json:"type"`
37-
Key string `json:"key"`
38-
Value *string `json:"value,omitempty"`
39-
ConfigMapKeyRef *v1.ConfigMapKeySelector `json:"configMapKeyRef,omitempty"`
40-
SecretKeyRef *v1.SecretKeySelector `json:"secretKeyRef,omitempty" protobuf:"bytes,4,opt,name=secretKeyRef"`
41-
FieldRef *v1.ObjectFieldSelector `json:"fieldRef,omitempty" protobuf:"bytes,4,opt,name=fieldRef"`
37+
// +kubebuilder:validation:Optional
38+
Type string `json:"type"`
39+
Key string `json:"key"`
40+
// +kubebuilder:validation:Optional
41+
Value *string `json:"value,omitempty"`
42+
// +kubebuilder:validation:Optional
43+
ConfigMapKeyRef *v1.ConfigMapKeySelector `json:"configMapKeyRef,omitempty"`
44+
// +kubebuilder:validation:Optional
45+
SecretKeyRef *v1.SecretKeySelector `json:"secretKeyRef,omitempty" protobuf:"bytes,4,opt,name=secretKeyRef"`
46+
// +kubebuilder:validation:Optional
47+
FieldRef *v1.ObjectFieldSelector `json:"fieldRef,omitempty" protobuf:"bytes,4,opt,name=fieldRef"`
48+
// +kubebuilder:validation:Optional
4249
ResourceFieldRef *v1.ResourceFieldSelector `json:"resourceFieldRef,omitempty" protobuf:"bytes,4,opt,name=resourceFieldRef"`
4350
}
4451

api/v1beta1/kconfigbinding_types.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,9 @@ import (
2525

2626
// KconfigBindingSpec defines the desired state of KconfigBinding
2727
type KconfigBindingSpec struct {
28-
Level int `json:"level"`
29-
Envs []v1.EnvVar `json:"envs"`
28+
Level int `json:"level"`
29+
Envs []v1.EnvVar `json:"envs"`
30+
// +kubebuilder:validation:Optional
3031
Selector metav1.LabelSelector `json:"selector"`
3132
}
3233

config/crd/bases/kconfigcontroller.atteg.com_kconfigbindings.yaml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,6 @@ spec:
184184
required:
185185
- envs
186186
- level
187-
- selector
188187
type: object
189188
status:
190189
description: KconfigBindingStatus defines the observed state of KconfigBinding

config/crd/bases/kconfigcontroller.atteg.com_kconfigs.yaml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,6 @@ spec:
115115
type: string
116116
required:
117117
- key
118-
- type
119118
type: object
120119
type: array
121120
level:
@@ -169,7 +168,6 @@ spec:
169168
required:
170169
- envConfigs
171170
- level
172-
- selector
173171
type: object
174172
status:
175173
description: KconfigStatus defines the observed state of Kconfig

config/default/kustomization.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ patchesStrategicMerge:
2727
# Protect the /metrics endpoint by putting it behind auth.
2828
# Only one of manager_auth_proxy_patch.yaml and
2929
# manager_prometheus_metrics_patch.yaml should be enabled.
30-
- manager_auth_proxy_patch.yaml
30+
#- manager_auth_proxy_patch.yaml
3131
# If you want your controller-manager to expose the /metrics
3232
# endpoint w/o any authn/z, uncomment the following line and
3333
# comment manager_auth_proxy_patch.yaml.

config/manager/kustomization.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@ apiVersion: kustomize.config.k8s.io/v1beta1
44
kind: Kustomization
55
images:
66
- name: controller
7-
newName: docker-registry.aeg.cloud/kcnfig-system/kconfig-controller
8-
newTag: ref-202002231749
7+
newName: docker-registry.aeg.cloud/kconfig-system/kconfig-controller
8+
newTag: v1beta1-202002261210

config/manager/manager.yaml

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,19 +21,25 @@ spec:
2121
metadata:
2222
labels:
2323
control-plane: controller-manager
24+
annotations:
25+
prometheus.io/path: /metrics
26+
prometheus.io/port: "8080"
27+
prometheus.io/scheme: https
28+
prometheus.io/scrape: "true"
2429
spec:
2530
containers:
2631
- command:
2732
- /manager
2833
args:
34+
- --metrics-addr=:8080
2935
- --enable-leader-election
3036
image: controller:latest
3137
name: manager
3238
resources:
3339
limits:
34-
cpu: 100m
35-
memory: 30Mi
40+
cpu: "4"
41+
memory: 6Gi
3642
requests:
37-
cpu: 100m
38-
memory: 20Mi
43+
cpu: "4"
44+
memory: 6Gi
3945
terminationGracePeriodSeconds: 10

config/rbac/kustomization.yaml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@ resources:
66
# Comment the following 3 lines if you want to disable
77
# the auth proxy (https://github.com/brancz/kube-rbac-proxy)
88
# which protects your /metrics endpoint.
9-
- auth_proxy_service.yaml
10-
- auth_proxy_role.yaml
11-
- auth_proxy_role_binding.yaml
9+
#- auth_proxy_service.yaml
10+
#- auth_proxy_role.yaml
11+
#- auth_proxy_role_binding.yaml

config/rbac/role.yaml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,30 @@ metadata:
66
creationTimestamp: null
77
name: manager-role
88
rules:
9+
- apiGroups:
10+
- ""
11+
resources:
12+
- configmaps
13+
- secrets
14+
verbs:
15+
- create
16+
- delete
17+
- get
18+
- list
19+
- patch
20+
- update
21+
- watch
22+
- apiGroups:
23+
- apps
24+
resources:
25+
- deployments
26+
- statefulsets
27+
verbs:
28+
- get
29+
- list
30+
- patch
31+
- update
32+
- watch
933
- apiGroups:
1034
- kconfigcontroller.atteg.com
1135
resources:

config/webhook/manifests.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ webhooks:
1212
name: webhook-service
1313
namespace: system
1414
path: /mutate-v1-pod
15-
failurePolicy: Fail
15+
failurePolicy: Ignore
1616
name: config-injector.kconfigcontroller.aeg.cloud
1717
rules:
1818
- apiGroups:

controllers/const.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,12 @@ const (
44
WarningEventType = "Warning"
55
InvalidEnvConfigEvent = "InvalidEnvConfig"
66

7+
ValueEnvConfigType = "Value"
78
ConfigMapEnvConfigType = "ConfigMap"
89
SecretEnvConfigType = "Secret"
910
FieldRefEnvConfigType = "FieldRef"
1011
ResourceFieldRefEnvConfigType = "ResourceFieldRef"
1112

12-
AllowTemplateUpdatesAnnotation = "kconfigcontroller.atteg.com/update-template"
13+
AllowTemplateUpdatesAnnotation = "kconfigcontroller.atteg.com/refresh-template"
1314
GenerationAnnotationPrefix = "kconfigcontroller.atteg.com/"
1415
)

controllers/kconfig_controller.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ type KconfigReconciler struct {
4545

4646
// +kubebuilder:rbac:groups=kconfigcontroller.atteg.com,resources=kconfigs,verbs=get;list;watch;create;update;patch;delete
4747
// +kubebuilder:rbac:groups=kconfigcontroller.atteg.com,resources=kconfigs/status,verbs=get;update;patch
48+
// +kubebuilder:rbac:groups="",resources=configmaps;secrets,verbs=get;list;watch;create;update;patch;delete
4849

4950
func (r *KconfigReconciler) Reconcile(req ctrl.Request) (ctrl.Result, error) {
5051
ctx := context.Background()
@@ -129,9 +130,10 @@ func (r *KconfigReconciler) processKconfig(ctx context.Context, kc *kconfigcontr
129130
func (r *KconfigReconciler) processValueEnvConfig(ec kconfigcontrollerv1beta1.EnvConfig, envVars *[]v1.EnvVar, updatedECs *[]kconfigcontrollerv1beta1.EnvConfig) error {
130131
if ec.Key == "" || ec.Value == nil {
131132
r.Recorder.Event(&kconfigcontrollerv1beta1.Kconfig{}, WarningEventType, InvalidEnvConfigEvent, "Either key or value is empty for value type EnvConfig. This entry will be removed")
133+
return nil
132134
}
133135
*envVars = append(*envVars, v1.EnvVar{Name: ec.Key, Value: *ec.Value})
134-
*updatedECs = append(*updatedECs, *ec.DeepCopy())
136+
*updatedECs = append(*updatedECs, kconfigcontrollerv1beta1.EnvConfig{Type: ValueEnvConfigType, Key: ec.Key, Value: ec.Value})
135137
return nil
136138
}
137139

main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ func main() {
9595
hookServer.Register("/mutate-v1-pod", &webhook.Admission{
9696
Handler: &webhooks.PodConfigInjector{
9797
Client: mgr.GetClient(),
98-
Log: ctrl.Log.WithName("webhooks").WithName("pod-config-injector"),
98+
Log: ctrl.Log.WithName("webhooks").WithName("pod-config-injector"),
9999
},
100100
})
101101

webhooks/pod_injector_webhook.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,12 @@ const (
3535
InjectConfigAnnotation = "kconfigcontroller.atteg.com/inject"
3636
)
3737

38-
// +kubebuilder:webhook:path=/mutate-v1-pod,mutating=true,failurePolicy=fail,groups="",resources=pods,verbs=create,versions=v1,name=config-injector.kconfigcontroller.aeg.cloud
38+
// +kubebuilder:webhook:path=/mutate-v1-pod,mutating=true,failurePolicy=ignore,groups="",resources=pods,verbs=create,versions=v1,name=config-injector.kconfigcontroller.aeg.cloud
3939

4040
type PodConfigInjector struct {
4141
Client client.Client
4242
decoder *admission.Decoder
43-
Log logr.Logger
43+
Log logr.Logger
4444
}
4545

4646
func (r *PodConfigInjector) InjectDecoder(d *admission.Decoder) error {
@@ -50,6 +50,7 @@ func (r *PodConfigInjector) InjectDecoder(d *admission.Decoder) error {
5050

5151
func (r *PodConfigInjector) Handle(ctx context.Context, req admission.Request) admission.Response {
5252
pod := &v1.Pod{}
53+
5354
err := r.decoder.Decode(req, pod)
5455
if err != nil {
5556
return admission.Errored(http.StatusBadRequest, err)
@@ -61,7 +62,7 @@ func (r *PodConfigInjector) Handle(ctx context.Context, req admission.Request) a
6162
}
6263
// get bindings that select this rs
6364
kcbs := v1beta1.KconfigBindingList{}
64-
if err := r.Client.List(ctx, &kcbs, client.InNamespace(pod.Namespace)); err != nil {
65+
if err := r.Client.List(ctx, &kcbs, client.InNamespace(req.Namespace)); err != nil {
6566
return admission.Errored(http.StatusInternalServerError, fmt.Errorf("could not get kconfigbininglist: %s", err.Error()))
6667
}
6768

0 commit comments

Comments
 (0)