-
Notifications
You must be signed in to change notification settings - Fork 32
feat: SKR Webhook & Watcher Cert Setup Deletion #2906
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
Open
lindnerby
wants to merge
14
commits into
kyma-project:feat/kyma-deletion-service
Choose a base branch
from
lindnerby:kyma-deletiom-skr-webhook
base: feat/kyma-deletion-service
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
fa12c7d
wrap skrwebhookmanager into usecase
lindnerby 62f89a9
stash
lindnerby c80b44a
remove-webhook usecase
lindnerby 68448e2
add secret deletion
lindnerby ac066ed
extend repo and tests
lindnerby 6ac0def
Merge branch 'feat/kyma-deletion-service' into kyma-deletiom-skr-webhook
lindnerby 663f10d
split usecase
lindnerby 280fe6b
Add exists func to cert repots
lindnerby 1b9f8ff
reuse interface
lindnerby d9a31e9
tests for new methods
lindnerby 979cf42
Merge branch 'feat/kyma-deletion-service' into kyma-deletiom-skr-webhook
lindnerby ecd0be6
apply fixes from review
lindnerby a608698
Merge branch 'feat/kyma-deletion-service' into kyma-deletiom-skr-webhook
lindnerby f0c2bcc
Merge branch 'feat/kyma-deletion-service' into kyma-deletiom-skr-webhook
lindnerby File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,188 @@ | ||
| package webhook | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
|
|
||
| "golang.org/x/sync/errgroup" | ||
| admissionregistrationv1 "k8s.io/api/admissionregistration/v1" | ||
| apierrors "k8s.io/apimachinery/pkg/api/errors" | ||
| apimetav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
| "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" | ||
| "k8s.io/apimachinery/pkg/apis/meta/v1beta1" | ||
| "k8s.io/apimachinery/pkg/types" | ||
| "sigs.k8s.io/controller-runtime/pkg/client" | ||
|
|
||
| "github.com/kyma-project/lifecycle-manager/internal/errors" | ||
| skrwebhookresources "github.com/kyma-project/lifecycle-manager/internal/service/watcher/resources" | ||
| "github.com/kyma-project/lifecycle-manager/pkg/util" | ||
| ) | ||
|
|
||
| type SkrClientCache interface { | ||
| Get(key client.ObjectKey) client.Client | ||
| } | ||
|
|
||
| //// resourceRef contains the minimal information needed to identify and delete a resource. | ||
| //type resourceRef struct { | ||
| // Name string | ||
| // APIVersion string | ||
| // Kind string | ||
| //} | ||
|
|
||
| type ResourceRepository struct { | ||
| resources []v1beta1.PartialObjectMetadata | ||
| skrClientCache SkrClientCache | ||
| remoteSyncNamespace string | ||
| } | ||
|
|
||
| func NewResourceRepository( | ||
| skrClientCache SkrClientCache, | ||
| remoteSyncNamespace string, | ||
| baseResources []*unstructured.Unstructured, | ||
| ) *ResourceRepository { | ||
| resources := make([]v1beta1.PartialObjectMetadata, 0, len(baseResources)+2) | ||
| for _, res := range baseResources { | ||
| meta := v1beta1.PartialObjectMetadata{ | ||
| TypeMeta: apimetav1.TypeMeta{ | ||
| Kind: res.GetKind(), | ||
| APIVersion: res.GetAPIVersion(), | ||
| }, | ||
| ObjectMeta: apimetav1.ObjectMeta{ | ||
| Name: res.GetName(), | ||
| Namespace: remoteSyncNamespace, | ||
| }, | ||
| } | ||
| resources = append(resources, meta) | ||
| } | ||
|
|
||
| // Add generated resources that are created dynamically from SkrWebhookManifestManager (not read from resources.yaml) | ||
| resources = append(resources, | ||
| v1beta1.PartialObjectMetadata{ | ||
| TypeMeta: apimetav1.TypeMeta{ | ||
| Kind: "ValidatingWebhookConfiguration", | ||
| APIVersion: admissionregistrationv1.SchemeGroupVersion.String(), | ||
| }, | ||
| ObjectMeta: apimetav1.ObjectMeta{ | ||
| Name: skrwebhookresources.SkrResourceName, | ||
| Namespace: remoteSyncNamespace, | ||
| }, | ||
| }, | ||
| v1beta1.PartialObjectMetadata{ | ||
| TypeMeta: apimetav1.TypeMeta{ | ||
| Kind: "Secret", | ||
| APIVersion: "v1", | ||
| }, | ||
| ObjectMeta: apimetav1.ObjectMeta{ | ||
| Name: skrwebhookresources.SkrTLSName, | ||
| Namespace: remoteSyncNamespace, | ||
| }, | ||
| }, | ||
| ) | ||
|
|
||
| return &ResourceRepository{ | ||
| resources: resources, | ||
| skrClientCache: skrClientCache, | ||
| remoteSyncNamespace: remoteSyncNamespace, | ||
| } | ||
| } | ||
|
|
||
| // ResourcesExist checks if any of the skr-webhook resources exist in the SKR cluster for the given Kyma. | ||
| func (r *ResourceRepository) ResourcesExist(kymaName string) (bool, error) { | ||
| skrClient, err := r.getSkrClient(types.NamespacedName{ | ||
| Name: kymaName, | ||
| Namespace: r.remoteSyncNamespace, | ||
| }) | ||
| if err != nil { | ||
| return false, err | ||
| } | ||
|
|
||
| ctx, cancel := context.WithCancel(context.Background()) | ||
| defer cancel() | ||
|
|
||
| errGrp, grpCtx := errgroup.WithContext(ctx) | ||
| resourceExists := make(chan bool, 1) | ||
|
|
||
| for resIdx := range r.resources { | ||
| errGrp.Go(func() error { | ||
| select { | ||
| case <-grpCtx.Done(): | ||
| // Short-circuit if context is cancelled (resource already found) | ||
| return nil | ||
| default: | ||
| } | ||
|
|
||
| ref := r.resources[resIdx] | ||
| err := skrClient.Get(grpCtx, client.ObjectKeyFromObject(&ref), &unstructured.Unstructured{}) | ||
| if err != nil { | ||
| if apierrors.IsNotFound(err) { | ||
| // Resource does not exist, continue checking others | ||
| return nil | ||
| } | ||
| return fmt.Errorf("failed to check resource %s: %w", ref.Name, err) | ||
| } | ||
| select { | ||
| case resourceExists <- true: | ||
| cancel() | ||
| default: | ||
| } | ||
| return nil | ||
| }) | ||
| } | ||
|
|
||
| if err := errGrp.Wait(); err != nil { | ||
| return false, err | ||
| } | ||
|
|
||
| select { | ||
| case <-resourceExists: | ||
| return true, nil | ||
| default: | ||
| return false, nil | ||
| } | ||
| } | ||
|
|
||
| // DeleteWebhookResources deletes all skr-webhook resources from the SKR cluster for the given Kyma. | ||
| func (r *ResourceRepository) DeleteWebhookResources(ctx context.Context, kymaName string) error { | ||
| skrClient, err := r.getSkrClient(types.NamespacedName{ | ||
| Name: kymaName, | ||
| Namespace: r.remoteSyncNamespace, | ||
| }) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| errGrp, grpCtx := errgroup.WithContext(ctx) | ||
|
|
||
| for resIdx := range r.resources { | ||
| errGrp.Go(func() error { | ||
| ref := r.resources[resIdx] | ||
| //resource := &unstructured.Unstructured{} | ||
| //resource.SetGroupVersionKind(schema.FromAPIVersionAndKind(ref.APIVersion, ref.Kind)) | ||
| //resource.SetName(ref.Name) | ||
| //resource.SetNamespace(r.remoteSyncNamespace) | ||
| err := skrClient.Delete(grpCtx, &ref) | ||
| if client.IgnoreNotFound(err) != nil { | ||
| return fmt.Errorf("failed to delete resource %s: %w", ref.Name, err) | ||
| } | ||
| return nil | ||
| }) | ||
| } | ||
|
|
||
| if err := errGrp.Wait(); err != nil && !util.IsNotFound(err) { | ||
| return fmt.Errorf("failed to delete webhook resources: %w", err) | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| func (r *ResourceRepository) getSkrClient(kymaName types.NamespacedName) (client.Client, error) { | ||
| skrClient := r.skrClientCache.Get(kymaName) | ||
|
|
||
| if skrClient == nil { | ||
| // TODO if multiple Repositories are using this error generation, consider creating a shared error instance | ||
| // or even provide this get functionality with error on the skrClientCache itself | ||
| return nil, fmt.Errorf("%w: Kyma %s", errors.ErrSkrClientNotFound, kymaName.String()) | ||
| } | ||
|
|
||
| return skrClient, nil | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add the existing context to the
ResourcesExistfunc signature instaed of creating a new background context?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't want to pollute the signature of this function with
ctxandcancel.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we don't have a cancel from outside anyway. So would only be the ctx we receive from the Reconcile func, and we add the cancel within the func