Skip to content

Commit

Permalink
Merge pull request #658 from ritmun/sdcicd-1418
Browse files Browse the repository at this point in the history
SDCICD-1418 add cmd option to promote operator saas through canary hives
  • Loading branch information
openshift-merge-bot[bot] authored Feb 7, 2025
2 parents aaab7c1 + 8dd61fc commit 2b93fdf
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 49 deletions.
69 changes: 64 additions & 5 deletions cmd/promote/git/app_interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,27 @@ import (
"os"
"os/exec"
"path/filepath"
"regexp"
"strings"

kyaml "sigs.k8s.io/kustomize/kyaml/yaml"

"gopkg.in/yaml.v3"
)

const (
canaryStr = "-prod-canary"
prodHiveStr = "hivep"
resourcePath = "/hack/olm-registry/olm-artifacts-template.yaml"
)

type Service struct {
Name string `yaml:"name"`
ResourceTemplates []struct {
Name string `yaml:"name"`
URL string `yaml:"url"`
Targets []struct {
Name string
Namespace map[string]string `yaml:"namespace"`
Ref string `yaml:"ref"`
Parameters map[string]interface{} `yaml:"parameters"`
Expand All @@ -28,6 +38,35 @@ type AppInterface struct {
GitDirectory string
}

// replaceTargetSha replaces sha for targets in file whose name matches a given substring
// returns updated yaml, error, and false if no targets were found.
func replaceTargetSha(fileContent string, targetSuffix string, promotionGitHash string) (string, error, bool) {
node, err := kyaml.Parse(fileContent)
if err != nil {
return "", fmt.Errorf("error parsing saas YAML: %v", err), false
}
targetFound := false
targets, err := kyaml.Lookup("resourceTemplates", "[path="+resourcePath+"]", "targets").Filter(node)
if err != nil {
return "", fmt.Errorf("error querying saas YAML: %v", err), false
}
err = targets.VisitElements(func(element *kyaml.RNode) error {
name, _ := element.GetString("name")
match, _ := regexp.MatchString("(.*)"+targetSuffix, name)
if match {
targetFound = true
fmt.Println("updating target: ", name)
_, err = element.Pipe(kyaml.SetField("ref", kyaml.NewStringRNode(promotionGitHash)))
if err != nil {
return fmt.Errorf("error setting ref: %v", err)
}
}
return nil
})

return node.MustString(), err, targetFound
}

func DefaultAppInterfaceDirectory() string {
return filepath.Join(os.Getenv("HOME"), "git", "app-interface")
}
Expand Down Expand Up @@ -141,11 +180,19 @@ func GetCurrentGitHashFromAppInterface(saarYamlFile []byte, serviceName string,
for _, resourceTemplate := range service.ResourceTemplates {
if !strings.Contains(resourceTemplate.Name, "package") {
for _, target := range resourceTemplate.Targets {
if strings.Contains(target.Namespace["$ref"], "hivep") {
currentGitHash = target.Ref
if strings.Contains(target.Name, canaryStr) {
currentGitHash = target.Ref // get canary target ref
break
}
}
if currentGitHash == "" { // canary targets not found
for _, target := range resourceTemplate.Targets {
if strings.Contains(target.Namespace["$ref"], prodHiveStr) {
currentGitHash = target.Ref
break
}
}
}
}
}
}
Expand Down Expand Up @@ -187,7 +234,7 @@ func GetCurrentPackageTagFromAppInterface(saasFile string) (string, error) {
for _, resourceTemplate := range service.ResourceTemplates {
if strings.Contains(resourceTemplate.Name, "package") {
for _, target := range resourceTemplate.Targets {
if strings.Contains(target.Namespace["$ref"], "hivep") {
if strings.Contains(target.Namespace["$ref"], prodHiveStr) {
currentPackageTag = target.Parameters["PACKAGE_TAG"].(string)
}
}
Expand Down Expand Up @@ -223,9 +270,21 @@ func (a AppInterface) UpdateAppInterface(serviceName, saasFile, currentGitHash,
if err != nil {
return fmt.Errorf("failed to read file %s: %v", saasFile, err)
}
var newContent string

// Replace the hash in the file content
newContent := strings.ReplaceAll(string(fileContent), currentGitHash, promotionGitHash)
// If canary targets are set up in saas, replace the hash only in canary targets in the file content
// Otherwise proceed to promoting to all prod hives.
newContent, err, canaryTargetsSetUp := replaceTargetSha(string(fileContent), canaryStr, promotionGitHash)
if err != nil {
return fmt.Errorf("error modifying YAML: %v", err)
}
if !canaryTargetsSetUp {
fmt.Println("canary targets not set, continuing to promote all production hives.")
newContent, err, _ = replaceTargetSha(string(fileContent), prodHiveStr, promotionGitHash)
}
if err != nil {
return fmt.Errorf("error modifying YAML: %v", err)
}

err = os.WriteFile(saasFile, []byte(newContent), 0644)
if err != nil {
Expand Down
1 change: 0 additions & 1 deletion cmd/promote/saas/saas.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ func NewCmdSaas() *cobra.Command {
}

os.Exit(0)

},
}

Expand Down
3 changes: 0 additions & 3 deletions cmd/promote/saas/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,6 @@ func servicePromotion(appInterface git.AppInterface, serviceName, gitHash string
}
fmt.Printf("Service: %s will be promoted to %s\n", serviceName, promotionGitHash)

if err != nil {
return fmt.Errorf("error in executing git log: %v", err)
}
branchName := fmt.Sprintf("promote-%s-%s", serviceName, promotionGitHash)
err = appInterface.UpdateAppInterface(serviceName, saasDir, currentGitHash, promotionGitHash, branchName)
if err != nil {
Expand Down
25 changes: 13 additions & 12 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,11 @@ require (
github.com/spf13/pflag v1.0.6-0.20210604193023-d5e0c0615ace
github.com/spf13/viper v1.19.0
github.com/stretchr/testify v1.9.0
github.com/xanzy/go-gitlab v0.109.0
github.com/xanzy/go-gitlab v0.115.0
go.uber.org/mock v0.4.0
golang.org/x/oauth2 v0.23.0
golang.org/x/sync v0.8.0
golang.org/x/term v0.25.0
golang.org/x/sync v0.10.0
golang.org/x/term v0.27.0
google.golang.org/api v0.171.0
google.golang.org/genproto v0.0.0-20240311173647-c811ad7063a7
gopkg.in/yaml.v2 v2.4.0
Expand All @@ -69,6 +69,7 @@ require (
open-cluster-management.io/api v0.13.0
sigs.k8s.io/cluster-api-provider-aws/v2 v2.3.1
sigs.k8s.io/controller-runtime v0.16.3
sigs.k8s.io/kustomize/kyaml v0.17.1
sigs.k8s.io/yaml v1.4.0
)

Expand Down Expand Up @@ -101,6 +102,7 @@ require (
github.com/cenkalti/backoff/v4 v4.3.0 // indirect
github.com/cespare/xxhash/v2 v2.3.0 // indirect
github.com/chai2010/gettext-go v1.0.2 // indirect
github.com/creack/pty v1.1.20 // indirect
github.com/danieljoos/wincred v1.2.0 // indirect
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
github.com/deepmap/oapi-codegen v1.12.4 // indirect
Expand Down Expand Up @@ -162,7 +164,7 @@ require (
github.com/mailru/easyjson v0.7.7 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/mattn/go-runewidth v0.0.14 // indirect
github.com/mattn/go-runewidth v0.0.16 // indirect
github.com/mgutz/ansi v0.0.0-20200706080929-d51e80ef957d // indirect
github.com/microcosm-cc/bluemonday v1.0.27 // indirect
github.com/mitchellh/go-homedir v1.1.0 // indirect
Expand Down Expand Up @@ -190,7 +192,7 @@ require (
github.com/prometheus/client_model v0.6.1 // indirect
github.com/prometheus/common v0.60.0 // indirect
github.com/prometheus/procfs v0.15.1 // indirect
github.com/rivo/uniseg v0.4.2 // indirect
github.com/rivo/uniseg v0.4.7 // indirect
github.com/russross/blackfriday/v2 v2.1.0 // indirect
github.com/sagikazarmark/locafero v0.4.0 // indirect
github.com/sagikazarmark/slog-shim v0.1.0 // indirect
Expand All @@ -208,14 +210,14 @@ require (
go.opentelemetry.io/otel v1.24.0 // indirect
go.opentelemetry.io/otel/metric v1.24.0 // indirect
go.opentelemetry.io/otel/trace v1.24.0 // indirect
go.starlark.net v0.0.0-20230525235612-a134d8f9ddca // indirect
go.starlark.net v0.0.0-20240925182052-1207426daebd // indirect
go.uber.org/multierr v1.11.0 // indirect
go.uber.org/zap v1.26.0 // indirect
golang.org/x/crypto v0.28.0 // indirect
golang.org/x/exp v0.0.0-20230905200255-921286631fa9 // indirect
golang.org/x/net v0.30.0 // indirect
golang.org/x/sys v0.26.0 // indirect
golang.org/x/text v0.19.0 // indirect
golang.org/x/crypto v0.31.0 // indirect
golang.org/x/exp v0.0.0-20241108190413-2d47ceb2692f // indirect
golang.org/x/net v0.33.0 // indirect
golang.org/x/sys v0.28.0 // indirect
golang.org/x/text v0.21.0 // indirect
golang.org/x/time v0.5.0 // indirect
gomodules.xyz/jsonpatch/v2 v2.4.0 // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20240311173647-c811ad7063a7 // indirect
Expand All @@ -234,6 +236,5 @@ require (
sigs.k8s.io/cluster-api v1.5.3 // indirect
sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd // indirect
sigs.k8s.io/kustomize/api v0.17.2 // indirect
sigs.k8s.io/kustomize/kyaml v0.17.1 // indirect
sigs.k8s.io/structured-merge-diff/v4 v4.4.1 // indirect
)
Loading

0 comments on commit 2b93fdf

Please sign in to comment.