/
template_client.go
109 lines (91 loc) · 3.94 KB
/
template_client.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
/*
Copyright 2020 The Kubernetes 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 repository
import (
"fmt"
"github.com/pkg/errors"
"sigs.k8s.io/cluster-api/cmd/clusterctl/client/config"
logf "sigs.k8s.io/cluster-api/cmd/clusterctl/log"
)
// TemplateOptions defines a set of well-know variables that all the cluster templates are expected to manage;
// this set of variables defines a simple, day1 experience that will be made accessible via flags in the clusterctl CLI.
// Please note that each provider/each template is allowed to add more variables, but additional variables are exposed
// only via environment variables or the clusterctl configuration file.
type TemplateOptions struct {
ClusterName string
Namespace string
KubernetesVersion string
ControlplaneCount int
WorkerCount int
}
// TemplateClient has methods to work with cluster templates hosted on a provider repository.
// Templates are yaml files to be used for creating a guest cluster.
type TemplateClient interface {
Get(flavor, targetNamespace string, listVariablesOnly bool) (Template, error)
}
// templateClient implements TemplateClient.
type templateClient struct {
provider config.Provider
version string
repository Repository
configVariablesClient config.VariablesClient
}
// Ensure templateClient implements the TemplateClient interface.
var _ TemplateClient = &templateClient{}
// newTemplateClient returns a templateClient.
func newTemplateClient(provider config.Provider, version string, repository Repository, configVariablesClient config.VariablesClient) *templateClient {
return &templateClient{
provider: provider,
version: version,
repository: repository,
configVariablesClient: configVariablesClient,
}
}
// Get return the template for the flavor specified.
// In case the template does not exists, an error is returned.
// Get assumes the following naming convention for templates: cluster-template[-<flavor_name>].yaml
func (c *templateClient) Get(flavor, targetNamespace string, listVariablesOnly bool) (Template, error) {
log := logf.Log
if targetNamespace == "" {
return nil, errors.New("invalid arguments: please provide a targetNamespace")
}
// we are always reading templateClient for a well know version, that usually is
// the version of the provider installed in the management cluster.
version := c.version
// building template name according with the naming convention
name := "cluster-template"
if flavor != "" {
name = fmt.Sprintf("%s-%s", name, flavor)
}
name = fmt.Sprintf("%s.yaml", name)
// read the component YAML, reading the local override file if it exists, otherwise read from the provider repository
rawYaml, err := getLocalOverride(&newOverrideInput{
configVariablesClient: c.configVariablesClient,
provider: c.provider,
version: version,
filePath: name,
})
if err != nil {
return nil, err
}
if rawYaml == nil {
log.V(5).Info("Fetching", "File", name, "Provider", c.provider.ManifestLabel(), "Version", version)
rawYaml, err = c.repository.GetFile(version, name)
if err != nil {
return nil, errors.Wrapf(err, "failed to read %q from provider's repository %q", name, c.provider.ManifestLabel())
}
} else {
log.V(1).Info("Using", "Override", name, "Provider", c.provider.ManifestLabel(), "Version", version)
}
return NewTemplate(rawYaml, c.configVariablesClient, targetNamespace, listVariablesOnly)
}