Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

POC: running locally with webhooks enabled #559

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package controllers
import (
"bytes"
"context"
"fmt"
"github.com/otterize/intents-operator/src/operator/otterizecrds"
"github.com/otterize/intents-operator/src/shared/errors"
"github.com/otterize/intents-operator/src/shared/filters"
Expand Down Expand Up @@ -76,13 +77,16 @@ func (r *CustomResourceDefinitionsReconciler) Reconcile(ctx context.Context, req
if resourceCopy.Spec.Conversion == nil || resourceCopy.Spec.Conversion.Webhook == nil || resourceCopy.Spec.Conversion.Webhook.ClientConfig == nil || resourceCopy.Spec.Conversion.Webhook.ClientConfig.Service == nil {
return ctrl.Result{}, errors.Errorf("CRD does not contain a proper conversion webhook definition")
}
if bytes.Equal(crd.Spec.Conversion.Webhook.ClientConfig.CABundle, r.certPem) && crd.Spec.Conversion.Webhook.ClientConfig.Service.Namespace == r.namespace {
if bytes.Equal(crd.Spec.Conversion.Webhook.ClientConfig.CABundle, r.certPem) && ((crd.Spec.Conversion.Webhook.ClientConfig.Service != nil && crd.Spec.Conversion.Webhook.ClientConfig.Service.Namespace == r.namespace) || (crd.Spec.Conversion.Webhook.ClientConfig.Service == nil)) {
logrus.Infof("CustomResourceDefinition %s already has the correct CA bundle and namespace", resourceCopy.Name)
return ctrl.Result{}, nil

}
resourceCopy.Spec.Conversion.Webhook.ClientConfig.CABundle = r.certPem
resourceCopy.Spec.Conversion.Webhook.ClientConfig.Service.Namespace = r.namespace
if resourceCopy.Spec.Conversion.Webhook.ClientConfig.Service != nil {
resourceCopy.Spec.Conversion.Webhook.ClientConfig.URL = lo.ToPtr(fmt.Sprintf("http://host.minikube.internal:9443/%s", lo.FromPtr(resourceCopy.Spec.Conversion.Webhook.ClientConfig.Service.Path)))
}

resourceCopy.Spec.Conversion.Webhook.ClientConfig.Service = nil

// Use optimistic locking to avoid using "mergeFrom" with an outdated resource
if err := r.Patch(ctx, resourceCopy, client.MergeFromWithOptions(crd, client.MergeFromWithOptimisticLock{})); err != nil {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package controllers
import (
"bytes"
"context"
"fmt"
"github.com/otterize/intents-operator/src/shared/errors"
"github.com/samber/lo"
"github.com/sirupsen/logrus"
Expand Down Expand Up @@ -77,6 +78,13 @@ func (r *ValidatingWebhookConfigsReconciler) Reconcile(ctx context.Context, req
resourceCopy := webhookConfig.DeepCopy()
for i := range resourceCopy.Webhooks {
resourceCopy.Webhooks[i].ClientConfig.CABundle = r.certPEM
if resourceCopy.Webhooks[i].ClientConfig.Service == nil {
continue
}

resourceCopy.Webhooks[i].ClientConfig.URL = lo.ToPtr(fmt.Sprintf("https://host.minikube.internal:9443%s", lo.FromPtr(resourceCopy.Webhooks[i].ClientConfig.Service.Path)))
resourceCopy.Webhooks[i].ClientConfig.Service = nil

}

// Use optimistic locking to avoid using "mergeFrom" with an outdated resource
Expand Down
3 changes: 2 additions & 1 deletion src/operator/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,7 @@ func main() {
if selfSignedCert {
logrus.Infoln("Creating self signing certs")
certBundle, err :=
webhooks.GenerateSelfSignedCertificate("intents-operator-webhook-service", podNamespace)
webhooks.GenerateSelfSignedCertificate("host.minikube.internal", podNamespace)
if err != nil {
logrus.WithError(err).Panic("unable to create self signed certs for webhook")
}
Expand Down Expand Up @@ -561,6 +561,7 @@ func main() {
}

func initWebhookValidators(mgr manager.Manager) {

intentsValidator := webhooks.NewIntentsValidatorV1alpha2(mgr.GetClient())
if err := (&otterizev1alpha2.ClientIntents{}).SetupWebhookWithManager(mgr, intentsValidator); err != nil {
logrus.WithError(err).Panic(err, "unable to create webhook for v1alpha2", "webhook", "ClientIntents")
Expand Down
7 changes: 6 additions & 1 deletion src/operator/otterizecrds/ensure.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ package otterizecrds
import (
"context"
_ "embed"
"fmt"
"github.com/otterize/intents-operator/src/shared/errors"
"github.com/samber/lo"
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
k8serrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/types"
Expand Down Expand Up @@ -56,8 +58,11 @@ func ensureCRD(ctx context.Context, k8sClient client.Client, operatorNamespace s
if err != nil {
return errors.Errorf("failed to unmarshal ClientIntents CRD: %w", err)
}
crdToCreate.Spec.Conversion.Webhook.ClientConfig.Service.Namespace = operatorNamespace
crdToCreate.Spec.Conversion.Webhook.ClientConfig.CABundle = certPem
x := fmt.Sprintf("https://host.minikube.internal:9443%s", lo.FromPtr(crdToCreate.Spec.Conversion.Webhook.ClientConfig.Service.Path))
crdToCreate.Spec.Conversion.Webhook.ClientConfig.URL = lo.ToPtr(x)
crdToCreate.Spec.Conversion.Webhook.ClientConfig.Service = nil

crd := apiextensionsv1.CustomResourceDefinition{}
err = k8sClient.Get(ctx, types.NamespacedName{Name: crdToCreate.Name}, &crd)
if err != nil && !k8serrors.IsNotFound(err) {
Expand Down
4 changes: 4 additions & 0 deletions src/shared/local.env
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,7 @@ OTTERIZE_DISABLE_WEBHOOK_SERVER=true

# Credentials operator vars =====================
POD_NAMESPACE=otterize-system

# For running locally with webhooks ==============
# OTTERIZE_SELF_SIGNED_CERT=true
# OTTERIZE_DISABLE_WEBHOOK_SERVER=false
Loading