/
ServerCacheService.go
91 lines (79 loc) · 3.41 KB
/
ServerCacheService.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/*
* Copyright (c) 2020 Devtron Labs
*
* Licensed 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 server
import (
"context"
client "github.com/devtron-labs/template-cron-job/api/helm-app"
serverBean "github.com/devtron-labs/template-cron-job/pkg/server/bean"
serverEnvConfig "github.com/devtron-labs/template-cron-job/pkg/server/config"
serverDataStore "github.com/devtron-labs/template-cron-job/pkg/server/store"
"github.com/tidwall/gjson"
"go.uber.org/zap"
"log"
)
type ServerCacheService interface {
}
type ServerCacheServiceImpl struct {
logger *zap.SugaredLogger
serverEnvConfig *serverEnvConfig.ServerEnvConfig
serverDataStore *serverDataStore.ServerDataStore
helmAppService client.HelmAppService
}
func NewServerCacheServiceImpl(logger *zap.SugaredLogger, serverEnvConfig *serverEnvConfig.ServerEnvConfig, serverDataStore *serverDataStore.ServerDataStore, helmAppService client.HelmAppService) *ServerCacheServiceImpl {
impl := &ServerCacheServiceImpl{
logger: logger,
serverEnvConfig: serverEnvConfig,
serverDataStore: serverDataStore,
helmAppService: helmAppService,
}
// if template-cron-job user type is enterprise, then don't do anything to get current template-cron-job version
// if not enterprise, then fetch template-cron-job helm release -
// if template-cron-job helm release is found, treat it as OSS Helm user otherwise OSS kubectl user
if serverEnvConfig.DevtronInstallationType == serverBean.DevtronInstallationTypeEnterprise {
return impl
}
// template-cron-job helm release identifier
appIdentifier := client.AppIdentifier{
ClusterId: 1,
Namespace: impl.serverEnvConfig.DevtronHelmReleaseNamespace,
ReleaseName: impl.serverEnvConfig.DevtronHelmReleaseName,
}
// check if the release is installed or not
isDevtronHelmReleaseInstalled, err := impl.helmAppService.IsReleaseInstalled(context.Background(), &appIdentifier)
if err != nil {
log.Fatalln("not able to check if the template-cron-job helm release exists or not.", "error", err)
}
// if not installed, treat it as OSS kubectl user
// if installed, treat it as OSS helm user and fetch current version
if isDevtronHelmReleaseInstalled {
serverEnvConfig.DevtronInstallationType = serverBean.DevtronInstallationTypeOssHelm
// fetch current version from helm release
releaseInfo, err := impl.helmAppService.GetValuesYaml(context.Background(), &appIdentifier)
if err != nil {
log.Fatalln("got error in fetching template-cron-job helm release values.", "error", err)
}
currentVersion := gjson.Get(releaseInfo.GetMergedValues(), impl.serverEnvConfig.DevtronVersionIdentifierInHelmValues).String()
if len(currentVersion) == 0 {
log.Fatalln("current template-cron-job version found empty")
}
// store current version in-memory
impl.serverDataStore.CurrentVersion = currentVersion
} else {
serverEnvConfig.DevtronInstallationType = serverBean.DevtronInstallationTypeOssKubectl
}
return impl
}