From 7f9a5bf5e211eb961e8932c29fb682bafd632a8e Mon Sep 17 00:00:00 2001 From: Lyubo Kamenov Date: Wed, 2 Oct 2024 13:54:02 -0400 Subject: [PATCH] Add support for earliest conduit version available --- api/v1alpha/conduit_types.go | 4 ++-- api/v1alpha/conduit_webhook.go | 29 +++++++++++++++++++++++++++++ controllers/conduit_containers.go | 5 +---- controllers/conduit_ver.go | 29 ----------------------------- 4 files changed, 32 insertions(+), 35 deletions(-) delete mode 100644 controllers/conduit_ver.go diff --git a/api/v1alpha/conduit_types.go b/api/v1alpha/conduit_types.go index e2daeb8..99e5825 100644 --- a/api/v1alpha/conduit_types.go +++ b/api/v1alpha/conduit_types.go @@ -54,8 +54,8 @@ const ( ) var ( - ConduitPipelineFile = path.Join(ConduitPipelinePath, "pipeline.yaml") - ConduitWithProcessorsVersion = "0.9.0" + ConduitPipelineFile = path.Join(ConduitPipelinePath, "pipeline.yaml") + ConduitEarliestAvailable = "v0.11.1" ) // ConduitSpec defines the desired state of Conduit diff --git a/api/v1alpha/conduit_webhook.go b/api/v1alpha/conduit_webhook.go index a8b0ac4..afe762b 100644 --- a/api/v1alpha/conduit_webhook.go +++ b/api/v1alpha/conduit_webhook.go @@ -5,6 +5,7 @@ import ( "path/filepath" "strings" + "github.com/Masterminds/semver/v3" "github.com/hashicorp/go-multierror" corev1 "k8s.io/api/core/v1" "k8s.io/apimachinery/pkg/runtime" @@ -13,6 +14,18 @@ import ( "sigs.k8s.io/controller-runtime/pkg/webhook/admission" ) +var conduitVerConstraint *semver.Constraints + +func init() { + var err error + // validate constraint + sanitized, _ := strings.CutPrefix(ConduitEarliestAvailable, "v") + conduitVerConstraint, err = semver.NewConstraint(fmt.Sprint(">= ", sanitized)) + if err != nil { + panic(fmt.Errorf("failed to create version constraint: %w", err)) + } +} + func (r *Conduit) SetupWebhookWithManager(mgr ctrl.Manager) error { return ctrl.NewWebhookManagedBy(mgr). For(r). @@ -103,6 +116,13 @@ var _ webhook.Validator = &Conduit{} func (r *Conduit) ValidateCreate() (admission.Warnings, error) { var errs error + if ok := validateConduitVersion(r.Spec.Version); !ok { + errs = multierror.Append(errs, fmt.Errorf("unsupported conduit version %s, minimum required %s", + r.Spec.Version, + ConduitEarliestAvailable, + )) + } + if _, err := validateConnectors(r.Spec.Connectors); err != nil { errs = multierror.Append(errs, err) } @@ -165,3 +185,12 @@ func validateProcessors(pp []*ConduitProcessor) (admission.Warnings, error) { return nil, errs } + +func validateConduitVersion(ver string) bool { + sanitized, _ := strings.CutPrefix(ver, "v") + v, err := semver.NewVersion(sanitized) + if err != nil { + return false + } + return conduitVerConstraint.Check(v) +} diff --git a/controllers/conduit_containers.go b/controllers/conduit_containers.go index 09d4100..050329c 100644 --- a/controllers/conduit_containers.go +++ b/controllers/conduit_containers.go @@ -139,10 +139,7 @@ func ConduitRuntimeContainer(image, version string, envVars []corev1.EnvVar) cor "-db.type", "badger", "-db.badger.path", v1alpha.ConduitDBPath, "-pipelines.exit-on-error", - } - - if withProcessors(version) { - args = append(args, "-processors.path", v1alpha.ConduitProcessorsPath) + "-processors.path", v1alpha.ConduitProcessorsPath, } return corev1.Container{ diff --git a/controllers/conduit_ver.go b/controllers/conduit_ver.go deleted file mode 100644 index 62425de..0000000 --- a/controllers/conduit_ver.go +++ /dev/null @@ -1,29 +0,0 @@ -package controllers - -import ( - "fmt" - "strings" - - "github.com/Masterminds/semver/v3" - v1alpha "github.com/conduitio/conduit-operator/api/v1alpha" -) - -func init() { - // validate constraint - constraint := fmt.Sprint(">= ", v1alpha.ConduitWithProcessorsVersion) - if _, err := semver.NewConstraint(constraint); err != nil { - panic(fmt.Errorf("failed to create version constraint: %w", err)) - } -} - -// WithProcessors returns true when Conduit supports the new processors sdk. -// Returns false when Conduit does not offer support or when the version cannot be parsed. -func withProcessors(ver string) bool { - sanitized, _ := strings.CutPrefix(ver, "v") - v, err := semver.NewVersion(sanitized) - if err != nil { - return false - } - c, _ := semver.NewConstraint(fmt.Sprint(">= ", v1alpha.ConduitWithProcessorsVersion)) - return c.Check(v) -}