mirror of
https://github.com/lingble/talos.git
synced 2025-11-30 21:03:41 +00:00
refactor: rename Helper to Client
The name helper isn't very good. This renames it to Client. A new func was also added, NewForConfig, that will allow for the creation of the helper client from an arbitrary Kubernetes REST config. Signed-off-by: Andrew Rynhard <andrew@andrewrynhard.com>
This commit is contained in:
@@ -35,9 +35,9 @@ func (task *CordonAndDrain) standard() (err error) {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
var kubeHelper *kubernetes.Helper
|
var kubeHelper *kubernetes.Client
|
||||||
|
|
||||||
if kubeHelper, err = kubernetes.NewHelper(); err != nil {
|
if kubeHelper, err = kubernetes.NewClientFromKubeletKubeconfig(); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -77,7 +77,7 @@ func (o *APID) Runner(config runtime.Configurator) (runner.Runner, error) {
|
|||||||
opts := []retry.Option{retry.WithUnits(3 * time.Second), retry.WithJitter(time.Second)}
|
opts := []retry.Option{retry.WithUnits(3 * time.Second), retry.WithJitter(time.Second)}
|
||||||
|
|
||||||
err := retry.Constant(10*time.Minute, opts...).Retry(func() error {
|
err := retry.Constant(10*time.Minute, opts...).Retry(func() error {
|
||||||
h, err := kubernetes.NewHelper()
|
h, err := kubernetes.NewClientFromKubeletKubeconfig()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return retry.ExpectedError(fmt.Errorf("failed to create client: %w", err))
|
return retry.ExpectedError(fmt.Errorf("failed to create client: %w", err))
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -31,14 +31,14 @@ import (
|
|||||||
"github.com/talos-systems/talos/pkg/retry"
|
"github.com/talos-systems/talos/pkg/retry"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Helper represents a set of helper methods for interacting with the
|
// Client represents a set of helper methods for interacting with the
|
||||||
// Kubernetes API.
|
// Kubernetes API.
|
||||||
type Helper struct {
|
type Client struct {
|
||||||
client *kubernetes.Clientset
|
client *kubernetes.Clientset
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewHelper initializes and returns a Helper.
|
// NewClientFromKubeletKubeconfig initializes and returns a Client.
|
||||||
func NewHelper() (helper *Helper, err error) {
|
func NewClientFromKubeletKubeconfig() (client *Client, err error) {
|
||||||
var config *restclient.Config
|
var config *restclient.Config
|
||||||
|
|
||||||
config, err = clientcmd.BuildConfigFromFlags("", constants.KubeletKubeconfig)
|
config, err = clientcmd.BuildConfigFromFlags("", constants.KubeletKubeconfig)
|
||||||
@@ -53,11 +53,23 @@ func NewHelper() (helper *Helper, err error) {
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
return &Helper{clientset}, nil
|
return &Client{clientset}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewClientFromPKI initializes and returns a Helper.
|
// NewForConfig initializes and returns a client using the provided config.
|
||||||
func NewClientFromPKI(ca, crt, key []byte, host, port string) (helper *Helper, err error) {
|
func NewForConfig(config *restclient.Config) (client *Client, err error) {
|
||||||
|
var clientset *kubernetes.Clientset
|
||||||
|
|
||||||
|
clientset, err = kubernetes.NewForConfig(config)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return &Client{clientset}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewClientFromPKI initializes and returns a Client.
|
||||||
|
func NewClientFromPKI(ca, crt, key []byte, host, port string) (client *Client, err error) {
|
||||||
tlsClientConfig := restclient.TLSClientConfig{
|
tlsClientConfig := restclient.TLSClientConfig{
|
||||||
CAData: ca,
|
CAData: ca,
|
||||||
CertData: crt,
|
CertData: crt,
|
||||||
@@ -76,12 +88,12 @@ func NewClientFromPKI(ca, crt, key []byte, host, port string) (helper *Helper, e
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
return &Helper{clientset}, nil
|
return &Client{clientset}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewTemporaryClientFromPKI initializes a Kubernetes client using a certificate
|
// NewTemporaryClientFromPKI initializes a Kubernetes client using a certificate
|
||||||
// with a TTL of 10 minutes.
|
// with a TTL of 10 minutes.
|
||||||
func NewTemporaryClientFromPKI(caCrt, caKey []byte, endpoint, port string) (helper *Helper, err error) {
|
func NewTemporaryClientFromPKI(caCrt, caKey []byte, endpoint, port string) (client *Client, err error) {
|
||||||
opts := []x509.Option{
|
opts := []x509.Option{
|
||||||
x509.RSA(true),
|
x509.RSA(true),
|
||||||
x509.CommonName("admin"),
|
x509.CommonName("admin"),
|
||||||
@@ -123,7 +135,7 @@ func NewTemporaryClientFromPKI(caCrt, caKey []byte, endpoint, port string) (help
|
|||||||
}
|
}
|
||||||
|
|
||||||
// MasterIPs cordons and drains a node in one call.
|
// MasterIPs cordons and drains a node in one call.
|
||||||
func (h *Helper) MasterIPs() (addrs []string, err error) {
|
func (h *Client) MasterIPs() (addrs []string, err error) {
|
||||||
endpoints, err := h.client.CoreV1().Endpoints("default").Get("kubernetes", metav1.GetOptions{})
|
endpoints, err := h.client.CoreV1().Endpoints("default").Get("kubernetes", metav1.GetOptions{})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@@ -141,7 +153,7 @@ func (h *Helper) MasterIPs() (addrs []string, err error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// LabelNodeAsMaster labels a node with the required master label.
|
// LabelNodeAsMaster labels a node with the required master label.
|
||||||
func (h *Helper) LabelNodeAsMaster(name string) (err error) {
|
func (h *Client) LabelNodeAsMaster(name string) (err error) {
|
||||||
n, err := h.client.CoreV1().Nodes().Get(name, metav1.GetOptions{})
|
n, err := h.client.CoreV1().Nodes().Get(name, metav1.GetOptions{})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
@@ -182,7 +194,7 @@ func (h *Helper) LabelNodeAsMaster(name string) (err error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// CordonAndDrain cordons and drains a node in one call.
|
// CordonAndDrain cordons and drains a node in one call.
|
||||||
func (h *Helper) CordonAndDrain(node string) (err error) {
|
func (h *Client) CordonAndDrain(node string) (err error) {
|
||||||
if err = h.Cordon(node); err != nil {
|
if err = h.Cordon(node); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -191,7 +203,7 @@ func (h *Helper) CordonAndDrain(node string) (err error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Cordon marks a node as unschedulable.
|
// Cordon marks a node as unschedulable.
|
||||||
func (h *Helper) Cordon(name string) error {
|
func (h *Client) Cordon(name string) error {
|
||||||
node, err := h.client.CoreV1().Nodes().Get(name, metav1.GetOptions{})
|
node, err := h.client.CoreV1().Nodes().Get(name, metav1.GetOptions{})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("failed to get node %s: %w", name, err)
|
return fmt.Errorf("failed to get node %s: %w", name, err)
|
||||||
@@ -211,7 +223,7 @@ func (h *Helper) Cordon(name string) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Uncordon marks a node as schedulable.
|
// Uncordon marks a node as schedulable.
|
||||||
func (h *Helper) Uncordon(name string) error {
|
func (h *Client) Uncordon(name string) error {
|
||||||
node, err := h.client.CoreV1().Nodes().Get(name, metav1.GetOptions{})
|
node, err := h.client.CoreV1().Nodes().Get(name, metav1.GetOptions{})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("failed to get node %s: %w", name, err)
|
return fmt.Errorf("failed to get node %s: %w", name, err)
|
||||||
@@ -228,7 +240,7 @@ func (h *Helper) Uncordon(name string) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Drain evicts all pods on a given node.
|
// Drain evicts all pods on a given node.
|
||||||
func (h *Helper) Drain(node string) error {
|
func (h *Client) Drain(node string) error {
|
||||||
opts := metav1.ListOptions{
|
opts := metav1.ListOptions{
|
||||||
FieldSelector: fields.SelectorFromSet(fields.Set{"spec.nodeName": node}).String(),
|
FieldSelector: fields.SelectorFromSet(fields.Set{"spec.nodeName": node}).String(),
|
||||||
}
|
}
|
||||||
@@ -264,7 +276,7 @@ func (h *Helper) Drain(node string) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *Helper) evict(p corev1.Pod, gracePeriod int64) error {
|
func (h *Client) evict(p corev1.Pod, gracePeriod int64) error {
|
||||||
for {
|
for {
|
||||||
pol := &policy.Eviction{
|
pol := &policy.Eviction{
|
||||||
ObjectMeta: metav1.ObjectMeta{Namespace: p.GetNamespace(), Name: p.GetName()},
|
ObjectMeta: metav1.ObjectMeta{Namespace: p.GetNamespace(), Name: p.GetName()},
|
||||||
@@ -287,7 +299,7 @@ func (h *Helper) evict(p corev1.Pod, gracePeriod int64) error {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *Helper) waitForPodDeleted(p *corev1.Pod) error {
|
func (h *Client) waitForPodDeleted(p *corev1.Pod) error {
|
||||||
return retry.Constant(time.Minute, retry.WithUnits(3*time.Second)).Retry(func() error {
|
return retry.Constant(time.Minute, retry.WithUnits(3*time.Second)).Retry(func() error {
|
||||||
pod, err := h.client.CoreV1().Pods(p.GetNamespace()).Get(p.GetName(), metav1.GetOptions{})
|
pod, err := h.client.CoreV1().Pods(p.GetNamespace()).Get(p.GetName(), metav1.GetOptions{})
|
||||||
switch {
|
switch {
|
||||||
|
|||||||
Reference in New Issue
Block a user