Skip to content

Commit

Permalink
THREESCALE-11395 Fixed APIManager to fail when watched secret is missing
Browse files Browse the repository at this point in the history
  • Loading branch information
carlkyrillos committed Dec 3, 2024
1 parent 1f3285e commit 55b3bb3
Showing 1 changed file with 46 additions and 5 deletions.
51 changes: 46 additions & 5 deletions controllers/apps/apimanager_status_reconciler.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,28 @@ package controllers
import (
"context"
"fmt"
"sort"
"strings"

appsv1alpha1 "github.com/3scale/3scale-operator/apis/apps/v1alpha1"
subController "github.com/3scale/3scale-operator/controllers/subscription"
"github.com/3scale/3scale-operator/pkg/3scale/amp/component"
"github.com/3scale/3scale-operator/pkg/apispkg/common"
"github.com/3scale/3scale-operator/pkg/helper"
"github.com/3scale/3scale-operator/pkg/reconcilers"
"github.com/3scale/3scale-operator/version"

"github.com/RHsyseng/operator-utils/pkg/olm"
"github.com/go-logr/logr"
routev1 "github.com/openshift/api/route/v1"

k8sappsv1 "k8s.io/api/apps/v1"
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
"sort"
)

type APIManagerStatusReconciler struct {
Expand Down Expand Up @@ -90,7 +94,10 @@ func (s *APIManagerStatusReconciler) calculateStatus() (*appsv1alpha1.APIManager

newStatus.Conditions = s.apimanagerResource.Status.Conditions.Copy()

availableCondition, err := s.apimanagerAvailableCondition(deployments)
// Check if any of the watched secrets are missing
watchedSecretsExist, watchedSecretsMessage := s.watchedSecretsExist(s.apimanagerResource)

availableCondition, err := s.apimanagerAvailableCondition(deployments, watchedSecretsExist, watchedSecretsMessage)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -188,7 +195,7 @@ func (s *APIManagerStatusReconciler) existingDeployments() ([]k8sappsv1.Deployme
return deployments, nil
}

func (s *APIManagerStatusReconciler) apimanagerAvailableCondition(existingDeployments []k8sappsv1.Deployment) (common.Condition, error) {
func (s *APIManagerStatusReconciler) apimanagerAvailableCondition(existingDeployments []k8sappsv1.Deployment, watchedSecretsExist bool, missingSecretsMessage string) (common.Condition, error) {
deploymentsAvailable := s.deploymentsAvailable(existingDeployments)

defaultRoutesReady, err := s.defaultRoutesReady()
Expand All @@ -201,11 +208,16 @@ func (s *APIManagerStatusReconciler) apimanagerAvailableCondition(existingDeploy
Status: v1.ConditionFalse,
}

s.logger.V(1).Info("Status apimanagerAvailableCondition", "deploymentsAvailable", deploymentsAvailable, "defaultRoutesReady", defaultRoutesReady)
if deploymentsAvailable && defaultRoutesReady {
s.logger.V(1).Info("Status apimanagerAvailableCondition", "deploymentsAvailable", deploymentsAvailable, "defaultRoutesReady", defaultRoutesReady, "watchedSecretsExist", watchedSecretsExist)
if deploymentsAvailable && defaultRoutesReady && watchedSecretsExist {
newAvailableCondition.Status = v1.ConditionTrue
}

if !watchedSecretsExist {
newAvailableCondition.Message = missingSecretsMessage
newAvailableCondition.Reason = "MissingWatchedSecrets"
}

return newAvailableCondition, nil
}

Expand Down Expand Up @@ -417,3 +429,32 @@ func (s *APIManagerStatusReconciler) reconcilePreflightsStatus(conditions *commo

return nil
}

func (s *APIManagerStatusReconciler) watchedSecretsExist(cr *appsv1alpha1.APIManager) (bool, string) {
secretsToCheck := cr.Get3scaleSecretRefs()
if len(secretsToCheck) == 0 {
// Return because there are no watched secrets to check
return true, ""
}

allWatchedSecretsExist := true
watchedSecretsMessage := ""
var missingSecretNames []string

for _, secretRef := range secretsToCheck {
secret := &v1.Secret{}
secretKey := client.ObjectKey{Name: secretRef.Name, Namespace: cr.Namespace}
err := s.Client().Get(s.Context(), secretKey, secret)
if err != nil {
allWatchedSecretsExist = false
missingSecretNames = append(missingSecretNames, secretRef.Name)
}
}

// If there are watched secrets that can't be found, add the warning condition
if len(missingSecretNames) > 0 {
watchedSecretsMessage = fmt.Sprintf("The following secret(s) could not be found: %s", strings.Join(missingSecretNames, ", "))
}

return allWatchedSecretsExist, watchedSecretsMessage
}

0 comments on commit 55b3bb3

Please sign in to comment.