istio
395 строк · 11.7 Кб
1//go:build integ
2// +build integ
3
4// Copyright Istio Authors
5//
6// Licensed under the Apache License, Version 2.0 (the "License");
7// you may not use this file except in compliance with the License.
8// You may obtain a copy of the License at
9//
10// http://www.apache.org/licenses/LICENSE-2.0
11//
12// Unless required by applicable law or agreed to in writing, software
13// distributed under the License is distributed on an "AS IS" BASIS,
14// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15// See the License for the specific language governing permissions and
16// limitations under the License.
17
18package ambient19
20import (21"context"22"strings"23"testing"24
25kerrors "k8s.io/apimachinery/pkg/api/errors"26metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"27
28"istio.io/istio/pkg/config/constants"29"istio.io/istio/pkg/test/framework"30"istio.io/istio/pkg/test/framework/components/ambient"31"istio.io/istio/pkg/test/framework/components/echo"32"istio.io/istio/pkg/test/framework/components/echo/common/ports"33"istio.io/istio/pkg/test/framework/components/echo/deployment"34"istio.io/istio/pkg/test/framework/components/echo/match"35"istio.io/istio/pkg/test/framework/components/istio"36"istio.io/istio/pkg/test/framework/components/namespace"37"istio.io/istio/pkg/test/framework/components/prometheus"38"istio.io/istio/pkg/test/framework/label"39"istio.io/istio/pkg/test/framework/resource"40"istio.io/istio/pkg/test/scopes"41"istio.io/istio/tests/integration/security/util/cert"42)
43
44var (45i istio.Instance46
47// Below are various preconfigured echo deployments. Whenever possible, tests should utilize these48// to avoid excessive creation/tear down of deployments. In general, a test should only deploy echo if49// its doing something unique to that specific test.50apps = &EchoDeployments{}51
52// used to validate telemetry in-cluster53prom prometheus.Instance54)
55
56type EchoDeployments struct {57// Namespace echo apps will be deployed58Namespace namespace.Instance59AllWaypoint echo.Instances60WorkloadAddressedWaypoint echo.Instances61ServiceAddressedWaypoint echo.Instances62Captured echo.Instances63Uncaptured echo.Instances64SidecarWaypoint echo.Instances65SidecarCaptured echo.Instances66SidecarUncaptured echo.Instances67All echo.Instances68Mesh echo.Instances69MeshExternal echo.Instances70
71WaypointProxies map[string]ambient.WaypointProxy72}
73
74// TestMain defines the entrypoint for pilot tests using a standard Istio installation.
75// If a test requires a custom install it should go into its own package, otherwise it should go
76// here to reuse a single install across tests.
77func TestMain(m *testing.M) {78// nolint: staticcheck79framework.80NewSuite(m).81RequireMinVersion(24).82SkipIf("https://github.com/istio/istio/issues/43243", func(ctx resource.Context) bool {83return strings.Contains(ctx.Settings().Image.Tag, "distroless")84}).85Label(label.IPv4). // https://github.com/istio/istio/issues/4100886Setup(func(t resource.Context) error {87t.Settings().Ambient = true88return nil89}).90Setup(istio.Setup(&i, func(ctx resource.Context, cfg *istio.Config) {91// can't deploy VMs without eastwest gateway92ctx.Settings().SkipVMs()93cfg.EnableCNI = true94cfg.DeployEastWestGW = false95cfg.ControlPlaneValues = `96values:
97ztunnel:
98terminationGracePeriodSeconds: 5
99env:
100SECRET_TTL: 5m
101`
102}, cert.CreateCASecretAlt)).103Setup(func(t resource.Context) error {104gatewayConformanceInputs.Client = t.Clusters().Default()105gatewayConformanceInputs.Cleanup = !t.Settings().NoCleanup106
107return nil108}).109Setup(func(t resource.Context) error {110return SetupApps(t, i, apps)111}).112Run()113}
114
115const (116WorkloadAddressedWaypoint = "workload-addressed-waypoint"117ServiceAddressedWaypoint = "service-addressed-waypoint"118Captured = "captured"119Uncaptured = "uncaptured"120SidecarWaypoint = "sidecar-waypoint"121SidecarCaptured = "sidecar-captured"122SidecarUncaptured = "sidecar-uncaptured"123)
124
125var inMesh = match.Matcher(func(instance echo.Instance) bool {126names := []string{"waypoint", "captured", "sidecar"}127for _, name := range names {128if strings.Contains(instance.Config().Service, name) {129return true130}131}132return false133})134
135func SetupApps(t resource.Context, i istio.Instance, apps *EchoDeployments) error {136var err error137apps.Namespace, err = namespace.New(t, namespace.Config{138Prefix: "echo",139Inject: false,140Labels: map[string]string{141constants.DataplaneMode: "ambient",142},143})144if err != nil {145return err146}147
148prom, err = prometheus.New(t, prometheus.Config{})149if err != nil {150return err151}152
153// Headless services don't work with targetPort, set to same port154headlessPorts := make([]echo.Port, len(ports.All()))155for i, p := range ports.All() {156p.ServicePort = p.WorkloadPort157headlessPorts[i] = p158}159builder := deployment.New(t).160WithClusters(t.Clusters()...).161WithConfig(echo.Config{162Service: WorkloadAddressedWaypoint,163Namespace: apps.Namespace,164Ports: ports.All(),165ServiceAccount: true,166WorkloadWaypointProxy: "waypoint",167Subsets: []echo.SubsetConfig{168{169Replicas: 1,170Version: "v1",171Labels: map[string]string{172"app": WorkloadAddressedWaypoint,173"version": "v1",174},175Annotations: map[echo.Annotation]*echo.AnnotationValue{176echo.AmbientUseWaypoint: {177Value: "waypoint",178},179},180},181{182Replicas: 1,183Version: "v2",184Labels: map[string]string{185"app": WorkloadAddressedWaypoint,186"version": "v2",187},188Annotations: map[echo.Annotation]*echo.AnnotationValue{189echo.AmbientUseWaypoint: {190Value: "waypoint",191},192},193},194},195}).196WithConfig(echo.Config{197Service: ServiceAddressedWaypoint,198Namespace: apps.Namespace,199Ports: ports.All(),200ServiceAnnotations: echo.NewAnnotations().Set(echo.AmbientUseWaypoint, "waypoint"),201ServiceAccount: true,202ServiceWaypointProxy: "waypoint",203Subsets: []echo.SubsetConfig{204{205Replicas: 1,206Version: "v1",207Labels: map[string]string{208"app": ServiceAddressedWaypoint,209"version": "v1",210},211Annotations: map[echo.Annotation]*echo.AnnotationValue{212echo.AmbientUseWaypoint: {213Value: "waypoint",214},215},216},217{218Replicas: 1,219Version: "v2",220Labels: map[string]string{221"app": ServiceAddressedWaypoint,222"version": "v2",223},224Annotations: map[echo.Annotation]*echo.AnnotationValue{225echo.AmbientUseWaypoint: {226Value: "waypoint",227},228},229},230},231}).232WithConfig(echo.Config{233Service: Captured,234Namespace: apps.Namespace,235Ports: ports.All(),236ServiceAccount: true,237Subsets: []echo.SubsetConfig{238{239Replicas: 1,240Version: "v1",241},242{243Replicas: 1,244Version: "v2",245},246},247}).248WithConfig(echo.Config{249Service: Uncaptured,250Namespace: apps.Namespace,251Ports: ports.All(),252ServiceAccount: true,253Subsets: []echo.SubsetConfig{254{255Replicas: 1,256Version: "v1",257Annotations: echo.NewAnnotations().Set(echo.AmbientType, constants.AmbientRedirectionDisabled),258},259{260Replicas: 1,261Version: "v2",262Annotations: echo.NewAnnotations().Set(echo.AmbientType, constants.AmbientRedirectionDisabled),263},264},265})266
267_, whErr := t.Clusters().Default().268Kube().AdmissionregistrationV1().MutatingWebhookConfigurations().269Get(context.Background(), "istio-sidecar-injector", metav1.GetOptions{})270if whErr != nil && !kerrors.IsNotFound(whErr) {271return whErr272}273// Only setup sidecar tests if webhook is installed274if whErr == nil {275// TODO(https://github.com/istio/istio/issues/43244) support sidecars that are captured276//builder = builder.WithConfig(echo.Config{277// Service: SidecarWaypoint,278// Namespace: apps.Namespace,279// Ports: ports.All(),280// Subsets: []echo.SubsetConfig{281// {282// Replicas: 1,283// Version: "v1",284// Labels: map[string]string{285// "ambient-type": "workload",286// "sidecar.istio.io/inject": "true",287// },288// },289// {290// Replicas: 1,291// Version: "v2",292// Labels: map[string]string{293// "ambient-type": "workload",294// "sidecar.istio.io/inject": "true",295// },296// },297// },298//})299// builder = builder.WithConfig(echo.Config{300// Service: SidecarCaptured,301// Namespace: apps.Namespace,302// Ports: ports.All(),303// Subsets: []echo.SubsetConfig{304// {305// Replicas: 1,306// Version: "v1",307// Labels: map[string]string{308// "ambient-type": "workload",309// "sidecar.istio.io/inject": "true",310// },311// },312// {313// Replicas: 1,314// Version: "v2",315// Labels: map[string]string{316// "ambient-type": "workload",317// "sidecar.istio.io/inject": "true",318// },319// },320// },321// })322builder = builder.WithConfig(echo.Config{323Service: SidecarUncaptured,324Namespace: apps.Namespace,325Ports: ports.All(),326ServiceAccount: true,327Subsets: []echo.SubsetConfig{328{329Replicas: 1,330Version: "v1",331Annotations: echo.NewAnnotations().Set(echo.AmbientType, constants.AmbientRedirectionDisabled),332Labels: map[string]string{333"sidecar.istio.io/inject": "true",334},335},336{337Replicas: 1,338Version: "v2",339Annotations: echo.NewAnnotations().Set(echo.AmbientType, constants.AmbientRedirectionDisabled),340Labels: map[string]string{341"sidecar.istio.io/inject": "true",342},343},344},345})346}347
348echos, err := builder.Build()349if err != nil {350return err351}352for _, b := range echos {353scopes.Framework.Infof("built %v", b.Config().Service)354}355apps.All = echos356apps.WorkloadAddressedWaypoint = match.ServiceName(echo.NamespacedName{Name: WorkloadAddressedWaypoint, Namespace: apps.Namespace}).GetMatches(echos)357apps.ServiceAddressedWaypoint = match.ServiceName(echo.NamespacedName{Name: ServiceAddressedWaypoint, Namespace: apps.Namespace}).GetMatches(echos)358apps.AllWaypoint = apps.AllWaypoint.Append(apps.WorkloadAddressedWaypoint)359apps.AllWaypoint = apps.AllWaypoint.Append(apps.ServiceAddressedWaypoint)360apps.Uncaptured = match.ServiceName(echo.NamespacedName{Name: Uncaptured, Namespace: apps.Namespace}).GetMatches(echos)361apps.Captured = match.ServiceName(echo.NamespacedName{Name: Captured, Namespace: apps.Namespace}).GetMatches(echos)362apps.SidecarWaypoint = match.ServiceName(echo.NamespacedName{Name: SidecarWaypoint, Namespace: apps.Namespace}).GetMatches(echos)363apps.SidecarUncaptured = match.ServiceName(echo.NamespacedName{Name: SidecarUncaptured, Namespace: apps.Namespace}).GetMatches(echos)364apps.SidecarCaptured = match.ServiceName(echo.NamespacedName{Name: SidecarCaptured, Namespace: apps.Namespace}).GetMatches(echos)365apps.Mesh = inMesh.GetMatches(echos)366apps.MeshExternal = match.Not(inMesh).GetMatches(echos)367
368if apps.WaypointProxies == nil {369apps.WaypointProxies = make(map[string]ambient.WaypointProxy)370}371
372for _, echo := range echos {373svcwp := echo.Config().ServiceWaypointProxy374wlwp := echo.Config().WorkloadWaypointProxy375if svcwp != "" {376if _, found := apps.WaypointProxies[svcwp]; !found {377apps.WaypointProxies[svcwp], err = ambient.NewWaypointProxy(t, apps.Namespace, svcwp)378if err != nil {379return err380}381}382}383if wlwp != "" {384if _, found := apps.WaypointProxies[wlwp]; !found {385apps.WaypointProxies[wlwp], err = ambient.NewWaypointProxy(t, apps.Namespace, wlwp)386if err != nil {387return err388}389}390}391
392}393
394return nil395}
396