/
step_verify_requirements.go
161 lines (133 loc) · 4.88 KB
/
step_verify_requirements.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
package verify
import (
"fmt"
"os"
"path/filepath"
"github.com/jenkins-x/jx/v2/pkg/cmd/opts/step"
"github.com/jenkins-x/jx/v2/pkg/versionstream"
"github.com/jenkins-x/jx-logging/pkg/log"
"github.com/jenkins-x/jx/v2/pkg/cmd/helper"
"github.com/jenkins-x/jx/v2/pkg/cmd/opts"
"github.com/jenkins-x/jx/v2/pkg/cmd/templates"
"github.com/jenkins-x/jx/v2/pkg/config"
"github.com/jenkins-x/jx/v2/pkg/helm"
"github.com/pkg/errors"
"github.com/spf13/cobra"
)
var (
verifyRequirementsLong = templates.LongDesc(`
Verifies all the helm requirements.yaml files have a version number populated from the Version Stream.
` + helper.SeeAlsoText("jx create project"))
verifyRequirementsExample = templates.Examples(`
Verifies all the helm requirements.yaml files have a version number populated from the Version Stream
# verify packages and fail if any are not valid:
jx step verify packages
# override the error if the 'jx' binary is out of range (e.g. for development)
export JX_DISABLE_VERIFY_JX="true"
jx step verify packages
`)
)
// StepVerifyRequirementsOptions contains the command line flags
type StepVerifyRequirementsOptions struct {
step.StepOptions
Dir string
}
// NewCmdStepVerifyRequirements creates the `jx step verify pod` command
func NewCmdStepVerifyRequirements(commonOpts *opts.CommonOptions) *cobra.Command {
options := &StepVerifyRequirementsOptions{
StepOptions: step.StepOptions{
CommonOptions: commonOpts,
},
}
cmd := &cobra.Command{
Use: "requirements",
Aliases: []string{"requirement", "req"},
Short: "Verifies all the helm requirements.yaml files have a version number populated from the Version Stream",
Long: verifyRequirementsLong,
Example: verifyRequirementsExample,
Run: func(cmd *cobra.Command, args []string) {
options.Cmd = cmd
options.Args = args
err := options.Run()
helper.CheckErr(err)
},
}
cmd.Flags().StringVarP(&options.Dir, "dir", "d", ".", "the directory to recursively look for 'requirements.yaml' files")
return cmd
}
// Run implements this command
func (o *StepVerifyRequirementsOptions) Run() error {
if o.Dir == "" {
var err error
o.Dir, err = os.Getwd()
if err != nil {
return err
}
}
requirements, _, err := config.LoadRequirementsConfig(o.Dir, config.DefaultFailOnValidationError)
if err != nil {
return errors.Wrapf(err, "failed to load boot requirements")
}
vs := requirements.VersionStream
log.Logger().Debugf("Verifying the helm requirements versions in dir: %s using version stream URL: %s and git ref: %s\n", o.Dir, vs.URL, vs.Ref)
resolver, err := o.CreateVersionResolver(vs.URL, vs.Ref)
if err != nil {
return errors.Wrapf(err, "failed to create version resolver")
}
repoPrefixes, err := resolver.GetRepositoryPrefixes()
if err != nil {
return errors.Wrapf(err, "failed to load repository prefixes")
}
err = filepath.Walk(o.Dir, func(path string, info os.FileInfo, err error) error {
name := info.Name()
if info.IsDir() || name != helm.RequirementsFileName {
return nil
}
log.Logger().Infof("found %s", path)
return o.verifyRequirementsYAML(resolver, repoPrefixes, path)
})
return err
}
func (o *StepVerifyRequirementsOptions) verifyRequirementsYAML(resolver *versionstream.VersionResolver, prefixes *versionstream.RepositoryPrefixes, fileName string) error {
req, err := helm.LoadRequirementsFile(fileName)
if err != nil {
return errors.Wrapf(err, "failed to load %s", fileName)
}
modified := false
for _, dep := range req.Dependencies {
if dep.Version == "" {
name := dep.Alias
if name == "" {
name = dep.Name
}
repo := dep.Repository
if repo == "" {
return fmt.Errorf("cannot to find a version for dependency %s in file %s as there is no 'repository'", name, fileName)
}
prefix := prefixes.PrefixForURL(repo)
if prefix == "" {
return fmt.Errorf("the helm repository %s does not have an associated prefix in in the 'charts/repositories.yml' file the version stream, so we cannot default the version in file %s", repo, fileName)
}
newVersion := ""
fullChartName := prefix + "/" + dep.Name
newVersion, err := resolver.StableVersionNumber(versionstream.KindChart, fullChartName)
if err != nil {
return errors.Wrapf(err, "failed to find version of chart %s in file %s", fullChartName, fileName)
}
if newVersion == "" {
return fmt.Errorf("failed to find a version for dependency %s in file %s in the current version stream - please either add an explicit version to this file or add chart %s to the version stream", name, fileName, fullChartName)
}
dep.Version = newVersion
modified = true
log.Logger().Debugf("adding version %s to dependency %s in file %s", newVersion, name, fileName)
}
}
if modified {
err = helm.SaveFile(fileName, req)
if err != nil {
return errors.Wrapf(err, "failed to save %s", fileName)
}
log.Logger().Infof("adding dependency versions to file %s", fileName)
}
return nil
}