Migrate to generated clients
This commit is contained in:
@@ -7,9 +7,6 @@ import (
|
||||
|
||||
apierrors "k8s.io/apimachinery/pkg/api/errors"
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
|
||||
"k8s.io/apimachinery/pkg/runtime"
|
||||
"k8s.io/apimachinery/pkg/runtime/schema"
|
||||
"k8s.io/client-go/util/retry"
|
||||
"k8s.io/klog/v2"
|
||||
|
||||
@@ -17,16 +14,6 @@ import (
|
||||
"example.com/monok8s/pkg/kube"
|
||||
)
|
||||
|
||||
var (
|
||||
unstructuredConverter = runtime.DefaultUnstructuredConverter
|
||||
|
||||
osup_gvr = schema.GroupVersionResource{
|
||||
Group: monov1alpha1.Group,
|
||||
Version: monov1alpha1.Version,
|
||||
Resource: "osupgradeprogresses",
|
||||
}
|
||||
)
|
||||
|
||||
func EnsureOSUpgradeProgressForNode(
|
||||
ctx context.Context,
|
||||
clients *kube.Clients,
|
||||
@@ -39,6 +26,7 @@ func EnsureOSUpgradeProgressForNode(
|
||||
}
|
||||
|
||||
name := fmt.Sprintf("%s-%s", osu.Name, nodeName)
|
||||
now := metav1.Now()
|
||||
|
||||
progress := &monov1alpha1.OSUpgradeProgress{
|
||||
TypeMeta: metav1.TypeMeta{
|
||||
@@ -55,9 +43,13 @@ func EnsureOSUpgradeProgressForNode(
|
||||
Name: osu.Name,
|
||||
},
|
||||
},
|
||||
Status: &monov1alpha1.OSUpgradeProgressStatus{
|
||||
Phase: monov1alpha1.OSUpgradeProgressPhasePending,
|
||||
LastUpdatedAt: &now,
|
||||
},
|
||||
}
|
||||
|
||||
created, err := createProgress(ctx, clients, osup_gvr, progress)
|
||||
created, err := createProgress(ctx, clients, progress)
|
||||
if err == nil {
|
||||
klog.InfoS("created osupgradeprogress", "name", created.Name, "namespace", created.Namespace)
|
||||
return nil
|
||||
@@ -66,7 +58,7 @@ func EnsureOSUpgradeProgressForNode(
|
||||
return fmt.Errorf("create OSUpgradeProgress %s/%s: %w", namespace, name, err)
|
||||
}
|
||||
|
||||
existing, err := getProgress(ctx, clients, osup_gvr, namespace, name)
|
||||
existing, err := getProgress(ctx, clients, namespace, name)
|
||||
if err != nil {
|
||||
return fmt.Errorf("get existing OSUpgradeProgress %s/%s: %w", namespace, name, err)
|
||||
}
|
||||
@@ -96,7 +88,7 @@ func updateProgressRobust(
|
||||
var out *monov1alpha1.OSUpgradeProgress
|
||||
|
||||
err := retry.RetryOnConflict(retry.DefaultRetry, func() error {
|
||||
current, err := getProgress(ctx, clients, osup_gvr, namespace, name)
|
||||
current, err := getProgress(ctx, clients, namespace, name)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -107,10 +99,10 @@ func updateProgressRobust(
|
||||
|
||||
mutate(current)
|
||||
|
||||
updated, err := updateProgressStatus(ctx, clients, osup_gvr, current)
|
||||
updated, err := updateProgressStatus(ctx, clients, current)
|
||||
if err != nil {
|
||||
if isUnknownUpdateResult(err) {
|
||||
latest, getErr := getProgress(ctx, clients, osup_gvr, namespace, name)
|
||||
latest, getErr := getProgress(ctx, clients, namespace, name)
|
||||
if getErr == nil {
|
||||
out = latest
|
||||
}
|
||||
@@ -152,84 +144,55 @@ func isUnknownUpdateResult(err error) bool {
|
||||
func createProgress(
|
||||
ctx context.Context,
|
||||
clients *kube.Clients,
|
||||
gvr schema.GroupVersionResource,
|
||||
progress *monov1alpha1.OSUpgradeProgress,
|
||||
) (*monov1alpha1.OSUpgradeProgress, error) {
|
||||
obj, err := toUnstructured(progress)
|
||||
toCreate := progress.DeepCopy()
|
||||
toCreate.Status = nil
|
||||
|
||||
created, err := clients.MonoKS.
|
||||
Monok8sV1alpha1().
|
||||
OSUpgradeProgresses(toCreate.Namespace).
|
||||
Create(ctx, toCreate, metav1.CreateOptions{})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
created, err := clients.Dynamic.
|
||||
Resource(gvr).
|
||||
Namespace(progress.Namespace).
|
||||
Create(ctx, obj, metav1.CreateOptions{})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
if progress.Status != nil {
|
||||
toUpdate := created.DeepCopy()
|
||||
toUpdate.Status = progress.Status
|
||||
return updateProgressStatus(ctx, clients, toUpdate)
|
||||
}
|
||||
|
||||
return fromUnstructuredProgress(created)
|
||||
}
|
||||
|
||||
func getProgress(
|
||||
ctx context.Context,
|
||||
clients *kube.Clients,
|
||||
gvr schema.GroupVersionResource,
|
||||
namespace, name string,
|
||||
) (*monov1alpha1.OSUpgradeProgress, error) {
|
||||
got, err := clients.Dynamic.
|
||||
Resource(gvr).
|
||||
Namespace(namespace).
|
||||
Get(ctx, name, metav1.GetOptions{})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return fromUnstructuredProgress(got)
|
||||
}
|
||||
|
||||
func updateProgressSpec(
|
||||
ctx context.Context,
|
||||
clients *kube.Clients,
|
||||
gvr schema.GroupVersionResource,
|
||||
progress *monov1alpha1.OSUpgradeProgress,
|
||||
) (*monov1alpha1.OSUpgradeProgress, error) {
|
||||
obj, err := toUnstructured(progress)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
updated, err := clients.Dynamic.
|
||||
Resource(gvr).
|
||||
Namespace(progress.Namespace).
|
||||
Update(ctx, obj, metav1.UpdateOptions{})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return fromUnstructuredProgress(updated)
|
||||
return created, nil
|
||||
}
|
||||
|
||||
func updateProgressStatus(
|
||||
ctx context.Context,
|
||||
clients *kube.Clients,
|
||||
gvr schema.GroupVersionResource,
|
||||
progress *monov1alpha1.OSUpgradeProgress,
|
||||
) (*monov1alpha1.OSUpgradeProgress, error) {
|
||||
obj, err := toUnstructured(progress)
|
||||
updated, err := clients.MonoKS.
|
||||
Monok8sV1alpha1().
|
||||
OSUpgradeProgresses(progress.Namespace).
|
||||
UpdateStatus(ctx, progress, metav1.UpdateOptions{})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return nil, fmt.Errorf(
|
||||
"update status for OSUpgradeProgress %s/%s: %w",
|
||||
progress.Namespace, progress.Name, err,
|
||||
)
|
||||
}
|
||||
return updated, nil
|
||||
}
|
||||
|
||||
updated, err := clients.Dynamic.
|
||||
Resource(gvr).
|
||||
Namespace(progress.Namespace).
|
||||
UpdateStatus(ctx, obj, metav1.UpdateOptions{})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return fromUnstructuredProgress(updated)
|
||||
func getProgress(
|
||||
ctx context.Context,
|
||||
clients *kube.Clients,
|
||||
namespace, name string,
|
||||
) (*monov1alpha1.OSUpgradeProgress, error) {
|
||||
return clients.MonoKS.
|
||||
Monok8sV1alpha1().
|
||||
OSUpgradeProgresses(namespace).
|
||||
Get(ctx, name, metav1.GetOptions{})
|
||||
}
|
||||
|
||||
func failProgress(
|
||||
@@ -287,19 +250,3 @@ func markProgressCompleted(
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func toUnstructured(progress *monov1alpha1.OSUpgradeProgress) (*unstructured.Unstructured, error) {
|
||||
m, err := unstructuredConverter.ToUnstructured(progress)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("convert OSUpgradeProgress to unstructured: %w", err)
|
||||
}
|
||||
return &unstructured.Unstructured{Object: m}, nil
|
||||
}
|
||||
|
||||
func fromUnstructuredProgress(obj *unstructured.Unstructured) (*monov1alpha1.OSUpgradeProgress, error) {
|
||||
var progress monov1alpha1.OSUpgradeProgress
|
||||
if err := unstructuredConverter.FromUnstructured(obj.Object, &progress); err != nil {
|
||||
return nil, fmt.Errorf("convert unstructured to OSUpgradeProgress: %w", err)
|
||||
}
|
||||
return &progress, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user