diff --git a/.goreleaser.yml b/.goreleaser.yml index 8fe3d140d..b636ecaf3 100644 --- a/.goreleaser.yml +++ b/.goreleaser.yml @@ -9,8 +9,6 @@ builds: asmflags: "{{ .Env.GO_BUILD_ASMFLAGS }}" gcflags: "{{ .Env.GO_BUILD_GCFLAGS }}" ldflags: "{{ .Env.GO_BUILD_LDFLAGS }}" - tags: - - "{{ .Env.GO_BUILD_TAGS }}" goos: - linux goarch: diff --git a/Makefile b/Makefile index 48d7ea6b5..a3f8f6910 100644 --- a/Makefile +++ b/Makefile @@ -2,52 +2,59 @@ # Configuration Variables # ########################### # Image URL to use all building/pushing image targets -export IMAGE_REPO ?= quay.io/operator-framework/operator-controller -export IMAGE_TAG ?= devel -export CERT_MGR_VERSION ?= v1.9.0 -export CATALOGD_VERSION ?= $(shell go list -mod=mod -m -f "{{.Version}}" github.com/operator-framework/catalogd) -export KAPP_VERSION ?= $(shell go list -mod=mod -m -f "{{.Version}}" github.com/vmware-tanzu/carvel-kapp-controller) -export RUKPAK_VERSION=$(shell go list -mod=mod -m -f "{{.Version}}" github.com/operator-framework/rukpak) -export WAIT_TIMEOUT ?= 60s -IMG?=$(IMAGE_REPO):$(IMAGE_TAG) -TESTDATA_DIR := testdata - -# setup-envtest on *nix uses XDG_DATA_HOME, falling back to HOME, as the default storage directory. Some CI setups -# don't have XDG_DATA_HOME set; in those cases, we set it here so setup-envtest functions correctly. This shouldn't -# affect developers. -export XDG_DATA_HOME ?= /tmp/.local/share +ifeq ($(origin IMAGE_REPO), undefined) +IMAGE_REPO := quay.io/operator-framework/operator-controller +endif +export IMAGE_REPO + +ifeq ($(origin IMAGE_TAG), undefined) +IMAGE_TAG := devel +endif +export IMAGE_TAG + +IMG := $(IMAGE_REPO):$(IMAGE_TAG) + + +# Define dependency versions (use go.mod if we also use Go code from dependency) +export CERT_MGR_VERSION := v1.9.0 +export CATALOGD_VERSION := $(shell go list -mod=mod -m -f "{{.Version}}" github.com/operator-framework/catalogd) +export KAPP_VERSION := $(shell go list -mod=mod -m -f "{{.Version}}" github.com/vmware-tanzu/carvel-kapp-controller) +export RUKPAK_VERSION := $(shell go list -mod=mod -m -f "{{.Version}}" github.com/operator-framework/rukpak) +export WAIT_TIMEOUT := 60s + +# By default setup-envtest will write to $XDG_DATA_HOME, or $HOME/.local/share if that is not defined. +# If $HOME is not set, we need to specify a binary directory to prevent an error in setup-envtest. +# Useful for some CI/CD environments that set neither $XDG_DATA_HOME nor $HOME. +SETUP_ENVTEST_BIN_DIR_OVERRIDE= +ifeq ($(shell [[ $$HOME == "" || $$HOME == "/" ]] && [[ $$XDG_DATA_HOME == "" ]] && echo true ), true) + SETUP_ENVTEST_BIN_DIR_OVERRIDE += --bin-dir /tmp/envtest-binaries +endif # bingo manages consistent tooling versions for things like kind, kustomize, etc. include .bingo/Variables.mk -# ARTIFACT_PATH is the absolute path to the directory where the operator-controller e2e tests will store the artifacts -# for example: ARTIFACT_PATH=/tmp/artifacts make test -export ARTIFACT_PATH ?= - -OPERATOR_CONTROLLER_NAMESPACE ?= operator-controller-system -KIND_CLUSTER_NAME ?= operator-controller +KIND_CLUSTER_NAME := operator-controller # Not guaranteed to have patch releases available and node image tags are full versions (i.e v1.28.0 - no v1.28, v1.29, etc.) # The KIND_NODE_VERSION is set by getting the version of the k8s.io/client-go dependency from the go.mod # and sets major version to "1" and the patch version to "0". For example, a client-go version of v0.28.5 # will map to a KIND_NODE_VERSION of 1.28.0 -KIND_NODE_VERSION = $(shell go list -m k8s.io/client-go | cut -d" " -f2 | sed 's/^v0\.\([[:digit:]]\{1,\}\)\.[[:digit:]]\{1,\}$$/1.\1.0/') -KIND_CLUSTER_IMAGE ?= kindest/node:v${KIND_NODE_VERSION} - -CONTAINER_RUNTIME ?= docker +KIND_NODE_VERSION := $(shell go list -m k8s.io/client-go | cut -d" " -f2 | sed 's/^v0\.\([[:digit:]]\{1,\}\)\.[[:digit:]]\{1,\}$$/1.\1.0/') +KIND_CLUSTER_IMAGE := kindest/node:v$(KIND_NODE_VERSION) -KUSTOMIZE_BUILD_DIR ?= config/default - -# Get the currently used golang install path (in GOPATH/bin, unless GOBIN is set) -ifeq (,$(shell go env GOBIN)) -GOBIN=$(shell go env GOPATH)/bin +ifneq (, $(shell command -v docker 2>/dev/null)) +CONTAINER_RUNTIME := docker +else ifneq (, $(shell command -v podman 2>/dev/null)) +CONTAINER_RUNTIME := podman else -GOBIN=$(shell go env GOBIN) +$(error Could not find docker or podman in path!) endif +KUSTOMIZE_BUILD_DIR := config/default + # Setting SHELL to bash allows bash commands to be executed by recipes. # Options are set to exit when a recipe line exits non-zero or a piped command fails. -SHELL = /usr/bin/env bash -o pipefail -.SHELLFLAGS = -ec +SHELL := /usr/bin/env bash -o pipefail +.SHELLFLAGS := -ec # Disable -j flag for make .NOTPARALLEL: @@ -80,7 +87,7 @@ help-extended: #HELP Display extended help. .PHONY: lint lint: $(GOLANGCI_LINT) #HELP Run golangci linter. - $(GOLANGCI_LINT) run --build-tags $(GO_BUILD_TAGS) $(GOLANGCI_LINT_ARGS) + $(GOLANGCI_LINT) run $(GOLANGCI_LINT_ARGS) .PHONY: tidy tidy: #HELP Update dependencies. @@ -111,38 +118,43 @@ test: manifests generate fmt vet test-unit test-e2e #HELP Run all tests. .PHONY: e2e e2e: $(SETUP_ENVTEST) #EXHELP Run the e2e tests. - go test -tags $(GO_BUILD_TAGS) -v ./test/e2e/... + go test -v ./test/e2e/... -export REG_PKG_NAME=registry-operator -export PLAIN_PKG_NAME=plain-operator -export CATALOG_IMG=${E2E_REGISTRY_NAME}.${E2E_REGISTRY_NAMESPACE}.svc:5000/test-catalog:e2e +E2E_REGISTRY_NAME := docker-registry +E2E_REGISTRY_NAMESPACE := operator-controller-e2e +export REG_PKG_NAME := registry-operator +export PLAIN_PKG_NAME := plain-operator +export CATALOG_IMG := $(E2E_REGISTRY_NAME).$(E2E_REGISTRY_NAMESPACE).svc:5000/test-catalog:e2e .PHONY: test-ext-dev-e2e test-ext-dev-e2e: $(SETUP_ENVTEST) $(OPERATOR_SDK) $(KUSTOMIZE) $(KIND) #HELP Run extension create, upgrade and delete tests. - test/extension-developer-e2e/setup.sh $(OPERATOR_SDK) $(CONTAINER_RUNTIME) $(KUSTOMIZE) $(KIND) $(KIND_CLUSTER_NAME) ${E2E_REGISTRY_NAMESPACE} - go test -tags $(GO_BUILD_TAGS) -v ./test/extension-developer-e2e/... + test/extension-developer-e2e/setup.sh $(OPERATOR_SDK) $(CONTAINER_RUNTIME) $(KUSTOMIZE) $(KIND) $(KIND_CLUSTER_NAME) $(E2E_REGISTRY_NAMESPACE) + go test -v ./test/extension-developer-e2e/... .PHONY: test-unit -ENVTEST_VERSION = $(shell go list -m k8s.io/client-go | cut -d" " -f2 | sed 's/^v0\.\([[:digit:]]\{1,\}\)\.[[:digit:]]\{1,\}$$/1.\1.x/') -UNIT_TEST_DIRS=$(shell go list ./... | grep -v /test/) +ENVTEST_VERSION := $(shell go list -m k8s.io/client-go | cut -d" " -f2 | sed 's/^v0\.\([[:digit:]]\{1,\}\)\.[[:digit:]]\{1,\}$$/1.\1.x/') +UNIT_TEST_DIRS := $(shell go list ./... | grep -v /test/) test-unit: $(SETUP_ENVTEST) #HELP Run the unit tests - eval $$($(SETUP_ENVTEST) use -p env $(ENVTEST_VERSION)) && go test -tags $(GO_BUILD_TAGS) -count=1 -short $(UNIT_TEST_DIRS) -coverprofile cover.out + eval $$($(SETUP_ENVTEST) use -p env $(ENVTEST_VERSION) $(SETUP_ENVTEST_BIN_DIR_OVERRIDE)) && go test -count=1 -short $(UNIT_TEST_DIRS) -coverprofile cover.out -E2E_REGISTRY_NAME=docker-registry -E2E_REGISTRY_NAMESPACE=operator-controller-e2e image-registry: ## Setup in-cluster image registry - ./test/tools/image-registry.sh ${E2E_REGISTRY_NAMESPACE} ${E2E_REGISTRY_NAME} + ./test/tools/image-registry.sh $(E2E_REGISTRY_NAMESPACE) $(E2E_REGISTRY_NAME) build-push-e2e-catalog: ## Build the testdata catalog used for e2e tests and push it to the image registry - ./test/tools/build-push-e2e-catalog.sh ${E2E_REGISTRY_NAMESPACE} ${CATALOG_IMG} + ./test/tools/build-push-e2e-catalog.sh $(E2E_REGISTRY_NAMESPACE) $(CATALOG_IMG) +# When running the e2e suite, you can set the ARTIFACT_PATH variable to the absolute path +# of the directory for the operator-controller e2e tests to store the artifacts, which +# may be helpful for debugging purposes after a test run. +# +# for example: ARTIFACT_PATH=/tmp/artifacts make test-e2e .PHONY: test-e2e -test-e2e: KIND_CLUSTER_NAME=operator-controller-e2e -test-e2e: KUSTOMIZE_BUILD_DIR=config/e2e -test-e2e: GO_BUILD_FLAGS=-cover +test-e2e: KIND_CLUSTER_NAME := operator-controller-e2e +test-e2e: KUSTOMIZE_BUILD_DIR := config/e2e +test-e2e: GO_BUILD_FLAGS := -cover test-e2e: run image-registry build-push-e2e-catalog kind-load-test-artifacts e2e e2e-coverage kind-clean #HELP Run e2e test suite on local kind cluster .PHONY: extension-developer-e2e -extension-developer-e2e: KIND_CLUSTER_NAME=operator-controller-ext-dev-e2e #EXHELP Run extension-developer e2e on local kind cluster +extension-developer-e2e: KIND_CLUSTER_NAME := operator-controller-ext-dev-e2e #EXHELP Run extension-developer e2e on local kind cluster extension-developer-e2e: run image-registry test-ext-dev-e2e kind-clean .PHONY: e2e-coverage @@ -151,39 +163,31 @@ e2e-coverage: .PHONY: kind-load kind-load: $(KIND) #EXHELP Loads the currently constructed image onto the cluster. -ifeq ($(CONTAINER_RUNTIME),podman) - @echo "Using Podman" - podman save $(IMG) -o $(IMG).tar - $(KIND) load image-archive $(IMG).tar --name $(KIND_CLUSTER_NAME) - rm $(IMG).tar -else - @echo "Using Docker" - $(KIND) load docker-image $(IMG) --name $(KIND_CLUSTER_NAME) -endif + $(CONTAINER_RUNTIME) save $(IMG) | $(KIND) load image-archive /dev/stdin --name $(KIND_CLUSTER_NAME) -kind-deploy: export MANIFEST="./operator-controller.yaml" +kind-deploy: export MANIFEST := ./operator-controller.yaml kind-deploy: manifests $(KUSTOMIZE) #EXHELP Install controller and dependencies onto the kind cluster. $(KUSTOMIZE) build $(KUSTOMIZE_BUILD_DIR) > operator-controller.yaml envsubst '$$CATALOGD_VERSION,$$CERT_MGR_VERSION,$$KAPP_VERSION,$$RUKPAK_VERSION,$$MANIFEST' < scripts/install.tpl.sh | bash -s .PHONY: kind-cluster kind-cluster: $(KIND) #EXHELP Standup a kind cluster. - -$(KIND) delete cluster --name ${KIND_CLUSTER_NAME} + -$(KIND) delete cluster --name $(KIND_CLUSTER_NAME) # kind-config.yaml can be deleted after upgrading to Kubernetes 1.30 - $(KIND) create cluster --name ${KIND_CLUSTER_NAME} --image ${KIND_CLUSTER_IMAGE} --config ./kind-config.yaml - $(KIND) export kubeconfig --name ${KIND_CLUSTER_NAME} + $(KIND) create cluster --name $(KIND_CLUSTER_NAME) --image $(KIND_CLUSTER_IMAGE) --config ./kind-config.yaml + $(KIND) export kubeconfig --name $(KIND_CLUSTER_NAME) .PHONY: kind-clean kind-clean: $(KIND) #EXHELP Delete the kind cluster. - $(KIND) delete cluster --name ${KIND_CLUSTER_NAME} + $(KIND) delete cluster --name $(KIND_CLUSTER_NAME) .PHONY: kind-load-test-artifacts kind-load-test-artifacts: $(KIND) #EXHELP Load the e2e testdata container images into a kind cluster. - $(CONTAINER_RUNTIME) build $(TESTDATA_DIR)/bundles/registry-v1/prometheus-operator.v1.0.0 -t localhost/testdata/bundles/registry-v1/prometheus-operator:v1.0.0 + $(CONTAINER_RUNTIME) build testdata/bundles/registry-v1/prometheus-operator.v1.0.0 -t localhost/testdata/bundles/registry-v1/prometheus-operator:v1.0.0 $(CONTAINER_RUNTIME) tag localhost/testdata/bundles/registry-v1/prometheus-operator:v1.0.0 localhost/testdata/bundles/registry-v1/prometheus-operator:v1.0.1 $(CONTAINER_RUNTIME) tag localhost/testdata/bundles/registry-v1/prometheus-operator:v1.0.0 localhost/testdata/bundles/registry-v1/prometheus-operator:v1.2.0 $(CONTAINER_RUNTIME) tag localhost/testdata/bundles/registry-v1/prometheus-operator:v1.0.0 localhost/testdata/bundles/registry-v1/prometheus-operator:v2.0.0 - $(CONTAINER_RUNTIME) build $(TESTDATA_DIR)/bundles/plain-v0/plain.v0.1.0 -t localhost/testdata/bundles/plain-v0/plain:v0.1.0 + $(CONTAINER_RUNTIME) build testdata/bundles/plain-v0/plain.v0.1.0 -t localhost/testdata/bundles/plain-v0/plain:v0.1.0 $(KIND) load docker-image localhost/testdata/bundles/registry-v1/prometheus-operator:v1.0.0 --name $(KIND_CLUSTER_NAME) $(KIND) load docker-image localhost/testdata/bundles/registry-v1/prometheus-operator:v1.0.1 --name $(KIND_CLUSTER_NAME) $(KIND) load docker-image localhost/testdata/bundles/registry-v1/prometheus-operator:v1.2.0 --name $(KIND_CLUSTER_NAME) @@ -193,28 +197,38 @@ kind-load-test-artifacts: $(KIND) #EXHELP Load the e2e testdata container images #SECTION Build -export VERSION ?= $(shell git describe --tags --always --dirty) -export CGO_ENABLED ?= 0 -export GO_BUILD_ASMFLAGS ?= all=-trimpath=${PWD} -export GO_BUILD_LDFLAGS ?= -s -w -X $(shell go list -m)/version.Version=$(VERSION) -export GO_BUILD_GCFLAGS ?= all=-trimpath=${PWD} -export GO_BUILD_TAGS ?= upstream -export GO_BUILD_FLAGS ?= +ifeq ($(origin VERSION), undefined) +VERSION := $(shell git describe --tags --always --dirty) +endif +export VERSION + +ifeq ($(origin CGO_ENABLED), undefined) +CGO_ENABLED := 0 +endif +export CGO_ENABLED + +export GIT_REPO := $(shell go list -m) +export VERSION_PATH := ${GIT_REPO}/internal/version +export GO_BUILD_ASMFLAGS := all=-trimpath=$(PWD) +export GO_BUILD_GCFLAGS := all=-trimpath=$(PWD) +export GO_BUILD_FLAGS := +export GO_BUILD_LDFLAGS := -s -w \ + -X '$(VERSION_PATH).version=$(VERSION)' \ -BUILDCMD = go build $(GO_BUILD_FLAGS) -tags '$(GO_BUILD_TAGS)' -ldflags '$(GO_BUILD_LDFLAGS)' -gcflags '$(GO_BUILD_GCFLAGS)' -asmflags '$(GO_BUILD_ASMFLAGS)' -o $(BUILDBIN)/manager ./cmd/manager +BUILDCMD = go build $(GO_BUILD_FLAGS) -ldflags '$(GO_BUILD_LDFLAGS)' -gcflags '$(GO_BUILD_GCFLAGS)' -asmflags '$(GO_BUILD_ASMFLAGS)' -o $(BUILDBIN)/manager ./cmd/manager .PHONY: build-deps build-deps: manifests generate fmt vet .PHONY: build go-build-local build: build-deps go-build-local #HELP Build manager binary for current GOOS and GOARCH. Default target. -go-build-local: BUILDBIN = bin +go-build-local: BUILDBIN := bin go-build-local: $(BUILDCMD) .PHONY: build-linux go-build-linux build-linux: build-deps go-build-linux #EXHELP Build manager binary for GOOS=linux and local GOARCH. -go-build-linux: BUILDBIN = bin/linux +go-build-linux: BUILDBIN := bin/linux go-build-linux: GOOS=linux $(BUILDCMD) @@ -223,27 +237,32 @@ run: docker-build kind-cluster kind-load kind-deploy #HELP Build the operator-co .PHONY: docker-build docker-build: build-linux #EXHELP Build docker image for operator-controller with GOOS=linux and local GOARCH. - $(CONTAINER_RUNTIME) build -t ${IMG} -f Dockerfile ./bin/linux + $(CONTAINER_RUNTIME) build -t $(IMG) -f Dockerfile ./bin/linux #SECTION Release +ifeq ($(origin ENABLE_RELEASE_PIPELINE), undefined) +ENABLE_RELEASE_PIPELINE := false +endif +ifeq ($(origin GORELEASER_ARGS), undefined) +GORELEASER_ARGS := --snapshot --clean +endif -export ENABLE_RELEASE_PIPELINE ?= false -export GORELEASER_ARGS ?= --snapshot --clean +export ENABLE_RELEASE_PIPELINE +export GORELEASER_ARGS .PHONY: release release: $(GORELEASER) #EXHELP Runs goreleaser for the operator-controller. By default, this will run only as a snapshot and will not publish any artifacts unless it is run with different arguments. To override the arguments, run with "GORELEASER_ARGS=...". When run as a github action from a tag, this target will publish a full release. $(GORELEASER) $(GORELEASER_ARGS) .PHONY: quickstart -quickstart: export MANIFEST="https://github.com/operator-framework/operator-controller/releases/download/$(VERSION)/operator-controller.yaml" +quickstart: export MANIFEST := https://github.com/operator-framework/operator-controller/releases/download/$(VERSION)/operator-controller.yaml quickstart: $(KUSTOMIZE) manifests #EXHELP Generate the installation release manifests and scripts. $(KUSTOMIZE) build $(KUSTOMIZE_BUILD_DIR) | sed "s/:devel/:$(VERSION)/g" > operator-controller.yaml envsubst '$$CATALOGD_VERSION,$$CERT_MGR_VERSION,$$KAPP_VERSION,$$RUKPAK_VERSION,$$MANIFEST' < scripts/install.tpl.sh > install.sh ##@ Docs -VENVDIR=$(abspath docs/.venv) -REQUIREMENTS_TXT=docs/requirements.txt +VENVDIR := $(abspath docs/.venv) .PHONY: build-docs build-docs: venv diff --git a/api/v1alpha1/clusterextension_types.go b/api/v1alpha1/clusterextension_types.go index 0d7f0b83d..cd87db021 100644 --- a/api/v1alpha1/clusterextension_types.go +++ b/api/v1alpha1/clusterextension_types.go @@ -66,11 +66,13 @@ type ClusterExtensionSpec struct { // Defines the policy for how to handle upgrade constraints UpgradeConstraintPolicy UpgradeConstraintPolicy `json:"upgradeConstraintPolicy,omitempty"` - //+kubebuilder:Optional + //+kubebuilder:validation:Pattern:=^[a-z0-9]([-a-z0-9]*[a-z0-9])?$ + //+kubebuilder:validation:MaxLength:=63 // - // watchNamespaces indicates which namespaces the extension should watch. - // This feature is currently supported only with RegistryV1 bundles. - WatchNamespaces []string `json:"watchNamespaces,omitempty"` + // installNamespace is the namespace where the bundle should be installed. However, note that + // the bundle may contain resources that are cluster-scoped or that are + // installed in a different namespace. This namespace is expected to exist. + InstallNamespace string `json:"installNamespace"` } const ( diff --git a/api/v1alpha1/zz_generated.deepcopy.go b/api/v1alpha1/zz_generated.deepcopy.go index cef0eb870..bc1276b2f 100644 --- a/api/v1alpha1/zz_generated.deepcopy.go +++ b/api/v1alpha1/zz_generated.deepcopy.go @@ -45,7 +45,7 @@ func (in *ClusterExtension) DeepCopyInto(out *ClusterExtension) { *out = *in out.TypeMeta = in.TypeMeta in.ObjectMeta.DeepCopyInto(&out.ObjectMeta) - in.Spec.DeepCopyInto(&out.Spec) + out.Spec = in.Spec in.Status.DeepCopyInto(&out.Status) } @@ -102,11 +102,6 @@ func (in *ClusterExtensionList) DeepCopyObject() runtime.Object { // DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. func (in *ClusterExtensionSpec) DeepCopyInto(out *ClusterExtensionSpec) { *out = *in - if in.WatchNamespaces != nil { - in, out := &in.WatchNamespaces, &out.WatchNamespaces - *out = make([]string, len(*in)) - copy(*out, *in) - } } // DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new ClusterExtensionSpec. diff --git a/cmd/manager/main.go b/cmd/manager/main.go index 64038ed3c..56b9e6bfe 100644 --- a/cmd/manager/main.go +++ b/cmd/manager/main.go @@ -18,6 +18,7 @@ package main import ( "flag" + "fmt" "net/http" "os" "time" @@ -30,11 +31,10 @@ import ( "sigs.k8s.io/controller-runtime/pkg/log/zap" "sigs.k8s.io/controller-runtime/pkg/metrics/server" - "github.com/operator-framework/deppy/pkg/deppy/solver" - "github.com/operator-framework/operator-controller/internal/catalogmetadata/cache" catalogclient "github.com/operator-framework/operator-controller/internal/catalogmetadata/client" "github.com/operator-framework/operator-controller/internal/controllers" + "github.com/operator-framework/operator-controller/internal/version" "github.com/operator-framework/operator-controller/pkg/features" "github.com/operator-framework/operator-controller/pkg/scheme" ) @@ -45,10 +45,11 @@ var ( func main() { var ( - metricsAddr string - enableLeaderElection bool - probeAddr string - cachePath string + metricsAddr string + enableLeaderElection bool + probeAddr string + cachePath string + operatorControllerVersion bool ) flag.StringVar(&metricsAddr, "metrics-bind-address", ":8080", "The address the metric endpoint binds to.") flag.StringVar(&probeAddr, "health-probe-bind-address", ":8081", "The address the probe endpoint binds to.") @@ -56,6 +57,7 @@ func main() { "Enable leader election for controller manager. "+ "Enabling this will ensure there is only one active controller manager.") flag.StringVar(&cachePath, "cache-path", "/var/cache", "The local directory path used for filesystem based caching") + flag.BoolVar(&operatorControllerVersion, "version", false, "Prints operator-controller version information") opts := zap.Options{ Development: true, } @@ -65,7 +67,13 @@ func main() { features.OperatorControllerFeatureGate.AddFlag(pflag.CommandLine) pflag.Parse() + if operatorControllerVersion { + fmt.Println(version.String()) + os.Exit(0) + } + ctrl.SetLogger(zap.New(zap.UseFlagOptions(&opts), zap.StacktraceLevel(zapcore.DPanicLevel))) + setupLog.Info("starting up the controller", "version info", version.String()) mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{ Scheme: scheme.Scheme, @@ -93,17 +101,9 @@ func main() { cl := mgr.GetClient() catalogClient := catalogclient.New(cl, cache.NewFilesystemCache(cachePath, &http.Client{Timeout: 10 * time.Second})) - resolver, err := solver.New() - if err != nil { - setupLog.Error(err, "unable to create a solver") - os.Exit(1) - } - if err = (&controllers.ClusterExtensionReconciler{ Client: cl, BundleProvider: catalogClient, - Scheme: mgr.GetScheme(), - Resolver: resolver, }).SetupWithManager(mgr); err != nil { setupLog.Error(err, "unable to create controller", "controller", "ClusterExtension") os.Exit(1) diff --git a/config/crd/bases/olm.operatorframework.io_clusterextensions.yaml b/config/crd/bases/olm.operatorframework.io_clusterextensions.yaml index bafec1096..2b0ebb960 100644 --- a/config/crd/bases/olm.operatorframework.io_clusterextensions.yaml +++ b/config/crd/bases/olm.operatorframework.io_clusterextensions.yaml @@ -44,6 +44,14 @@ spec: maxLength: 48 pattern: ^[a-z0-9]+([\.-][a-z0-9]+)*$ type: string + installNamespace: + description: |- + installNamespace is the namespace where the bundle should be installed. However, note that + the bundle may contain resources that are cluster-scoped or that are + installed in a different namespace. This namespace is expected to exist. + maxLength: 63 + pattern: ^[a-z0-9]([-a-z0-9]*[a-z0-9])?$ + type: string packageName: maxLength: 48 pattern: ^[a-z0-9]+(-[a-z0-9]+)*$ @@ -66,14 +74,8 @@ spec: maxLength: 64 pattern: ^(\s*(=||!=|>|<|>=|=>|<=|=<|~|~>|\^)\s*(v?(0|[1-9]\d*|[x|X|\*])(\.(0|[1-9]\d*|x|X|\*]))?(\.(0|[1-9]\d*|x|X|\*))?(-([0-9A-Za-z\-]+(\.[0-9A-Za-z\-]+)*))?(\+([0-9A-Za-z\-]+(\.[0-9A-Za-z\-]+)*))?)\s*)((?:\s+|,\s*|\s*\|\|\s*)(=||!=|>|<|>=|=>|<=|=<|~|~>|\^)\s*(v?(0|[1-9]\d*|x|X|\*])(\.(0|[1-9]\d*|x|X|\*))?(\.(0|[1-9]\d*|x|X|\*]))?(-([0-9A-Za-z\-]+(\.[0-9A-Za-z\-]+)*))?(\+([0-9A-Za-z\-]+(\.[0-9A-Za-z\-]+)*))?)\s*)*$ type: string - watchNamespaces: - description: |- - watchNamespaces indicates which namespaces the extension should watch. - This feature is currently supported only with RegistryV1 bundles. - items: - type: string - type: array required: + - installNamespace - packageName type: object status: diff --git a/config/e2e/manager_e2e_coverage_copy_pod.yaml b/config/e2e/manager_e2e_coverage_copy_pod.yaml index 3802cc265..7794ba97d 100644 --- a/config/e2e/manager_e2e_coverage_copy_pod.yaml +++ b/config/e2e/manager_e2e_coverage_copy_pod.yaml @@ -18,6 +18,7 @@ spec: capabilities: drop: - "ALL" + terminationMessagePolicy: FallbackToLogsOnError volumeMounts: - name: e2e-coverage-volume mountPath: /e2e-coverage diff --git a/config/manager/manager.yaml b/config/manager/manager.yaml index bde0f91a7..5c3b89f91 100644 --- a/config/manager/manager.yaml +++ b/config/manager/manager.yaml @@ -84,6 +84,7 @@ spec: requests: cpu: 10m memory: 64Mi + terminationMessagePolicy: FallbackToLogsOnError - name: kube-rbac-proxy securityContext: allowPrivilegeEscalation: false @@ -104,6 +105,7 @@ spec: requests: cpu: 5m memory: 64Mi + terminationMessagePolicy: FallbackToLogsOnError serviceAccountName: controller-manager terminationGracePeriodSeconds: 10 volumes: diff --git a/docs/requirements.txt b/docs/requirements.txt deleted file mode 100644 index 21bd60152..000000000 --- a/docs/requirements.txt +++ /dev/null @@ -1,35 +0,0 @@ -Babel==2.14.0 -beautifulsoup4==4.12.3 -certifi==2024.2.2 -charset-normalizer==3.3.2 -click==8.1.7 -colorama==0.4.6 -cssselect==1.2.0 -ghp-import==2.1.0 -idna==3.7 -Jinja2==3.1.3 -lxml==5.2.1 -Markdown==3.6 -markdown2==2.4.13 -MarkupSafe==2.1.5 -mergedeep==1.3.4 -mkdocs==1.5.3 -mkdocs-material==9.5.18 -mkdocs-material-extensions==1.3.1 -packaging==24.0 -paginate==0.5.6 -pathspec==0.12.1 -platformdirs==4.2.1 -Pygments==2.17.2 -pymdown-extensions==10.8 -pyquery==2.0.0 -python-dateutil==2.9.0.post0 -PyYAML==6.0.1 -pyyaml_env_tag==0.1 -readtime==3.0.0 -regex==2024.4.16 -requests==2.31.0 -six==1.16.0 -soupsieve==2.5 -urllib3==2.2.1 -watchdog==4.0.0 diff --git a/go.mod b/go.mod index d00b6856c..e4442fbe7 100644 --- a/go.mod +++ b/go.mod @@ -10,9 +10,8 @@ require ( github.com/go-logr/logr v1.4.1 github.com/google/go-cmp v0.6.0 github.com/operator-framework/catalogd v0.12.0 - github.com/operator-framework/deppy v0.3.0 github.com/operator-framework/operator-registry v1.40.0 - github.com/operator-framework/rukpak v0.19.0 + github.com/operator-framework/rukpak v0.20.0 github.com/spf13/pflag v1.0.5 github.com/stretchr/testify v1.9.0 github.com/vmware-tanzu/carvel-kapp-controller v0.51.0 @@ -36,10 +35,9 @@ require ( github.com/evanphx/json-patch v5.7.0+incompatible // indirect github.com/evanphx/json-patch/v5 v5.8.0 // indirect github.com/fsnotify/fsnotify v1.7.0 // indirect - github.com/go-air/gini v1.0.4 // indirect github.com/go-git/gcfg v1.5.1-0.20230307220236-3a3c6141e376 // indirect github.com/go-git/go-billy/v5 v5.5.0 // indirect - github.com/go-git/go-git/v5 v5.11.0 // indirect + github.com/go-git/go-git/v5 v5.12.0 // indirect github.com/go-logr/zapr v1.3.0 // indirect github.com/go-openapi/jsonpointer v0.20.2 // indirect github.com/go-openapi/jsonreference v0.20.4 // indirect @@ -64,9 +62,9 @@ require ( github.com/operator-framework/api v0.23.0 // indirect github.com/pkg/errors v0.9.1 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect - github.com/prometheus/client_golang v1.18.0 // indirect + github.com/prometheus/client_golang v1.19.0 // indirect github.com/prometheus/client_model v0.6.0 // indirect - github.com/prometheus/common v0.47.0 // indirect + github.com/prometheus/common v0.48.0 // indirect github.com/prometheus/procfs v0.12.0 // indirect github.com/sirupsen/logrus v1.9.3 // indirect go.uber.org/multierr v1.11.0 // indirect diff --git a/go.sum b/go.sum index b90d8ad02..f665c6c2d 100644 --- a/go.sum +++ b/go.sum @@ -24,14 +24,12 @@ github.com/evanphx/json-patch/v5 v5.8.0 h1:lRj6N9Nci7MvzrXuX6HFzU8XjmhPiXPlsKEy1 github.com/evanphx/json-patch/v5 v5.8.0/go.mod h1:VNkHZ/282BpEyt/tObQO8s5CMPmYYq14uClGH4abBuQ= github.com/fsnotify/fsnotify v1.7.0 h1:8JEhPFa5W2WU7YfeZzPNqzMP6Lwt7L2715Ggo0nosvA= github.com/fsnotify/fsnotify v1.7.0/go.mod h1:40Bi/Hjc2AVfZrqy+aj+yEI+/bRxZnMJyTJwOpGvigM= -github.com/go-air/gini v1.0.4 h1:lteMAxHKNOAjIqazL/klOJJmxq6YxxSuJ17MnMXny+s= -github.com/go-air/gini v1.0.4/go.mod h1:dd8RvT1xcv6N1da33okvBd8DhMh1/A4siGy6ErjTljs= github.com/go-git/gcfg v1.5.1-0.20230307220236-3a3c6141e376 h1:+zs/tPmkDkHx3U66DAb0lQFJrpS6731Oaa12ikc+DiI= github.com/go-git/gcfg v1.5.1-0.20230307220236-3a3c6141e376/go.mod h1:an3vInlBmSxCcxctByoQdvwPiA7DTK7jaaFDBTtu0ic= github.com/go-git/go-billy/v5 v5.5.0 h1:yEY4yhzCDuMGSv83oGxiBotRzhwhNr8VZyphhiu+mTU= github.com/go-git/go-billy/v5 v5.5.0/go.mod h1:hmexnoNsr2SJU1Ju67OaNz5ASJY3+sHgFRpCtpDCKow= -github.com/go-git/go-git/v5 v5.11.0 h1:XIZc1p+8YzypNr34itUfSvYJcv+eYdTnTvOZ2vD3cA4= -github.com/go-git/go-git/v5 v5.11.0/go.mod h1:6GFcX2P3NM7FPBfpePbpLd21XxsgdAt+lKqXmCUiUCY= +github.com/go-git/go-git/v5 v5.12.0 h1:7Md+ndsjrzZxbddRDZjF14qK+NN56sy6wkqaVrjZtys= +github.com/go-git/go-git/v5 v5.12.0/go.mod h1:FTM9VKtnI2m65hNI/TenDDDnUf2Q9FHnXYjuz9i5OEY= github.com/go-logr/logr v1.4.1 h1:pKouT5E8xu9zeFC39JXRDukb6JFQPXM5p5I91188VAQ= github.com/go-logr/logr v1.4.1/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY= github.com/go-logr/zapr v1.3.0 h1:XGdV8XW8zdwFiwOA2Dryh1gj2KRQyOOoNmBy4EplIcQ= @@ -43,7 +41,8 @@ github.com/go-openapi/jsonreference v0.20.4/go.mod h1:5pZJyJP2MnYCpoeoMAql78cCHa github.com/go-openapi/swag v0.22.10 h1:4y86NVn7Z2yYd6pfS4Z+Nyh3aAUL3Nul+LMbhFKy0gA= github.com/go-openapi/swag v0.22.10/go.mod h1:Cnn8BYtRlx6BNE3DPN86f/xkapGIcLWzh3CLEb4C1jI= github.com/go-task/slim-sprig v0.0.0-20230315185526-52ccab3ef572 h1:tfuBGBXKqDEevZMzYi5KSi8KkcZtzBcTgAUUtapy0OI= -github.com/go-task/slim-sprig v0.0.0-20230315185526-52ccab3ef572/go.mod h1:9Pwr4B2jHnOSGXyyzV8ROjYa2ojvAY6HCGYYfMoC3Ls= +github.com/go-task/slim-sprig/v3 v3.0.0 h1:sUs3vkvUymDpBKi3qH1YSqBQk9+9D/8M2mN1vB6EwHI= +github.com/go-task/slim-sprig/v3 v3.0.0/go.mod h1:W848ghGpv3Qj3dhTPRyJypKRiqCdHZiAzKg9hl15HA8= github.com/gogo/protobuf v1.3.2 h1:Ov1cvc58UF3b5XjBnZv7+opcTcQFZebYjWzi34vdm4Q= github.com/gogo/protobuf v1.3.2/go.mod h1:P1XiOD3dCwIKUDQYPy72D8LYyHL2YPYrpS2s69NZV8Q= github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da h1:oI5xCqsCo564l8iNU+DwB5epxmsaqB+rhGL0m5jtYqE= @@ -63,8 +62,8 @@ github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeN github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= github.com/google/gofuzz v1.2.0 h1:xRy4A+RhZaiKjJ1bPfwQ8sedCA+YS2YcCHW6ec7JMi0= github.com/google/gofuzz v1.2.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= -github.com/google/pprof v0.0.0-20230907193218-d3ddc7976beb h1:LCMfzVg3sflxTs4UvuP4D8CkoZnfHLe2qzqgDn/4OHs= -github.com/google/pprof v0.0.0-20230907193218-d3ddc7976beb/go.mod h1:czg5+yv1E0ZGTi6S6vVK1mke0fV+FaUhNGcd6VRS9Ik= +github.com/google/pprof v0.0.0-20240424215950-a892ee059fd6 h1:k7nVchz72niMH6YLQNvHSdIE7iqsQxK1P41mySCvssg= +github.com/google/pprof v0.0.0-20240424215950-a892ee059fd6/go.mod h1:kf6iHlnVGwgKolg33glAes7Yg/8iWP8ukqeldJSO7jw= github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0= github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= github.com/h2non/filetype v1.1.3 h1:FKkx9QbD7HR/zjK1Ia5XiBsq9zdLi5Kf3zGyFTAFkGg= @@ -97,30 +96,28 @@ github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjY github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 h1:C3w9PqII01/Oq1c1nUAm88MOHcQC9l5mIlSMApZMrHA= github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822/go.mod h1:+n7T8mK8HuQTcFwEeznm/DIxMOiR9yIdICNftLE1DvQ= github.com/onsi/ginkgo v1.16.4 h1:29JGrr5oVBm5ulCWet69zQkzWipVXIol6ygQUe/EzNc= -github.com/onsi/ginkgo/v2 v2.17.1 h1:V++EzdbhI4ZV4ev0UTIj0PzhzOcReJFyJaLjtSF55M8= -github.com/onsi/ginkgo/v2 v2.17.1/go.mod h1:llBI3WDLL9Z6taip6f33H76YcWtJv+7R3HigUjbIBOs= -github.com/onsi/gomega v1.33.0 h1:snPCflnZrpMsy94p4lXVEkHo12lmPnc3vY5XBbreexE= -github.com/onsi/gomega v1.33.0/go.mod h1:+925n5YtiFsLzzafLUHzVMBpvvRAzrydIBiSIxjX3wY= +github.com/onsi/ginkgo/v2 v2.17.2 h1:7eMhcy3GimbsA3hEnVKdw/PQM9XN9krpKVXsZdph0/g= +github.com/onsi/ginkgo/v2 v2.17.2/go.mod h1:nP2DPOQoNsQmsVyv5rDA8JkXQoCs6goXIvr/PRJ1eCc= +github.com/onsi/gomega v1.33.1 h1:dsYjIxxSR755MDmKVsaFQTE22ChNBcuuTWgkUDSubOk= +github.com/onsi/gomega v1.33.1/go.mod h1:U4R44UsT+9eLIaYRB2a5qajjtQYn0hauxvRm16AVYg0= github.com/operator-framework/api v0.23.0 h1:kHymOwcHBpBVujT49SKOCd4EVG7Odwj4wl3NbOR2LLA= github.com/operator-framework/api v0.23.0/go.mod h1:oKcFOz+Xc1UhMi2Pzcp6qsO7wjS4r+yP7EQprQBXrfM= github.com/operator-framework/catalogd v0.12.0 h1:Cww+CyowkfTFugB9ZjUDpKvumh2vPe/TjCUpMHDmVBM= github.com/operator-framework/catalogd v0.12.0/go.mod h1:4lryGtBTVOdqlKR0MaVYnlsSOc7HiagVRVo3J4uIo7E= -github.com/operator-framework/deppy v0.3.0 h1:W8wpF0ehcTAdH2WfMyqMPI5Ja0Qv8M5FMO5cXgJvEQ8= -github.com/operator-framework/deppy v0.3.0/go.mod h1:EHDxZz8fKGvuymCng3G/Ou7wuX14GaLr0cmf2u29Oog= github.com/operator-framework/operator-registry v1.40.0 h1:CaYNE4F/jzahpC7UCILItaIHmB5/oE0sS066nK+5Glw= github.com/operator-framework/operator-registry v1.40.0/go.mod h1:D2YxapkfRDgjqNTO9d3h3v0DeREbV+8utCLG52zrOy4= -github.com/operator-framework/rukpak v0.19.0 h1:8cW43z4jsvARlsmj2eum5bAsZEvSxqDwfMW3dSq1zq8= -github.com/operator-framework/rukpak v0.19.0/go.mod h1:yRJe6JRwgae4s/tnzEDCsNvdT+t4eDARdtfoJMLYiP4= +github.com/operator-framework/rukpak v0.20.0 h1:BqF1nIlocyYLMmv6CvlbtB9QTwSMrEfTzhA+H3+do3c= +github.com/operator-framework/rukpak v0.20.0/go.mod h1:WAyS3DXZ19pLg/324PEoudWZmaRlYZ6i4j4NV3/T/mI= github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= -github.com/prometheus/client_golang v1.18.0 h1:HzFfmkOzH5Q8L8G+kSJKUx5dtG87sewO+FoDDqP5Tbk= -github.com/prometheus/client_golang v1.18.0/go.mod h1:T+GXkCk5wSJyOqMIzVgvvjFDlkOQntgjkJWKrN5txjA= +github.com/prometheus/client_golang v1.19.0 h1:ygXvpU1AoN1MhdzckN+PyD9QJOSD4x7kmXYlnfbA6JU= +github.com/prometheus/client_golang v1.19.0/go.mod h1:ZRM9uEAypZakd+q/x7+gmsvXdURP+DABIEIjnmDdp+k= github.com/prometheus/client_model v0.6.0 h1:k1v3CzpSRUTrKMppY35TLwPvxHqBu0bYgxZzqGIgaos= github.com/prometheus/client_model v0.6.0/go.mod h1:NTQHnmxFpouOD0DpvP4XujX3CdOAGQPoaGhyTchlyt8= -github.com/prometheus/common v0.47.0 h1:p5Cz0FNHo7SnWOmWmoRozVcjEp0bIVU8cV7OShpjL1k= -github.com/prometheus/common v0.47.0/go.mod h1:0/KsvlIEfPQCQ5I2iNSAWKPZziNCvRs5EC6ILDTlAPc= +github.com/prometheus/common v0.48.0 h1:QO8U2CdOzSn1BBsmXJXduaaW+dY/5QLjfB8svtSzKKE= +github.com/prometheus/common v0.48.0/go.mod h1:0/KsvlIEfPQCQ5I2iNSAWKPZziNCvRs5EC6ILDTlAPc= github.com/prometheus/procfs v0.12.0 h1:jluTpSng7V9hY0O2R9DzzJHYb2xULk9VTR1V1R/k6Bo= github.com/prometheus/procfs v0.12.0/go.mod h1:pcuDEFsWDnvcgNzo4EEweacyhjeA9Zk3cnaOZAZEfOo= github.com/rogpeppe/go-internal v1.11.0 h1:cWPaGQEPrBb5/AsnsZesgZZ9yb1OQ+GOISoDNXVBh4M= @@ -199,8 +196,8 @@ golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtn golang.org/x/tools v0.0.0-20200619180055-7c47624df98f/go.mod h1:EkVYQZoAsY45+roYkvgYkIh4xh/qjgUK9TdY2XT94GE= golang.org/x/tools v0.0.0-20210106214847-113979e3529a/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA= golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc= -golang.org/x/tools v0.19.0 h1:tfGCXNR1OsFG+sVdLAitlpjAvD/I6dHDKnYrpEZUHkw= -golang.org/x/tools v0.19.0/go.mod h1:qoJWxmGSIBmAeriMx19ogtrEPrGtDbPK634QFIcLAhc= +golang.org/x/tools v0.20.0 h1:hz/CVckiOxybQvFw6h7b/q80NTr9IUQb4s1IIzW7KNY= +golang.org/x/tools v0.20.0/go.mod h1:WvitBU7JJf6A4jOdg4S1tviW9bhUxkgeCui/0JHctQg= golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= diff --git a/internal/controllers/clusterextension_admission_test.go b/internal/controllers/clusterextension_admission_test.go index 28e2bc967..16629c410 100644 --- a/internal/controllers/clusterextension_admission_test.go +++ b/internal/controllers/clusterextension_admission_test.go @@ -2,6 +2,7 @@ package controllers_test import ( "context" + "strings" "testing" "github.com/stretchr/testify/require" @@ -42,7 +43,8 @@ func TestClusterExtensionAdmissionPackageName(t *testing.T) { t.Parallel() cl := newClient(t) err := cl.Create(context.Background(), buildClusterExtension(ocv1alpha1.ClusterExtensionSpec{ - PackageName: tc.pkgName, + PackageName: tc.pkgName, + InstallNamespace: "default", })) if tc.errMsg == "" { require.NoError(t, err, "unexpected error for package name %q: %w", tc.pkgName, err) @@ -129,8 +131,9 @@ func TestClusterExtensionAdmissionVersion(t *testing.T) { t.Parallel() cl := newClient(t) err := cl.Create(context.Background(), buildClusterExtension(ocv1alpha1.ClusterExtensionSpec{ - PackageName: "package", - Version: tc.version, + PackageName: "package", + Version: tc.version, + InstallNamespace: "default", })) if tc.errMsg == "" { require.NoError(t, err, "unexpected error for version %q: %w", tc.version, err) @@ -173,8 +176,9 @@ func TestClusterExtensionAdmissionChannel(t *testing.T) { t.Parallel() cl := newClient(t) err := cl.Create(context.Background(), buildClusterExtension(ocv1alpha1.ClusterExtensionSpec{ - PackageName: "package", - Channel: tc.channelName, + PackageName: "package", + Channel: tc.channelName, + InstallNamespace: "default", })) if tc.errMsg == "" { require.NoError(t, err, "unexpected error for channel %q: %w", tc.channelName, err) @@ -186,6 +190,51 @@ func TestClusterExtensionAdmissionChannel(t *testing.T) { } } +func TestClusterExtensionAdmissionInstallNamespace(t *testing.T) { + tooLongError := "spec.installNamespace: Too long: may not be longer than 63" + regexMismatchError := "spec.installNamespace in body should match" + + testCases := []struct { + name string + installNamespace string + errMsg string + }{ + {"just alphanumeric", "justalphanumberic1", ""}, + {"hypen-separated", "hyphenated-name", ""}, + {"no install namespace", "", regexMismatchError}, + {"dot-separated", "dotted.name", regexMismatchError}, + {"longest valid install namespace", strings.Repeat("x", 63), ""}, + {"too long install namespace name", strings.Repeat("x", 64), tooLongError}, + {"spaces", "spaces spaces", regexMismatchError}, + {"capitalized", "Capitalized", regexMismatchError}, + {"camel case", "camelCase", regexMismatchError}, + {"invalid characters", "many/invalid$characters+in_name", regexMismatchError}, + {"starts with hyphen", "-start-with-hyphen", regexMismatchError}, + {"ends with hyphen", "end-with-hyphen-", regexMismatchError}, + {"starts with period", ".start-with-period", regexMismatchError}, + {"ends with period", "end-with-period.", regexMismatchError}, + } + + t.Parallel() + for _, tc := range testCases { + tc := tc + t.Run(tc.name, func(t *testing.T) { + t.Parallel() + cl := newClient(t) + err := cl.Create(context.Background(), buildClusterExtension(ocv1alpha1.ClusterExtensionSpec{ + PackageName: "package", + InstallNamespace: tc.installNamespace, + })) + if tc.errMsg == "" { + require.NoError(t, err, "unexpected error for installNamespace %q: %w", tc.installNamespace, err) + } else { + require.Error(t, err) + require.Contains(t, err.Error(), tc.errMsg) + } + }) + } +} + func buildClusterExtension(spec ocv1alpha1.ClusterExtensionSpec) *ocv1alpha1.ClusterExtension { return &ocv1alpha1.ClusterExtension{ ObjectMeta: metav1.ObjectMeta{ diff --git a/internal/controllers/clusterextension_controller.go b/internal/controllers/clusterextension_controller.go index b71baaf2c..56575fbad 100644 --- a/internal/controllers/clusterextension_controller.go +++ b/internal/controllers/clusterextension_controller.go @@ -19,8 +19,10 @@ package controllers import ( "context" "fmt" + "sort" "strings" + mmsemver "github.com/Masterminds/semver/v3" "github.com/go-logr/logr" "k8s.io/apimachinery/pkg/api/equality" apimeta "k8s.io/apimachinery/pkg/api/meta" @@ -29,6 +31,7 @@ import ( "k8s.io/apimachinery/pkg/runtime" "k8s.io/apimachinery/pkg/types" utilerrors "k8s.io/apimachinery/pkg/util/errors" + "k8s.io/apimachinery/pkg/util/sets" "k8s.io/utils/ptr" ctrl "sigs.k8s.io/controller-runtime" "sigs.k8s.io/controller-runtime/pkg/client" @@ -37,22 +40,20 @@ import ( "sigs.k8s.io/controller-runtime/pkg/reconcile" catalogd "github.com/operator-framework/catalogd/api/core/v1alpha1" - "github.com/operator-framework/deppy/pkg/deppy" - "github.com/operator-framework/deppy/pkg/deppy/solver" "github.com/operator-framework/operator-registry/alpha/declcfg" + "github.com/operator-framework/operator-registry/alpha/property" rukpakv1alpha2 "github.com/operator-framework/rukpak/api/v1alpha2" ocv1alpha1 "github.com/operator-framework/operator-controller/api/v1alpha1" "github.com/operator-framework/operator-controller/internal/catalogmetadata" - olmvariables "github.com/operator-framework/operator-controller/internal/resolution/variables" + catalogfilter "github.com/operator-framework/operator-controller/internal/catalogmetadata/filter" + catalogsort "github.com/operator-framework/operator-controller/internal/catalogmetadata/sort" ) // ClusterExtensionReconciler reconciles a ClusterExtension object type ClusterExtensionReconciler struct { client.Client BundleProvider BundleProvider - Scheme *runtime.Scheme - Resolver *solver.Solver } //+kubebuilder:rbac:groups=olm.operatorframework.io,resources=clusterextensions,verbs=get;list;watch @@ -116,33 +117,8 @@ func checkForUnexpectedFieldChange(a, b ocv1alpha1.ClusterExtension) bool { // //nolint:unparam func (r *ClusterExtensionReconciler) reconcile(ctx context.Context, ext *ocv1alpha1.ClusterExtension) (ctrl.Result, error) { - // gather vars for resolution - vars, err := r.variables(ctx) - if err != nil { - ext.Status.InstalledBundle = nil - setInstalledStatusConditionUnknown(&ext.Status.Conditions, "installation has not been attempted due to failure to gather data for resolution", ext.GetGeneration()) - ext.Status.ResolvedBundle = nil - setResolvedStatusConditionFailed(&ext.Status.Conditions, err.Error(), ext.GetGeneration()) - - setDeprecationStatusesUnknown(&ext.Status.Conditions, "deprecation checks have not been attempted due to failure to gather data for resolution", ext.GetGeneration()) - return ctrl.Result{}, err - } - - // run resolution - selection, err := r.Resolver.Solve(vars) - if err != nil { - ext.Status.InstalledBundle = nil - setInstalledStatusConditionUnknown(&ext.Status.Conditions, "installation has not been attempted as resolution failed", ext.GetGeneration()) - ext.Status.ResolvedBundle = nil - setResolvedStatusConditionFailed(&ext.Status.Conditions, err.Error(), ext.GetGeneration()) - - setDeprecationStatusesUnknown(&ext.Status.Conditions, "deprecation checks have not been attempted as resolution failed", ext.GetGeneration()) - return ctrl.Result{}, err - } - - // lookup the bundle in the solution that corresponds to the - // ClusterExtension's desired package name. - bundle, err := r.bundleFromSolution(selection, ext.Spec.PackageName) + // Lookup the bundle that corresponds to the ClusterExtension's desired package. + bundle, err := r.resolve(ctx, ext) if err != nil { ext.Status.InstalledBundle = nil setInstalledStatusConditionUnknown(&ext.Status.Conditions, "installation has not been attempted as resolution failed", ext.GetGeneration()) @@ -165,6 +141,13 @@ func (r *ClusterExtensionReconciler) reconcile(ctx context.Context, ext *ocv1alp setDeprecationStatusesUnknown(&ext.Status.Conditions, "deprecation checks have not been attempted as installation has failed", ext.GetGeneration()) return ctrl.Result{}, err } + + if err := r.validateBundle(bundle); err != nil { + setInstalledStatusConditionFailed(&ext.Status.Conditions, err.Error(), ext.GetGeneration()) + setDeprecationStatusesUnknown(&ext.Status.Conditions, "deprecation checks have not been attempted as installation has failed", ext.GetGeneration()) + return ctrl.Result{}, err + } + bundleProvisioner, err := mapBundleMediaTypeToBundleProvisioner(mediaType) if err != nil { setInstalledStatusConditionFailed(&ext.Status.Conditions, err.Error(), ext.GetGeneration()) @@ -202,21 +185,124 @@ func (r *ClusterExtensionReconciler) reconcile(ctx context.Context, ext *ocv1alp return ctrl.Result{}, nil } -func (r *ClusterExtensionReconciler) variables(ctx context.Context) ([]deppy.Variable, error) { +func (r *ClusterExtensionReconciler) resolve(ctx context.Context, ext *ocv1alpha1.ClusterExtension) (*catalogmetadata.Bundle, error) { allBundles, err := r.BundleProvider.Bundles(ctx) if err != nil { return nil, err } - clusterExtensionList := ocv1alpha1.ClusterExtensionList{} - if err := r.Client.List(ctx, &clusterExtensionList); err != nil { + + installedBundle, err := r.installedBundle(ctx, allBundles, ext) + if err != nil { return nil, err } - bundleDeploymentList := rukpakv1alpha2.BundleDeploymentList{} - if err := r.Client.List(ctx, &bundleDeploymentList); err != nil { + + packageName := ext.Spec.PackageName + channelName := ext.Spec.Channel + versionRange := ext.Spec.Version + + predicates := []catalogfilter.Predicate[catalogmetadata.Bundle]{ + catalogfilter.WithPackageName(packageName), + } + + if channelName != "" { + predicates = append(predicates, catalogfilter.InChannel(channelName)) + } + + if versionRange != "" { + vr, err := mmsemver.NewConstraint(versionRange) + if err != nil { + return nil, fmt.Errorf("invalid version range %q: %w", versionRange, err) + } + predicates = append(predicates, catalogfilter.InMastermindsSemverRange(vr)) + } + + if ext.Spec.UpgradeConstraintPolicy != ocv1alpha1.UpgradeConstraintPolicyIgnore && installedBundle != nil { + upgradePredicate, err := SuccessorsPredicate(installedBundle) + if err != nil { + return nil, err + } + + predicates = append(predicates, upgradePredicate) + } + + resultSet := catalogfilter.Filter(allBundles, catalogfilter.And(predicates...)) + + var upgradeErrorPrefix string + if installedBundle != nil { + installedBundleVersion, err := installedBundle.Version() + if err != nil { + return nil, err + } + upgradeErrorPrefix = fmt.Sprintf("error upgrading from currently installed version %q: ", installedBundleVersion.String()) + } + if len(resultSet) == 0 { + if versionRange != "" && channelName != "" { + return nil, fmt.Errorf("%sno package %q matching version %q found in channel %q", upgradeErrorPrefix, packageName, versionRange, channelName) + } + if versionRange != "" { + return nil, fmt.Errorf("%sno package %q matching version %q found", upgradeErrorPrefix, packageName, versionRange) + } + if channelName != "" { + return nil, fmt.Errorf("%sno package %q found in channel %q", upgradeErrorPrefix, packageName, channelName) + } + return nil, fmt.Errorf("%sno package %q found", upgradeErrorPrefix, packageName) + } + sort.SliceStable(resultSet, func(i, j int) bool { + return catalogsort.ByVersion(resultSet[i], resultSet[j]) + }) + sort.SliceStable(resultSet, func(i, j int) bool { + return catalogsort.ByDeprecated(resultSet[i], resultSet[j]) + }) + + return resultSet[0], nil +} + +func (r *ClusterExtensionReconciler) installedBundle(ctx context.Context, allBundles []*catalogmetadata.Bundle, ext *ocv1alpha1.ClusterExtension) (*catalogmetadata.Bundle, error) { + bd := &rukpakv1alpha2.BundleDeployment{} + err := r.Client.Get(ctx, types.NamespacedName{Name: ext.GetName()}, bd) + if client.IgnoreNotFound(err) != nil { return nil, err } - return GenerateVariables(allBundles, clusterExtensionList.Items, bundleDeploymentList.Items) + if bd.Spec.Source.Image == nil || bd.Spec.Source.Image.Ref == "" { + // Bundle not yet installed + return nil, nil + } + + bundleImage := bd.Spec.Source.Image.Ref + // find corresponding bundle for the installed content + resultSet := catalogfilter.Filter(allBundles, catalogfilter.And( + catalogfilter.WithPackageName(ext.Spec.PackageName), + catalogfilter.WithBundleImage(bundleImage), + )) + if len(resultSet) == 0 { + return nil, fmt.Errorf("bundle with image %q for package %q not found in available catalogs but is currently installed via BundleDeployment %q", bundleImage, ext.Spec.PackageName, bd.Name) + } + + sort.SliceStable(resultSet, func(i, j int) bool { + return catalogsort.ByVersion(resultSet[i], resultSet[j]) + }) + + return resultSet[0], nil +} + +func (r *ClusterExtensionReconciler) validateBundle(bundle *catalogmetadata.Bundle) error { + unsupportedProps := sets.New( + property.TypePackageRequired, + property.TypeGVKRequired, + property.TypeConstraint, + ) + for i := range bundle.Properties { + if unsupportedProps.Has(bundle.Properties[i].Type) { + return fmt.Errorf( + "bundle %q has a dependency declared via property %q which is currently not supported", + bundle.Name, + bundle.Properties[i].Type, + ) + } + } + + return nil } func mapBDStatusToInstalledCondition(existingTypedBundleDeployment *rukpakv1alpha2.BundleDeployment, ext *ocv1alpha1.ClusterExtension, bundle *catalogmetadata.Bundle) { @@ -346,19 +432,6 @@ func SetDeprecationStatus(ext *ocv1alpha1.ClusterExtension, bundle *catalogmetad } } -func (r *ClusterExtensionReconciler) bundleFromSolution(selection []deppy.Variable, packageName string) (*catalogmetadata.Bundle, error) { - for _, variable := range selection { - switch v := variable.(type) { - case *olmvariables.BundleVariable: - bundlePkgName := v.Bundle().Package - if packageName == bundlePkgName { - return v.Bundle(), nil - } - } - } - return nil, fmt.Errorf("bundle for package %q not found in solution", packageName) -} - func (r *ClusterExtensionReconciler) GenerateExpectedBundleDeployment(o ocv1alpha1.ClusterExtension, bundlePath string, bundleProvisioner string) *unstructured.Unstructured { // We use unstructured here to avoid problems of serializing default values when sending patches to the apiserver. // If you use a typed object, any default values from that struct get serialized into the JSON patch, which could @@ -367,7 +440,7 @@ func (r *ClusterExtensionReconciler) GenerateExpectedBundleDeployment(o ocv1alph // identical to "kubectl apply -f" spec := map[string]interface{}{ - // TODO: Don't assume plain provisioner + "installNamespace": o.Spec.InstallNamespace, "provisionerClassName": bundleProvisioner, "source": map[string]interface{}{ // TODO: Don't assume image type @@ -378,10 +451,6 @@ func (r *ClusterExtensionReconciler) GenerateExpectedBundleDeployment(o ocv1alph }, } - if len(o.Spec.WatchNamespaces) > 0 { - spec["watchNamespaces"] = o.Spec.WatchNamespaces - } - bd := &unstructured.Unstructured{Object: map[string]interface{}{ "apiVersion": rukpakv1alpha2.GroupVersion.String(), "kind": rukpakv1alpha2.BundleDeploymentKind, diff --git a/internal/controllers/clusterextension_controller_test.go b/internal/controllers/clusterextension_controller_test.go index 5f5b7fdb4..418bd4a0e 100644 --- a/internal/controllers/clusterextension_controller_test.go +++ b/internal/controllers/clusterextension_controller_test.go @@ -12,7 +12,6 @@ import ( "github.com/stretchr/testify/require" apimeta "k8s.io/apimachinery/pkg/api/meta" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" "k8s.io/apimachinery/pkg/runtime" "k8s.io/apimachinery/pkg/types" "k8s.io/apimachinery/pkg/util/rand" @@ -53,7 +52,10 @@ func TestClusterExtensionNonExistentPackage(t *testing.T) { pkgName := fmt.Sprintf("non-existent-%s", rand.String(6)) clusterExtension := &ocv1alpha1.ClusterExtension{ ObjectMeta: metav1.ObjectMeta{Name: extKey.Name}, - Spec: ocv1alpha1.ClusterExtensionSpec{PackageName: pkgName}, + Spec: ocv1alpha1.ClusterExtensionSpec{ + PackageName: pkgName, + InstallNamespace: "default", + }, } require.NoError(t, cl.Create(ctx, clusterExtension)) @@ -93,8 +95,9 @@ func TestClusterExtensionNonExistentVersion(t *testing.T) { clusterExtension := &ocv1alpha1.ClusterExtension{ ObjectMeta: metav1.ObjectMeta{Name: extKey.Name}, Spec: ocv1alpha1.ClusterExtensionSpec{ - PackageName: pkgName, - Version: "0.50.0", // this version of the package does not exist + PackageName: pkgName, + Version: "0.50.0", // this version of the package does not exist + InstallNamespace: "default", }, } require.NoError(t, cl.Create(ctx, clusterExtension)) @@ -122,7 +125,7 @@ func TestClusterExtensionNonExistentVersion(t *testing.T) { require.NotNil(t, cond) require.Equal(t, metav1.ConditionUnknown, cond.Status) require.Equal(t, ocv1alpha1.ReasonInstallationStatusUnknown, cond.Reason) - require.Equal(t, "installation has not been attempted due to failure to gather data for resolution", cond.Message) + require.Equal(t, "installation has not been attempted as resolution failed", cond.Message) verifyInvariants(ctx, t, reconciler.Client, clusterExtension) require.NoError(t, cl.DeleteAllOf(ctx, &ocv1alpha1.ClusterExtension{})) @@ -134,12 +137,16 @@ func TestClusterExtensionBundleDeploymentDoesNotExist(t *testing.T) { ctx := context.Background() extKey := types.NamespacedName{Name: fmt.Sprintf("cluster-extension-test-%s", rand.String(8))} const pkgName = "prometheus" + installNamespace := fmt.Sprintf("test-ns-%s", rand.String(8)) t.Log("When the cluster extension specifies a valid available package") t.Log("By initializing cluster state") clusterExtension := &ocv1alpha1.ClusterExtension{ ObjectMeta: metav1.ObjectMeta{Name: extKey.Name}, - Spec: ocv1alpha1.ClusterExtensionSpec{PackageName: pkgName}, + Spec: ocv1alpha1.ClusterExtensionSpec{ + PackageName: pkgName, + InstallNamespace: installNamespace, + }, } require.NoError(t, cl.Create(ctx, clusterExtension)) @@ -156,6 +163,7 @@ func TestClusterExtensionBundleDeploymentDoesNotExist(t *testing.T) { bd := &rukpakv1alpha2.BundleDeployment{} require.NoError(t, cl.Get(ctx, types.NamespacedName{Name: extKey.Name}, bd)) require.Equal(t, "core-rukpak-io-registry", bd.Spec.ProvisionerClassName) + require.Equal(t, installNamespace, bd.Spec.InstallNamespace) require.Equal(t, rukpakv1alpha2.SourceTypeImage, bd.Spec.Source.Type) require.NotNil(t, bd.Spec.Source.Image) require.Equal(t, "quay.io/operatorhubio/prometheus@fake2.0.0", bd.Spec.Source.Image.Ref) @@ -189,12 +197,16 @@ func TestClusterExtensionBundleDeploymentOutOfDate(t *testing.T) { ctx := context.Background() extKey := types.NamespacedName{Name: fmt.Sprintf("cluster-extension-test-%s", rand.String(8))} const pkgName = "prometheus" + installNamespace := fmt.Sprintf("test-ns-%s", rand.String(8)) t.Log("When the cluster extension specifies a valid available package") t.Log("By initializing cluster state") clusterExtension := &ocv1alpha1.ClusterExtension{ ObjectMeta: metav1.ObjectMeta{Name: extKey.Name}, - Spec: ocv1alpha1.ClusterExtensionSpec{PackageName: pkgName}, + Spec: ocv1alpha1.ClusterExtensionSpec{ + PackageName: pkgName, + InstallNamespace: installNamespace, + }, } require.NoError(t, cl.Create(ctx, clusterExtension)) @@ -216,6 +228,7 @@ func TestClusterExtensionBundleDeploymentOutOfDate(t *testing.T) { }, }, Spec: rukpakv1alpha2.BundleDeploymentSpec{ + InstallNamespace: "default", ProvisionerClassName: "core-rukpak-io-registry", Source: rukpakv1alpha2.BundleSource{ Type: rukpakv1alpha2.SourceTypeImage, @@ -227,6 +240,7 @@ func TestClusterExtensionBundleDeploymentOutOfDate(t *testing.T) { } t.Log("By modifying the BD spec and creating the object") + bd.Spec.InstallNamespace = "incorrect" bd.Spec.ProvisionerClassName = "core-rukpak-io-helm" require.NoError(t, cl.Create(ctx, bd)) @@ -244,6 +258,7 @@ func TestClusterExtensionBundleDeploymentOutOfDate(t *testing.T) { bd = &rukpakv1alpha2.BundleDeployment{} require.NoError(t, cl.Get(ctx, types.NamespacedName{Name: extKey.Name}, bd)) require.Equal(t, "core-rukpak-io-registry", bd.Spec.ProvisionerClassName) + require.Equal(t, installNamespace, bd.Spec.InstallNamespace) require.Equal(t, rukpakv1alpha2.SourceTypeImage, bd.Spec.Source.Type) require.NotNil(t, bd.Spec.Source.Image) require.Equal(t, "quay.io/operatorhubio/prometheus@fake2.0.0", bd.Spec.Source.Image.Ref) @@ -274,12 +289,16 @@ func TestClusterExtensionBundleDeploymentUpToDate(t *testing.T) { ctx := context.Background() extKey := types.NamespacedName{Name: fmt.Sprintf("cluster-extension-test-%s", rand.String(8))} const pkgName = "prometheus" + installNamespace := fmt.Sprintf("test-ns-%s", rand.String(8)) t.Log("When the cluster extension specifies a valid available package") t.Log("By initializing cluster state") clusterExtension := &ocv1alpha1.ClusterExtension{ ObjectMeta: metav1.ObjectMeta{Name: extKey.Name}, - Spec: ocv1alpha1.ClusterExtensionSpec{PackageName: pkgName}, + Spec: ocv1alpha1.ClusterExtensionSpec{ + PackageName: pkgName, + InstallNamespace: installNamespace, + }, } require.NoError(t, cl.Create(ctx, clusterExtension)) @@ -301,6 +320,7 @@ func TestClusterExtensionBundleDeploymentUpToDate(t *testing.T) { }, }, Spec: rukpakv1alpha2.BundleDeploymentSpec{ + InstallNamespace: installNamespace, ProvisionerClassName: "core-rukpak-io-registry", Source: rukpakv1alpha2.BundleSource{ Type: rukpakv1alpha2.SourceTypeImage, @@ -522,7 +542,7 @@ func TestClusterExtensionBundleDeploymentUpToDate(t *testing.T) { require.Equal(t, &ocv1alpha1.BundleMetadata{Name: "operatorhub/prometheus/beta/2.0.0", Version: "2.0.0"}, ext.Status.ResolvedBundle) require.Nil(t, ext.Status.InstalledBundle) - t.Log("By cchecking the expected conditions") + t.Log("By checking the expected conditions") cond = apimeta.FindStatusCondition(ext.Status.Conditions, ocv1alpha1.TypeResolved) require.NotNil(t, cond) require.Equal(t, metav1.ConditionTrue, cond.Status) @@ -544,12 +564,16 @@ func TestClusterExtensionExpectedBundleDeployment(t *testing.T) { ctx := context.Background() extKey := types.NamespacedName{Name: fmt.Sprintf("cluster-extension-test-%s", rand.String(8))} const pkgName = "prometheus" + installNamespace := fmt.Sprintf("test-ns-%s", rand.String(8)) t.Log("When the cluster extension specifies a valid available package") t.Log("By initializing cluster state") clusterExtension := &ocv1alpha1.ClusterExtension{ ObjectMeta: metav1.ObjectMeta{Name: extKey.Name}, - Spec: ocv1alpha1.ClusterExtensionSpec{PackageName: pkgName}, + Spec: ocv1alpha1.ClusterExtensionSpec{ + PackageName: pkgName, + InstallNamespace: installNamespace, + }, } require.NoError(t, cl.Create(ctx, clusterExtension)) @@ -558,6 +582,7 @@ func TestClusterExtensionExpectedBundleDeployment(t *testing.T) { bd := &rukpakv1alpha2.BundleDeployment{ ObjectMeta: metav1.ObjectMeta{Name: extKey.Name}, Spec: rukpakv1alpha2.BundleDeploymentSpec{ + InstallNamespace: "foo", ProvisionerClassName: "bar", Source: rukpakv1alpha2.BundleSource{ Type: rukpakv1alpha2.SourceTypeHTTP, @@ -581,6 +606,7 @@ func TestClusterExtensionExpectedBundleDeployment(t *testing.T) { bd = &rukpakv1alpha2.BundleDeployment{} require.NoError(t, cl.Get(ctx, types.NamespacedName{Name: extKey.Name}, bd)) require.Equal(t, "core-rukpak-io-registry", bd.Spec.ProvisionerClassName) + require.Equal(t, installNamespace, bd.Spec.InstallNamespace) require.Equal(t, rukpakv1alpha2.SourceTypeImage, bd.Spec.Source.Type) require.NotNil(t, bd.Spec.Source.Image) require.Equal(t, "quay.io/operatorhubio/prometheus@fake2.0.0", bd.Spec.Source.Image.Ref) @@ -608,57 +634,6 @@ func TestClusterExtensionExpectedBundleDeployment(t *testing.T) { require.NoError(t, cl.DeleteAllOf(ctx, &rukpakv1alpha2.BundleDeployment{})) } -func TestClusterExtensionDuplicatePackage(t *testing.T) { - cl, reconciler := newClientAndReconciler(t) - ctx := context.Background() - extKey := types.NamespacedName{Name: fmt.Sprintf("cluster-extension-test-%s", rand.String(8))} - const pkgName = "prometheus" - - t.Log("When the cluster extension specifies a duplicate package") - t.Log("By initializing cluster state") - dupClusterExtension := &ocv1alpha1.ClusterExtension{ - ObjectMeta: metav1.ObjectMeta{Name: fmt.Sprintf("orig-%s", extKey.Name)}, - Spec: ocv1alpha1.ClusterExtensionSpec{PackageName: pkgName}, - } - require.NoError(t, cl.Create(ctx, dupClusterExtension)) - - clusterExtension := &ocv1alpha1.ClusterExtension{ - ObjectMeta: metav1.ObjectMeta{Name: extKey.Name}, - Spec: ocv1alpha1.ClusterExtensionSpec{PackageName: pkgName}, - } - require.NoError(t, cl.Create(ctx, clusterExtension)) - - t.Log("It sets resolution failure status") - t.Log("By running reconcile") - res, err := reconciler.Reconcile(ctx, ctrl.Request{NamespacedName: extKey}) - require.Equal(t, ctrl.Result{}, res) - require.EqualError(t, err, `duplicate identifier "required package prometheus" in input`) - - t.Log("By fetching updated cluster extension after reconcile") - require.NoError(t, cl.Get(ctx, extKey, clusterExtension)) - - t.Log("By checking the status fields") - require.Empty(t, clusterExtension.Status.ResolvedBundle) - require.Empty(t, clusterExtension.Status.InstalledBundle) - - t.Log("By checking the expected conditions") - cond := apimeta.FindStatusCondition(clusterExtension.Status.Conditions, ocv1alpha1.TypeResolved) - require.NotNil(t, cond) - require.Equal(t, metav1.ConditionFalse, cond.Status) - require.Equal(t, ocv1alpha1.ReasonResolutionFailed, cond.Reason) - require.Equal(t, `duplicate identifier "required package prometheus" in input`, cond.Message) - - cond = apimeta.FindStatusCondition(clusterExtension.Status.Conditions, ocv1alpha1.TypeInstalled) - require.NotNil(t, cond) - require.Equal(t, metav1.ConditionUnknown, cond.Status) - require.Equal(t, ocv1alpha1.ReasonInstallationStatusUnknown, cond.Reason) - require.Equal(t, "installation has not been attempted as resolution failed", cond.Message) - - verifyInvariants(ctx, t, reconciler.Client, clusterExtension) - require.NoError(t, cl.DeleteAllOf(ctx, &ocv1alpha1.ClusterExtension{})) - require.NoError(t, cl.DeleteAllOf(ctx, &rukpakv1alpha2.BundleDeployment{})) -} - func TestClusterExtensionChannelVersionExists(t *testing.T) { cl, reconciler := newClientAndReconciler(t) ctx := context.Background() @@ -669,12 +644,15 @@ func TestClusterExtensionChannelVersionExists(t *testing.T) { pkgName := "prometheus" pkgVer := "1.0.0" pkgChan := "beta" + installNamespace := fmt.Sprintf("test-ns-%s", rand.String(8)) + clusterExtension := &ocv1alpha1.ClusterExtension{ ObjectMeta: metav1.ObjectMeta{Name: extKey.Name}, Spec: ocv1alpha1.ClusterExtensionSpec{ - PackageName: pkgName, - Version: pkgVer, - Channel: pkgChan, + PackageName: pkgName, + Version: pkgVer, + Channel: pkgChan, + InstallNamespace: installNamespace, }, } err := cl.Create(ctx, clusterExtension) @@ -709,6 +687,7 @@ func TestClusterExtensionChannelVersionExists(t *testing.T) { bd := &rukpakv1alpha2.BundleDeployment{} require.NoError(t, cl.Get(ctx, types.NamespacedName{Name: extKey.Name}, bd)) require.Equal(t, "core-rukpak-io-registry", bd.Spec.ProvisionerClassName) + require.Equal(t, installNamespace, bd.Spec.InstallNamespace) require.Equal(t, rukpakv1alpha2.SourceTypeImage, bd.Spec.Source.Type) require.NotNil(t, bd.Spec.Source.Image) require.Equal(t, "quay.io/operatorhubio/prometheus@fake1.0.0", bd.Spec.Source.Image.Ref) @@ -728,12 +707,14 @@ func TestClusterExtensionChannelExistsNoVersion(t *testing.T) { pkgName := "prometheus" pkgVer := "" pkgChan := "beta" + installNamespace := fmt.Sprintf("test-ns-%s", rand.String(8)) clusterExtension := &ocv1alpha1.ClusterExtension{ ObjectMeta: metav1.ObjectMeta{Name: extKey.Name}, Spec: ocv1alpha1.ClusterExtensionSpec{ - PackageName: pkgName, - Version: pkgVer, - Channel: pkgChan, + PackageName: pkgName, + Version: pkgVer, + Channel: pkgChan, + InstallNamespace: installNamespace, }, } require.NoError(t, cl.Create(ctx, clusterExtension)) @@ -766,6 +747,7 @@ func TestClusterExtensionChannelExistsNoVersion(t *testing.T) { bd := &rukpakv1alpha2.BundleDeployment{} require.NoError(t, cl.Get(ctx, types.NamespacedName{Name: extKey.Name}, bd)) require.Equal(t, "core-rukpak-io-registry", bd.Spec.ProvisionerClassName) + require.Equal(t, installNamespace, bd.Spec.InstallNamespace) require.Equal(t, rukpakv1alpha2.SourceTypeImage, bd.Spec.Source.Type) require.NotNil(t, bd.Spec.Source.Image) require.Equal(t, "quay.io/operatorhubio/prometheus@fake2.0.0", bd.Spec.Source.Image.Ref) @@ -788,9 +770,10 @@ func TestClusterExtensionVersionNoChannel(t *testing.T) { clusterExtension := &ocv1alpha1.ClusterExtension{ ObjectMeta: metav1.ObjectMeta{Name: extKey.Name}, Spec: ocv1alpha1.ClusterExtensionSpec{ - PackageName: pkgName, - Version: pkgVer, - Channel: pkgChan, + PackageName: pkgName, + Version: pkgVer, + Channel: pkgChan, + InstallNamespace: "default", }, } require.NoError(t, cl.Create(ctx, clusterExtension)) @@ -819,7 +802,7 @@ func TestClusterExtensionVersionNoChannel(t *testing.T) { require.NotNil(t, cond) require.Equal(t, metav1.ConditionUnknown, cond.Status) require.Equal(t, ocv1alpha1.ReasonInstallationStatusUnknown, cond.Reason) - require.Equal(t, "installation has not been attempted due to failure to gather data for resolution", cond.Message) + require.Equal(t, "installation has not been attempted as resolution failed", cond.Message) verifyInvariants(ctx, t, reconciler.Client, clusterExtension) require.NoError(t, cl.DeleteAllOf(ctx, &ocv1alpha1.ClusterExtension{})) @@ -838,8 +821,9 @@ func TestClusterExtensionNoChannel(t *testing.T) { clusterExtension := &ocv1alpha1.ClusterExtension{ ObjectMeta: metav1.ObjectMeta{Name: extKey.Name}, Spec: ocv1alpha1.ClusterExtensionSpec{ - PackageName: pkgName, - Channel: pkgChan, + PackageName: pkgName, + Channel: pkgChan, + InstallNamespace: "default", }, } require.NoError(t, cl.Create(ctx, clusterExtension)) @@ -867,7 +851,7 @@ func TestClusterExtensionNoChannel(t *testing.T) { require.NotNil(t, cond) require.Equal(t, metav1.ConditionUnknown, cond.Status) require.Equal(t, ocv1alpha1.ReasonInstallationStatusUnknown, cond.Reason) - require.Equal(t, "installation has not been attempted due to failure to gather data for resolution", cond.Message) + require.Equal(t, "installation has not been attempted as resolution failed", cond.Message) verifyInvariants(ctx, t, reconciler.Client, clusterExtension) require.NoError(t, cl.DeleteAllOf(ctx, &ocv1alpha1.ClusterExtension{})) @@ -887,9 +871,10 @@ func TestClusterExtensionNoVersion(t *testing.T) { clusterExtension := &ocv1alpha1.ClusterExtension{ ObjectMeta: metav1.ObjectMeta{Name: extKey.Name}, Spec: ocv1alpha1.ClusterExtensionSpec{ - PackageName: pkgName, - Version: pkgVer, - Channel: pkgChan, + PackageName: pkgName, + Version: pkgVer, + Channel: pkgChan, + InstallNamespace: "default", }, } require.NoError(t, cl.Create(ctx, clusterExtension)) @@ -917,7 +902,7 @@ func TestClusterExtensionNoVersion(t *testing.T) { require.NotNil(t, cond) require.Equal(t, metav1.ConditionUnknown, cond.Status) require.Equal(t, ocv1alpha1.ReasonInstallationStatusUnknown, cond.Reason) - require.Equal(t, "installation has not been attempted due to failure to gather data for resolution", cond.Message) + require.Equal(t, "installation has not been attempted as resolution failed", cond.Message) verifyInvariants(ctx, t, reconciler.Client, clusterExtension) require.NoError(t, cl.DeleteAllOf(ctx, &ocv1alpha1.ClusterExtension{})) @@ -937,9 +922,10 @@ func TestClusterExtensionPlainV0Bundle(t *testing.T) { clusterExtension := &ocv1alpha1.ClusterExtension{ ObjectMeta: metav1.ObjectMeta{Name: extKey.Name}, Spec: ocv1alpha1.ClusterExtensionSpec{ - PackageName: pkgName, - Version: pkgVer, - Channel: pkgChan, + PackageName: pkgName, + Version: pkgVer, + Channel: pkgChan, + InstallNamespace: "default", }, } require.NoError(t, cl.Create(ctx, clusterExtension)) @@ -994,9 +980,10 @@ func TestClusterExtensionBadBundleMediaType(t *testing.T) { clusterExtension := &ocv1alpha1.ClusterExtension{ ObjectMeta: metav1.ObjectMeta{Name: extKey.Name}, Spec: ocv1alpha1.ClusterExtensionSpec{ - PackageName: pkgName, - Version: pkgVer, - Channel: pkgChan, + PackageName: pkgName, + Version: pkgVer, + Channel: pkgChan, + InstallNamespace: "default", }, } require.NoError(t, cl.Create(ctx, clusterExtension)) @@ -1056,11 +1043,10 @@ func verifyConditionsInvariants(t *testing.T, ext *ocv1alpha1.ClusterExtension) func TestGeneratedBundleDeployment(t *testing.T) { test := []struct { - name string - clusterExtension ocv1alpha1.ClusterExtension - bundlePath string - bundleProvisioner string - expectedBundleDeployment *unstructured.Unstructured + name string + clusterExtension ocv1alpha1.ClusterExtension + bundlePath string + bundleProvisioner string }{ { name: "when all the specs are provided.", @@ -1070,24 +1056,11 @@ func TestGeneratedBundleDeployment(t *testing.T) { UID: types.UID("test"), }, Spec: ocv1alpha1.ClusterExtensionSpec{ - WatchNamespaces: []string{"alpha", "beta", "gamma"}, - }, - }, - bundlePath: "testpath", - bundleProvisioner: "foo", - expectedBundleDeployment: &unstructured.Unstructured{}, - }, - { - name: "when watchNamespaces are not provided.", - clusterExtension: ocv1alpha1.ClusterExtension{ - ObjectMeta: metav1.ObjectMeta{ - Name: "test-bd", - UID: types.UID("test"), + InstallNamespace: "test-ns", }, }, - bundlePath: "testpath", - bundleProvisioner: "foo", - expectedBundleDeployment: &unstructured.Unstructured{}, + bundlePath: "testpath", + bundleProvisioner: "foo", }, } @@ -1100,7 +1073,7 @@ func TestGeneratedBundleDeployment(t *testing.T) { require.Equal(t, tt.clusterExtension.GetName(), resultBundleDeployment.GetName()) require.Equal(t, tt.bundlePath, resultBundleDeployment.Spec.Source.Image.Ref) require.Equal(t, tt.bundleProvisioner, resultBundleDeployment.Spec.ProvisionerClassName) - require.Equal(t, tt.clusterExtension.Spec.WatchNamespaces, resultBundleDeployment.Spec.WatchNamespaces) + require.Equal(t, tt.clusterExtension.Spec.InstallNamespace, resultBundleDeployment.Spec.InstallNamespace) } } @@ -1118,13 +1091,15 @@ func TestClusterExtensionUpgrade(t *testing.T) { pkgName := "prometheus" pkgVer := "1.0.0" pkgChan := "beta" + installNamespace := fmt.Sprintf("test-ns-%s", rand.String(8)) extKey := types.NamespacedName{Name: fmt.Sprintf("cluster-extension-test-%s", rand.String(8))} clusterExtension := &ocv1alpha1.ClusterExtension{ ObjectMeta: metav1.ObjectMeta{Name: extKey.Name}, Spec: ocv1alpha1.ClusterExtensionSpec{ - PackageName: pkgName, - Version: pkgVer, - Channel: pkgChan, + PackageName: pkgName, + Version: pkgVer, + Channel: pkgChan, + InstallNamespace: installNamespace, }, } // Create a cluster extension @@ -1173,8 +1148,7 @@ func TestClusterExtensionUpgrade(t *testing.T) { require.NotNil(t, cond) assert.Equal(t, metav1.ConditionFalse, cond.Status) assert.Equal(t, ocv1alpha1.ReasonResolutionFailed, cond.Reason) - assert.Contains(t, cond.Message, "constraints not satisfiable") - assert.Regexp(t, "installed package prometheus requires at least one of fake-catalog-prometheus-operatorhub/prometheus/beta/1.2.0, fake-catalog-prometheus-operatorhub/prometheus/beta/1.0.1, fake-catalog-prometheus-operatorhub/prometheus/beta/1.0.0$", cond.Message) + assert.Equal(t, "error upgrading from currently installed version \"1.0.0\": no package \"prometheus\" matching version \"2.0.0\" found in channel \"beta\"", cond.Message) // Valid update skipping one version clusterExtension.Spec.Version = "1.2.0" @@ -1211,13 +1185,15 @@ func TestClusterExtensionUpgrade(t *testing.T) { pkgName := "prometheus" pkgVer := "1.0.0" pkgChan := "beta" + installNamespace := fmt.Sprintf("test-ns-%s", rand.String(8)) extKey := types.NamespacedName{Name: fmt.Sprintf("cluster-extension-test-%s", rand.String(8))} clusterExtension := &ocv1alpha1.ClusterExtension{ ObjectMeta: metav1.ObjectMeta{Name: extKey.Name}, Spec: ocv1alpha1.ClusterExtensionSpec{ - PackageName: pkgName, - Version: pkgVer, - Channel: pkgChan, + PackageName: pkgName, + Version: pkgVer, + Channel: pkgChan, + InstallNamespace: installNamespace, }, } // Create a cluster extension @@ -1266,8 +1242,7 @@ func TestClusterExtensionUpgrade(t *testing.T) { require.NotNil(t, cond) assert.Equal(t, metav1.ConditionFalse, cond.Status) assert.Equal(t, ocv1alpha1.ReasonResolutionFailed, cond.Reason) - assert.Contains(t, cond.Message, "constraints not satisfiable") - assert.Contains(t, cond.Message, "installed package prometheus requires at least one of fake-catalog-prometheus-operatorhub/prometheus/beta/1.0.1, fake-catalog-prometheus-operatorhub/prometheus/beta/1.0.0\n") + assert.Equal(t, "error upgrading from currently installed version \"1.0.0\": no package \"prometheus\" matching version \"1.2.0\" found in channel \"beta\"", cond.Message) // Valid update skipping one version clusterExtension.Spec.Version = "1.0.1" @@ -1315,6 +1290,7 @@ func TestClusterExtensionUpgrade(t *testing.T) { require.NoError(t, cl.DeleteAllOf(ctx, &rukpakv1alpha2.BundleDeployment{})) }() + installNamespace := fmt.Sprintf("test-ns-%s", rand.String(8)) extKey := types.NamespacedName{Name: fmt.Sprintf("cluster-extension-test-%s", rand.String(8))} clusterExtension := &ocv1alpha1.ClusterExtension{ ObjectMeta: metav1.ObjectMeta{Name: extKey.Name}, @@ -1323,6 +1299,7 @@ func TestClusterExtensionUpgrade(t *testing.T) { Version: "1.0.0", Channel: "beta", UpgradeConstraintPolicy: ocv1alpha1.UpgradeConstraintPolicyIgnore, + InstallNamespace: installNamespace, }, } // Create a cluster extension @@ -1403,13 +1380,15 @@ func TestClusterExtensionDowngrade(t *testing.T) { require.NoError(t, cl.DeleteAllOf(ctx, &rukpakv1alpha2.BundleDeployment{})) }() + installNamespace := fmt.Sprintf("test-ns-%s", rand.String(8)) extKey := types.NamespacedName{Name: fmt.Sprintf("cluster-extension-test-%s", rand.String(8))} clusterExtension := &ocv1alpha1.ClusterExtension{ ObjectMeta: metav1.ObjectMeta{Name: extKey.Name}, Spec: ocv1alpha1.ClusterExtensionSpec{ - PackageName: "prometheus", - Version: "1.0.1", - Channel: "beta", + PackageName: "prometheus", + Version: "1.0.1", + Channel: "beta", + InstallNamespace: installNamespace, }, } // Create a cluster extension @@ -1458,8 +1437,7 @@ func TestClusterExtensionDowngrade(t *testing.T) { require.NotNil(t, cond) assert.Equal(t, metav1.ConditionFalse, cond.Status) assert.Equal(t, ocv1alpha1.ReasonResolutionFailed, cond.Reason) - assert.Contains(t, cond.Message, "constraints not satisfiable") - assert.Contains(t, cond.Message, "installed package prometheus requires at least one of fake-catalog-prometheus-operatorhub/prometheus/beta/1.2.0, fake-catalog-prometheus-operatorhub/prometheus/beta/1.0.1\n") + assert.Equal(t, "error upgrading from currently installed version \"1.0.1\": no package \"prometheus\" matching version \"1.0.0\" found in channel \"beta\"", cond.Message) }) } }) @@ -1485,6 +1463,7 @@ func TestClusterExtensionDowngrade(t *testing.T) { require.NoError(t, cl.DeleteAllOf(ctx, &rukpakv1alpha2.BundleDeployment{})) }() + installNamespace := fmt.Sprintf("test-ns-%s", rand.String(8)) extKey := types.NamespacedName{Name: fmt.Sprintf("cluster-extension-test-%s", rand.String(8))} clusterExtension := &ocv1alpha1.ClusterExtension{ ObjectMeta: metav1.ObjectMeta{Name: extKey.Name}, @@ -1493,6 +1472,7 @@ func TestClusterExtensionDowngrade(t *testing.T) { Version: "2.0.0", Channel: "beta", UpgradeConstraintPolicy: ocv1alpha1.UpgradeConstraintPolicyIgnore, + InstallNamespace: installNamespace, }, } // Create a cluster extension diff --git a/internal/controllers/clusterextension_registryv1_validation_test.go b/internal/controllers/clusterextension_registryv1_validation_test.go new file mode 100644 index 000000000..4339e768d --- /dev/null +++ b/internal/controllers/clusterextension_registryv1_validation_test.go @@ -0,0 +1,139 @@ +package controllers_test + +import ( + "context" + "encoding/json" + "fmt" + "testing" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + apimeta "k8s.io/apimachinery/pkg/api/meta" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/apimachinery/pkg/types" + "k8s.io/apimachinery/pkg/util/rand" + ctrl "sigs.k8s.io/controller-runtime" + + "github.com/operator-framework/operator-registry/alpha/declcfg" + "github.com/operator-framework/operator-registry/alpha/property" + rukpakv1alpha2 "github.com/operator-framework/rukpak/api/v1alpha2" + + ocv1alpha1 "github.com/operator-framework/operator-controller/api/v1alpha1" + "github.com/operator-framework/operator-controller/internal/catalogmetadata" + "github.com/operator-framework/operator-controller/internal/controllers" + testutil "github.com/operator-framework/operator-controller/test/util" +) + +func TestClusterExtensionRegistryV1DisallowDependencies(t *testing.T) { + ctx := context.Background() + cl := newClient(t) + + for _, tt := range []struct { + name string + bundle *catalogmetadata.Bundle + wantErr string + }{ + { + name: "package with no dependencies", + bundle: &catalogmetadata.Bundle{ + Bundle: declcfg.Bundle{ + Name: "fake-catalog/no-dependencies-package/alpha/1.0.0", + Package: "no-dependencies-package", + Image: "quay.io/fake-catalog/no-dependencies-package@sha256:3e281e587de3d03011440685fc4fb782672beab044c1ebadc42788ce05a21c35", + Properties: []property.Property{ + {Type: property.TypePackage, Value: json.RawMessage(`{"packageName":"no-dependencies-package","version":"1.0.0"}`)}, + }, + }, + CatalogName: "fake-catalog", + }, + }, + { + name: "package with olm.package.required property", + bundle: &catalogmetadata.Bundle{ + Bundle: declcfg.Bundle{ + Name: "fake-catalog/package-required-test/alpha/1.0.0", + Package: "package-required-test", + Image: "quay.io/fake-catalog/package-required-test@sha256:3e281e587de3d03011440685fc4fb782672beab044c1ebadc42788ce05a21c35", + Properties: []property.Property{ + {Type: property.TypePackage, Value: json.RawMessage(`{"packageName":"package-required-test","version":"1.0.0"}`)}, + {Type: property.TypePackageRequired, Value: json.RawMessage("content-is-not-relevant")}, + }, + }, + CatalogName: "fake-catalog", + }, + wantErr: `bundle "fake-catalog/package-required-test/alpha/1.0.0" has a dependency declared via property "olm.package.required" which is currently not supported`, + }, + { + name: "package with olm.gvk.required property", + bundle: &catalogmetadata.Bundle{ + Bundle: declcfg.Bundle{ + Name: "fake-catalog/gvk-required-test/alpha/1.0.0", + Package: "gvk-required-test", + Image: "quay.io/fake-catalog/gvk-required-test@sha256:3e281e587de3d03011440685fc4fb782672beab044c1ebadc42788ce05a21c35", + Properties: []property.Property{ + {Type: property.TypePackage, Value: json.RawMessage(`{"packageName":"gvk-required-test","version":"1.0.0"}`)}, + {Type: property.TypeGVKRequired, Value: json.RawMessage(`content-is-not-relevant`)}, + }, + }, + CatalogName: "fake-catalog", + }, + wantErr: `bundle "fake-catalog/gvk-required-test/alpha/1.0.0" has a dependency declared via property "olm.gvk.required" which is currently not supported`, + }, + { + name: "package with olm.constraint property", + bundle: &catalogmetadata.Bundle{ + Bundle: declcfg.Bundle{ + Name: "fake-catalog/constraint-test/alpha/1.0.0", + Package: "constraint-test", + Image: "quay.io/fake-catalog/constraint-test@sha256:3e281e587de3d03011440685fc4fb782672beab044c1ebadc42788ce05a21c35", + Properties: []property.Property{ + {Type: property.TypePackage, Value: json.RawMessage(`{"packageName":"constraint-test","version":"1.0.0"}`)}, + {Type: property.TypeConstraint, Value: json.RawMessage(`content-is-not-relevant`)}, + }, + }, + CatalogName: "fake-catalog", + }, + wantErr: `bundle "fake-catalog/constraint-test/alpha/1.0.0" has a dependency declared via property "olm.constraint" which is currently not supported`, + }, + } { + t.Run(tt.name, func(t *testing.T) { + defer func() { + require.NoError(t, cl.DeleteAllOf(ctx, &ocv1alpha1.ClusterExtension{})) + require.NoError(t, cl.DeleteAllOf(ctx, &rukpakv1alpha2.BundleDeployment{})) + }() + + fakeCatalogClient := testutil.NewFakeCatalogClient([]*catalogmetadata.Bundle{tt.bundle}) + reconciler := &controllers.ClusterExtensionReconciler{ + Client: cl, + BundleProvider: &fakeCatalogClient, + } + + installNamespace := fmt.Sprintf("test-ns-%s", rand.String(8)) + extKey := types.NamespacedName{Name: fmt.Sprintf("cluster-extension-test-%s", rand.String(8))} + clusterExtension := &ocv1alpha1.ClusterExtension{ + ObjectMeta: metav1.ObjectMeta{Name: extKey.Name}, + Spec: ocv1alpha1.ClusterExtensionSpec{ + PackageName: tt.bundle.Package, + InstallNamespace: installNamespace, + }, + } + require.NoError(t, cl.Create(ctx, clusterExtension)) + + res, err := reconciler.Reconcile(ctx, ctrl.Request{NamespacedName: extKey}) + require.Equal(t, ctrl.Result{}, res) + if tt.wantErr == "" { + assert.NoError(t, err) + } else { + assert.EqualError(t, err, tt.wantErr) + + // In case of an error we want it to be included in the installed condition + require.NoError(t, cl.Get(ctx, extKey, clusterExtension)) + cond := apimeta.FindStatusCondition(clusterExtension.Status.Conditions, ocv1alpha1.TypeInstalled) + require.NotNil(t, cond) + require.Equal(t, metav1.ConditionFalse, cond.Status) + require.Equal(t, ocv1alpha1.ReasonInstallationFailed, cond.Reason) + require.Equal(t, tt.wantErr, cond.Message) + } + }) + } +} diff --git a/internal/controllers/successors.go b/internal/controllers/successors.go new file mode 100644 index 000000000..04283d70f --- /dev/null +++ b/internal/controllers/successors.go @@ -0,0 +1,79 @@ +package controllers + +import ( + "fmt" + + mmsemver "github.com/Masterminds/semver/v3" + + "github.com/operator-framework/operator-controller/internal/catalogmetadata" + catalogfilter "github.com/operator-framework/operator-controller/internal/catalogmetadata/filter" + "github.com/operator-framework/operator-controller/pkg/features" +) + +func SuccessorsPredicate(installedBundle *catalogmetadata.Bundle) (catalogfilter.Predicate[catalogmetadata.Bundle], error) { + var successors successorsPredicateFunc = legacySemanticsSuccessorsPredicate + if features.OperatorControllerFeatureGate.Enabled(features.ForceSemverUpgradeConstraints) { + successors = semverSuccessorsPredicate + } + + installedBundleVersion, err := installedBundle.Version() + if err != nil { + return nil, err + } + + installedVersionConstraint, err := mmsemver.NewConstraint(installedBundleVersion.String()) + if err != nil { + return nil, err + } + + successorsPredicate, err := successors(installedBundle) + if err != nil { + return nil, err + } + + // We need either successors or current version (no upgrade) + return catalogfilter.Or( + successorsPredicate, + catalogfilter.And( + catalogfilter.WithPackageName(installedBundle.Package), + catalogfilter.InMastermindsSemverRange(installedVersionConstraint), + ), + ), nil +} + +// successorsPredicateFunc returns a predicate to find successors +// for a bundle. Predicate must not include the current version. +type successorsPredicateFunc func(bundle *catalogmetadata.Bundle) (catalogfilter.Predicate[catalogmetadata.Bundle], error) + +// legacySemanticsSuccessorsPredicate returns a predicate to find successors +// based on legacy OLMv0 semantics which rely on Replaces, Skips and skipRange. +func legacySemanticsSuccessorsPredicate(bundle *catalogmetadata.Bundle) (catalogfilter.Predicate[catalogmetadata.Bundle], error) { + // find the bundles that replace, skip, or skipRange the bundle provided + return catalogfilter.And( + catalogfilter.WithPackageName(bundle.Package), + catalogfilter.LegacySuccessor(bundle), + ), nil +} + +// semverSuccessorsPredicate returns a predicate to find successors based on Semver. +// Successors will not include versions outside the major version of the +// installed bundle as major version is intended to indicate breaking changes. +func semverSuccessorsPredicate(bundle *catalogmetadata.Bundle) (catalogfilter.Predicate[catalogmetadata.Bundle], error) { + currentVersion, err := bundle.Version() + if err != nil { + return nil, err + } + + // Based on current version create a caret range comparison constraint + // to allow only minor and patch version as successors and exclude current version. + constraintStr := fmt.Sprintf("^%s, != %s", currentVersion.String(), currentVersion.String()) + wantedVersionRangeConstraint, err := mmsemver.NewConstraint(constraintStr) + if err != nil { + return nil, err + } + + return catalogfilter.And( + catalogfilter.WithPackageName(bundle.Package), + catalogfilter.InMastermindsSemverRange(wantedVersionRangeConstraint), + ), nil +} diff --git a/internal/resolution/variablesources/installed_package_test.go b/internal/controllers/successors_test.go similarity index 56% rename from internal/resolution/variablesources/installed_package_test.go rename to internal/controllers/successors_test.go index 3c488947b..cda5261ad 100644 --- a/internal/resolution/variablesources/installed_package_test.go +++ b/internal/controllers/successors_test.go @@ -1,7 +1,8 @@ -package variablesources_test +package controllers_test import ( "encoding/json" + "sort" "testing" "github.com/google/go-cmp/cmp" @@ -10,20 +11,17 @@ import ( "github.com/stretchr/testify/require" featuregatetesting "k8s.io/component-base/featuregate/testing" - "github.com/operator-framework/deppy/pkg/deppy/constraint" - "github.com/operator-framework/deppy/pkg/deppy/input" "github.com/operator-framework/operator-registry/alpha/declcfg" "github.com/operator-framework/operator-registry/alpha/property" - rukpakv1alpha2 "github.com/operator-framework/rukpak/api/v1alpha2" - ocv1alpha1 "github.com/operator-framework/operator-controller/api/v1alpha1" "github.com/operator-framework/operator-controller/internal/catalogmetadata" - olmvariables "github.com/operator-framework/operator-controller/internal/resolution/variables" - "github.com/operator-framework/operator-controller/internal/resolution/variablesources" + catalogfilter "github.com/operator-framework/operator-controller/internal/catalogmetadata/filter" + catalogsort "github.com/operator-framework/operator-controller/internal/catalogmetadata/sort" + "github.com/operator-framework/operator-controller/internal/controllers" "github.com/operator-framework/operator-controller/pkg/features" ) -func TestMakeInstalledPackageVariablesWithForceSemverUpgradeConstraintsEnabled(t *testing.T) { +func TestSuccessorsPredicateWithForceSemverUpgradeConstraintsEnabled(t *testing.T) { defer featuregatetesting.SetFeatureGateDuringTest(t, features.OperatorControllerFeatureGate, features.ForceSemverUpgradeConstraints, true)() const testPackageName = "test-package" @@ -173,67 +171,40 @@ func TestMakeInstalledPackageVariablesWithForceSemverUpgradeConstraintsEnabled(t } for _, tt := range []struct { - name string - upgradeConstraintPolicy ocv1alpha1.UpgradeConstraintPolicy - installedBundle *catalogmetadata.Bundle - expectedResult []*olmvariables.InstalledPackageVariable - expectedError string + name string + installedBundle *catalogmetadata.Bundle + expectedResult []*catalogmetadata.Bundle }{ { - name: "with non-zero major version", - upgradeConstraintPolicy: ocv1alpha1.UpgradeConstraintPolicyEnforce, - installedBundle: bundleSet["test-package.v2.0.0"], - expectedResult: []*olmvariables.InstalledPackageVariable{ - olmvariables.NewInstalledPackageVariable(testPackageName, []*catalogmetadata.Bundle{ - // Updates are allowed within the major version. - // Ensure bundles are in version order (high to low) - // with current version at the end - bundleSet["test-package.v2.2.0"], - bundleSet["test-package.v2.1.0"], - bundleSet["test-package.v2.0.0"], - }), + name: "with non-zero major version", + installedBundle: bundleSet["test-package.v2.0.0"], + expectedResult: []*catalogmetadata.Bundle{ + // Updates are allowed within the major version + bundleSet["test-package.v2.2.0"], + bundleSet["test-package.v2.1.0"], + bundleSet["test-package.v2.0.0"], }, }, { - name: "with zero major and zero minor version", - upgradeConstraintPolicy: ocv1alpha1.UpgradeConstraintPolicyEnforce, - installedBundle: bundleSet["test-package.v0.0.1"], - expectedResult: []*olmvariables.InstalledPackageVariable{ - olmvariables.NewInstalledPackageVariable(testPackageName, []*catalogmetadata.Bundle{ - // No updates are allowed in major version zero when minor version is also zero - bundleSet["test-package.v0.0.1"], - }), + name: "with zero major and zero minor version", + installedBundle: bundleSet["test-package.v0.0.1"], + expectedResult: []*catalogmetadata.Bundle{ + // No updates are allowed in major version zero when minor version is also zero + bundleSet["test-package.v0.0.1"], }, }, { - name: "with zero major and non-zero minor version", - upgradeConstraintPolicy: ocv1alpha1.UpgradeConstraintPolicyEnforce, - installedBundle: bundleSet["test-package.v0.1.0"], - expectedResult: []*olmvariables.InstalledPackageVariable{ - olmvariables.NewInstalledPackageVariable(testPackageName, []*catalogmetadata.Bundle{ - // Patch version updates are allowed within the minor version - // Ensure bundles are in version order (high to low) - // with current version at the end. - bundleSet["test-package.v0.1.2"], - bundleSet["test-package.v0.1.1"], - bundleSet["test-package.v0.1.0"], - }), + name: "with zero major and non-zero minor version", + installedBundle: bundleSet["test-package.v0.1.0"], + expectedResult: []*catalogmetadata.Bundle{ + // Patch version updates are allowed within the minor version + bundleSet["test-package.v0.1.2"], + bundleSet["test-package.v0.1.1"], + bundleSet["test-package.v0.1.0"], }, }, { - name: "UpgradeConstraintPolicy is set to Ignore", - upgradeConstraintPolicy: ocv1alpha1.UpgradeConstraintPolicyIgnore, - installedBundle: bundleSet["test-package.v2.0.0"], - expectedResult: []*olmvariables.InstalledPackageVariable{}, - }, - { - name: "no BundleDeployment for an ClusterExtension", - upgradeConstraintPolicy: ocv1alpha1.UpgradeConstraintPolicyEnforce, - expectedResult: []*olmvariables.InstalledPackageVariable{}, - }, - { - name: "installed bundle not found", - upgradeConstraintPolicy: ocv1alpha1.UpgradeConstraintPolicyEnforce, + name: "installed bundle not found", installedBundle: &catalogmetadata.Bundle{ Bundle: declcfg.Bundle{ Name: "test-package.v9.0.0", @@ -245,41 +216,28 @@ func TestMakeInstalledPackageVariablesWithForceSemverUpgradeConstraintsEnabled(t }, InChannels: []*catalogmetadata.Channel{&testPackageChannel}, }, - expectedError: `bundle with image "registry.io/repo/test-package@v9.0.0" for package "test-package" not found in available catalogs but is currently installed via BundleDeployment "test-package-bd"`, + expectedResult: []*catalogmetadata.Bundle{}, }, } { t.Run(tt.name, func(t *testing.T) { - fakeOwnerClusterExtension := fakeClusterExtension("test-extension-semver", testPackageName, tt.upgradeConstraintPolicy) - bundleDeployments := []rukpakv1alpha2.BundleDeployment{} - if tt.installedBundle != nil { - bundleDeployments = append(bundleDeployments, fakeBundleDeployment("test-package-bd", tt.installedBundle.Image, &fakeOwnerClusterExtension)) - } + successors, err := controllers.SuccessorsPredicate(tt.installedBundle) + assert.NoError(t, err) + result := catalogfilter.Filter(allBundles, successors) - installedPackages, err := variablesources.MakeInstalledPackageVariables( - allBundles, - []ocv1alpha1.ClusterExtension{fakeOwnerClusterExtension}, - bundleDeployments, - ) - if tt.expectedError == "" { - assert.NoError(t, err) - } else { - assert.ErrorContains(t, err, tt.expectedError) - } + // sort before comparison for stable order + sort.SliceStable(result, func(i, j int) bool { + return catalogsort.ByVersion(result[i], result[j]) + }) gocmpopts := []cmp.Option{ cmpopts.IgnoreUnexported(catalogmetadata.Bundle{}), - cmp.AllowUnexported( - olmvariables.InstalledPackageVariable{}, - input.SimpleVariable{}, - constraint.DependencyConstraint{}, - ), } - require.Empty(t, cmp.Diff(installedPackages, tt.expectedResult, gocmpopts...)) + require.Empty(t, cmp.Diff(result, tt.expectedResult, gocmpopts...)) }) } } -func TestMakeInstalledPackageVariablesWithForceSemverUpgradeConstraintsDisabled(t *testing.T) { +func TestSuccessorsPredicateWithForceSemverUpgradeConstraintsDisabled(t *testing.T) { defer featuregatetesting.SetFeatureGateDuringTest(t, features.OperatorControllerFeatureGate, features.ForceSemverUpgradeConstraints, false)() const testPackageName = "test-package" @@ -391,7 +349,7 @@ func TestMakeInstalledPackageVariablesWithForceSemverUpgradeConstraintsDisabled( InChannels: []*catalogmetadata.Channel{&testPackageChannel}, }, // We need a bundle from different package to ensure that - // we filter out bundles certain bundle image + // we filter out certain bundle image "some-other-package.v2.3.0": { Bundle: declcfg.Bundle{ Name: "some-other-package.v2.3.0", @@ -410,84 +368,45 @@ func TestMakeInstalledPackageVariablesWithForceSemverUpgradeConstraintsDisabled( } for _, tt := range []struct { - name string - upgradeConstraintPolicy ocv1alpha1.UpgradeConstraintPolicy - installedBundle *catalogmetadata.Bundle - expectedResult []*olmvariables.InstalledPackageVariable - expectedError string + name string + installedBundle *catalogmetadata.Bundle + expectedResult []*catalogmetadata.Bundle }{ { - name: "respect replaces directive from catalog", - upgradeConstraintPolicy: ocv1alpha1.UpgradeConstraintPolicyEnforce, - installedBundle: bundleSet["test-package.v2.0.0"], - expectedResult: []*olmvariables.InstalledPackageVariable{ - olmvariables.NewInstalledPackageVariable(testPackageName, []*catalogmetadata.Bundle{ - // Must only have two bundle: - // - the one which replaces the current version - // - the current version (to allow to stay on the current version) - bundleSet["test-package.v2.1.0"], - bundleSet["test-package.v2.0.0"], - }), + name: "respect replaces directive from catalog", + installedBundle: bundleSet["test-package.v2.0.0"], + expectedResult: []*catalogmetadata.Bundle{ + // Must only have two bundle: + // - the one which replaces the current version + // - the current version (to allow to stay on the current version) + bundleSet["test-package.v2.1.0"], + bundleSet["test-package.v2.0.0"], }, }, { - name: "respect skips directive from catalog", - upgradeConstraintPolicy: ocv1alpha1.UpgradeConstraintPolicyEnforce, - installedBundle: bundleSet["test-package.v2.2.1"], - expectedResult: []*olmvariables.InstalledPackageVariable{ - olmvariables.NewInstalledPackageVariable(testPackageName, []*catalogmetadata.Bundle{ - // Must only have two bundle: - // - the one which skips the current version - // - the current version (to allow to stay on the current version) - bundleSet["test-package.v2.3.0"], - bundleSet["test-package.v2.2.1"], - }), + name: "respect skips directive from catalog", + installedBundle: bundleSet["test-package.v2.2.1"], + expectedResult: []*catalogmetadata.Bundle{ + // Must only have two bundle: + // - the one which skips the current version + // - the current version (to allow to stay on the current version) + bundleSet["test-package.v2.3.0"], + bundleSet["test-package.v2.2.1"], }, }, { - name: "respect skipRange directive from catalog", - upgradeConstraintPolicy: ocv1alpha1.UpgradeConstraintPolicyEnforce, - installedBundle: bundleSet["test-package.v2.3.0"], - expectedResult: []*olmvariables.InstalledPackageVariable{ - olmvariables.NewInstalledPackageVariable(testPackageName, []*catalogmetadata.Bundle{ - // Must only have two bundle: - // - the one which is skipRanges the current version - // - the current version (to allow to stay on the current version) - bundleSet["test-package.v2.4.0"], - bundleSet["test-package.v2.3.0"], - }), - }, - }, - { - name: "UpgradeConstraintPolicy is set to Ignore", - upgradeConstraintPolicy: ocv1alpha1.UpgradeConstraintPolicyIgnore, - installedBundle: bundleSet["test-package.v2.0.0"], - expectedResult: []*olmvariables.InstalledPackageVariable{}, - }, - { - name: "no BundleDeployment for an ClusterExtension", - upgradeConstraintPolicy: ocv1alpha1.UpgradeConstraintPolicyEnforce, - expectedResult: []*olmvariables.InstalledPackageVariable{}, - }, - { - name: "installed bundle not found", - upgradeConstraintPolicy: ocv1alpha1.UpgradeConstraintPolicyEnforce, - installedBundle: &catalogmetadata.Bundle{ - Bundle: declcfg.Bundle{ - Name: "test-package.v9.0.0", - Package: testPackageName, - Image: "registry.io/repo/test-package@v9.0.0", - Properties: []property.Property{ - {Type: property.TypePackage, Value: json.RawMessage(`{"packageName": "test-package", "version": "9.0.0"}`)}, - }, - }, - InChannels: []*catalogmetadata.Channel{&testPackageChannel}, + name: "respect skipRange directive from catalog", + installedBundle: bundleSet["test-package.v2.3.0"], + expectedResult: []*catalogmetadata.Bundle{ + // Must only have two bundle: + // - the one which is skipRanges the current version + // - the current version (to allow to stay on the current version) + bundleSet["test-package.v2.4.0"], + bundleSet["test-package.v2.3.0"], }, - expectedError: `bundle with image "registry.io/repo/test-package@v9.0.0" for package "test-package" not found in available catalogs but is currently installed via BundleDeployment "test-package-bd"`, }, { - name: "installed bundle not found, but UpgradeConstraintPolicy is set to Ignore", - upgradeConstraintPolicy: ocv1alpha1.UpgradeConstraintPolicyIgnore, + name: "installed bundle not found", installedBundle: &catalogmetadata.Bundle{ Bundle: declcfg.Bundle{ Name: "test-package.v9.0.0", @@ -499,36 +418,23 @@ func TestMakeInstalledPackageVariablesWithForceSemverUpgradeConstraintsDisabled( }, InChannels: []*catalogmetadata.Channel{&testPackageChannel}, }, - expectedResult: []*olmvariables.InstalledPackageVariable{}, + expectedResult: []*catalogmetadata.Bundle{}, }, } { t.Run(tt.name, func(t *testing.T) { - fakeOwnerClusterExtension := fakeClusterExtension("test-extension-legacy", testPackageName, tt.upgradeConstraintPolicy) - bundleDeployments := []rukpakv1alpha2.BundleDeployment{} - if tt.installedBundle != nil { - bundleDeployments = append(bundleDeployments, fakeBundleDeployment("test-package-bd", tt.installedBundle.Image, &fakeOwnerClusterExtension)) - } + successors, err := controllers.SuccessorsPredicate(tt.installedBundle) + assert.NoError(t, err) + result := catalogfilter.Filter(allBundles, successors) - installedPackages, err := variablesources.MakeInstalledPackageVariables( - allBundles, - []ocv1alpha1.ClusterExtension{fakeOwnerClusterExtension}, - bundleDeployments, - ) - if tt.expectedError == "" { - assert.NoError(t, err) - } else { - assert.ErrorContains(t, err, tt.expectedError) - } + // sort before comparison for stable order + sort.SliceStable(result, func(i, j int) bool { + return catalogsort.ByVersion(result[i], result[j]) + }) gocmpopts := []cmp.Option{ cmpopts.IgnoreUnexported(catalogmetadata.Bundle{}), - cmp.AllowUnexported( - olmvariables.InstalledPackageVariable{}, - input.SimpleVariable{}, - constraint.DependencyConstraint{}, - ), } - require.Empty(t, cmp.Diff(installedPackages, tt.expectedResult, gocmpopts...)) + require.Empty(t, cmp.Diff(result, tt.expectedResult, gocmpopts...)) }) } } diff --git a/internal/controllers/suite_test.go b/internal/controllers/suite_test.go index cceaeaa5d..bb71f0a4f 100644 --- a/internal/controllers/suite_test.go +++ b/internal/controllers/suite_test.go @@ -28,8 +28,6 @@ import ( "sigs.k8s.io/controller-runtime/pkg/client" "sigs.k8s.io/controller-runtime/pkg/envtest" - "github.com/operator-framework/deppy/pkg/deppy/solver" - "github.com/operator-framework/operator-controller/internal/controllers" "github.com/operator-framework/operator-controller/pkg/scheme" testutil "github.com/operator-framework/operator-controller/test/util" @@ -43,16 +41,11 @@ func newClient(t *testing.T) client.Client { } func newClientAndReconciler(t *testing.T) (client.Client, *controllers.ClusterExtensionReconciler) { - resolver, err := solver.New() - require.NoError(t, err) - cl := newClient(t) fakeCatalogClient := testutil.NewFakeCatalogClient(testBundleList) reconciler := &controllers.ClusterExtensionReconciler{ Client: cl, BundleProvider: &fakeCatalogClient, - Scheme: scheme.Scheme, - Resolver: resolver, } return cl, reconciler } diff --git a/internal/controllers/variables.go b/internal/controllers/variables.go deleted file mode 100644 index d03f8bcae..000000000 --- a/internal/controllers/variables.go +++ /dev/null @@ -1,60 +0,0 @@ -/* -Copyright 2023. - -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 controllers - -import ( - "github.com/operator-framework/deppy/pkg/deppy" - rukpakv1alpha2 "github.com/operator-framework/rukpak/api/v1alpha2" - - ocv1alpha1 "github.com/operator-framework/operator-controller/api/v1alpha1" - "github.com/operator-framework/operator-controller/internal/catalogmetadata" - "github.com/operator-framework/operator-controller/internal/resolution/variablesources" -) - -func GenerateVariables(allBundles []*catalogmetadata.Bundle, clusterExtensions []ocv1alpha1.ClusterExtension, bundleDeployments []rukpakv1alpha2.BundleDeployment) ([]deppy.Variable, error) { - requiredPackages, err := variablesources.MakeRequiredPackageVariables(allBundles, clusterExtensions) - if err != nil { - return nil, err - } - - installedPackages, err := variablesources.MakeInstalledPackageVariables(allBundles, clusterExtensions, bundleDeployments) - if err != nil { - return nil, err - } - - bundles, err := variablesources.MakeBundleVariables(allBundles, requiredPackages, installedPackages) - if err != nil { - return nil, err - } - - bundleUniqueness := variablesources.MakeBundleUniquenessVariables(bundles) - - result := []deppy.Variable{} - for _, v := range requiredPackages { - result = append(result, v) - } - for _, v := range installedPackages { - result = append(result, v) - } - for _, v := range bundles { - result = append(result, v) - } - for _, v := range bundleUniqueness { - result = append(result, v) - } - return result, nil -} diff --git a/internal/controllers/variables_test.go b/internal/controllers/variables_test.go deleted file mode 100644 index 3db23daa8..000000000 --- a/internal/controllers/variables_test.go +++ /dev/null @@ -1,121 +0,0 @@ -package controllers_test - -import ( - "encoding/json" - "fmt" - "testing" - - "github.com/google/go-cmp/cmp" - "github.com/google/go-cmp/cmp/cmpopts" - "github.com/stretchr/testify/require" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - "k8s.io/apimachinery/pkg/util/rand" - "k8s.io/utils/ptr" - - "github.com/operator-framework/deppy/pkg/deppy" - "github.com/operator-framework/deppy/pkg/deppy/constraint" - "github.com/operator-framework/deppy/pkg/deppy/input" - "github.com/operator-framework/operator-registry/alpha/declcfg" - "github.com/operator-framework/operator-registry/alpha/property" - rukpakv1alpha2 "github.com/operator-framework/rukpak/api/v1alpha2" - - ocv1alpha1 "github.com/operator-framework/operator-controller/api/v1alpha1" - "github.com/operator-framework/operator-controller/internal/catalogmetadata" - "github.com/operator-framework/operator-controller/internal/controllers" - olmvariables "github.com/operator-framework/operator-controller/internal/resolution/variables" -) - -func TestVariableSource(t *testing.T) { - stableChannel := catalogmetadata.Channel{Channel: declcfg.Channel{ - Name: "stable", - Entries: []declcfg.ChannelEntry{ - { - Name: "packageA.v2.0.0", - }, - }, - }} - bundleSet := map[string]*catalogmetadata.Bundle{ - "packageA.v2.0.0": { - Bundle: declcfg.Bundle{ - Name: "packageA.v2.0.0", - Package: "packageA", - Image: "foo.io/packageA/packageA:v2.0.0", - Properties: []property.Property{ - {Type: property.TypePackage, Value: json.RawMessage(`{"packageName":"packageA","version":"2.0.0"}`)}, - }, - }, - CatalogName: "fake-catalog", - InChannels: []*catalogmetadata.Channel{&stableChannel}, - }, - } - allBundles := make([]*catalogmetadata.Bundle, 0, len(bundleSet)) - for _, bundle := range bundleSet { - allBundles = append(allBundles, bundle) - } - - pkgName := "packageA" - clusterExtensionName := fmt.Sprintf("clusterextension-test-%s", rand.String(8)) - clusterExtension := ocv1alpha1.ClusterExtension{ - ObjectMeta: metav1.ObjectMeta{Name: clusterExtensionName}, - Spec: ocv1alpha1.ClusterExtensionSpec{ - PackageName: pkgName, - Channel: "stable", - Version: "2.0.0", - }, - } - - bd := rukpakv1alpha2.BundleDeployment{ - ObjectMeta: metav1.ObjectMeta{ - Name: clusterExtensionName, - OwnerReferences: []metav1.OwnerReference{ - { - APIVersion: ocv1alpha1.GroupVersion.String(), - Kind: "ClusterExtension", - Name: clusterExtension.Name, - UID: clusterExtension.UID, - Controller: ptr.To(true), - BlockOwnerDeletion: ptr.To(true), - }, - }, - }, - Spec: rukpakv1alpha2.BundleDeploymentSpec{ - ProvisionerClassName: "core-rukpak-io-registry", - Source: rukpakv1alpha2.BundleSource{ - Type: rukpakv1alpha2.SourceTypeImage, - Image: &rukpakv1alpha2.ImageSource{ - Ref: "foo.io/packageA/packageA:v2.0.0", - }, - }, - }, - } - - vars, err := controllers.GenerateVariables(allBundles, []ocv1alpha1.ClusterExtension{clusterExtension}, []rukpakv1alpha2.BundleDeployment{bd}) - require.NoError(t, err) - - expectedVars := []deppy.Variable{ - olmvariables.NewRequiredPackageVariable("packageA", []*catalogmetadata.Bundle{ - bundleSet["packageA.v2.0.0"], - }), - olmvariables.NewInstalledPackageVariable("packageA", []*catalogmetadata.Bundle{ - bundleSet["packageA.v2.0.0"], - }), - olmvariables.NewBundleVariable(bundleSet["packageA.v2.0.0"], nil), - olmvariables.NewBundleUniquenessVariable( - "packageA package uniqueness", - deppy.Identifier("fake-catalog-packageA-packageA.v2.0.0"), - ), - } - gocmpopts := []cmp.Option{ - cmpopts.IgnoreUnexported(catalogmetadata.Bundle{}), - cmp.AllowUnexported( - olmvariables.RequiredPackageVariable{}, - olmvariables.InstalledPackageVariable{}, - olmvariables.BundleVariable{}, - olmvariables.BundleUniquenessVariable{}, - input.SimpleVariable{}, - constraint.DependencyConstraint{}, - constraint.AtMostConstraint{}, - ), - } - require.Empty(t, cmp.Diff(vars, expectedVars, gocmpopts...)) -} diff --git a/internal/resolution/variables/bundle.go b/internal/resolution/variables/bundle.go deleted file mode 100644 index 51723fb12..000000000 --- a/internal/resolution/variables/bundle.go +++ /dev/null @@ -1,67 +0,0 @@ -package variables - -import ( - "fmt" - - "github.com/operator-framework/deppy/pkg/deppy" - "github.com/operator-framework/deppy/pkg/deppy/constraint" - "github.com/operator-framework/deppy/pkg/deppy/input" - - "github.com/operator-framework/operator-controller/internal/catalogmetadata" -) - -var _ deppy.Variable = &BundleVariable{} - -type BundleVariable struct { - *input.SimpleVariable - bundle *catalogmetadata.Bundle - dependencies []*catalogmetadata.Bundle -} - -func (b *BundleVariable) Bundle() *catalogmetadata.Bundle { - return b.bundle -} - -func (b *BundleVariable) Dependencies() []*catalogmetadata.Bundle { - return b.dependencies -} - -func NewBundleVariable(bundle *catalogmetadata.Bundle, dependencies []*catalogmetadata.Bundle) *BundleVariable { - dependencyIDs := make([]deppy.Identifier, 0, len(dependencies)) - for _, dependency := range dependencies { - dependencyIDs = append(dependencyIDs, BundleVariableID(dependency)) - } - var constraints []deppy.Constraint - if len(dependencyIDs) > 0 { - constraints = append(constraints, constraint.Dependency(dependencyIDs...)) - } - return &BundleVariable{ - SimpleVariable: input.NewSimpleVariable(BundleVariableID(bundle), constraints...), - bundle: bundle, - dependencies: dependencies, - } -} - -var _ deppy.Variable = &BundleUniquenessVariable{} - -type BundleUniquenessVariable struct { - *input.SimpleVariable -} - -// NewBundleUniquenessVariable creates a new variable that instructs the resolver to choose at most a single bundle -// from the input 'atMostID'. Examples: -// 1. restrict the solution to at most a single bundle per package -// 2. restrict the solution to at most a single bundler per provided gvk -// this guarantees that no two extensions provide the same gvk and no two version of the same extension are running at the same time -func NewBundleUniquenessVariable(id deppy.Identifier, atMostIDs ...deppy.Identifier) *BundleUniquenessVariable { - return &BundleUniquenessVariable{ - SimpleVariable: input.NewSimpleVariable(id, constraint.AtMost(1, atMostIDs...)), - } -} - -// BundleVariableID returns an ID for a given bundle. -func BundleVariableID(bundle *catalogmetadata.Bundle) deppy.Identifier { - return deppy.Identifier( - fmt.Sprintf("%s-%s-%s", bundle.CatalogName, bundle.Package, bundle.Name), - ) -} diff --git a/internal/resolution/variables/bundle_test.go b/internal/resolution/variables/bundle_test.go deleted file mode 100644 index 26d831f54..000000000 --- a/internal/resolution/variables/bundle_test.go +++ /dev/null @@ -1,74 +0,0 @@ -package variables_test - -import ( - "encoding/json" - "testing" - - "github.com/operator-framework/deppy/pkg/deppy" - "github.com/operator-framework/deppy/pkg/deppy/constraint" - "github.com/operator-framework/operator-registry/alpha/declcfg" - "github.com/operator-framework/operator-registry/alpha/property" - - "github.com/operator-framework/operator-controller/internal/catalogmetadata" - olmvariables "github.com/operator-framework/operator-controller/internal/resolution/variables" -) - -func TestBundleVariable(t *testing.T) { - bundle := &catalogmetadata.Bundle{ - InChannels: []*catalogmetadata.Channel{ - {Channel: declcfg.Channel{Name: "stable"}}, - }, - Bundle: declcfg.Bundle{Name: "bundle-1", Properties: []property.Property{ - {Type: property.TypePackage, Value: json.RawMessage(`{"packageName": "test-package", "version": "1.0.0"}`)}, - }}, - } - dependencies := []*catalogmetadata.Bundle{ - { - InChannels: []*catalogmetadata.Channel{ - {Channel: declcfg.Channel{Name: "stable"}}, - }, - Bundle: declcfg.Bundle{Name: "bundle-2", Properties: []property.Property{ - {Type: property.TypePackage, Value: json.RawMessage(`{"packageName": "test-package", "version": "2.0.0"}`)}, - }}, - }, - { - InChannels: []*catalogmetadata.Channel{ - {Channel: declcfg.Channel{Name: "stable"}}, - }, - Bundle: declcfg.Bundle{Name: "bundle-3", Properties: []property.Property{ - {Type: property.TypePackage, Value: json.RawMessage(`{"packageName": "test-package", "version": "3.0.0"}`)}, - }}, - }, - } - bv := olmvariables.NewBundleVariable(bundle, dependencies) - - if bv.Bundle() != bundle { - t.Errorf("bundle '%v' does not match expected '%v'", bv.Bundle(), bundle) - } - - for i, d := range bv.Dependencies() { - if d != dependencies[i] { - t.Errorf("dependency[%v] '%v' does not match expected '%v'", i, d, dependencies[i]) - } - } -} - -func TestBundleUniquenessVariable(t *testing.T) { - id := deppy.IdentifierFromString("test-id") - atMostIDs := []deppy.Identifier{ - deppy.IdentifierFromString("test-at-most-id-1"), - deppy.IdentifierFromString("test-at-most-id-2"), - } - globalConstraintVariable := olmvariables.NewBundleUniquenessVariable(id, atMostIDs...) - - if globalConstraintVariable.Identifier() != id { - t.Errorf("identifier '%v' does not match expected '%v'", globalConstraintVariable.Identifier(), id) - } - - constraints := []deppy.Constraint{constraint.AtMost(1, atMostIDs...)} - for i, c := range globalConstraintVariable.Constraints() { - if c.String("test") != constraints[i].String("test") { - t.Errorf("constraint[%v] '%v' does not match expected '%v'", i, c, constraints[i]) - } - } -} diff --git a/internal/resolution/variables/installed_package.go b/internal/resolution/variables/installed_package.go deleted file mode 100644 index f5ccd12cd..000000000 --- a/internal/resolution/variables/installed_package.go +++ /dev/null @@ -1,34 +0,0 @@ -package variables - -import ( - "fmt" - - "github.com/operator-framework/deppy/pkg/deppy" - "github.com/operator-framework/deppy/pkg/deppy/constraint" - "github.com/operator-framework/deppy/pkg/deppy/input" - - "github.com/operator-framework/operator-controller/internal/catalogmetadata" -) - -var _ deppy.Variable = &InstalledPackageVariable{} - -type InstalledPackageVariable struct { - *input.SimpleVariable - bundles []*catalogmetadata.Bundle -} - -func (r *InstalledPackageVariable) Bundles() []*catalogmetadata.Bundle { - return r.bundles -} - -func NewInstalledPackageVariable(packageName string, bundles []*catalogmetadata.Bundle) *InstalledPackageVariable { - id := deppy.IdentifierFromString(fmt.Sprintf("installed package %s", packageName)) - variableIDs := make([]deppy.Identifier, 0, len(bundles)) - for _, bundle := range bundles { - variableIDs = append(variableIDs, BundleVariableID(bundle)) - } - return &InstalledPackageVariable{ - SimpleVariable: input.NewSimpleVariable(id, constraint.Mandatory(), constraint.Dependency(variableIDs...)), - bundles: bundles, - } -} diff --git a/internal/resolution/variables/installed_package_test.go b/internal/resolution/variables/installed_package_test.go deleted file mode 100644 index 850783a6c..000000000 --- a/internal/resolution/variables/installed_package_test.go +++ /dev/null @@ -1,42 +0,0 @@ -package variables_test - -import ( - "encoding/json" - "fmt" - "testing" - - "github.com/operator-framework/deppy/pkg/deppy" - "github.com/operator-framework/operator-registry/alpha/declcfg" - "github.com/operator-framework/operator-registry/alpha/property" - - "github.com/operator-framework/operator-controller/internal/catalogmetadata" - olmvariables "github.com/operator-framework/operator-controller/internal/resolution/variables" -) - -func TestInstalledPackageVariable(t *testing.T) { - packageName := "test-package" - - bundles := []*catalogmetadata.Bundle{ - {Bundle: declcfg.Bundle{Name: "bundle-1", Properties: []property.Property{ - {Type: property.TypePackage, Value: json.RawMessage(`{"packageName": "test-package", "version": "1.0.0"}`)}, - }}}, - {Bundle: declcfg.Bundle{Name: "bundle-2", Properties: []property.Property{ - {Type: property.TypePackage, Value: json.RawMessage(`{"packageName": "test-package", "version": "2.0.0"}`)}, - }}}, - {Bundle: declcfg.Bundle{Name: "bundle-3", Properties: []property.Property{ - {Type: property.TypePackage, Value: json.RawMessage(`{"packageName": "test-package", "version": "3.0.0"}`)}, - }}}, - } - ipv := olmvariables.NewInstalledPackageVariable(packageName, bundles) - - id := deppy.IdentifierFromString(fmt.Sprintf("installed package %s", packageName)) - if ipv.Identifier() != id { - t.Errorf("package name '%v' does not match expected '%v'", ipv.Identifier(), id) - } - - for i, e := range ipv.Bundles() { - if e != bundles[i] { - t.Errorf("bundle[%v] '%v' does not match expected '%v'", i, e, bundles[i]) - } - } -} diff --git a/internal/resolution/variables/required_package.go b/internal/resolution/variables/required_package.go deleted file mode 100644 index dd1add43b..000000000 --- a/internal/resolution/variables/required_package.go +++ /dev/null @@ -1,34 +0,0 @@ -package variables - -import ( - "fmt" - - "github.com/operator-framework/deppy/pkg/deppy" - "github.com/operator-framework/deppy/pkg/deppy/constraint" - "github.com/operator-framework/deppy/pkg/deppy/input" - - "github.com/operator-framework/operator-controller/internal/catalogmetadata" -) - -var _ deppy.Variable = &RequiredPackageVariable{} - -type RequiredPackageVariable struct { - *input.SimpleVariable - bundles []*catalogmetadata.Bundle -} - -func (r *RequiredPackageVariable) Bundles() []*catalogmetadata.Bundle { - return r.bundles -} - -func NewRequiredPackageVariable(packageName string, bundles []*catalogmetadata.Bundle) *RequiredPackageVariable { - id := deppy.IdentifierFromString(fmt.Sprintf("required package %s", packageName)) - variableIDs := make([]deppy.Identifier, 0, len(bundles)) - for _, bundle := range bundles { - variableIDs = append(variableIDs, BundleVariableID(bundle)) - } - return &RequiredPackageVariable{ - SimpleVariable: input.NewSimpleVariable(id, constraint.Mandatory(), constraint.Dependency(variableIDs...)), - bundles: bundles, - } -} diff --git a/internal/resolution/variables/required_package_test.go b/internal/resolution/variables/required_package_test.go deleted file mode 100644 index d8ece1473..000000000 --- a/internal/resolution/variables/required_package_test.go +++ /dev/null @@ -1,45 +0,0 @@ -package variables_test - -import ( - "encoding/json" - "fmt" - "testing" - - "github.com/operator-framework/deppy/pkg/deppy" - "github.com/operator-framework/operator-registry/alpha/declcfg" - "github.com/operator-framework/operator-registry/alpha/property" - - "github.com/operator-framework/operator-controller/internal/catalogmetadata" - olmvariables "github.com/operator-framework/operator-controller/internal/resolution/variables" -) - -func TestRequiredPackageVariable(t *testing.T) { - packageName := "test-package" - bundles := []*catalogmetadata.Bundle{ - {Bundle: declcfg.Bundle{Name: "bundle-1", Properties: []property.Property{ - {Type: property.TypePackage, Value: json.RawMessage(`{"packageName": "test-package", "version": "1.0.0"}`)}, - }}}, - {Bundle: declcfg.Bundle{Name: "bundle-2", Properties: []property.Property{ - {Type: property.TypePackage, Value: json.RawMessage(`{"packageName": "test-package", "version": "2.0.0"}`)}, - }}}, - {Bundle: declcfg.Bundle{Name: "bundle-3", Properties: []property.Property{ - {Type: property.TypePackage, Value: json.RawMessage(`{"packageName": "test-package", "version": "3.0.0"}`)}, - }}}, - } - rpv := olmvariables.NewRequiredPackageVariable(packageName, bundles) - - id := deppy.IdentifierFromString(fmt.Sprintf("required package %s", packageName)) - if rpv.Identifier() != id { - t.Errorf("package name '%v' does not match expected '%v'", rpv.Identifier(), id) - } - - for i, e := range rpv.Bundles() { - if e != bundles[i] { - t.Errorf("bundle entity[%v] '%v' does not match expected '%v'", i, e, bundles[i]) - } - } - - // TODO: add this test once https://github.com/operator-framework/deppy/pull/85 gets merged - // then we'll be able to inspect constraint types - // "should contain both mandatory and dependency constraints" -} diff --git a/internal/resolution/variablesources/bundle.go b/internal/resolution/variablesources/bundle.go deleted file mode 100644 index 6e7f83dc2..000000000 --- a/internal/resolution/variablesources/bundle.go +++ /dev/null @@ -1,92 +0,0 @@ -package variablesources - -import ( - "fmt" - "sort" - - "k8s.io/apimachinery/pkg/util/sets" - - "github.com/operator-framework/deppy/pkg/deppy" - - "github.com/operator-framework/operator-controller/internal/catalogmetadata" - catalogfilter "github.com/operator-framework/operator-controller/internal/catalogmetadata/filter" - catalogsort "github.com/operator-framework/operator-controller/internal/catalogmetadata/sort" - olmvariables "github.com/operator-framework/operator-controller/internal/resolution/variables" -) - -func MakeBundleVariables( - allBundles []*catalogmetadata.Bundle, - requiredPackages []*olmvariables.RequiredPackageVariable, - installedPackages []*olmvariables.InstalledPackageVariable, -) ([]*olmvariables.BundleVariable, error) { - var bundleQueue []*catalogmetadata.Bundle - for _, variable := range requiredPackages { - bundleQueue = append(bundleQueue, variable.Bundles()...) - } - for _, variable := range installedPackages { - bundleQueue = append(bundleQueue, variable.Bundles()...) - } - - // build bundle and dependency variables - var result []*olmvariables.BundleVariable - visited := sets.Set[deppy.Identifier]{} - for len(bundleQueue) > 0 { - // pop head of queue - var head *catalogmetadata.Bundle - head, bundleQueue = bundleQueue[0], bundleQueue[1:] - - id := olmvariables.BundleVariableID(head) - - // ignore bundles that have already been processed - if visited.Has(id) { - continue - } - visited.Insert(id) - - // get bundle dependencies - dependencies, err := filterBundleDependencies(allBundles, head) - if err != nil { - return nil, fmt.Errorf("could not determine dependencies for bundle %q from package %q in catalog %q: %s", head.Name, head.Package, head.CatalogName, err) - } - - // add bundle dependencies to queue for processing - bundleQueue = append(bundleQueue, dependencies...) - - // create variable - result = append(result, olmvariables.NewBundleVariable(head, dependencies)) - } - - return result, nil -} - -func filterBundleDependencies(allBundles []*catalogmetadata.Bundle, bundle *catalogmetadata.Bundle) ([]*catalogmetadata.Bundle, error) { - var dependencies []*catalogmetadata.Bundle - added := sets.Set[deppy.Identifier]{} - - // gather required package dependencies - requiredPackages, _ := bundle.RequiredPackages() - for _, requiredPackage := range requiredPackages { - packageDependencyBundles := catalogfilter.Filter(allBundles, catalogfilter.And( - catalogfilter.WithPackageName(requiredPackage.PackageName), - catalogfilter.InBlangSemverRange(requiredPackage.SemverRange), - )) - if len(packageDependencyBundles) == 0 { - return nil, fmt.Errorf("no bundles found matching required package %q in range %q", requiredPackage.PackageName, requiredPackage.VersionRange) - } - for i := 0; i < len(packageDependencyBundles); i++ { - bundle := packageDependencyBundles[i] - id := olmvariables.BundleVariableID(bundle) - if !added.Has(id) { - dependencies = append(dependencies, bundle) - added.Insert(id) - } - } - } - - // sort bundles in version order - sort.SliceStable(dependencies, func(i, j int) bool { - return catalogsort.ByVersion(dependencies[i], dependencies[j]) - }) - - return dependencies, nil -} diff --git a/internal/resolution/variablesources/bundle_test.go b/internal/resolution/variablesources/bundle_test.go deleted file mode 100644 index 9cad4ae9f..000000000 --- a/internal/resolution/variablesources/bundle_test.go +++ /dev/null @@ -1,195 +0,0 @@ -package variablesources_test - -import ( - "encoding/json" - "testing" - - "github.com/google/go-cmp/cmp" - "github.com/google/go-cmp/cmp/cmpopts" - "github.com/stretchr/testify/assert" - "github.com/stretchr/testify/require" - - "github.com/operator-framework/deppy/pkg/deppy/constraint" - "github.com/operator-framework/deppy/pkg/deppy/input" - "github.com/operator-framework/operator-registry/alpha/declcfg" - "github.com/operator-framework/operator-registry/alpha/property" - - "github.com/operator-framework/operator-controller/internal/catalogmetadata" - olmvariables "github.com/operator-framework/operator-controller/internal/resolution/variables" - "github.com/operator-framework/operator-controller/internal/resolution/variablesources" -) - -func TestMakeBundleVariables_ValidDepedencies(t *testing.T) { - const fakeCatalogName = "fake-catalog" - fakeChannel := catalogmetadata.Channel{Channel: declcfg.Channel{Name: "stable"}} - bundleSet := map[string]*catalogmetadata.Bundle{ - // Test package which we will be using as input into - // the testable function - "test-package.v1.0.0": { - Bundle: declcfg.Bundle{ - Name: "test-package.v1.0.0", - Package: "test-package", - Properties: []property.Property{ - {Type: property.TypePackage, Value: json.RawMessage(`{"packageName": "test-package", "version": "1.0.0"}`)}, - {Type: property.TypePackageRequired, Value: json.RawMessage(`{"packageName": "first-level-dependency", "versionRange": ">=1.0.0 <2.0.0"}`)}, - }, - }, - CatalogName: fakeCatalogName, - InChannels: []*catalogmetadata.Channel{&fakeChannel}, - }, - - // First level dependency of test-package. Will be explicitly - // provided into the testable function as part of variable. - // This package must have at least one dependency with a version - // range so we can test that result has correct ordering: - // the testable function must give priority to newer versions. - "first-level-dependency.v1.0.0": { - Bundle: declcfg.Bundle{ - Name: "first-level-dependency.v1.0.0", - Package: "first-level-dependency", - Properties: []property.Property{ - {Type: property.TypePackage, Value: json.RawMessage(`{"packageName": "first-level-dependency", "version": "1.0.0"}`)}, - {Type: property.TypePackageRequired, Value: json.RawMessage(`{"packageName": "second-level-dependency", "versionRange": ">=1.0.0 <2.0.0"}`)}, - }, - }, - CatalogName: fakeCatalogName, - InChannels: []*catalogmetadata.Channel{&fakeChannel}, - }, - - // Second level dependency that matches requirements of the first level dependency. - "second-level-dependency.v1.0.0": { - Bundle: declcfg.Bundle{ - Name: "second-level-dependency.v1.0.0", - Package: "second-level-dependency", - Properties: []property.Property{ - {Type: property.TypePackage, Value: json.RawMessage(`{"packageName": "second-level-dependency", "version": "1.0.0"}`)}, - }, - }, - CatalogName: fakeCatalogName, - InChannels: []*catalogmetadata.Channel{&fakeChannel}, - }, - - // Second level dependency that matches requirements of the first level dependency. - "second-level-dependency.v1.0.1": { - Bundle: declcfg.Bundle{ - Name: "second-level-dependency.v1.0.1", - Package: "second-level-dependency", - Properties: []property.Property{ - {Type: property.TypePackage, Value: json.RawMessage(`{"packageName": "second-level-dependency", "version": "1.0.1"}`)}, - }, - }, - CatalogName: fakeCatalogName, - InChannels: []*catalogmetadata.Channel{&fakeChannel}, - }, - - // Second level dependency that does not match requirements of the first level dependency. - "second-level-dependency.v2.0.0": { - Bundle: declcfg.Bundle{ - Name: "second-level-dependency.v2.0.0", - Package: "second-level-dependency", - Properties: []property.Property{ - {Type: property.TypePackage, Value: json.RawMessage(`{"packageName": "second-level-dependency", "version": "2.0.0"}`)}, - }, - }, - CatalogName: fakeCatalogName, - InChannels: []*catalogmetadata.Channel{&fakeChannel}, - }, - - // Package that is in a our fake catalog, but is not involved - // in this dependency chain. We need this to make sure that - // the testable function filters out unrelated bundles. - "uninvolved-package.v1.0.0": { - Bundle: declcfg.Bundle{ - Name: "uninvolved-package.v1.0.0", - Package: "uninvolved-package", - Properties: []property.Property{ - {Type: property.TypePackage, Value: json.RawMessage(`{"packageName": "uninvolved-package", "version": "1.0.0"}`)}, - }, - }, - CatalogName: fakeCatalogName, - InChannels: []*catalogmetadata.Channel{&fakeChannel}, - }, - } - - allBundles := make([]*catalogmetadata.Bundle, 0, len(bundleSet)) - for _, bundle := range bundleSet { - allBundles = append(allBundles, bundle) - } - requiredPackages := []*olmvariables.RequiredPackageVariable{ - olmvariables.NewRequiredPackageVariable("test-package", []*catalogmetadata.Bundle{ - bundleSet["first-level-dependency.v1.0.0"], - }), - } - installedPackages := []*olmvariables.InstalledPackageVariable{ - olmvariables.NewInstalledPackageVariable("test-package", []*catalogmetadata.Bundle{ - bundleSet["first-level-dependency.v1.0.0"], - }), - } - - bundles, err := variablesources.MakeBundleVariables(allBundles, requiredPackages, installedPackages) - require.NoError(t, err) - - // Each dependency must have a variable. - // Dependencies from the same package must be sorted by version - // with higher versions first. - expectedVariables := []*olmvariables.BundleVariable{ - olmvariables.NewBundleVariable( - bundleSet["first-level-dependency.v1.0.0"], - []*catalogmetadata.Bundle{ - bundleSet["second-level-dependency.v1.0.1"], - bundleSet["second-level-dependency.v1.0.0"], - }, - ), - olmvariables.NewBundleVariable( - bundleSet["second-level-dependency.v1.0.1"], - nil, - ), - olmvariables.NewBundleVariable( - bundleSet["second-level-dependency.v1.0.0"], - nil, - ), - } - gocmpopts := []cmp.Option{ - cmpopts.IgnoreUnexported(catalogmetadata.Bundle{}), - cmp.AllowUnexported( - olmvariables.BundleVariable{}, - input.SimpleVariable{}, - constraint.DependencyConstraint{}, - ), - } - require.Empty(t, cmp.Diff(bundles, expectedVariables, gocmpopts...)) -} - -func TestMakeBundleVariables_NonExistentDepedencies(t *testing.T) { - const fakeCatalogName = "fake-catalog" - fakeChannel := catalogmetadata.Channel{Channel: declcfg.Channel{Name: "stable"}} - bundleSet := map[string]*catalogmetadata.Bundle{ - "test-package.v1.0.0": { - Bundle: declcfg.Bundle{ - Name: "test-package.v1.0.0", - Package: "test-package", - Properties: []property.Property{ - {Type: property.TypePackage, Value: json.RawMessage(`{"packageName": "test-package", "version": "1.0.0"}`)}, - {Type: property.TypePackageRequired, Value: json.RawMessage(`{"packageName": "first-level-dependency", "versionRange": ">=1.0.0 <2.0.0"}`)}, - }, - }, - CatalogName: fakeCatalogName, - InChannels: []*catalogmetadata.Channel{&fakeChannel}, - }, - } - - allBundles := make([]*catalogmetadata.Bundle, 0, len(bundleSet)) - for _, bundle := range bundleSet { - allBundles = append(allBundles, bundle) - } - requiredPackages := []*olmvariables.RequiredPackageVariable{ - olmvariables.NewRequiredPackageVariable("test-package", []*catalogmetadata.Bundle{ - bundleSet["test-package.v1.0.0"], - }), - } - installedPackages := []*olmvariables.InstalledPackageVariable{} - - bundles, err := variablesources.MakeBundleVariables(allBundles, requiredPackages, installedPackages) - assert.ErrorContains(t, err, `could not determine dependencies for bundle "test-package.v1.0.0" from package "test-package" in catalog "fake-catalog"`) - assert.Nil(t, bundles) -} diff --git a/internal/resolution/variablesources/bundle_uniqueness.go b/internal/resolution/variablesources/bundle_uniqueness.go deleted file mode 100644 index cb4546e2c..000000000 --- a/internal/resolution/variablesources/bundle_uniqueness.go +++ /dev/null @@ -1,51 +0,0 @@ -package variablesources - -import ( - "fmt" - - "k8s.io/apimachinery/pkg/util/sets" - - "github.com/operator-framework/deppy/pkg/deppy" - - "github.com/operator-framework/operator-controller/internal/catalogmetadata" - olmvariables "github.com/operator-framework/operator-controller/internal/resolution/variables" -) - -// MakeBundleUniquenessVariables produces variables that constrain -// the solution to at most 1 bundle per package. -// These variables guarantee that no two versions of -// the same package are running at the same time. -func MakeBundleUniquenessVariables(bundleVariables []*olmvariables.BundleVariable) []*olmvariables.BundleUniquenessVariable { - result := []*olmvariables.BundleUniquenessVariable{} - - bundleIDs := sets.Set[deppy.Identifier]{} - packageOrder := []string{} - bundleOrder := map[string][]deppy.Identifier{} - for _, bundleVariable := range bundleVariables { - bundles := make([]*catalogmetadata.Bundle, 0, 1+len(bundleVariable.Dependencies())) - bundles = append(bundles, bundleVariable.Bundle()) - bundles = append(bundles, bundleVariable.Dependencies()...) - for _, bundle := range bundles { - id := olmvariables.BundleVariableID(bundle) - // get bundleID package and update map - packageName := bundle.Package - - if _, ok := bundleOrder[packageName]; !ok { - packageOrder = append(packageOrder, packageName) - } - - if !bundleIDs.Has(id) { - bundleIDs.Insert(id) - bundleOrder[packageName] = append(bundleOrder[packageName], id) - } - } - } - - // create global constraint variables - for _, packageName := range packageOrder { - varID := deppy.IdentifierFromString(fmt.Sprintf("%s package uniqueness", packageName)) - result = append(result, olmvariables.NewBundleUniquenessVariable(varID, bundleOrder[packageName]...)) - } - - return result -} diff --git a/internal/resolution/variablesources/bundle_uniqueness_test.go b/internal/resolution/variablesources/bundle_uniqueness_test.go deleted file mode 100644 index 6404bbcd3..000000000 --- a/internal/resolution/variablesources/bundle_uniqueness_test.go +++ /dev/null @@ -1,111 +0,0 @@ -package variablesources_test - -import ( - "encoding/json" - "testing" - - "github.com/google/go-cmp/cmp" - "github.com/stretchr/testify/require" - - "github.com/operator-framework/deppy/pkg/deppy" - "github.com/operator-framework/deppy/pkg/deppy/constraint" - "github.com/operator-framework/deppy/pkg/deppy/input" - "github.com/operator-framework/operator-registry/alpha/declcfg" - "github.com/operator-framework/operator-registry/alpha/property" - - "github.com/operator-framework/operator-controller/internal/catalogmetadata" - olmvariables "github.com/operator-framework/operator-controller/internal/resolution/variables" - "github.com/operator-framework/operator-controller/internal/resolution/variablesources" -) - -func TestMakeBundleUniquenessVariables(t *testing.T) { - const fakeCatalogName = "fake-catalog" - channel := catalogmetadata.Channel{Channel: declcfg.Channel{Name: "stable"}} - bundleSet := map[string]*catalogmetadata.Bundle{ - "test-package.v1.0.0": { - Bundle: declcfg.Bundle{ - Name: "test-package.v1.0.0", - Package: "test-package", - Properties: []property.Property{ - {Type: property.TypePackage, Value: json.RawMessage(`{"packageName": "test-package", "version": "1.0.0"}`)}, - {Type: property.TypePackageRequired, Value: json.RawMessage(`{"packageName": "some-package", "versionRange": ">=1.0.0 <2.0.0"}`)}, - }, - }, - CatalogName: fakeCatalogName, - InChannels: []*catalogmetadata.Channel{&channel}, - }, - "test-package.v1.0.1": { - Bundle: declcfg.Bundle{ - Name: "test-package.v1.0.1", - Package: "test-package", - Properties: []property.Property{ - {Type: property.TypePackage, Value: json.RawMessage(`{"packageName": "test-package", "version": "1.0.1"}`)}, - {Type: property.TypePackageRequired, Value: json.RawMessage(`{"packageName": "some-package", "versionRange": ">=1.0.0 <2.0.0"}`)}, - }, - }, - CatalogName: fakeCatalogName, - InChannels: []*catalogmetadata.Channel{&channel}, - }, - - "some-package.v1.0.0": { - Bundle: declcfg.Bundle{ - Name: "some-package.v1.0.0", - Package: "some-package", - Properties: []property.Property{ - {Type: property.TypePackage, Value: json.RawMessage(`{"packageName": "some-package", "version": "1.0.0"}`)}, - }, - }, - CatalogName: fakeCatalogName, - InChannels: []*catalogmetadata.Channel{&channel}, - }, - } - - // Input into the testable function must include more than one bundle - // from the same package to ensure that the function - // enforces uniqueness correctly. - // We also need at least one bundle variable to have a dependency - // on another package. This will help to make sure that the function - // also creates uniqueness variables for dependencies. - bundleVariables := []*olmvariables.BundleVariable{ - olmvariables.NewBundleVariable( - bundleSet["test-package.v1.0.0"], - []*catalogmetadata.Bundle{ - bundleSet["some-package.v1.0.0"], - }, - ), - olmvariables.NewBundleVariable( - bundleSet["test-package.v1.0.1"], - []*catalogmetadata.Bundle{ - bundleSet["some-package.v1.0.0"], - }, - ), - } - - variables := variablesources.MakeBundleUniquenessVariables(bundleVariables) - - // Each package in the input must have own uniqueness variable. - // Each variable expected to have one constraint enforcing at most one - // of the involved bundles to be allowed in the solution - expectedVariables := []*olmvariables.BundleUniquenessVariable{ - { - SimpleVariable: input.NewSimpleVariable( - "test-package package uniqueness", - constraint.AtMost( - 1, - deppy.Identifier("fake-catalog-test-package-test-package.v1.0.0"), - deppy.Identifier("fake-catalog-test-package-test-package.v1.0.1"), - ), - ), - }, - { - SimpleVariable: input.NewSimpleVariable( - "some-package package uniqueness", - constraint.AtMost( - 1, - deppy.Identifier("fake-catalog-some-package-some-package.v1.0.0"), - ), - ), - }, - } - require.Empty(t, cmp.Diff(variables, expectedVariables, cmp.AllowUnexported(input.SimpleVariable{}, constraint.AtMostConstraint{}))) -} diff --git a/internal/resolution/variablesources/fake_object_utils_test.go b/internal/resolution/variablesources/fake_object_utils_test.go deleted file mode 100644 index 765cdc97f..000000000 --- a/internal/resolution/variablesources/fake_object_utils_test.go +++ /dev/null @@ -1,58 +0,0 @@ -package variablesources_test - -import ( - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - "k8s.io/apimachinery/pkg/util/uuid" - "k8s.io/utils/ptr" - - rukpakv1alpha2 "github.com/operator-framework/rukpak/api/v1alpha2" - - ocv1alpha1 "github.com/operator-framework/operator-controller/api/v1alpha1" -) - -func fakeClusterExtension(name, packageName string, upgradeConstraintPolicy ocv1alpha1.UpgradeConstraintPolicy) ocv1alpha1.ClusterExtension { - return ocv1alpha1.ClusterExtension{ - ObjectMeta: metav1.ObjectMeta{ - Name: name, - // We manually set a fake UID here because the code we test - // uses UID to determine ClusterExtension CR which - // owns `BundleDeployment` - UID: uuid.NewUUID(), - }, - Spec: ocv1alpha1.ClusterExtensionSpec{ - PackageName: packageName, - UpgradeConstraintPolicy: upgradeConstraintPolicy, - }, - } -} - -func fakeBundleDeployment(name, bundleImage string, owner *ocv1alpha1.ClusterExtension) rukpakv1alpha2.BundleDeployment { - bd := rukpakv1alpha2.BundleDeployment{ - ObjectMeta: metav1.ObjectMeta{ - Name: name, - }, - Spec: rukpakv1alpha2.BundleDeploymentSpec{ - ProvisionerClassName: "core-rukpak-io-plain", - Source: rukpakv1alpha2.BundleSource{ - Image: &rukpakv1alpha2.ImageSource{ - Ref: bundleImage, - }, - }, - }, - } - - if owner != nil { - bd.SetOwnerReferences([]metav1.OwnerReference{ - { - APIVersion: ocv1alpha1.GroupVersion.String(), - Kind: "ClusterExtension", - Name: owner.Name, - UID: owner.UID, - Controller: ptr.To(true), - BlockOwnerDeletion: ptr.To(true), - }, - }) - } - - return bd -} diff --git a/internal/resolution/variablesources/installed_package.go b/internal/resolution/variablesources/installed_package.go deleted file mode 100644 index 8ba3e6a61..000000000 --- a/internal/resolution/variablesources/installed_package.go +++ /dev/null @@ -1,148 +0,0 @@ -package variablesources - -import ( - "fmt" - "sort" - - mmsemver "github.com/Masterminds/semver/v3" - "k8s.io/apimachinery/pkg/types" - "k8s.io/apimachinery/pkg/util/sets" - - rukpakv1alpha2 "github.com/operator-framework/rukpak/api/v1alpha2" - - ocv1alpha1 "github.com/operator-framework/operator-controller/api/v1alpha1" - "github.com/operator-framework/operator-controller/internal/catalogmetadata" - catalogfilter "github.com/operator-framework/operator-controller/internal/catalogmetadata/filter" - catalogsort "github.com/operator-framework/operator-controller/internal/catalogmetadata/sort" - olmvariables "github.com/operator-framework/operator-controller/internal/resolution/variables" - "github.com/operator-framework/operator-controller/pkg/features" -) - -// MakeInstalledPackageVariables returns variables representing packages -// already installed in the system. -// Meaning that each BundleDeployment managed by operator-controller -// has own variable. -func MakeInstalledPackageVariables( - allBundles []*catalogmetadata.Bundle, - clusterExtensions []ocv1alpha1.ClusterExtension, - bundleDeployments []rukpakv1alpha2.BundleDeployment, -) ([]*olmvariables.InstalledPackageVariable, error) { - var successors successorsFunc = legacySemanticsSuccessors - if features.OperatorControllerFeatureGate.Enabled(features.ForceSemverUpgradeConstraints) { - successors = semverSuccessors - } - - ownerIDToBundleDeployment := mapOwnerIDToBundleDeployment(bundleDeployments) - - result := make([]*olmvariables.InstalledPackageVariable, 0, len(clusterExtensions)) - processed := sets.Set[string]{} - for _, clusterExtension := range clusterExtensions { - if clusterExtension.Spec.UpgradeConstraintPolicy == ocv1alpha1.UpgradeConstraintPolicyIgnore { - continue - } - - bundleDeployment, ok := ownerIDToBundleDeployment[clusterExtension.UID] - if !ok { - // This can happen when an ClusterExtension is requested, - // but not yet installed (e.g. no BundleDeployment created for it) - continue - } - - sourceImage := bundleDeployment.Spec.Source.Image - if sourceImage == nil || sourceImage.Ref == "" { - continue - } - - if processed.Has(sourceImage.Ref) { - continue - } - processed.Insert(sourceImage.Ref) - - bundleImage := sourceImage.Ref - - // find corresponding bundle for the installed content - resultSet := catalogfilter.Filter(allBundles, catalogfilter.And( - catalogfilter.WithPackageName(clusterExtension.Spec.PackageName), - catalogfilter.WithBundleImage(bundleImage), - )) - if len(resultSet) == 0 { - return nil, fmt.Errorf("bundle with image %q for package %q not found in available catalogs but is currently installed via BundleDeployment %q", bundleImage, clusterExtension.Spec.PackageName, bundleDeployment.Name) - } - - sort.SliceStable(resultSet, func(i, j int) bool { - return catalogsort.ByVersion(resultSet[i], resultSet[j]) - }) - installedBundle := resultSet[0] - - upgradeEdges, err := successors(allBundles, installedBundle) - if err != nil { - return nil, err - } - - // you can always upgrade to yourself, i.e. not upgrade - upgradeEdges = append(upgradeEdges, installedBundle) - result = append(result, olmvariables.NewInstalledPackageVariable(installedBundle.Package, upgradeEdges)) - } - - return result, nil -} - -// successorsFunc must return successors of a currently installed bundle -// from a list of all bundles provided to the function. -// Must not return installed bundle as a successor -type successorsFunc func(allBundles []*catalogmetadata.Bundle, installedBundle *catalogmetadata.Bundle) ([]*catalogmetadata.Bundle, error) - -// legacySemanticsSuccessors returns successors based on legacy OLMv0 semantics -// which rely on Replaces, Skips and skipRange. -func legacySemanticsSuccessors(allBundles []*catalogmetadata.Bundle, installedBundle *catalogmetadata.Bundle) ([]*catalogmetadata.Bundle, error) { - // find the bundles that replace, skip, or skipRange the bundle provided - upgradeEdges := catalogfilter.Filter(allBundles, catalogfilter.And( - catalogfilter.WithPackageName(installedBundle.Package), - catalogfilter.LegacySuccessor(installedBundle), - )) - sort.SliceStable(upgradeEdges, func(i, j int) bool { - return catalogsort.ByVersion(upgradeEdges[i], upgradeEdges[j]) - }) - - return upgradeEdges, nil -} - -// semverSuccessors returns successors based on Semver. -// Successors will not include versions outside the major version of the -// installed bundle as major version is intended to indicate breaking changes. -func semverSuccessors(allBundles []*catalogmetadata.Bundle, installedBundle *catalogmetadata.Bundle) ([]*catalogmetadata.Bundle, error) { - currentVersion, err := installedBundle.Version() - if err != nil { - return nil, err - } - - // Based on current version create a caret range comparison constraint - // to allow only minor and patch version as successors and exclude current version. - constraintStr := fmt.Sprintf("^%s, != %s", currentVersion.String(), currentVersion.String()) - wantedVersionRangeConstraint, err := mmsemver.NewConstraint(constraintStr) - if err != nil { - return nil, err - } - - upgradeEdges := catalogfilter.Filter(allBundles, catalogfilter.And( - catalogfilter.WithPackageName(installedBundle.Package), - catalogfilter.InMastermindsSemverRange(wantedVersionRangeConstraint), - )) - sort.SliceStable(upgradeEdges, func(i, j int) bool { - return catalogsort.ByVersion(upgradeEdges[i], upgradeEdges[j]) - }) - - return upgradeEdges, nil -} - -func mapOwnerIDToBundleDeployment(bundleDeployments []rukpakv1alpha2.BundleDeployment) map[types.UID]*rukpakv1alpha2.BundleDeployment { - result := map[types.UID]*rukpakv1alpha2.BundleDeployment{} - - for idx := range bundleDeployments { - for _, ref := range bundleDeployments[idx].OwnerReferences { - result[ref.UID] = &bundleDeployments[idx] - } - } - - return result -} diff --git a/internal/resolution/variablesources/required_package.go b/internal/resolution/variablesources/required_package.go deleted file mode 100644 index 2f23a6062..000000000 --- a/internal/resolution/variablesources/required_package.go +++ /dev/null @@ -1,67 +0,0 @@ -package variablesources - -import ( - "fmt" - "sort" - - mmsemver "github.com/Masterminds/semver/v3" - - ocv1alpha1 "github.com/operator-framework/operator-controller/api/v1alpha1" - "github.com/operator-framework/operator-controller/internal/catalogmetadata" - catalogfilter "github.com/operator-framework/operator-controller/internal/catalogmetadata/filter" - catalogsort "github.com/operator-framework/operator-controller/internal/catalogmetadata/sort" - olmvariables "github.com/operator-framework/operator-controller/internal/resolution/variables" -) - -// MakeRequiredPackageVariables returns a variable which represent -// explicit requirement for a package from an user. -// This is when a user explicitly asks "install this" via ClusterExtension API. -func MakeRequiredPackageVariables(allBundles []*catalogmetadata.Bundle, clusterExtensions []ocv1alpha1.ClusterExtension) ([]*olmvariables.RequiredPackageVariable, error) { - result := make([]*olmvariables.RequiredPackageVariable, 0, len(clusterExtensions)) - - for _, clusterExtension := range clusterExtensions { - packageName := clusterExtension.Spec.PackageName - channelName := clusterExtension.Spec.Channel - versionRange := clusterExtension.Spec.Version - - predicates := []catalogfilter.Predicate[catalogmetadata.Bundle]{ - catalogfilter.WithPackageName(packageName), - } - - if channelName != "" { - predicates = append(predicates, catalogfilter.InChannel(channelName)) - } - - if versionRange != "" { - vr, err := mmsemver.NewConstraint(versionRange) - if err != nil { - return nil, fmt.Errorf("invalid version range %q: %w", versionRange, err) - } - predicates = append(predicates, catalogfilter.InMastermindsSemverRange(vr)) - } - - resultSet := catalogfilter.Filter(allBundles, catalogfilter.And(predicates...)) - if len(resultSet) == 0 { - if versionRange != "" && channelName != "" { - return nil, fmt.Errorf("no package %q matching version %q found in channel %q", packageName, versionRange, channelName) - } - if versionRange != "" { - return nil, fmt.Errorf("no package %q matching version %q found", packageName, versionRange) - } - if channelName != "" { - return nil, fmt.Errorf("no package %q found in channel %q", packageName, channelName) - } - return nil, fmt.Errorf("no package %q found", packageName) - } - sort.SliceStable(resultSet, func(i, j int) bool { - return catalogsort.ByVersion(resultSet[i], resultSet[j]) - }) - sort.SliceStable(resultSet, func(i, j int) bool { - return catalogsort.ByDeprecated(resultSet[i], resultSet[j]) - }) - - result = append(result, olmvariables.NewRequiredPackageVariable(packageName, resultSet)) - } - - return result, nil -} diff --git a/internal/resolution/variablesources/required_package_test.go b/internal/resolution/variablesources/required_package_test.go deleted file mode 100644 index 164f9a411..000000000 --- a/internal/resolution/variablesources/required_package_test.go +++ /dev/null @@ -1,254 +0,0 @@ -package variablesources_test - -import ( - "encoding/json" - "fmt" - "testing" - - "github.com/google/go-cmp/cmp" - "github.com/google/go-cmp/cmp/cmpopts" - "github.com/stretchr/testify/assert" - "github.com/stretchr/testify/require" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - - "github.com/operator-framework/deppy/pkg/deppy/constraint" - "github.com/operator-framework/deppy/pkg/deppy/input" - "github.com/operator-framework/operator-registry/alpha/declcfg" - "github.com/operator-framework/operator-registry/alpha/property" - - ocv1alpha1 "github.com/operator-framework/operator-controller/api/v1alpha1" - "github.com/operator-framework/operator-controller/internal/catalogmetadata" - olmvariables "github.com/operator-framework/operator-controller/internal/resolution/variables" - "github.com/operator-framework/operator-controller/internal/resolution/variablesources" -) - -func TestMakeRequiredPackageVariables(t *testing.T) { - stableChannel := catalogmetadata.Channel{Channel: declcfg.Channel{ - Name: "stable", - }} - betaChannel := catalogmetadata.Channel{Channel: declcfg.Channel{ - Name: "beta", - }} - bundleSet := map[string]*catalogmetadata.Bundle{ - // Bundles which belong to test-package we will be using - // to assert wether the testable function filters out the data - // correctly. - "test-package.v1.0.0": { - Bundle: declcfg.Bundle{ - Name: "test-package.v1.0.0", - Package: "test-package", - Properties: []property.Property{ - {Type: property.TypePackage, Value: json.RawMessage(`{"packageName": "test-package", "version": "1.0.0"}`)}, - }, - }, - InChannels: []*catalogmetadata.Channel{&stableChannel}, - }, - "test-package.v3.0.0": { - Bundle: declcfg.Bundle{ - Name: "test-package.v3.0.0", - Package: "test-package", - Properties: []property.Property{ - {Type: property.TypePackage, Value: json.RawMessage(`{"packageName": "test-package", "version": "3.0.0"}`)}, - }, - }, - InChannels: []*catalogmetadata.Channel{&stableChannel, &betaChannel}, - }, - "test-package.v2.0.0": { - Bundle: declcfg.Bundle{ - Name: "test-package.v2.0.0", - Package: "test-package", - Properties: []property.Property{ - {Type: property.TypePackage, Value: json.RawMessage(`{"packageName": "test-package", "version": "2.0.0"}`)}, - }, - }, - InChannels: []*catalogmetadata.Channel{&stableChannel}, - }, - "test-package.v4.0.0": { - Bundle: declcfg.Bundle{ - Name: "test-package.v4.0.0", - Package: "test-package", - Properties: []property.Property{ - {Type: property.TypePackage, Value: json.RawMessage(`{"packageName": "test-package", "version": "4.0.0"}`)}, - }, - }, - InChannels: []*catalogmetadata.Channel{&stableChannel}, - Deprecations: []declcfg.DeprecationEntry{ - { - Reference: declcfg.PackageScopedReference{ - Schema: declcfg.SchemaBundle, - Name: "test-package.v4.0.0", - }, - Message: "test-package.v4.0.0 has been deprecated", - }, - }, - }, - "test-package.v4.1.0": { - Bundle: declcfg.Bundle{ - Name: "test-package.v4.1.0", - Package: "test-package", - Properties: []property.Property{ - {Type: property.TypePackage, Value: json.RawMessage(`{"packageName": "test-package", "version": "4.1.0"}`)}, - }, - }, - InChannels: []*catalogmetadata.Channel{&stableChannel}, - Deprecations: []declcfg.DeprecationEntry{ - { - Reference: declcfg.PackageScopedReference{ - Schema: declcfg.SchemaBundle, - Name: "test-package.v4.1.0", - }, - Message: "test-package.v4.1.0 has been deprecated", - }, - }, - }, - "test-package.v5.0.0": { - Bundle: declcfg.Bundle{ - Name: "test-package.v5.0.0", - Package: "test-package", - Properties: []property.Property{ - {Type: property.TypePackage, Value: json.RawMessage(`{"packageName": "test-package", "version": "5.0.0"}`)}, - }, - }, - InChannels: []*catalogmetadata.Channel{&stableChannel}, - Deprecations: []declcfg.DeprecationEntry{ - { - Reference: declcfg.PackageScopedReference{ - Schema: declcfg.SchemaBundle, - Name: "test-package.v5.0.0", - }, - Message: "test-package.v5.0.0 has been deprecated", - }, - }, - }, - - // We need at least one bundle from different package - // to make sure that we are filtering it out. - "test-package-2.v1.0.0": { - Bundle: declcfg.Bundle{ - Name: "test-package-2.v1.0.0", - Package: "test-package-2", - Properties: []property.Property{ - {Type: property.TypePackage, Value: json.RawMessage(`{"packageName": "test-package-2", "version": "1.0.0"}`)}, - }, - }, - InChannels: []*catalogmetadata.Channel{&stableChannel}, - }, - } - allBundles := make([]*catalogmetadata.Bundle, 0, len(bundleSet)) - for _, bundle := range bundleSet { - allBundles = append(allBundles, bundle) - } - - fakeClusterExtension := func(packageName, channelName, versionRange string) ocv1alpha1.ClusterExtension { - return ocv1alpha1.ClusterExtension{ - ObjectMeta: metav1.ObjectMeta{ - Name: fmt.Sprintf("op-%s-%s-%s", packageName, channelName, versionRange), - }, - Spec: ocv1alpha1.ClusterExtensionSpec{ - PackageName: packageName, - Version: versionRange, - Channel: channelName, - }, - } - } - - for _, tt := range []struct { - name string - clusterExtensions []ocv1alpha1.ClusterExtension - expectedResult []*olmvariables.RequiredPackageVariable - expectedError string - }{ - { - name: "package name only", - clusterExtensions: []ocv1alpha1.ClusterExtension{ - fakeClusterExtension("test-package", "", ""), - }, - expectedResult: []*olmvariables.RequiredPackageVariable{ - olmvariables.NewRequiredPackageVariable("test-package", []*catalogmetadata.Bundle{ - bundleSet["test-package.v3.0.0"], - bundleSet["test-package.v2.0.0"], - bundleSet["test-package.v1.0.0"], - bundleSet["test-package.v5.0.0"], - bundleSet["test-package.v4.1.0"], - bundleSet["test-package.v4.0.0"], - }), - }, - }, - { - name: "package name and channel", - clusterExtensions: []ocv1alpha1.ClusterExtension{ - fakeClusterExtension("test-package", "beta", ""), - }, - expectedResult: []*olmvariables.RequiredPackageVariable{ - olmvariables.NewRequiredPackageVariable("test-package", []*catalogmetadata.Bundle{ - bundleSet["test-package.v3.0.0"], - }), - }, - }, - { - name: "package name and version range", - clusterExtensions: []ocv1alpha1.ClusterExtension{ - fakeClusterExtension("test-package", "", ">=1.0.0 !=2.0.0 <3.0.0"), - }, - expectedResult: []*olmvariables.RequiredPackageVariable{ - olmvariables.NewRequiredPackageVariable("test-package", []*catalogmetadata.Bundle{ - bundleSet["test-package.v1.0.0"], - }), - }, - }, - { - name: "package name and invalid version range", - clusterExtensions: []ocv1alpha1.ClusterExtension{ - fakeClusterExtension("test-package", "", "not a valid semver"), - }, - expectedError: `invalid version range "not a valid semver"`, - }, - { - name: "not found: package name only", - clusterExtensions: []ocv1alpha1.ClusterExtension{ - fakeClusterExtension("non-existent-test-package", "", ""), - }, - expectedError: `no package "non-existent-test-package" found`, - }, - { - name: "not found: package name and channel", - clusterExtensions: []ocv1alpha1.ClusterExtension{ - fakeClusterExtension("non-existent-test-package", "stable", ""), - }, - expectedError: `no package "non-existent-test-package" found in channel "stable"`, - }, - { - name: "not found: package name and version range", - clusterExtensions: []ocv1alpha1.ClusterExtension{ - fakeClusterExtension("non-existent-test-package", "", "1.0.0"), - }, - expectedError: `no package "non-existent-test-package" matching version "1.0.0" found`, - }, - { - name: "not found: package name with channel and version range", - clusterExtensions: []ocv1alpha1.ClusterExtension{ - fakeClusterExtension("non-existent-test-package", "stable", "1.0.0"), - }, - expectedError: `no package "non-existent-test-package" matching version "1.0.0" found in channel "stable"`, - }, - } { - t.Run(tt.name, func(t *testing.T) { - vars, err := variablesources.MakeRequiredPackageVariables(allBundles, tt.clusterExtensions) - if tt.expectedError == "" { - assert.NoError(t, err) - } else { - assert.ErrorContains(t, err, tt.expectedError) - } - - gocmpopts := []cmp.Option{ - cmpopts.IgnoreUnexported(catalogmetadata.Bundle{}), - cmp.AllowUnexported( - olmvariables.RequiredPackageVariable{}, - input.SimpleVariable{}, - constraint.DependencyConstraint{}, - ), - } - require.Empty(t, cmp.Diff(vars, tt.expectedResult, gocmpopts...)) - }) - } -} diff --git a/internal/version/version.go b/internal/version/version.go new file mode 100644 index 000000000..ef90dffa0 --- /dev/null +++ b/internal/version/version.go @@ -0,0 +1,44 @@ +package version + +import ( + "fmt" + "runtime/debug" +) + +var ( + gitCommit = "unknown" + commitDate = "unknown" + repoState = "unknown" + version = "unknown" + + stateMap = map[string]string{ + "true": "dirty", + "false": "clean", + } +) + +func String() string { + return fmt.Sprintf("version: %q, commit: %q, date: %q, state: %q", version, gitCommit, commitDate, repoState) +} + +func init() { + info, ok := debug.ReadBuildInfo() + if !ok { + return + } + for _, setting := range info.Settings { + switch setting.Key { + case "vcs.revision": + gitCommit = setting.Value + case "vcs.time": + commitDate = setting.Value + case "vcs.modified": + if v, ok := stateMap[setting.Value]; ok { + repoState = v + } + } + } + if version == "unknown" { + version = info.Main.Version + } +} diff --git a/requirements.txt b/requirements.txt index ed79de9d2..3bd0138e3 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1 +1,35 @@ -mkdocs-material==9.5.18 +Babel==2.14.0 +beautifulsoup4==4.12.3 +certifi==2024.2.2 +charset-normalizer==3.3.2 +click==8.1.7 +colorama==0.4.6 +cssselect==1.2.0 +ghp-import==2.1.0 +idna==3.7 +Jinja2==3.1.3 +lxml==5.2.1 +Markdown==3.6 +markdown2==2.4.13 +MarkupSafe==2.1.5 +mergedeep==1.3.4 +mkdocs==1.6.0 +mkdocs-material==9.5.20 +mkdocs-material-extensions==1.3.1 +packaging==24.0 +paginate==0.5.6 +pathspec==0.12.1 +platformdirs==4.2.1 +Pygments==2.17.2 +pymdown-extensions==10.8.1 +pyquery==2.0.0 +python-dateutil==2.9.0.post0 +PyYAML==6.0.1 +pyyaml_env_tag==0.1 +readtime==3.0.0 +regex==2024.4.28 +requests==2.31.0 +six==1.16.0 +soupsieve==2.5 +urllib3==2.2.1 +watchdog==4.0.0 diff --git a/test/e2e/cluster_extension_admission_test.go b/test/e2e/cluster_extension_admission_test.go index cc6b9eaea..b1cca6c3f 100644 --- a/test/e2e/cluster_extension_admission_test.go +++ b/test/e2e/cluster_extension_admission_test.go @@ -9,12 +9,14 @@ import ( "k8s.io/apimachinery/pkg/api/errors" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/types" + "sigs.k8s.io/controller-runtime/pkg/client" ocv1alpha1 "github.com/operator-framework/operator-controller/api/v1alpha1" ) func TestClusterExtensionPackageUniqueness(t *testing.T) { ctx := context.Background() + fieldOwner := client.FieldOwner("operator-controller-e2e") deleteClusterExtension := func(clusterExtension *ocv1alpha1.ClusterExtension) { require.NoError(t, c.Delete(ctx, clusterExtension)) @@ -33,7 +35,8 @@ func TestClusterExtensionPackageUniqueness(t *testing.T) { Name: firstResourceName, }, Spec: ocv1alpha1.ClusterExtensionSpec{ - PackageName: firstResourcePackageName, + PackageName: firstResourcePackageName, + InstallNamespace: "default", }, } require.NoError(t, c.Create(ctx, clusterExtension1)) @@ -45,7 +48,8 @@ func TestClusterExtensionPackageUniqueness(t *testing.T) { GenerateName: "test-extension-", }, Spec: ocv1alpha1.ClusterExtensionSpec{ - PackageName: firstResourcePackageName, + PackageName: firstResourcePackageName, + InstallNamespace: "default", }, } err := c.Create(ctx, clusterExtension2) @@ -57,19 +61,43 @@ func TestClusterExtensionPackageUniqueness(t *testing.T) { GenerateName: "test-extension-", }, Spec: ocv1alpha1.ClusterExtensionSpec{ - PackageName: "package2", + PackageName: "package2", + InstallNamespace: "default", }, } require.NoError(t, c.Create(ctx, clusterExtension2)) defer deleteClusterExtension(clusterExtension2) t.Log("update second resource with package which already exists on the cluster") - clusterExtension2.Spec.PackageName = firstResourcePackageName - err = c.Update(ctx, clusterExtension2) + intent := &ocv1alpha1.ClusterExtension{ + TypeMeta: metav1.TypeMeta{ + APIVersion: ocv1alpha1.GroupVersion.String(), + Kind: "ClusterExtension", + }, + ObjectMeta: metav1.ObjectMeta{ + Name: clusterExtension2.Name, + }, + Spec: ocv1alpha1.ClusterExtensionSpec{ + PackageName: firstResourcePackageName, + InstallNamespace: "default", + }, + } + err = c.Patch(ctx, intent, client.Apply, client.ForceOwnership, fieldOwner) require.ErrorContains(t, err, fmt.Sprintf("Package %q is already installed via ClusterExtension %q", firstResourcePackageName, firstResourceName)) t.Log("update second resource with package which does not exist on the cluster") - require.NoError(t, c.Get(ctx, types.NamespacedName{Name: clusterExtension2.Name}, clusterExtension2)) - clusterExtension2.Spec.PackageName = "package3" - require.NoError(t, c.Update(ctx, clusterExtension2)) + intent = &ocv1alpha1.ClusterExtension{ + TypeMeta: metav1.TypeMeta{ + APIVersion: ocv1alpha1.GroupVersion.String(), + Kind: "ClusterExtension", + }, + ObjectMeta: metav1.ObjectMeta{ + Name: clusterExtension2.Name, + }, + Spec: ocv1alpha1.ClusterExtensionSpec{ + PackageName: "package3", + InstallNamespace: "default", + }, + } + require.NoError(t, c.Patch(ctx, intent, client.Apply, client.ForceOwnership, fieldOwner)) } diff --git a/test/e2e/cluster_extension_install_test.go b/test/e2e/cluster_extension_install_test.go index 1ac60b869..3f2b7df86 100644 --- a/test/e2e/cluster_extension_install_test.go +++ b/test/e2e/cluster_extension_install_test.go @@ -73,7 +73,8 @@ func TestClusterExtensionInstallRegistry(t *testing.T) { defer getArtifactsOutput(t) clusterExtension.Spec = ocv1alpha1.ClusterExtensionSpec{ - PackageName: "prometheus", + PackageName: "prometheus", + InstallNamespace: "default", } t.Log("It resolves the specified package with correct bundle path") t.Log("By creating the ClusterExtension resource") @@ -129,7 +130,8 @@ func TestClusterExtensionInstallPlain(t *testing.T) { defer getArtifactsOutput(t) clusterExtension.Spec = ocv1alpha1.ClusterExtensionSpec{ - PackageName: "plain", + PackageName: "plain", + InstallNamespace: "default", } t.Log("It resolves the specified package with correct bundle path") t.Log("By creating the ClusterExtension resource") @@ -186,7 +188,8 @@ func TestClusterExtensionInstallReResolvesWhenNewCatalog(t *testing.T) { pkgName := "prometheus" clusterExtension.Spec = ocv1alpha1.ClusterExtensionSpec{ - PackageName: pkgName, + PackageName: pkgName, + InstallNamespace: "default", } t.Log("By deleting the catalog first") @@ -247,8 +250,9 @@ func TestClusterExtensionBlockInstallNonSuccessorVersion(t *testing.T) { t.Log("By creating an ClusterExtension at a specified version") clusterExtension.Spec = ocv1alpha1.ClusterExtensionSpec{ - PackageName: "prometheus", - Version: "1.0.0", + PackageName: "prometheus", + Version: "1.0.0", + InstallNamespace: "default", } require.NoError(t, c.Create(context.Background(), clusterExtension)) t.Log("By eventually reporting a successful resolution") @@ -276,8 +280,7 @@ func TestClusterExtensionBlockInstallNonSuccessorVersion(t *testing.T) { return } assert.Equal(ct, ocv1alpha1.ReasonResolutionFailed, cond.Reason) - assert.Contains(ct, cond.Message, "constraints not satisfiable") - assert.Contains(ct, cond.Message, "installed package prometheus requires at least one of test-catalog-prometheus-prometheus-operator.1.0.1, test-catalog-prometheus-prometheus-operator.1.0.0") + assert.Equal(ct, "error upgrading from currently installed version \"1.0.0\": no package \"prometheus\" matching version \"1.2.0\" found", cond.Message) assert.Empty(ct, clusterExtension.Status.ResolvedBundle) }, pollDuration, pollInterval) } @@ -292,8 +295,9 @@ func TestClusterExtensionForceInstallNonSuccessorVersion(t *testing.T) { t.Log("By creating an ClusterExtension at a specified version") clusterExtension.Spec = ocv1alpha1.ClusterExtensionSpec{ - PackageName: "prometheus", - Version: "1.0.0", + PackageName: "prometheus", + Version: "1.0.0", + InstallNamespace: "default", } require.NoError(t, c.Create(context.Background(), clusterExtension)) t.Log("By eventually reporting a successful resolution") @@ -336,8 +340,9 @@ func TestClusterExtensionInstallSuccessorVersion(t *testing.T) { t.Log("By creating an ClusterExtension at a specified version") clusterExtension.Spec = ocv1alpha1.ClusterExtensionSpec{ - PackageName: "prometheus", - Version: "1.0.0", + PackageName: "prometheus", + Version: "1.0.0", + InstallNamespace: "default", } require.NoError(t, c.Create(context.Background(), clusterExtension)) t.Log("By eventually reporting a successful resolution") diff --git a/test/extension-developer-e2e/extension_developer_test.go b/test/extension-developer-e2e/extension_developer_test.go index 325d32c86..cb7b5f7d0 100644 --- a/test/extension-developer-e2e/extension_developer_test.go +++ b/test/extension-developer-e2e/extension_developer_test.go @@ -37,7 +37,8 @@ func TestExtensionDeveloper(t *testing.T) { Name: "plainv0", }, Spec: ocv1alpha1.ClusterExtensionSpec{ - PackageName: os.Getenv("PLAIN_PKG_NAME"), + PackageName: os.Getenv("PLAIN_PKG_NAME"), + InstallNamespace: "default", }, }, { @@ -45,7 +46,8 @@ func TestExtensionDeveloper(t *testing.T) { Name: "registryv1", }, Spec: ocv1alpha1.ClusterExtensionSpec{ - PackageName: os.Getenv("REG_PKG_NAME"), + PackageName: os.Getenv("REG_PKG_NAME"), + InstallNamespace: "default", }, }, } diff --git a/testdata/crds/core.rukpak.io_bundledeployments.yaml b/testdata/crds/core.rukpak.io_bundledeployments.yaml index 0cedcf0c5..c9a7e6f9e 100644 --- a/testdata/crds/core.rukpak.io_bundledeployments.yaml +++ b/testdata/crds/core.rukpak.io_bundledeployments.yaml @@ -50,21 +50,33 @@ spec: More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds type: string metadata: + properties: + name: + maxLength: 52 + type: string type: object spec: description: BundleDeploymentSpec defines the desired state of BundleDeployment properties: config: - description: Config is provisioner specific configurations + description: config is provisioner specific configurations type: object x-kubernetes-preserve-unknown-fields: true + installNamespace: + description: |- + installNamespace is the namespace where the bundle should be installed. However, note that + the bundle may contain resources that are cluster-scoped or that are + installed in a different namespace. This namespace is expected to exist. + maxLength: 63 + pattern: ^[a-z0-9]([-a-z0-9]*[a-z0-9])?$ + type: string provisionerClassName: - description: ProvisionerClassName sets the name of the provisioner + description: provisionerClassName sets the name of the provisioner that should reconcile this BundleDeployment. pattern: ^[a-z0-9]([-a-z0-9]*[a-z0-9])?$ type: string source: - description: Source defines the configuration for the underlying Bundle + description: source defines the configuration for the underlying Bundle content. properties: configMaps: @@ -136,6 +148,13 @@ spec: from the specified repo. Ref is required, and exactly one field within Ref is required. Setting more than one field or zero fields will result in an error. + oneOf: + - required: + - branch + - required: + - commit + - required: + - tag properties: branch: description: |- @@ -221,22 +240,11 @@ spec: required: - type type: object - watchNamespaces: - description: watchNamespaces indicates which namespaces the operator - should watch. - items: - type: string - type: array required: + - installNamespace - provisionerClassName - source type: object - x-kubernetes-validations: - - message: Empty string not accepted if length of watchNamespaces is more - than 1. - rule: '!has(self.watchNamespaces) || size(self.watchNamespaces) <= 1 - || (size(self.watchNamespaces) > 1 && !self.watchNamespaces.exists(e, - e == ''''))' status: description: BundleDeploymentStatus defines the observed state of BundleDeployment properties: