Skip to content

Commit b040d1e

Browse files
committed
SREP-1107: Added a new 'managed-scripts' subcommand to osdctl promote
This also contains the following changes: - 'pko' promote subcommand removed as it was no longer applying to any saas file - '--osd' and '--hcp' options removed as a there is no service which could be both hcp and osd - Now exclusively using 'sigs.k8s.io/kustomize/kyaml' as both a YAML parser & marshaller as 'gopkg.in/yaml.v3' is no longer supported - Now interacting with Git repo with 'github.com/go-git/go-git/v5' lib rather than running shell commands - Code has been factorized
1 parent b6874d0 commit b040d1e

32 files changed

+2738
-3178
lines changed

cmd/promote/cmd.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import (
44
"fmt"
55

66
"github.com/openshift/osdctl/cmd/promote/dynatrace"
7-
"github.com/openshift/osdctl/cmd/promote/pko"
7+
"github.com/openshift/osdctl/cmd/promote/managedscripts"
88
"github.com/openshift/osdctl/cmd/promote/saas"
99
"github.com/spf13/cobra"
1010
)
@@ -19,8 +19,8 @@ func NewCmdPromote() *cobra.Command {
1919
}
2020

2121
promoteCmd.AddCommand(saas.NewCmdSaas())
22-
promoteCmd.AddCommand(pko.NewCmdPKO())
2322
promoteCmd.AddCommand(dynatrace.NewCmdDynatrace())
23+
promoteCmd.AddCommand(managedscripts.NewCmdManagedScripts())
2424

2525
return promoteCmd
2626
}

cmd/promote/dynatrace/dt_utils.go

Lines changed: 41 additions & 172 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,10 @@ import (
99
"sort"
1010
"strings"
1111

12-
"github.com/openshift/osdctl/cmd/promote/git"
1312
"github.com/openshift/osdctl/cmd/promote/iexec"
14-
"gopkg.in/yaml.v3"
13+
"github.com/openshift/osdctl/cmd/promote/utils"
14+
15+
kyaml "sigs.k8s.io/kustomize/kyaml/yaml"
1516
)
1617

