Skip to content

Commit

Permalink
fixed issue #48
Browse files Browse the repository at this point in the history
Signed-off-by: Mohamed Belgaied Hassine <[email protected]>
  • Loading branch information
belgaied2 committed Jun 18, 2024
1 parent db1ee42 commit 5642e2a
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 15 deletions.
52 changes: 37 additions & 15 deletions controllers/harvestercluster_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -554,28 +554,19 @@ func (r *HarvesterClusterReconciler) reconcileHarvesterConfig(ctx context.Contex

kubeconfig := secret.Data[locutil.ConfigSecretDataKey]

var config *clientcmdapi.Config

if config, err = clientcmd.Load(kubeconfig); err != nil {
harvesterServer, err := getHarvesterServerFromKubeconfig(kubeconfig)
if err != nil {
cluster.Status = infrav1.HarvesterClusterStatus{
FailureReason: "MalformedIdentitySecret",
FailureMessage: "unable to Load a valid Harvester config from the referenced Secret",
FailureMessage: err.Error(),
Ready: false,
}

if err := r.Status().Update(ctx, cluster); err != nil {
return &rest.Config{}, errors.Wrapf(err, "failed to update status")
}

return &rest.Config{}, errors.Wrapf(err, "unable to Load a valid Harvester config from the referenced Secret %s", ctx)
return &rest.Config{}, err
}

configCluster := config.Clusters[config.CurrentContext]

configServer := configCluster.Server

if cluster.Spec.Server == "" || cluster.Spec.Server != configServer {
cluster.Spec.Server = configServer
if cluster.Spec.Server == "" || cluster.Spec.Server != harvesterServer {
cluster.Spec.Server = harvesterServer
logger.Info("Value for Server is now set to " + cluster.Spec.Server)
}

Expand Down Expand Up @@ -837,3 +828,34 @@ func (r *HarvesterClusterReconciler) ReconcileDelete(scope ClusterScope) (ctrl.R

return ctrl.Result{}, nil
}

func getHarvesterServerFromKubeconfig(kubeconfig []byte) (server string, err error) {
var config *clientcmdapi.Config

if config, err = clientcmd.Load(kubeconfig); err != nil {
return "", errors.Wrapf(err, "unable to Load a valid Harvester config from the referenced Secret")
}

if config.CurrentContext == "" {
return "", fmt.Errorf("the provided Kubeconfig is malformed: no current-context set.")
}

configContext := config.Contexts[config.CurrentContext]
if configContext == nil {
return "", fmt.Errorf("the provided Kubeconfig is malformed, no context section corresponds to the current-context, with the name %s",
config.CurrentContext)
}

configCluster := config.Clusters[configContext.Cluster]
if configCluster == nil {
return "", fmt.Errorf("the provided Kubeconfig is malformed, no cluster section corresponds to the cluster name %s in the context %s",
configContext.Cluster, config.CurrentContext)
}

configServer := configCluster.Server
if configServer == "" {
return "", fmt.Errorf("the provided Kubeconfig is malformed, no server found for cluster %s", configContext.Cluster)
}

return configServer, nil
}
53 changes: 53 additions & 0 deletions controllers/harvestercluster_controller_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
Copyright 2024.
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 controllers

import (
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)

var _ = Describe("Extract Server from Kubeconfig", func() {
var kubeconfig []byte

BeforeEach(func() {
kubeconfig = []byte(`apiVersion: v1
clusters:
- cluster:
certificate-authority-data: LS0tLS1CRUdJTiBDRVJUSUZJQ0FURS0tLS0tCk1JSUJ2akNDQVdPZ0F3SUJBZ0lCQURBS0JnZ3Foa2pPUFFRREFqQkdNUnd3R2dZRFZRUUtFeE5rZVc1aGJXbGoKYkdsemRHVnVaWEl0YjNKbk1TWXdKQVlEVlFRRERCMWtlVzVoYldsamJHbHpkR1Z1WlhJdFkyRkFNVGN4TXpnMApNVFUwTWpBZUZ3MHlOREEwTWpNd016QTFOREphRncwek5EQTBNakV3TXpBMU5ESmFNRVl4SERBYUJnTlZCQW9UCkUyUjVibUZ0YVdOc2FYTjBaVzVsY2kxdmNtY3hKakFrQmdOVkJBTU1IV1I1Ym1GdGFXTnNhWE4wWlc1bGNpMWoKWVVBeE56RXpPRFF4TlRReU1Ga3dFd1lIS29aSXpqMENBUVlJS29aSXpqMERBUWNEUWdBRVA3V0RnRnk1NzRWVwp0SVYySzFGMExVZnE1VDJkQlFYVFovUUFIdWVqNDAzMGR1MklvN2tubzZ0SlI5OEJrNVk0bmpDK0VzT3c4UlZvCnJiWkdOVzJJdEtOQ01FQXdEZ1lEVlIwUEFRSC9CQVFEQWdLa01BOEdBMVVkRXdFQi93UUZNQU1CQWY4d0hRWUQKVlIwT0JCWUVGTWJ1c3dyTTZEQS8vcjV2NjNhejJCU3VXSkVjTUFvR0NDcUdTTTQ5QkFNQ0Ewa0FNRVlDSVFETgpKQVhOUHFtZEY4SGViUm5IMTJkTkNVWEY0TXpTd0haSTZwZzVhNDVsd1FJaEFLNHZiRGVjTEIyVzBuQnJ1S0F2ClprNy9lb2JLT05TcEthRzBJdjhHaGhTdQotLS0tLUVORCBDRVJUSUZJQ0FURS0tLS0t
server: https://10.10.0.10/k8s/clusters/local
name: local
contexts:
- context:
cluster: local
user: local
name: deathstar
current-context: deathstar
kind: Config
preferences: {}
users:
- name: local
user:
token: kubeconfig-user-gqrx7b7k5x:qsp8xzzd4dpb9d99n9fg8vrtdrqndplmlfjtzpkshc9jcndn9fc2ns
`)
})
Context("When we provide a kubeconfig with different current-context and cluster.name", func() {
It("Should still return the right cluster", func() {
Expect(getHarvesterServerFromKubeconfig(kubeconfig)).To(Equal("https://10.10.0.10/k8s/clusters/local"))
})
})
})

0 comments on commit 5642e2a

Please sign in to comment.