/
application_definitions.go
123 lines (105 loc) · 4.18 KB
/
application_definitions.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
/*
Copyright 2023 The Kubermatic Kubernetes Platform contributors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package images
import (
"context"
"fmt"
"os"
"path"
"time"
"github.com/sirupsen/logrus"
appskubermaticv1 "k8c.io/api/v3/pkg/apis/apps.kubermatic/v1"
kubermaticv1 "k8c.io/api/v3/pkg/apis/kubermatic/v1"
"k8c.io/kubermatic/v3/pkg/applications/providers"
"k8c.io/kubermatic/v3/pkg/cni/cilium"
"k8c.io/kubermatic/v3/pkg/install/helm"
"k8c.io/kubermatic/v3/pkg/log"
"k8c.io/kubermatic/v3/pkg/resources/reconciling"
)
func GetImagesFromSystemApplicationDefinitions(logger logrus.FieldLogger, config *kubermaticv1.KubermaticConfiguration, helmClient helm.Client, helmTimeout time.Duration, registryPrefix string) ([]string, error) {
var images []string
var appDefReconcilers []reconciling.NamedApplicationDefinitionReconcilerFactory
appDefReconcilers = append(appDefReconcilers,
cilium.ApplicationDefinitionReconciler(config),
)
for _, createFunc := range appDefReconcilers {
appName, creator := createFunc()
appDef, err := creator(&appskubermaticv1.ApplicationDefinition{})
if err != nil {
return nil, err
}
appLog := logger.WithFields(logrus.Fields{"application-name": appName})
appDefImages, err := getImagesFromApplicationDefinition(appLog, helmClient, appDef, helmTimeout, registryPrefix)
if err != nil {
return nil, err
}
images = append(images, appDefImages...)
}
return images, nil
}
func getImagesFromApplicationDefinition(logger logrus.FieldLogger, helmClient helm.Client, appDef *appskubermaticv1.ApplicationDefinition, helmTimeout time.Duration, registryPrefix string) ([]string, error) {
if appDef.Spec.Method != appskubermaticv1.HelmTemplateMethod {
// Only Helm ApplicationDefinitions are supported at the moment
logger.Debugf("Skipping the ApplicationDefinition as the method '%s' is not supported yet", appDef.Spec.Method)
return nil, nil
}
logger.Info("Retrieving images…")
tmpDir, err := os.MkdirTemp("", "helm-charts")
if err != nil {
return nil, fmt.Errorf("failed to create temp dir: %w", err)
}
defer func() {
if err := os.RemoveAll(tmpDir); err != nil {
logger.Fatalf("Failed to remove temp dir: %v", err)
}
}()
// if DefaultValues is provided, use it as values file
valuesFile := ""
if appDef.Spec.DefaultValues != nil {
valuesFile = path.Join(tmpDir, "values.yaml")
err = os.WriteFile(valuesFile, appDef.Spec.DefaultValues.Raw, 0644)
if err != nil {
return nil, fmt.Errorf("failed to create values file: %w", err)
}
}
var images []string
for _, appVer := range appDef.Spec.Versions {
appVerLog := logger.WithField("application-version", appVer.Version)
appVerLog.Debug("Downloading Helm chart…")
// pull the chart
chartPath, err := downloadAppSourceChart(&appVer.Template.Source, tmpDir, helmTimeout)
if err != nil {
return nil, fmt.Errorf("failed to pull app chart: %w", err)
}
// get images
chartImages, err := GetImagesForHelmChart(appVerLog, nil, helmClient, chartPath, valuesFile, registryPrefix)
if err != nil {
return nil, fmt.Errorf("failed to get images for chart: %w", err)
}
images = append(images, chartImages...)
}
return images, nil
}
func downloadAppSourceChart(appSource *appskubermaticv1.ApplicationSource, directory string, timeout time.Duration) (chartPath string, err error) {
ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()
sp, err := providers.NewSourceProvider(ctx, log.NewDefault().Sugar(), nil, "", directory, appSource, "")
if err != nil {
return "", fmt.Errorf("failed to create app source provider: %w", err)
}
chartPath, err = sp.DownloadSource(directory)
if err != nil {
return "", fmt.Errorf("failed to download app source: %w", err)
}
return chartPath, nil
}