Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
7 changes: 7 additions & 0 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -307,12 +307,19 @@ func main() {
}
}

pluginNamespace, err := util.GetOperatorNamespace()
if err != nil {
setupLog.Error(err, "Error retrieving operator's running namespace")
os.Exit(1)
}

if util.IsOpenShiftCluster() {
if err = (&controllers.ReconcileGitopsService{
Client: client,
Scheme: mgr.GetScheme(),
DisableDefaultInstall: strings.ToLower(os.Getenv(common.DisableDefaultInstallEnvVar)) == "true",
CentralTLSProfile: profile,
PluginNamespace: pluginNamespace,
}).SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "GitopsService")
os.Exit(1)
Expand Down
67 changes: 51 additions & 16 deletions controllers/consoleplugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ func getPluginPodSpec(crImagePullPolicy corev1.PullPolicy) corev1.PodSpec {
return podSpec
}

func pluginDeployment(crImagePullPolicy corev1.PullPolicy) *appsv1.Deployment {
func pluginDeployment(namespace string, crImagePullPolicy corev1.PullPolicy) *appsv1.Deployment {
podSpec := getPluginPodSpec(crImagePullPolicy)
template := corev1.PodTemplateSpec{
ObjectMeta: metav1.ObjectMeta{
Expand All @@ -153,13 +153,13 @@ func pluginDeployment(crImagePullPolicy corev1.PullPolicy) *appsv1.Deployment {
return &appsv1.Deployment{
ObjectMeta: metav1.ObjectMeta{
Name: gitopsPluginName,
Namespace: serviceNamespace,
Namespace: namespace,
Labels: map[string]string{
kubeAppLabelApp: gitopsPluginName,
kubeAppLabelComponent: gitopsPluginName,
kubeAppLabelInstance: gitopsPluginName,
kubeAppLabelPartOf: gitopsPluginName,
kubeAppLabelRuntimeNamespace: serviceNamespace,
kubeAppLabelRuntimeNamespace: namespace,
},
},
Spec: appsv1.DeploymentSpec{
Expand All @@ -174,7 +174,7 @@ func pluginDeployment(crImagePullPolicy corev1.PullPolicy) *appsv1.Deployment {
}
}

func consolePlugin() *consolev1.ConsolePlugin {
func consolePlugin(namespace string) *consolev1.ConsolePlugin {
return &consolev1.ConsolePlugin{
ObjectMeta: metav1.ObjectMeta{
Name: gitopsPluginName,
Expand All @@ -185,7 +185,7 @@ func consolePlugin() *consolev1.ConsolePlugin {
Type: consolev1.Service,
Service: &consolev1.ConsolePluginService{
Name: gitopsPluginName,
Namespace: serviceNamespace,
Namespace: namespace,
Port: servicePort,
BasePath: "/",
},
Expand All @@ -197,7 +197,7 @@ func consolePlugin() *consolev1.ConsolePlugin {
}
}

func pluginService() *corev1.Service {
func pluginService(namespace string) *corev1.Service {
spec := corev1.ServiceSpec{
Selector: map[string]string{
kubeAppLabelApp: gitopsPluginName,
Expand All @@ -213,7 +213,7 @@ func pluginService() *corev1.Service {
svc := &corev1.Service{
ObjectMeta: metav1.ObjectMeta{
Name: gitopsPluginName,
Namespace: serviceNamespace,
Namespace: namespace,
Labels: map[string]string{
kubeAppLabelApp: gitopsPluginName,
kubeAppLabelComponent: gitopsPluginName,
Expand Down Expand Up @@ -275,13 +275,13 @@ ServerRoot "/etc/httpd"
}

// pluginConfigMap creates the ConfigMap with dynamic httpd.conf
func (r *ReconcileGitopsService) pluginConfigMap() *corev1.ConfigMap {
func (r *ReconcileGitopsService) pluginConfigMap(namespace string) *corev1.ConfigMap {
httpdConfig := r.buildHttpdConfig()

cm := &corev1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{
Name: httpdConfigMapName,
Namespace: serviceNamespace,
Namespace: namespace,
Labels: map[string]string{
kubeAppLabelApp: gitopsPluginName,
kubeAppLabelPartOf: gitopsPluginName,
Expand Down Expand Up @@ -365,7 +365,7 @@ func sortTolerations(tolerations []corev1.Toleration) []corev1.Toleration {

func (r *ReconcileGitopsService) reconcileDeployment(cr *pipelinesv1alpha1.GitopsService, request reconcile.Request, newPluginConfigMap *corev1.ConfigMap) (reconcile.Result, error) {
reqLogger := logs.WithValues("Request.Namespace", request.Namespace, "Request.Name", request.Name)
newPluginDeployment := pluginDeployment(cr.Spec.ImagePullPolicy)
newPluginDeployment := pluginDeployment(r.PluginNamespace, cr.Spec.ImagePullPolicy)

if err := controllerutil.SetControllerReference(cr, newPluginDeployment, r.Scheme); err != nil {
return reconcile.Result{}, err
Expand Down Expand Up @@ -450,7 +450,7 @@ func (r *ReconcileGitopsService) reconcileDeployment(cr *pipelinesv1alpha1.Gitop

func (r *ReconcileGitopsService) reconcileService(instance *pipelinesv1alpha1.GitopsService, request reconcile.Request) (reconcile.Result, error) {
reqLogger := logs.WithValues("Request.Namespace", request.Namespace, "Request.Name", request.Name)
pluginServiceRef := pluginService()
pluginServiceRef := pluginService(r.PluginNamespace)
Comment thread
coderabbitai[bot] marked this conversation as resolved.
// Set GitopsService instance as the owner and controller
if err := controllerutil.SetControllerReference(instance, pluginServiceRef, r.Scheme); err != nil {
return reconcile.Result{}, err
Expand Down Expand Up @@ -482,15 +482,15 @@ func (r *ReconcileGitopsService) reconcileService(instance *pipelinesv1alpha1.Gi
existingServiceRef.Labels = pluginServiceRef.Labels
existingServiceRef.Spec.Selector = pluginServiceRef.Spec.Selector
existingServiceRef.Spec.Ports = pluginServiceRef.Spec.Ports
return reconcile.Result{}, r.Client.Update(context.TODO(), pluginServiceRef)
return reconcile.Result{}, r.Client.Update(context.TODO(), existingServiceRef)
}
}
return reconcile.Result{}, nil
}

func (r *ReconcileGitopsService) reconcileConsolePlugin(instance *pipelinesv1alpha1.GitopsService, request reconcile.Request) (reconcile.Result, error) {
reqLogger := logs.WithValues("Request.Namespace", request.Namespace, "Request.Name", request.Name)
newConsolePlugin := consolePlugin()
newConsolePlugin := consolePlugin(r.PluginNamespace)
Comment thread
coderabbitai[bot] marked this conversation as resolved.

if err := controllerutil.SetControllerReference(instance, newConsolePlugin, r.Scheme); err != nil {
return reconcile.Result{}, err
Expand All @@ -501,7 +501,7 @@ func (r *ReconcileGitopsService) reconcileConsolePlugin(instance *pipelinesv1alp
if err := r.Client.Get(context.TODO(), types.NamespacedName{Name: gitopsPluginName},
existingPlugin); err != nil {
if errors.IsNotFound(err) {
reqLogger.Info("Creating a new ConsolePlugin", "Namespace", serviceNamespace, "Name", gitopsPluginName)
reqLogger.Info("Creating a new ConsolePlugin", "Namespace", r.PluginNamespace, "Name", gitopsPluginName)
err = r.Client.Create(context.TODO(), newConsolePlugin)
if err != nil {
reqLogger.Error(err, "Error creating a new console plugin",
Expand All @@ -519,7 +519,7 @@ func (r *ReconcileGitopsService) reconcileConsolePlugin(instance *pipelinesv1alp
reqLogger.Info("Reconciling Console Plugin", "Namespace", existingPlugin.Namespace, "Name", existingPlugin.Name)
existingPlugin.Spec.DisplayName = newConsolePlugin.Spec.DisplayName
existingPlugin.Spec.Backend.Service = newConsolePlugin.Spec.Backend.Service
return reconcile.Result{}, r.Client.Update(context.TODO(), newConsolePlugin)
return reconcile.Result{}, r.Client.Update(context.TODO(), existingPlugin)
}
}
return reconcile.Result{}, nil
Expand Down Expand Up @@ -577,6 +577,41 @@ func (r *ReconcileGitopsService) reconcileConfigMap(instance *pipelinesv1alpha1.
return reconcile.Result{}, nil
}

// cleanupOldPluginResources removes plugin resources from the old namespace (openshift-gitops) after they have been moved to the operator namespace, since owner references on the cluster-scoped GitopsService CR won't trigger garbage collection.
func (r *ReconcileGitopsService) cleanupOldPluginResources(ctx context.Context) error {
if r.PluginNamespace == serviceNamespace {
return nil
}

reqLogger := logs.WithValues()

oldDeploy := &appsv1.Deployment{}
if err := r.Client.Get(ctx, types.NamespacedName{Name: gitopsPluginName, Namespace: serviceNamespace}, oldDeploy); err == nil {
reqLogger.Info("Cleaning up old plugin Deployment from previous namespace", "Namespace", serviceNamespace)
if err := r.Client.Delete(ctx, oldDeploy); err != nil && !errors.IsNotFound(err) {
return fmt.Errorf("failed to delete old plugin Deployment from namespace %s: %w", serviceNamespace, err)
}
}

oldSvc := &corev1.Service{}
if err := r.Client.Get(ctx, types.NamespacedName{Name: gitopsPluginName, Namespace: serviceNamespace}, oldSvc); err == nil {
reqLogger.Info("Cleaning up old plugin Service from previous namespace", "Namespace", serviceNamespace)
if err := r.Client.Delete(ctx, oldSvc); err != nil && !errors.IsNotFound(err) {
return fmt.Errorf("failed to delete old plugin Service from namespace %s: %w", serviceNamespace, err)
}
}

oldCM := &corev1.ConfigMap{}
if err := r.Client.Get(ctx, types.NamespacedName{Name: httpdConfigMapName, Namespace: serviceNamespace}, oldCM); err == nil {
reqLogger.Info("Cleaning up old plugin ConfigMap from previous namespace", "Namespace", serviceNamespace)
if err := r.Client.Delete(ctx, oldCM); err != nil && !errors.IsNotFound(err) {
return fmt.Errorf("failed to delete old plugin ConfigMap from namespace %s: %w", serviceNamespace, err)
}
}

return nil
}

// is this func the reconciler enty point to reconcile the current plugin state?
func (r *ReconcileGitopsService) reconcilePlugin(instance *pipelinesv1alpha1.GitopsService, request reconcile.Request) (reconcile.Result, error) {
reqLogger := logs.WithValues("Request.Namespace", request.Namespace, "Request.Name", request.Name)
Expand All @@ -586,7 +621,7 @@ func (r *ReconcileGitopsService) reconcilePlugin(instance *pipelinesv1alpha1.Git
}

// Generate ConfigMap once
newPluginConfigMap := r.pluginConfigMap()
newPluginConfigMap := r.pluginConfigMap(r.PluginNamespace)

if result, err := r.reconcileService(instance, request); err != nil {
return result, err
Expand Down
Loading
Loading