Skip to content

Commit

Permalink
Push some local test cleanups (istio#50653)
Browse files Browse the repository at this point in the history
* Use `Label` suffixes

Signed-off-by: Benjamin Leggett <benjamin.leggett@solo.io>

* Tidy test annotation handling

Signed-off-by: Benjamin Leggett <benjamin.leggett@solo.io>

* Lints

Signed-off-by: Benjamin Leggett <benjamin.leggett@solo.io>

---------

Signed-off-by: Benjamin Leggett <benjamin.leggett@solo.io>
  • Loading branch information
bleggett committed May 3, 2024
1 parent 595c772 commit a63892b
Show file tree
Hide file tree
Showing 48 changed files with 223 additions and 397 deletions.
4 changes: 2 additions & 2 deletions cni/pkg/nodeagent/cni-watcher_test.go
Expand Up @@ -118,7 +118,7 @@ func TestCNIPluginServer(t *testing.T) {

// label the namespace
labelsPatch := []byte(fmt.Sprintf(`{"metadata":{"labels":{"%s":"%s"}}}`,
constants.DataplaneMode, constants.DataplaneModeAmbient))
constants.DataplaneModeLabel, constants.DataplaneModeAmbient))
_, err := client.Kube().CoreV1().Namespaces().Patch(ctx, ns.Name,
types.MergePatchType, labelsPatch, metav1.PatchOptions{})
assert.NoError(t, err)
Expand Down Expand Up @@ -194,7 +194,7 @@ func TestCNIPluginServerPrefersCNIProvidedPodIP(t *testing.T) {

// label the namespace
labelsPatch := []byte(fmt.Sprintf(`{"metadata":{"labels":{"%s":"%s"}}}`,
constants.DataplaneMode, constants.DataplaneModeAmbient))
constants.DataplaneModeLabel, constants.DataplaneModeAmbient))
_, err := client.Kube().CoreV1().Namespaces().Patch(ctx, ns.Name,
types.MergePatchType, labelsPatch, metav1.PatchOptions{})
assert.NoError(t, err)
Expand Down
4 changes: 2 additions & 2 deletions cni/pkg/nodeagent/informers.go
Expand Up @@ -119,7 +119,7 @@ func (s *InformerHandlers) GetAmbientPods() []*corev1.Pod {
func (s *InformerHandlers) enqueueNamespace(o controllers.Object) {
namespace := o.GetName()
labels := o.GetLabels()
matchAmbient := labels[constants.DataplaneMode] == constants.DataplaneModeAmbient
matchAmbient := labels[constants.DataplaneModeLabel] == constants.DataplaneModeAmbient
if matchAmbient {
log.Infof("Namespace %s is enabled in ambient mesh", namespace)
} else {
Expand Down Expand Up @@ -177,7 +177,7 @@ func getModeLabel(m map[string]string) string {
if m == nil {
return ""
}
return m[constants.DataplaneMode]
return m[constants.DataplaneModeLabel]
}

func (s *InformerHandlers) reconcilePod(input any) error {
Expand Down
24 changes: 12 additions & 12 deletions cni/pkg/nodeagent/informers_test.go
Expand Up @@ -76,7 +76,7 @@ func TestExistingPodAddedWhenNsLabeled(t *testing.T) {

// label the namespace
labelsPatch := []byte(fmt.Sprintf(`{"metadata":{"labels":{"%s":"%s"}}}`,
constants.DataplaneMode, constants.DataplaneModeAmbient))
constants.DataplaneModeLabel, constants.DataplaneModeAmbient))
_, err := client.Kube().CoreV1().Namespaces().Patch(ctx, ns.Name,
types.MergePatchType, labelsPatch, metav1.PatchOptions{})
assert.NoError(t, err)
Expand Down Expand Up @@ -139,7 +139,7 @@ func TestExistingPodAddedWhenDualStack(t *testing.T) {

// label the namespace
labelsPatch := []byte(fmt.Sprintf(`{"metadata":{"labels":{"%s":"%s"}}}`,
constants.DataplaneMode, constants.DataplaneModeAmbient))
constants.DataplaneModeLabel, constants.DataplaneModeAmbient))
_, err := client.Kube().CoreV1().Namespaces().Patch(ctx, ns.Name,
types.MergePatchType, labelsPatch, metav1.PatchOptions{})
assert.NoError(t, err)
Expand Down Expand Up @@ -190,7 +190,7 @@ func TestExistingPodNotAddedIfNoIPInAnyStatusField(t *testing.T) {

// label the namespace
labelsPatch := []byte(fmt.Sprintf(`{"metadata":{"labels":{"%s":"%s"}}}`,
constants.DataplaneMode, constants.DataplaneModeAmbient))
constants.DataplaneModeLabel, constants.DataplaneModeAmbient))
_, err := client.Kube().CoreV1().Namespaces().Patch(ctx, ns.Name,
types.MergePatchType, labelsPatch, metav1.PatchOptions{})
assert.NoError(t, err)
Expand Down Expand Up @@ -227,7 +227,7 @@ func TestExistingPodRemovedWhenNsUnlabeled(t *testing.T) {
ns := &corev1.Namespace{
ObjectMeta: metav1.ObjectMeta{Name: "test"},
// TODO: once we if the add pod bug, re-enable this and remove the patch below
// Labels: map[string]string{constants.DataplaneMode: constants.DataplaneModeAmbient},
// Labels: map[string]string{constants.DataplaneModeLabel: constants.DataplaneModeAmbient},

}

Expand Down Expand Up @@ -258,7 +258,7 @@ func TestExistingPodRemovedWhenNsUnlabeled(t *testing.T) {
log.Debug("labeling namespace")
_, err := client.Kube().CoreV1().Namespaces().Patch(ctx, ns.Name,
types.MergePatchType, []byte(fmt.Sprintf(`{"metadata":{"labels":{"%s":"%s"}}}`,
constants.DataplaneMode, constants.DataplaneModeAmbient)), metav1.PatchOptions{})
constants.DataplaneModeLabel, constants.DataplaneModeAmbient)), metav1.PatchOptions{})
assert.NoError(t, err)

