Skip to content

Commit 591b55c

Browse files
Merge pull request #48 from jparrill/OADP-6121
OADP-6121: Return early when plugin enabled but not HC involved
2 parents 90193ae + bdb6cad commit 591b55c

File tree

3 files changed

+93
-0
lines changed

3 files changed

+93
-0
lines changed

pkg/common/utils.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -400,3 +400,41 @@ func GetHCP(ctx context.Context, nsList []string, client crclient.Client, log lo
400400
func GetHCPNamespace(name, namespace string) string {
401401
return fmt.Sprintf("%s-%s", namespace, name)
402402
}
403+
404+
// ShouldEndPluginExecution checks if the plugin should end execution by verifying if the required
405+
// Hypershift resources (HostedControlPlane and HostedCluster) exist in the cluster.
406+
// Returns true if the plugin should end execution (i.e., if this is not a Hypershift cluster).
407+
func ShouldEndPluginExecution(namespaces []string, client crclient.Client, log logrus.FieldLogger) bool {
408+
// Check if HostedControlPlane exists
409+
hcpList := &hyperv1.HostedControlPlaneList{}
410+
for _, ns := range namespaces {
411+
if err := client.List(context.TODO(), hcpList, crclient.InNamespace(ns)); err != nil {
412+
log.Debugf("Error checking for HostedControlPlanes: %v", err)
413+
return true
414+
}
415+
if len(hcpList.Items) > 0 {
416+
break
417+
}
418+
}
419+
420+
// Check if HostedCluster exists
421+
hcList := &hyperv1.HostedClusterList{}
422+
for _, ns := range namespaces {
423+
if err := client.List(context.TODO(), hcList, crclient.InNamespace(ns)); err != nil {
424+
log.Debugf("Error checking for HostedCluster: %v", err)
425+
return true
426+
}
427+
if len(hcList.Items) > 0 {
428+
break
429+
}
430+
}
431+
432+
// If the resources are found, we assume a HostedControlPlane needs to be backed up
433+
if len(hcpList.Items) > 0 && len(hcList.Items) > 0 {
434+
log.Debug("Found Hypershift resources")
435+
return false
436+
}
437+
438+
log.Debug("No Hypershift resources found")
439+
return true
440+
}

pkg/common/utils_test.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -736,3 +736,50 @@ func (f *fakeClient) Delete(ctx context.Context, obj client.Object, opts ...clie
736736
f.deletedPods[obj.GetName()] = true
737737
return nil
738738
}
739+
740+
func TestShouldEndPluginExecution(t *testing.T) {
741+
tests := []struct {
742+
name string
743+
objects []client.Object
744+
expectedResult bool
745+
}{
746+
{
747+
name: "CRDs exist",
748+
objects: []client.Object{
749+
&hyperv1.HostedControlPlane{
750+
ObjectMeta: metav1.ObjectMeta{
751+
Name: "test-hcp",
752+
Namespace: "test-namespace",
753+
},
754+
},
755+
&hyperv1.HostedCluster{
756+
ObjectMeta: metav1.ObjectMeta{
757+
Name: "test-hc",
758+
Namespace: "test-namespace",
759+
},
760+
},
761+
},
762+
expectedResult: false,
763+
},
764+
{
765+
name: "CRDs do not exist",
766+
objects: []client.Object{},
767+
expectedResult: true,
768+
},
769+
}
770+
771+
scheme := runtime.NewScheme()
772+
_ = hyperv1.AddToScheme(scheme)
773+
774+
for _, tt := range tests {
775+
t.Run(tt.name, func(t *testing.T) {
776+
g := NewWithT(t)
777+
778+
client := fake.NewClientBuilder().WithScheme(scheme).WithObjects(tt.objects...).Build()
779+
log := logrus.New()
780+
781+
result := ShouldEndPluginExecution([]string{"test-namespace"}, client, log)
782+
g.Expect(result).To(Equal(tt.expectedResult))
783+
})
784+
}
785+
}

pkg/core/backup.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,10 +132,18 @@ func (p *BackupPlugin) Execute(item runtime.Unstructured, backup *velerov1.Backu
132132
p.log.Debug("Entering Hypershift backup plugin")
133133
ctx := context.Context(p.ctx)
134134

135+
if returnEarly := common.ShouldEndPluginExecution(backup.Spec.IncludedNamespaces, p.client, p.log); returnEarly {
136+
return item, nil, nil
137+
}
138+
135139
if p.hcp == nil {
136140
var err error
137141
p.hcp, err = common.GetHCP(ctx, backup.Spec.IncludedNamespaces, p.client, p.log)
138142
if err != nil {
143+
if apierrors.IsNotFound(err) {
144+
p.log.Infof("HCP not found, assuming not hypershift cluster to backup")
145+
return item, nil, nil
146+
}
139147
return nil, nil, fmt.Errorf("error getting HCP namespace: %v", err)
140148
}
141149

0 commit comments

Comments
 (0)