diff --git a/.github/workflows/build.yaml b/.github/workflows/build.yaml index 3bfcaa80..b9777f07 100644 --- a/.github/workflows/build.yaml +++ b/.github/workflows/build.yaml @@ -20,7 +20,13 @@ jobs: build: name: Build & Test runs-on: ubuntu-latest + outputs: + registry: ${{ steps.registry.outputs.registry }} # workaround since env is not available outside of steps, i.e. in calling external workflows like we later do in e2e-test + steps: + - id: registry + run: echo "registry=${{ env.REGISTRY }}" >> "$GITHUB_OUTPUT" + - name: Check out the code uses: actions/checkout@v2 with: @@ -59,6 +65,15 @@ jobs: build-args: | "VERSION=0.0.${{ github.run_id }}" + helm-e2e-test: + uses: otterize/helm-charts/.github/workflows/e2e-test.yaml@main + name: Trigger e2e tests from helm charts repo + secrets: inherit + with: + gcr-registry: ${{ needs.build.outputs.registry }} + credentials-operator-tag: ${{ github.sha }} + needs: + - build tag-latest: name: Tag latest diff --git a/src/operator/controllers/poduserpassword/db_credentials_pod_reconciler.go b/src/operator/controllers/poduserpassword/db_credentials_pod_reconciler.go index bde04771..7d02ac13 100644 --- a/src/operator/controllers/poduserpassword/db_credentials_pod_reconciler.go +++ b/src/operator/controllers/poduserpassword/db_credentials_pod_reconciler.go @@ -296,17 +296,86 @@ func (e *Reconciler) runAlterPasswordForSecrets(ctx context.Context, secrets []v return nil } +func (e *Reconciler) extractDBCredentials(ctx context.Context, namespace string, credentialsSpec otterizev1alpha3.DatabaseCredentials) (databaseconfigurator.DatabaseCredentials, error) { + creds := databaseconfigurator.DatabaseCredentials{} + if credentialsSpec.Username != "" { + creds.Username = credentialsSpec.Username + } + if credentialsSpec.Password != "" { + creds.Password = credentialsSpec.Password + } + if credentialsSpec.SecretRef != nil { + secret := v1.Secret{} + name := credentialsSpec.SecretRef.Name + if credentialsSpec.SecretRef.Namespace != "" { + namespace = credentialsSpec.SecretRef.Namespace + } + err := e.client.Get(ctx, client.ObjectKey{Name: name, Namespace: namespace}, &secret) + if err != nil { + return creds, errors.Wrap(err) + } + if username, ok := secret.Data[credentialsSpec.SecretRef.UsernameKey]; ok { + creds.Username = string(username) + } + if password, ok := secret.Data[credentialsSpec.SecretRef.PasswordKey]; ok { + creds.Password = string(password) + } + } + + if creds.Username == "" || creds.Password == "" { + return creds, errors.New("credentials missing either username or password") + } + + return creds, nil +} + +func (e *Reconciler) createPostgresDBConfigurator(ctx context.Context, pgServerConfig otterizev1alpha3.PostgreSQLServerConfig) (databaseconfigurator.DatabaseConfigurator, error) { + credentials, err := e.extractDBCredentials(ctx, pgServerConfig.Namespace, pgServerConfig.Spec.Credentials) + if err != nil { + return nil, errors.Wrap(err) + } + + dbInfo := postgres.PostgresDatabaseInfo{ + Credentials: credentials, + Address: pgServerConfig.Spec.Address, + } + + dbconfigurator, err := postgres.NewPostgresConfigurator(ctx, dbInfo) + if err != nil { + return nil, errors.Wrap(err) + } + return dbconfigurator, nil +} + +func (e *Reconciler) createMySQLDBConfigurator(ctx context.Context, mySQLServerConfig otterizev1alpha3.MySQLServerConfig) (databaseconfigurator.DatabaseConfigurator, error) { + credentials, err := e.extractDBCredentials(ctx, mySQLServerConfig.Namespace, mySQLServerConfig.Spec.Credentials) + if err != nil { + return nil, errors.Wrap(err) + } + + dbInfo := mysql.MySQLDatabaseInfo{ + Credentials: credentials, + Address: mySQLServerConfig.Spec.Address, + } + + dbconfigurator, err := mysql.NewMySQLConfigurator(ctx, dbInfo) + if err != nil { + return nil, errors.Wrap(err) + } + return dbconfigurator, nil +} + func (e *Reconciler) createDBConfigurator( ctx context.Context, database string, mysqlServerConfigs []otterizev1alpha3.MySQLServerConfig, pgServerConfigs []otterizev1alpha3.PostgreSQLServerConfig) (databaseconfigurator.DatabaseConfigurator, bool, error) { - mysqlConf, found := lo.Find(mysqlServerConfigs, func(config otterizev1alpha3.MySQLServerConfig) bool { + mysqlServerConf, found := lo.Find(mysqlServerConfigs, func(config otterizev1alpha3.MySQLServerConfig) bool { return config.Name == database }) if found { - dbconfigurator, err := mysql.NewMySQLConfigurator(ctx, mysqlConf.Spec) + dbconfigurator, err := e.createMySQLDBConfigurator(ctx, mysqlServerConf) if err != nil { return nil, false, errors.Wrap(err) } @@ -317,7 +386,7 @@ func (e *Reconciler) createDBConfigurator( return config.Name == database }) if found { - dbconfigurator, err := postgres.NewPostgresConfigurator(ctx, pgServerConf.Spec) + dbconfigurator, err := e.createPostgresDBConfigurator(ctx, pgServerConf) if err != nil { return nil, false, errors.Wrap(err) } @@ -359,7 +428,7 @@ func closeAllConnections(ctx context.Context, allConfigurators []databaseconfigu func (e *Reconciler) GetAllDBConfigurators(ctx context.Context, mysqlServerConfigs []otterizev1alpha3.MySQLServerConfig, pgServerConfigs []otterizev1alpha3.PostgreSQLServerConfig) []databaseconfigurator.DatabaseConfigurator { configurators := make([]databaseconfigurator.DatabaseConfigurator, 0) for _, mysqlServerConfig := range mysqlServerConfigs { - dbconfigurator, err := mysql.NewMySQLConfigurator(ctx, mysqlServerConfig.Spec) + dbconfigurator, err := e.createMySQLDBConfigurator(ctx, mysqlServerConfig) if err != nil { logrus.WithError(err).Errorf("Failed to create configurator for MySQL server config: %s", mysqlServerConfig.Name) continue @@ -367,7 +436,7 @@ func (e *Reconciler) GetAllDBConfigurators(ctx context.Context, mysqlServerConfi configurators = append(configurators, dbconfigurator) } for _, pgServerConfig := range pgServerConfigs { - dbconfigurator, err := postgres.NewPostgresConfigurator(ctx, pgServerConfig.Spec) + dbconfigurator, err := e.createPostgresDBConfigurator(ctx, pgServerConfig) if err != nil { logrus.WithError(err).Errorf("Failed to create configurator for PostgreSQL server config: %s", pgServerConfig.Name) continue diff --git a/src/operator/go.mod b/src/operator/go.mod index c08cf6f8..ac55536a 100644 --- a/src/operator/go.mod +++ b/src/operator/go.mod @@ -117,7 +117,7 @@ require ( github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect github.com/modern-go/reflect2 v1.0.2 // indirect github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect - github.com/otterize/intents-operator/src v0.0.0-20240521104220-ba00b7c59637 // indirect + github.com/otterize/intents-operator/src v0.0.0-20240623163818-27d9ebb4f4eb // indirect github.com/otterize/nilable v0.0.0-20240410132629-f242bb6f056f // indirect github.com/pelletier/go-toml v1.9.5 // indirect github.com/pelletier/go-toml/v2 v2.0.8 // indirect diff --git a/src/operator/go.sum b/src/operator/go.sum index dc2bec38..5e3c8d5e 100644 --- a/src/operator/go.sum +++ b/src/operator/go.sum @@ -436,6 +436,16 @@ github.com/otterize/intents-operator/src v0.0.0-20240521053840-36662b8fd8fa h1:R github.com/otterize/intents-operator/src v0.0.0-20240521053840-36662b8fd8fa/go.mod h1:7vDL6/NAo7AobUGqDGU/277xGyb0KTRQoqRjoouhh44= github.com/otterize/intents-operator/src v0.0.0-20240521104220-ba00b7c59637 h1:fhtXDgHYymOrHaAdaBg7kTs8D53u35nmGXYLiDhVjtU= github.com/otterize/intents-operator/src v0.0.0-20240521104220-ba00b7c59637/go.mod h1:7vDL6/NAo7AobUGqDGU/277xGyb0KTRQoqRjoouhh44= +github.com/otterize/intents-operator/src v0.0.0-20240618094714-4c1711705a94 h1:WdA+1YWuYc75WGuhQ2XGdZGD/PQQq/Qoml3CJYYs1xE= +github.com/otterize/intents-operator/src v0.0.0-20240618094714-4c1711705a94/go.mod h1:7vDL6/NAo7AobUGqDGU/277xGyb0KTRQoqRjoouhh44= +github.com/otterize/intents-operator/src v0.0.0-20240623111815-b96773bfb6c6 h1:M5ZLoxXTpm5u3MV3c0M5WOsIB03ESLZiHDjTl5y2xYg= +github.com/otterize/intents-operator/src v0.0.0-20240623111815-b96773bfb6c6/go.mod h1:7vDL6/NAo7AobUGqDGU/277xGyb0KTRQoqRjoouhh44= +github.com/otterize/intents-operator/src v0.0.0-20240623113422-6820f7294c78 h1:YDVGIeLdoLqSZfF9eRJyBP0GRK7UsIoWWCUQcX9qW5M= +github.com/otterize/intents-operator/src v0.0.0-20240623113422-6820f7294c78/go.mod h1:7vDL6/NAo7AobUGqDGU/277xGyb0KTRQoqRjoouhh44= +github.com/otterize/intents-operator/src v0.0.0-20240623153715-3086fe975a9f h1:60VQ/nyov3Pqq6NCBPrf4VjJzpsfjSsC6L7Zw9qot8Q= +github.com/otterize/intents-operator/src v0.0.0-20240623153715-3086fe975a9f/go.mod h1:7vDL6/NAo7AobUGqDGU/277xGyb0KTRQoqRjoouhh44= +github.com/otterize/intents-operator/src v0.0.0-20240623163818-27d9ebb4f4eb h1:aGBbne9jtKspb0fgvQHpIgN5t9mErIMXBNp1Y7XEx8g= +github.com/otterize/intents-operator/src v0.0.0-20240623163818-27d9ebb4f4eb/go.mod h1:7vDL6/NAo7AobUGqDGU/277xGyb0KTRQoqRjoouhh44= github.com/otterize/lox v0.0.0-20220525164329-9ca2bf91c3dd h1:7Sb95VrtAPb9m2ewtqLnX1oeKQy03dt7yr6F/hP7Htg= github.com/otterize/lox v0.0.0-20220525164329-9ca2bf91c3dd/go.mod h1:RXvgymN8MxiELFkmGHzJ23KJU2ObVsNsNSM80/HO8qQ= github.com/otterize/nilable v0.0.0-20240410132629-f242bb6f056f h1:gv92189CW53A+Y0UQ550zr6RfCBYqvYJ8oq6Jll1YqQ= diff --git a/src/operator/main.go b/src/operator/main.go index 40677b15..56be8e54 100644 --- a/src/operator/main.go +++ b/src/operator/main.go @@ -118,10 +118,6 @@ func initSpireClient(ctx context.Context, spireServerAddr string) (spireclient.S } func main() { - errorreporter.Init("credentials-operator", version.Version(), viper.GetString(operatorconfig.TelemetryErrorsAPIKeyKey)) - defer errorreporter.AutoNotify() - shared.RegisterPanicHandlers() - var secretsManager tls_pod.SecretsManager var workloadRegistry tls_pod.WorkloadRegistry @@ -132,8 +128,20 @@ func main() { TimestampFormat: time.RFC3339, }) + signalHandlerCtx := ctrl.SetupSignalHandler() + + clusterUID, err := clusterutils.GetOrCreateClusterUID(signalHandlerCtx) + if err != nil { + logrus.WithError(err).Panic("Failed obtaining cluster ID") + } + componentinfo.SetGlobalContextId(telemetrysender.Anonymize(clusterUID)) + ctrl.SetLogger(logrusr.New(logrus.StandardLogger())) + errorreporter.Init(telemetriesgql.TelemetryComponentTypeCredentialsOperator, version.Version()) + defer errorreporter.AutoNotify() + shared.RegisterPanicHandlers() + mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{ Scheme: scheme, Metrics: server.Options{ @@ -148,7 +156,6 @@ func main() { logrus.WithError(err).Panic("unable to initialize manager") } - signalHandlerCtx := ctrl.SetupSignalHandler() podNamespace := os.Getenv("POD_NAMESPACE") if podNamespace == "" { logrus.Panic("POD_NAMESPACE environment variable is required") @@ -166,13 +173,6 @@ func main() { logrus.WithError(err).Panic("unable to ensure otterize CRDs") } - clusterUID, err := clusterutils.GetOrCreateClusterUID(signalHandlerCtx) - if err != nil { - logrus.WithError(err).Panic("Failed fetching cluster UID") - } - componentinfo.SetGlobalContextId(telemetrysender.Anonymize(clusterUID)) - componentinfo.SetGlobalVersion(version.Version()) - serviceIdResolver := serviceidresolver.NewResolver(mgr.GetClient()) eventRecorder := mgr.GetEventRecorderFor("credentials-operator")