Skip to content

Commit af6122b

Browse files
feat: create a cluster configuration config map (#106)
* feat: create a cluster configuration config map this config map will hold the cluster configuration. for now it is empty. * feat: add version to the cluster config configmap
1 parent 5125340 commit af6122b

File tree

2 files changed

+47
-3
lines changed

2 files changed

+47
-3
lines changed

pkg/addons/adminconsole/adminconsole.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ func (a *AdminConsole) Apply(ctx context.Context) error {
133133
if _, err := act.RunWithContext(ctx, hchart, helmValues); err != nil {
134134
return fmt.Errorf("unable to install chart: %w", err)
135135
}
136-
return a.customization.apply(ctx)
136+
return a.customization.apply(ctx, version)
137137
}
138138

139139
a.logger("Admin Console already installed on the cluster, checking version.")
@@ -151,7 +151,7 @@ func (a *AdminConsole) Apply(ctx context.Context) error {
151151
if _, err := act.RunWithContext(ctx, releaseName, hchart, helmValues); err != nil {
152152
return fmt.Errorf("unable to upgrade chart: %w", err)
153153
}
154-
return a.customization.apply(ctx)
154+
return a.customization.apply(ctx, version)
155155
}
156156

157157
func (a *AdminConsole) Latest() (string, error) {

pkg/addons/adminconsole/customize.go

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@ import (
2626
"github.com/replicatedhq/helmvm/pkg/preflights"
2727
)
2828

29+
const (
30+
clusterConfigMapName = "embedded-cluster-config"
31+
clusterConfigMapNS = "kube-system"
32+
)
33+
2934
// ParsedSection holds the parsed section from the binary. We only care about the
3035
// application object, whatever HostPreflight we can find, and the app License.
3136
type ParsedSection struct {
@@ -122,12 +127,20 @@ func (a *AdminConsoleCustomization) kubeClient() (client.Client, error) {
122127

123128
// apply will attempt to read the helmvm binary and extract the kotsadm portal customization
124129
// from it. If it finds one, it will apply it to the cluster.
125-
func (a *AdminConsoleCustomization) apply(ctx context.Context) error {
130+
func (a *AdminConsoleCustomization) apply(ctx context.Context, version string) error {
126131
logrus.Infof("Applying admin console customization")
127132
if runtime.GOOS != "linux" {
128133
logrus.Infof("Skipping admin console customization on %s", runtime.GOOS)
129134
return nil
130135
}
136+
if err := a.kotsConfig(ctx); err != nil {
137+
return err
138+
}
139+
return a.clusterConfig(ctx, version)
140+
}
141+
142+
// kotsConfig reads the embedded kots config and creates it in the cluster.
143+
func (a *AdminConsoleCustomization) kotsConfig(ctx context.Context) error {
131144
cust, err := a.extractCustomization()
132145
if err != nil {
133146
return fmt.Errorf("unable to extract customization from binary: %w", err)
@@ -170,6 +183,37 @@ func (a *AdminConsoleCustomization) apply(ctx context.Context) error {
170183
return nil
171184
}
172185

186+
// clusterConfig makes sure a config map named "embedded-cluster-config" exists in the cluster.
187+
// XXX this will eventually be customizable by the user through the app release.
188+
func (a *AdminConsoleCustomization) clusterConfig(ctx context.Context, version string) error {
189+
kubeclient, err := a.kubeClient()
190+
if err != nil {
191+
return fmt.Errorf("unable to create kubernetes client: %w", err)
192+
}
193+
nsn := client.ObjectKey{Namespace: clusterConfigMapNS, Name: clusterConfigMapName}
194+
var cm corev1.ConfigMap
195+
if err := kubeclient.Get(ctx, nsn, &cm); err != nil {
196+
if !errors.IsNotFound(err) {
197+
return fmt.Errorf("unable to get cluster config configmap: %w", err)
198+
}
199+
logrus.Infof("Creating cluster config configmap")
200+
cm = corev1.ConfigMap{
201+
ObjectMeta: metav1.ObjectMeta{Namespace: nsn.Namespace, Name: nsn.Name},
202+
Data: map[string]string{"version": version},
203+
}
204+
if err := kubeclient.Create(ctx, &cm); err != nil {
205+
return fmt.Errorf("unable to create cluster config configmap: %w", err)
206+
}
207+
return nil
208+
}
209+
logrus.Infof("Updating cluster config configmap")
210+
cm.Data["version"] = version
211+
if err := kubeclient.Update(ctx, &cm); err != nil {
212+
return fmt.Errorf("unable to update cluster config configmap: %w", err)
213+
}
214+
return nil
215+
}
216+
173217
// hostPreflights returns a list of HostPreflight specs that are found in the binary.
174218
// These are part of the embedded Kots Application Release.
175219
func (a *AdminConsoleCustomization) hostPreflights() (*v1beta2.HostPreflightSpec, error) {

0 commit comments

Comments
 (0)