-
Notifications
You must be signed in to change notification settings - Fork 346
/
certificate.go
96 lines (87 loc) · 3.97 KB
/
certificate.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 2020 Antrea 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 certificate
import (
"fmt"
"os"
"path/filepath"
"time"
apiextensionclientset "k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/apiserver/pkg/server/dynamiccertificates"
"k8s.io/apiserver/pkg/server/options"
"k8s.io/client-go/kubernetes"
"k8s.io/klog/v2"
"k8s.io/kube-aggregator/pkg/client/clientset_generated/clientset"
"antrea.io/antrea/pkg/util/env"
)
const (
// The names of the files that should contain the CA certificate and the TLS key pair.
CACertFile = "ca.crt"
TLSCertFile = "tls.crt"
TLSKeyFile = "tls.key"
)
// GetAntreaServerNames returns the DNS names that the TLS certificate will be signed with.
func GetAntreaServerNames(serviceName string) []string {
namespace := env.GetAntreaNamespace()
antreaServerName := serviceName + "." + namespace + ".svc"
// TODO: Although antrea-agent and kube-aggregator only verify the server name "antrea.<Namespace>.svc",
// We should add the whole FQDN "antrea.<Namespace>.svc.<Cluster Domain>" as an alternate DNS name when
// other clients need to access it directly with that name.
return []string{antreaServerName}
}
func ApplyServerCert(selfSignedCert bool,
client kubernetes.Interface,
aggregatorClient clientset.Interface,
apiExtensionClient apiextensionclientset.Interface,
secureServing *options.SecureServingOptionsWithLoopback,
caConfig *CAConfig) (*CACertController, error) {
var err error
var caContentProvider dynamiccertificates.CAContentProvider
if selfSignedCert {
caContentProvider, err = newSelfSignedCertProvider(client, secureServing, caConfig)
if err != nil {
return nil, fmt.Errorf("failed to initialize self-signed certificate: %w", err)
}
} else {
caCertPath := filepath.Join(caConfig.CertDir, CACertFile)
tlsCertPath := filepath.Join(caConfig.CertDir, TLSCertFile)
tlsKeyPath := filepath.Join(caConfig.CertDir, TLSKeyFile)
// The secret may be created after the Pod is created, for example, when cert-manager is used the secret
// is created asynchronously. It waits for a while before it's considered to be failed.
if err = wait.PollImmediate(2*time.Second, caConfig.CertReadyTimeout, func() (bool, error) {
for _, path := range []string{caCertPath, tlsCertPath, tlsKeyPath} {
f, err := os.Open(path)
if err != nil {
klog.Warningf("Couldn't read %s when applying server certificate, retrying", path)
return false, nil
}
f.Close()
}
return true, nil
}); err != nil {
return nil, fmt.Errorf("error reading TLS certificate and/or key. Please make sure the TLS CA (%s), cert (%s), and key (%s) files are present in \"%s\", when selfSignedCert is set to false", CACertFile, TLSCertFile, TLSKeyFile, caConfig.CertDir)
}
// Since 1.17.0 (https://github.com/kubernetes/kubernetes/commit/3f5fbfbfac281f40c11de2f57d58cc332affc37b),
// apiserver reloads certificate cert and key file from disk every minute, allowing serving tls config to be updated.
secureServing.ServerCert.CertKey.CertFile = tlsCertPath
secureServing.ServerCert.CertKey.KeyFile = tlsKeyPath
caContentProvider, err = dynamiccertificates.NewDynamicCAContentFromFile("user-provided CA cert", caCertPath)
if err != nil {
return nil, fmt.Errorf("error reading user-provided CA certificate: %v", err)
}
}
caCertController := newCACertController(caContentProvider, client, aggregatorClient, apiExtensionClient, caConfig)
return caCertController, nil
}