Skip to content
This repository was archived by the owner on Jun 29, 2022. It is now read-only.

Commit b7da7fd

Browse files
authored
Merge pull request #605 from kinvolk/invidian/improvements
pkg/components/util: improvements
2 parents 3070ead + 5e69aac commit b7da7fd

File tree

5 files changed

+73
-89
lines changed

5 files changed

+73
-89
lines changed

cli/cmd/component-apply.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ func applyComponents(lokoConfig *config.Config, kubeconfig string, componentName
8484
return diags
8585
}
8686

87-
if err := util.InstallComponent(componentName, component, kubeconfig); err != nil {
87+
if err := util.InstallComponent(component, kubeconfig); err != nil {
8888
return err
8989
}
9090

pkg/components/util/helm.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -153,13 +153,13 @@ func filterOutUnusedFiles(files map[string]string) map[string]string {
153153

154154
// chartFromComponent creates Helm chart object in memory for given component and makes
155155
// sure it is valid.
156-
func chartFromComponent(name string, c components.Component) (*chart.Chart, error) {
156+
func chartFromComponent(c components.Component) (*chart.Chart, error) {
157157
m, err := c.RenderManifests()
158158
if err != nil {
159159
return nil, fmt.Errorf("rendering manifests failed: %w", err)
160160
}
161161

162-
ch, err := chartFromManifests(name, m)
162+
ch, err := chartFromManifests(c.Metadata().Name, m)
163163
if err != nil {
164164
return nil, fmt.Errorf("creating chart from manifests failed: %w", err)
165165
}

pkg/components/util/install.go

Lines changed: 68 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
"fmt"
2020

2121
"helm.sh/helm/v3/pkg/action"
22+
"helm.sh/helm/v3/pkg/chart"
2223
"helm.sh/helm/v3/pkg/kube"
2324
"helm.sh/helm/v3/pkg/storage/driver"
2425
v1 "k8s.io/api/core/v1"
@@ -29,40 +30,44 @@ import (
2930
"github.com/kinvolk/lokomotive/pkg/k8sutil"
3031
)
3132

32-
// InstallComponent installs given component using given kubeconfig.
33-
func InstallComponent(name string, c components.Component, kubeconfig string) error {
34-
return InstallAsRelease(name, c, kubeconfig)
35-
}
36-
37-
// InstallAsRelease installs a component as a Helm release using a Helm client.
38-
func InstallAsRelease(name string, c components.Component, kubeconfig string) error {
39-
cs, err := k8sutil.NewClientset(kubeconfig)
33+
func ensureNamespaceExists(name string, kubeconfigPath string) error {
34+
cs, err := k8sutil.NewClientset(kubeconfigPath)
4035
if err != nil {
41-
return err
36+
return fmt.Errorf("creating clientset: %w", err)
4237
}
4338

44-
// Get the namespace in which the component should be created.
45-
ns := c.Metadata().Namespace
46-
if ns == "" {
47-
return fmt.Errorf("component %s namespace is empty", name)
39+
if name == "" {
40+
return fmt.Errorf("namespace name can't be empty")
4841
}
4942

5043
// Ensure the namespace in which we create release and resources exists.
5144
_, err = cs.CoreV1().Namespaces().Create(context.TODO(), &v1.Namespace{
5245
ObjectMeta: metav1.ObjectMeta{
53-
Name: ns,
46+
Name: name,
5447
},
5548
}, metav1.CreateOptions{})
5649
if err != nil && !errors.IsAlreadyExists(err) {
5750
return err
5851
}
5952

53+
return nil
54+
}
55+
56+
// InstallComponent installs given component using given kubeconfig as a Helm release using a Helm client.
57+
func InstallComponent(c components.Component, kubeconfig string) error {
58+
name := c.Metadata().Name
59+
ns := c.Metadata().Namespace
60+
61+
if err := ensureNamespaceExists(ns, kubeconfig); err != nil {
62+
return fmt.Errorf("failed ensuring that namespace %q for component %q exists: %w", ns, name, err)
63+
}
64+
6065
actionConfig, err := HelmActionConfig(ns, kubeconfig)
6166
if err != nil {
6267
return fmt.Errorf("failed preparing helm client: %w", err)
6368
}
6469

65-
chart, err := chartFromComponent(name, c)
70+
chart, err := chartFromComponent(c)
6671
if err != nil {
6772
return err
6873
}
@@ -78,37 +83,59 @@ func InstallAsRelease(name string, c components.Component, kubeconfig string) er
7883

7984
wait := c.Metadata().Helm.Wait
8085

86+
helmAction := &helmAction{
87+
releaseName: name,
88+
chart: chart,
89+
actionConfig: actionConfig,
90+
wait: wait,
91+
}
92+
8193
if !exists {
82-
install := action.NewInstall(actionConfig)
83-
install.ReleaseName = name
84-
install.Namespace = ns
85-
86-
// Currently, we install components one-by-one, in the order how they are
87-
// defined in the configuration and we do not support any dependencies between
88-
// the components.
89-
//
90-
// If it is critical for component to have it's dependencies ready before it is
91-
// installed, all dependencies should set Wait field to 'true' in components.HelmMetadata
92-
// struct.
93-
//
94-
// The example of such dependency is between prometheus-operator and openebs-storage-class, where
95-
// both openebs-operator and openebs-storage-class components must be fully functional, before
96-
// prometheus-operator is deployed, otherwise it won't pick the default storage class.
97-
install.Wait = wait
98-
99-
if _, err := install.Run(chart, map[string]interface{}{}); err != nil {
100-
return fmt.Errorf("installing component '%s' as chart failed: %w", name, err)
101-
}
102-
103-
return nil
94+
return install(helmAction, ns)
10495
}
10596

106-
upgrade := action.NewUpgrade(actionConfig)
107-
upgrade.Wait = wait
97+
return upgrade(helmAction)
98+
}
99+
100+
type helmAction struct {
101+
releaseName string
102+
chart *chart.Chart
103+
actionConfig *action.Configuration
104+
wait bool
105+
}
106+
107+
func install(helmAction *helmAction, namespace string) error {
108+
install := action.NewInstall(helmAction.actionConfig)
109+
install.ReleaseName = helmAction.releaseName
110+
install.Namespace = namespace
111+
112+
// Currently, we install components one-by-one, in the order how they are
113+
// defined in the configuration and we do not support any dependencies between
114+
// the components.
115+
//
116+
// If it is critical for component to have it's dependencies ready before it is
117+
// installed, all dependencies should set Wait field to 'true' in components.HelmMetadata
118+
// struct.
119+
//
120+
// The example of such dependency is between prometheus-operator and openebs-storage-class, where
121+
// both openebs-operator and openebs-storage-class components must be fully functional, before
122+
// prometheus-operator is deployed, otherwise it won't pick the default storage class.
123+
install.Wait = helmAction.wait
124+
125+
if _, err := install.Run(helmAction.chart, map[string]interface{}{}); err != nil {
126+
return fmt.Errorf("installing release failed: %w", err)
127+
}
128+
129+
return nil
130+
}
131+
132+
func upgrade(helmAction *helmAction) error {
133+
upgrade := action.NewUpgrade(helmAction.actionConfig)
134+
upgrade.Wait = helmAction.wait
108135
upgrade.RecreateResources = true
109136

110-
if _, err := upgrade.Run(name, chart, map[string]interface{}{}); err != nil {
111-
return fmt.Errorf("updating chart failed: %w", err)
137+
if _, err := upgrade.Run(helmAction.releaseName, helmAction.chart, map[string]interface{}{}); err != nil {
138+
return fmt.Errorf("upgrading release failed: %w", err)
112139
}
113140

114141
return nil

pkg/k8sutil/template.go

Lines changed: 0 additions & 43 deletions
This file was deleted.

test/components/install_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,11 @@ component "flatcar-linux-update-operator" {}
5151
}
5252

5353
k := testutil.KubeconfigPath(t)
54-
if err := util.InstallAsRelease(n, c, k); err != nil {
54+
if err := util.InstallComponent(c, k); err != nil {
5555
t.Fatalf("Installing component as release should succeed, got: %v", err)
5656
}
5757

58-
if err := util.InstallAsRelease(n, c, k); err != nil {
58+
if err := util.InstallComponent(c, k); err != nil {
5959
t.Fatalf("Installing component twice as release should succeed, got: %v", err)
6060
}
6161
}

0 commit comments

Comments
 (0)