-
Notifications
You must be signed in to change notification settings - Fork 38.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Decouple master bootstrap from CSR in kubeadm #33543
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
/* | ||
Copyright 2016 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 node | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
|
||
kubeadmapi "k8s.io/kubernetes/cmd/kubeadm/app/apis/kubeadm" | ||
kubeadmutil "k8s.io/kubernetes/cmd/kubeadm/app/util" | ||
clientset "k8s.io/kubernetes/pkg/client/clientset_generated/internalclientset" | ||
certclient "k8s.io/kubernetes/pkg/client/clientset_generated/internalclientset/typed/certificates/unversioned" | ||
"k8s.io/kubernetes/pkg/client/unversioned/clientcmd" | ||
"k8s.io/kubernetes/pkg/types" | ||
) | ||
|
||
// ConnectionDetails represents a master API endpoint connection | ||
type ConnectionDetails struct { | ||
CertClient *certclient.CertificatesClient | ||
Endpoint string | ||
CACert []byte | ||
NodeName types.NodeName | ||
} | ||
|
||
// EstablishMasterConnection establishes a connection with exactly one of the provided API endpoints or errors. | ||
func EstablishMasterConnection(s *kubeadmapi.NodeConfiguration, clusterInfo *kubeadmapi.ClusterInfo) (*ConnectionDetails, error) { | ||
hostName, err := os.Hostname() | ||
if err != nil { | ||
return nil, fmt.Errorf("<node/bootstrap> failed to get node hostname [%v]", err) | ||
} | ||
// TODO(phase1+) https://github.com/kubernetes/kubernetes/issues/33641 | ||
nodeName := types.NodeName(hostName) | ||
|
||
endpoints := clusterInfo.Endpoints | ||
caCert := []byte(clusterInfo.CertificateAuthorities[0]) | ||
|
||
var establishedConnection *ConnectionDetails | ||
// TODO: add a wait mechanism for the API endpoints (retrying to connect to at least one) | ||
for _, endpoint := range endpoints { | ||
clientSet, err := createClients(caCert, endpoint, s.Secrets.BearerToken, nodeName) | ||
if err != nil { | ||
fmt.Printf("<node/bootstrap> warning: %s. Skipping endpoint %s\n", err, endpoint) | ||
continue | ||
} | ||
fmt.Printf("<node/bootstrap> trying to connect to endpoint %s\n", endpoint) | ||
|
||
// TODO: add a simple GET /version request to fail early if needed before attempting | ||
// to connect with a discovery client. | ||
if err := checkCertsAPI(clientSet.DiscoveryClient); err != nil { | ||
fmt.Printf("<node/bootstrap> warning: failed to connect to %s: %v\n", endpoint, err) | ||
continue | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If all endpoints continue, the read from There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Mhm, I'll fix it. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done. |
||
} | ||
|
||
fmt.Printf("<node/bootstrap> successfully established connection with endpoint %s\n", endpoint) | ||
// connection established | ||
establishedConnection = &ConnectionDetails{ | ||
CertClient: clientSet.CertificatesClient, | ||
Endpoint: endpoint, | ||
CACert: caCert, | ||
NodeName: nodeName, | ||
} | ||
break | ||
} | ||
|
||
if establishedConnection == nil { | ||
return nil, fmt.Errorf("<node/bootstrap> failed to create bootstrap clients " + | ||
"for any of the provided API endpoints") | ||
} | ||
return establishedConnection, nil | ||
} | ||
|
||
// Creates a set of clients for this endpoint | ||
func createClients(caCert []byte, endpoint, token string, nodeName types.NodeName) (*clientset.Clientset, error) { | ||
bareClientConfig := kubeadmutil.CreateBasicClientConfig("kubernetes", endpoint, caCert) | ||
bootstrapClientConfig, err := clientcmd.NewDefaultClientConfig( | ||
*kubeadmutil.MakeClientConfigWithToken( | ||
bareClientConfig, "kubernetes", fmt.Sprintf("kubelet-%s", nodeName), token, | ||
), | ||
&clientcmd.ConfigOverrides{}, | ||
).ClientConfig() | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to create API client configuration [%v]", err) | ||
} | ||
clientSet, err := clientset.NewForConfig(bootstrapClientConfig) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to create clients for the API endpoint %s [%v]", endpoint, err) | ||
} | ||
return clientSet, nil | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please use a versioned clientset.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
kubeadm
currently calls this function for the CSR, which expects an unversioned cert client in its signature and is also currently used by thekubelet
as well. If I use a versioned client, I cannot directly pass it as first argument to the above function, as it does not implement the interface. What is the correct pattern for dealing with this? Is there some magic way to cast it? I am not 100% familiar with the hierarchy here ...There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fair enough. I want to prevent the spread of the unversioned client, but it can't be helped here.