Skip to content

Commit ff9ab85

Browse files
aFlyBird0steinliber
authored andcommitted
refactor: helm-installer to make code more clear
Signed-off-by: Bird <[email protected]>
1 parent 2e42e2c commit ff9ab85

File tree

16 files changed

+155
-142
lines changed

16 files changed

+155
-142
lines changed

internal/pkg/plugin/helminstaller/defaults/argocd.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88
"github.com/devstream-io/devstream/pkg/util/types"
99
)
1010

11-
var toolArgoCD = "argocd"
11+
const toolArgoCD = "argocd"
1212

1313
var DefaultConfigWithArgoCD = helm.Options{
1414
Chart: helmCommon.Chart{
@@ -28,8 +28,7 @@ var DefaultConfigWithArgoCD = helm.Options{
2828
}
2929

3030
func init() {
31-
DefaultOptionsMap[toolArgoCD] = &DefaultConfigWithArgoCD
32-
StatusGetterFuncMap[toolArgoCD] = GetArgoCDStatus
31+
RegisterDefaultHelmAppInstance(toolArgoCD, &DefaultConfigWithArgoCD, GetArgoCDStatus)
3332
}
3433

3534
func GetArgoCDStatus(options configmanager.RawOptions) (statemanager.ResourceStatus, error) {

internal/pkg/plugin/helminstaller/defaults/artifactory.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88
"github.com/devstream-io/devstream/pkg/util/types"
99
)
1010

11-
var toolArtifactory = "artifactory"
11+
const toolArtifactory = "artifactory"
1212

1313
var DefaultConfigWithArtifactory = helm.Options{
1414
Chart: helmCommon.Chart{
@@ -28,8 +28,7 @@ var DefaultConfigWithArtifactory = helm.Options{
2828
}
2929

3030
func init() {
31-
DefaultOptionsMap[toolArtifactory] = &DefaultConfigWithArtifactory
32-
StatusGetterFuncMap[toolArtifactory] = GetArtifactoryStatus
31+
RegisterDefaultHelmAppInstance(toolArtifactory, &DefaultConfigWithArtifactory, GetArtifactoryStatus)
3332
}
3433

3534
func GetArtifactoryStatus(options configmanager.RawOptions) (statemanager.ResourceStatus, error) {

internal/pkg/plugin/helminstaller/defaults/defaultconfig.go

Lines changed: 0 additions & 11 deletions
This file was deleted.
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package defaults_test
2+
3+
import (
4+
"testing"
5+
6+
. "github.com/onsi/ginkgo/v2"
7+
. "github.com/onsi/gomega"
8+
)
9+
10+
func TestDefaults(t *testing.T) {
11+
RegisterFailHandler(Fail)
12+
RunSpecs(t, "Defaults Suite")
13+
}

internal/pkg/plugin/helminstaller/defaults/devlake.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,10 @@ import (
1111
"github.com/devstream-io/devstream/pkg/util/types"
1212
)
1313

14-
const DevLakeSvcName = "devlake-lake"
15-
16-
var toolDevLake = "devlake"
14+
const (
15+
DevLakeSvcName = "devlake-lake"
16+
toolDevLake = "devlake"
17+
)
1718

1819
var DefaultConfigWithDevLake = helm.Options{
1920
Chart: helmCommon.Chart{
@@ -33,8 +34,7 @@ var DefaultConfigWithDevLake = helm.Options{
3334
}
3435

3536
func init() {
36-
DefaultOptionsMap[toolDevLake] = &DefaultConfigWithDevLake
37-
StatusGetterFuncMap[toolDevLake] = GetDevLakeStatus
37+
RegisterDefaultHelmAppInstance(toolDevLake, &DefaultConfigWithDevLake, GetDevLakeStatus)
3838
}
3939

4040
func GetDevLakeStatus(options configmanager.RawOptions) (statemanager.ResourceStatus, error) {

internal/pkg/plugin/helminstaller/defaults/harbor.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88
"github.com/devstream-io/devstream/pkg/util/types"
99
)
1010

11-
var toolHarbor = "harbor"
11+
const toolHarbor = "harbor"
1212

1313
var DefaultConfigWithHarbor = helm.Options{
1414
Chart: helmCommon.Chart{
@@ -28,8 +28,7 @@ var DefaultConfigWithHarbor = helm.Options{
2828
}
2929

3030
func init() {
31-
DefaultOptionsMap[toolHarbor] = &DefaultConfigWithHarbor
32-
StatusGetterFuncMap[toolHarbor] = GetHarborStatus
31+
RegisterDefaultHelmAppInstance(toolHarbor, &DefaultConfigWithHarbor, GetHarborStatus)
3332
}
3433

3534
func GetHarborStatus(options configmanager.RawOptions) (statemanager.ResourceStatus, error) {
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package defaults
2+
3+
import (
4+
"strings"
5+
"sync"
6+
7+
"github.com/devstream-io/devstream/internal/pkg/plugin/installer"
8+
"github.com/devstream-io/devstream/internal/pkg/plugin/installer/helm"
9+
)
10+
11+
type HelmAppInstance struct {
12+
Name string
13+
HelmOptions *helm.Options
14+
StatusGetter installer.StatusGetterOperation
15+
}
16+
17+
var (
18+
helmAppInstances map[string]*HelmAppInstance
19+
once sync.Once
20+
)
21+
22+
func RegisterDefaultHelmAppInstance(name string, options *helm.Options, statusGetter installer.StatusGetterOperation) {
23+
// make sure the map is initialized only once
24+
once.Do(func() {
25+
helmAppInstances = make(map[string]*HelmAppInstance)
26+
})
27+
28+
helmAppInstances[name] = &HelmAppInstance{
29+
Name: name,
30+
HelmOptions: options,
31+
StatusGetter: statusGetter,
32+
}
33+
}
34+
35+
// GetDefaultHelmAppInstanceByInstanceID will return the default helm app instance
36+
// by matching the prefix of given instanceID and the list of helm app instances names.
37+
// It will return the longest matched name if there are multiple matched names, and return nil if no matched name is found.
38+
// e.g. instanceID="argocd-config-001", "argocd" and "argocd-config" both are supported helm charts,
39+
// then the HelmAppInstance named "argocd-config" will be returned.
40+
func GetDefaultHelmAppInstanceByInstanceID(instanceID string) *HelmAppInstance {
41+
longestMatchedName := ""
42+
for curNameToMatch := range helmAppInstances {
43+
if strings.HasPrefix(instanceID, curNameToMatch) && len(curNameToMatch) > len(longestMatchedName) {
44+
longestMatchedName = curNameToMatch
45+
}
46+
}
47+
48+
if longestMatchedName != "" {
49+
return helmAppInstances[longestMatchedName]
50+
}
51+
return nil
52+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package defaults
2+
3+
import (
4+
"reflect"
5+
6+
. "github.com/onsi/ginkgo/v2"
7+
. "github.com/onsi/gomega"
8+
)
9+
10+
var _ = Describe("instance test", func() {
11+
var (
12+
instanceID string
13+
instance *HelmAppInstance
14+
)
15+
16+
// main logic to get the default instance
17+
JustBeforeEach(func() {
18+
instance = GetDefaultHelmAppInstanceByInstanceID(instanceID)
19+
})
20+
21+
Context("GetDefaultHelmAppInstanceByInstanceID", func() {
22+
When("instanceID is not exists", func() {
23+
BeforeEach(func() {
24+
instanceID = "not-exists"
25+
})
26+
It("should return nil", func() {
27+
// main logic is in JustBeforeEach
28+
Expect(instance).To(BeNil())
29+
})
30+
})
31+
32+
When("instanceID is exists(argocd)", func() {
33+
JustAfterEach(func() {
34+
Expect(instance).NotTo(BeNil())
35+
Expect(instance.Name).To(Equal("argocd"))
36+
Expect(*instance.HelmOptions).To(Equal(DefaultConfigWithArgoCD))
37+
pointer1 := reflect.ValueOf(instance.StatusGetter).Pointer()
38+
pointer2 := reflect.ValueOf(GetArgoCDStatus).Pointer()
39+
Expect(pointer1).To(Equal(pointer2))
40+
})
41+
When(`instanceID is the same as "argocd"`, func() {
42+
BeforeEach(func() {
43+
instanceID = "argocd"
44+
})
45+
It("should return instance", func() {
46+
// main logic is in JustAfterEach
47+
})
48+
})
49+
50+
When(`instanceID has prefix "argocd"`, func() {
51+
BeforeEach(func() {
52+
instanceID = "argocd-001"
53+
})
54+
It("should return instance", func() {
55+
// main logic is in JustAfterEach
56+
})
57+
})
58+
})
59+
})
60+
})

internal/pkg/plugin/helminstaller/defaults/jenkins.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111
"github.com/devstream-io/devstream/pkg/util/types"
1212
)
1313

14-
var toolJenkins = "jenkins"
14+
const toolJenkins = "jenkins"
1515

1616
var DefaultConfigWithJenkins = helm.Options{
1717
Chart: helmCommon.Chart{
@@ -31,8 +31,7 @@ var DefaultConfigWithJenkins = helm.Options{
3131
}
3232

3333
func init() {
34-
DefaultOptionsMap[toolJenkins] = &DefaultConfigWithJenkins
35-
StatusGetterFuncMap[toolJenkins] = GetJenkinsStatus
34+
RegisterDefaultHelmAppInstance(toolJenkins, &DefaultConfigWithJenkins, GetJenkinsStatus)
3635
}
3736

3837
func GetJenkinsStatus(options configmanager.RawOptions) (statemanager.ResourceStatus, error) {

internal/pkg/plugin/helminstaller/defaults/kubeprometheus.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88
"github.com/devstream-io/devstream/pkg/util/types"
99
)
1010

11-
var toolKubePrometheus = "kube-prometheus"
11+
const toolKubePrometheus = "kube-prometheus"
1212

1313
var DefaultConfigWithKubePrometheus = helm.Options{
1414
Chart: helmCommon.Chart{
@@ -28,8 +28,7 @@ var DefaultConfigWithKubePrometheus = helm.Options{
2828
}
2929

3030
func init() {
31-
DefaultOptionsMap[toolKubePrometheus] = &DefaultConfigWithKubePrometheus
32-
StatusGetterFuncMap[toolKubePrometheus] = GetKubePrometheusStatus
31+
RegisterDefaultHelmAppInstance(toolKubePrometheus, &DefaultConfigWithKubePrometheus, GetKubePrometheusStatus)
3332
}
3433

3534
func GetKubePrometheusStatus(options configmanager.RawOptions) (statemanager.ResourceStatus, error) {

0 commit comments

Comments
 (0)