1718
const (
@@ -22,203 +23,73 @@ const (
2223
)
2324

2425
var (
25-
ServicesSlice []string
26-
ServicesFilesMap = map[string]string{}
27-
ModulesSlice []string
28-
ModulesFilesMap = map[string]string{}
26+
ModulesSlice []string
27+
ModulesFilesMap = map[string]string{}
2928
)
3029

31-
func listServiceNames(appInterface git.AppInterface) error {
32-
_, err := GetServiceNames(appInterface, saasDynatraceDir)
33-
if err != nil {
34-
return err
35-
}
36-
37-
sort.Strings(ServicesSlice)
38-
fmt.Println("### Available Dynatrace components ###")
39-
fmt.Println()
40-
41-
// Find the longest service name for alignment
42-
maxLen := 0
43-
for _, service := range ServicesSlice {
44-
if len(service) > maxLen {
45-
maxLen = len(service)
46-
}
47-
}
48-
49-
for _, service := range ServicesSlice {
50-
// Read the service YAML file to extract the path
51-
saasDir, err := GetSaasDir(service)
52-
if err != nil {
53-
fmt.Printf("%-*s (unable to read config)\n", maxLen, service)
54-
continue
55-
}
56-
57-
serviceData, err := os.ReadFile(saasDir)
58-
if err != nil {
59-
fmt.Printf("%-*s (unable to read config)\n", maxLen, service)
60-
continue
61-
}
62-
63-
// Extract the path by parsing the YAML directly
64-
serviceFullPath := extractPathFromServiceYAML(serviceData)
65-
66-
// Display service name with its path
67-
if serviceFullPath != "" {
68-
fmt.Printf("%-*s → %s\n", maxLen, service, serviceFullPath)
69-
} else {
70-
fmt.Printf("%-*s (no specific path)\n", maxLen, service)
71-
}
30+
func validateDynatraceServiceFilePath(filePath string) string {
31+
if !strings.HasSuffix(filePath, ".yaml") {
32+
return ""
7233
}
7334

74-
return nil
35+
return filePath
7536
}
7637

77-
// extractPathFromServiceYAML extracts all unique path fields from resourceTemplates
78-
func extractPathFromServiceYAML(yamlData []byte) string {
79-
var service struct {
80-
ResourceTemplates []struct {
81-
PATH string `yaml:"path"`
82-
} `yaml:"resourceTemplates"`
83-
}
84-
85-
err := yaml.Unmarshal(yamlData, &service)
38+
func getResourceTemplatesPaths(serviceRegistry *utils.ServicesRegistry, serviceId string) string {
39+
service, err := serviceRegistry.GetService(serviceId)
8640
if err != nil {
8741
return ""
8842
}
8943

90-
pathSet := make(map[string]bool)
91-
for _, rt := range service.ResourceTemplates {
92-
if rt.PATH != "" {
93-
pathSet[rt.PATH] = true
94-
}
95-
}
96-
9744
var paths []string
98-
for path := range pathSet {
99-
paths = append(paths, path)
100-
}
101-
sort.Strings(paths)
102-
103-
return strings.Join(paths, ", ")
104-
}
105-
106-
func servicePromotion(appInterface git.AppInterface, component, gitHash string) error {
107-
108-
_, err := GetServiceNames(appInterface, saasDynatraceDir)
109-
if err != nil {
110-
return err
111-
}
112-
113-
component, err = ValidateServiceName(ServicesSlice, component)
114-
if err != nil {
115-
return err
116-
}
117-
118-
saasDir, err := GetSaasDir(component)
119-
if err != nil {
120-
return err
121-
}
122-
fmt.Printf("SAAS Directory: %v\n", saasDir)
123-
124-
serviceData, err := os.ReadFile(saasDir)
125-
if err != nil {
126-
return fmt.Errorf("failed to read SAAS file: %v", err)
127-
}
128-
129-
currentGitHash, serviceRepo, serviceFullPath, err := git.GetCurrentGitHashAndPathFromAppInterface(serviceData, component, "")
130-
if err != nil {
131-
return fmt.Errorf("failed to get current git hash or service repo: %v", err)
132-
}
133-
134-
fmt.Printf("Current Git Hash: %v\nGit Repo: %v\nComponent path: %v\n", currentGitHash, serviceRepo, serviceFullPath)
13545

136-
promotionGitHash, commitLog, err := git.CheckoutAndCompareGitHash(appInterface.GitExecutor, serviceRepo, gitHash, currentGitHash, strings.TrimPrefix(serviceFullPath, "/"))
137-
if err != nil {
138-
return fmt.Errorf("failed to checkout and compare git hash: %v", err)
139-
} else if promotionGitHash == "" {
140-
fmt.Printf("Unable to find a git hash to promote. Exiting.\n")
141-
os.Exit(6)
142-
}
143-
144-
fmt.Printf("Service: %s will be promoted to %s\n", component, promotionGitHash)
145-
fmt.Printf("commitLog: %v\n", commitLog)
146-
147-
branchName := fmt.Sprintf("promote-%s-%s", component, promotionGitHash)
148-
149-
err = appInterface.UpdateAppInterface(component, saasDir, currentGitHash, promotionGitHash, branchName, false)
150-
if err != nil {
151-
fmt.Printf("FAILURE: %v\n", err)
152-
}
153-
154-
commitMessage := fmt.Sprintf("Promote %s to %s\n\nSee %s/compare/%s...%s for contents of the promotion.\n clog:%s", component, promotionGitHash, serviceRepo, currentGitHash, promotionGitHash, commitLog)
155-
156-
fmt.Printf("commitMessage: %s\n", commitMessage)
46+
err = service.GetResourceTemplatesSequenceNode().VisitElements(func(resourceTemplateNode *kyaml.RNode) error {
47+
path, err := resourceTemplateNode.GetString("path")
48+
if err != nil || path == "" {
49+
return nil
50+
}
15751

158-
err = appInterface.CommitSaasFile(saasDir, commitMessage)
52+
paths = append(paths, path)
53+
return nil
54+
})
15955
if err != nil {
160-
return fmt.Errorf("failed to commit changes to app-interface; manual commit may still succeed: %w", err)
56+
return ""
16157
}
162-
163-
fmt.Printf("The branch %s is ready to be pushed\n", branchName)
164-
fmt.Println("")
165-
fmt.Println("DT service:", component)
166-
fmt.Println("from:", currentGitHash)
167-
fmt.Println("to:", promotionGitHash)
168-
fmt.Println("READY TO PUSH,", component, "promotion commit is ready locally")
169-
return nil
58+
return strings.Join(paths, ", ")
17059
}
17160

172-
func GetServiceNames(appInterface git.AppInterface, saaDirs ...string) ([]string, error) {
173-
baseDir := appInterface.GitDirectory
61+
func listServiceIds(serviceRegistry *utils.ServicesRegistry) error {
62+
serviceIds := serviceRegistry.GetServicesIds()
17463

175-
for _, dir := range saaDirs {
176-
dirGlob := filepath.Join(baseDir, dir)
177-
filepaths, err := filepath.Glob(dirGlob + "/*.yaml")
178-
if err != nil {
179-
return nil, err
180-
}
64+
fmt.Println("### Available Dynatrace components ###")
18165

182-
for _, filepath := range filepaths {
183-
filename := strings.TrimPrefix(filepath, baseDir+"/"+dir+"/")
184-
filename = strings.TrimSuffix(filename, ".yaml")
185-
ServicesSlice = append(ServicesSlice, filename)
186-
ServicesFilesMap[filename] = filepath
66+
// Find the longest service name for alignment
67+
maxLen := 0
68+
for _, serviceId := range serviceIds {
69+
if len(serviceId) > maxLen {
70+
maxLen = len(serviceId)
18771
}
18872
}
18973

190-
return ServicesSlice, nil
191-
}
74+
for _, serviceId := range serviceIds {
75+
// Extract the path by parsing the YAML directly
76+
resourcesTemplatesPaths := getResourceTemplatesPaths(serviceRegistry, serviceId)
19277

193-
func ValidateServiceName(serviceSlice []string, serviceName string) (string, error) {
194-
fmt.Printf("### Checking if service %s exists ###\n", serviceName)
195-
for _, service := range serviceSlice {
196-
if service == serviceName {
197-
fmt.Printf("Service %s found\n", serviceName)
198-
return serviceName, nil
199-
}
200-
if service == "dynatrace-"+serviceName {
201-
fmt.Printf("Service %s found\n", serviceName)
202-
return "dynatrace-" + serviceName, nil
78+
// Display service name with its path
79+
if resourcesTemplatesPaths != "" {
80+
fmt.Printf("%-*s → %s\n", maxLen, serviceId, resourcesTemplatesPaths)
81+
} else {
82+
fmt.Printf("%-*s (no specific path)\n", maxLen, serviceId)
20383
}
20484
}
20585

206-
return serviceName, fmt.Errorf("service %s not found", serviceName)
207-
}
208-
209-
func GetSaasDir(component string) (string, error) {
210-
if saasDir, ok := ServicesFilesMap[component]; ok {
211-
if strings.Contains(saasDir, ".yaml") {
212-
return saasDir, nil
213-
}
214-
}
215-
return "", fmt.Errorf("saas directory for service %s not found", component)
86+
return nil
21687
}
21788

21889
func listDynatraceModuleNames(dynatraceConfig DynatraceConfig) error {
21990

22091
baseDir := dynatraceConfig.GitDirectory
221-
_, err := GeModulesNames(baseDir, moduleDir)
92+
_, err := GetModulesNames(baseDir, moduleDir)
22293
if err != nil {
22394
return err
22495
}
@@ -232,8 +103,7 @@ func listDynatraceModuleNames(dynatraceConfig DynatraceConfig) error {
232103
return nil
233104
}
234105

235-
func GeModulesNames(baseDir, dir string) ([]string, error) {
236-
106+
func GetModulesNames(baseDir, dir string) ([]string, error) {
237107
dirGlob := filepath.Join(baseDir, dir)
238108
filepaths, err := os.ReadDir(dirGlob)
239109

@@ -337,10 +207,9 @@ func getLatestGitHash(basedir, module string) (string, error) {
337207
}
338208

339209
func modulePromotion(dynatraceConfig DynatraceConfig, module string) error {
340-
341210
baseDir := dynatraceConfig.GitDirectory
342211

343-
_, err := GeModulesNames(baseDir, moduleDir)
212+
_, err := GetModulesNames(baseDir, moduleDir)
344213
if err != nil {
345214
return err
346215
}

0 commit comments

Comments
 (0)