Upgrade k8s package verison (#5358)

* upgrade k8s package version

Signed-off-by: hongzhouzi <hongzhouzi@kubesphere.io>

* Script upgrade and code formatting.

Signed-off-by: hongzhouzi <hongzhouzi@kubesphere.io>

Signed-off-by: hongzhouzi <hongzhouzi@kubesphere.io>
This commit is contained in:
hongzhouzi
2022-11-15 14:56:38 +08:00
committed by GitHub
parent 5f91c1663a
commit 44167aa47a
3106 changed files with 321340 additions and 172080 deletions

View File

@@ -18,9 +18,12 @@ package resource
import (
"context"
"fmt"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/fields"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
@@ -34,6 +37,8 @@ var metadataAccessor = meta.NewAccessor()
type Helper struct {
// The name of this resource as the server would recognize it
Resource string
// The name of the subresource as the server would recognize it
Subresource string
// A RESTClient capable of mutating this resource.
RESTClient RESTClient
// True if the resource type is scoped to namespaces
@@ -49,6 +54,10 @@ type Helper struct {
// FieldManager is the name associated with the actor or entity that is making
// changes.
FieldManager string
// FieldValidation is the directive used to indicate how the server should perform
// field validation (Ignore, Warn, or Strict)
FieldValidation string
}
// NewHelper creates a Helper from a ResourceMapping
@@ -74,11 +83,25 @@ func (m *Helper) WithFieldManager(fieldManager string) *Helper {
return m
}
// WithFieldValidation sets the field validation option to indicate
// how the server should perform field validation (Ignore, Warn, or Strict).
func (m *Helper) WithFieldValidation(validationDirective string) *Helper {
m.FieldValidation = validationDirective
return m
}
// Subresource sets the helper to access (<resource>/[ns/<namespace>/]<name>/<subresource>)
func (m *Helper) WithSubresource(subresource string) *Helper {
m.Subresource = subresource
return m
}
func (m *Helper) Get(namespace, name string) (runtime.Object, error) {
req := m.RESTClient.Get().
NamespaceIfScoped(namespace, m.NamespaceScoped).
Resource(m.Resource).
Name(name)
Name(name).
SubResource(m.Subresource)
return req.Do(context.TODO()).Get()
}
@@ -90,6 +113,54 @@ func (m *Helper) List(namespace, apiVersion string, options *metav1.ListOptions)
return req.Do(context.TODO()).Get()
}
// FollowContinue handles the continue parameter returned by the API server when using list
// chunking. To take advantage of this, the initial ListOptions provided by the consumer
// should include a non-zero Limit parameter.
func FollowContinue(initialOpts *metav1.ListOptions,
listFunc func(metav1.ListOptions) (runtime.Object, error)) error {
opts := initialOpts
for {
list, err := listFunc(*opts)
if err != nil {
return err
}
nextContinueToken, _ := metadataAccessor.Continue(list)
if len(nextContinueToken) == 0 {
return nil
}
opts.Continue = nextContinueToken
}
}
// EnhanceListError augments errors typically returned by List operations with additional context,
// making sure to retain the StatusError type when applicable.
func EnhanceListError(err error, opts metav1.ListOptions, subj string) error {
if apierrors.IsResourceExpired(err) {
return err
}
if apierrors.IsBadRequest(err) || apierrors.IsNotFound(err) {
if se, ok := err.(*apierrors.StatusError); ok {
// modify the message without hiding this is an API error
if len(opts.LabelSelector) == 0 && len(opts.FieldSelector) == 0 {
se.ErrStatus.Message = fmt.Sprintf("Unable to list %q: %v", subj,
se.ErrStatus.Message)
} else {
se.ErrStatus.Message = fmt.Sprintf(
"Unable to find %q that match label selector %q, field selector %q: %v", subj,
opts.LabelSelector,
opts.FieldSelector, se.ErrStatus.Message)
}
return se
}
if len(opts.LabelSelector) == 0 && len(opts.FieldSelector) == 0 {
return fmt.Errorf("Unable to list %q: %v", subj, err)
}
return fmt.Errorf("Unable to find %q that match label selector %q, field selector %q: %v",
subj, opts.LabelSelector, opts.FieldSelector, err)
}
return err
}
func (m *Helper) Watch(namespace, apiVersion string, options *metav1.ListOptions) (watch.Interface, error) {
options.Watch = true
return m.RESTClient.Get().
@@ -146,6 +217,9 @@ func (m *Helper) CreateWithOptions(namespace string, modify bool, obj runtime.Ob
if m.FieldManager != "" {
options.FieldManager = m.FieldManager
}
if m.FieldValidation != "" {
options.FieldValidation = m.FieldValidation
}
if modify {
// Attempt to version the object based on client logic.
version, err := metadataAccessor.ResourceVersion(obj)
@@ -182,10 +256,14 @@ func (m *Helper) Patch(namespace, name string, pt types.PatchType, data []byte,
if m.FieldManager != "" {
options.FieldManager = m.FieldManager
}
if m.FieldValidation != "" {
options.FieldValidation = m.FieldValidation
}
return m.RESTClient.Patch(pt).
NamespaceIfScoped(namespace, m.NamespaceScoped).
Resource(m.Resource).
Name(name).
SubResource(m.Subresource).
VersionedParams(options, metav1.ParameterCodec).
Body(data).
Do(context.TODO()).
@@ -201,6 +279,9 @@ func (m *Helper) Replace(namespace, name string, overwrite bool, obj runtime.Obj
if m.FieldManager != "" {
options.FieldManager = m.FieldManager
}
if m.FieldValidation != "" {
options.FieldValidation = m.FieldValidation
}
// Attempt to version the object based on client logic.
version, err := metadataAccessor.ResourceVersion(obj)
@@ -210,7 +291,7 @@ func (m *Helper) Replace(namespace, name string, overwrite bool, obj runtime.Obj
}
if version == "" && overwrite {
// Retrieve the current version of the object to overwrite the server object
serverObj, err := c.Get().NamespaceIfScoped(namespace, m.NamespaceScoped).Resource(m.Resource).Name(name).Do(context.TODO()).Get()
serverObj, err := c.Get().NamespaceIfScoped(namespace, m.NamespaceScoped).Resource(m.Resource).Name(name).SubResource(m.Subresource).Do(context.TODO()).Get()
if err != nil {
// The object does not exist, but we want it to be created
return m.replaceResource(c, m.Resource, namespace, name, obj, options)
@@ -232,6 +313,7 @@ func (m *Helper) replaceResource(c RESTClient, resource, namespace, name string,
NamespaceIfScoped(namespace, m.NamespaceScoped).
Resource(resource).
Name(name).
SubResource(m.Subresource).
VersionedParams(options, metav1.ParameterCodec).
Body(obj).
Do(context.TODO()).