Skip to content
This repository has been archived by the owner on Aug 12, 2024. It is now read-only.

Commit

Permalink
[fix] Fix issue with cascading deletion of image unpack pods
Browse files Browse the repository at this point in the history
Signed-off-by: Varsha Prasad Narsing <[email protected]>
  • Loading branch information
varshaprasad96 committed Sep 13, 2023
1 parent 0256166 commit 46b089c
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 6 deletions.
5 changes: 4 additions & 1 deletion api/v1alpha2/bundledeployment_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,11 @@ const (
// +kubebuilder:object:root=true
// +kubebuilder:subresource:status
// +kubebuilder:resource:scope=Cluster,shortName={"bd","bds"}
// BundleDeployment is the Schema for the bundledeployments API
// +kubebuilder:printcolumn:name="Install State",type=string,JSONPath=`.status.conditions[?(.type=="Installed")].reason`
// +kubebuilder:printcolumn:name=Age,type=date,JSONPath=`.metadata.creationTimestamp`
// +kubebuilder:storageversion

// BundleDeployment is the Schema for the bundledeployments API
type BundleDeployment struct {
metav1.TypeMeta `json:",inline"`
metav1.ObjectMeta `json:"metadata,omitempty"`
Expand Down
3 changes: 2 additions & 1 deletion api/v1alpha2/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions cmd/core/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ func main() {
dependentSelector := labels.NewSelector().Add(*dependentRequirement)

cfg := ctrl.GetConfigOrDie()
fmt.Println("systemNs:!!!!", systemNamespace)
if systemNamespace == "" {
systemNamespace = util.PodNamespace()
}
Expand All @@ -126,6 +127,7 @@ func main() {
opts.Scheme = scheme
opts.Namespace = systemNamespace
})
fmt.Println("systemNsCluster!!!!!!!", systemNsCluster)
if err != nil {
setupLog.Error(err, "unable to create system namespace cluster")
os.Exit(1)
Expand Down Expand Up @@ -260,6 +262,7 @@ func main() {
// }

if err := v1alpha2bd.SetupWithManager(mgr,
systemNsCluster.GetCache(),
v1alpha2bd.WithUnpacker(defaultUnpacker),
v1alpha2bd.WithValidators(v1alpha2validators.NewDefaultValidator()),
v1alpha2bd.WithDeployer(deployer)); err != nil {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,12 @@ import (
"sync"
"time"

"github.com/go-logr/logr"
"github.com/operator-framework/rukpak/api/v1alpha2"
corev1 "k8s.io/api/core/v1"
"sigs.k8s.io/controller-runtime/pkg/cache"
"sigs.k8s.io/controller-runtime/pkg/client/apiutil"
"sigs.k8s.io/controller-runtime/pkg/reconcile"

v1alpha2deployer "github.com/operator-framework/rukpak/internal/controllers/v1alpha2/deployer"
v1alpha2source "github.com/operator-framework/rukpak/internal/controllers/v1alpha2/source"
Expand All @@ -35,6 +40,7 @@ import (
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/types"
apimacherrors "k8s.io/apimachinery/pkg/util/errors"
utilerrors "k8s.io/apimachinery/pkg/util/errors"
"k8s.io/client-go/tools/record"
Expand All @@ -45,6 +51,7 @@ import (
"sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/manager"
"sigs.k8s.io/controller-runtime/pkg/source"
crsource "sigs.k8s.io/controller-runtime/pkg/source"
)

// BundleDeploymentReconciler reconciles a BundleDeployment object
Expand Down Expand Up @@ -280,7 +287,7 @@ func (b *bundleDeploymentReconciler) validateConfig() error {
return utilerrors.NewAggregate(errs)
}

func SetupWithManager(mgr manager.Manager, opts ...Option) error {
func SetupWithManager(mgr manager.Manager, systemNsCache cache.Cache, opts ...Option) error {
bd := &bundleDeploymentReconciler{
Client: mgr.GetClient(),
dynamicWatchGVKs: map[schema.GroupVersionKind]struct{}{},
Expand All @@ -294,13 +301,52 @@ func SetupWithManager(mgr manager.Manager, opts ...Option) error {
}

controllerName := fmt.Sprintf("controller-bundledeployment.%s", v1alpha2.BundleDeploymentGVK.Version)
l := mgr.GetLogger().WithName(controllerName)
controller, err := ctrl.NewControllerManagedBy(mgr).
Named(controllerName).
For(&v1alpha2.BundleDeployment{}).
Watches(crsource.NewKindWithCache(&corev1.Pod{}, systemNsCache), MapOwnerToBundleDeploymentHandler(context.Background(), mgr.GetClient(), l, &v1alpha2.BundleDeployment{})).
Build(bd)
if err != nil {
return err
}
bd.controller = controller
return nil
}

// MapOwnerToBundleDeploymentHandler is a handler implementation that finds an owner reference in the event object that
// references the provided owner. If a reference for the provided owner is found this handler enqueues a request for that owner to be reconciled.
func MapOwnerToBundleDeploymentHandler(ctx context.Context, cl client.Client, log logr.Logger, owner client.Object) handler.EventHandler {
return handler.EnqueueRequestsFromMapFunc(func(obj client.Object) []reconcile.Request {
ownerGVK, err := apiutil.GVKForObject(owner, cl.Scheme())
if err != nil {
log.Error(err, "map ownee to owner: lookup GVK for owner")
return nil
}
type ownerInfo struct {
key types.NamespacedName
gvk schema.GroupVersionKind
}
var oi *ownerInfo

for _, ref := range obj.GetOwnerReferences() {
gv, err := schema.ParseGroupVersion(ref.APIVersion)
if err != nil {
log.Error(err, fmt.Sprintf("map ownee to owner: parse ownee's owner reference group version %q", ref.APIVersion))
return nil
}
refGVK := gv.WithKind(ref.Kind)
if refGVK == ownerGVK && ref.Controller != nil && *ref.Controller {
oi = &ownerInfo{
key: types.NamespacedName{Name: ref.Name},
gvk: ownerGVK,
}
break
}
}
if oi == nil {
return nil
}
return []reconcile.Request{{NamespacedName: oi.key}}
})
}
2 changes: 1 addition & 1 deletion internal/controllers/v1alpha2/source/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ func (i *Image) getDesiredPodApplyConfig(bdName string, bundleSrc *v1alpha2.Bund
WithOwnerReferences(v1.OwnerReference().
WithName(bdName).
WithKind(v1alpha2.BundleDeploymentKind).
WithAPIVersion(v1alpha2.BundleDeploymentGVK.Version).
WithAPIVersion(v1alpha2.BundleDeploymentGVK.GroupVersion().String()).
WithUID(opts.BundleDeploymentUID).
WithController(true).
WithBlockOwnerDeletion(true),
Expand Down
2 changes: 1 addition & 1 deletion internal/source/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ func (i *Image) getDesiredPodApplyConfig(bundle *rukpakv1alpha1.Bundle) *applyco
WithAPIVersion(bundle.APIVersion).
WithUID(bundle.UID).
WithController(true).
WithBlockOwnerDeletion(true),
WithBlockOwnerDeletion(false),
).
WithSpec(applyconfigurationcorev1.PodSpec().
WithAutomountServiceAccountToken(false).
Expand Down
15 changes: 14 additions & 1 deletion manifests/base/apis/crds/core.rukpak.io_bundledeployments.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,14 @@ spec:
type: object
served: true
storage: false
- name: v1alpha2
- additionalPrinterColumns:
- jsonPath: .status.conditions[?(.type=="Installed")].reason
name: Install State
type: string
- jsonPath: .metadata.creationTimestamp
name: Age
type: date
name: v1alpha2
schema:
openAPIV3Schema:
description: BundleDeployment is the Schema for the bundledeployments API
Expand All @@ -379,6 +386,12 @@ spec:
spec:
description: BundleDeploymentSpec defines the desired state of BundleDeployment
properties:
config:
description: 'Config is provisioner specific configurations TODO:
This should be become deployer specific. Should move to helm deployer
configuration.'
type: object
x-kubernetes-preserve-unknown-fields: true
format:
description: Format refers to the bundle type which is being passed
through the bundle deployment API.
Expand Down

0 comments on commit 46b089c

Please sign in to comment.