Skip to content

Commit 70fddcb

Browse files
committed
Refactoring: use yaml file instead of env vars to load GSM project config
1 parent c96dc28 commit 70fddcb

3 files changed

Lines changed: 21 additions & 15 deletions

File tree

cmd/ci-operator/main.go

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -458,6 +458,8 @@ type options struct {
458458
enableSecretsStoreCSIDriver bool
459459
gsmConfigPath string
460460
gsmConfig api.GSMConfig
461+
gsmProjectConfigPath string
462+
gsmProjectConfig gsm.Config
461463
gsmCredentialsFile string
462464

463465
metricsAgent *metrics.MetricsAgent
@@ -514,7 +516,8 @@ func bindOptions(flag *flag.FlagSet) *options {
514516
flag.StringVar(&opt.impersonateUser, "as", "", "Username to impersonate")
515517
flag.BoolVar(&opt.restrictNetworkAccess, "restrict-network-access", false, "Restrict network access to 10.0.0.0/8 (RedHat intranet).")
516518
flag.BoolVar(&opt.enableSecretsStoreCSIDriver, "enable-secrets-store-csi-driver", false, "Use Secrets Store CSI driver for accessing multi-stage credentials.")
517-
flag.StringVar(&opt.gsmConfigPath, "gsm-config", "", "Path to the gsm config file.")
519+
flag.StringVar(&opt.gsmConfigPath, "gsm-config", "", "Path to the gsm secrets config file.")
520+
flag.StringVar(&opt.gsmProjectConfigPath, "gsm-project-config", "", "Path to the GSM project config file.")
518521
flag.StringVar(&opt.gsmCredentialsFile, "gsm-credentials-file", "", "Path to GCP service account credentials.")
519522

520523
// flags needed for the configresolver
@@ -769,8 +772,10 @@ func (o *options) Complete() error {
769772
handleTargetAdditionalSuffix(o)
770773

771774
if o.enableSecretsStoreCSIDriver {
772-
err := api.LoadGSMConfigFromFile(o.gsmConfigPath, &o.gsmConfig)
773-
if err != nil {
775+
if err := api.LoadGSMConfigFromFile(o.gsmConfigPath, &o.gsmConfig); err != nil {
776+
return err
777+
}
778+
if err = api.LoadGSMProjectConfigFromFile(o.gsmProjectConfigPath, &o.gsmProjectConfig); err != nil {
774779
return err
775780
}
776781
}
@@ -1062,10 +1067,6 @@ func (o *options) Run() (errs []error) {
10621067

10631068
var gsmConfig *multi_stage.GSMConfiguration
10641069
if o.enableSecretsStoreCSIDriver {
1065-
gsmProjectConfig, err := gsm.GetConfigFromEnv()
1066-
if err != nil {
1067-
return []error{results.ForReason("gsm_config").WithError(err).Errorf("failed to get GSM project config from environment: %v", err)}
1068-
}
10691070
var opts []option.ClientOption
10701071
if o.gsmCredentialsFile != "" {
10711072
opts = append(opts, option.WithCredentialsFile(o.gsmCredentialsFile))
@@ -1082,7 +1083,7 @@ func (o *options) Run() (errs []error) {
10821083
gsmConfig = &multi_stage.GSMConfiguration{
10831084
Config: &o.gsmConfig,
10841085
CredentialsFile: o.gsmCredentialsFile,
1085-
ProjectConfig: gsmProjectConfig,
1086+
ProjectConfig: o.gsmProjectConfig,
10861087
Client: gsmClient,
10871088
}
10881089
}

pkg/api/gsm.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
utilerrors "k8s.io/apimachinery/pkg/util/errors"
1010
"sigs.k8s.io/yaml"
1111

12+
gsm "github.com/openshift/ci-tools/pkg/gsm-secrets"
1213
gsmvalidation "github.com/openshift/ci-tools/pkg/gsm-validation"
1314
"github.com/openshift/ci-tools/pkg/util/gzip"
1415
)
@@ -91,6 +92,15 @@ func LoadGSMConfigFromFile(file string, config *GSMConfig) error {
9192
return yaml.UnmarshalStrict(bytes, config)
9293
}
9394

95+
// LoadGSMProjectConfigFromFile loads a GSM project configuration from a YAML file
96+
func LoadGSMProjectConfigFromFile(file string, config *gsm.Config) error {
97+
bytes, err := gzip.ReadFileMaybeGZIP(file)
98+
if err != nil {
99+
return fmt.Errorf("couldn't read GSM project config file: %w", err)
100+
}
101+
return yaml.UnmarshalStrict(bytes, config)
102+
}
103+
94104
func (c *GSMConfig) UnmarshalJSON(d []byte) error {
95105
type Alias GSMConfig
96106
aux := (*Alias)(c)

pkg/gsm-secrets/types.go

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,8 @@ const (
3434
)
3535

3636
type Config struct {
37-
ProjectIdString string
38-
ProjectIdNumber string
39-
}
40-
41-
var Production = Config{
42-
ProjectIdString: "openshift-ci-secrets",
43-
ProjectIdNumber: "384486694155",
37+
ProjectIdString string `json:"GCP_PROJECT_ID" yaml:"GCP_PROJECT_ID"`
38+
ProjectIdNumber string `json:"GCP_PROJECT_NUMBER" yaml:"GCP_PROJECT_NUMBER"`
4439
}
4540

4641
func (c Config) GetSecretAccessorRole() string {

0 commit comments

Comments
 (0)