Skip to content

Commit 04bcf68

Browse files
Merge pull request #295 from james-milligan/perm-backfill
feat: role binding backfill
2 parents 1eff914 + ca0f35e commit 04bcf68

File tree

6 files changed

+256
-51
lines changed

6 files changed

+256
-51
lines changed

docs/permissions.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@ The `proxy-role` definition can be found [here](../config/rbac/auth_proxy_role.y
4444
### Flagd Kubernetes Sync
4545

4646
The `flagd-kubernetes-sync` role providers the permission to get, watch and list all `core.openfeature.dev` resources, permitting the kubernetes sync feature in injected `flagd` containers.
47-
Its definition can be found [here](../config/rbac/flagd_kubernetes_sync_clusterrole.yaml)
47+
Its definition can be found [here](../config/rbac/flagd_kubernetes_sync_clusterrole.yaml).
48+
During startup the operator will backfill permissions to the `flagd-kubernetes-sync` cluster role binding from the current state of the cluster, adding all service accounts from pods with the `core.openfeature.dev/enabled` annotation set to `"true"`, preventing unexpected behavior during upgrades.
4849

4950
| API Group | Resource | Verbs |
5051
| ----------- | ----------- | ----------- |

main.go

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,12 @@ limitations under the License.
1717
package main
1818

1919
import (
20+
"context"
2021
"flag"
22+
"os"
23+
2124
corev1 "k8s.io/api/core/v1"
2225
"k8s.io/apimachinery/pkg/api/resource"
23-
"os"
2426

2527
// Import all Kubernetes client auth plugins (e.g. Azure, GCP, OIDC, etc.)
2628
// to ensure that exec-entrypoint and run can make use of them.
@@ -130,6 +132,17 @@ func main() {
130132
os.Exit(1)
131133
}
132134

135+
// setup indexer for backfilling permissions on the flagd-kubernetes-sync role binding
136+
if err := mgr.GetFieldIndexer().IndexField(
137+
context.Background(),
138+
&corev1.Pod{},
139+
webhooks.OpenFeatureEnabledAnnotationPath,
140+
webhooks.OpenFeatureEnabledAnnotationIndex,
141+
); err != nil {
142+
setupLog.Error(err, "unable to create indexer", "webhook", webhooks.OpenFeatureEnabledAnnotationPath)
143+
os.Exit(1)
144+
}
145+
133146
if err = (&controllers.FeatureFlagConfigurationReconciler{
134147
Client: mgr.GetClient(),
135148
Scheme: mgr.GetScheme(),
@@ -149,7 +162,7 @@ func main() {
149162

150163
//+kubebuilder:scaffold:builder
151164
hookServer := mgr.GetWebhookServer()
152-
hookServer.Register("/mutate-v1-pod", &webhook.Admission{Handler: &webhooks.PodMutator{
165+
podMutator := &webhooks.PodMutator{
153166
FlagDResourceRequirements: corev1.ResourceRequirements{
154167
Limits: map[corev1.ResourceName]resource.Quantity{
155168
corev1.ResourceCPU: flagDCpuLimitResource,
@@ -162,7 +175,8 @@ func main() {
162175
},
163176
Client: mgr.GetClient(),
164177
Log: ctrl.Log.WithName("mutating-pod-webhook"),
165-
}})
178+
}
179+
hookServer.Register("/mutate-v1-pod", &webhook.Admission{Handler: podMutator})
166180
hookServer.Register("/validate-v1alpha1-featureflagconfiguration", &webhook.Admission{Handler: &webhooks.FeatureFlagConfigurationValidator{
167181
Client: mgr.GetClient(),
168182
Log: ctrl.Log.WithName("validating-featureflagconfiguration-webhook"),
@@ -178,7 +192,19 @@ func main() {
178192
}
179193

180194
setupLog.Info("starting manager")
181-
if err := mgr.Start(ctrl.SetupSignalHandler()); err != nil {
195+
ctx := ctrl.SetupSignalHandler()
196+
errChan := make(chan error, 1)
197+
go func(chan error) {
198+
if err := mgr.Start(ctx); err != nil {
199+
errChan <- err
200+
}
201+
}(errChan)
202+
203+
setupLog.Info("restoring flagd-kubernetes-sync cluster role binding subjects from current cluster state")
204+
// backfill can be handled asynchronously, so we do not need to block via the channel
205+
go podMutator.BackfillPermissions(ctx, make(chan struct{}, 1))
206+
207+
if err := <-errChan; err != nil {
182208
setupLog.Error(err, "problem running manager")
183209
os.Exit(1)
184210
}

webhooks/pod_webhook.go

Lines changed: 74 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ import (
88
"os"
99
"reflect"
1010
"strings"
11+
"time"
12+
13+
goErr "errors"
1114

1215
"github.com/go-logr/logr"
1316
corev1alpha1 "github.com/open-feature/open-feature-operator/apis/core/v1alpha1"
@@ -16,16 +19,18 @@ import (
1619
v1 "k8s.io/api/rbac/v1"
1720
"k8s.io/apimachinery/pkg/api/errors"
1821
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
22+
"sigs.k8s.io/controller-runtime/pkg/cache"
1923
"sigs.k8s.io/controller-runtime/pkg/client"
2024
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
2125
)
2226

2327
// we likely want these to be configurable, eventually
2428
const (
25-
FlagDImagePullPolicy corev1.PullPolicy = "Always"
26-
clusterRoleBindingName string = "open-feature-operator-flagd-kubernetes-sync"
27-
flagdMetricPortEnvVar string = "FLAGD_METRICS_PORT"
28-
fileSyncMountPath string = "/etc/flagd/"
29+
FlagDImagePullPolicy corev1.PullPolicy = "Always"
30+
clusterRoleBindingName string = "open-feature-operator-flagd-kubernetes-sync"
31+
flagdMetricPortEnvVar string = "FLAGD_METRICS_PORT"
32+
fileSyncMountPath string = "/etc/flagd/"
33+
OpenFeatureEnabledAnnotationPath = "metadata.annotations.openfeature.dev/enabled"
2934
)
3035

3136
var FlagDTag = "main"
@@ -47,6 +52,47 @@ type PodMutator struct {
4752
Log logr.Logger
4853
}
4954

55+
// BackfillPermissions recovers the state of the flagd-kubernetes-sync role binding in the event of upgrade
56+
func (m *PodMutator) BackfillPermissions(ctx context.Context, backfillComplete chan struct{}) {
57+
defer func() {
58+
backfillComplete <- struct{}{}
59+
}()
60+
61+
for i := 0; i < 5; i++ {
62+
// fetch all pods with the "openfeature.dev/enabled" annotation set to "true"
63+
podList := &corev1.PodList{}
64+
err := m.Client.List(ctx, podList, client.MatchingFields{OpenFeatureEnabledAnnotationPath: "true"})
65+
if err != nil {
66+
if !goErr.Is(err, &cache.ErrCacheNotStarted{}) {
67+
m.Log.Error(err, "unable to list annotated pods", "webhook", OpenFeatureEnabledAnnotationPath)
68+
return
69+
}
70+
time.Sleep(1 * time.Second)
71+
continue
72+
}
73+
74+
// add each new service account to the flagd-kubernetes-sync role binding
75+
for _, pod := range podList.Items {
76+
m.Log.V(1).Info(fmt.Sprintf("backfilling permissions for pod %s/%s", pod.Namespace, pod.Name))
77+
if err := m.enableClusterRoleBinding(ctx, &pod); err != nil {
78+
m.Log.Error(
79+
err,
80+
fmt.Sprintf("unable backfill permissions for pod %s/%s", pod.Namespace, pod.Name),
81+
"webhook",
82+
OpenFeatureEnabledAnnotationPath,
83+
)
84+
}
85+
}
86+
return
87+
}
88+
err := goErr.New("unable to backfill permissions for the flagd-kubernetes-sync role binding: timeout")
89+
m.Log.Error(
90+
err,
91+
"webhook",
92+
OpenFeatureEnabledAnnotationPath,
93+
)
94+
}
95+
5096
// Handle injects the flagd sidecar (if the prerequisites are all met)
5197
func (m *PodMutator) Handle(ctx context.Context, req admission.Request) admission.Response {
5298
defer func() {
@@ -383,3 +429,27 @@ func setSecurityContext() *corev1.SecurityContext {
383429
},
384430
}
385431
}
432+
433+
func OpenFeatureEnabledAnnotationIndex(o client.Object) []string {
434+
pod := o.(*corev1.Pod)
435+
if pod.ObjectMeta.Annotations == nil {
436+
return []string{
437+
"false",
438+
}
439+
}
440+
val, ok := pod.ObjectMeta.Annotations["openfeature.dev/enabled"]
441+
if ok && val == "true" {
442+
return []string{
443+
"true",
444+
}
445+
}
446+
val, ok = pod.ObjectMeta.Annotations["openfeature.dev"]
447+
if ok && val == "enabled" {
448+
return []string{
449+
"true",
450+
}
451+
}
452+
return []string{
453+
"false",
454+
}
455+
}

0 commit comments

Comments
 (0)