Merge pull request #2334 from ellis-johnson/create-update-aro-openshift-versions-command

Create update OpenShift versions command
This commit is contained in:
David Newman 2022-08-30 12:24:04 +10:00 коммит произвёл GitHub
Родитель a1ed324037 8587b0f5b0
Коммит 0609cee051
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
2 изменённых файлов: 180 добавлений и 0 удалений

Просмотреть файл

@ -29,6 +29,7 @@ func usage() {
fmt.Fprintf(flag.CommandLine.Output(), " %s portal\n", os.Args[0])
fmt.Fprintf(flag.CommandLine.Output(), " %s rp\n", os.Args[0])
fmt.Fprintf(flag.CommandLine.Output(), " %s operator {master,worker}\n", os.Args[0])
fmt.Fprintf(flag.CommandLine.Output(), " %s update-versions\n", os.Args[0])
flag.PrintDefaults()
}
@ -79,6 +80,9 @@ func main() {
case "operator":
checkArgs(2)
err = operator(ctx, log)
case "update-versions":
checkArgs(1)
err = updateOCPVersions(ctx, log)
default:
usage()
os.Exit(2)

Просмотреть файл

@ -0,0 +1,176 @@
package main
// Copyright (c) Microsoft Corporation.
// Licensed under the Apache License 2.0.
import (
"context"
"fmt"
"os"
"github.com/sirupsen/logrus"
"github.com/Azure/ARO-RP/pkg/api"
"github.com/Azure/ARO-RP/pkg/database"
"github.com/Azure/ARO-RP/pkg/env"
"github.com/Azure/ARO-RP/pkg/metrics/statsd"
"github.com/Azure/ARO-RP/pkg/util/encryption"
"github.com/Azure/ARO-RP/pkg/util/keyvault"
"github.com/Azure/ARO-RP/pkg/util/version"
)
func getLatestOCPVersions(ctx context.Context, log *logrus.Entry) ([]api.OpenShiftVersion, error) {
env, err := env.NewCoreForCI(ctx, log)
if err != nil {
return nil, err
}
dstAcr := os.Getenv("DST_ACR_NAME")
acrDomainSuffix := "." + env.Environment().ContainerRegistryDNSSuffix
dstRepo := dstAcr + acrDomainSuffix
var (
OpenshiftVersions = []api.OpenShiftVersion{
{
Version: version.InstallStream.Version.String(),
OpenShiftPullspec: version.InstallStream.PullSpec,
InstallerPullspec: dstRepo + "/aro-installer:release-4.10",
Enabled: true,
},
}
)
return OpenshiftVersions, nil
}
func getVersionsDatabase(ctx context.Context, log *logrus.Entry) (database.OpenShiftVersions, error) {
_env, err := env.NewCore(ctx, log)
if err != nil {
return nil, err
}
for _, key := range []string{
"DST_ACR_NAME",
} {
if _, found := os.LookupEnv(key); !found {
return nil, fmt.Errorf("environment variable %q unset", key)
}
}
if !_env.IsLocalDevelopmentMode() {
for _, key := range []string{
"MDM_ACCOUNT",
"MDM_NAMESPACE",
} {
if _, found := os.LookupEnv(key); !found {
return nil, fmt.Errorf("environment variable %q unset", key)
}
}
}
msiAuthorizer, err := _env.NewMSIAuthorizer(env.MSIContextRP, _env.Environment().ResourceManagerEndpoint)
if err != nil {
return nil, fmt.Errorf("MSI Authorizer failed with: %s", err.Error())
}
msiKVAuthorizer, err := _env.NewMSIAuthorizer(env.MSIContextRP, _env.Environment().ResourceIdentifiers.KeyVault)
if err != nil {
return nil, fmt.Errorf("MSI KeyVault Authorizer failed with: %s", err.Error())
}
m := statsd.New(ctx, log.WithField("component", "update-ocp-versions"), _env, os.Getenv("MDM_ACCOUNT"), os.Getenv("MDM_NAMESPACE"), os.Getenv("MDM_STATSD_SOCKET"))
serviceKeyvaultURI, err := keyvault.URI(_env, env.ServiceKeyvaultSuffix)
if err != nil {
return nil, err
}
serviceKeyvault := keyvault.NewManager(msiKVAuthorizer, serviceKeyvaultURI)
aead, err := encryption.NewMulti(ctx, serviceKeyvault, env.EncryptionSecretV2Name, env.EncryptionSecretName)
if err != nil {
return nil, err
}
dbAuthorizer, err := database.NewMasterKeyAuthorizer(ctx, _env, msiAuthorizer)
if err != nil {
return nil, err
}
dbc, err := database.NewDatabaseClient(log.WithField("component", "database"), _env, dbAuthorizer, m, aead)
if err != nil {
return nil, err
}
dbOpenShiftVersions, err := database.NewOpenShiftVersions(ctx, _env.IsLocalDevelopmentMode(), dbc)
if err != nil {
return nil, err
}
return dbOpenShiftVersions, nil
}
func updateOpenShiftVersions(ctx context.Context, dbOpenShiftVersions database.OpenShiftVersions, log *logrus.Entry) error {
existingVersions, err := dbOpenShiftVersions.ListAll(ctx)
if err != nil {
return err
}
latestVersions, err := getLatestOCPVersions(ctx, log)
if err != nil {
return err
}
newVersions := make(map[string]api.OpenShiftVersion)
for _, doc := range latestVersions {
newVersions[doc.Version] = doc
}
for _, doc := range existingVersions.OpenShiftVersionDocuments {
existing, found := newVersions[doc.OpenShiftVersion.Version]
if found {
log.Printf("Found Version %q, patching", existing.Version)
_, err := dbOpenShiftVersions.Patch(ctx, doc.ID, func(inFlightDoc *api.OpenShiftVersionDocument) error {
inFlightDoc.OpenShiftVersion = &existing
return nil
})
if err != nil {
return err
}
log.Printf("Version %q found", existing.Version)
delete(newVersions, existing.Version)
continue
}
log.Printf("Version %q not found, deleting", doc.OpenShiftVersion.Version)
err := dbOpenShiftVersions.Delete(ctx, doc)
if err != nil {
return err
}
}
for _, doc := range newVersions {
log.Printf("Version %q not found in database, creating", doc.Version)
newDoc := api.OpenShiftVersionDocument{
ID: dbOpenShiftVersions.NewUUID(),
OpenShiftVersion: &doc,
}
_, err := dbOpenShiftVersions.Create(ctx, &newDoc)
if err != nil {
return err
}
}
return nil
}
func updateOCPVersions(ctx context.Context, log *logrus.Entry) error {
dbOpenShiftVersions, err := getVersionsDatabase(ctx, log)
if err != nil {
return err
}
err = updateOpenShiftVersions(ctx, dbOpenShiftVersions, log)
if err != nil {
return err
}
return nil
}