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

Remove obsolete finalizer from KnativeServing resources. #35

Merged
Merged
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
88 changes: 47 additions & 41 deletions pkg/reconciler/knativeserving/knativeserving.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package knativeserving

import (
"context"
"encoding/json"
"fmt"

mf "github.com/manifestival/manifestival"
Expand All @@ -29,6 +30,8 @@ import (
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/sets"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/cache"
servingv1alpha1 "knative.dev/operator/pkg/apis/operator/v1alpha1"
Expand All @@ -43,10 +46,10 @@ import (
)

const (
finalizerName = "delete-knative-serving-manifest"
creationChange = "creation"
editChange = "edit"
deletionChange = "deletion"
oldFinalizerName = "delete-knative-serving-manifest"
creationChange = "creation"
editChange = "edit"
deletionChange = "deletion"
)

var (
Expand Down Expand Up @@ -87,7 +90,22 @@ func (r *Reconciler) FinalizeKind(ctx context.Context, original *servingv1alpha1
if _, ok := r.servings[key]; ok {
delete(r.servings, key)
}
return r.delete(ctx, original)

logger.Info("Deleting resources")
var RBAC = mf.Any(mf.ByKind("Role"), mf.ByKind("ClusterRole"), mf.ByKind("RoleBinding"), mf.ByKind("ClusterRoleBinding"))
if len(r.servings) == 0 {
if err := r.config.Filter(mf.ByKind("Deployment")).Delete(); err != nil {
return err
}
if err := r.config.Filter(mf.NoCRDs, mf.None(RBAC)).Delete(); err != nil {
return err
}
// Delete Roles last, as they may be useful for human operators to clean up.
if err := r.config.Filter(RBAC).Delete(); err != nil {
return err
}
}
return nil
}

// ReconcileKind compares the actual state with the desired, and attempts to
Expand Down Expand Up @@ -135,7 +153,7 @@ func (r *Reconciler) reconcile(ctx context.Context, ks *servingv1alpha1.KnativeS
reqLogger.Infow("Reconciling KnativeServing", "status", ks.Status)

stages := []func(context.Context, *mf.Manifest, *servingv1alpha1.KnativeServing) error{
r.ensureFinalizer,
r.ensureFinalizerRemoval,
r.initStatus,
r.install,
r.checkDeployments,
Expand Down Expand Up @@ -251,47 +269,35 @@ func (r *Reconciler) checkDeployments(ctx context.Context, manifest *mf.Manifest
return nil
}

// ensureFinalizer attaches a "delete manifest" finalizer to the instance
func (r *Reconciler) ensureFinalizer(ctx context.Context, manifest *mf.Manifest, instance *servingv1alpha1.KnativeServing) error {
for _, finalizer := range instance.GetFinalizers() {
if finalizer == finalizerName {
return nil
}
}
instance.SetFinalizers(append(instance.GetFinalizers(), finalizerName))
instance, err := r.knativeServingClientSet.OperatorV1alpha1().KnativeServings(instance.Namespace).Update(instance)
return err
}

// delete all the resources in the release manifest
func (r *Reconciler) delete(ctx context.Context, instance *servingv1alpha1.KnativeServing) error {
logger := logging.FromContext(ctx)
// ensureFinalizerRemoval ensures that the obsolete "delete-knative-serving-manifest" is removed from the resource.
func (r *Reconciler) ensureFinalizerRemoval(_ context.Context, _ *mf.Manifest, instance *servingv1alpha1.KnativeServing) error {
Copy link
Contributor

@houshengbo houshengbo Apr 27, 2020

Choose a reason for hiding this comment

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

If I understand correctly, the comment of this function should be changed into

ensureFinalizerRemovel ensures that Finalizers in the operator Serving CR is consistent with the ones generated by genreconciler.

The oldFinalizerName = "delete-knative-serving-manifest" is removed, if it exists in old operator serving CR.

In the delete function, we do not even check the Finalizers any more.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good catch, changed the comment.

finalizers := sets.NewString(instance.Finalizers...)

if len(instance.GetFinalizers()) == 0 || instance.GetFinalizers()[0] != finalizerName {
if !finalizers.Has(oldFinalizerName) {
// Nothing to do.
return nil
}
logger.Info("Deleting resources")
var RBAC = mf.Any(mf.ByKind("Role"), mf.ByKind("ClusterRole"), mf.ByKind("RoleBinding"), mf.ByKind("ClusterRoleBinding"))
if len(r.servings) == 0 {
if err := r.config.Filter(mf.ByKind("Deployment")).Delete(); err != nil {
return err
}
if err := r.config.Filter(mf.NoCRDs, mf.None(RBAC)).Delete(); err != nil {
return err
}
// Delete Roles last, as they may be useful for human operators to clean up.
if err := r.config.Filter(RBAC).Delete(); err != nil {
return err
}

// Remove the finalizer
finalizers.Delete(oldFinalizerName)

mergePatch := map[string]interface{}{
"metadata": map[string]interface{}{
"finalizers": finalizers.List(),
"resourceVersion": instance.ResourceVersion,
},
}
// The deletionTimestamp might've changed. Fetch the resource again.
refetched, err := r.knativeServingLister.KnativeServings(instance.Namespace).Get(instance.Name)

patch, err := json.Marshal(mergePatch)
if err != nil {
return err
return fmt.Errorf("failed to construct finalizer patch: %w", err)
}
refetched.SetFinalizers(refetched.GetFinalizers()[1:])
_, err = r.knativeServingClientSet.OperatorV1alpha1().KnativeServings(refetched.Namespace).Update(refetched)
return err

patcher := r.knativeServingClientSet.OperatorV1alpha1().KnativeServings(instance.Namespace)
if _, err := patcher.Patch(instance.Name, types.MergePatchType, patch); err != nil {
return fmt.Errorf("failed to patch finalizer away: %w", err)
}
return nil
}

// Delete obsolete resources from previous versions
Expand Down