Skip to content

Commit

Permalink
feat: allow labels to be copied to delegate pods without prefix
Browse files Browse the repository at this point in the history
  • Loading branch information
marwanad committed Aug 13, 2024
1 parent b704bb7 commit 35b3f7e
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 3 deletions.
4 changes: 4 additions & 0 deletions pkg/common/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ var (

AnnotationKeyUseConstraintsFromSpecForProxyPodScheduling = KeyPrefix + "use-constraints-from-spec-for-proxy-pod-scheduling"

// AnnotationKeyDelegateLabelKeysToSkipPrefixing defines label keys that won't get prefixed with KeyPrefix
// on the delegate pod and retain the original label key
AnnotationKeyDelegateLabelKeysToSkipPrefixing = KeyPrefix + "label-keys-to-skip-prefixing"

// annotations on proxy pods (by mutating admission webhook)

KeyPrefixSourcePod = KeyPrefix + "sourcepod-"
Expand Down
2 changes: 1 addition & 1 deletion pkg/controllers/follow/service/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ func (r reconciler) Handle(obj interface{}) (requeueAfter *time.Duration, err er
svcCopy.Annotations[common.AnnotationKeyOriginalSelector] = originalSelector
}
if r.serviceRerouteEnabled {
selector, changed := delegatepod.ChangeLabels(svcCopy.Spec.Selector)
selector, changed := delegatepod.ChangeLabels(svcCopy.Spec.Selector, svc.Annotations[common.AnnotationKeyDelegateLabelKeysToSkipPrefixing])
if changed {
needUpdateLocal = true
svcCopy.Spec.Selector = selector
Expand Down
11 changes: 9 additions & 2 deletions pkg/model/delegatepod/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package delegatepod

import (
"slices"
"strings"

corev1 "k8s.io/api/core/v1"
Expand Down Expand Up @@ -48,7 +49,7 @@ func MakeDelegatePod(proxyPod *corev1.Pod, clusterName string) (*v1alpha1.PodCha
}
}

labels, _ := ChangeLabels(srcPod.Labels)
labels, _ := ChangeLabels(srcPod.Labels, srcPod.Annotations[common.AnnotationKeyDelegateLabelKeysToSkipPrefixing])

delegatePod := &v1alpha1.PodChaperon{
ObjectMeta: metav1.ObjectMeta{
Expand Down Expand Up @@ -90,11 +91,17 @@ func MakeDelegatePod(proxyPod *corev1.Pod, clusterName string) (*v1alpha1.PodCha
// The name segment is required and must be 63 characters or less"
// https://kubernetes.io/docs/concepts/overview/working-with-objects/labels/#syntax-and-character-set
// TODO: resolve conflict two keys have same name but different prefixes
func ChangeLabels(labels map[string]string) (map[string]string, bool) {
func ChangeLabels(labels map[string]string, labelKeysToSkipPrefixing string) (map[string]string, bool) {
changed := false
newLabels := make(map[string]string)
labelKeysToSkip := strings.Split(labelKeysToSkipPrefixing, ",")

for k, v := range labels {
keySplit := strings.Split(k, "/") // note: assume no empty key (enforced by Kubernetes)
if slices.Contains(labelKeysToSkip, k) {
newLabels[k] = v
continue
}
if len(keySplit) == 1 || keySplit[0] != common.KeyPrefix {
changed = true
newKey := common.KeyPrefix + keySplit[len(keySplit)-1]
Expand Down
71 changes: 71 additions & 0 deletions pkg/model/delegatepod/model_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
* Copyright The Multicluster-Scheduler Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package delegatepod

import (
"testing"

"github.com/stretchr/testify/require"
)

func TestChangeLabels(t *testing.T) {
tests := []struct {
name string
inputLabels map[string]string
labelKeysToSkipPrefixing string
outputLabels map[string]string
}{
{
name: "mix of domains, no skip",
inputLabels: map[string]string{
"foo.com/bar": "a",
"baz.com/foo": "b",
"multicluster.admiralty.io/baz": "c",
"a": "d",
},
labelKeysToSkipPrefixing: "",
outputLabels: map[string]string{
"multicluster.admiralty.io/bar": "a",
"multicluster.admiralty.io/foo": "b",
"multicluster.admiralty.io/baz": "c",
"multicluster.admiralty.io/a": "d",
},
},
{
name: "skip multiple domains",
inputLabels: map[string]string{
"foo.com/bar": "a",
"baz.com/foo": "b",
"multicluster.admiralty.io/foo": "c",
"a": "d",
},
labelKeysToSkipPrefixing: "foo.com/bar,baz.com/foo",
outputLabels: map[string]string{
"foo.com/bar": "a",
"baz.com/foo": "b",
"multicluster.admiralty.io/foo": "c",
"multicluster.admiralty.io/a": "d",
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
out, _ := ChangeLabels(tt.inputLabels, tt.labelKeysToSkipPrefixing)
require.Equal(t, tt.outputLabels, out)
})
}
}

0 comments on commit 35b3f7e

Please sign in to comment.