Skip to content

Bug 1932626: Gracefully handle service unavailable errors from kube-apiserver #2024

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

Merged
Merged
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
4 changes: 2 additions & 2 deletions pkg/controller/operators/olm/apiservices.go
Original file line number Diff line number Diff line change
Expand Up @@ -239,8 +239,8 @@ func (a *Operator) areAPIServicesAvailable(csv *v1alpha1.ClusterServiceVersion)
return false, nil
}

if err := a.isGVKRegistered(desc.Group, desc.Version, desc.Kind); err != nil {
return false, nil
if ok, err := a.isGVKRegistered(desc.Group, desc.Version, desc.Kind); !ok || err != nil {
return false, err
}
}

Expand Down
37 changes: 34 additions & 3 deletions pkg/controller/operators/olm/operator.go
Original file line number Diff line number Diff line change
Expand Up @@ -1565,15 +1565,21 @@ func (a *Operator) transitionCSVState(in v1alpha1.ClusterServiceVersion) (out *v
out.SetPhaseWithEvent(v1alpha1.CSVPhasePending, v1alpha1.CSVReasonNeedsReinstall, "calculated deployment install is bad", now, a.recorder)
return
}
if installErr := a.updateInstallStatus(out, installer, strategy, v1alpha1.CSVPhaseInstalling, v1alpha1.CSVReasonWaiting); installErr == nil {
logger.WithField("strategy", out.Spec.InstallStrategy.StrategyName).Infof("install strategy successful")
} else {
if installErr := a.updateInstallStatus(out, installer, strategy, v1alpha1.CSVPhaseInstalling, v1alpha1.CSVReasonWaiting); installErr != nil {
// Re-sync if kube-apiserver was unavailable
if k8serrors.IsServiceUnavailable(installErr) {
logger.WithError(installErr).Info("could not update install status")
syncError = installErr
return
}
// Set phase to failed if it's been a long time since the last transition (5 minutes)
if out.Status.LastTransitionTime != nil && a.now().Sub(out.Status.LastTransitionTime.Time) >= 5*time.Minute {
logger.Warn("install timed out")
out.SetPhaseWithEvent(v1alpha1.CSVPhaseFailed, v1alpha1.CSVReasonInstallCheckFailed, fmt.Sprintf("install timeout"), now, a.recorder)
return
}
}
logger.WithField("strategy", out.Spec.InstallStrategy.StrategyName).Infof("install strategy successful")

case v1alpha1.CSVPhaseSucceeded:
// Check if the current CSV is being replaced, return with replacing status if so
Expand Down Expand Up @@ -1622,6 +1628,12 @@ func (a *Operator) transitionCSVState(in v1alpha1.ClusterServiceVersion) (out *v
return
}
if installErr := a.updateInstallStatus(out, installer, strategy, v1alpha1.CSVPhaseFailed, v1alpha1.CSVReasonComponentUnhealthy); installErr != nil {
// Re-sync if kube-apiserver was unavailable
if k8serrors.IsServiceUnavailable(installErr) {
logger.WithError(installErr).Info("could not update install status")
syncError = installErr
return
}
logger.WithField("strategy", out.Spec.InstallStrategy.StrategyName).Warnf("unhealthy component: %s", installErr)
return
}
Expand Down Expand Up @@ -1704,6 +1716,12 @@ func (a *Operator) transitionCSVState(in v1alpha1.ClusterServiceVersion) (out *v
return
}
if installErr := a.updateInstallStatus(out, installer, strategy, v1alpha1.CSVPhasePending, v1alpha1.CSVReasonNeedsReinstall); installErr != nil {
// Re-sync if kube-apiserver was unavailable
if k8serrors.IsServiceUnavailable(installErr) {
logger.WithError(installErr).Info("could not update install status")
syncError = installErr
return
}
logger.WithField("strategy", out.Spec.InstallStrategy.StrategyName).Warnf("needs reinstall: %s", installErr)
}

Expand Down Expand Up @@ -1782,6 +1800,10 @@ func (a *Operator) updateInstallStatus(csv *v1alpha1.ClusterServiceVersion, inst
return nil
}

if err := findFirstError(k8serrors.IsServiceUnavailable, strategyErr, apiServiceErr, webhookErr); err != nil {
return err
}

// installcheck determined we can't progress (e.g. deployment failed to come up in time)
if install.IsErrorUnrecoverable(strategyErr) {
csv.SetPhaseWithEventIfChanged(v1alpha1.CSVPhaseFailed, v1alpha1.CSVReasonInstallCheckFailed, fmt.Sprintf("install failed: %s", strategyErr), now, a.recorder)
Expand Down Expand Up @@ -1829,6 +1851,15 @@ func (a *Operator) updateInstallStatus(csv *v1alpha1.ClusterServiceVersion, inst
return nil
}

func findFirstError(f func(error) bool, errs ...error) error {
for _, err := range errs {
if f(err) {
return err
}
}
return nil
}

// parseStrategiesAndUpdateStatus returns a StrategyInstaller and a Strategy for a CSV if it can, else it sets a status on the CSV and returns
func (a *Operator) parseStrategiesAndUpdateStatus(csv *v1alpha1.ClusterServiceVersion) (install.StrategyInstaller, install.Strategy) {
strategy, err := a.resolver.UnmarshalStrategy(csv.Spec.InstallStrategy)
Expand Down
13 changes: 6 additions & 7 deletions pkg/controller/operators/olm/requirements.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

"github.com/operator-framework/api/pkg/operators/v1alpha1"
olmErrors "github.com/operator-framework/operator-lifecycle-manager/pkg/controller/errors"
"github.com/operator-framework/operator-lifecycle-manager/pkg/controller/install"
"github.com/operator-framework/operator-lifecycle-manager/pkg/lib/ownerutil"
)
Expand Down Expand Up @@ -156,7 +155,7 @@ func (a *Operator) requirementStatus(strategyDetailsDeployment *v1alpha1.Strateg
}

// Check if GVK exists
if err := a.isGVKRegistered(r.Group, r.Version, r.Kind); err != nil {
if ok, err := a.isGVKRegistered(r.Group, r.Version, r.Kind); !ok || err != nil {
status.Status = "NotPresent"
met = false
statuses = append(statuses, status)
Expand Down Expand Up @@ -219,7 +218,7 @@ func (a *Operator) requirementStatus(strategyDetailsDeployment *v1alpha1.Strateg
Name: name,
}

if err := a.isGVKRegistered(r.Group, r.Version, r.Kind); err != nil {
if ok, err := a.isGVKRegistered(r.Group, r.Version, r.Kind); !ok || err != nil {
status.Status = v1alpha1.RequirementStatusReasonNotPresent
status.Message = "Native API does not exist"
met = false
Expand Down Expand Up @@ -397,7 +396,7 @@ func (a *Operator) requirementAndPermissionStatus(csv *v1alpha1.ClusterServiceVe
return met, statuses, nil
}

func (a *Operator) isGVKRegistered(group, version, kind string) error {
func (a *Operator) isGVKRegistered(group, version, kind string) (bool, error) {
logger := a.logger.WithFields(logrus.Fields{
"group": group,
"version": version,
Expand All @@ -408,15 +407,15 @@ func (a *Operator) isGVKRegistered(group, version, kind string) error {
resources, err := a.opClient.KubernetesInterface().Discovery().ServerResourcesForGroupVersion(gv.String())
if err != nil {
logger.WithField("err", err).Info("could not query for GVK in api discovery")
return err
return false, err
}

for _, r := range resources.APIResources {
if r.Kind == kind {
return nil
return true, nil
}
}

logger.Info("couldn't find GVK in api discovery")
return olmErrors.GroupVersionKindNotFoundError{group, version, kind}
return false, nil
Copy link
Member Author

@joelanford joelanford Mar 1, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I could not find any usage of isGVKRegistered where the actual content of the error was used, so I changed the function signature to separate the found/not found question from any errors that may have occurred trying to arrive at the answer.

If necessary, any caller of isGVKRegistered could trivially re-construct the previous behavior with:

func (a *Operator) oldIsGVKRegistered(group, version, kind string) error {
	ok, err := a.isGVKRegistered(group, version, kind)
	if err != nil {
		return err
	}
	if !ok {
		return olmErrors.GroupVersionKindNotFoundError{group, version, kind}
	}
	return nil
}

}