Skip to content
This repository was archived by the owner on Jul 30, 2021. It is now read-only.

Commit 67621ba

Browse files
authored
Merge pull request #198 from vincepri/remove-kubeconfig-generation
⚠️ Remove kubeconfig generation
2 parents 5d6bcdb + e206c28 commit 67621ba

File tree

3 files changed

+0
-111
lines changed

3 files changed

+0
-111
lines changed

certs/certs.go

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -200,25 +200,3 @@ func EncodePublicKeyPEM(key *rsa.PublicKey) ([]byte, error) {
200200
}
201201
return pem.EncodeToMemory(&block), nil
202202
}
203-
204-
// DecodeCertPEM attempts to return a decoded certificate or nil
205-
// if the encoded input does not contain a certificate.
206-
func DecodeCertPEM(encoded []byte) (*x509.Certificate, error) {
207-
block, _ := pem.Decode(encoded)
208-
if block == nil {
209-
return nil, nil
210-
}
211-
212-
return x509.ParseCertificate(block.Bytes)
213-
}
214-
215-
// DecodePrivateKeyPEM attempts to return a decoded key or nil
216-
// if the encoded input does not contain a private key.
217-
func DecodePrivateKeyPEM(encoded []byte) (*rsa.PrivateKey, error) {
218-
block, _ := pem.Decode(encoded)
219-
if block == nil {
220-
return nil, nil
221-
}
222-
223-
return x509.ParsePKCS1PrivateKey(block.Bytes)
224-
}

certs/utils.go

Lines changed: 0 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,12 @@ import (
2323
"crypto/x509"
2424
"crypto/x509/pkix"
2525
"encoding/hex"
26-
"fmt"
2726
"math"
2827
"math/big"
2928
"strings"
3029
"time"
3130

3231
"github.com/pkg/errors"
33-
"k8s.io/client-go/tools/clientcmd/api"
3432
"k8s.io/client-go/util/cert"
3533
)
3634

@@ -96,50 +94,6 @@ func (c *Certificates) ToMap() map[string][]byte {
9694
}
9795
}
9896

99-
// NewKubeconfig creates a new Kubeconfig where endpoint is the ELB endpoint.
100-
func NewKubeconfig(clusterName, endpoint string, caCert *x509.Certificate, caKey *rsa.PrivateKey) (*api.Config, error) {
101-
cfg := &Config{
102-
CommonName: "kubernetes-admin",
103-
Organization: []string{"system:masters"},
104-
Usages: []x509.ExtKeyUsage{x509.ExtKeyUsageClientAuth},
105-
}
106-
107-
clientKey, err := NewPrivateKey()
108-
if err != nil {
109-
return nil, errors.Wrap(err, "unable to create private key")
110-
}
111-
112-
clientCert, err := cfg.NewSignedCert(clientKey, caCert, caKey)
113-
if err != nil {
114-
return nil, errors.Wrap(err, "unable to sign certificate")
115-
}
116-
117-
userName := "kubernetes-admin"
118-
contextName := fmt.Sprintf("%s@%s", userName, clusterName)
119-
120-
return &api.Config{
121-
Clusters: map[string]*api.Cluster{
122-
clusterName: {
123-
Server: endpoint,
124-
CertificateAuthorityData: EncodeCertPEM(caCert),
125-
},
126-
},
127-
Contexts: map[string]*api.Context{
128-
contextName: {
129-
Cluster: clusterName,
130-
AuthInfo: userName,
131-
},
132-
},
133-
AuthInfos: map[string]*api.AuthInfo{
134-
userName: {
135-
ClientKeyData: EncodePrivateKeyPEM(clientKey),
136-
ClientCertificateData: EncodeCertPEM(clientCert),
137-
},
138-
},
139-
CurrentContext: contextName,
140-
}, nil
141-
}
142-
14397
// NewSignedCert creates a signed certificate using the given CA certificate and key
14498
func (cfg *Config) NewSignedCert(key *rsa.PrivateKey, caCert *x509.Certificate, caKey *rsa.PrivateKey) (*x509.Certificate, error) {
14599
serial, err := rand.Int(rand.Reader, new(big.Int).SetInt64(math.MaxInt64))

controllers/kubeadmconfig_controller.go

Lines changed: 0 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ import (
2929
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
3030
"k8s.io/apimachinery/pkg/types"
3131
typedcorev1 "k8s.io/client-go/kubernetes/typed/core/v1"
32-
"k8s.io/client-go/tools/clientcmd"
3332
bootstrapv1 "sigs.k8s.io/cluster-api-bootstrap-provider-kubeadm/api/v1alpha2"
3433
"sigs.k8s.io/cluster-api-bootstrap-provider-kubeadm/certs"
3534
"sigs.k8s.io/cluster-api-bootstrap-provider-kubeadm/cloudinit"
@@ -234,12 +233,6 @@ func (r *KubeadmConfigReconciler) Reconcile(req ctrl.Request) (_ ctrl.Result, re
234233
}
235234
}
236235

237-
err = r.createKubeconfigSecret(ctx, config.Spec.ClusterConfiguration.ClusterName, config.Spec.ClusterConfiguration.ControlPlaneEndpoint, req.Namespace, certificates)
238-
if err != nil {
239-
log.Error(err, "unable to create and write kubeconfig as a Secret")
240-
return ctrl.Result{}, err
241-
}
242-
243236
cloudInitData, err := cloudinit.NewInitControlPlane(&cloudinit.ControlPlaneInput{
244237
BaseUserData: cloudinit.BaseUserData{
245238
AdditionalFiles: config.Spec.Files,
@@ -547,39 +540,3 @@ func (r *KubeadmConfigReconciler) createClusterCertificates(ctx context.Context,
547540

548541
return certificates, nil
549542
}
550-
551-
func (r *KubeadmConfigReconciler) createKubeconfigSecret(ctx context.Context, clusterName, endpoint, namespace string, certificates *certs.Certificates) error {
552-
cert, err := certs.DecodeCertPEM(certificates.ClusterCA.Cert)
553-
if err != nil {
554-
return errors.Wrap(err, "failed to decode CA Cert")
555-
} else if cert == nil {
556-
return errors.New("certificate not found in config")
557-
}
558-
559-
key, err := certs.DecodePrivateKeyPEM(certificates.ClusterCA.Key)
560-
if err != nil {
561-
return errors.Wrap(err, "failed to decode private key")
562-
} else if key == nil {
563-
return errors.New("CA private key not found")
564-
}
565-
566-
server := fmt.Sprintf("https://%s", endpoint)
567-
cfg, err := certs.NewKubeconfig(clusterName, server, cert, key)
568-
if err != nil {
569-
return errors.Wrap(err, "failed to generate a kubeconfig")
570-
}
571-
572-
yaml, err := clientcmd.Write(*cfg)
573-
if err != nil {
574-
return errors.Wrap(err, "failed to serialize config to yaml")
575-
}
576-
577-
secret := &corev1.Secret{}
578-
secretName := fmt.Sprintf("%s-kubeconfig", clusterName)
579-
580-
secret.ObjectMeta.Name = secretName
581-
secret.ObjectMeta.Namespace = namespace
582-
secret.StringData = map[string]string{"value": string(yaml)}
583-
584-
return r.Create(ctx, secret)
585-
}

0 commit comments

Comments
 (0)