Skip to content

Commit

Permalink
refactor: add kind as annotation
Browse files Browse the repository at this point in the history
  • Loading branch information
FabianKramm committed Jul 16, 2024
1 parent db3dfe0 commit b94f976
Show file tree
Hide file tree
Showing 21 changed files with 70 additions and 2 deletions.
7 changes: 7 additions & 0 deletions pkg/controllers/generic/import_syncer.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@ import (

vclusterconfig "github.com/loft-sh/vcluster/config"
"github.com/loft-sh/vcluster/pkg/config"
"github.com/loft-sh/vcluster/pkg/scheme"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/klog/v2"
"sigs.k8s.io/controller-runtime/pkg/client/apiutil"

"github.com/loft-sh/vcluster/pkg/constants"
"github.com/loft-sh/vcluster/pkg/controllers/syncer"
Expand Down Expand Up @@ -467,6 +469,7 @@ func (s *importer) updateVirtualAnnotations(a map[string]string) map[string]stri
delete(a, translate.NameAnnotation)
delete(a, translate.NamespaceAnnotation)
delete(a, translate.UIDAnnotation)
delete(a, translate.KindAnnotation)
delete(a, corev1.LastAppliedConfigAnnotation)
return a
}
Expand All @@ -485,6 +488,10 @@ func (s *importer) addAnnotationsToPhysicalObject(ctx *synccontext.SyncContext,
annotations[translate.NameAnnotation] = vObj.GetName()
annotations[translate.NamespaceAnnotation] = vObj.GetNamespace()
annotations[translate.UIDAnnotation] = string(vObj.GetUID())
gvk, err := apiutil.GVKForObject(vObj, scheme.Scheme)
if err == nil {
annotations[translate.KindAnnotation] = gvk.String()
}
annotations[translate.ControllerLabel] = s.Name()
pObj.SetAnnotations(annotations)

Expand Down
1 change: 1 addition & 0 deletions pkg/controllers/resources/configmaps/syncer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ func TestSync(t *testing.T) {
translate.NameAnnotation: baseConfigMap.Name,
translate.NamespaceAnnotation: baseConfigMap.Namespace,
translate.UIDAnnotation: "",
translate.KindAnnotation: corev1.SchemeGroupVersion.WithKind("ConfigMap").String(),
},
Labels: map[string]string{
translate.NamespaceLabel: baseConfigMap.Namespace,
Expand Down
2 changes: 2 additions & 0 deletions pkg/controllers/resources/csistoragecapacities/syncer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ func TestSyncHostStorageClass(t *testing.T) {
translate.NameAnnotation: "test-csistoragecapacity",
translate.NamespaceAnnotation: "test",
translate.UIDAnnotation: "",
translate.KindAnnotation: storagev1.SchemeGroupVersion.WithKind("CSIStorageCapacity").String(),
},
Labels: map[string]string{
"vcluster.loft.sh/namespace": "test",
Expand Down Expand Up @@ -156,6 +157,7 @@ func TestSyncStorageClass(t *testing.T) {
translate.NameAnnotation: "test-csistoragecapacity",
translate.NamespaceAnnotation: "test",
translate.UIDAnnotation: "",
translate.KindAnnotation: storagev1.SchemeGroupVersion.WithKind("CSIStorageCapacity").String(),
},
Labels: map[string]string{
"vcluster.loft.sh/namespace": "test",
Expand Down
3 changes: 3 additions & 0 deletions pkg/controllers/resources/endpoints/syncer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ func TestExistingEndpoints(t *testing.T) {
translate.NameAnnotation: vEndpoints.Name,
translate.NamespaceAnnotation: vEndpoints.Namespace,
translate.UIDAnnotation: "",
translate.KindAnnotation: corev1.SchemeGroupVersion.WithKind("Service").String(),
},
Labels: map[string]string{
translate.NamespaceLabel: vEndpoints.Namespace,
Expand All @@ -74,6 +75,7 @@ func TestExistingEndpoints(t *testing.T) {
translate.NameAnnotation: vEndpoints.Name,
translate.NamespaceAnnotation: vEndpoints.Namespace,
translate.UIDAnnotation: "",
translate.KindAnnotation: corev1.SchemeGroupVersion.WithKind("Endpoints").String(),
},
Labels: map[string]string{
translate.NamespaceLabel: vEndpoints.Namespace,
Expand Down Expand Up @@ -151,6 +153,7 @@ func TestSync(t *testing.T) {
translate.NameAnnotation: baseEndpoints.Name,
translate.NamespaceAnnotation: baseEndpoints.Namespace,
translate.UIDAnnotation: "",
translate.KindAnnotation: corev1.SchemeGroupVersion.WithKind("Endpoints").String(),
},
Labels: map[string]string{
translate.NamespaceLabel: baseEndpoints.Namespace,
Expand Down
3 changes: 3 additions & 0 deletions pkg/controllers/resources/ingressclasses/syncer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ func TestSync(t *testing.T) {
Annotations: map[string]string{
translate.NameAnnotation: "test-ingc",
translate.UIDAnnotation: "",
translate.KindAnnotation: networkingv1.SchemeGroupVersion.WithKind("IngressClass").String(),
},
}

Expand All @@ -39,6 +40,7 @@ func TestSync(t *testing.T) {
Annotations: map[string]string{
translate.NameAnnotation: "test-ingc",
translate.UIDAnnotation: "",
translate.KindAnnotation: networkingv1.SchemeGroupVersion.WithKind("IngressClass").String(),
},
},
Spec: networkingv1.IngressClassSpec{
Expand Down Expand Up @@ -67,6 +69,7 @@ func TestSync(t *testing.T) {
Annotations: map[string]string{
translate.NameAnnotation: "test-ingc",
translate.UIDAnnotation: "",
translate.KindAnnotation: networkingv1.SchemeGroupVersion.WithKind("IngressClass").String(),
},
},
Spec: networkingv1.IngressClassSpec{
Expand Down
2 changes: 2 additions & 0 deletions pkg/controllers/resources/ingresses/legacy/syncer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ func TestSync(t *testing.T) {
translate.NameAnnotation: vObjectMeta.Name,
translate.NamespaceAnnotation: vObjectMeta.Namespace,
translate.UIDAnnotation: "",
translate.KindAnnotation: networkingv1beta1.SchemeGroupVersion.WithKind("Ingress").String(),
},
Labels: map[string]string{
translate.MarkerLabel: translate.VClusterName,
Expand Down Expand Up @@ -329,6 +330,7 @@ func TestSync(t *testing.T) {
"vcluster.loft.sh/object-name": baseIngress.Name,
"vcluster.loft.sh/object-namespace": baseIngress.Namespace,
translate.UIDAnnotation: "",
translate.KindAnnotation: networkingv1beta1.SchemeGroupVersion.WithKind("Ingress").String(),
"alb.ingress.kubernetes.io/actions.testservice-x-test-x-suffix": "{\"forwardConfig\":{\"targetGroups\":[{\"serviceName\":\"nginx-service-x-test-x-suffix\",\"servicePort\":\"80\",\"weight\":100}]}}",
},
},
Expand Down
3 changes: 3 additions & 0 deletions pkg/controllers/resources/ingresses/syncer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ func TestSync(t *testing.T) {
translate.NameAnnotation: vObjectMeta.Name,
translate.NamespaceAnnotation: vObjectMeta.Namespace,
translate.UIDAnnotation: "",
translate.KindAnnotation: networkingv1.SchemeGroupVersion.WithKind("Ingress").String(),
},
Labels: map[string]string{
translate.MarkerLabel: translate.VClusterName,
Expand Down Expand Up @@ -338,6 +339,7 @@ func TestSync(t *testing.T) {
"vcluster.loft.sh/object-name": baseIngress.Name,
"vcluster.loft.sh/object-namespace": baseIngress.Namespace,
translate.UIDAnnotation: "",
translate.KindAnnotation: networkingv1.SchemeGroupVersion.WithKind("Ingress").String(),
},
},
},
Expand Down Expand Up @@ -412,6 +414,7 @@ func TestSync(t *testing.T) {
"vcluster.loft.sh/object-name": baseIngress.Name,
"vcluster.loft.sh/object-namespace": baseIngress.Namespace,
translate.UIDAnnotation: "",
translate.KindAnnotation: networkingv1.SchemeGroupVersion.WithKind("Ingress").String(),
"alb.ingress.kubernetes.io/actions.testservice-x-test-x-suffix": `{"forwardConfig":{"targetGroups":[{"serviceName":"nginx-service-x-test-x-suffix","servicePort":"80","weight":100}]}}`,
"alb.ingress.kubernetes.io/actions.ssl-redirect-x-test-x-suffix": `{"redirectConfig":{"Port":"443","Protocol":"HTTPS","StatusCode":"HTTP_301"},"type":"redirect","forwardConfig":{}}`,
},
Expand Down
1 change: 1 addition & 0 deletions pkg/controllers/resources/networkpolicies/syncer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ func TestSync(t *testing.T) {
translate.NameAnnotation: vObjectMeta.Name,
translate.NamespaceAnnotation: vObjectMeta.Namespace,
translate.UIDAnnotation: "",
translate.KindAnnotation: networkingv1.SchemeGroupVersion.WithKind("NetworkPolicy").String(),
},
Labels: map[string]string{
translate.MarkerLabel: translate.VClusterName,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ func TestSync(t *testing.T) {
translate.NameAnnotation: vObjectMeta.Name,
translate.NamespaceAnnotation: vObjectMeta.Namespace,
translate.UIDAnnotation: "",
translate.KindAnnotation: corev1.SchemeGroupVersion.WithKind("PersistentVolumeClaim").String(),
},
Labels: map[string]string{
translate.MarkerLabel: translate.VClusterName,
Expand Down Expand Up @@ -78,6 +79,7 @@ func TestSync(t *testing.T) {
translate.NameAnnotation: vObjectMeta.Name,
translate.NamespaceAnnotation: vObjectMeta.Namespace,
translate.UIDAnnotation: "",
translate.KindAnnotation: corev1.SchemeGroupVersion.WithKind("PersistentVolumeClaim").String(),
translate.ManagedAnnotationsAnnotation: "otherAnnotationKey",
"otherAnnotationKey": "update this",
},
Expand All @@ -95,6 +97,7 @@ func TestSync(t *testing.T) {
translate.NameAnnotation: vObjectMeta.Name,
translate.NamespaceAnnotation: vObjectMeta.Namespace,
translate.UIDAnnotation: "",
translate.KindAnnotation: corev1.SchemeGroupVersion.WithKind("PersistentVolumeClaim").String(),
translate.ManagedAnnotationsAnnotation: "otherAnnotationKey",
bindCompletedAnnotation: "testannotation",
boundByControllerAnnotation: "testannotation2",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ func TestSync(t *testing.T) {
translate.NameAnnotation: vObjectMeta.Name,
translate.NamespaceAnnotation: vObjectMeta.Namespace,
translate.UIDAnnotation: "",
translate.KindAnnotation: policyv1.SchemeGroupVersion.WithKind("PodDisruptionBudget").String(),
},
Labels: map[string]string{
translate.NamespaceLabel: vObjectMeta.Namespace,
Expand Down
2 changes: 2 additions & 0 deletions pkg/controllers/resources/pods/syncer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ func TestSync(t *testing.T) {
translate.NameAnnotation: vObjectMeta.Name,
translate.UIDAnnotation: "",
translate.NamespaceAnnotation: vObjectMeta.Namespace,
translate.KindAnnotation: corev1.SchemeGroupVersion.WithKind("Pod").String(),
podtranslate.ServiceAccountNameAnnotation: "",
podtranslate.UIDAnnotation: string(vObjectMeta.UID),
},
Expand Down Expand Up @@ -247,6 +248,7 @@ func TestSync(t *testing.T) {
translate.NameAnnotation: vHostPathPod.Name,
translate.NamespaceAnnotation: vHostPathPod.Namespace,
translate.UIDAnnotation: "",
translate.KindAnnotation: corev1.SchemeGroupVersion.WithKind("Pod").String(),
podtranslate.ServiceAccountNameAnnotation: "",
podtranslate.UIDAnnotation: string(vHostPathPod.UID),
},
Expand Down
1 change: 1 addition & 0 deletions pkg/controllers/resources/secrets/syncer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ func TestSync(t *testing.T) {
translate.NameAnnotation: baseSecret.Name,
translate.NamespaceAnnotation: baseSecret.Namespace,
translate.UIDAnnotation: "",
translate.KindAnnotation: corev1.SchemeGroupVersion.WithKind("Secret").String(),
},
Labels: map[string]string{
translate.NamespaceLabel: baseSecret.Namespace,
Expand Down
1 change: 1 addition & 0 deletions pkg/controllers/resources/serviceaccounts/syncer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ func TestSync(t *testing.T) {
translate.NameAnnotation: vSA.Name,
translate.NamespaceAnnotation: vSA.Namespace,
translate.UIDAnnotation: "",
translate.KindAnnotation: corev1.SchemeGroupVersion.WithKind("ServiceAccount").String(),
},
Labels: map[string]string{
translate.NamespaceLabel: vSA.Namespace,
Expand Down
2 changes: 2 additions & 0 deletions pkg/controllers/resources/services/syncer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ func TestSync(t *testing.T) {
translate.NameAnnotation: vObjectMeta.Name,
translate.NamespaceAnnotation: vObjectMeta.Namespace,
translate.UIDAnnotation: "",
translate.KindAnnotation: corev1.SchemeGroupVersion.WithKind("Service").String(),
},
Labels: map[string]string{
translate.NamespaceLabel: vObjectMeta.Namespace,
Expand Down Expand Up @@ -86,6 +87,7 @@ func TestSync(t *testing.T) {
translate.NameAnnotation: vObjectMeta.Name,
translate.NamespaceAnnotation: vObjectMeta.Namespace,
translate.UIDAnnotation: "",
translate.KindAnnotation: corev1.SchemeGroupVersion.WithKind("Service").String(),
translate.ManagedAnnotationsAnnotation: "a",
"a": "b",
},
Expand Down
2 changes: 2 additions & 0 deletions pkg/controllers/resources/storageclasses/syncer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ func TestSync(t *testing.T) {
Annotations: map[string]string{
translate.NameAnnotation: "testsc",
translate.UIDAnnotation: "",
translate.KindAnnotation: storagev1.SchemeGroupVersion.WithKind("StorageClass").String(),
},
},
Provisioner: "my-provisioner",
Expand All @@ -53,6 +54,7 @@ func TestSync(t *testing.T) {
Annotations: map[string]string{
translate.NameAnnotation: "testsc",
translate.UIDAnnotation: "",
translate.KindAnnotation: storagev1.SchemeGroupVersion.WithKind("StorageClass").String(),
},
},
Provisioner: "my-provisioner",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ func TestSync(t *testing.T) {
Annotations: map[string]string{
translate.NameAnnotation: vObjectMeta.Name,
translate.UIDAnnotation: "",
translate.KindAnnotation: volumesnapshotv1.SchemeGroupVersion.WithKind("VolumeSnapshotContent").String(),
},
}
pPreProvisioned := &volumesnapshotv1.VolumeSnapshotContent{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ func TestSync(t *testing.T) {
translate.NameAnnotation: vObjectMeta.Name,
translate.NamespaceAnnotation: vObjectMeta.Namespace,
translate.UIDAnnotation: "",
translate.KindAnnotation: volumesnapshotv1.SchemeGroupVersion.WithKind("VolumeSnapshot").String(),
},
Labels: map[string]string{
translate.MarkerLabel: translate.VClusterName,
Expand Down
1 change: 1 addition & 0 deletions pkg/controllers/syncer/syncer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ func TestReconcile(t *testing.T) {
translate.NameAnnotation: "a",
translate.NamespaceAnnotation: namespaceInVclusterA,
translate.UIDAnnotation: "123",
translate.KindAnnotation: corev1.SchemeGroupVersion.WithKind("Secret").String(),
},
Labels: map[string]string{
translate.NamespaceLabel: namespaceInVclusterA,
Expand Down
17 changes: 16 additions & 1 deletion pkg/util/translate/multi_namespace.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@ import (
"regexp"
"strings"

"github.com/loft-sh/vcluster/pkg/scheme"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/equality"
"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/client/apiutil"
)

var _ Translator = &multiNamespace{}
Expand Down Expand Up @@ -91,6 +93,13 @@ func (s *multiNamespace) IsManagedCluster(obj runtime.Object) bool {
return false
}

if metaAccessor.GetAnnotations() != nil && metaAccessor.GetAnnotations()[KindAnnotation] != "" {
gvk, err := apiutil.GVKForObject(obj, scheme.Scheme)
if err == nil && gvk.String() != metaAccessor.GetAnnotations()[KindAnnotation] {
return false
}
}

return metaAccessor.GetLabels()[MarkerLabel] == SafeConcatName(s.currentNamespace, "x", VClusterName)
}

Expand Down Expand Up @@ -205,7 +214,7 @@ func (s *multiNamespace) ApplyMetadataUpdate(vObj client.Object, pObj client.Obj
}

func (s *multiNamespace) ApplyAnnotations(src client.Object, to client.Object, excluded []string) map[string]string {
excluded = append(excluded, NameAnnotation, NamespaceAnnotation)
excluded = append(excluded, NameAnnotation, NamespaceAnnotation, UIDAnnotation, KindAnnotation)
toAnnotations := map[string]string{}
if to != nil {
toAnnotations = to.GetAnnotations()
Expand All @@ -219,6 +228,12 @@ func (s *multiNamespace) ApplyAnnotations(src client.Object, to client.Object, e
} else {
retMap[NamespaceAnnotation] = src.GetNamespace()
}

gvk, err := apiutil.GVKForObject(src, scheme.Scheme)
if err == nil {
retMap[KindAnnotation] = gvk.String()
}

return retMap
}

Expand Down
17 changes: 16 additions & 1 deletion pkg/util/translate/single_namespace.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@ import (
"regexp"
"strings"

"github.com/loft-sh/vcluster/pkg/scheme"
"github.com/loft-sh/vcluster/pkg/util/base36"
"k8s.io/apimachinery/pkg/api/equality"
"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/client/apiutil"
)

var _ Translator = &singleNamespace{}
Expand Down Expand Up @@ -92,6 +94,14 @@ func (s *singleNamespace) IsManaged(obj runtime.Object, physicalName PhysicalNam
return false
}

// if kind doesn't match vCluster has probably not synced the object
if metaAccessor.GetAnnotations() != nil && metaAccessor.GetAnnotations()[KindAnnotation] != "" {
gvk, err := apiutil.GVKForObject(obj, scheme.Scheme)
if err == nil && gvk.String() != metaAccessor.GetAnnotations()[KindAnnotation] {
return false
}
}

return metaAccessor.GetLabels()[MarkerLabel] == VClusterName
}

Expand Down Expand Up @@ -203,7 +213,7 @@ func (s *singleNamespace) ApplyMetadataUpdate(vObj client.Object, pObj client.Ob
}

func (s *singleNamespace) ApplyAnnotations(src client.Object, to client.Object, excluded []string) map[string]string {
excluded = append(excluded, NameAnnotation, UIDAnnotation, NamespaceAnnotation)
excluded = append(excluded, NameAnnotation, UIDAnnotation, KindAnnotation, NamespaceAnnotation)
toAnnotations := map[string]string{}
if to != nil {
toAnnotations = to.GetAnnotations()
Expand All @@ -218,6 +228,11 @@ func (s *singleNamespace) ApplyAnnotations(src client.Object, to client.Object,
retMap[NamespaceAnnotation] = src.GetNamespace()
}

gvk, err := apiutil.GVKForObject(src, scheme.Scheme)
if err == nil {
retMap[KindAnnotation] = gvk.String()
}

return retMap
}

Expand Down
1 change: 1 addition & 0 deletions pkg/util/translate/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ var (
NamespaceAnnotation = "vcluster.loft.sh/object-namespace"
NameAnnotation = "vcluster.loft.sh/object-name"
UIDAnnotation = "vcluster.loft.sh/object-uid"
KindAnnotation = "vcluster.loft.sh/object-kind"
)

var Default Translator = &singleNamespace{}
Expand Down

0 comments on commit b94f976

Please sign in to comment.