99 "fmt"
1010 "os"
1111 "path"
12+ "path/filepath"
13+ "slices"
1214
1315 "gopkg.in/yaml.v2"
1416 corev1 "k8s.io/api/core/v1"
@@ -36,57 +38,46 @@ var log = ctrl.LoggerFrom(context.Background())
3638func main () {
3739 args := os .Args
3840 var (
39- outputFile string
40- inputConfigMapFile string
41+ outputFile string
42+ inputConfigMapFile string
43+ previousConfigMapFile string
4144 )
4245 flagSet := flag .NewFlagSet ("mindthegap-helm-registry" , flag .ExitOnError )
43- flagSet .StringVar (& outputFile , "output-file" , "" ,
44- "output file name to write config map to." )
45- flagSet .StringVar (& inputConfigMapFile , "input-configmap-file" , "" ,
46- "input configmap file to create the mindthegap repo file from" )
46+ flagSet .StringVar (
47+ & outputFile ,
48+ "output-file" ,
49+ "" ,
50+ "output file name to write config map to." ,
51+ )
52+ flagSet .StringVar (
53+ & inputConfigMapFile ,
54+ "input-configmap-file" ,
55+ "" ,
56+ "input configmap file to create the mindthegap repo file from" ,
57+ )
58+ flagSet .StringVar (
59+ & previousConfigMapFile ,
60+ "previous-configmap-file" ,
61+ "" ,
62+ "input configmap file to create the mindthegap repo file from" ,
63+ )
4764 err := flagSet .Parse (args [1 :])
4865 if err != nil {
4966 log .Error (err , "failed to parse args" )
5067 }
51- fullPath := inputConfigMapFile
52- if ! path .IsAbs (fullPath ) {
53- wd , err := os .Getwd ()
54- if err != nil {
55- log .Error (err , "failed to get wd" )
56- return
57- }
58- fullPath = path .Join (wd , inputConfigMapFile )
59- }
60- f , err := os .Open (fullPath )
61- if err != nil {
62- log .Error (err , "failed to open file" )
63- return
64- }
65- defer f .Close ()
66- cm := & corev1.ConfigMap {}
67- err = yamlDecode .NewYAMLOrJSONDecoder (f , 1024 ).Decode (cm )
68+ inputCm , err := getConfigMapFromFile (inputConfigMapFile )
6869 if err != nil {
69- log .Error (err , fmt .Sprintf ("failed to unmarshal file %s" , fullPath ))
70+ log .Error (err , fmt .Sprintf ("failed to get configmap from file %s %w " , inputConfigMapFile , err ))
7071 }
7172 out := HelmChartsConfig {
7273 map [string ]Repository {},
7374 }
74- for _ , info := range cm .Data {
75- var settings HelmChartFromConfigMap
76- err = yaml .Unmarshal ([]byte (info ), & settings )
77- if err != nil {
78- log .Error (err , "failed unmarshl settings" )
79- return
80- }
81- out .Repositories [settings .Name ] = Repository {
82- RepoURL : settings .Repository ,
83- Charts : map [string ][]string {
84- settings .Name : {
85- settings .Version ,
86- },
87- },
88- }
75+ ConfigMapToHelmChartConfig (& out , inputCm )
76+ previousCm , err := getConfigMapFromFile (previousConfigMapFile )
77+ if err != nil {
78+ log .Error (err , fmt .Sprintf ("failed to get configmap from file %s %w" , inputConfigMapFile , err ))
8979 }
80+ ConfigMapToHelmChartConfig (& out , previousCm )
9081 b , err := yaml .Marshal (out )
9182 if err != nil {
9283 log .Error (err , fmt .Sprintf ("failed to marshal obj %v" , out ))
@@ -99,7 +90,7 @@ func main() {
9990 }
10091 fullOutputfilePath = path .Join (wd , outputFile )
10192 }
102- f , err = os .OpenFile (fullOutputfilePath , os .O_RDWR | os .O_CREATE | os .O_TRUNC , 0o666 )
93+ f , err : = os .OpenFile (fullOutputfilePath , os .O_RDWR | os .O_CREATE | os .O_TRUNC , 0o666 )
10394 if err != nil {
10495 log .Error (err , "failed to create file" )
10596 }
@@ -109,3 +100,58 @@ func main() {
109100 log .Error (err , "failed to write to file" )
110101 }
111102}
103+
104+ func ConfigMapToHelmChartConfig (out * HelmChartsConfig , cm * corev1.ConfigMap ) {
105+ for _ , info := range cm .Data {
106+ var settings HelmChartFromConfigMap
107+ err := yaml .Unmarshal ([]byte (info ), & settings )
108+ if err != nil {
109+ log .Error (err , "failed unmarshl settings" )
110+ return
111+ }
112+ repo , ok := out .Repositories [settings .Name ]
113+ // if this is the first time we saw this add a new entry
114+ if ! ok {
115+ out .Repositories [settings .Name ] = Repository {
116+ RepoURL : settings .Repository ,
117+ Charts : map [string ][]string {
118+ settings .Name : {
119+ settings .Version ,
120+ },
121+ },
122+ }
123+ continue
124+ }
125+ // we've seen it already only add a new chart if the versions are different
126+ if ! slices .Contains (repo .Charts [settings .Name ], settings .Version ) {
127+ repo .Charts [settings .Name ] = append (repo .Charts [settings .Name ], settings .Version )
128+ }
129+ }
130+ }
131+
132+ func getConfigMapFromFile (configMapFile string ) (* corev1.ConfigMap , error ) {
133+ fullPath , err := EnsureFullPath (configMapFile )
134+ if err != nil {
135+ return nil , err
136+ }
137+ f , err := os .Open (fullPath )
138+ if err != nil {
139+ return nil , err
140+ }
141+ defer f .Close ()
142+ cm := & corev1.ConfigMap {}
143+ err = yamlDecode .NewYAMLOrJSONDecoder (f , 1024 ).Decode (cm )
144+ return cm , err
145+ }
146+
147+ func EnsureFullPath (filename string ) (string , error ) {
148+ fullPath , err := filepath .Abs (filename )
149+ if err != nil {
150+ return "" , err
151+ }
152+ _ , err = os .Stat (fullPath )
153+ if err != nil {
154+ return "" , err
155+ }
156+ return fullPath , nil
157+ }
0 commit comments