Skip to content

Commit 4967e19

Browse files
committed
Add new status for originCluster reference
1 parent 5331844 commit 4967e19

File tree

5 files changed

+60
-16
lines changed

5 files changed

+60
-16
lines changed

charts/rancher-backup-crd/templates/backup.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,9 @@ spec:
127127
type: string
128128
observedGeneration:
129129
type: integer
130+
originCluster:
131+
nullable: true
132+
type: string
130133
storageLocation:
131134
nullable: true
132135
type: string

pkg/apis/resources.cattle.io/v1/types.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,6 @@ var (
1717
RestoreConditionReady = "Ready"
1818
)
1919

20-
const (
21-
BackupClusterOriginIndex = "field.cattle.io/originClusterId"
22-
)
23-
2420
// +genclient
2521
// +genclient:nonNamespaced
2622
// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
@@ -43,6 +39,7 @@ type BackupSpec struct {
4339

4440
type BackupStatus struct {
4541
Conditions []genericcondition.GenericCondition `json:"conditions"`
42+
OriginCluster string `json:"originCluster,omitempty"`
4643
LastSnapshotTS string `json:"lastSnapshotTs"`
4744
NextSnapshotAt string `json:"nextSnapshotAt"`
4845
ObservedGeneration int64 `json:"observedGeneration"`

pkg/controllers/backup/cluster_origin.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,10 @@ func newBackupClusterOriginConditionMeta(controllerClusterID string, backup *v1.
2727
currentInPlaceRestoreCondition: false,
2828
}
2929

30-
originAnnotationValue, ok := backup.GetAnnotations()[v1.BackupClusterOriginIndex]
31-
conditionMeta.hasClusterOriginID = ok && originAnnotationValue != ""
30+
originalValue := backup.Status.OriginCluster
31+
conditionMeta.hasClusterOriginID = originalValue != ""
3232
if conditionMeta.hasClusterOriginID {
33-
conditionMeta.clusterOriginID = originAnnotationValue
33+
conditionMeta.clusterOriginID = originalValue
3434
}
3535

3636
currentOriginConditionString := condition.Cond(v1.BackupConditionClusterOrigin).GetStatus(backup)

pkg/controllers/backup/controller.go

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@ type handler struct {
4040
defaultBackupMountPath string
4141
defaultS3BackupLocation *v1.S3ObjectStore
4242
// TODO: rename to kubeSystemNamespaceUID; nit to improve clarity, it's not the string representation nor the NS resource
43-
kubeSystemNS string
43+
kubeSystemNS string
44+
canUseClusterOrigin bool
4445
}
4546

4647
const DefaultRetentionCount = 10
@@ -66,6 +67,7 @@ func Register(
6667
dynamicClient: dynamicInterface,
6768
defaultBackupMountPath: defaultLocalBackupLocation,
6869
defaultS3BackupLocation: defaultS3,
70+
canUseClusterOrigin: util.VerifyBackupCrdHasClusterStatus(dynamicInterface),
6971
}
7072
if controller.defaultBackupMountPath != "" {
7173
logrus.Infof("Default location for storing backups is %v", controller.defaultBackupMountPath)
@@ -185,21 +187,15 @@ func (h *handler) OnBackupChange(_ string, backup *v1.Backup) (*v1.Backup, error
185187
}
186188
}
187189

188-
backupAnnotations := backup.GetAnnotations()
189-
if backupAnnotations == nil {
190-
backupAnnotations = map[string]string{}
191-
}
192-
backupAnnotations[v1.BackupClusterOriginIndex] = h.kubeSystemNS
193-
backup.SetAnnotations(backupAnnotations)
194-
_, err = h.backups.Update(backup)
195-
196190
storageLocationType := backup.Status.StorageLocation
197191
updateErr := retry.RetryOnConflict(retry.DefaultRetry, func() error {
198192
var err error
199193
backup, err = h.backups.Get(backup.Name, k8sv1.GetOptions{})
200194
if err != nil {
201195
return err
202196
}
197+
// Set the Cluster origin reference on backup
198+
backup.Status.OriginCluster = h.kubeSystemNS
203199
// reset conditions to remove the reconciling condition, because as per kstatus lib its presence is considered an error
204200
backup.Status.Conditions = []genericcondition.GenericCondition{}
205201

pkg/util/util.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,15 @@ package util
33
import (
44
"context"
55
"fmt"
6+
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
7+
"k8s.io/client-go/dynamic"
68
"os"
79
"reflect"
810

911
v1core "github.com/rancher/wrangler/v3/pkg/generated/controllers/core/v1"
1012
"github.com/sirupsen/logrus"
1113
k8sv1 "k8s.io/apimachinery/pkg/apis/meta/v1"
14+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1215
"k8s.io/apimachinery/pkg/runtime/schema"
1316
"k8s.io/apiserver/pkg/server/options/encryptionconfig"
1417
"k8s.io/apiserver/pkg/storage/value"
@@ -83,3 +86,48 @@ func FetchClusterUID(namespaces v1core.NamespaceController) (string, error) {
8386

8487
return string(kubesystemNamespace.UID), nil
8588
}
89+
90+
// Define the GroupVersionResource for CRDs
91+
var crdGVR = schema.GroupVersionResource{
92+
Group: "apiextensions.k8s.io",
93+
Version: "v1",
94+
Resource: "customresourcedefinitions",
95+
}
96+
97+
func getCRDDefinition(dynamicClient dynamic.Interface, crdName string) (*unstructured.Unstructured, error) {
98+
crd, err := dynamicClient.Resource(crdGVR).Get(context.TODO(), crdName, metav1.GetOptions{})
99+
if err != nil {
100+
return nil, err
101+
}
102+
return crd, nil
103+
}
104+
105+
func VerifyBackupCrdHasClusterStatus(client dynamic.Interface) bool {
106+
crdName := "backups.resources.cattle.io"
107+
108+
crd, err := getCRDDefinition(client, crdName)
109+
if err != nil {
110+
logrus.Infof("Error fetching CRD: %v", err)
111+
return false
112+
}
113+
114+
// Inspect the status schema, for example
115+
if statusSchema, found, _ := unstructured.NestedMap(
116+
crd.Object,
117+
"spec",
118+
"versions",
119+
"0",
120+
"schema",
121+
"openAPIV3Schema",
122+
"properties",
123+
"status",
124+
"properties",
125+
"originCluster",
126+
); found {
127+
logrus.Debugf("Status schema contains `originCluster` on CRD %s: %v\n", crdName, statusSchema)
128+
return true
129+
}
130+
131+
logrus.Debugf("`originCluster` not found on status schema for CRD %s\n", crdName)
132+
return false
133+
}

0 commit comments

Comments
 (0)