From cd63e9127ad99bc2f19c80631ef190c7111ea030 Mon Sep 17 00:00:00 2001 From: elliotxx <951376975@qq.com> Date: Thu, 15 Aug 2024 16:23:35 +0800 Subject: [PATCH] refactor: optimize obtain way of default relationship config (#592) ## What type of PR is this? /kind refactor ## What this PR does / why we need it: Optimize obtain way of default relationship config, change to obtain from go embed. --- .goreleaser/.goreleaser-dev.yml | 3 -- .goreleaser/.goreleaser.yml | 2 - Dockerfile | 1 - ...ionship.yaml => default-relationship.yaml} | 0 config/embed.go | 9 ++-- pkg/core/manager/insight/topology_test.go | 49 ------------------- pkg/infra/topology/relationship.go | 18 ++++--- 7 files changed, 17 insertions(+), 65 deletions(-) rename config/{relationship.yaml => default-relationship.yaml} (100%) diff --git a/.goreleaser/.goreleaser-dev.yml b/.goreleaser/.goreleaser-dev.yml index 631fedec..b62f8227 100644 --- a/.goreleaser/.goreleaser-dev.yml +++ b/.goreleaser/.goreleaser-dev.yml @@ -91,7 +91,6 @@ dockers: goarch: amd64 extra_files: - pkg/version/VERSION - - config/relationship.yaml - image_templates: - 'kusionstack/{{ .ProjectName }}:{{ .Tag }}-arm64' dockerfile: Dockerfile @@ -107,7 +106,6 @@ dockers: goarch: arm64 extra_files: - pkg/version/VERSION - - config/relationship.yaml docker_manifests: - name_template: "kusionstack/{{ .ProjectName }}:{{ .Tag }}" @@ -118,4 +116,3 @@ docker_manifests: image_templates: - "kusionstack/{{ .ProjectName }}:{{ .Tag }}-amd64" - "kusionstack/{{ .ProjectName }}:{{ .Tag }}-arm64" - diff --git a/.goreleaser/.goreleaser.yml b/.goreleaser/.goreleaser.yml index 9533aa36..ae9bb339 100644 --- a/.goreleaser/.goreleaser.yml +++ b/.goreleaser/.goreleaser.yml @@ -173,7 +173,6 @@ dockers: goarch: amd64 extra_files: - pkg/version/VERSION - - config/relationship.yaml - image_templates: - 'kusionstack/{{ .ProjectName }}:{{ .Tag }}-arm64' dockerfile: Dockerfile @@ -189,7 +188,6 @@ dockers: goarch: arm64 extra_files: - pkg/version/VERSION - - config/relationship.yaml docker_manifests: - name_template: "kusionstack/{{ .ProjectName }}:{{ .Tag }}" diff --git a/Dockerfile b/Dockerfile index a1ceccb9..974be856 100644 --- a/Dockerfile +++ b/Dockerfile @@ -19,7 +19,6 @@ WORKDIR / COPY karpor . COPY cert-generator . -COPY config/relationship.yaml . COPY pkg/version/VERSION . RUN apk update && apk add --no-cache aws-cli diff --git a/config/relationship.yaml b/config/default-relationship.yaml similarity index 100% rename from config/relationship.yaml rename to config/default-relationship.yaml diff --git a/config/embed.go b/config/embed.go index df318f6c..a707956d 100644 --- a/config/embed.go +++ b/config/embed.go @@ -18,8 +18,11 @@ import _ "embed" var DefaultConfig = [][]byte{DefaultSyncStrategy, DefaultRBAC} -//go:embed default-sync-strategy.yaml -var DefaultSyncStrategy []byte - //go:embed default-rbac.yaml var DefaultRBAC []byte + +//go:embed default-relationship.yaml +var DefaultRelationship []byte + +//go:embed default-sync-strategy.yaml +var DefaultSyncStrategy []byte diff --git a/pkg/core/manager/insight/topology_test.go b/pkg/core/manager/insight/topology_test.go index 2092d423..894534a4 100644 --- a/pkg/core/manager/insight/topology_test.go +++ b/pkg/core/manager/insight/topology_test.go @@ -16,8 +16,6 @@ package insight import ( "context" - "os" - "path/filepath" "reflect" "testing" @@ -30,10 +28,6 @@ import ( ) func TestInsightManager_GetTopologyForCluster(t *testing.T) { - // Set up environment variable for relationship file - setRelationshipFilePath() - defer os.Unsetenv("KARPOR_RELATIONSHIP_FILE") - // Set up mocks for dynamic client mockey.Mock((*dynamic.DynamicClient).Resource).Return(&mockNamespaceableResource{}).Build() mockey.Mock(topology.GVRNamespaced).Return(true).Build() @@ -89,10 +83,6 @@ func TestInsightManager_GetTopologyForCluster(t *testing.T) { } func TestInsightManager_GetTopologyForResource(t *testing.T) { - // Set up environment variable for relationship file - setRelationshipFilePath() - defer os.Unsetenv("KARPOR_RELATIONSHIP_FILE") - // Initialize InsightManager manager, err := NewInsightManager(&mockSearchStorage{}, &mockResourceStorage{}, &mockResourceGroupRuleStorage{}, &genericapiserver.CompletedConfig{}) require.NoError(t, err, "Unexpected error initializing InsightManager") @@ -164,10 +154,6 @@ func TestInsightManager_GetTopologyForResource(t *testing.T) { } func TestInsightManager_GetTopologyForClusterNamespace(t *testing.T) { - // Set up environment variable for relationship file - setRelationshipFilePath() - defer os.Unsetenv("KARPOR_RELATIONSHIP_FILE") - // Set up mocks for dynamic client mockey.Mock((*dynamic.DynamicClient).Resource).Return(&mockNamespaceableResource{}).Build() mockey.Mock(topology.GVRNamespaced).Return(true).Build() @@ -224,38 +210,3 @@ func TestInsightManager_GetTopologyForClusterNamespace(t *testing.T) { }) } } - -func setRelationshipFilePath() { - filePath := os.Getenv("KARPOR_RELATIONSHIP_FILE") - if filePath == "" { - // Default file path - filePath = "relationship.yaml" - // Find the file in each parent directory - curdir, err := os.Getwd() - if err != nil { - panic("Unable to get current directory") - } - dir := curdir - for { - configPath := filepath.Join(dir, "config", "relationship.yaml") - if _, err := os.Stat(configPath); err == nil { - filePath, err = filepath.Rel(curdir, configPath) - if err != nil { - panic("Failed to get relative path of relationship.yaml as " + err.Error()) - } - break - } - // Move up to the parent directory - parent := filepath.Dir(dir) - if parent == dir { - // Reached the root directory, break the loop - break - } - dir = parent - } - // Set the environment variable - if err := os.Setenv("KARPOR_RELATIONSHIP_FILE", filePath); err != nil { - panic("Failed to set environment variable") - } - } -} diff --git a/pkg/infra/topology/relationship.go b/pkg/infra/topology/relationship.go index 3f8e476c..a0267fc8 100644 --- a/pkg/infra/topology/relationship.go +++ b/pkg/infra/topology/relationship.go @@ -24,6 +24,7 @@ import ( "github.com/pkg/errors" + "github.com/KusionStack/karpor/config" "github.com/KusionStack/karpor/pkg/core/entity" "github.com/KusionStack/karpor/pkg/infra/search/storage" "github.com/KusionStack/karpor/pkg/infra/search/storage/elasticsearch" @@ -110,17 +111,20 @@ func BuildBuiltinRelationshipGraph(ctx context.Context, client *dynamic.DynamicC // TODO: Obtaining topological relationship from CR in the future. // Get the file path from the environment variable, fallback to default if // not set. + var err error + relationshipYAML := config.DefaultRelationship + filePath := os.Getenv("KARPOR_RELATIONSHIP_FILE") - if filePath == "" { - filePath = "relationship.yaml" // Default file path + if filePath != "" { + log.Info("Using custom relationship file: " + filePath) + relationshipYAML, err = os.ReadFile(filePath) + if err != nil { + log.Error(err, "ReadFile error") + } } - yamlFile, err := os.ReadFile(filePath) - if err != nil { - log.Error(err, "yamlFile.Get err") - } r := RelationshipGraph{} - err = yaml.Unmarshal(yamlFile, &r) + err = yaml.Unmarshal(relationshipYAML, &r) if err != nil { log.Error(err, "Unmarshal error") }