// wait for an update event
Expand All @@ -279,7 +279,7 @@ func TestExistingPodRemovedWhenNsUnlabeled(t *testing.T) {

// unlabel the namespace
labelsPatch := []byte(fmt.Sprintf(`{"metadata":{"labels":{"%s":null}}}`,
constants.DataplaneMode))
constants.DataplaneModeLabel))
_, err = client.Kube().CoreV1().Namespaces().Patch(ctx, ns.Name,
types.MergePatchType, labelsPatch, metav1.PatchOptions{})
assert.NoError(t, err)
Expand Down Expand Up @@ -319,7 +319,7 @@ func TestExistingPodRemovedWhenPodAnnotated(t *testing.T) {
ns := &corev1.Namespace{
ObjectMeta: metav1.ObjectMeta{Name: "test"},
// TODO: once we if the add pod bug, re-enable this and remove the patch below
// Labels: map[string]string{constants.DataplaneMode: constants.DataplaneModeAmbient},
// Labels: map[string]string{constants.DataplaneModeLabel: constants.DataplaneModeAmbient},

}

Expand Down Expand Up @@ -350,7 +350,7 @@ func TestExistingPodRemovedWhenPodAnnotated(t *testing.T) {
log.Debug("labeling namespace")
_, err := client.Kube().CoreV1().Namespaces().Patch(ctx, ns.Name,
types.MergePatchType, []byte(fmt.Sprintf(`{"metadata":{"labels":{"%s":"%s"}}}`,
constants.DataplaneMode, constants.DataplaneModeAmbient)), metav1.PatchOptions{})
constants.DataplaneModeLabel, constants.DataplaneModeAmbient)), metav1.PatchOptions{})
assert.NoError(t, err)

