Skip to content

Commit e7867ed

Browse files
authored
flagd customisation (#64)
Signed-off-by: Alex Jones <[email protected]>
1 parent 7a6fa5c commit e7867ed

File tree

6 files changed

+37
-19
lines changed

6 files changed

+37
-19
lines changed

apis/core/v1alpha1/featureflagconfiguration_types.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,17 @@ type FeatureFlagConfigurationSpec struct {
3535
// +optional
3636
// +nullable
3737
SyncProvider *FeatureFlagSyncProvider `json:"syncProvider"`
38+
// +optional
39+
// +nullable
40+
FlagDSpec *FlagDSpec `json:"flagDSpec"`
3841
// FeatureFlagSpec is the json representation of the feature flag
3942
FeatureFlagSpec string `json:"featureFlagSpec,omitempty"`
4043
}
4144

45+
type FlagDSpec struct {
46+
Port string `json:"port"`
47+
}
48+
4249
type FeatureFlagSyncProvider struct {
4350
// +kubebuilder:validation:Enum=filepath
4451
Name string `json:"name"`

controllers/featureflagconfiguration_controller.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,12 @@ type FeatureFlagConfigurationReconciler struct {
6262
// For more details, check Reconcile and its Result here:
6363
// - https://pkg.go.dev/sigs.k8s.io/[email protected]/pkg/reconcile
6464

65-
const crdName = "FeatureFlagConfiguration"
66-
const reconcileErrorInterval = 10 * time.Second
67-
const reconcileSuccessInterval = 120 * time.Second
68-
const finalizerName = "featureflagconfiguration.core.openfeature.dev/finalizer"
65+
const (
66+
crdName = "FeatureFlagConfiguration"
67+
reconcileErrorInterval = 10 * time.Second
68+
reconcileSuccessInterval = 120 * time.Second
69+
finalizerName = "featureflagconfiguration.core.openfeature.dev/finalizer"
70+
)
6971

7072
func (r *FeatureFlagConfigurationReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
7173
r.Log = log.FromContext(ctx)

controllers/suite_test.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,11 @@ import (
3737
// These tests use Ginkgo (BDD-style Go testing framework). Refer to
3838
// http://onsi.github.io/ginkgo/ to learn more about Ginkgo.
3939

40-
var cfg *rest.Config
41-
var k8sClient client.Client
42-
var testEnv *envtest.Environment
40+
var (
41+
cfg *rest.Config
42+
k8sClient client.Client
43+
testEnv *envtest.Environment
44+
)
4345

4446
func TestAPIs(t *testing.T) {
4547
RegisterFailHandler(Fail)
@@ -70,7 +72,6 @@ var _ = BeforeSuite(func() {
7072
k8sClient, err = client.New(cfg, client.Options{Scheme: scheme.Scheme})
7173
Expect(err).NotTo(HaveOccurred())
7274
Expect(k8sClient).NotTo(BeNil())
73-
7475
}, 60)
7576

7677
var _ = AfterSuite(func() {

main.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -88,10 +88,14 @@ func main() {
8888

8989
//+kubebuilder:scaffold:builder
9090
hookServer := mgr.GetWebhookServer()
91-
hookServer.Register("/mutate-v1-pod", &webhook.Admission{Handler: &webhooks.PodMutator{Client: mgr.GetClient(),
92-
Log: ctrl.Log.WithName("mutating-pod-webhook")}})
93-
hookServer.Register("/validate-v1alpha1-featureflagconfiguration", &webhook.Admission{Handler: &webhooks.FeatureFlagConfigurationValidator{Client: mgr.GetClient(),
94-
Log: ctrl.Log.WithName("validating-featureflagconfiguration-webhook")}})
91+
hookServer.Register("/mutate-v1-pod", &webhook.Admission{Handler: &webhooks.PodMutator{
92+
Client: mgr.GetClient(),
93+
Log: ctrl.Log.WithName("mutating-pod-webhook"),
94+
}})
95+
hookServer.Register("/validate-v1alpha1-featureflagconfiguration", &webhook.Admission{Handler: &webhooks.FeatureFlagConfigurationValidator{
96+
Client: mgr.GetClient(),
97+
Log: ctrl.Log.WithName("validating-featureflagconfiguration-webhook"),
98+
}})
9599

96100
if err := mgr.AddHealthzCheck("healthz", healthz.Ping); err != nil {
97101
setupLog.Error(err, "unable to set up health check")

webhooks/featureflagconfiguration_webhook.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ type FeatureFlagConfigurationValidator struct {
3232

3333
// FeatureFlagConfigurationValidator adds an annotation to every incoming pods.
3434
func (m *FeatureFlagConfigurationValidator) Handle(ctx context.Context, req admission.Request) admission.Response {
35-
3635
config := corev1alpha1.FeatureFlagConfiguration{}
3736
err := m.decoder.Decode(req, &config)
3837
if err != nil {
@@ -51,8 +50,10 @@ func (m *FeatureFlagConfigurationValidator) Handle(ctx context.Context, req admi
5150
if config.Spec.ServiceProvider != nil && config.Spec.ServiceProvider.Credentials != nil {
5251
// Check the provider and whether it has an existing secret
5352
providerKeySecret := corev1.Secret{}
54-
if err := m.Client.Get(ctx, client.ObjectKey{Name: config.Spec.ServiceProvider.Credentials.Name,
55-
Namespace: config.Spec.ServiceProvider.Credentials.Namespace}, &providerKeySecret); errors.IsNotFound(err) {
53+
if err := m.Client.Get(ctx, client.ObjectKey{
54+
Name: config.Spec.ServiceProvider.Credentials.Name,
55+
Namespace: config.Spec.ServiceProvider.Credentials.Namespace,
56+
}, &providerKeySecret); errors.IsNotFound(err) {
5657
return admission.Denied("credentials secret not found")
5758
}
5859
}
@@ -75,7 +76,6 @@ func (m *FeatureFlagConfigurationValidator) isJSON(str string) bool {
7576
}
7677

7778
func (m *FeatureFlagConfigurationValidator) validateJSONSchema(schemaJSON string, inputJSON string) error {
78-
7979
schemaLoader := gojsonschema.NewBytesLoader([]byte(schemaJSON))
8080
valuesLoader := gojsonschema.NewBytesLoader([]byte(inputJSON))
8181
result, err := gojsonschema.Validate(schemaLoader, valuesLoader)

webhooks/pod_webhook.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,7 @@ const (
2323
FlagDImagePullPolicy = "Always"
2424
)
2525

26-
var (
27-
FlagDTag = "main"
28-
)
26+
var FlagDTag = "main"
2927

3028
// NOTE: RBAC not needed here.
3129
//+kubebuilder:rbac:groups="",resources=pods,verbs=get;list;watch;create;update;patch;delete
@@ -159,6 +157,12 @@ func (m *PodMutator) injectSidecar(pod *corev1.Pod, configMap string, featureFla
159157
commandSequence := []string{
160158
"start", "--uri", "/etc/flagd/config.json",
161159
}
160+
// Parse the FlagDConfigurationSpec (apis/core/v1alpha1/featureflagconfiguration_types.go)
161+
if featureFlag.Spec.FlagDSpec != nil {
162+
if featureFlag.Spec.FlagDSpec.Port != "" {
163+
commandSequence = append(commandSequence, "--port", featureFlag.Spec.FlagDSpec.Port)
164+
}
165+
}
162166
// FlagD is the default provider name externally
163167
if featureFlag.Spec.ServiceProvider != nil && featureFlag.Spec.ServiceProvider.Name != "flagd" {
164168
commandSequence = append(commandSequence, "--service-provider")

0 commit comments

Comments
 (0)