github.com/openshift/installer@v1.4.17/pkg/agent/kube.go (about) 1 package agent 2 3 import ( 4 "context" 5 6 "github.com/pkg/errors" 7 "github.com/sirupsen/logrus" 8 certificatesv1 "k8s.io/api/certificates/v1" 9 corev1 "k8s.io/api/core/v1" 10 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 11 "k8s.io/client-go/kubernetes" 12 certificatesClient "k8s.io/client-go/kubernetes/typed/certificates/v1" 13 "k8s.io/client-go/rest" 14 "k8s.io/client-go/tools/clientcmd" 15 ) 16 17 // ClusterKubeAPIClient is a kube client to interact with the cluster that agent installer is installing. 18 type ClusterKubeAPIClient struct { 19 Client *kubernetes.Clientset 20 csrClient certificatesClient.CertificateSigningRequestInterface 21 ctx context.Context 22 config *rest.Config 23 configPath string 24 } 25 26 // NewClusterKubeAPIClient Create a new kube client to interact with the cluster under install. 27 func NewClusterKubeAPIClient(ctx context.Context, kubeconfigPath string) (*ClusterKubeAPIClient, error) { 28 kubeClient := &ClusterKubeAPIClient{} 29 30 var kubeconfig *rest.Config 31 var err error 32 if kubeconfigPath != "" { 33 kubeconfig, err = clientcmd.BuildConfigFromFlags("", kubeconfigPath) 34 } else { 35 kubeconfig, err = rest.InClusterConfig() 36 } 37 if err != nil { 38 return nil, errors.Wrap(err, "error loading kubeconfig from assets") 39 } 40 41 kubeclient, err := kubernetes.NewForConfig(kubeconfig) 42 if err != nil { 43 return nil, errors.Wrap(err, "creating a Kubernetes client from assets failed") 44 } 45 46 csrClient := kubeclient.CertificatesV1().CertificateSigningRequests() 47 48 kubeClient.Client = kubeclient 49 kubeClient.csrClient = csrClient 50 kubeClient.ctx = ctx 51 kubeClient.config = kubeconfig 52 kubeClient.configPath = kubeconfigPath 53 54 return kubeClient, nil 55 } 56 57 // IsKubeAPILive Determine if the cluster under install has initailized the kubenertes API. 58 func (kube *ClusterKubeAPIClient) IsKubeAPILive() bool { 59 discovery := kube.Client.Discovery() 60 _, err := discovery.ServerVersion() 61 return err == nil 62 } 63 64 // DoesKubeConfigExist Determine if the kubeconfig for the cluster can be used without errors. 65 func (kube *ClusterKubeAPIClient) DoesKubeConfigExist() (bool, error) { 66 _, err := clientcmd.LoadFromFile(kube.configPath) 67 if err != nil { 68 return false, errors.Wrap(err, "error loading kubeconfig from file") 69 } 70 return true, nil 71 } 72 73 // IsBootstrapConfigMapComplete Detemine if the cluster's bootstrap configmap has the status complete. 74 func (kube *ClusterKubeAPIClient) IsBootstrapConfigMapComplete() (bool, error) { 75 // Get latest version of bootstrap configmap 76 bootstrap, err := kube.Client.CoreV1().ConfigMaps("kube-system").Get(kube.ctx, "bootstrap", metav1.GetOptions{}) 77 78 if err != nil { 79 // bootstrap configmap not found 80 return false, nil 81 } 82 // Found a bootstrap configmap need to check its status 83 if bootstrap != nil { 84 status, ok := bootstrap.Data["status"] 85 if !ok { 86 logrus.Debug("no status found in bootstrap configmap") 87 return false, nil 88 } 89 if status == "complete" { 90 return true, nil 91 } 92 } 93 return false, nil 94 } 95 96 // ListNodes returns a list of nodes that have joined the cluster. 97 func (kube *ClusterKubeAPIClient) ListNodes() (*corev1.NodeList, error) { 98 nodeList, err := kube.Client.CoreV1().Nodes().List(context.TODO(), metav1.ListOptions{}) 99 if err != nil { 100 return &corev1.NodeList{}, err 101 } 102 return nodeList, nil 103 } 104 105 // ListCSRs returns a list of this cluster's CSRs. 106 func (kube *ClusterKubeAPIClient) ListCSRs() (*certificatesv1.CertificateSigningRequestList, error) { 107 csrs, err := kube.csrClient.List(context.TODO(), metav1.ListOptions{}) 108 if err != nil { 109 return nil, err 110 } 111 return csrs, nil 112 }