supabase-operator/internal/webhook/v1alpha1/apigateway_webhook_defaulter.go
Peter Kurfer 647f602c79
Some checks failed
Lint / Run on Ubuntu (push) Failing after 2m58s
E2E Tests / Run on Ubuntu (push) Failing after 4m18s
Tests / Run on Ubuntu (push) Failing after 2m39s
feat: basic functionality implemented
- added Core CRD to manage DB migrations & configuration, PostgREST and
  GoTrue (auth)
- added APIGateway CRD to manage Envoy proxy
- added Dashboard CRD to manage (so far) pg-meta and (soon) studio
  deployments
- implemented basic Envoy control plane based on K8s watcher
2025-01-04 17:07:49 +01:00

102 lines
3.5 KiB
Go

/*
Copyright 2024 Peter Kurfer.
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 v1alpha1
import (
"context"
"fmt"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/tools/record"
"sigs.k8s.io/controller-runtime/pkg/webhook"
supabasev1alpha1 "code.icb4dc0.de/prskr/supabase-operator/api/v1alpha1"
"code.icb4dc0.de/prskr/supabase-operator/internal/supabase"
)
// +kubebuilder:webhook:path=/mutate-supabase-k8s-icb4dc0-de-v1alpha1-apigateway,mutating=true,failurePolicy=fail,sideEffects=None,groups=supabase.k8s.icb4dc0.de,resources=apigateways,verbs=create;update,versions=v1alpha1,name=mapigateway-v1alpha1.kb.io,admissionReviewVersions=v1
// APIGatewayCustomDefaulter struct is responsible for setting default values on the custom resource of the
// Kind APIGateway when those are created or updated.
//
// NOTE: The +kubebuilder:object:generate=false marker prevents controller-gen from generating DeepCopy methods,
// as it is used only for temporary operations and does not need to be deeply copied.
type APIGatewayCustomDefaulter struct {
CurrentNamespace string
Recorder record.EventRecorder
}
var _ webhook.CustomDefaulter = &APIGatewayCustomDefaulter{}
// Default implements webhook.CustomDefaulter so a webhook will be registered for the Kind APIGateway.
func (d *APIGatewayCustomDefaulter) Default(ctx context.Context, obj runtime.Object) error {
const (
defaultManagerNamespace = "supabase-system"
)
apiGateway, ok := obj.(*supabasev1alpha1.APIGateway)
if !ok {
return fmt.Errorf("expected an APIGateway object but got %T", obj)
}
apigatewaylog.Info("Defaulting for APIGateway", "name", apiGateway.GetName())
if apiGateway.Spec.JWKSSelector == nil {
apiGateway.Spec.JWKSSelector = &corev1.SecretKeySelector{
LocalObjectReference: corev1.LocalObjectReference{
Name: supabase.ServiceConfig.JWT.ObjectName(apiGateway),
},
Key: supabase.ServiceConfig.JWT.Defaults.JwksKey,
}
}
if apiGateway.Spec.Envoy == nil {
apiGateway.Spec.Envoy = new(supabasev1alpha1.EnvoySpec)
}
if apiGateway.Spec.Envoy.ControlPlane == nil {
if d.CurrentNamespace == defaultManagerNamespace {
d.Recorder.Event(
apiGateway,
corev1.EventTypeNormal,
"Guessing Envoy control plane endpoint",
"Making guess of control plane config, most likely this is correct as the current namespace is the default namespace where the operator is deployed but of course it could be wrong as well",
)
apiGateway.Spec.Envoy.ControlPlane = &supabasev1alpha1.ControlPlaneSpec{
Host: "supabase-control-plane.supabase-system.svc",
Port: 18000,
}
} else {
d.Recorder.Eventf(
apiGateway,
corev1.EventTypeWarning,
"Guessing Envoy control plane endpoint",
"Making guess of control plane config based on the namespace of the manager (%s) - could be wrong if control plane was manually deployed to another namespace",
d.CurrentNamespace,
)
apiGateway.Spec.Envoy.ControlPlane = &supabasev1alpha1.ControlPlaneSpec{
Host: fmt.Sprintf("supabase-control-plane.%s.svc", d.CurrentNamespace),
Port: 18000,
}
}
}
return nil
}