forked from knechtionscoding/keel
/
common.go
105 lines (86 loc) · 2.96 KB
/
common.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
package helm3
import (
"errors"
"fmt"
"github.com/datagravity-ai/keel/types"
"github.com/datagravity-ai/keel/util/image"
"helm.sh/helm/v3/pkg/chartutil"
log "github.com/sirupsen/logrus"
)
// ErrKeelConfigNotFound - default error when keel configuration for chart is not defined
var ErrKeelConfigNotFound = errors.New("keel configuration not found")
// getImages - get images from chart values
func getImages(vals chartutil.Values) ([]*types.TrackedImage, error) {
var images []*types.TrackedImage
keelCfg, err := getKeelConfig(vals)
if err != nil {
if err == ErrPolicyNotSpecified {
// nothing to do
return images, nil
}
log.WithFields(log.Fields{
"error": err,
}).Error("provider.helm3: failed to get keel configuration for release")
// ignoring this release, no keel config found
return nil, ErrKeelConfigNotFound
}
for _, imageDetails := range keelCfg.Images {
imageRef, err := parseImage(vals, &imageDetails)
if err != nil {
log.WithFields(log.Fields{
"error": err,
"repository_name": imageDetails.RepositoryPath,
"repository_tag": imageDetails.TagPath,
}).Error("provider.helm3: failed to parse image")
continue
}
trackedImage := &types.TrackedImage{
Image: imageRef,
PollSchedule: keelCfg.PollSchedule,
Trigger: keelCfg.Trigger,
Policy: keelCfg.Plc,
}
if imageDetails.ImagePullSecret != "" {
trackedImage.Secrets = append(trackedImage.Secrets, imageDetails.ImagePullSecret)
}
images = append(images, trackedImage)
}
return images, nil
}
func getPlanValues(newVersion *types.Version, ref *image.Reference, imageDetails *ImageDetails) (path, value string) {
// if tag is not supplied, then user specified full image name
if imageDetails.TagPath == "" {
return imageDetails.RepositoryPath, getUpdatedImage(ref, newVersion.String())
}
return imageDetails.TagPath, newVersion.String()
}
func getUnversionedPlanValues(newTag string, ref *image.Reference, imageDetails *ImageDetails) (path, value string) {
// if tag is not supplied, then user specified full image name
if imageDetails.TagPath == "" {
return imageDetails.RepositoryPath, getUpdatedImage(ref, newTag)
}
return imageDetails.TagPath, newTag
}
func getUpdatedImage(ref *image.Reference, version string) string {
// updating image
if ref.Registry() == image.DefaultRegistryHostname {
return fmt.Sprintf("%s:%s", ref.ShortName(), version)
}
return fmt.Sprintf("%s:%s", ref.Repository(), version)
}
func parseImage(vals chartutil.Values, details *ImageDetails) (*image.Reference, error) {
if details.RepositoryPath == "" {
return nil, fmt.Errorf("repository name path cannot be empty")
}
imageName, err := getValueAsString(vals, details.RepositoryPath)
if err != nil {
return nil, err
}
// getting image tag
imageTag, err := getValueAsString(vals, details.TagPath)
if err != nil {
// failed to find tag, returning anyway
return image.Parse(imageName)
}
return image.Parse(imageName + ":" + imageTag)
}