forked from open-cluster-management-io/addon-framework
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelloworld_helm.go
108 lines (94 loc) · 2.87 KB
/
helloworld_helm.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
package helloworld_helm
import (
"context"
"embed"
"fmt"
"os"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"open-cluster-management.io/addon-framework/pkg/addonfactory"
addonapiv1alpha1 "open-cluster-management.io/api/addon/v1alpha1"
clusterv1 "open-cluster-management.io/api/cluster/v1"
)
const (
imageName = "helloWorldHelm"
defaultImage = "quay.io/open-cluster-management/addon-examples:latest"
defaultImagePullPolicy = "IfNotPresent"
)
//go:embed manifests
//go:embed manifests/charts/helloworld
//go:embed manifests/charts/helloworld/templates/_helpers.tpl
var FS embed.FS
const (
AddonName = "helloworldhelm"
)
type global struct {
ImagePullPolicy string `json:"imagePullPolicy,omitempty"`
ImagePullSecret string `json:"imagePullSecret,omitempty"`
ImageOverrides map[string]string `json:"imageOverrides,omitempty"`
}
type userValues struct {
ClusterNamespace string `json:"clusterNamespace,omitempty"`
Global global `json:"global"`
}
func GetDefaultValues(cluster *clusterv1.ManagedCluster,
addon *addonapiv1alpha1.ManagedClusterAddOn) (addonfactory.Values, error) {
image := os.Getenv("EXAMPLE_IMAGE_NAME")
if len(image) == 0 {
image = defaultImage
}
userJsonValues := userValues{
ClusterNamespace: cluster.GetName(),
Global: global{
ImagePullPolicy: defaultImagePullPolicy,
ImageOverrides: map[string]string{
imageName: image,
},
},
}
values, err := addonfactory.JsonStructToValues(userJsonValues)
if err != nil {
return nil, err
}
return values, nil
}
func GetImageValues(kubeClient kubernetes.Interface) addonfactory.GetValuesFunc {
return func(
cluster *clusterv1.ManagedCluster,
addon *addonapiv1alpha1.ManagedClusterAddOn,
) (addonfactory.Values, error) {
overrideValues := addonfactory.Values{}
for _, config := range addon.Status.ConfigReferences {
if config.ConfigGroupResource.Group != "" ||
config.ConfigGroupResource.Resource != "configmaps" {
continue
}
configMap, err := kubeClient.CoreV1().ConfigMaps(config.Namespace).Get(context.Background(), config.Name, metav1.GetOptions{})
if err != nil {
return nil, err
}
image, ok := configMap.Data["image"]
if !ok {
return nil, fmt.Errorf("no image in configmap %s/%s", config.Namespace, config.Name)
}
imagePullPolicy, ok := configMap.Data["imagePullPolicy"]
if !ok {
return nil, fmt.Errorf("no imagePullPolicy in configmap %s/%s", config.Namespace, config.Name)
}
userJsonValues := userValues{
Global: global{
ImagePullPolicy: imagePullPolicy,
ImageOverrides: map[string]string{
imageName: image,
},
},
}
values, err := addonfactory.JsonStructToValues(userJsonValues)
if err != nil {
return nil, err
}
overrideValues = addonfactory.MergeValues(overrideValues, values)
}
return overrideValues, nil
}
}