Skip to content

Commit

Permalink
trying to use controllerutil.SetControllerReference
Browse files Browse the repository at this point in the history
Signed-off-by: schristoff <[email protected]>
  • Loading branch information
schristoff authored and troy0820 committed Aug 25, 2023
1 parent ecb945c commit 14f4498
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 74 deletions.
44 changes: 13 additions & 31 deletions controllers/agentconfig_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import (
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/utils/ptr"

ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/builder"
Expand Down Expand Up @@ -145,6 +144,7 @@ func (r *AgentConfigReconciler) Reconcile(ctx context.Context, req ctrl.Request)
if created {
log.V(Log4Debug).Info("Created new temporary persistent volume claim.", "name", pvc.Name)
}

// Use porter to finish reconciling the agent config
err = r.applyAgentConfig(ctx, log, pvc, agentCfg)
if err != nil {
Expand Down Expand Up @@ -208,16 +208,6 @@ func (r *AgentConfigReconciler) createEmptyPluginVolume(ctx context.Context, log
Namespace: agentCfg.Namespace,
Labels: labels,
Annotations: agentCfg.GetPluginsPVCNameAnnotation(),
OwnerReferences: []metav1.OwnerReference{
{ // I'm not using controllerutil.SetControllerReference because I can't track down why that throws a panic when running our tests
APIVersion: agentCfg.APIVersion,
Kind: agentCfg.Kind,
Name: agentCfg.Name,
UID: agentCfg.UID,
Controller: ptr.To(true),
BlockOwnerDeletion: ptr.To(true),
},
},
},
Spec: corev1.PersistentVolumeClaimSpec{
AccessModes: []corev1.PersistentVolumeAccessMode{corev1.ReadWriteOnce},
Expand All @@ -231,6 +221,9 @@ func (r *AgentConfigReconciler) createEmptyPluginVolume(ctx context.Context, log
if storageClassName != "" {
pvc.Spec.StorageClassName = &storageClassName
}
if err := controllerutil.SetControllerReference(agentCfg, pvc, r.Scheme); err != nil {
return nil, false, errors.Wrap(err, "error attaching owner reference to agent volume (pvc)")
}

if err := r.Create(ctx, pvc); err != nil {
return nil, false, errors.Wrap(err, "error creating the agent volume (pvc)")
Expand Down Expand Up @@ -276,16 +269,6 @@ func (r *AgentConfigReconciler) createAgentAction(ctx context.Context, log logr.
GenerateName: agentCfg.Name + "-",
Labels: labels,
Annotations: agentCfg.Annotations,
OwnerReferences: []metav1.OwnerReference{
{ // I'm not using controllerutil.SetControllerReference because I can't track down why that throws a panic when running our tests
APIVersion: agentCfg.APIVersion,
Kind: agentCfg.Kind,
Name: agentCfg.Name,
UID: agentCfg.UID,
Controller: ptr.To(true),
BlockOwnerDeletion: ptr.To(true),
},
},
},
Spec: porterv1.AgentActionSpec{
AgentConfig: agentCfgName,
Expand All @@ -302,6 +285,11 @@ func (r *AgentConfigReconciler) createAgentAction(ctx context.Context, log logr.
return nil, errors.Wrap(err, "error creating the porter agent action")
}

if err := controllerutil.SetControllerReference(agentCfg, action, r.Scheme); err != nil {
return nil, errors.Wrap(err, "error attaching owner reference"+
"while creating porter agent action")
}

log.V(Log4Debug).Info("Created porter agent action", "name", action.Name)
return action, nil
}
Expand Down Expand Up @@ -390,16 +378,6 @@ func (r *AgentConfigReconciler) createHashPVC(ctx context.Context, log logr.Logg
Name: agentCfg.GetPluginsPVCName(),
Namespace: agentCfg.Namespace,
Labels: labels,
OwnerReferences: []metav1.OwnerReference{
{ // I'm not using controllerutil.SetControllerReference because I can't track down why that throws a panic when running our tests
APIVersion: agentCfg.APIVersion,
Kind: agentCfg.Kind,
Name: agentCfg.Name,
UID: agentCfg.UID,
Controller: ptr.To(true),
BlockOwnerDeletion: ptr.To(true),
},
},
},
Spec: corev1.PersistentVolumeClaimSpec{
AccessModes: []corev1.PersistentVolumeAccessMode{corev1.ReadOnlyMany},
Expand All @@ -415,6 +393,10 @@ func (r *AgentConfigReconciler) createHashPVC(ctx context.Context, log logr.Logg
pvc.Spec.StorageClassName = &storageClassName
}

if err := controllerutil.SetControllerReference(agentCfg, pvc, r.Scheme); err != nil {
return nil, errors.Wrap(err, "error attaching owner reference to agent volume (pvc)")
}

if err := r.Create(ctx, pvc); err != nil {
return nil, errors.Wrap(err, "error creating the agent volume (pvc)")
}
Expand Down
10 changes: 0 additions & 10 deletions controllers/agentconfig_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -758,16 +758,6 @@ func TestAgentConfigReconciler_createAgentAction(t *testing.T) {
Namespace: agentCfg.Namespace,
Labels: labels,
Annotations: wrapper.GetPluginsPVCNameAnnotation(),
OwnerReferences: []metav1.OwnerReference{
{ // I'm not using controllerutil.SetControllerReference because I can't track down why that throws a panic when running our tests
APIVersion: agentCfg.APIVersion,
Kind: agentCfg.Kind,
Name: agentCfg.Name,
UID: agentCfg.UID,
Controller: ptr.To(true),
BlockOwnerDeletion: ptr.To(true),
},
},
},
Spec: corev1.PersistentVolumeClaimSpec{
AccessModes: []corev1.PersistentVolumeAccessMode{corev1.ReadWriteOnce},
Expand Down
16 changes: 5 additions & 11 deletions controllers/credentialset_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/utils/ptr"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/builder"
"sigs.k8s.io/controller-runtime/pkg/client"
Expand Down Expand Up @@ -200,16 +199,6 @@ func newAgentAction(cs *porterv1.CredentialSet) *porterv1.AgentAction {
GenerateName: cs.Name + "-",
Labels: labels,
Annotations: cs.Annotations,
OwnerReferences: []metav1.OwnerReference{
{ // I'm not using controllerutil.SetControllerReference because I can't track down why that throws a panic when running our tests
APIVersion: cs.APIVersion,
Kind: cs.Kind,
Name: cs.Name,
UID: cs.UID,
Controller: ptr.To(true),
BlockOwnerDeletion: ptr.To(true),
},
},
},
Spec: porterv1.AgentActionSpec{
AgentConfig: cs.Spec.AgentConfig,
Expand All @@ -236,6 +225,11 @@ func (r *CredentialSetReconciler) createAgentAction(ctx context.Context, log log
action.Spec.Files = map[string][]byte{"credentials.yaml": credSetResourceB}
}

if err := controllerutil.SetControllerReference(cs, action, r.Scheme); err != nil {
return nil, errors.Wrap(err, "error attaching owner reference to porter "+
"credential set agent action")
}

if err := r.Create(ctx, action); err != nil {
return nil, errors.Wrap(err, "error creating the porter credential set agent action")
}
Expand Down
16 changes: 5 additions & 11 deletions controllers/installation_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ import (
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/utils/ptr"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/builder"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
)

const (
Expand Down Expand Up @@ -212,16 +212,6 @@ func (r *InstallationReconciler) createAgentAction(ctx context.Context, log logr
GenerateName: inst.Name + "-",
Labels: labels,
Annotations: inst.Annotations,
OwnerReferences: []metav1.OwnerReference{
{ // I'm not using controllerutil.SetControllerReference because I can't track down why that throws a panic when running our tests
APIVersion: inst.APIVersion,
Kind: inst.Kind,
Name: inst.Name,
UID: inst.UID,
Controller: ptr.To(true),
BlockOwnerDeletion: ptr.To(true),
},
},
},
Spec: porterv1.AgentActionSpec{
AgentConfig: inst.Spec.AgentConfig,
Expand All @@ -232,6 +222,10 @@ func (r *InstallationReconciler) createAgentAction(ctx context.Context, log logr
},
}

if err := controllerutil.SetControllerReference(inst, action, r.Scheme); err != nil {
return nil, errors.Wrap(err, "error attaching owner reference to porter agent action")
}

if err := r.Create(ctx, action); err != nil {
return nil, errors.Wrap(err, "error creating the porter agent action")
}
Expand Down
16 changes: 5 additions & 11 deletions controllers/parameterset_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/utils/ptr"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/builder"
"sigs.k8s.io/controller-runtime/pkg/client"
Expand Down Expand Up @@ -201,6 +200,11 @@ func (r *ParameterSetReconciler) createAgentAction(ctx context.Context, log logr
action.Spec.Files = map[string][]byte{"parameters.yaml": paramSetResourceB}
}

if err := controllerutil.SetControllerReference(ps, action, r.Scheme); err != nil {
return nil, errors.Wrap(err, "error attaching owner reference to "+
"porter parameter set agent action")
}

if err := r.Create(ctx, action); err != nil {
return nil, errors.Wrap(err, "error creating the porter parameter set agent action")
}
Expand Down Expand Up @@ -253,16 +257,6 @@ func newPSAgentAction(ps *porterv1.ParameterSet) *porterv1.AgentAction {
GenerateName: ps.Name + "-",
Labels: labels,
Annotations: ps.Annotations,
OwnerReferences: []metav1.OwnerReference{
{ // I'm not using controllerutil.SetControllerReference because I can't track down why that throws a panic when running our tests
APIVersion: ps.APIVersion,
Kind: ps.Kind,
Name: ps.Name,
UID: ps.UID,
Controller: ptr.To(true),
BlockOwnerDeletion: ptr.To(true),
},
},
},
Spec: porterv1.AgentActionSpec{
AgentConfig: ps.Spec.AgentConfig,
Expand Down

0 comments on commit 14f4498

Please sign in to comment.