Skip to content

Commit

Permalink
Merge pull request #2083 from FabianKramm/main
Browse files Browse the repository at this point in the history
refactor: add host name & namespace as annotations
  • Loading branch information
FabianKramm authored Aug 20, 2024
2 parents e5d0c6b + 76185ff commit 3bb7084
Show file tree
Hide file tree
Showing 26 changed files with 693 additions and 122 deletions.
2 changes: 2 additions & 0 deletions pkg/controllers/generic/import_syncer.go
Original file line number Diff line number Diff line change
Expand Up @@ -426,6 +426,8 @@ func (s *importer) updateVirtualAnnotations(a map[string]string) map[string]stri
delete(a, translate.NamespaceAnnotation)
delete(a, translate.UIDAnnotation)
delete(a, translate.KindAnnotation)
delete(a, translate.HostNameAnnotation)
delete(a, translate.HostNamespaceAnnotation)
delete(a, corev1.LastAppliedConfigAnnotation)
return a
}
Expand Down
10 changes: 6 additions & 4 deletions pkg/controllers/resources/configmaps/syncer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,12 @@ func TestSync(t *testing.T) {
Name: translate.Default.HostName(nil, baseConfigMap.Name, baseConfigMap.Namespace).Name,
Namespace: "test",
Annotations: map[string]string{
translate.NameAnnotation: baseConfigMap.Name,
translate.NamespaceAnnotation: baseConfigMap.Namespace,
translate.UIDAnnotation: "",
translate.KindAnnotation: corev1.SchemeGroupVersion.WithKind("ConfigMap").String(),
translate.NameAnnotation: baseConfigMap.Name,
translate.NamespaceAnnotation: baseConfigMap.Namespace,
translate.UIDAnnotation: "",
translate.KindAnnotation: corev1.SchemeGroupVersion.WithKind("ConfigMap").String(),
translate.HostNamespaceAnnotation: "test",
translate.HostNameAnnotation: translate.Default.HostName(nil, baseConfigMap.Name, baseConfigMap.Namespace).Name,
},
Labels: map[string]string{
translate.NamespaceLabel: baseConfigMap.Namespace,
Expand Down
20 changes: 12 additions & 8 deletions pkg/controllers/resources/csistoragecapacities/syncer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,12 @@ func TestSyncHostStorageClass(t *testing.T) {
Name: "test-csistoragecapacity-x-test",
Namespace: "kube-system",
Annotations: map[string]string{
translate.NameAnnotation: "test-csistoragecapacity",
translate.NamespaceAnnotation: "test",
translate.UIDAnnotation: "",
translate.KindAnnotation: storagev1.SchemeGroupVersion.WithKind("CSIStorageCapacity").String(),
translate.NameAnnotation: "test-csistoragecapacity",
translate.NamespaceAnnotation: "test",
translate.UIDAnnotation: "",
translate.KindAnnotation: storagev1.SchemeGroupVersion.WithKind("CSIStorageCapacity").String(),
translate.HostNameAnnotation: "test-csistoragecapacity-x-test",
translate.HostNamespaceAnnotation: "kube-system",
},
Labels: map[string]string{
"vcluster.loft.sh/namespace": "test",
Expand Down Expand Up @@ -159,10 +161,12 @@ func TestSyncStorageClass(t *testing.T) {
Name: "test-csistoragecapacity-x-test",
Namespace: "kube-system",
Annotations: map[string]string{
translate.NameAnnotation: "test-csistoragecapacity",
translate.NamespaceAnnotation: "test",
translate.UIDAnnotation: "",
translate.KindAnnotation: storagev1.SchemeGroupVersion.WithKind("CSIStorageCapacity").String(),
translate.NameAnnotation: "test-csistoragecapacity",
translate.NamespaceAnnotation: "test",
translate.UIDAnnotation: "",
translate.KindAnnotation: storagev1.SchemeGroupVersion.WithKind("CSIStorageCapacity").String(),
translate.HostNameAnnotation: "test-csistoragecapacity-x-test",
translate.HostNamespaceAnnotation: "kube-system",
},
Labels: map[string]string{
"vcluster.loft.sh/namespace": "test",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func (s *csistoragecapacitySyncer) translateBackwards(ctx *synccontext.SyncConte
// TranslateMetadata translates the object's metadata
func (s *csistoragecapacitySyncer) virtualMetadata(ctx *synccontext.SyncContext, pObj *storagev1.CSIStorageCapacity) *storagev1.CSIStorageCapacity {
vObj := translate.CopyObjectWithName(pObj, s.HostToVirtual(ctx, types.NamespacedName{Name: pObj.Name, Namespace: pObj.Namespace}, pObj), false)
vObj.SetAnnotations(translate.HostAnnotations(pObj, nil))
vObj.SetAnnotations(translate.HostAnnotations(pObj, vObj))
vObj.SetLabels(translate.HostLabels(pObj, nil))
return vObj
}
Expand Down
30 changes: 18 additions & 12 deletions pkg/controllers/resources/endpoints/syncer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,12 @@ func TestExistingEndpoints(t *testing.T) {
Name: translate.Default.HostName(nil, vEndpoints.Name, vEndpoints.Namespace).Name,
Namespace: "test",
Annotations: map[string]string{
translate.NameAnnotation: vEndpoints.Name,
translate.NamespaceAnnotation: vEndpoints.Namespace,
translate.UIDAnnotation: "",
translate.KindAnnotation: corev1.SchemeGroupVersion.WithKind("Service").String(),
translate.NameAnnotation: vEndpoints.Name,
translate.NamespaceAnnotation: vEndpoints.Namespace,
translate.UIDAnnotation: "",
translate.KindAnnotation: corev1.SchemeGroupVersion.WithKind("Service").String(),
translate.HostNamespaceAnnotation: "test",
translate.HostNameAnnotation: translate.Default.HostName(nil, vEndpoints.Name, vEndpoints.Namespace).Name,
},
Labels: map[string]string{
translate.NamespaceLabel: vEndpoints.Namespace,
Expand All @@ -72,10 +74,12 @@ func TestExistingEndpoints(t *testing.T) {
Name: translate.Default.HostName(nil, vEndpoints.Name, vEndpoints.Namespace).Name,
Namespace: "test",
Annotations: map[string]string{
translate.NameAnnotation: vEndpoints.Name,
translate.NamespaceAnnotation: vEndpoints.Namespace,
translate.UIDAnnotation: "",
translate.KindAnnotation: corev1.SchemeGroupVersion.WithKind("Endpoints").String(),
translate.NameAnnotation: vEndpoints.Name,
translate.NamespaceAnnotation: vEndpoints.Namespace,
translate.UIDAnnotation: "",
translate.KindAnnotation: corev1.SchemeGroupVersion.WithKind("Endpoints").String(),
translate.HostNamespaceAnnotation: "test",
translate.HostNameAnnotation: translate.Default.HostName(nil, vEndpoints.Name, vEndpoints.Namespace).Name,
},
Labels: map[string]string{
translate.NamespaceLabel: vEndpoints.Namespace,
Expand Down Expand Up @@ -151,10 +155,12 @@ func TestSync(t *testing.T) {
Name: translate.Default.HostName(nil, baseEndpoints.Name, baseEndpoints.Namespace).Name,
Namespace: "test",
Annotations: map[string]string{
translate.NameAnnotation: baseEndpoints.Name,
translate.NamespaceAnnotation: baseEndpoints.Namespace,
translate.UIDAnnotation: "",
translate.KindAnnotation: corev1.SchemeGroupVersion.WithKind("Endpoints").String(),
translate.NameAnnotation: baseEndpoints.Name,
translate.NamespaceAnnotation: baseEndpoints.Namespace,
translate.UIDAnnotation: "",
translate.KindAnnotation: corev1.SchemeGroupVersion.WithKind("Endpoints").String(),
translate.HostNamespaceAnnotation: "test",
translate.HostNameAnnotation: translate.Default.HostName(nil, baseEndpoints.Name, baseEndpoints.Namespace).Name,
},
Labels: map[string]string{
translate.NamespaceLabel: baseEndpoints.Namespace,
Expand Down
14 changes: 10 additions & 4 deletions pkg/controllers/resources/ingresses/syncer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,10 +108,12 @@ func TestSync(t *testing.T) {
Name: translate.Default.HostName(nil, "testingress", "test").Name,
Namespace: "test",
Annotations: map[string]string{
translate.NameAnnotation: vObjectMeta.Name,
translate.NamespaceAnnotation: vObjectMeta.Namespace,
translate.UIDAnnotation: "",
translate.KindAnnotation: networkingv1.SchemeGroupVersion.WithKind("Ingress").String(),
translate.NameAnnotation: vObjectMeta.Name,
translate.NamespaceAnnotation: vObjectMeta.Namespace,
translate.UIDAnnotation: "",
translate.KindAnnotation: networkingv1.SchemeGroupVersion.WithKind("Ingress").String(),
translate.HostNamespaceAnnotation: "test",
translate.HostNameAnnotation: translate.Default.HostName(nil, "testingress", "test").Name,
},
Labels: map[string]string{
translate.MarkerLabel: translate.VClusterName,
Expand Down Expand Up @@ -345,6 +347,8 @@ func TestSync(t *testing.T) {
"vcluster.loft.sh/object-namespace": baseIngress.Namespace,
translate.UIDAnnotation: "",
translate.KindAnnotation: networkingv1.SchemeGroupVersion.WithKind("Ingress").String(),
translate.HostNamespaceAnnotation: createdIngress.Namespace,
translate.HostNameAnnotation: createdIngress.Name,
},
},
},
Expand Down Expand Up @@ -420,6 +424,8 @@ func TestSync(t *testing.T) {
"vcluster.loft.sh/object-namespace": baseIngress.Namespace,
translate.UIDAnnotation: "",
translate.KindAnnotation: networkingv1.SchemeGroupVersion.WithKind("Ingress").String(),
translate.HostNamespaceAnnotation: createdIngress.Namespace,
translate.HostNameAnnotation: createdIngress.Name,
"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
10 changes: 6 additions & 4 deletions pkg/controllers/resources/networkpolicies/syncer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,12 @@ func TestSync(t *testing.T) {
Name: translate.Default.HostName(nil, "testnetworkpolicy", "test").Name,
Namespace: "test",
Annotations: map[string]string{
translate.NameAnnotation: vObjectMeta.Name,
translate.NamespaceAnnotation: vObjectMeta.Namespace,
translate.UIDAnnotation: "",
translate.KindAnnotation: networkingv1.SchemeGroupVersion.WithKind("NetworkPolicy").String(),
translate.NameAnnotation: vObjectMeta.Name,
translate.NamespaceAnnotation: vObjectMeta.Namespace,
translate.UIDAnnotation: "",
translate.KindAnnotation: networkingv1.SchemeGroupVersion.WithKind("NetworkPolicy").String(),
translate.HostNameAnnotation: translate.Default.HostName(nil, "testnetworkpolicy", "test").Name,
translate.HostNamespaceAnnotation: "test",
},
Labels: map[string]string{
translate.MarkerLabel: translate.VClusterName,
Expand Down
28 changes: 17 additions & 11 deletions pkg/controllers/resources/persistentvolumeclaims/syncer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,12 @@ func TestSync(t *testing.T) {
Name: translate.Default.HostName(nil, "testpvc", "testns").Name,
Namespace: "test",
Annotations: map[string]string{
translate.NameAnnotation: vObjectMeta.Name,
translate.NamespaceAnnotation: vObjectMeta.Namespace,
translate.UIDAnnotation: "",
translate.KindAnnotation: corev1.SchemeGroupVersion.WithKind("PersistentVolumeClaim").String(),
translate.NameAnnotation: vObjectMeta.Name,
translate.NamespaceAnnotation: vObjectMeta.Namespace,
translate.UIDAnnotation: "",
translate.KindAnnotation: corev1.SchemeGroupVersion.WithKind("PersistentVolumeClaim").String(),
translate.HostNamespaceAnnotation: "test",
translate.HostNameAnnotation: translate.Default.HostName(nil, "testpvc", "testns").Name,
},
Labels: map[string]string{
translate.MarkerLabel: translate.VClusterName,
Expand Down Expand Up @@ -81,6 +83,8 @@ func TestSync(t *testing.T) {
translate.NamespaceAnnotation: vObjectMeta.Namespace,
translate.UIDAnnotation: "",
translate.KindAnnotation: corev1.SchemeGroupVersion.WithKind("PersistentVolumeClaim").String(),
translate.HostNamespaceAnnotation: pObjectMeta.Namespace,
translate.HostNameAnnotation: pObjectMeta.Name,
translate.ManagedAnnotationsAnnotation: "otherAnnotationKey",
"otherAnnotationKey": "update this",
},
Expand All @@ -95,13 +99,15 @@ func TestSync(t *testing.T) {
Name: pObjectMeta.Name,
Namespace: pObjectMeta.Namespace,
Annotations: map[string]string{
translate.NameAnnotation: vObjectMeta.Name,
translate.NamespaceAnnotation: vObjectMeta.Namespace,
translate.UIDAnnotation: "",
translate.KindAnnotation: corev1.SchemeGroupVersion.WithKind("PersistentVolumeClaim").String(),
bindCompletedAnnotation: "testannotation",
boundByControllerAnnotation: "testannotation2",
storageProvisionerAnnotation: "testannotation3",
translate.NameAnnotation: vObjectMeta.Name,
translate.NamespaceAnnotation: vObjectMeta.Namespace,
translate.UIDAnnotation: "",
translate.KindAnnotation: corev1.SchemeGroupVersion.WithKind("PersistentVolumeClaim").String(),
translate.HostNameAnnotation: pObjectMeta.Name,
translate.HostNamespaceAnnotation: pObjectMeta.Namespace,
bindCompletedAnnotation: "testannotation",
boundByControllerAnnotation: "testannotation2",
storageProvisionerAnnotation: "testannotation3",
},
Labels: pObjectMeta.Labels,
},
Expand Down
10 changes: 6 additions & 4 deletions pkg/controllers/resources/poddisruptionbudgets/syncer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,12 @@ func TestSync(t *testing.T) {
Name: translate.Default.HostName(nil, "testPDB", vObjectMeta.Namespace).Name,
Namespace: "test",
Annotations: map[string]string{
translate.NameAnnotation: vObjectMeta.Name,
translate.NamespaceAnnotation: vObjectMeta.Namespace,
translate.UIDAnnotation: "",
translate.KindAnnotation: policyv1.SchemeGroupVersion.WithKind("PodDisruptionBudget").String(),
translate.NameAnnotation: vObjectMeta.Name,
translate.NamespaceAnnotation: vObjectMeta.Namespace,
translate.UIDAnnotation: "",
translate.KindAnnotation: policyv1.SchemeGroupVersion.WithKind("PodDisruptionBudget").String(),
translate.HostNameAnnotation: translate.Default.HostName(nil, "testPDB", vObjectMeta.Namespace).Name,
translate.HostNamespaceAnnotation: "test",
},
Labels: map[string]string{
translate.NamespaceLabel: vObjectMeta.Namespace,
Expand Down
6 changes: 6 additions & 0 deletions pkg/controllers/resources/pods/syncer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ func TestSyncTable(t *testing.T) {
translate.UIDAnnotation: "",
translate.NamespaceAnnotation: vObjectMeta.Namespace,
translate.KindAnnotation: corev1.SchemeGroupVersion.WithKind("Pod").String(),
translate.HostNamespaceAnnotation: "test",
translate.HostNameAnnotation: translate.Default.HostName(nil, "testpod", "testns").Name,
podtranslate.ServiceAccountNameAnnotation: "",
podtranslate.UIDAnnotation: string(vObjectMeta.UID),
},
Expand Down Expand Up @@ -365,6 +367,8 @@ func TestSync(t *testing.T) {
translate.UIDAnnotation: "",
translate.NamespaceAnnotation: vObjectMeta.Namespace,
translate.KindAnnotation: corev1.SchemeGroupVersion.WithKind("Pod").String(),
translate.HostNameAnnotation: translate.Default.HostName(nil, "testpod", "testns").Name,
translate.HostNamespaceAnnotation: "test",
podtranslate.ServiceAccountNameAnnotation: "",
podtranslate.UIDAnnotation: string(vObjectMeta.UID),
},
Expand Down Expand Up @@ -467,6 +471,8 @@ func TestSync(t *testing.T) {
translate.NamespaceAnnotation: vHostPathPod.Namespace,
translate.UIDAnnotation: "",
translate.KindAnnotation: corev1.SchemeGroupVersion.WithKind("Pod").String(),
translate.HostNamespaceAnnotation: "test",
translate.HostNameAnnotation: translate.Default.HostName(nil, vHostPathPod.Name, testingutil.DefaultTestCurrentNamespace).Name,
podtranslate.ServiceAccountNameAnnotation: "",
podtranslate.UIDAnnotation: string(vHostPathPod.UID),
},
Expand Down
10 changes: 6 additions & 4 deletions pkg/controllers/resources/secrets/syncer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,12 @@ func TestSync(t *testing.T) {
Name: translate.Default.HostName(nil, baseSecret.Name, baseSecret.Namespace).Name,
Namespace: "test",
Annotations: map[string]string{
translate.NameAnnotation: baseSecret.Name,
translate.NamespaceAnnotation: baseSecret.Namespace,
translate.UIDAnnotation: "",
translate.KindAnnotation: corev1.SchemeGroupVersion.WithKind("Secret").String(),
translate.NameAnnotation: baseSecret.Name,
translate.NamespaceAnnotation: baseSecret.Namespace,
translate.UIDAnnotation: "",
translate.KindAnnotation: corev1.SchemeGroupVersion.WithKind("Secret").String(),
translate.HostNamespaceAnnotation: "test",
translate.HostNameAnnotation: translate.Default.HostName(nil, baseSecret.Name, baseSecret.Namespace).Name,
},
Labels: map[string]string{
translate.NamespaceLabel: baseSecret.Namespace,
Expand Down
2 changes: 2 additions & 0 deletions pkg/controllers/resources/serviceaccounts/syncer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ func TestSync(t *testing.T) {
translate.NamespaceAnnotation: vSA.Namespace,
translate.UIDAnnotation: "",
translate.KindAnnotation: corev1.SchemeGroupVersion.WithKind("ServiceAccount").String(),
translate.HostNamespaceAnnotation: "test",
translate.HostNameAnnotation: translate.Default.HostName(nil, vSA.Name, vSA.Namespace).Name,
},
Labels: map[string]string{
translate.NamespaceLabel: vSA.Namespace,
Expand Down
2 changes: 1 addition & 1 deletion pkg/controllers/resources/services/syncer.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ func (s *serviceSyncer) Sync(ctx *synccontext.SyncContext, event *synccontext.Sy

// translate selector
if event.Source == synccontext.SyncEventSourceHost {
event.Host.Spec.Selector = translate.VirtualLabelsMap(event.Host.Spec.Selector, event.Virtual.Spec.Selector)
event.Virtual.Spec.Selector = translate.VirtualLabelsMap(event.Host.Spec.Selector, event.Virtual.Spec.Selector)
} else {
event.Host.Spec.Selector = translate.HostLabelsMap(event.Virtual.Spec.Selector, event.Host.Spec.Selector, event.Virtual.Namespace, false)
}
Expand Down
12 changes: 8 additions & 4 deletions pkg/controllers/resources/services/syncer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,12 @@ func TestSync(t *testing.T) {
Name: translate.Default.HostName(nil, "testservice", "testns").Name,
Namespace: "test",
Annotations: map[string]string{
translate.NameAnnotation: vObjectMeta.Name,
translate.NamespaceAnnotation: vObjectMeta.Namespace,
translate.UIDAnnotation: "",
translate.KindAnnotation: corev1.SchemeGroupVersion.WithKind("Service").String(),
translate.NameAnnotation: vObjectMeta.Name,
translate.NamespaceAnnotation: vObjectMeta.Namespace,
translate.UIDAnnotation: "",
translate.KindAnnotation: corev1.SchemeGroupVersion.WithKind("Service").String(),
translate.HostNamespaceAnnotation: "test",
translate.HostNameAnnotation: translate.Default.HostName(nil, "testservice", "testns").Name,
},
Labels: map[string]string{
translate.NamespaceLabel: vObjectMeta.Namespace,
Expand Down Expand Up @@ -88,6 +90,8 @@ func TestSync(t *testing.T) {
translate.NamespaceAnnotation: vObjectMeta.Namespace,
translate.UIDAnnotation: "",
translate.KindAnnotation: corev1.SchemeGroupVersion.WithKind("Service").String(),
translate.HostNamespaceAnnotation: pObjectMeta.Namespace,
translate.HostNameAnnotation: pObjectMeta.Name,
translate.ManagedAnnotationsAnnotation: "a",
"a": "b",
},
Expand Down
14 changes: 8 additions & 6 deletions pkg/controllers/resources/storageclasses/syncer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,10 @@ func TestSync(t *testing.T) {
translate.MarkerLabel: translate.VClusterName,
},
Annotations: map[string]string{
translate.NameAnnotation: "testsc",
translate.UIDAnnotation: "",
translate.KindAnnotation: storagev1.SchemeGroupVersion.WithKind("StorageClass").String(),
translate.NameAnnotation: "testsc",
translate.UIDAnnotation: "",
translate.KindAnnotation: storagev1.SchemeGroupVersion.WithKind("StorageClass").String(),
translate.HostNameAnnotation: translate.Default.HostNameCluster(vObjectMeta.Name),
},
},
Provisioner: "my-provisioner",
Expand All @@ -56,9 +57,10 @@ func TestSync(t *testing.T) {
translate.MarkerLabel: translate.VClusterName,
},
Annotations: map[string]string{
translate.NameAnnotation: "testsc",
translate.UIDAnnotation: "",
translate.KindAnnotation: storagev1.SchemeGroupVersion.WithKind("StorageClass").String(),
translate.NameAnnotation: "testsc",
translate.UIDAnnotation: "",
translate.KindAnnotation: storagev1.SchemeGroupVersion.WithKind("StorageClass").String(),
translate.HostNameAnnotation: translate.Default.HostNameCluster(vObjectMeta.Name),
},
},
Provisioner: "my-provisioner",
Expand Down
Loading

0 comments on commit 3bb7084

Please sign in to comment.