From ea82904ad5c72c8f526af8981368c5dfc3fa81ab Mon Sep 17 00:00:00 2001 From: Wen Zhou Date: Mon, 23 Sep 2024 12:43:08 +0200 Subject: [PATCH] feat: pass version into CSV from makefile VERSION - use envstubst to replace version - get the value from env variable similar to the platform one - uninstall will use the version from existing DSCI/DSC - normal flow will use GetRelease() which is mainly from env or fallback to 0.0.0 Signed-off-by: Wen Zhou --- Makefile | 3 +- .../v1/zz_generated.deepcopy.go | 2 +- .../v1/zz_generated.deepcopy.go | 2 +- ...atahub-operator.clusterserviceversion.yaml | 2 + config/manager/kustomization.yaml.in | 13 +++++ main.go | 3 +- pkg/cluster/cluster_config.go | 48 +++++++------------ pkg/cluster/zz_generated.deepcopy.go | 1 - pkg/common/common.go | 35 ++++++++++++++ pkg/upgrade/upgrade.go | 14 ++++-- 10 files changed, 82 insertions(+), 41 deletions(-) diff --git a/Makefile b/Makefile index 53619fd07b3..49331ed2119 100644 --- a/Makefile +++ b/Makefile @@ -208,7 +208,8 @@ prepare: manifests kustomize manager-kustomization .PHONY: manager-kustomization manager-kustomization: config/manager/kustomization.yaml.in cd config/manager \ - && cp -f kustomization.yaml.in kustomization.yaml \ + && export RELEASE_VERSION=$(VERSION) \ + && envsubst < kustomization.yaml.in > kustomization.yaml \ && $(KUSTOMIZE) edit set image controller=$(IMG) .PHONY: install diff --git a/apis/datasciencecluster/v1/zz_generated.deepcopy.go b/apis/datasciencecluster/v1/zz_generated.deepcopy.go index f089d70e9ee..1cb6ce77867 100644 --- a/apis/datasciencecluster/v1/zz_generated.deepcopy.go +++ b/apis/datasciencecluster/v1/zz_generated.deepcopy.go @@ -171,7 +171,7 @@ func (in *DataScienceClusterStatus) DeepCopyInto(out *DataScienceClusterStatus) } } in.Components.DeepCopyInto(&out.Components) - in.Release.DeepCopyInto(&out.Release) + out.Release = in.Release } // DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new DataScienceClusterStatus. diff --git a/apis/dscinitialization/v1/zz_generated.deepcopy.go b/apis/dscinitialization/v1/zz_generated.deepcopy.go index 4f9bb201778..be36439fcc6 100644 --- a/apis/dscinitialization/v1/zz_generated.deepcopy.go +++ b/apis/dscinitialization/v1/zz_generated.deepcopy.go @@ -132,7 +132,7 @@ func (in *DSCInitializationStatus) DeepCopyInto(out *DSCInitializationStatus) { *out = make([]corev1.ObjectReference, len(*in)) copy(*out, *in) } - in.Release.DeepCopyInto(&out.Release) + out.Release = in.Release } // DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new DSCInitializationStatus. diff --git a/bundle/manifests/opendatahub-operator.clusterserviceversion.yaml b/bundle/manifests/opendatahub-operator.clusterserviceversion.yaml index 1e6d56af870..30ddea44e6d 100644 --- a/bundle/manifests/opendatahub-operator.clusterserviceversion.yaml +++ b/bundle/manifests/opendatahub-operator.clusterserviceversion.yaml @@ -1085,6 +1085,8 @@ spec: env: - name: DISABLE_DSC_CONFIG value: "true" + - name: OPERATOR_RELEASE_VERSION ++ value: 2.18.1 - name: OPERATOR_NAMESPACE valueFrom: fieldRef: diff --git a/config/manager/kustomization.yaml.in b/config/manager/kustomization.yaml.in index f3dcf81a77b..bae5d8dee35 100644 --- a/config/manager/kustomization.yaml.in +++ b/config/manager/kustomization.yaml.in @@ -9,3 +9,16 @@ kind: Kustomization images: - name: controller newName: REPLACE_IMAGE + +patches: +- target: + kind: Deployment + name: controller-manager + version: v1 + group: apps + patch: |- + - op: add + path: /spec/template/spec/containers/0/env/1 + value: + name: OPERATOR_RELEASE_VERSION + value: $RELEASE_VERSION \ No newline at end of file diff --git a/main.go b/main.go index 7f9a0b438c9..9cff9654b97 100644 --- a/main.go +++ b/main.go @@ -261,7 +261,8 @@ func main() { //nolint:funlen,maintidx } // get old release version before we create default DSCI CR - oldReleaseVersion, _ := upgrade.GetDeployedRelease(ctx, setupClient) + oldRelease, _ := upgrade.GetDeployedRelease(ctx, setupClient) + oldReleaseVersion := oldRelease.Version // Check if user opted for disabling DSC configuration disableDSCConfig, existDSCConfig := os.LookupEnv("DISABLE_DSC_CONFIG") diff --git a/pkg/cluster/cluster_config.go b/pkg/cluster/cluster_config.go index 13a82632357..2d5894943a9 100644 --- a/pkg/cluster/cluster_config.go +++ b/pkg/cluster/cluster_config.go @@ -7,8 +7,6 @@ import ( "os" "strings" - "github.com/blang/semver/v4" - "github.com/operator-framework/api/pkg/lib/version" ofapiv1alpha1 "github.com/operator-framework/api/pkg/operators/v1alpha1" corev1 "k8s.io/api/core/v1" k8serr "k8s.io/apimachinery/pkg/api/errors" @@ -21,6 +19,13 @@ import ( // +kubebuilder:rbac:groups="config.openshift.io",resources=ingresses,verbs=get +// Release includes information on operator version and platform +// +kubebuilder:object:generate=true +type Release struct { + Name Platform `json:"name,omitempty"` + Version string `json:"version,omitempty"` +} + func GetDomain(ctx context.Context, c client.Client) (string, error) { ingress := &unstructured.Unstructured{} ingress.SetGroupVersionKind(gvk.OpenshiftIngress) @@ -140,46 +145,27 @@ func GetPlatform(ctx context.Context, cli client.Client) (Platform, error) { } } -// Release includes information on operator version and platform -// +kubebuilder:object:generate=true -type Release struct { - Name Platform `json:"name,omitempty"` - Version version.OperatorVersion `json:"version,omitempty"` -} - func GetRelease(ctx context.Context, cli client.Client) (Release, error) { initRelease := Release{ // dummy version set to name "", version 0.0.0 - Version: version.OperatorVersion{ - Version: semver.Version{}, - }, + Name: OpenDataHub, + Version: "0.0.0", } - // Set platform - platform, err := DetectPlatform(ctx, cli) - if err != nil { - return initRelease, err - } - initRelease.Name = platform // For unit-tests if os.Getenv("CI") == "true" { return initRelease, nil } - // Set Version - // Get watchNamespace - operatorNamespace, err := GetOperatorNamespace() - if err != nil { - // unit test does not have k8s file or env var set, return default - return initRelease, err - } - csv, err := GetClusterServiceVersion(ctx, cli, operatorNamespace) - if k8serr.IsNotFound(err) { - // hide not found, return default - return initRelease, nil - } + + // Set platform + platform, err := GetPlatform(ctx, cli) if err != nil { return initRelease, err } - initRelease.Version = csv.Spec.Version + initRelease.Name = platform + + // Set version + initRelease.Version = os.Getenv("OPERATOR_RELEASE_VERSION") + return initRelease, nil } diff --git a/pkg/cluster/zz_generated.deepcopy.go b/pkg/cluster/zz_generated.deepcopy.go index 0bc3e0f557d..e9f5071a9f0 100644 --- a/pkg/cluster/zz_generated.deepcopy.go +++ b/pkg/cluster/zz_generated.deepcopy.go @@ -25,7 +25,6 @@ import () // DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. func (in *Release) DeepCopyInto(out *Release) { *out = *in - in.Version.DeepCopyInto(&out.Version) } // DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new Release. diff --git a/pkg/common/common.go b/pkg/common/common.go index 4fb00641649..7e4c15abbeb 100644 --- a/pkg/common/common.go +++ b/pkg/common/common.go @@ -21,9 +21,11 @@ package common import ( "crypto/sha256" b64 "encoding/base64" + "errors" "fmt" "os" "regexp" + "strconv" "strings" ) @@ -116,3 +118,36 @@ func GetMonitoringData(data string) (string, error) { return encodedData, nil } + +// ParseVersion parses a version string in the format "major.minor.patch" and returns the individual parts. +// It is up to the caller to discard parts that are not needed. +func ParseVersion(version string) (int, int, int, error) { + var major, minor, patch int + parts := strings.Split(version, ".") + if len(parts) < 3 { + return major, minor, patch, errors.New("invalid version format, required format is major.minor.patch") + } + + parseEach := func(part string, name string) (int, error) { + value, err := strconv.Atoi(part) + if err != nil { + return 0, fmt.Errorf("invalid %s part in version: %w", name, err) + } + return value, nil + } + + major, err := parseEach(parts[0], "major") + if err != nil { + return major, minor, patch, err + } + minor, err = parseEach(parts[1], "minor") + if err != nil { + return major, minor, patch, err + } + patch, err = parseEach(parts[2], "patch") + if err != nil { + return major, minor, patch, err + } + + return major, minor, patch, nil +} diff --git a/pkg/upgrade/upgrade.go b/pkg/upgrade/upgrade.go index 29233462852..33a2d07b03a 100644 --- a/pkg/upgrade/upgrade.go +++ b/pkg/upgrade/upgrade.go @@ -43,6 +43,7 @@ import ( "github.com/opendatahub-io/opendatahub-operator/v2/components/workbenches" "github.com/opendatahub-io/opendatahub-operator/v2/pkg/cluster" "github.com/opendatahub-io/opendatahub-operator/v2/pkg/cluster/gvk" + "github.com/opendatahub-io/opendatahub-operator/v2/pkg/common" ) type ResourceSpec struct { @@ -212,7 +213,7 @@ func CleanupExistingResource(ctx context.Context, cli client.Client, platform cluster.Platform, dscApplicationsNamespace, dscMonitoringNamespace string, - oldReleaseVersion cluster.Release, + oldReleaseVersion string, ) error { var multiErr *multierror.Error // Special Handling of cleanup of deprecated model monitoring stack @@ -413,7 +414,7 @@ func removOdhApplicationsCR(ctx context.Context, cli client.Client, gvk schema.G // upgradODCCR handles different cases: // 1. unset ownerreference for CR odh-dashboard-config // 2. flip TrustyAI BiasMetrics to false (.spec.dashboardConfig.disableBiasMetrics) if it is lower release version than version. -func upgradeODCCR(ctx context.Context, cli client.Client, instanceName string, applicationNS string, release cluster.Release) error { +func upgradeODCCR(ctx context.Context, cli client.Client, instanceName string, applicationNS string, releaseVersion string) error { crd := &apiextv1.CustomResourceDefinition{} if err := cli.Get(ctx, client.ObjectKey{Name: "odhdashboardconfigs.opendatahub.io"}, crd); err != nil { return client.IgnoreNotFound(err) @@ -430,7 +431,7 @@ func upgradeODCCR(ctx context.Context, cli client.Client, instanceName string, a if err := unsetOwnerReference(ctx, cli, instanceName, odhObject); err != nil { return err } - return updateODCBiasMetrics(ctx, cli, instanceName, release, odhObject) + return updateODCBiasMetrics(ctx, cli, instanceName, releaseVersion, odhObject) } func unsetOwnerReference(ctx context.Context, cli client.Client, instanceName string, odhObject *unstructured.Unstructured) error { @@ -444,10 +445,13 @@ func unsetOwnerReference(ctx context.Context, cli client.Client, instanceName st return nil } -func updateODCBiasMetrics(ctx context.Context, cli client.Client, instanceName string, oldRelease cluster.Release, odhObject *unstructured.Unstructured) error { +func updateODCBiasMetrics(ctx context.Context, cli client.Client, instanceName string, oldRelease string, odhObject *unstructured.Unstructured) error { // "from version" as oldRelease, if return "0.0.0" meaning running on 2.10- release/dummy CI build // if oldRelease is lower than 2.14.0(e.g 2.13.x-a), flip TrustyAI BiasMetrics to false (even the field did not exist) - if oldRelease.Version.Minor < 14 { + // if oldRelease is lower than 2.14.0 (e.g 2.13.x-a), flip TrustyAI BiasMetrics to false (even the field did not exist) + + major, minor, _, _ := common.ParseVersion(oldRelease) + if minor < 14 && major == 2 { ctrl.Log.Info("Upgrade force BiasMetrics to false due to from release < 2.14.0") // flip TrustyAI BiasMetrics to false (.spec.dashboardConfig.disableBiasMetrics) disableBiasMetricsValue := []byte(`{"spec": {"dashboardConfig": {"disableBiasMetrics": false}}}`)