Skip to content

Commit

Permalink
refactor: optimize obtain way of default relationship config (#592)
Browse files Browse the repository at this point in the history
## 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.
  • Loading branch information
elliotxx authored Aug 15, 2024
1 parent 59bbd29 commit cd63e91
Show file tree
Hide file tree
Showing 7 changed files with 17 additions and 65 deletions.
3 changes: 0 additions & 3 deletions .goreleaser/.goreleaser-dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,6 @@ dockers:
goarch: amd64
extra_files:
- pkg/version/VERSION
- config/relationship.yaml
- image_templates:
- 'kusionstack/{{ .ProjectName }}:{{ .Tag }}-arm64'
dockerfile: Dockerfile
Expand All @@ -107,7 +106,6 @@ dockers:
goarch: arm64
extra_files:
- pkg/version/VERSION
- config/relationship.yaml

docker_manifests:
- name_template: "kusionstack/{{ .ProjectName }}:{{ .Tag }}"
Expand All @@ -118,4 +116,3 @@ docker_manifests:
image_templates:
- "kusionstack/{{ .ProjectName }}:{{ .Tag }}-amd64"
- "kusionstack/{{ .ProjectName }}:{{ .Tag }}-arm64"

2 changes: 0 additions & 2 deletions .goreleaser/.goreleaser.yml
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,6 @@ dockers:
goarch: amd64
extra_files:
- pkg/version/VERSION
- config/relationship.yaml
- image_templates:
- 'kusionstack/{{ .ProjectName }}:{{ .Tag }}-arm64'
dockerfile: Dockerfile
Expand All @@ -189,7 +188,6 @@ dockers:
goarch: arm64
extra_files:
- pkg/version/VERSION
- config/relationship.yaml

docker_manifests:
- name_template: "kusionstack/{{ .ProjectName }}:{{ .Tag }}"
Expand Down
1 change: 0 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
File renamed without changes.
9 changes: 6 additions & 3 deletions config/embed.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
49 changes: 0 additions & 49 deletions pkg/core/manager/insight/topology_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ package insight

import (
"context"
"os"
"path/filepath"
"reflect"
"testing"

Expand All @@ -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()
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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")
}
}
}
18 changes: 11 additions & 7 deletions pkg/infra/topology/relationship.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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")
}
Expand Down

0 comments on commit cd63e91

Please sign in to comment.