Skip to content

Commit

Permalink
feat: pass platform from env variable and fall back to use old logic
Browse files Browse the repository at this point in the history
- introduce new env var ODH_PLATFORM_TYPE
- rename old function to DetectPlatform
- create new GetPlatform to check env first or fall to call DetectPlatform
- move old call cluster.GetPlatform, either pass as parameter or out of subcomponent reconcile

Signed-off-by: Wen Zhou <[email protected]>
  • Loading branch information
zdtsw committed Sep 19, 2024
1 parent c87dc1c commit f02c23b
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1131,6 +1131,8 @@ spec:
fieldPath: metadata.namespace
- name: DEFAULT_MANIFESTS_PATH
value: /opt/manifests
- name: ODH_PLATFORM_TYPE
value: OpenDataHub
image: REPLACE_IMAGE:latest
imagePullPolicy: Always
livenessProbe:
Expand Down
2 changes: 2 additions & 0 deletions config/manager/manager.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ spec:
fieldPath: metadata.namespace
- name: DEFAULT_MANIFESTS_PATH
value: /opt/manifests
- name: ODH_PLATFORM_TYPE
value: OpenDataHub
args:
- --leader-elect
- --operator-name=opendatahub
Expand Down
21 changes: 11 additions & 10 deletions controllers/datasciencecluster/datasciencecluster_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,13 @@ const (
func (r *DataScienceClusterReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) { //nolint:maintidx,gocyclo
r.Log.Info("Reconciling DataScienceCluster resources", "Request.Name", req.Name)

// Get platform
platform, err := cluster.GetPlatform(ctx, r.Client)
if err != nil {
r.Log.Error(err, "Failed to determine platform")
return ctrl.Result{}, err
}

// Get information on version
currentOperatorReleaseVersion, err := cluster.GetRelease(ctx, r.Client)
if err != nil {
Expand All @@ -103,7 +110,7 @@ func (r *DataScienceClusterReconciler) Reconcile(ctx context.Context, req ctrl.R
// For additional cleanup logic use operatorUninstall function.
// Return and don't requeue
if upgrade.HasDeleteConfigMap(ctx, r.Client) {
if uninstallErr := upgrade.OperatorUninstall(ctx, r.Client); uninstallErr != nil {
if uninstallErr := upgrade.OperatorUninstall(ctx, r.Client, platform); uninstallErr != nil {
return ctrl.Result{}, fmt.Errorf("error while operator uninstall: %w", uninstallErr)
}
}
Expand Down Expand Up @@ -241,7 +248,7 @@ func (r *DataScienceClusterReconciler) Reconcile(ctx context.Context, req ctrl.R
var componentErrors *multierror.Error

for _, component := range allComponents {
if instance, err = r.reconcileSubComponent(ctx, instance, component); err != nil {
if instance, err = r.reconcileSubComponent(ctx, instance, platform, component); err != nil {
componentErrors = multierror.Append(componentErrors, err)
}
}
Expand Down Expand Up @@ -285,7 +292,7 @@ func (r *DataScienceClusterReconciler) Reconcile(ctx context.Context, req ctrl.R
}

func (r *DataScienceClusterReconciler) reconcileSubComponent(ctx context.Context, instance *dscv1.DataScienceCluster,
component components.ComponentInterface,
platform cluster.Platform, component components.ComponentInterface,
) (*dscv1.DataScienceCluster, error) {
componentName := component.GetComponentName()

Expand All @@ -308,13 +315,7 @@ func (r *DataScienceClusterReconciler) reconcileSubComponent(ctx context.Context
}
}
// Reconcile component
// Get platform
platform, err := cluster.GetPlatform(ctx, r.Client)
if err != nil {
r.Log.Error(err, "Failed to determine platform")
return instance, err
}
err = component.ReconcileComponent(ctx, r.Client, r.Log, instance, r.DataScienceCluster.DSCISpec, platform, installedComponentValue)
err := component.ReconcileComponent(ctx, r.Client, r.Log, instance, r.DataScienceCluster.DSCISpec, platform, installedComponentValue)

// TODO: replace this hack with a full refactor of component status in the future

Expand Down
18 changes: 9 additions & 9 deletions controllers/dscinitialization/dscinitialization_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,9 +153,17 @@ func (r *DSCInitializationReconciler) Reconcile(ctx context.Context, req ctrl.Re
}
}

// Get platform
platform, err := cluster.GetPlatform(ctx, r.Client)
if err != nil {
r.Log.Error(err, "Failed to determine platform")

return reconcile.Result{}, err
}

// Check namespace is not exist, then create
namespace := instance.Spec.ApplicationsNamespace
err = r.createOdhNamespace(ctx, instance, namespace)
err = r.createOdhNamespace(ctx, instance, namespace, platform)
if err != nil {
// no need to log error as it was already logged in createOdhNamespace
return reconcile.Result{}, err
Expand All @@ -167,14 +175,6 @@ func (r *DSCInitializationReconciler) Reconcile(ctx context.Context, req ctrl.Re
}
managementStateChangeTrustedCA = false

// Get platform
platform, err := cluster.GetPlatform(ctx, r.Client)
if err != nil {
r.Log.Error(err, "Failed to determine platform (managed vs self-managed)")

return reconcile.Result{}, err
}

switch req.Name {
case "prometheus": // prometheus configmap
if instance.Spec.Monitoring.ManagementState == operatorv1.Managed && platform == cluster.ManagedRhods {
Expand Down
14 changes: 5 additions & 9 deletions controllers/dscinitialization/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ var (
// - ConfigMap 'odh-common-config'
// - Network Policies 'opendatahub' that allow traffic between the ODH namespaces
// - RoleBinding 'opendatahub'.
func (r *DSCInitializationReconciler) createOdhNamespace(ctx context.Context, dscInit *dsciv1.DSCInitialization, name string) error {
func (r *DSCInitializationReconciler) createOdhNamespace(ctx context.Context, dscInit *dsciv1.DSCInitialization, name string, platform cluster.Platform) error {
// Expected application namespace for the given name
desiredNamespace := &corev1.Namespace{
ObjectMeta: metav1.ObjectMeta{
Expand Down Expand Up @@ -118,7 +118,7 @@ func (r *DSCInitializationReconciler) createOdhNamespace(ctx context.Context, ds
}

// Create default NetworkPolicy for the namespace
err = r.reconcileDefaultNetworkPolicy(ctx, name, dscInit)
err = r.reconcileDefaultNetworkPolicy(ctx, name, dscInit, platform)
if err != nil {
r.Log.Error(err, "error reconciling network policy ", "name", name)
return err
Expand Down Expand Up @@ -190,14 +190,10 @@ func (r *DSCInitializationReconciler) createDefaultRoleBinding(ctx context.Conte
return nil
}

func (r *DSCInitializationReconciler) reconcileDefaultNetworkPolicy(ctx context.Context, name string, dscInit *dsciv1.DSCInitialization) error {
platform, err := cluster.GetPlatform(ctx, r.Client)
if err != nil {
return err
}
func (r *DSCInitializationReconciler) reconcileDefaultNetworkPolicy(ctx context.Context, name string, dscInit *dsciv1.DSCInitialization, platform cluster.Platform) error {
if platform == cluster.ManagedRhods || platform == cluster.SelfManagedRhods {
// Deploy networkpolicy for operator namespace
err = deploy.DeployManifestsFromPath(ctx, r.Client, dscInit, networkpolicyPath+"/operator", "redhat-ods-operator", "networkpolicy", true)
err := deploy.DeployManifestsFromPath(ctx, r.Client, dscInit, networkpolicyPath+"/operator", "redhat-ods-operator", "networkpolicy", true)
if err != nil {
r.Log.Error(err, "error to set networkpolicy in operator namespace", "path", networkpolicyPath)
return err
Expand Down Expand Up @@ -287,7 +283,7 @@ func (r *DSCInitializationReconciler) reconcileDefaultNetworkPolicy(ctx context.
// Create NetworkPolicy if it doesn't exist
foundNetworkPolicy := &networkingv1.NetworkPolicy{}
justCreated := false
err = r.Client.Get(ctx, client.ObjectKey{
err := r.Client.Get(ctx, client.ObjectKey{
Name: name,
Namespace: name,
}, foundNetworkPolicy)
Expand Down
2 changes: 2 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,12 +143,14 @@ func main() { //nolint:funlen,maintidx
setupLog.Error(err, "error getting client for setup")
os.Exit(1)
}

// Get operator platform
platform, err := cluster.GetPlatform(ctx, setupClient)
if err != nil {
setupLog.Error(err, "error getting platform")
os.Exit(1)
}
setupLog.Info("running on", "platform", platform)

secretCache := createSecretCacheConfig(platform)
deploymentCache := createDeploymentCacheConfig(platform)
Expand Down
18 changes: 16 additions & 2 deletions pkg/cluster/cluster_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ func detectManagedRHODS(ctx context.Context, cli client.Client) (Platform, error
return ManagedRhods, nil
}

func GetPlatform(ctx context.Context, cli client.Client) (Platform, error) {
func DetectPlatform(ctx context.Context, cli client.Client) (Platform, error) {
// First check if its addon installation to return 'ManagedRhods, nil'
if platform, err := detectManagedRHODS(ctx, cli); err != nil {
return Unknown, err
Expand All @@ -126,6 +126,20 @@ func GetPlatform(ctx context.Context, cli client.Client) (Platform, error) {
return detectSelfManaged(ctx, cli)
}

func GetPlatform(ctx context.Context, cli client.Client) (Platform, error) {
switch os.Getenv("ODH_PLATFORM_TYPE") {
case "OpenDataHub", "":
return OpenDataHub, nil
case "ManagedRHOAI":
return ManagedRhods, nil
case "SelfManagedRHOAI":
return SelfManagedRhods, nil
default:
// fall back to detect platform if ODH_PLATFORM_TYPE env is not provided
return DetectPlatform(ctx, cli)
}
}

// Release includes information on operator version and platform
// +kubebuilder:object:generate=true
type Release struct {
Expand All @@ -141,7 +155,7 @@ func GetRelease(ctx context.Context, cli client.Client) (Release, error) {
},
}
// Set platform
platform, err := GetPlatform(ctx, cli)
platform, err := DetectPlatform(ctx, cli)
if err != nil {
return initRelease, err
}
Expand Down
7 changes: 1 addition & 6 deletions pkg/upgrade/uninstallation.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,7 @@ const (

// OperatorUninstall deletes all the externally generated resources.
// This includes DSCI, namespace created by operator (but not workbench or MR's), subscription and CSV.
func OperatorUninstall(ctx context.Context, cli client.Client) error {
platform, err := cluster.GetPlatform(ctx, cli)
if err != nil {
return err
}

func OperatorUninstall(ctx context.Context, cli client.Client, platform cluster.Platform) error {
if err := removeDSCInitialization(ctx, cli); err != nil {
return err
}
Expand Down

0 comments on commit f02c23b

Please sign in to comment.