Skip to content

Commit

Permalink
feat: pass version into CSV from makefile VERSION
Browse files Browse the repository at this point in the history
- 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 <[email protected]>
  • Loading branch information
zdtsw committed Sep 23, 2024
1 parent bb4b99c commit ea82904
Show file tree
Hide file tree
Showing 10 changed files with 82 additions and 41 deletions.
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion apis/datasciencecluster/v1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion apis/dscinitialization/v1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
13 changes: 13 additions & 0 deletions config/manager/kustomization.yaml.in
Original file line number Diff line number Diff line change
Expand Up @@ -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
3 changes: 2 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
48 changes: 17 additions & 31 deletions pkg/cluster/cluster_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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)
Expand Down Expand Up @@ -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
}
1 change: 0 additions & 1 deletion pkg/cluster/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

35 changes: 35 additions & 0 deletions pkg/common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,11 @@ package common
import (
"crypto/sha256"
b64 "encoding/base64"
"errors"
"fmt"
"os"
"regexp"
"strconv"
"strings"
)

Expand Down Expand Up @@ -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
}
14 changes: 9 additions & 5 deletions pkg/upgrade/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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 {
Expand All @@ -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}}}`)
Expand Down

0 comments on commit ea82904

Please sign in to comment.