Skip to content

Commit e991102

Browse files
authored
Merge pull request #958 from Vafilor/feat/cache.values
feat: cache artifactRepositoryType
2 parents d9c7937 + 467f7f7 commit e991102

File tree

2 files changed

+50
-11
lines changed

2 files changed

+50
-11
lines changed

pkg/client.go

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,17 @@ import (
55
sq "github.com/Masterminds/squirrel"
66
argoprojv1alpha1 "github.com/argoproj/argo/pkg/client/clientset/versioned/typed/workflow/v1alpha1"
77
"github.com/jmoiron/sqlx"
8+
"github.com/onepanelio/core/pkg/util"
89
"github.com/onepanelio/core/pkg/util/env"
910
"github.com/onepanelio/core/pkg/util/gcs"
1011
"github.com/onepanelio/core/pkg/util/router"
1112
"github.com/onepanelio/core/pkg/util/s3"
1213
log "github.com/sirupsen/logrus"
14+
"google.golang.org/grpc/codes"
1315
"k8s.io/client-go/kubernetes"
1416
"k8s.io/client-go/rest"
1517
"k8s.io/client-go/tools/clientcmd"
18+
"sigs.k8s.io/yaml"
1619
"strconv"
1720
"time"
1821
)
@@ -27,6 +30,7 @@ type Client struct {
2730
argoprojV1alpha1 argoprojv1alpha1.ArgoprojV1alpha1Interface
2831
*DB
2932
systemConfig SystemConfig
33+
cache map[string]interface{}
3034
}
3135

3236
func (c *Client) ArgoprojV1alpha1() argoprojv1alpha1.ArgoprojV1alpha1Interface {
@@ -102,6 +106,7 @@ func NewClient(config *Config, db *DB, systemConfig SystemConfig) (client *Clien
102106
argoprojV1alpha1: argoClient,
103107
DB: db,
104108
systemConfig: systemConfig,
109+
cache: make(map[string]interface{}),
105110
}, nil
106111
}
107112

@@ -175,7 +180,12 @@ func (c *Client) GetWebRouter() (router.Web, error) {
175180
// GetArtifactRepositoryType returns the configured artifact repository type for the given namespace.
176181
// possible return values are: "s3", "gcs"
177182
func (c *Client) GetArtifactRepositoryType(namespace string) (string, error) {
178-
artifactRepositoryType := "s3"
183+
artifactRepositoryType, ok := c.cache["artifactRepositoryType"]
184+
if ok {
185+
return artifactRepositoryType.(string), nil
186+
}
187+
188+
artifactRepositoryType = "s3"
179189
nsConfig, err := c.GetNamespaceConfig(namespace)
180190
if err != nil {
181191
return "", err
@@ -184,7 +194,38 @@ func (c *Client) GetArtifactRepositoryType(namespace string) (string, error) {
184194
artifactRepositoryType = "gcs"
185195
}
186196

187-
return artifactRepositoryType, nil
197+
c.cache["artifactRepositoryType"] = artifactRepositoryType
198+
199+
return artifactRepositoryType.(string), nil
200+
}
201+
202+
// GetArtifactRepositorySource returns the original source for the artifact repository
203+
// This can be s3, abs, gcs, etc. Since everything goes through an S3 compatible API,
204+
// it is sometimes useful to know the source.
205+
func (c *Client) GetArtifactRepositorySource(namespace string) (string, error) {
206+
configMap, err := c.getConfigMap(namespace, "onepanel")
207+
if err != nil {
208+
log.WithFields(log.Fields{
209+
"Namespace": namespace,
210+
"Error": err.Error(),
211+
}).Error("getArtifactRepositorySource failed getting config map.")
212+
return "", err
213+
}
214+
215+
config := &NamespaceConfig{
216+
ArtifactRepository: ArtifactRepositoryProvider{},
217+
}
218+
219+
err = yaml.Unmarshal([]byte(configMap.Data["artifactRepository"]), &config.ArtifactRepository)
220+
if err != nil || (config.ArtifactRepository.S3 == nil && config.ArtifactRepository.GCS == nil) {
221+
return "", util.NewUserError(codes.NotFound, "Artifact repository config not found.")
222+
}
223+
224+
if config.ArtifactRepository.S3 != nil {
225+
return config.ArtifactRepository.S3.Source, nil
226+
}
227+
228+
return config.ArtifactRepository.GCS.Source, nil
188229
}
189230

190231
// getKubernetesTimeout returns the timeout for kubernetes requests.

pkg/config.go

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ func (c *Client) getConfigMap(namespace, name string) (configMap *ConfigMap, err
2626
// GetSystemConfig will pull it from the resources
2727
func (c *Client) ClearSystemConfigCache() {
2828
c.systemConfig = nil
29+
c.cache = make(map[string]interface{})
2930
}
3031

3132
// GetSystemConfig loads various system configurations and bundles them into a map.
@@ -90,17 +91,14 @@ func (c *Client) GetNamespaceConfig(namespace string) (config *NamespaceConfig,
9091
return
9192
}
9293

93-
switch {
94-
case config.ArtifactRepository.S3 != nil:
95-
{
96-
accessKey, _ := base64.StdEncoding.DecodeString(secret.Data[config.ArtifactRepository.S3.AccessKeySecret.Key])
97-
config.ArtifactRepository.S3.AccessKey = string(accessKey)
98-
secretKey, _ := base64.StdEncoding.DecodeString(secret.Data[config.ArtifactRepository.S3.SecretKeySecret.Key])
99-
config.ArtifactRepository.S3.Secretkey = string(secretKey)
100-
}
101-
default:
94+
if config.ArtifactRepository.S3 == nil {
10295
return nil, util.NewUserError(codes.NotFound, "Artifact repository config not found.")
10396
}
10497

98+
accessKey, _ := base64.StdEncoding.DecodeString(secret.Data[config.ArtifactRepository.S3.AccessKeySecret.Key])
99+
config.ArtifactRepository.S3.AccessKey = string(accessKey)
100+
secretKey, _ := base64.StdEncoding.DecodeString(secret.Data[config.ArtifactRepository.S3.SecretKeySecret.Key])
101+
config.ArtifactRepository.S3.Secretkey = string(secretKey)
102+
105103
return
106104
}

0 commit comments

Comments
 (0)