Skip to content
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

fix: x509 error when adding cluster with different cadata (Fixes #21326) #21327

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion cmd/argocd/commands/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,13 +166,14 @@ func NewClusterAddCommand(clientOpts *argocdclient.ClientOptions, pathOpts *clie
if clusterOpts.InClusterEndpoint() {
clst.Server = argoappv1.KubernetesInternalAPIServerAddr
} else if clusterOpts.ClusterEndpoint == string(cmdutil.KubePublicEndpoint) {
endpoint, err := cmdutil.GetKubePublicEndpoint(clientset)
endpoint, caData, err := cmdutil.GetKubePublicEndpoint(clientset)
if err != nil || len(endpoint) == 0 {
log.Warnf("Failed to find the cluster endpoint from kube-public data: %v", err)
log.Infof("Falling back to the endpoint '%s' as listed in the kubeconfig context", clst.Server)
endpoint = clst.Server
}
clst.Server = endpoint
clst.Config.TLSClientConfig.CAData = caData
}

if clusterOpts.Shard >= 0 {
Expand Down
16 changes: 9 additions & 7 deletions cmd/util/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,28 +123,30 @@ func NewCluster(name string, namespaces []string, clusterResources bool, conf *r
return &clst
}

// GetKubePublicEndpoint returns the kubernetes apiserver endpoint as published
// GetKubePublicEndpoint returns the kubernetes apiserver endpoint and certificate authority data as published
// in the kube-public.
func GetKubePublicEndpoint(client kubernetes.Interface) (string, error) {
func GetKubePublicEndpoint(client kubernetes.Interface) (string, []byte, error) {
clusterInfo, err := client.CoreV1().ConfigMaps("kube-public").Get(context.TODO(), "cluster-info", metav1.GetOptions{})
if err != nil {
return "", err
return "", nil, err
}
kubeconfig, ok := clusterInfo.Data["kubeconfig"]
if !ok {
return "", stderrors.New("cluster-info does not contain a public kubeconfig")
return "", nil, stderrors.New("cluster-info does not contain a public kubeconfig")
}
// Parse Kubeconfig and get server address
config := &clientcmdapiv1.Config{}
err = yaml.Unmarshal([]byte(kubeconfig), config)
if err != nil {
return "", fmt.Errorf("failed to parse cluster-info kubeconfig: %w", err)
return "", nil, fmt.Errorf("failed to parse cluster-info kubeconfig: %w", err)
}
if len(config.Clusters) == 0 {
return "", stderrors.New("cluster-info kubeconfig does not have any clusters")
return "", nil, stderrors.New("cluster-info kubeconfig does not have any clusters")
}

return config.Clusters[0].Cluster.Server, nil
endpoint := config.Clusters[0].Cluster.Server
certificateAuthorityData := config.Clusters[0].Cluster.CertificateAuthorityData
return endpoint, certificateAuthorityData, nil
}

type ClusterOptions struct {
Expand Down
28 changes: 23 additions & 5 deletions cmd/util/cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,23 @@ func TestGetKubePublicEndpoint(t *testing.T) {
name string
clusterInfo *corev1.ConfigMap
expectedEndpoint string
expectedCAData []byte
expectError bool
}{
{
name: "has public endpoint and certificate authority data",
clusterInfo: &corev1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{
Namespace: "kube-public",
Name: "cluster-info",
},
Data: map[string]string{
"kubeconfig": kubeconfigFixture("https://test-cluster:6443", []byte("test-ca-data")),
},
},
expectedEndpoint: "https://test-cluster:6443",
expectedCAData: []byte("test-ca-data"),
},
{
name: "has public endpoint",
clusterInfo: &corev1.ConfigMap{
Expand All @@ -106,10 +121,11 @@ func TestGetKubePublicEndpoint(t *testing.T) {
Name: "cluster-info",
},
Data: map[string]string{
"kubeconfig": kubeconfigFixture("https://test-cluster:6443"),
"kubeconfig": kubeconfigFixture("https://test-cluster:6443", nil),
},
},
expectedEndpoint: "https://test-cluster:6443",
expectedCAData: nil,
},
{
name: "no cluster-info",
Expand All @@ -136,7 +152,7 @@ func TestGetKubePublicEndpoint(t *testing.T) {
Name: "cluster-info",
},
Data: map[string]string{
"kubeconfig": kubeconfigFixture(""),
"kubeconfig": kubeconfigFixture("", nil),
},
},
expectError: true,
Expand All @@ -163,25 +179,27 @@ func TestGetKubePublicEndpoint(t *testing.T) {
objects = append(objects, tc.clusterInfo)
}
clientset := fake.NewClientset(objects...)
endpoint, err := GetKubePublicEndpoint(clientset)
endpoint, caData, err := GetKubePublicEndpoint(clientset)
if tc.expectError {
require.Error(t, err)
} else {
require.NoError(t, err)
}
require.Equalf(t, tc.expectedEndpoint, endpoint, "expected endpoint %s, got %s", tc.expectedEndpoint, endpoint)
require.Equalf(t, tc.expectedCAData, caData, "expected caData %s, got %s", tc.expectedCAData, caData)
})
}
}

func kubeconfigFixture(endpoint string) string {
func kubeconfigFixture(endpoint string, certificateAuthorityData []byte) string {
kubeconfig := &clientcmdapiv1.Config{}
if len(endpoint) > 0 {
kubeconfig.Clusters = []clientcmdapiv1.NamedCluster{
{
Name: "test-kube",
Cluster: clientcmdapiv1.Cluster{
Server: endpoint,
Server: endpoint,
CertificateAuthorityData: certificateAuthorityData,
},
},
}
Expand Down
Loading