// wait for an update event
Expand Down Expand Up @@ -421,7 +421,7 @@ func TestAmbientEnabledReturnsPodIfEnabled(t *testing.T) {
ns := &corev1.Namespace{
ObjectMeta: metav1.ObjectMeta{
Name: "test",
Labels: map[string]string{constants.DataplaneMode: constants.DataplaneModeAmbient},
Labels: map[string]string{constants.DataplaneModeLabel: constants.DataplaneModeAmbient},
},
}

Expand Down Expand Up @@ -464,7 +464,7 @@ func TestAmbientEnabledReturnsNoPodIfNotEnabled(t *testing.T) {
ns := &corev1.Namespace{
ObjectMeta: metav1.ObjectMeta{
Name: "test",
Labels: map[string]string{constants.DataplaneMode: constants.DataplaneModeAmbient},
Labels: map[string]string{constants.DataplaneModeLabel: constants.DataplaneModeAmbient},
},
}

Expand Down Expand Up @@ -508,7 +508,7 @@ func TestAmbientEnabledReturnsErrorIfBogusNS(t *testing.T) {
ns := &corev1.Namespace{
ObjectMeta: metav1.ObjectMeta{
Name: "test",
Labels: map[string]string{constants.DataplaneMode: constants.DataplaneModeAmbient},
Labels: map[string]string{constants.DataplaneModeLabel: constants.DataplaneModeAmbient},
},
}

Expand Down Expand Up @@ -552,7 +552,7 @@ func TestExistingPodAddedWhenItPreExists(t *testing.T) {
ns := &corev1.Namespace{
ObjectMeta: metav1.ObjectMeta{
Name: "test",
Labels: map[string]string{constants.DataplaneMode: constants.DataplaneModeAmbient},
Labels: map[string]string{constants.DataplaneModeLabel: constants.DataplaneModeAmbient},
},
}

Expand Down
16 changes: 8 additions & 8 deletions cni/pkg/plugin/plugin_test.go
Expand Up @@ -232,7 +232,7 @@ func TestCmdAddAmbientEnabledOnNS(t *testing.T) {
cniConf := buildMockConf(true, url)

pod, ns := buildFakePodAndNSForClient()
ns.ObjectMeta.Labels = map[string]string{constants.DataplaneMode: constants.DataplaneModeAmbient}
ns.ObjectMeta.Labels = map[string]string{constants.DataplaneModeLabel: constants.DataplaneModeAmbient}

testDoAddRun(t, cniConf, testNSName, pod, ns)

Expand All @@ -247,7 +247,7 @@ func TestCmdAddAmbientEnabledOnNSServerFails(t *testing.T) {
cniConf := buildMockConf(true, url)

pod, ns := buildFakePodAndNSForClient()
ns.ObjectMeta.Labels = map[string]string{constants.DataplaneMode: constants.DataplaneModeAmbient}
ns.ObjectMeta.Labels = map[string]string{constants.DataplaneModeLabel: constants.DataplaneModeAmbient}

testCmdAddExpectFail(t, cniConf, pod, ns)

Expand All @@ -268,7 +268,7 @@ func TestCmdAddPodWithProxySidecarAmbientEnabledNS(t *testing.T) {

pod.Spec.Containers = []corev1.Container{app, proxy}
pod.ObjectMeta.Annotations = map[string]string{annotation.SidecarStatus.Name: "true"}
ns.ObjectMeta.Labels = map[string]string{constants.DataplaneMode: constants.DataplaneModeAmbient}
ns.ObjectMeta.Labels = map[string]string{constants.DataplaneModeLabel: constants.DataplaneModeAmbient}

testDoAddRun(t, cniConf, testNSName, pod, ns)

Expand All @@ -288,7 +288,7 @@ func TestCmdAddPodWithGenericSidecar(t *testing.T) {
app := corev1.Container{Name: "app"}

pod.Spec.Containers = []corev1.Container{app, proxy}
ns.ObjectMeta.Labels = map[string]string{constants.DataplaneMode: constants.DataplaneModeAmbient}
ns.ObjectMeta.Labels = map[string]string{constants.DataplaneModeLabel: constants.DataplaneModeAmbient}

testDoAddRun(t, cniConf, testNSName, pod, ns)

Expand All @@ -305,8 +305,8 @@ func TestCmdAddPodDisabledAnnotation(t *testing.T) {
pod, ns := buildFakePodAndNSForClient()

app := corev1.Container{Name: "app"}
ns.ObjectMeta.Labels = map[string]string{constants.DataplaneMode: constants.AmbientRedirectionEnabled}
pod.ObjectMeta.Annotations = map[string]string{constants.DataplaneMode: constants.AmbientRedirectionDisabled}
ns.ObjectMeta.Labels = map[string]string{constants.DataplaneModeLabel: constants.AmbientRedirectionEnabled}
pod.ObjectMeta.Annotations = map[string]string{constants.DataplaneModeLabel: constants.AmbientRedirectionDisabled}
pod.Spec.Containers = []corev1.Container{app}

testDoAddRun(t, cniConf, testNSName, pod, ns)
Expand All @@ -324,7 +324,7 @@ func TestCmdAddPodEnabledNamespaceDisabled(t *testing.T) {
pod, ns := buildFakePodAndNSForClient()

app := corev1.Container{Name: "app"}
pod.ObjectMeta.Annotations = map[string]string{constants.DataplaneMode: constants.AmbientRedirectionEnabled}
pod.ObjectMeta.Annotations = map[string]string{constants.DataplaneModeLabel: constants.AmbientRedirectionEnabled}
pod.Spec.Containers = []corev1.Container{app}

testDoAddRun(t, cniConf, testNSName, pod, ns)
Expand All @@ -345,7 +345,7 @@ func TestCmdAddPodInExcludedNamespace(t *testing.T) {

app := corev1.Container{Name: "app"}
ns.ObjectMeta.Name = excludedNS
ns.ObjectMeta.Labels = map[string]string{constants.DataplaneMode: constants.AmbientRedirectionEnabled}
ns.ObjectMeta.Labels = map[string]string{constants.DataplaneModeLabel: constants.AmbientRedirectionEnabled}

pod.ObjectMeta.Namespace = excludedNS
pod.Spec.Containers = []corev1.Container{app}
Expand Down
4 changes: 2 additions & 2 deletions cni/pkg/util/podutil.go
Expand Up @@ -44,8 +44,8 @@ var annotationRemovePatch = []byte(fmt.Sprintf(
// PodRedirectionEnabled determines if a pod should or should not be configured
// to have traffic redirected thru the node proxy.
func PodRedirectionEnabled(namespace *corev1.Namespace, pod *corev1.Pod) bool {
if !(namespace.GetLabels()[constants.DataplaneMode] == constants.DataplaneModeAmbient ||
pod.GetLabels()[constants.DataplaneMode] == constants.DataplaneModeAmbient) {
if !(namespace.GetLabels()[constants.DataplaneModeLabel] == constants.DataplaneModeAmbient ||
pod.GetLabels()[constants.DataplaneModeLabel] == constants.DataplaneModeAmbient) {
// Neither namespace nor pod has ambient mode enabled
return false
}
Expand Down
2 changes: 1 addition & 1 deletion cni/pkg/util/podutil_test.go
Expand Up @@ -80,7 +80,7 @@ func TestGetPodIPsIfNoPodIPPresent(t *testing.T) {

func TestPodRedirectionEnabled(t *testing.T) {
var (
ambientEnabledLabel = map[string]string{constants.DataplaneMode: constants.DataplaneModeAmbient}
ambientEnabledLabel = map[string]string{constants.DataplaneModeLabel: constants.DataplaneModeAmbient}
ambientDisabledAnnotation = map[string]string{constants.AmbientRedirection: constants.AmbientRedirectionDisabled}
sidecarStatusAnnotation = map[string]string{annotation.SidecarStatus.Name: "test"}

Expand Down
6 changes: 3 additions & 3 deletions istioctl/pkg/injector/injector-list_test.go
Expand Up @@ -89,11 +89,11 @@ func Test_getNamespaces(t *testing.T) {
createNamespace("kube-system", nil),
createNamespace("istio-system", nil),
createNamespace("ambient", map[string]string{
constants.DataplaneMode: constants.DataplaneModeAmbient,
constants.DataplaneModeLabel: constants.DataplaneModeAmbient,
}),
createNamespace("no-ambient", map[string]string{
constants.DataplaneMode: constants.DataplaneModeAmbient,
"istio-injection": "enabled",
constants.DataplaneModeLabel: constants.DataplaneModeAmbient,
"istio-injection": "enabled",
}),
}

Expand Down
2 changes: 1 addition & 1 deletion istioctl/pkg/util/ambient/util.go
Expand Up @@ -56,7 +56,7 @@ func InAmbient(obj metav1.Object) bool {
if v, ok := t.GetLabels()[label.IoIstioRev.Name]; ok && v != "" {
return false
}
return t.GetLabels()[constants.DataplaneMode] == constants.DataplaneModeAmbient
return t.GetLabels()[constants.DataplaneModeLabel] == constants.DataplaneModeAmbient
}
return false
}
9 changes: 5 additions & 4 deletions istioctl/pkg/waypoint/waypoint.go
Expand Up @@ -95,7 +95,7 @@ func Cmd(ctx cli.Context) *cobra.Command {
if gw.Labels == nil {
gw.Labels = map[string]string{}
}
gw.Labels[constants.AmbientWaypointForTrafficType] = trafficType
gw.Labels[constants.AmbientWaypointForTrafficTypeLabel] = trafficType

if revision != "" {
gw.Labels = map[string]string{label.IoIstioRev.Name: revision}
Expand Down Expand Up @@ -219,7 +219,8 @@ func Cmd(ctx cli.Context) *cobra.Command {
if err != nil {
return fmt.Errorf("failed to label namespace with waypoint: %v", err)
}
fmt.Fprintf(cmd.OutOrStdout(), "namespace %v labeled with \"%v: %v\"\n", ctx.NamespaceOrDefault(ctx.Namespace()), constants.AmbientUseWaypoint, gw.Name)
fmt.Fprintf(cmd.OutOrStdout(), "namespace %v labeled with \"%v: %v\"\n", ctx.NamespaceOrDefault(ctx.Namespace()),
constants.AmbientUseWaypointLabel, gw.Name)
}
return nil
},
Expand Down Expand Up @@ -432,7 +433,7 @@ func labelNamespaceWithWaypoint(kubeClient kube.CLIClient, ns string) error {
if nsObj.Labels == nil {
nsObj.Labels = map[string]string{}
}
nsObj.Labels[constants.AmbientUseWaypoint] = waypointName
nsObj.Labels[constants.AmbientUseWaypointLabel] = waypointName
if _, err := kubeClient.Kube().CoreV1().Namespaces().Update(context.Background(), nsObj, metav1.UpdateOptions{}); err != nil {
return fmt.Errorf("failed to update namespace %s: %v", ns, err)
}
Expand All @@ -449,5 +450,5 @@ func namespaceIsLabeledAmbient(kubeClient kube.CLIClient, ns string) (bool, erro
if nsObj.Labels == nil {
return false, nil
}
return nsObj.Labels[constants.DataplaneMode] == constants.DataplaneModeAmbient, nil
return nsObj.Labels[constants.DataplaneModeLabel] == constants.DataplaneModeAmbient, nil
}

0 comments on commit a63892b

Please sign in to comment.