| /* |
| * Licensed to the Apache Software Foundation (ASF) under one or more |
| * contributor license agreements. See the NOTICE file distributed with |
| * this work for additional information regarding copyright ownership. |
| * The ASF licenses this file to You 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 tracker |
| |
| import ( |
| "context" |
| "time" |
| ) |
| |
| import ( |
| envoy_config_core_v3 "github.com/envoyproxy/go-control-plane/envoy/config/core/v3" |
| |
| . "github.com/onsi/ginkgo/v2" |
| |
| . "github.com/onsi/gomega" |
| ) |
| |
| import ( |
| dp_server "github.com/apache/dubbo-kubernetes/pkg/config/dp-server" |
| config_types "github.com/apache/dubbo-kubernetes/pkg/config/types" |
| "github.com/apache/dubbo-kubernetes/pkg/core/resources/apis/mesh" |
| "github.com/apache/dubbo-kubernetes/pkg/core/resources/manager" |
| "github.com/apache/dubbo-kubernetes/pkg/core/resources/model" |
| "github.com/apache/dubbo-kubernetes/pkg/core/resources/store" |
| "github.com/apache/dubbo-kubernetes/pkg/hds/cache" |
| "github.com/apache/dubbo-kubernetes/pkg/plugins/resources/memory" |
| "github.com/apache/dubbo-kubernetes/pkg/test/matchers" |
| util_proto "github.com/apache/dubbo-kubernetes/pkg/util/proto" |
| ) |
| |
| var _ = Describe("HDS Snapshot generator", func() { |
| var resourceManager manager.ResourceManager |
| |
| BeforeEach(func() { |
| resourceManager = manager.NewResourceManager(memory.NewStore()) |
| |
| err := resourceManager.Create(context.Background(), mesh.NewMeshResource(), store.CreateByKey("mesh-1", model.NoMesh)) |
| Expect(err).ToNot(HaveOccurred()) |
| }) |
| |
| type testCase struct { |
| goldenFile string |
| dataplane string |
| hdsConfig *dp_server.HdsConfig |
| } |
| |
| DescribeTable("should generate HDS response", |
| func(given testCase) { |
| // given |
| dp := mesh.NewDataplaneResource() |
| err := util_proto.FromYAML([]byte(given.dataplane), dp.Spec) |
| Expect(err).ToNot(HaveOccurred()) |
| err = resourceManager.Create(context.Background(), dp, store.CreateByKey("dp-1", "mesh-1")) |
| Expect(err).ToNot(HaveOccurred()) |
| generator := NewSnapshotGenerator(resourceManager, given.hdsConfig, 9901) |
| |
| // when |
| snapshot, err := generator.GenerateSnapshot(context.Background(), &envoy_config_core_v3.Node{Id: "mesh-1.dp-1"}) |
| |
| // then |
| Expect(err).ToNot(HaveOccurred()) |
| actual, err := util_proto.ToYAML(snapshot.GetResources(cache.HealthCheckSpecifierType)["hcs"]) |
| Expect(err).ToNot(HaveOccurred()) |
| Expect(actual).To(matchers.MatchGoldenYAML("testdata", given.goldenFile)) |
| }, |
| Entry("should generate HealthCheckSpecifier", testCase{ |
| goldenFile: "hds.1.golden.yaml", |
| dataplane: ` |
| networking: |
| address: 10.20.0.1 |
| inbound: |
| - port: 9000 |
| serviceAddress: 192.168.0.1 |
| servicePort: 80 |
| serviceProbe: |
| tcp: {} |
| tags: |
| dubbo.io/service: backend |
| `, |
| hdsConfig: &dp_server.HdsConfig{ |
| Interval: config_types.Duration{Duration: 8 * time.Second}, |
| Enabled: true, |
| CheckDefaults: &dp_server.HdsCheck{ |
| Interval: config_types.Duration{Duration: 1 * time.Second}, |
| NoTrafficInterval: config_types.Duration{Duration: 2 * time.Second}, |
| Timeout: config_types.Duration{Duration: 3 * time.Second}, |
| HealthyThreshold: 4, |
| UnhealthyThreshold: 5, |
| }, |
| }, |
| }), |
| Entry("should generate HealthCheckSpecifier", testCase{ |
| goldenFile: "hds.2.golden.yaml", |
| dataplane: ` |
| networking: |
| address: 10.20.0.1 |
| inbound: |
| - port: 9000 |
| serviceAddress: 192.168.0.1 |
| servicePort: 80 |
| serviceProbe: |
| tcp: {} |
| tags: |
| dubbo.io/service: backend |
| `, |
| hdsConfig: &dp_server.HdsConfig{ |
| Interval: config_types.Duration{Duration: 8 * time.Second}, |
| Enabled: true, |
| CheckDefaults: &dp_server.HdsCheck{ |
| Interval: config_types.Duration{Duration: 1 * time.Second}, |
| NoTrafficInterval: config_types.Duration{Duration: 2 * time.Second}, |
| Timeout: config_types.Duration{Duration: 3 * time.Second}, |
| HealthyThreshold: 4, |
| UnhealthyThreshold: 5, |
| }, |
| }, |
| }), |
| Entry("should generate HealthCheckSpecifier with localhost bound", testCase{ |
| goldenFile: "hds.3.golden.yaml", |
| dataplane: ` |
| networking: |
| address: 10.20.0.1 |
| inbound: |
| - port: 9000 |
| serviceAddress: 127.0.0.1 |
| servicePort: 80 |
| serviceProbe: |
| tcp: {} |
| tags: |
| dubbo.io/service: backend |
| `, |
| hdsConfig: &dp_server.HdsConfig{ |
| Interval: config_types.Duration{Duration: 8 * time.Second}, |
| Enabled: true, |
| CheckDefaults: &dp_server.HdsCheck{ |
| Interval: config_types.Duration{Duration: 1 * time.Second}, |
| NoTrafficInterval: config_types.Duration{Duration: 2 * time.Second}, |
| Timeout: config_types.Duration{Duration: 3 * time.Second}, |
| HealthyThreshold: 4, |
| UnhealthyThreshold: 5, |
| }, |
| }, |
| }), |
| ) |
| }) |