-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
cert_manager_client.go
96 lines (76 loc) · 3 KB
/
cert_manager_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
/*
Copyright 2021 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 config
import (
"os"
"time"
"github.com/drone/envsubst/v2"
"github.com/pkg/errors"
)
const (
// CertManagerConfigKey defines the name of the top level config key for cert-manager configuration.
CertManagerConfigKey = "cert-manager"
// CertManagerDefaultVersion defines the default cert-manager version to be used by clusterctl.
CertManagerDefaultVersion = "v1.14.5"
// CertManagerDefaultURL defines the default cert-manager repository url to be used by clusterctl.
// NOTE: At runtime CertManagerDefaultVersion may be replaced with the
// version defined by the user in the clusterctl configuration file.
CertManagerDefaultURL = "https://github.com/cert-manager/cert-manager/releases/" + CertManagerDefaultVersion + "/cert-manager.yaml"
// CertManagerDefaultTimeout defines the default cert-manager timeout to be used by clusterctl.
CertManagerDefaultTimeout = 10 * time.Minute
)
// CertManagerClient has methods to work with cert-manager configurations.
type CertManagerClient interface {
// Get returns the cert-manager configuration.
Get() (CertManager, error)
}
// certManagerClient implements CertManagerClient.
type certManagerClient struct {
reader Reader
}
// ensure certManagerClient implements CertManagerClient.
var _ CertManagerClient = &certManagerClient{}
func newCertManagerClient(reader Reader) *certManagerClient {
return &certManagerClient{
reader: reader,
}
}
// configCertManager mirrors config.CertManager interface and allows serialization of the corresponding info.
type configCertManager struct {
URL string `json:"url,omitempty"`
Version string `json:"version,omitempty"`
Timeout string `json:"timeout,omitempty"`
}
func (p *certManagerClient) Get() (CertManager, error) {
url := CertManagerDefaultURL
version := CertManagerDefaultVersion
timeout := CertManagerDefaultTimeout.String()
userCertManager := &configCertManager{}
if err := p.reader.UnmarshalKey(CertManagerConfigKey, &userCertManager); err != nil {
return nil, errors.Wrap(err, "failed to unmarshal certManager from the clusterctl configuration file")
}
if userCertManager.URL != "" {
url = userCertManager.URL
}
url, err := envsubst.Eval(url, os.Getenv)
if err != nil {
return nil, errors.Wrapf(err, "unable to evaluate url: %q", url)
}
if userCertManager.Version != "" {
version = userCertManager.Version
}
if userCertManager.Timeout != "" {
timeout = userCertManager.Timeout
}
return NewCertManager(url, version, timeout), nil
}