This repository has been archived by the owner on Jun 12, 2023. It is now read-only.
/
wrapper.go
97 lines (82 loc) 路 2.74 KB
/
wrapper.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
/*
Copyright The Pharmer 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 dokube
import (
"context"
"crypto/rsa"
"net/url"
api "pharmer.dev/pharmer/apis/v1alpha1"
"github.com/digitalocean/godo"
"gomodules.xyz/cert"
"k8s.io/client-go/tools/clientcmd"
clusterapi "sigs.k8s.io/cluster-api/pkg/apis/cluster/v1alpha1"
)
func (cm *ClusterManager) retrieveClusterStatus(cluster *godo.KubernetesCluster) error {
log := cm.Logger
u, err := url.Parse(cluster.Endpoint)
if err != nil {
log.Error(err, "failed to parse url", "endpoint", cluster.Endpoint)
return err
}
cm.Cluster.Spec.ClusterAPI.Status.APIEndpoints = append(cm.Cluster.Spec.ClusterAPI.Status.APIEndpoints, clusterapi.APIEndpoint{
Host: u.Host,
Port: 0,
})
return nil
}
func (cm *ClusterManager) StoreCertificate(c *godo.Client) error {
log := cm.Logger
kcc, _, err := c.Kubernetes.GetKubeConfig(context.Background(), cm.Cluster.Spec.Config.Cloud.Dokube.ClusterID)
if err != nil {
log.Error(err, "failed to get kubeconfig from digitalocean cluster")
return err
}
kc, err := clientcmd.Load(kcc.KubeconfigYAML)
if err != nil {
log.Error(err, "failed to load kubeconfig")
return err
}
currentContext := kc.CurrentContext
certStore := cm.StoreProvider.Certificates(cm.Cluster.Name)
_, caKey, err := certStore.Get(api.CACertName)
if err == nil {
if err = certStore.Delete(api.CACertName); err != nil {
return err
}
}
caCrt, err := cert.ParseCertsPEM(kc.Clusters[currentContext].CertificateAuthorityData)
if err != nil {
log.Error(err, "failed to parse ca-cert pem")
return err
}
if err := certStore.Create(api.CACertName, caCrt[0], caKey); err != nil {
log.Error(err, "failed to create ca-cert in store")
return err
}
adminCrt, err := cert.ParseCertsPEM(kc.AuthInfos[kc.Contexts[currentContext].AuthInfo].ClientCertificateData)
if err != nil {
log.Error(err, "failed to parse admin certs")
return err
}
adminKey, err := cert.ParsePrivateKeyPEM(kc.AuthInfos[kc.Contexts[currentContext].AuthInfo].ClientKeyData)
if err != nil {
log.Error(err, "failed to parse admin key")
return err
}
err = certStore.Create("admin", adminCrt[0], adminKey.(*rsa.PrivateKey))
if err != nil {
log.Error(err, "failed to create admin certs & key in store")
return err
}
return nil
}