Skip to content

Commit

Permalink
Merge pull request #1457 from FabianKramm/pluginv2
Browse files Browse the repository at this point in the history
refactor: improve plugin v2
  • Loading branch information
FabianKramm authored Jan 16, 2024
2 parents ccf6fd9 + 464d9f2 commit fb72265
Show file tree
Hide file tree
Showing 43 changed files with 392 additions and 548 deletions.
8 changes: 8 additions & 0 deletions .goreleaser.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,14 @@ archives:
builds_info:
group: root
owner: root
- id: syncer_archives
format: binary
builds:
- vcluster
name_template: "syncer-{{ .Os }}-{{ .Arch }}"
builds_info:
group: root
owner: root

sboms:
- id: vcluster_sbom
Expand Down
14 changes: 7 additions & 7 deletions cmd/vcluster/cmd/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,17 +76,17 @@ func NewStartCommand() *cobra.Command {

func ExecuteStart(ctx context.Context, options *options.VirtualClusterOptions) error {
// set suffix
translate.Suffix = options.Name
if translate.Suffix == "" {
translate.Suffix = options.DeprecatedSuffix
translate.VClusterName = options.Name
if translate.VClusterName == "" {
translate.VClusterName = options.DeprecatedSuffix
}
if translate.Suffix == "" {
translate.Suffix = "vcluster"
if translate.VClusterName == "" {
translate.VClusterName = "vcluster"
}

// set service name
if options.ServiceName == "" {
options.ServiceName = translate.Suffix
options.ServiceName = translate.VClusterName
}

// get current namespace
Expand Down Expand Up @@ -115,7 +115,7 @@ func ExecuteStart(ctx context.Context, options *options.VirtualClusterOptions) e
inClusterClient,
currentNamespace,
currentNamespace,
translate.Suffix,
translate.VClusterName,
options,
)
if err != nil {
Expand Down
77 changes: 0 additions & 77 deletions hack/build-cli.sh

This file was deleted.

2 changes: 1 addition & 1 deletion pkg/controllers/generic/import_syncer.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ func (s *importer) ExcludePhysical(pObj client.Object) bool {
func (s *importer) excludeObject(obj client.Object) bool {
// check if back sync is disabled eg. for service account token secrets
if obj.GetAnnotations() != nil &&
obj.GetAnnotations()[translate.SkipBacksyncInMultiNamespaceMode] == "true" {
obj.GetAnnotations()[translate.SkipBackSyncInMultiNamespaceMode] == "true" {
return true
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/controllers/manifests/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ type InitManifestsConfigMapReconciler struct {

func (r *InitManifestsConfigMapReconciler) Reconcile(ctx context.Context, req ctrl.Request) (result ctrl.Result, err error) {
// TODO: implement better filtration through predicates
if req.Name != translate.Suffix+InitManifestSuffix {
if req.Name != translate.VClusterName+InitManifestSuffix {
return ctrl.Result{}, nil
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/controllers/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ func RegisterInitManifestsController(controllerCtx *options.ControllerContext) e
result, err := controller.Reconcile(ctx, ctrl.Request{
NamespacedName: types.NamespacedName{
Namespace: controllerCtx.CurrentNamespace,
Name: translate.Suffix + manifests.InitManifestSuffix,
Name: translate.VClusterName + manifests.InitManifestSuffix,
},
})
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion pkg/controllers/resources/configmaps/syncer.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ type configMapSyncer struct {
func ConfigMapNameTranslator(vNN types.NamespacedName, _ client.Object) string {
name := translate.Default.PhysicalName(vNN.Name, vNN.Namespace)
if name == "kube-root-ca.crt" {
name = translate.SafeConcatName("vcluster", "kube-root-ca.crt", "x", translate.Suffix)
name = translate.SafeConcatName("vcluster", "kube-root-ca.crt", "x", translate.VClusterName)
}
return name
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/controllers/resources/ingressclasses/syncer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func TestSync(t *testing.T) {
ObjectMeta: metav1.ObjectMeta{
Name: vObjectMeta.Name,
Labels: map[string]string{
translate.MarkerLabel: translate.Suffix,
translate.MarkerLabel: translate.VClusterName,
},
Annotations: map[string]string{
translate.NameAnnotation: "test-ingc",
Expand Down Expand Up @@ -62,7 +62,7 @@ func TestSync(t *testing.T) {
ObjectMeta: metav1.ObjectMeta{
Name: translate.Default.PhysicalNameClusterScoped(vObjectMeta.Name),
Labels: map[string]string{
translate.MarkerLabel: translate.Suffix,
translate.MarkerLabel: translate.VClusterName,
},
Annotations: map[string]string{
translate.NameAnnotation: "test-ingc",
Expand Down
2 changes: 1 addition & 1 deletion pkg/controllers/resources/ingresses/legacy/syncer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ func TestSync(t *testing.T) {
translate.UIDAnnotation: "",
},
Labels: map[string]string{
translate.MarkerLabel: translate.Suffix,
translate.MarkerLabel: translate.VClusterName,
translate.NamespaceLabel: vObjectMeta.Namespace,
},
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/controllers/resources/ingresses/syncer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ func TestSync(t *testing.T) {
translate.UIDAnnotation: "",
},
Labels: map[string]string{
translate.MarkerLabel: translate.Suffix,
translate.MarkerLabel: translate.VClusterName,
translate.NamespaceLabel: vObjectMeta.Namespace,
},
}
Expand Down
10 changes: 5 additions & 5 deletions pkg/controllers/resources/networkpolicies/syncer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func TestSync(t *testing.T) {
MatchLabels: map[string]string{
translate.Default.ConvertLabelKey("mykey"): "mylabel",
translate.NamespaceLabel: vObjectMeta.Namespace,
translate.MarkerLabel: translate.Suffix,
translate.MarkerLabel: translate.VClusterName,
},
MatchExpressions: []metav1.LabelSelectorRequirement{
{
Expand All @@ -72,7 +72,7 @@ func TestSync(t *testing.T) {
translate.UIDAnnotation: "",
},
Labels: map[string]string{
translate.MarkerLabel: translate.Suffix,
translate.MarkerLabel: translate.VClusterName,
translate.NamespaceLabel: vObjectMeta.Namespace,
},
}
Expand All @@ -92,7 +92,7 @@ func TestSync(t *testing.T) {
pnetworkPolicyNoPodSelector.Spec.PodSelector = metav1.LabelSelector{
MatchLabels: map[string]string{
translate.NamespaceLabel: vObjectMeta.Namespace,
translate.MarkerLabel: translate.Suffix,
translate.MarkerLabel: translate.VClusterName,
},
}

Expand Down Expand Up @@ -125,7 +125,7 @@ func TestSync(t *testing.T) {
From: []networkingv1.NetworkPolicyPeer{{PodSelector: &metav1.LabelSelector{
MatchLabels: map[string]string{
translate.Default.ConvertLabelKey("random-key"): "value",
translate.MarkerLabel: translate.Suffix,
translate.MarkerLabel: translate.VClusterName,
translate.NamespaceLabel: vnetworkPolicyWithPodSelectorNoNs.GetNamespace(),
},
MatchExpressions: []metav1.LabelSelectorRequirement{},
Expand Down Expand Up @@ -196,7 +196,7 @@ func TestSync(t *testing.T) {
From: []networkingv1.NetworkPolicyPeer{{
PodSelector: &metav1.LabelSelector{
MatchLabels: map[string]string{
translate.MarkerLabel: translate.Suffix,
translate.MarkerLabel: translate.VClusterName,
},
MatchExpressions: []metav1.LabelSelectorRequirement{
{
Expand Down
4 changes: 2 additions & 2 deletions pkg/controllers/resources/networkpolicies/translate.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func translateSpec(spec *networkingv1.NetworkPolicySpec, namespace string) *netw
// add selector for namespace as NetworkPolicy podSelector applies to pods within it's namespace
outSpec.PodSelector.MatchLabels[translate.NamespaceLabel] = namespace
// add selector for the marker label to select only from pods belonging this vcluster instance
outSpec.PodSelector.MatchLabels[translate.MarkerLabel] = translate.Suffix
outSpec.PodSelector.MatchLabels[translate.MarkerLabel] = translate.VClusterName

outSpec.PolicyTypes = spec.PolicyTypes
return outSpec
Expand All @@ -99,7 +99,7 @@ func translateNetworkPolicyPeers(peers []networkingv1.NetworkPolicyPeer, namespa
newPeer.PodSelector.MatchLabels[translate.NamespaceLabel] = namespace
}
// add selector for the marker label to select only from pods belonging this vcluster instance
newPeer.PodSelector.MatchLabels[translate.MarkerLabel] = translate.Suffix
newPeer.PodSelector.MatchLabels[translate.MarkerLabel] = translate.VClusterName
} else {
newPeer.IPBlock = peer.IPBlock.DeepCopy()
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/controllers/resources/nodes/fake_syncer.go
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@ func GetNodeHost(nodeName string) string {
// GetNodeHostLegacy returns Node hostname in a format used in 0.14.x release.
// This function is added for backwards compatibility and may be removed in a future release.
func GetNodeHostLegacy(nodeName, currentNamespace string) string {
hostname := strings.ReplaceAll(nodeName, ".", "-") + "." + translate.Suffix + "." + currentNamespace + "." + constants.NodeSuffix
hostname := strings.ReplaceAll(nodeName, ".", "-") + "." + translate.VClusterName + "." + currentNamespace + "." + constants.NodeSuffix
log := loghelper.New("GetNodeHostLegacy()")
log.Debugf("translating nodename %q into hostname: %q", nodeName, hostname)
return hostname
Expand Down
6 changes: 3 additions & 3 deletions pkg/controllers/resources/nodes/nodeservice/node_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func (n *nodeServiceProvider) cleanupNodeServices(ctx context.Context) error {

serviceList := &corev1.ServiceList{}
err := n.currentNamespaceClient.List(ctx, serviceList, client.InNamespace(n.currentNamespace), client.MatchingLabels{
ServiceClusterLabel: translate.Suffix,
ServiceClusterLabel: translate.VClusterName,
})
if err != nil {
return errors.Wrap(err, "list services")
Expand Down Expand Up @@ -124,7 +124,7 @@ func (n *nodeServiceProvider) Unlock() {
}

func (n *nodeServiceProvider) GetNodeIP(ctx context.Context, name string) (string, error) {
serviceName := translate.SafeConcatName(translate.Suffix, "node", strings.ReplaceAll(name, ".", "-"))
serviceName := translate.SafeConcatName(translate.VClusterName, "node", strings.ReplaceAll(name, ".", "-"))

service := &corev1.Service{}
err := n.currentNamespaceClient.Get(ctx, types.NamespacedName{Name: serviceName, Namespace: n.currentNamespace}, service)
Expand Down Expand Up @@ -158,7 +158,7 @@ func (n *nodeServiceProvider) GetNodeIP(ctx context.Context, name string) (strin
Namespace: n.currentNamespace,
Name: serviceName,
Labels: map[string]string{
ServiceClusterLabel: translate.Suffix,
ServiceClusterLabel: translate.VClusterName,
ServiceNodeLabel: name,
},
},
Expand Down
2 changes: 1 addition & 1 deletion pkg/controllers/resources/nodes/syncer.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ var _ syncertypes.ControllerModifier = &nodeSyncer{}

func (s *nodeSyncer) ModifyController(ctx *synccontext.RegisterContext, bld *builder.Builder) (*builder.Builder, error) {
if s.enableScheduler {
notManagedSelector, err := labels.NewRequirement(translate.MarkerLabel, selection.NotEquals, []string{translate.Suffix})
notManagedSelector, err := labels.NewRequirement(translate.MarkerLabel, selection.NotEquals, []string{translate.VClusterName})
if err != nil {
return bld, fmt.Errorf("constructing label selector for non-vcluster pods: %w", err)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func TestSync(t *testing.T) {
translate.UIDAnnotation: "",
},
Labels: map[string]string{
translate.MarkerLabel: translate.Suffix,
translate.MarkerLabel: translate.VClusterName,
translate.NamespaceLabel: vObjectMeta.Namespace,
},
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func TestSync(t *testing.T) {
},
Labels: map[string]string{
translate.NamespaceLabel: vObjectMeta.Namespace,
translate.MarkerLabel: translate.Suffix,
translate.MarkerLabel: translate.VClusterName,
},
ResourceVersion: generictesting.FakeClientResourceVersion,
}
Expand Down
6 changes: 3 additions & 3 deletions pkg/controllers/resources/pods/syncer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func TestSync(t *testing.T) {
ClusterIP: "1.2.3.4",
},
}
translate.Suffix = generictesting.DefaultTestVclusterName
translate.VClusterName = generictesting.DefaultTestVclusterName
pDNSService := corev1.Service{
ObjectMeta: metav1.ObjectMeta{
Name: translate.Default.PhysicalName("kube-dns", "kube-system"),
Expand Down Expand Up @@ -104,7 +104,7 @@ func TestSync(t *testing.T) {
},
Labels: map[string]string{
translate.NamespaceLabel: vObjectMeta.Namespace,
translate.MarkerLabel: translate.Suffix,
translate.MarkerLabel: translate.VClusterName,
},
}
pPodBase := &corev1.Pod{
Expand Down Expand Up @@ -248,7 +248,7 @@ func TestSync(t *testing.T) {
},
Labels: map[string]string{
translate.NamespaceLabel: vHostPathPod.Namespace,
translate.MarkerLabel: translate.Suffix,
translate.MarkerLabel: translate.VClusterName,
},
// CreationTimestamp: metav1.Time{},
// ResourceVersion: "999",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func SATokenSecret(ctx context.Context, pClient client.Client, vPod *corev1.Pod,
Namespace: translate.Default.PhysicalNamespace(vPod.Namespace),

Annotations: map[string]string{
translate.SkipBacksyncInMultiNamespaceMode: "true",
translate.SkipBackSyncInMultiNamespaceMode: "true",
},
},
Type: corev1.SecretTypeOpaque,
Expand Down
6 changes: 3 additions & 3 deletions pkg/controllers/resources/pods/translate/translator.go
Original file line number Diff line number Diff line change
Expand Up @@ -450,7 +450,7 @@ func (t *translator) translateProjectedVolume(
if projectedVolume.Sources[i].ConfigMap != nil {
projectedVolume.Sources[i].ConfigMap.Name = translate.Default.PhysicalName(projectedVolume.Sources[i].ConfigMap.Name, vPod.Namespace)
if projectedVolume.Sources[i].ConfigMap.Name == "kube-root-ca.crt" {
projectedVolume.Sources[i].ConfigMap.Name = translate.SafeConcatName("vcluster", "kube-root-ca.crt", "x", translate.Suffix)
projectedVolume.Sources[i].ConfigMap.Name = translate.SafeConcatName("vcluster", "kube-root-ca.crt", "x", translate.VClusterName)
}
}
if projectedVolume.Sources[i].DownwardAPI != nil {
Expand Down Expand Up @@ -782,7 +782,7 @@ func (t *translator) translatePodAffinityTerm(vPod *corev1.Pod, term corev1.PodA
if newAffinityTerm.LabelSelector.MatchLabels == nil {
newAffinityTerm.LabelSelector.MatchLabels = map[string]string{}
}
newAffinityTerm.LabelSelector.MatchLabels[translate.MarkerLabel] = translate.Suffix
newAffinityTerm.LabelSelector.MatchLabels[translate.MarkerLabel] = translate.VClusterName
}
return newAffinityTerm
}
Expand All @@ -797,7 +797,7 @@ func translateTopologySpreadConstraints(vPod *corev1.Pod, pPod *corev1.Pod) {
pPod.Spec.TopologySpreadConstraints[i].LabelSelector.MatchLabels = map[string]string{}
}
pPod.Spec.TopologySpreadConstraints[i].LabelSelector.MatchLabels[translate.NamespaceLabel] = vPod.Namespace
pPod.Spec.TopologySpreadConstraints[i].LabelSelector.MatchLabels[translate.MarkerLabel] = translate.Suffix
pPod.Spec.TopologySpreadConstraints[i].LabelSelector.MatchLabels[translate.MarkerLabel] = translate.VClusterName
}
}
}
Expand Down
Loading

0 comments on commit fb72265

Please sign in to comment.