@@ -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
3236func (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"
177182func (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.
0 commit comments