Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: get platform calls #1251

Merged
merged 2 commits into from
Sep 20, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
1 change: 1 addition & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ func main() { //nolint:funlen,maintidx
setupLog.Error(err, "error getting platform")
os.Exit(1)
}
setupLog.Info("running on", "platform", platform)

secretCache := createSecretCacheConfig(platform)
deploymentCache := createDeploymentCacheConfig(platform)
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
Loading