forked from operator-framework/operator-sdk
/
new.go
168 lines (144 loc) · 5.67 KB
/
new.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
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
// Copyright 2018 The Operator-SDK 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 release
import (
"errors"
"fmt"
"io/ioutil"
"os"
yaml "gopkg.in/yaml.v2"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/helm/pkg/chartutil"
"k8s.io/helm/pkg/kube"
"k8s.io/helm/pkg/storage"
)
const (
// HelmChartWatchesEnvVar is the environment variable for a YAML
// configuration file containing mappings of GVKs to helm charts. Use of
// this environment variable overrides the watch configuration provided
// by API_VERSION, KIND, and HELM_CHART, and it allows users to configure
// multiple watches, each with a different chart.
HelmChartWatchesEnvVar = "HELM_CHART_WATCHES"
// APIVersionEnvVar is the environment variable for the group and version
// to be watched using the format `<group>/<version>`
// (e.g. "example.com/v1alpha1").
APIVersionEnvVar = "API_VERSION"
// KindEnvVar is the environment variable for the kind to be watched. The
// value is typically singular and should be CamelCased (e.g. "MyApp").
KindEnvVar = "KIND"
// HelmChartEnvVar is the environment variable for the directory location
// of the helm chart to be installed for CRs that match the values for the
// API_VERSION and KIND environment variables.
HelmChartEnvVar = "HELM_CHART"
defaultHelmChartWatchesFile = "/opt/helm/watches.yaml"
)
type watch struct {
Group string `yaml:"group"`
Version string `yaml:"version"`
Kind string `yaml:"kind"`
Chart string `yaml:"chart"`
}
// NewManagerFactory returns a new Helm manager factory capable of installing and uninstalling releases.
func NewManagerFactory(storageBackend *storage.Storage, tillerKubeClient *kube.Client, chartDir string) ManagerFactory {
return &managerFactory{storageBackend, tillerKubeClient, chartDir}
}
// newManagerFactoryFromEnv returns a GVK and manager factory based on configuration provided
// in the environment.
func newManagerFactoryFromEnv(storageBackend *storage.Storage, tillerKubeClient *kube.Client) (schema.GroupVersionKind, ManagerFactory, error) {
apiVersion := os.Getenv(APIVersionEnvVar)
kind := os.Getenv(KindEnvVar)
chartDir := os.Getenv(HelmChartEnvVar)
var gvk schema.GroupVersionKind
gv, err := schema.ParseGroupVersion(apiVersion)
if err != nil {
return gvk, nil, err
}
gvk = gv.WithKind(kind)
if err := verifyGVK(gvk); err != nil {
return gvk, nil, fmt.Errorf("invalid GVK: %s: %s", gvk, err)
}
if _, err := chartutil.IsChartDir(chartDir); err != nil {
return gvk, nil, fmt.Errorf("invalid chart directory %s: %s", chartDir, err)
}
managerFactory := NewManagerFactory(storageBackend, tillerKubeClient, chartDir)
return gvk, managerFactory, nil
}
// NewManagerFactoriesFromEnv returns a map of managers, keyed by GVK, based on
// configuration provided in the environment.
func NewManagerFactoriesFromEnv(storageBackend *storage.Storage, tillerKubeClient *kube.Client) (map[schema.GroupVersionKind]ManagerFactory, error) {
if watchesFile, ok := getWatchesFile(); ok {
return NewManagerFactoriesFromFile(storageBackend, tillerKubeClient, watchesFile)
}
gvk, managerFactory, err := newManagerFactoryFromEnv(storageBackend, tillerKubeClient)
if err != nil {
return nil, err
}
return map[schema.GroupVersionKind]ManagerFactory{gvk: managerFactory}, nil
}
// NewManagerFactoriesFromFile reads the config file at the provided path and returns a map
// of managers, keyed by each GVK in the config.
func NewManagerFactoriesFromFile(storageBackend *storage.Storage, tillerKubeClient *kube.Client, path string) (map[schema.GroupVersionKind]ManagerFactory, error) {
b, err := ioutil.ReadFile(path)
if err != nil {
return nil, fmt.Errorf("failed to read config file: %s", err)
}
watches := []watch{}
err = yaml.Unmarshal(b, &watches)
if err != nil {
return nil, fmt.Errorf("failed to unmarshal config: %s", err)
}
m := map[schema.GroupVersionKind]ManagerFactory{}
for _, w := range watches {
gvk := schema.GroupVersionKind{
Group: w.Group,
Version: w.Version,
Kind: w.Kind,
}
if err := verifyGVK(gvk); err != nil {
return nil, fmt.Errorf("invalid GVK: %s: %s", gvk, err)
}
if _, err := chartutil.IsChartDir(w.Chart); err != nil {
return nil, fmt.Errorf("invalid chart directory %s: %s", w.Chart, err)
}
if _, ok := m[gvk]; ok {
return nil, fmt.Errorf("duplicate GVK: %s", gvk)
}
m[gvk] = NewManagerFactory(storageBackend, tillerKubeClient, w.Chart)
}
return m, nil
}
func verifyGVK(gvk schema.GroupVersionKind) error {
// A GVK without a group is valid. Certain scenarios may cause a GVK
// without a group to fail in other ways later in the initialization
// process.
if gvk.Version == "" {
return errors.New("version must not be empty")
}
if gvk.Kind == "" {
return errors.New("kind must not be empty")
}
return nil
}
func getWatchesFile() (string, bool) {
// If the watches env variable is set (even if it's an empty string), use it
// since the user explicitly set it.
if watchesFile, ok := os.LookupEnv(HelmChartWatchesEnvVar); ok {
return watchesFile, true
}
// Next, check if the default watches file is present. If so, use it.
if _, err := os.Stat(defaultHelmChartWatchesFile); err == nil {
return defaultHelmChartWatchesFile, true
}
return "", false
}