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

@@ -61,7 +61,7 @@ type Factory interface {
UnstructuredClientForMapping(mapping *meta.RESTMapping) (resource.RESTClient, error)
// Returns a schema that can validate objects stored on disk.
Validator(validate bool) (validation.Schema, error)
Validator(validationDirective string, verifier *resource.QueryParamVerifier) (validation.Schema, error)
// OpenAPISchema returns the parsed openapi schema definition
OpenAPISchema() (openapi.Resources, error)
// OpenAPIGetter returns a getter for the openapi schema document

View File

@@ -24,6 +24,7 @@ import (
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/cli-runtime/pkg/genericclioptions"
"k8s.io/cli-runtime/pkg/resource"
"k8s.io/client-go/discovery"
@@ -141,8 +142,14 @@ func (f *factoryImpl) UnstructuredClientForMapping(mapping *meta.RESTMapping) (r
return restclient.RESTClientFor(cfg)
}
func (f *factoryImpl) Validator(validate bool) (validation.Schema, error) {
if !validate {
func (f *factoryImpl) Validator(validationDirective string, verifier *resource.QueryParamVerifier) (validation.Schema, error) {
// client-side schema validation is only performed
// when the validationDirective is strict.
// If the directive is warn, we rely on the ParamVerifyingSchema
// to ignore the client-side validation and provide a warning
// to the user that attempting warn validation when SS validation
// is unsupported is inert.
if validationDirective == metav1.FieldValidationIgnore {
return validation.NullSchema{}, nil
}
@@ -151,10 +158,11 @@ func (f *factoryImpl) Validator(validate bool) (validation.Schema, error) {
return nil, err
}
return validation.ConjunctiveSchema{
schema := validation.ConjunctiveSchema{
openapivalidation.NewSchemaValidation(resources),
validation.NoDoubleKeySchema{},
}, nil
}
return validation.NewParamVerifyingSchema(schema, verifier, string(validationDirective)), nil
}
// OpenAPISchema returns metadata and structural information about

View File

@@ -30,12 +30,14 @@ import (
jsonpatch "github.com/evanphx/json-patch"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
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/runtime"
utilerrors "k8s.io/apimachinery/pkg/util/errors"
"k8s.io/apimachinery/pkg/util/sets"
"k8s.io/apimachinery/pkg/util/strategicpatch"
"k8s.io/apimachinery/pkg/util/yaml"
"k8s.io/cli-runtime/pkg/genericclioptions"
"k8s.io/cli-runtime/pkg/resource"
@@ -50,6 +52,7 @@ import (
const (
ApplyAnnotationsFlag = "save-config"
DefaultErrorExitCode = 1
DefaultChunkSize = 500
)
type debugError interface {
@@ -86,10 +89,13 @@ func DefaultBehaviorOnFatal() {
fatalErrHandler = fatal
}
// fatal prints the message (if provided) and then exits. If V(6) or greater,
// klog.Fatal is invoked for extended information.
// fatal prints the message (if provided) and then exits. If V(99) or greater,
// klog.Fatal is invoked for extended information. This is intended for maintainer
// debugging and out of a reasonable range for users.
func fatal(msg string, code int) {
if klog.V(6).Enabled() {
// nolint:logcheck // Not using the result of klog.V(99) inside the if
// branch is okay, we just use it to determine how to terminate.
if klog.V(99).Enabled() {
klog.FatalDepth(2, msg)
}
if len(msg) > 0 {
@@ -127,6 +133,20 @@ func CheckDiffErr(err error) {
})
}
// isInvalidReasonStatusError returns true if this is an API Status error with reason=Invalid.
// This is distinct from generic 422 errors we want to fall back to generic error handling.
func isInvalidReasonStatusError(err error) bool {
if !apierrors.IsInvalid(err) {
return false
}
statusError, isStatusError := err.(*apierrors.StatusError)
if !isStatusError {
return false
}
status := statusError.Status()
return status.Reason == metav1.StatusReasonInvalid
}
// checkErr formats a given error as a string and calls the passed handleErr
// func with that string and an kubectl exit code.
func checkErr(err error, handleErr func(string, int)) {
@@ -142,16 +162,26 @@ func checkErr(err error, handleErr func(string, int)) {
switch {
case err == ErrExit:
handleErr("", DefaultErrorExitCode)
case apierrors.IsInvalid(err):
details := err.(*apierrors.StatusError).Status().Details
case isInvalidReasonStatusError(err):
status := err.(*apierrors.StatusError).Status()
details := status.Details
s := "The request is invalid"
if details == nil {
// if we have no other details, include the message from the server if present
if len(status.Message) > 0 {
s += ": " + status.Message
}
handleErr(s, DefaultErrorExitCode)
return
}
if len(details.Kind) != 0 || len(details.Name) != 0 {
s = fmt.Sprintf("The %s %q is invalid", details.Kind, details.Name)
} else if len(status.Message) > 0 && len(details.Causes) == 0 {
// only append the message if we have no kind/name details and no causes,
// since default invalid error constructors duplicate that information in the message
s += ": " + status.Message
}
if len(details.Causes) > 0 {
errs := statusCausesToAggrError(details.Causes)
handleErr(MultilineError(s+": ", errs), DefaultErrorExitCode)
@@ -393,11 +423,16 @@ func GetPodRunningTimeoutFlag(cmd *cobra.Command) (time.Duration, error) {
}
func AddValidateFlags(cmd *cobra.Command) {
cmd.Flags().Bool("validate", true, "If true, use a schema to validate the input before sending it")
}
cmd.Flags().String(
"validate",
"strict",
`Must be one of: strict (or true), warn, ignore (or false).
"true" or "strict" will use a schema to validate the input and fail the request if invalid. It will perform server side validation if ServerSideFieldValidation is enabled on the api-server, but will fall back to less reliable client-side validation if not.
"warn" will warn about unknown or duplicate fields without blocking the request if server-side field validation is enabled on the API server, and behave as "ignore" otherwise.
"false" or "ignore" will not perform any schema validation, silently dropping any unknown or duplicate fields.`,
)
func AddValidateOptionFlags(cmd *cobra.Command, options *ValidateOptions) {
cmd.Flags().BoolVar(&options.EnableValidation, "validate", options.EnableValidation, "If true, use a schema to validate the input before sending it")
cmd.Flags().Lookup("validate").NoOptDefVal = "strict"
}
func AddFilenameOptionFlags(cmd *cobra.Command, options *resource.FilenameOptions, usage string) {
@@ -455,19 +490,28 @@ func AddApplyAnnotationVarFlags(cmd *cobra.Command, applyAnnotation *bool) {
cmd.Flags().BoolVar(applyAnnotation, ApplyAnnotationsFlag, *applyAnnotation, "If true, the configuration of current object will be saved in its annotation. Otherwise, the annotation will be unchanged. This flag is useful when you want to perform kubectl apply on this object in the future.")
}
// AddGeneratorFlags adds flags common to resource generation commands
// TODO: need to take a pass at other generator commands to use this set of flags
func AddGeneratorFlags(cmd *cobra.Command, defaultGenerator string) {
cmd.Flags().String("generator", defaultGenerator, "The name of the API generator to use.")
cmd.Flags().MarkDeprecated("generator", "has no effect and will be removed in the future.")
AddDryRunFlag(cmd)
func AddChunkSizeFlag(cmd *cobra.Command, value *int64) {
cmd.Flags().Int64Var(value, "chunk-size", *value,
"Return large lists in chunks rather than all at once. Pass 0 to disable. This flag is beta and may change in the future.")
}
func AddLabelSelectorFlagVar(cmd *cobra.Command, p *string) {
cmd.Flags().StringVarP(p, "selector", "l", *p, "Selector (label query) to filter on, supports '=', '==', and '!='.(e.g. -l key1=value1,key2=value2). Matching objects must satisfy all of the specified label constraints.")
}
func AddSubresourceFlags(cmd *cobra.Command, subresource *string, usage string, allowedSubresources ...string) {
cmd.Flags().StringVar(subresource, "subresource", "", fmt.Sprintf("%s Must be one of %v. This flag is alpha and may change in the future.", usage, allowedSubresources))
CheckErr(cmd.RegisterFlagCompletionFunc("subresource", func(*cobra.Command, []string, string) ([]string, cobra.ShellCompDirective) {
return allowedSubresources, cobra.ShellCompDirectiveNoFileComp
}))
}
type ValidateOptions struct {
EnableValidation bool
ValidationDirective string
}
// Merge requires JSON serialization
// Merge converts the passed in object to JSON, merges the fragment into it using an RFC7396 JSON Merge Patch,
// and returns the resulting object
// TODO: merge assumes JSON serialization, and does not properly abstract API retrieval
func Merge(codec runtime.Codec, dst runtime.Object, fragment string) (runtime.Object, error) {
// encode dst into versioned json and apply fragment directly too it
@@ -486,6 +530,46 @@ func Merge(codec runtime.Codec, dst runtime.Object, fragment string) (runtime.Ob
return out, nil
}
// StrategicMerge converts the passed in object to JSON, merges the fragment into it using a Strategic Merge Patch,
// and returns the resulting object
func StrategicMerge(codec runtime.Codec, dst runtime.Object, fragment string, dataStruct runtime.Object) (runtime.Object, error) {
target, err := runtime.Encode(codec, dst)
if err != nil {
return nil, err
}
patched, err := strategicpatch.StrategicMergePatch(target, []byte(fragment), dataStruct)
if err != nil {
return nil, err
}
out, err := runtime.Decode(codec, patched)
if err != nil {
return nil, err
}
return out, nil
}
// JSONPatch converts the passed in object to JSON, performs an RFC6902 JSON Patch using operations specified in the
// fragment, and returns the resulting object
func JSONPatch(codec runtime.Codec, dst runtime.Object, fragment string) (runtime.Object, error) {
target, err := runtime.Encode(codec, dst)
if err != nil {
return nil, err
}
patch, err := jsonpatch.DecodePatch([]byte(fragment))
if err != nil {
return nil, err
}
patched, err := patch.Apply(target)
if err != nil {
return nil, err
}
out, err := runtime.Decode(codec, patched)
if err != nil {
return nil, err
}
return out, nil
}
// DumpReaderToFile writes all data from the given io.Reader to the specified file
// (usually for temporary use).
func DumpReaderToFile(reader io.Reader, filename string) error {
@@ -524,6 +608,28 @@ func GetFieldManagerFlag(cmd *cobra.Command) string {
return GetFlagString(cmd, "field-manager")
}
func GetValidationDirective(cmd *cobra.Command) (string, error) {
var validateFlag = GetFlagString(cmd, "validate")
b, err := strconv.ParseBool(validateFlag)
if err != nil {
switch validateFlag {
case "strict":
return metav1.FieldValidationStrict, nil
case "warn":
return metav1.FieldValidationWarn, nil
case "ignore":
return metav1.FieldValidationIgnore, nil
default:
return metav1.FieldValidationStrict, fmt.Errorf(`invalid - validate option %q; must be one of: strict (or true), warn, ignore (or false)`, validateFlag)
}
}
// The flag was a boolean
if b {
return metav1.FieldValidationStrict, nil
}
return metav1.FieldValidationIgnore, nil
}
type DryRunStrategy int
const (
@@ -666,7 +772,8 @@ func IsSiblingCommandExists(cmd *cobra.Command, targetCmdName string) bool {
// arguments (sub-commands) are provided, or a usage error otherwise.
func DefaultSubCommandRun(out io.Writer) func(c *cobra.Command, args []string) {
return func(c *cobra.Command, args []string) {
c.SetOutput(out)
c.SetOut(out)
c.SetErr(out)
RequireNoArguments(c, args)
c.Help()
CheckErr(ErrExit)
@@ -697,7 +804,9 @@ func ManualStrip(file []byte) []byte {
stripped := []byte{}
lines := bytes.Split(file, []byte("\n"))
for i, line := range lines {
if bytes.HasPrefix(bytes.TrimSpace(line), []byte("#")) {
trimline := bytes.TrimSpace(line)
if bytes.HasPrefix(trimline, []byte("#")) && !bytes.HasPrefix(trimline, []byte("#!")) {
continue
}
stripped = append(stripped, line...)
@@ -748,3 +857,18 @@ func Warning(cmdErr io.Writer, newGeneratorName, oldGeneratorName string) {
oldGeneratorName,
)
}
// Difference removes any elements of subArray from fullArray and returns the result
func Difference(fullArray []string, subArray []string) []string {
exclude := make(map[string]bool, len(subArray))
for _, elem := range subArray {
exclude[elem] = true
}
var result []string
for _, elem := range fullArray {
if _, found := exclude[elem]; !found {
result = append(result, elem)
}
}
return result
}

90
vendor/k8s.io/kubectl/pkg/cmd/util/override_options.go generated vendored Normal file
View File

@@ -0,0 +1,90 @@
/*
Copyright 2021 The Kubernetes Authors.
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 util
import (
"fmt"
"github.com/spf13/cobra"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/kubectl/pkg/scheme"
"k8s.io/kubectl/pkg/util/i18n"
)
type OverrideType string
const (
// OverrideTypeJSON will use an RFC6902 JSON Patch to alter the generated output
OverrideTypeJSON OverrideType = "json"
// OverrideTypeMerge will use an RFC7396 JSON Merge Patch to alter the generated output
OverrideTypeMerge OverrideType = "merge"
// OverrideTypeStrategic will use a Strategic Merge Patch to alter the generated output
OverrideTypeStrategic OverrideType = "strategic"
)
const DefaultOverrideType = OverrideTypeMerge
type OverrideOptions struct {
Overrides string
OverrideType OverrideType
}
func (o *OverrideOptions) AddOverrideFlags(cmd *cobra.Command) {
cmd.Flags().StringVar(&o.Overrides, "overrides", "", i18n.T("An inline JSON override for the generated object. If this is non-empty, it is used to override the generated object. Requires that the object supply a valid apiVersion field."))
cmd.Flags().StringVar((*string)(&o.OverrideType), "override-type", string(DefaultOverrideType), fmt.Sprintf("The method used to override the generated object: %s, %s, or %s.", OverrideTypeJSON, OverrideTypeMerge, OverrideTypeStrategic))
}
func (o *OverrideOptions) NewOverrider(dataStruct runtime.Object) *Overrider {
return &Overrider{
Options: o,
DataStruct: dataStruct,
}
}
type Overrider struct {
Options *OverrideOptions
DataStruct runtime.Object
}
func (o *Overrider) Apply(obj runtime.Object) (runtime.Object, error) {
if len(o.Options.Overrides) == 0 {
return obj, nil
}
codec := runtime.NewCodec(scheme.DefaultJSONEncoder(), scheme.Codecs.UniversalDecoder(scheme.Scheme.PrioritizedVersionsAllGroups()...))
var overrideType OverrideType
if len(o.Options.OverrideType) == 0 {
overrideType = DefaultOverrideType
} else {
overrideType = o.Options.OverrideType
}
switch overrideType {
case OverrideTypeJSON:
return JSONPatch(codec, obj, o.Options.Overrides)
case OverrideTypeMerge:
return Merge(codec, obj, o.Options.Overrides)
case OverrideTypeStrategic:
return StrategicMerge(codec, obj, o.Options.Overrides, o.DataStruct)
default:
return nil, fmt.Errorf("invalid override type: %v", overrideType)
}
}