This repository was archived by the owner on Jun 29, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 47
Expand file tree
/
Copy pathinstall.go
More file actions
176 lines (142 loc) · 5.18 KB
/
install.go
File metadata and controls
176 lines (142 loc) · 5.18 KB
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
// Copyright 2020 The Lokomotive Authors
//
// 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 util
import (
"context"
"fmt"
"io/ioutil"
"helm.sh/helm/v3/pkg/action"
"helm.sh/helm/v3/pkg/chart"
"helm.sh/helm/v3/pkg/kube"
"helm.sh/helm/v3/pkg/storage/driver"
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"github.com/kinvolk/lokomotive/pkg/components"
"github.com/kinvolk/lokomotive/pkg/k8sutil"
)
func ensureNamespaceExists(name string, kubeconfigPath string) error {
kubeconfig, err := ioutil.ReadFile(kubeconfigPath) // #nosec G304
if err != nil {
return fmt.Errorf("reading kubeconfig file: %w", err)
}
cs, err := k8sutil.NewClientset(kubeconfig)
if err != nil {
return fmt.Errorf("creating clientset: %w", err)
}
if name == "" {
return fmt.Errorf("namespace name can't be empty")
}
// Ensure the namespace in which we create release and resources exists.
_, err = cs.CoreV1().Namespaces().Create(context.TODO(), &v1.Namespace{
ObjectMeta: metav1.ObjectMeta{
Name: name,
},
}, metav1.CreateOptions{})
if err != nil && !errors.IsAlreadyExists(err) {
return err
}
return nil
}
// InstallComponent installs given component using given kubeconfig as a Helm release using a Helm client.
func InstallComponent(c components.Component, kubeconfig string) error {
name := c.Metadata().Name
ns := c.Metadata().Namespace
if err := ensureNamespaceExists(ns, kubeconfig); err != nil {
return fmt.Errorf("failed ensuring that namespace %q for component %q exists: %w", ns, name, err)
}
actionConfig, err := HelmActionConfig(ns, kubeconfig)
if err != nil {
return fmt.Errorf("failed preparing helm client: %w", err)
}
chart, err := chartFromComponent(c)
if err != nil {
return err
}
if err := chart.Validate(); err != nil {
return fmt.Errorf("chart is invalid: %w", err)
}
exists, err := ReleaseExists(*actionConfig, name)
if err != nil {
return fmt.Errorf("failed checking if component is installed: %w", err)
}
wait := c.Metadata().Helm.Wait
helmAction := &helmAction{
releaseName: name,
chart: chart,
actionConfig: actionConfig,
wait: wait,
}
if !exists {
return install(helmAction, ns)
}
return upgrade(helmAction)
}
type helmAction struct {
releaseName string
chart *chart.Chart
actionConfig *action.Configuration
wait bool
}
func install(helmAction *helmAction, namespace string) error {
install := action.NewInstall(helmAction.actionConfig)
install.ReleaseName = helmAction.releaseName
install.Namespace = namespace
// Currently, we install components one-by-one, in the order how they are
// defined in the configuration and we do not support any dependencies between
// the components.
//
// If it is critical for component to have it's dependencies ready before it is
// installed, all dependencies should set Wait field to 'true' in components.HelmMetadata
// struct.
//
// The example of such dependency is between prometheus-operator and openebs-storage-class, where
// both openebs-operator and openebs-storage-class components must be fully functional, before
// prometheus-operator is deployed, otherwise it won't pick the default storage class.
install.Wait = helmAction.wait
if _, err := install.Run(helmAction.chart, map[string]interface{}{}); err != nil {
return fmt.Errorf("installing release failed: %w", err)
}
return nil
}
func upgrade(helmAction *helmAction) error {
upgrade := action.NewUpgrade(helmAction.actionConfig)
upgrade.Wait = helmAction.wait
upgrade.RecreateResources = true
if _, err := upgrade.Run(helmAction.releaseName, helmAction.chart, map[string]interface{}{}); err != nil {
return fmt.Errorf("upgrading release failed: %w", err)
}
return nil
}
// HelmActionConfig creates initialized Helm action configuration.
func HelmActionConfig(ns string, kubeconfig string) (*action.Configuration, error) {
actionConfig := &action.Configuration{}
// TODO: Add some logging implementation? We currently just pass an empty function for logging.
kubeConfig := kube.GetConfig(kubeconfig, "", ns)
logF := func(format string, v ...interface{}) {}
if err := actionConfig.Init(kubeConfig, ns, "secret", logF); err != nil {
return nil, fmt.Errorf("failed initializing helm: %w", err)
}
return actionConfig, nil
}
// ReleaseExists checks if given Helm release exists.
func ReleaseExists(actionConfig action.Configuration, name string) (bool, error) {
histClient := action.NewHistory(&actionConfig)
histClient.Max = 1
_, err := histClient.Run(name)
if err != nil && err != driver.ErrReleaseNotFound {
return false, fmt.Errorf("failed checking for chart history: %w", err)
}
return err != driver.ErrReleaseNotFound, nil
}