Add generated file
This PR adds generated files under pkg/client and vendor folder.
This commit is contained in:
95
vendor/k8s.io/kubernetes/pkg/kubectl/cmd/util/BUILD
generated
vendored
Normal file
95
vendor/k8s.io/kubernetes/pkg/kubectl/cmd/util/BUILD
generated
vendored
Normal file
@@ -0,0 +1,95 @@
|
||||
load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test")
|
||||
|
||||
go_library(
|
||||
name = "go_default_library",
|
||||
srcs = [
|
||||
"conversion.go",
|
||||
"factory.go",
|
||||
"factory_client_access.go",
|
||||
"generator.go",
|
||||
"helpers.go",
|
||||
"kubectl_match_version.go",
|
||||
"printing.go",
|
||||
],
|
||||
importpath = "k8s.io/kubernetes/pkg/kubectl/cmd/util",
|
||||
visibility = ["//build/visible_to:pkg_kubectl_cmd_util_CONSUMERS"],
|
||||
deps = [
|
||||
"//pkg/api/legacyscheme:go_default_library",
|
||||
"//pkg/apis/core:go_default_library",
|
||||
"//pkg/client/clientset_generated/internalclientset:go_default_library",
|
||||
"//pkg/kubectl:go_default_library",
|
||||
"//pkg/kubectl/cmd/templates:go_default_library",
|
||||
"//pkg/kubectl/cmd/util/openapi:go_default_library",
|
||||
"//pkg/kubectl/cmd/util/openapi/validation:go_default_library",
|
||||
"//pkg/kubectl/genericclioptions:go_default_library",
|
||||
"//pkg/kubectl/genericclioptions/resource:go_default_library",
|
||||
"//pkg/kubectl/validation:go_default_library",
|
||||
"//pkg/printers:go_default_library",
|
||||
"//pkg/printers/internalversion:go_default_library",
|
||||
"//pkg/version:go_default_library",
|
||||
"//vendor/github.com/evanphx/json-patch:go_default_library",
|
||||
"//vendor/github.com/golang/glog:go_default_library",
|
||||
"//vendor/github.com/spf13/cobra:go_default_library",
|
||||
"//vendor/github.com/spf13/pflag:go_default_library",
|
||||
"//vendor/k8s.io/api/apps/v1:go_default_library",
|
||||
"//vendor/k8s.io/api/apps/v1beta1:go_default_library",
|
||||
"//vendor/k8s.io/api/batch/v1:go_default_library",
|
||||
"//vendor/k8s.io/api/batch/v1beta1:go_default_library",
|
||||
"//vendor/k8s.io/api/batch/v2alpha1:go_default_library",
|
||||
"//vendor/k8s.io/api/extensions/v1beta1:go_default_library",
|
||||
"//vendor/k8s.io/apimachinery/pkg/api/errors:go_default_library",
|
||||
"//vendor/k8s.io/apimachinery/pkg/api/meta:go_default_library",
|
||||
"//vendor/k8s.io/apimachinery/pkg/apis/meta/v1:go_default_library",
|
||||
"//vendor/k8s.io/apimachinery/pkg/apis/meta/v1/unstructured:go_default_library",
|
||||
"//vendor/k8s.io/apimachinery/pkg/runtime:go_default_library",
|
||||
"//vendor/k8s.io/apimachinery/pkg/runtime/schema:go_default_library",
|
||||
"//vendor/k8s.io/apimachinery/pkg/util/errors:go_default_library",
|
||||
"//vendor/k8s.io/apimachinery/pkg/util/sets:go_default_library",
|
||||
"//vendor/k8s.io/apimachinery/pkg/util/yaml:go_default_library",
|
||||
"//vendor/k8s.io/client-go/discovery:go_default_library",
|
||||
"//vendor/k8s.io/client-go/dynamic:go_default_library",
|
||||
"//vendor/k8s.io/client-go/kubernetes:go_default_library",
|
||||
"//vendor/k8s.io/client-go/rest:go_default_library",
|
||||
"//vendor/k8s.io/client-go/scale:go_default_library",
|
||||
"//vendor/k8s.io/client-go/tools/clientcmd:go_default_library",
|
||||
"//vendor/k8s.io/utils/exec:go_default_library",
|
||||
],
|
||||
)
|
||||
|
||||
go_test(
|
||||
name = "go_default_test",
|
||||
srcs = ["helpers_test.go"],
|
||||
embed = [":go_default_library"],
|
||||
deps = [
|
||||
"//pkg/api/testapi:go_default_library",
|
||||
"//pkg/api/testing:go_default_library",
|
||||
"//pkg/apis/core:go_default_library",
|
||||
"//vendor/k8s.io/apimachinery/pkg/api/equality:go_default_library",
|
||||
"//vendor/k8s.io/apimachinery/pkg/api/errors:go_default_library",
|
||||
"//vendor/k8s.io/apimachinery/pkg/api/meta:go_default_library",
|
||||
"//vendor/k8s.io/apimachinery/pkg/apis/meta/v1:go_default_library",
|
||||
"//vendor/k8s.io/apimachinery/pkg/runtime:go_default_library",
|
||||
"//vendor/k8s.io/apimachinery/pkg/runtime/schema:go_default_library",
|
||||
"//vendor/k8s.io/apimachinery/pkg/util/validation/field:go_default_library",
|
||||
"//vendor/k8s.io/utils/exec:go_default_library",
|
||||
],
|
||||
)
|
||||
|
||||
filegroup(
|
||||
name = "package-srcs",
|
||||
srcs = glob(["**"]),
|
||||
tags = ["automanaged"],
|
||||
visibility = ["//visibility:private"],
|
||||
)
|
||||
|
||||
filegroup(
|
||||
name = "all-srcs",
|
||||
srcs = [
|
||||
":package-srcs",
|
||||
"//pkg/kubectl/cmd/util/editor:all-srcs",
|
||||
"//pkg/kubectl/cmd/util/openapi:all-srcs",
|
||||
"//pkg/kubectl/cmd/util/sanity:all-srcs",
|
||||
],
|
||||
tags = ["automanaged"],
|
||||
visibility = ["//build/visible_to:pkg_kubectl_cmd_util_CONSUMERS"],
|
||||
)
|
40
vendor/k8s.io/kubernetes/pkg/kubectl/cmd/util/conversion.go
generated
vendored
Normal file
40
vendor/k8s.io/kubernetes/pkg/kubectl/cmd/util/conversion.go
generated
vendored
Normal file
@@ -0,0 +1,40 @@
|
||||
/*
|
||||
Copyright 2018 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 (
|
||||
"k8s.io/apimachinery/pkg/api/meta"
|
||||
"k8s.io/apimachinery/pkg/runtime"
|
||||
"k8s.io/apimachinery/pkg/runtime/schema"
|
||||
"k8s.io/kubernetes/pkg/api/legacyscheme"
|
||||
)
|
||||
|
||||
// AsDefaultVersionedOrOriginal returns the object as a Go object in the external form if possible (matching the
|
||||
// group version kind of the mapping if provided, a best guess based on serialization if not provided, or obj if it cannot be converted.
|
||||
// TODO update call sites to specify the scheme they want on their builder.
|
||||
func AsDefaultVersionedOrOriginal(obj runtime.Object, mapping *meta.RESTMapping) runtime.Object {
|
||||
converter := runtime.ObjectConvertor(legacyscheme.Scheme)
|
||||
groupVersioner := runtime.GroupVersioner(schema.GroupVersions(legacyscheme.Scheme.PrioritizedVersionsAllGroups()))
|
||||
if mapping != nil {
|
||||
groupVersioner = mapping.GroupVersionKind.GroupVersion()
|
||||
}
|
||||
|
||||
if obj, err := converter.ConvertToVersion(obj, groupVersioner); err == nil {
|
||||
return obj
|
||||
}
|
||||
return obj
|
||||
}
|
68
vendor/k8s.io/kubernetes/pkg/kubectl/cmd/util/editor/BUILD
generated
vendored
Normal file
68
vendor/k8s.io/kubernetes/pkg/kubectl/cmd/util/editor/BUILD
generated
vendored
Normal file
@@ -0,0 +1,68 @@
|
||||
load(
|
||||
"@io_bazel_rules_go//go:def.bzl",
|
||||
"go_library",
|
||||
"go_test",
|
||||
)
|
||||
|
||||
go_library(
|
||||
name = "go_default_library",
|
||||
srcs = [
|
||||
"editoptions.go",
|
||||
"editor.go",
|
||||
],
|
||||
importpath = "k8s.io/kubernetes/pkg/kubectl/cmd/util/editor",
|
||||
visibility = [
|
||||
"//build/visible_to:pkg_kubectl_cmd_util_editor_CONSUMERS",
|
||||
],
|
||||
deps = [
|
||||
"//pkg/apis/core:go_default_library",
|
||||
"//pkg/kubectl:go_default_library",
|
||||
"//pkg/kubectl/cmd/util:go_default_library",
|
||||
"//pkg/kubectl/cmd/util/editor/crlf:go_default_library",
|
||||
"//pkg/kubectl/genericclioptions:go_default_library",
|
||||
"//pkg/kubectl/genericclioptions/printers:go_default_library",
|
||||
"//pkg/kubectl/genericclioptions/resource:go_default_library",
|
||||
"//pkg/kubectl/scheme:go_default_library",
|
||||
"//pkg/kubectl/util/term:go_default_library",
|
||||
"//vendor/github.com/evanphx/json-patch:go_default_library",
|
||||
"//vendor/github.com/golang/glog:go_default_library",
|
||||
"//vendor/github.com/spf13/cobra:go_default_library",
|
||||
"//vendor/k8s.io/apimachinery/pkg/api/errors:go_default_library",
|
||||
"//vendor/k8s.io/apimachinery/pkg/api/meta:go_default_library",
|
||||
"//vendor/k8s.io/apimachinery/pkg/apis/meta/v1/unstructured:go_default_library",
|
||||
"//vendor/k8s.io/apimachinery/pkg/runtime:go_default_library",
|
||||
"//vendor/k8s.io/apimachinery/pkg/runtime/schema:go_default_library",
|
||||
"//vendor/k8s.io/apimachinery/pkg/types:go_default_library",
|
||||
"//vendor/k8s.io/apimachinery/pkg/util/mergepatch:go_default_library",
|
||||
"//vendor/k8s.io/apimachinery/pkg/util/strategicpatch:go_default_library",
|
||||
"//vendor/k8s.io/apimachinery/pkg/util/validation/field:go_default_library",
|
||||
"//vendor/k8s.io/apimachinery/pkg/util/yaml:go_default_library",
|
||||
],
|
||||
)
|
||||
|
||||
go_test(
|
||||
name = "go_default_test",
|
||||
srcs = [
|
||||
"editoptions_test.go",
|
||||
"editor_test.go",
|
||||
],
|
||||
embed = [":go_default_library"],
|
||||
)
|
||||
|
||||
filegroup(
|
||||
name = "package-srcs",
|
||||
srcs = glob(["**"]),
|
||||
tags = ["automanaged"],
|
||||
)
|
||||
|
||||
filegroup(
|
||||
name = "all-srcs",
|
||||
srcs = [
|
||||
":package-srcs",
|
||||
"//pkg/kubectl/cmd/util/editor/crlf:all-srcs",
|
||||
],
|
||||
tags = ["automanaged"],
|
||||
visibility = [
|
||||
"//build/visible_to:pkg_kubectl_cmd_util_editor_CONSUMERS",
|
||||
],
|
||||
)
|
25
vendor/k8s.io/kubernetes/pkg/kubectl/cmd/util/editor/crlf/BUILD
generated
vendored
Normal file
25
vendor/k8s.io/kubernetes/pkg/kubectl/cmd/util/editor/crlf/BUILD
generated
vendored
Normal file
@@ -0,0 +1,25 @@
|
||||
package(default_visibility = ["//visibility:public"])
|
||||
|
||||
load(
|
||||
"@io_bazel_rules_go//go:def.bzl",
|
||||
"go_library",
|
||||
)
|
||||
|
||||
go_library(
|
||||
name = "go_default_library",
|
||||
srcs = ["crlf.go"],
|
||||
importpath = "k8s.io/kubernetes/pkg/kubectl/cmd/util/editor/crlf",
|
||||
)
|
||||
|
||||
filegroup(
|
||||
name = "package-srcs",
|
||||
srcs = glob(["**"]),
|
||||
tags = ["automanaged"],
|
||||
visibility = ["//visibility:private"],
|
||||
)
|
||||
|
||||
filegroup(
|
||||
name = "all-srcs",
|
||||
srcs = [":package-srcs"],
|
||||
tags = ["automanaged"],
|
||||
)
|
57
vendor/k8s.io/kubernetes/pkg/kubectl/cmd/util/editor/crlf/crlf.go
generated
vendored
Normal file
57
vendor/k8s.io/kubernetes/pkg/kubectl/cmd/util/editor/crlf/crlf.go
generated
vendored
Normal file
@@ -0,0 +1,57 @@
|
||||
/*
|
||||
Copyright 2015 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 crlf
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"io"
|
||||
)
|
||||
|
||||
type crlfWriter struct {
|
||||
io.Writer
|
||||
}
|
||||
|
||||
// NewCRLFWriter implements a CR/LF line ending writer used for normalizing
|
||||
// text for Windows platforms.
|
||||
func NewCRLFWriter(w io.Writer) io.Writer {
|
||||
return crlfWriter{w}
|
||||
}
|
||||
|
||||
func (w crlfWriter) Write(b []byte) (n int, err error) {
|
||||
for i, written := 0, 0; ; {
|
||||
next := bytes.Index(b[i:], []byte("\n"))
|
||||
if next == -1 {
|
||||
n, err := w.Writer.Write(b[i:])
|
||||
return written + n, err
|
||||
}
|
||||
next = next + i
|
||||
n, err := w.Writer.Write(b[i:next])
|
||||
if err != nil {
|
||||
return written + n, err
|
||||
}
|
||||
written += n
|
||||
n, err = w.Writer.Write([]byte("\r\n"))
|
||||
if err != nil {
|
||||
if n > 1 {
|
||||
n = 1
|
||||
}
|
||||
return written + n, err
|
||||
}
|
||||
written++
|
||||
i = next + 1
|
||||
}
|
||||
}
|
816
vendor/k8s.io/kubernetes/pkg/kubectl/cmd/util/editor/editoptions.go
generated
vendored
Normal file
816
vendor/k8s.io/kubernetes/pkg/kubectl/cmd/util/editor/editoptions.go
generated
vendored
Normal file
@@ -0,0 +1,816 @@
|
||||
/*
|
||||
Copyright 2017 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 editor
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"bytes"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"reflect"
|
||||
goruntime "runtime"
|
||||
"strings"
|
||||
|
||||
"github.com/evanphx/json-patch"
|
||||
"github.com/golang/glog"
|
||||
"github.com/spf13/cobra"
|
||||
|
||||
apierrors "k8s.io/apimachinery/pkg/api/errors"
|
||||
"k8s.io/apimachinery/pkg/api/meta"
|
||||
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
|
||||
"k8s.io/apimachinery/pkg/runtime"
|
||||
"k8s.io/apimachinery/pkg/runtime/schema"
|
||||
"k8s.io/apimachinery/pkg/types"
|
||||
"k8s.io/apimachinery/pkg/util/mergepatch"
|
||||
"k8s.io/apimachinery/pkg/util/strategicpatch"
|
||||
"k8s.io/apimachinery/pkg/util/validation/field"
|
||||
"k8s.io/apimachinery/pkg/util/yaml"
|
||||
api "k8s.io/kubernetes/pkg/apis/core"
|
||||
"k8s.io/kubernetes/pkg/kubectl"
|
||||
cmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util"
|
||||
"k8s.io/kubernetes/pkg/kubectl/cmd/util/editor/crlf"
|
||||
"k8s.io/kubernetes/pkg/kubectl/genericclioptions"
|
||||
"k8s.io/kubernetes/pkg/kubectl/genericclioptions/printers"
|
||||
"k8s.io/kubernetes/pkg/kubectl/genericclioptions/resource"
|
||||
"k8s.io/kubernetes/pkg/kubectl/scheme"
|
||||
)
|
||||
|
||||
// EditOptions contains all the options for running edit cli command.
|
||||
type EditOptions struct {
|
||||
resource.FilenameOptions
|
||||
RecordFlags *genericclioptions.RecordFlags
|
||||
|
||||
PrintFlags *genericclioptions.PrintFlags
|
||||
ToPrinter func(string) (printers.ResourcePrinter, error)
|
||||
|
||||
Output string
|
||||
OutputPatch bool
|
||||
WindowsLineEndings bool
|
||||
|
||||
cmdutil.ValidateOptions
|
||||
|
||||
OriginalResult *resource.Result
|
||||
|
||||
EditMode EditMode
|
||||
|
||||
CmdNamespace string
|
||||
ApplyAnnotation bool
|
||||
ChangeCause string
|
||||
|
||||
genericclioptions.IOStreams
|
||||
|
||||
Recorder genericclioptions.Recorder
|
||||
f cmdutil.Factory
|
||||
editPrinterOptions *editPrinterOptions
|
||||
updatedResultGetter func(data []byte) *resource.Result
|
||||
}
|
||||
|
||||
func NewEditOptions(editMode EditMode, ioStreams genericclioptions.IOStreams) *EditOptions {
|
||||
return &EditOptions{
|
||||
RecordFlags: genericclioptions.NewRecordFlags(),
|
||||
|
||||
EditMode: editMode,
|
||||
|
||||
PrintFlags: genericclioptions.NewPrintFlags("edited").WithTypeSetter(scheme.Scheme),
|
||||
|
||||
WindowsLineEndings: goruntime.GOOS == "windows",
|
||||
|
||||
Recorder: genericclioptions.NoopRecorder{},
|
||||
|
||||
IOStreams: ioStreams,
|
||||
Output: "yaml",
|
||||
}
|
||||
}
|
||||
|
||||
type editPrinterOptions struct {
|
||||
printer printers.ResourcePrinter
|
||||
ext string
|
||||
addHeader bool
|
||||
}
|
||||
|
||||
// Complete completes all the required options
|
||||
func (o *EditOptions) Complete(f cmdutil.Factory, args []string, cmd *cobra.Command) error {
|
||||
var err error
|
||||
|
||||
o.RecordFlags.Complete(cmd)
|
||||
o.Recorder, err = o.RecordFlags.ToRecorder()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if o.EditMode != NormalEditMode && o.EditMode != EditBeforeCreateMode && o.EditMode != ApplyEditMode {
|
||||
return fmt.Errorf("unsupported edit mode %q", o.EditMode)
|
||||
}
|
||||
if o.Output != "" {
|
||||
if o.Output != "yaml" && o.Output != "json" {
|
||||
return fmt.Errorf("invalid output format %s, only yaml|json supported", o.Output)
|
||||
}
|
||||
}
|
||||
o.editPrinterOptions = getPrinter(o.Output)
|
||||
|
||||
if o.OutputPatch && o.EditMode != NormalEditMode {
|
||||
return fmt.Errorf("the edit mode doesn't support output the patch")
|
||||
}
|
||||
|
||||
cmdNamespace, enforceNamespace, err := f.ToRawKubeConfigLoader().Namespace()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
b := f.NewBuilder().
|
||||
Unstructured()
|
||||
if o.EditMode == NormalEditMode || o.EditMode == ApplyEditMode {
|
||||
// when do normal edit or apply edit we need to always retrieve the latest resource from server
|
||||
b = b.ResourceTypeOrNameArgs(true, args...).Latest()
|
||||
}
|
||||
includeUninitialized := cmdutil.ShouldIncludeUninitialized(cmd, false)
|
||||
r := b.NamespaceParam(cmdNamespace).DefaultNamespace().
|
||||
FilenameParam(enforceNamespace, &o.FilenameOptions).
|
||||
IncludeUninitialized(includeUninitialized).
|
||||
ContinueOnError().
|
||||
Flatten().
|
||||
Do()
|
||||
err = r.Err()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
o.OriginalResult = r
|
||||
|
||||
o.updatedResultGetter = func(data []byte) *resource.Result {
|
||||
// resource builder to read objects from edited data
|
||||
return f.NewBuilder().
|
||||
Unstructured().
|
||||
Stream(bytes.NewReader(data), "edited-file").
|
||||
IncludeUninitialized(includeUninitialized).
|
||||
ContinueOnError().
|
||||
Flatten().
|
||||
Do()
|
||||
}
|
||||
|
||||
o.ToPrinter = func(operation string) (printers.ResourcePrinter, error) {
|
||||
o.PrintFlags.NamePrintFlags.Operation = operation
|
||||
return o.PrintFlags.ToPrinter()
|
||||
}
|
||||
|
||||
o.CmdNamespace = cmdNamespace
|
||||
o.f = f
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// Validate checks the EditOptions to see if there is sufficient information to run the command.
|
||||
func (o *EditOptions) Validate() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (o *EditOptions) Run() error {
|
||||
edit := NewDefaultEditor(editorEnvs())
|
||||
// editFn is invoked for each edit session (once with a list for normal edit, once for each individual resource in a edit-on-create invocation)
|
||||
editFn := func(infos []*resource.Info) error {
|
||||
var (
|
||||
results = editResults{}
|
||||
original = []byte{}
|
||||
edited = []byte{}
|
||||
file string
|
||||
err error
|
||||
)
|
||||
|
||||
containsError := false
|
||||
// loop until we succeed or cancel editing
|
||||
for {
|
||||
// get the object we're going to serialize as input to the editor
|
||||
var originalObj runtime.Object
|
||||
switch len(infos) {
|
||||
case 1:
|
||||
originalObj = infos[0].Object
|
||||
default:
|
||||
l := &unstructured.UnstructuredList{
|
||||
Object: map[string]interface{}{
|
||||
"kind": "List",
|
||||
"apiVersion": "v1",
|
||||
"metadata": map[string]interface{}{},
|
||||
},
|
||||
}
|
||||
for _, info := range infos {
|
||||
l.Items = append(l.Items, *info.Object.(*unstructured.Unstructured))
|
||||
}
|
||||
originalObj = l
|
||||
}
|
||||
|
||||
// generate the file to edit
|
||||
buf := &bytes.Buffer{}
|
||||
var w io.Writer = buf
|
||||
if o.WindowsLineEndings {
|
||||
w = crlf.NewCRLFWriter(w)
|
||||
}
|
||||
|
||||
if o.editPrinterOptions.addHeader {
|
||||
results.header.writeTo(w, o.EditMode)
|
||||
}
|
||||
|
||||
if !containsError {
|
||||
if err := o.editPrinterOptions.printer.PrintObj(originalObj, w); err != nil {
|
||||
return preservedFile(err, results.file, o.ErrOut)
|
||||
}
|
||||
original = buf.Bytes()
|
||||
} else {
|
||||
// In case of an error, preserve the edited file.
|
||||
// Remove the comments (header) from it since we already
|
||||
// have included the latest header in the buffer above.
|
||||
buf.Write(cmdutil.ManualStrip(edited))
|
||||
}
|
||||
|
||||
// launch the editor
|
||||
editedDiff := edited
|
||||
edited, file, err = edit.LaunchTempFile(fmt.Sprintf("%s-edit-", filepath.Base(os.Args[0])), o.editPrinterOptions.ext, buf)
|
||||
if err != nil {
|
||||
return preservedFile(err, results.file, o.ErrOut)
|
||||
}
|
||||
// If we're retrying the loop because of an error, and no change was made in the file, short-circuit
|
||||
if containsError && bytes.Equal(cmdutil.StripComments(editedDiff), cmdutil.StripComments(edited)) {
|
||||
return preservedFile(fmt.Errorf("%s", "Edit cancelled, no valid changes were saved."), file, o.ErrOut)
|
||||
}
|
||||
// cleanup any file from the previous pass
|
||||
if len(results.file) > 0 {
|
||||
os.Remove(results.file)
|
||||
}
|
||||
glog.V(4).Infof("User edited:\n%s", string(edited))
|
||||
|
||||
// Apply validation
|
||||
schema, err := o.f.Validator(o.EnableValidation)
|
||||
if err != nil {
|
||||
return preservedFile(err, file, o.ErrOut)
|
||||
}
|
||||
err = schema.ValidateBytes(cmdutil.StripComments(edited))
|
||||
if err != nil {
|
||||
results = editResults{
|
||||
file: file,
|
||||
}
|
||||
containsError = true
|
||||
fmt.Fprintln(o.ErrOut, results.addError(apierrors.NewInvalid(api.Kind(""), "", field.ErrorList{field.Invalid(nil, "The edited file failed validation", fmt.Sprintf("%v", err))}), infos[0]))
|
||||
continue
|
||||
}
|
||||
|
||||
// Compare content without comments
|
||||
if bytes.Equal(cmdutil.StripComments(original), cmdutil.StripComments(edited)) {
|
||||
os.Remove(file)
|
||||
fmt.Fprintln(o.ErrOut, "Edit cancelled, no changes made.")
|
||||
return nil
|
||||
}
|
||||
|
||||
lines, err := hasLines(bytes.NewBuffer(edited))
|
||||
if err != nil {
|
||||
return preservedFile(err, file, o.ErrOut)
|
||||
}
|
||||
if !lines {
|
||||
os.Remove(file)
|
||||
fmt.Fprintln(o.ErrOut, "Edit cancelled, saved file was empty.")
|
||||
return nil
|
||||
}
|
||||
|
||||
results = editResults{
|
||||
file: file,
|
||||
}
|
||||
|
||||
// parse the edited file
|
||||
updatedInfos, err := o.updatedResultGetter(edited).Infos()
|
||||
if err != nil {
|
||||
// syntax error
|
||||
containsError = true
|
||||
results.header.reasons = append(results.header.reasons, editReason{head: fmt.Sprintf("The edited file had a syntax error: %v", err)})
|
||||
continue
|
||||
}
|
||||
// not a syntax error as it turns out...
|
||||
containsError = false
|
||||
updatedVisitor := resource.InfoListVisitor(updatedInfos)
|
||||
|
||||
// need to make sure the original namespace wasn't changed while editing
|
||||
if err := updatedVisitor.Visit(resource.RequireNamespace(o.CmdNamespace)); err != nil {
|
||||
return preservedFile(err, file, o.ErrOut)
|
||||
}
|
||||
|
||||
// iterate through all items to apply annotations
|
||||
if err := o.visitAnnotation(updatedVisitor); err != nil {
|
||||
return preservedFile(err, file, o.ErrOut)
|
||||
}
|
||||
|
||||
switch o.EditMode {
|
||||
case NormalEditMode:
|
||||
err = o.visitToPatch(infos, updatedVisitor, &results)
|
||||
case ApplyEditMode:
|
||||
err = o.visitToApplyEditPatch(infos, updatedVisitor)
|
||||
case EditBeforeCreateMode:
|
||||
err = o.visitToCreate(updatedVisitor)
|
||||
default:
|
||||
err = fmt.Errorf("unsupported edit mode %q", o.EditMode)
|
||||
}
|
||||
if err != nil {
|
||||
return preservedFile(err, results.file, o.ErrOut)
|
||||
}
|
||||
|
||||
// Handle all possible errors
|
||||
//
|
||||
// 1. retryable: propose kubectl replace -f
|
||||
// 2. notfound: indicate the location of the saved configuration of the deleted resource
|
||||
// 3. invalid: retry those on the spot by looping ie. reloading the editor
|
||||
if results.retryable > 0 {
|
||||
fmt.Fprintf(o.ErrOut, "You can run `%s replace -f %s` to try this update again.\n", filepath.Base(os.Args[0]), file)
|
||||
return cmdutil.ErrExit
|
||||
}
|
||||
if results.notfound > 0 {
|
||||
fmt.Fprintf(o.ErrOut, "The edits you made on deleted resources have been saved to %q\n", file)
|
||||
return cmdutil.ErrExit
|
||||
}
|
||||
|
||||
if len(results.edit) == 0 {
|
||||
if results.notfound == 0 {
|
||||
os.Remove(file)
|
||||
} else {
|
||||
fmt.Fprintf(o.Out, "The edits you made on deleted resources have been saved to %q\n", file)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
if len(results.header.reasons) > 0 {
|
||||
containsError = true
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
switch o.EditMode {
|
||||
// If doing normal edit we cannot use Visit because we need to edit a list for convenience. Ref: #20519
|
||||
case NormalEditMode:
|
||||
infos, err := o.OriginalResult.Infos()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if len(infos) == 0 {
|
||||
return errors.New("edit cancelled, no objects found.")
|
||||
}
|
||||
return editFn(infos)
|
||||
case ApplyEditMode:
|
||||
infos, err := o.OriginalResult.Infos()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
var annotationInfos []*resource.Info
|
||||
for i := range infos {
|
||||
data, err := kubectl.GetOriginalConfiguration(infos[i].Object)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if data == nil {
|
||||
continue
|
||||
}
|
||||
|
||||
tempInfos, err := o.updatedResultGetter(data).Infos()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
annotationInfos = append(annotationInfos, tempInfos[0])
|
||||
}
|
||||
if len(annotationInfos) == 0 {
|
||||
return errors.New("no last-applied-configuration annotation found on resources, to create the annotation, use command `kubectl apply set-last-applied --create-annotation`")
|
||||
}
|
||||
return editFn(annotationInfos)
|
||||
// If doing an edit before created, we don't want a list and instead want the normal behavior as kubectl create.
|
||||
case EditBeforeCreateMode:
|
||||
return o.OriginalResult.Visit(func(info *resource.Info, err error) error {
|
||||
return editFn([]*resource.Info{info})
|
||||
})
|
||||
default:
|
||||
return fmt.Errorf("unsupported edit mode %q", o.EditMode)
|
||||
}
|
||||
}
|
||||
|
||||
func (o *EditOptions) visitToApplyEditPatch(originalInfos []*resource.Info, patchVisitor resource.Visitor) error {
|
||||
err := patchVisitor.Visit(func(info *resource.Info, incomingErr error) error {
|
||||
editObjUID, err := meta.NewAccessor().UID(info.Object)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
var originalInfo *resource.Info
|
||||
for _, i := range originalInfos {
|
||||
originalObjUID, err := meta.NewAccessor().UID(i.Object)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if editObjUID == originalObjUID {
|
||||
originalInfo = i
|
||||
break
|
||||
}
|
||||
}
|
||||
if originalInfo == nil {
|
||||
return fmt.Errorf("no original object found for %#v", info.Object)
|
||||
}
|
||||
|
||||
originalJS, err := encodeToJson(originalInfo.Object.(runtime.Unstructured))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
editedJS, err := encodeToJson(info.Object.(runtime.Unstructured))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if reflect.DeepEqual(originalJS, editedJS) {
|
||||
printer, err := o.ToPrinter("skipped")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
printer.PrintObj(info.Object, o.Out)
|
||||
return nil
|
||||
} else {
|
||||
err := o.annotationPatch(info)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
printer, err := o.ToPrinter("edited")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
printer.PrintObj(info.Object, o.Out)
|
||||
return nil
|
||||
}
|
||||
})
|
||||
return err
|
||||
}
|
||||
|
||||
func (o *EditOptions) annotationPatch(update *resource.Info) error {
|
||||
patch, _, patchType, err := GetApplyPatch(update.Object.(runtime.Unstructured))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
mapping := update.ResourceMapping()
|
||||
client, err := o.f.UnstructuredClientForMapping(mapping)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
helper := resource.NewHelper(client, mapping)
|
||||
_, err = helper.Patch(o.CmdNamespace, update.Name, patchType, patch)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func GetApplyPatch(obj runtime.Unstructured) ([]byte, []byte, types.PatchType, error) {
|
||||
beforeJSON, err := encodeToJson(obj)
|
||||
if err != nil {
|
||||
return nil, []byte(""), types.MergePatchType, err
|
||||
}
|
||||
objCopy := obj.DeepCopyObject()
|
||||
accessor := meta.NewAccessor()
|
||||
annotations, err := accessor.Annotations(objCopy)
|
||||
if err != nil {
|
||||
return nil, beforeJSON, types.MergePatchType, err
|
||||
}
|
||||
if annotations == nil {
|
||||
annotations = map[string]string{}
|
||||
}
|
||||
annotations[api.LastAppliedConfigAnnotation] = string(beforeJSON)
|
||||
accessor.SetAnnotations(objCopy, annotations)
|
||||
afterJSON, err := encodeToJson(objCopy.(runtime.Unstructured))
|
||||
if err != nil {
|
||||
return nil, beforeJSON, types.MergePatchType, err
|
||||
}
|
||||
patch, err := jsonpatch.CreateMergePatch(beforeJSON, afterJSON)
|
||||
return patch, beforeJSON, types.MergePatchType, err
|
||||
}
|
||||
|
||||
func encodeToJson(obj runtime.Unstructured) ([]byte, error) {
|
||||
serialization, err := runtime.Encode(unstructured.UnstructuredJSONScheme, obj)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
js, err := yaml.ToJSON(serialization)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return js, nil
|
||||
}
|
||||
|
||||
func getPrinter(format string) *editPrinterOptions {
|
||||
switch format {
|
||||
case "json":
|
||||
return &editPrinterOptions{
|
||||
printer: &printers.JSONPrinter{},
|
||||
ext: ".json",
|
||||
addHeader: false,
|
||||
}
|
||||
case "yaml":
|
||||
return &editPrinterOptions{
|
||||
printer: &printers.YAMLPrinter{},
|
||||
ext: ".yaml",
|
||||
addHeader: true,
|
||||
}
|
||||
default:
|
||||
// if format is not specified, use yaml as default
|
||||
return &editPrinterOptions{
|
||||
printer: &printers.YAMLPrinter{},
|
||||
ext: ".yaml",
|
||||
addHeader: true,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (o *EditOptions) visitToPatch(originalInfos []*resource.Info, patchVisitor resource.Visitor, results *editResults) error {
|
||||
err := patchVisitor.Visit(func(info *resource.Info, incomingErr error) error {
|
||||
editObjUID, err := meta.NewAccessor().UID(info.Object)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
var originalInfo *resource.Info
|
||||
for _, i := range originalInfos {
|
||||
originalObjUID, err := meta.NewAccessor().UID(i.Object)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if editObjUID == originalObjUID {
|
||||
originalInfo = i
|
||||
break
|
||||
}
|
||||
}
|
||||
if originalInfo == nil {
|
||||
return fmt.Errorf("no original object found for %#v", info.Object)
|
||||
}
|
||||
|
||||
originalJS, err := encodeToJson(originalInfo.Object.(runtime.Unstructured))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
editedJS, err := encodeToJson(info.Object.(runtime.Unstructured))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if reflect.DeepEqual(originalJS, editedJS) {
|
||||
// no edit, so just skip it.
|
||||
printer, err := o.ToPrinter("skipped")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
printer.PrintObj(info.Object, o.Out)
|
||||
return nil
|
||||
}
|
||||
|
||||
preconditions := []mergepatch.PreconditionFunc{
|
||||
mergepatch.RequireKeyUnchanged("apiVersion"),
|
||||
mergepatch.RequireKeyUnchanged("kind"),
|
||||
mergepatch.RequireMetadataKeyUnchanged("name"),
|
||||
}
|
||||
|
||||
// Create the versioned struct from the type defined in the mapping
|
||||
// (which is the API version we'll be submitting the patch to)
|
||||
versionedObject, err := scheme.Scheme.New(info.Mapping.GroupVersionKind)
|
||||
var patchType types.PatchType
|
||||
var patch []byte
|
||||
switch {
|
||||
case runtime.IsNotRegisteredError(err):
|
||||
// fall back to generic JSON merge patch
|
||||
patchType = types.MergePatchType
|
||||
patch, err = jsonpatch.CreateMergePatch(originalJS, editedJS)
|
||||
if err != nil {
|
||||
glog.V(4).Infof("Unable to calculate diff, no merge is possible: %v", err)
|
||||
return err
|
||||
}
|
||||
for _, precondition := range preconditions {
|
||||
if !precondition(patch) {
|
||||
glog.V(4).Infof("Unable to calculate diff, no merge is possible: %v", err)
|
||||
return fmt.Errorf("%s", "At least one of apiVersion, kind and name was changed")
|
||||
}
|
||||
}
|
||||
case err != nil:
|
||||
return err
|
||||
default:
|
||||
patchType = types.StrategicMergePatchType
|
||||
patch, err = strategicpatch.CreateTwoWayMergePatch(originalJS, editedJS, versionedObject, preconditions...)
|
||||
if err != nil {
|
||||
glog.V(4).Infof("Unable to calculate diff, no merge is possible: %v", err)
|
||||
if mergepatch.IsPreconditionFailed(err) {
|
||||
return fmt.Errorf("%s", "At least one of apiVersion, kind and name was changed")
|
||||
}
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
if o.OutputPatch {
|
||||
fmt.Fprintf(o.Out, "Patch: %s\n", string(patch))
|
||||
}
|
||||
|
||||
patched, err := resource.NewHelper(info.Client, info.Mapping).Patch(info.Namespace, info.Name, patchType, patch)
|
||||
if err != nil {
|
||||
fmt.Fprintln(o.ErrOut, results.addError(err, info))
|
||||
return nil
|
||||
}
|
||||
info.Refresh(patched, true)
|
||||
printer, err := o.ToPrinter("edited")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
printer.PrintObj(info.Object, o.Out)
|
||||
return nil
|
||||
})
|
||||
return err
|
||||
}
|
||||
|
||||
func (o *EditOptions) visitToCreate(createVisitor resource.Visitor) error {
|
||||
err := createVisitor.Visit(func(info *resource.Info, incomingErr error) error {
|
||||
if err := resource.CreateAndRefresh(info); err != nil {
|
||||
return err
|
||||
}
|
||||
printer, err := o.ToPrinter("created")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
printer.PrintObj(info.Object, o.Out)
|
||||
return nil
|
||||
})
|
||||
return err
|
||||
}
|
||||
|
||||
func (o *EditOptions) visitAnnotation(annotationVisitor resource.Visitor) error {
|
||||
// iterate through all items to apply annotations
|
||||
err := annotationVisitor.Visit(func(info *resource.Info, incomingErr error) error {
|
||||
// put configuration annotation in "updates"
|
||||
if o.ApplyAnnotation {
|
||||
if err := kubectl.CreateOrUpdateAnnotation(true, info.Object, cmdutil.InternalVersionJSONEncoder()); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
if err := o.Recorder.Record(info.Object); err != nil {
|
||||
glog.V(4).Infof("error recording current command: %v", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
|
||||
})
|
||||
return err
|
||||
}
|
||||
|
||||
type EditMode string
|
||||
|
||||
const (
|
||||
NormalEditMode EditMode = "normal_mode"
|
||||
EditBeforeCreateMode EditMode = "edit_before_create_mode"
|
||||
ApplyEditMode EditMode = "edit_last_applied_mode"
|
||||
)
|
||||
|
||||
// editReason preserves a message about the reason this file must be edited again
|
||||
type editReason struct {
|
||||
head string
|
||||
other []string
|
||||
}
|
||||
|
||||
// editHeader includes a list of reasons the edit must be retried
|
||||
type editHeader struct {
|
||||
reasons []editReason
|
||||
}
|
||||
|
||||
// writeTo outputs the current header information into a stream
|
||||
func (h *editHeader) writeTo(w io.Writer, editMode EditMode) error {
|
||||
if editMode == ApplyEditMode {
|
||||
fmt.Fprint(w, `# Please edit the 'last-applied-configuration' annotations below.
|
||||
# Lines beginning with a '#' will be ignored, and an empty file will abort the edit.
|
||||
#
|
||||
`)
|
||||
} else {
|
||||
fmt.Fprint(w, `# Please edit the object below. Lines beginning with a '#' will be ignored,
|
||||
# and an empty file will abort the edit. If an error occurs while saving this file will be
|
||||
# reopened with the relevant failures.
|
||||
#
|
||||
`)
|
||||
}
|
||||
|
||||
for _, r := range h.reasons {
|
||||
if len(r.other) > 0 {
|
||||
fmt.Fprintf(w, "# %s:\n", hashOnLineBreak(r.head))
|
||||
} else {
|
||||
fmt.Fprintf(w, "# %s\n", hashOnLineBreak(r.head))
|
||||
}
|
||||
for _, o := range r.other {
|
||||
fmt.Fprintf(w, "# * %s\n", hashOnLineBreak(o))
|
||||
}
|
||||
fmt.Fprintln(w, "#")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (h *editHeader) flush() {
|
||||
h.reasons = []editReason{}
|
||||
}
|
||||
|
||||
// editResults capture the result of an update
|
||||
type editResults struct {
|
||||
header editHeader
|
||||
retryable int
|
||||
notfound int
|
||||
edit []*resource.Info
|
||||
file string
|
||||
|
||||
version schema.GroupVersion
|
||||
}
|
||||
|
||||
func (r *editResults) addError(err error, info *resource.Info) string {
|
||||
resourceString := info.Mapping.Resource.Resource
|
||||
if len(info.Mapping.Resource.Group) > 0 {
|
||||
resourceString = resourceString + "." + info.Mapping.Resource.Group
|
||||
}
|
||||
|
||||
switch {
|
||||
case apierrors.IsInvalid(err):
|
||||
r.edit = append(r.edit, info)
|
||||
reason := editReason{
|
||||
head: fmt.Sprintf("%s %q was not valid", resourceString, info.Name),
|
||||
}
|
||||
if err, ok := err.(apierrors.APIStatus); ok {
|
||||
if details := err.Status().Details; details != nil {
|
||||
for _, cause := range details.Causes {
|
||||
reason.other = append(reason.other, fmt.Sprintf("%s: %s", cause.Field, cause.Message))
|
||||
}
|
||||
}
|
||||
}
|
||||
r.header.reasons = append(r.header.reasons, reason)
|
||||
return fmt.Sprintf("error: %s %q is invalid", resourceString, info.Name)
|
||||
case apierrors.IsNotFound(err):
|
||||
r.notfound++
|
||||
return fmt.Sprintf("error: %s %q could not be found on the server", resourceString, info.Name)
|
||||
default:
|
||||
r.retryable++
|
||||
return fmt.Sprintf("error: %s %q could not be patched: %v", resourceString, info.Name, err)
|
||||
}
|
||||
}
|
||||
|
||||
// preservedFile writes out a message about the provided file if it exists to the
|
||||
// provided output stream when an error happens. Used to notify the user where
|
||||
// their updates were preserved.
|
||||
func preservedFile(err error, path string, out io.Writer) error {
|
||||
if len(path) > 0 {
|
||||
if _, err := os.Stat(path); !os.IsNotExist(err) {
|
||||
fmt.Fprintf(out, "A copy of your changes has been stored to %q\n", path)
|
||||
}
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
// hasLines returns true if any line in the provided stream is non empty - has non-whitespace
|
||||
// characters, or the first non-whitespace character is a '#' indicating a comment. Returns
|
||||
// any errors encountered reading the stream.
|
||||
func hasLines(r io.Reader) (bool, error) {
|
||||
// TODO: if any files we read have > 64KB lines, we'll need to switch to bytes.ReadLine
|
||||
// TODO: probably going to be secrets
|
||||
s := bufio.NewScanner(r)
|
||||
for s.Scan() {
|
||||
if line := strings.TrimSpace(s.Text()); len(line) > 0 && line[0] != '#' {
|
||||
return true, nil
|
||||
}
|
||||
}
|
||||
if err := s.Err(); err != nil && err != io.EOF {
|
||||
return false, err
|
||||
}
|
||||
return false, nil
|
||||
}
|
||||
|
||||
// hashOnLineBreak returns a string built from the provided string by inserting any necessary '#'
|
||||
// characters after '\n' characters, indicating a comment.
|
||||
func hashOnLineBreak(s string) string {
|
||||
r := ""
|
||||
for i, ch := range s {
|
||||
j := i + 1
|
||||
if j < len(s) && ch == '\n' && s[j] != '#' {
|
||||
r += "\n# "
|
||||
} else {
|
||||
r += string(ch)
|
||||
}
|
||||
}
|
||||
return r
|
||||
}
|
||||
|
||||
// editorEnvs returns an ordered list of env vars to check for editor preferences.
|
||||
func editorEnvs() []string {
|
||||
return []string{
|
||||
"KUBE_EDITOR",
|
||||
"EDITOR",
|
||||
}
|
||||
}
|
51
vendor/k8s.io/kubernetes/pkg/kubectl/cmd/util/editor/editoptions_test.go
generated
vendored
Normal file
51
vendor/k8s.io/kubernetes/pkg/kubectl/cmd/util/editor/editoptions_test.go
generated
vendored
Normal file
@@ -0,0 +1,51 @@
|
||||
/*
|
||||
Copyright 2018 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 editor
|
||||
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestHashOnLineBreak(t *testing.T) {
|
||||
tests := []struct {
|
||||
original string
|
||||
expected string
|
||||
}{
|
||||
{
|
||||
original: "",
|
||||
expected: "",
|
||||
},
|
||||
{
|
||||
original: "\n",
|
||||
expected: "\n",
|
||||
},
|
||||
{
|
||||
original: "a\na\na\n",
|
||||
expected: "a\n# a\n# a\n",
|
||||
},
|
||||
{
|
||||
original: "a\n\n\na\n\n",
|
||||
expected: "a\n# \n# \n# a\n# \n",
|
||||
},
|
||||
}
|
||||
for _, test := range tests {
|
||||
r := hashOnLineBreak(test.original)
|
||||
if r != test.expected {
|
||||
t.Errorf("expected: %s, saw: %s", test.expected, r)
|
||||
}
|
||||
}
|
||||
}
|
192
vendor/k8s.io/kubernetes/pkg/kubectl/cmd/util/editor/editor.go
generated
vendored
Normal file
192
vendor/k8s.io/kubernetes/pkg/kubectl/cmd/util/editor/editor.go
generated
vendored
Normal file
@@ -0,0 +1,192 @@
|
||||
/*
|
||||
Copyright 2015 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 editor
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"math/rand"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"strings"
|
||||
|
||||
"github.com/golang/glog"
|
||||
|
||||
"k8s.io/kubernetes/pkg/kubectl/util/term"
|
||||
)
|
||||
|
||||
const (
|
||||
// sorry, blame Git
|
||||
// TODO: on Windows rely on 'start' to launch the editor associated
|
||||
// with the given file type. If we can't because of the need of
|
||||
// blocking, use a script with 'ftype' and 'assoc' to detect it.
|
||||
defaultEditor = "vi"
|
||||
defaultShell = "/bin/bash"
|
||||
windowsEditor = "notepad"
|
||||
windowsShell = "cmd"
|
||||
)
|
||||
|
||||
type Editor struct {
|
||||
Args []string
|
||||
Shell bool
|
||||
}
|
||||
|
||||
// NewDefaultEditor creates a struct Editor that uses the OS environment to
|
||||
// locate the editor program, looking at EDITOR environment variable to find
|
||||
// the proper command line. If the provided editor has no spaces, or no quotes,
|
||||
// it is treated as a bare command to be loaded. Otherwise, the string will
|
||||
// be passed to the user's shell for execution.
|
||||
func NewDefaultEditor(envs []string) Editor {
|
||||
args, shell := defaultEnvEditor(envs)
|
||||
return Editor{
|
||||
Args: args,
|
||||
Shell: shell,
|
||||
}
|
||||
}
|
||||
|
||||
func defaultEnvShell() []string {
|
||||
shell := os.Getenv("SHELL")
|
||||
if len(shell) == 0 {
|
||||
shell = platformize(defaultShell, windowsShell)
|
||||
}
|
||||
flag := "-c"
|
||||
if shell == windowsShell {
|
||||
flag = "/C"
|
||||
}
|
||||
return []string{shell, flag}
|
||||
}
|
||||
|
||||
func defaultEnvEditor(envs []string) ([]string, bool) {
|
||||
var editor string
|
||||
for _, env := range envs {
|
||||
if len(env) > 0 {
|
||||
editor = os.Getenv(env)
|
||||
}
|
||||
if len(editor) > 0 {
|
||||
break
|
||||
}
|
||||
}
|
||||
if len(editor) == 0 {
|
||||
editor = platformize(defaultEditor, windowsEditor)
|
||||
}
|
||||
if !strings.Contains(editor, " ") {
|
||||
return []string{editor}, false
|
||||
}
|
||||
if !strings.ContainsAny(editor, "\"'\\") {
|
||||
return strings.Split(editor, " "), false
|
||||
}
|
||||
// rather than parse the shell arguments ourselves, punt to the shell
|
||||
shell := defaultEnvShell()
|
||||
return append(shell, editor), true
|
||||
}
|
||||
|
||||
func (e Editor) args(path string) []string {
|
||||
args := make([]string, len(e.Args))
|
||||
copy(args, e.Args)
|
||||
if e.Shell {
|
||||
last := args[len(args)-1]
|
||||
args[len(args)-1] = fmt.Sprintf("%s %q", last, path)
|
||||
} else {
|
||||
args = append(args, path)
|
||||
}
|
||||
return args
|
||||
}
|
||||
|
||||
// Launch opens the described or returns an error. The TTY will be protected, and
|
||||
// SIGQUIT, SIGTERM, and SIGINT will all be trapped.
|
||||
func (e Editor) Launch(path string) error {
|
||||
if len(e.Args) == 0 {
|
||||
return fmt.Errorf("no editor defined, can't open %s", path)
|
||||
}
|
||||
abs, err := filepath.Abs(path)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
args := e.args(abs)
|
||||
cmd := exec.Command(args[0], args[1:]...)
|
||||
cmd.Stdout = os.Stdout
|
||||
cmd.Stderr = os.Stderr
|
||||
cmd.Stdin = os.Stdin
|
||||
glog.V(5).Infof("Opening file with editor %v", args)
|
||||
if err := (term.TTY{In: os.Stdin, TryDev: true}).Safe(cmd.Run); err != nil {
|
||||
if err, ok := err.(*exec.Error); ok {
|
||||
if err.Err == exec.ErrNotFound {
|
||||
return fmt.Errorf("unable to launch the editor %q", strings.Join(e.Args, " "))
|
||||
}
|
||||
}
|
||||
return fmt.Errorf("there was a problem with the editor %q", strings.Join(e.Args, " "))
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// LaunchTempFile reads the provided stream into a temporary file in the given directory
|
||||
// and file prefix, and then invokes Launch with the path of that file. It will return
|
||||
// the contents of the file after launch, any errors that occur, and the path of the
|
||||
// temporary file so the caller can clean it up as needed.
|
||||
func (e Editor) LaunchTempFile(prefix, suffix string, r io.Reader) ([]byte, string, error) {
|
||||
f, err := tempFile(prefix, suffix)
|
||||
if err != nil {
|
||||
return nil, "", err
|
||||
}
|
||||
defer f.Close()
|
||||
path := f.Name()
|
||||
if _, err := io.Copy(f, r); err != nil {
|
||||
os.Remove(path)
|
||||
return nil, path, err
|
||||
}
|
||||
// This file descriptor needs to close so the next process (Launch) can claim it.
|
||||
f.Close()
|
||||
if err := e.Launch(path); err != nil {
|
||||
return nil, path, err
|
||||
}
|
||||
bytes, err := ioutil.ReadFile(path)
|
||||
return bytes, path, err
|
||||
}
|
||||
|
||||
func tempFile(prefix, suffix string) (f *os.File, err error) {
|
||||
dir := os.TempDir()
|
||||
|
||||
for i := 0; i < 10000; i++ {
|
||||
name := filepath.Join(dir, prefix+randSeq(5)+suffix)
|
||||
f, err = os.OpenFile(name, os.O_RDWR|os.O_CREATE|os.O_EXCL, 0600)
|
||||
if os.IsExist(err) {
|
||||
continue
|
||||
}
|
||||
break
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
var letters = []rune("abcdefghijklmnopqrstuvwxyz0123456789")
|
||||
|
||||
func randSeq(n int) string {
|
||||
b := make([]rune, n)
|
||||
for i := range b {
|
||||
b[i] = letters[rand.Intn(len(letters))]
|
||||
}
|
||||
return string(b)
|
||||
}
|
||||
|
||||
func platformize(linux, windows string) string {
|
||||
if runtime.GOOS == "windows" {
|
||||
return windows
|
||||
}
|
||||
return linux
|
||||
}
|
63
vendor/k8s.io/kubernetes/pkg/kubectl/cmd/util/editor/editor_test.go
generated
vendored
Normal file
63
vendor/k8s.io/kubernetes/pkg/kubectl/cmd/util/editor/editor_test.go
generated
vendored
Normal file
@@ -0,0 +1,63 @@
|
||||
/*
|
||||
Copyright 2015 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 editor
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"reflect"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestArgs(t *testing.T) {
|
||||
if e, a := []string{"/bin/bash", "-c \"test\""}, (Editor{Args: []string{"/bin/bash", "-c"}, Shell: true}).args("test"); !reflect.DeepEqual(e, a) {
|
||||
t.Errorf("unexpected args: %v", a)
|
||||
}
|
||||
if e, a := []string{"/bin/bash", "-c", "test"}, (Editor{Args: []string{"/bin/bash", "-c"}, Shell: false}).args("test"); !reflect.DeepEqual(e, a) {
|
||||
t.Errorf("unexpected args: %v", a)
|
||||
}
|
||||
if e, a := []string{"/bin/bash", "-i -c \"test\""}, (Editor{Args: []string{"/bin/bash", "-i -c"}, Shell: true}).args("test"); !reflect.DeepEqual(e, a) {
|
||||
t.Errorf("unexpected args: %v", a)
|
||||
}
|
||||
if e, a := []string{"/test", "test"}, (Editor{Args: []string{"/test"}}).args("test"); !reflect.DeepEqual(e, a) {
|
||||
t.Errorf("unexpected args: %v", a)
|
||||
}
|
||||
}
|
||||
|
||||
func TestEditor(t *testing.T) {
|
||||
edit := Editor{Args: []string{"cat"}}
|
||||
testStr := "test something\n"
|
||||
contents, path, err := edit.LaunchTempFile("", "someprefix", bytes.NewBufferString(testStr))
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
if _, err := os.Stat(path); err != nil {
|
||||
t.Fatalf("no temp file: %s", path)
|
||||
}
|
||||
defer os.Remove(path)
|
||||
if disk, err := ioutil.ReadFile(path); err != nil || !bytes.Equal(contents, disk) {
|
||||
t.Errorf("unexpected file on disk: %v %s", err, string(disk))
|
||||
}
|
||||
if !bytes.Equal(contents, []byte(testStr)) {
|
||||
t.Errorf("unexpected contents: %s", string(contents))
|
||||
}
|
||||
if !strings.Contains(path, "someprefix") {
|
||||
t.Errorf("path not expected: %s", path)
|
||||
}
|
||||
}
|
70
vendor/k8s.io/kubernetes/pkg/kubectl/cmd/util/factory.go
generated
vendored
Normal file
70
vendor/k8s.io/kubernetes/pkg/kubectl/cmd/util/factory.go
generated
vendored
Normal file
@@ -0,0 +1,70 @@
|
||||
/*
|
||||
Copyright 2014 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 (
|
||||
"k8s.io/apimachinery/pkg/api/meta"
|
||||
"k8s.io/client-go/dynamic"
|
||||
"k8s.io/client-go/kubernetes"
|
||||
restclient "k8s.io/client-go/rest"
|
||||
"k8s.io/kubernetes/pkg/client/clientset_generated/internalclientset"
|
||||
"k8s.io/kubernetes/pkg/kubectl/cmd/util/openapi"
|
||||
"k8s.io/kubernetes/pkg/kubectl/genericclioptions"
|
||||
"k8s.io/kubernetes/pkg/kubectl/genericclioptions/resource"
|
||||
"k8s.io/kubernetes/pkg/kubectl/validation"
|
||||
)
|
||||
|
||||
// Factory provides abstractions that allow the Kubectl command to be extended across multiple types
|
||||
// of resources and different API sets.
|
||||
// The rings are here for a reason. In order for composers to be able to provide alternative factory implementations
|
||||
// they need to provide low level pieces of *certain* functions so that when the factory calls back into itself
|
||||
// it uses the custom version of the function. Rather than try to enumerate everything that someone would want to override
|
||||
// we split the factory into rings, where each ring can depend on methods in an earlier ring, but cannot depend
|
||||
// upon peer methods in its own ring.
|
||||
// TODO: make the functions interfaces
|
||||
// TODO: pass the various interfaces on the factory directly into the command constructors (so the
|
||||
// commands are decoupled from the factory).
|
||||
type Factory interface {
|
||||
genericclioptions.RESTClientGetter
|
||||
|
||||
// ClientSet gives you back an internal, generated clientset
|
||||
ClientSet() (internalclientset.Interface, error)
|
||||
|
||||
// DynamicClient returns a dynamic client ready for use
|
||||
DynamicClient() (dynamic.Interface, error)
|
||||
|
||||
// KubernetesClientSet gives you back an external clientset
|
||||
KubernetesClientSet() (*kubernetes.Clientset, error)
|
||||
|
||||
// Returns a RESTClient for accessing Kubernetes resources or an error.
|
||||
RESTClient() (*restclient.RESTClient, error)
|
||||
|
||||
// NewBuilder returns an object that assists in loading objects from both disk and the server
|
||||
// and which implements the common patterns for CLI interactions with generic resources.
|
||||
NewBuilder() *resource.Builder
|
||||
|
||||
// Returns a RESTClient for working with the specified RESTMapping or an error. This is intended
|
||||
// for working with arbitrary resources and is not guaranteed to point to a Kubernetes APIServer.
|
||||
ClientForMapping(mapping *meta.RESTMapping) (resource.RESTClient, error)
|
||||
// Returns a RESTClient for working with Unstructured objects.
|
||||
UnstructuredClientForMapping(mapping *meta.RESTMapping) (resource.RESTClient, error)
|
||||
|
||||
// Returns a schema that can validate objects stored on disk.
|
||||
Validator(validate bool) (validation.Schema, error)
|
||||
// OpenAPISchema returns the schema openapi schema definition
|
||||
OpenAPISchema() (openapi.Resources, error)
|
||||
}
|
199
vendor/k8s.io/kubernetes/pkg/kubectl/cmd/util/factory_client_access.go
generated
vendored
Normal file
199
vendor/k8s.io/kubernetes/pkg/kubectl/cmd/util/factory_client_access.go
generated
vendored
Normal file
@@ -0,0 +1,199 @@
|
||||
/*
|
||||
Copyright 2016 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.
|
||||
*/
|
||||
|
||||
// this file contains factories with no other dependencies
|
||||
|
||||
package util
|
||||
|
||||
import (
|
||||
"sync"
|
||||
|
||||
"k8s.io/apimachinery/pkg/api/meta"
|
||||
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
|
||||
"k8s.io/apimachinery/pkg/runtime"
|
||||
"k8s.io/client-go/discovery"
|
||||
"k8s.io/client-go/dynamic"
|
||||
"k8s.io/client-go/kubernetes"
|
||||
restclient "k8s.io/client-go/rest"
|
||||
"k8s.io/client-go/tools/clientcmd"
|
||||
"k8s.io/kubernetes/pkg/api/legacyscheme"
|
||||
api "k8s.io/kubernetes/pkg/apis/core"
|
||||
"k8s.io/kubernetes/pkg/client/clientset_generated/internalclientset"
|
||||
"k8s.io/kubernetes/pkg/kubectl/cmd/util/openapi"
|
||||
openapivalidation "k8s.io/kubernetes/pkg/kubectl/cmd/util/openapi/validation"
|
||||
"k8s.io/kubernetes/pkg/kubectl/genericclioptions"
|
||||
"k8s.io/kubernetes/pkg/kubectl/genericclioptions/resource"
|
||||
"k8s.io/kubernetes/pkg/kubectl/validation"
|
||||
)
|
||||
|
||||
type factoryImpl struct {
|
||||
clientGetter genericclioptions.RESTClientGetter
|
||||
|
||||
// openAPIGetter loads and caches openapi specs
|
||||
openAPIGetter openAPIGetter
|
||||
}
|
||||
|
||||
type openAPIGetter struct {
|
||||
once sync.Once
|
||||
getter openapi.Getter
|
||||
}
|
||||
|
||||
func NewFactory(clientGetter genericclioptions.RESTClientGetter) Factory {
|
||||
if clientGetter == nil {
|
||||
panic("attempt to instantiate client_access_factory with nil clientGetter")
|
||||
}
|
||||
|
||||
f := &factoryImpl{
|
||||
clientGetter: clientGetter,
|
||||
}
|
||||
|
||||
return f
|
||||
}
|
||||
|
||||
func (f *factoryImpl) ToRESTConfig() (*restclient.Config, error) {
|
||||
return f.clientGetter.ToRESTConfig()
|
||||
}
|
||||
|
||||
func (f *factoryImpl) ToRESTMapper() (meta.RESTMapper, error) {
|
||||
return f.clientGetter.ToRESTMapper()
|
||||
}
|
||||
|
||||
func (f *factoryImpl) ToDiscoveryClient() (discovery.CachedDiscoveryInterface, error) {
|
||||
return f.clientGetter.ToDiscoveryClient()
|
||||
}
|
||||
|
||||
func (f *factoryImpl) ToRawKubeConfigLoader() clientcmd.ClientConfig {
|
||||
return f.clientGetter.ToRawKubeConfigLoader()
|
||||
}
|
||||
|
||||
func (f *factoryImpl) KubernetesClientSet() (*kubernetes.Clientset, error) {
|
||||
clientConfig, err := f.ToRESTConfig()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return kubernetes.NewForConfig(clientConfig)
|
||||
}
|
||||
|
||||
func (f *factoryImpl) ClientSet() (internalclientset.Interface, error) {
|
||||
clientConfig, err := f.ToRESTConfig()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return internalclientset.NewForConfig(clientConfig)
|
||||
}
|
||||
|
||||
func (f *factoryImpl) DynamicClient() (dynamic.Interface, error) {
|
||||
clientConfig, err := f.ToRESTConfig()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return dynamic.NewForConfig(clientConfig)
|
||||
}
|
||||
|
||||
// NewBuilder returns a new resource builder for structured api objects.
|
||||
func (f *factoryImpl) NewBuilder() *resource.Builder {
|
||||
return resource.NewBuilder(f.clientGetter)
|
||||
}
|
||||
|
||||
func (f *factoryImpl) RESTClient() (*restclient.RESTClient, error) {
|
||||
clientConfig, err := f.ToRESTConfig()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
setKubernetesDefaults(clientConfig)
|
||||
return restclient.RESTClientFor(clientConfig)
|
||||
}
|
||||
|
||||
func (f *factoryImpl) ClientForMapping(mapping *meta.RESTMapping) (resource.RESTClient, error) {
|
||||
cfg, err := f.clientGetter.ToRESTConfig()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := setKubernetesDefaults(cfg); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
gvk := mapping.GroupVersionKind
|
||||
switch gvk.Group {
|
||||
case api.GroupName:
|
||||
cfg.APIPath = "/api"
|
||||
default:
|
||||
cfg.APIPath = "/apis"
|
||||
}
|
||||
gv := gvk.GroupVersion()
|
||||
cfg.GroupVersion = &gv
|
||||
return restclient.RESTClientFor(cfg)
|
||||
}
|
||||
|
||||
func (f *factoryImpl) UnstructuredClientForMapping(mapping *meta.RESTMapping) (resource.RESTClient, error) {
|
||||
cfg, err := f.clientGetter.ToRESTConfig()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := restclient.SetKubernetesDefaults(cfg); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
cfg.APIPath = "/apis"
|
||||
if mapping.GroupVersionKind.Group == api.GroupName {
|
||||
cfg.APIPath = "/api"
|
||||
}
|
||||
gv := mapping.GroupVersionKind.GroupVersion()
|
||||
cfg.ContentConfig = resource.UnstructuredPlusDefaultContentConfig()
|
||||
cfg.GroupVersion = &gv
|
||||
return restclient.RESTClientFor(cfg)
|
||||
}
|
||||
|
||||
func (f *factoryImpl) Validator(validate bool) (validation.Schema, error) {
|
||||
if !validate {
|
||||
return validation.NullSchema{}, nil
|
||||
}
|
||||
|
||||
resources, err := f.OpenAPISchema()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return validation.ConjunctiveSchema{
|
||||
openapivalidation.NewSchemaValidation(resources),
|
||||
validation.NoDoubleKeySchema{},
|
||||
}, nil
|
||||
}
|
||||
|
||||
// OpenAPISchema returns metadata and structural information about Kubernetes object definitions.
|
||||
func (f *factoryImpl) OpenAPISchema() (openapi.Resources, error) {
|
||||
discovery, err := f.clientGetter.ToDiscoveryClient()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Lazily initialize the OpenAPIGetter once
|
||||
f.openAPIGetter.once.Do(func() {
|
||||
// Create the caching OpenAPIGetter
|
||||
f.openAPIGetter.getter = openapi.NewOpenAPIGetter(discovery)
|
||||
})
|
||||
|
||||
// Delegate to the OpenAPIGetter
|
||||
return f.openAPIGetter.getter.Get()
|
||||
}
|
||||
|
||||
// this method exists to help us find the points still relying on internal types.
|
||||
func InternalVersionDecoder() runtime.Decoder {
|
||||
return legacyscheme.Codecs.UniversalDecoder()
|
||||
}
|
||||
|
||||
func InternalVersionJSONEncoder() runtime.Encoder {
|
||||
encoder := legacyscheme.Codecs.LegacyCodec(legacyscheme.Scheme.PrioritizedVersionsAllGroups()...)
|
||||
return unstructured.JSONFallbackEncoder{Encoder: encoder}
|
||||
}
|
237
vendor/k8s.io/kubernetes/pkg/kubectl/cmd/util/generator.go
generated
vendored
Normal file
237
vendor/k8s.io/kubernetes/pkg/kubectl/cmd/util/generator.go
generated
vendored
Normal file
@@ -0,0 +1,237 @@
|
||||
/*
|
||||
Copyright 2018 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"
|
||||
"io"
|
||||
|
||||
appsv1 "k8s.io/api/apps/v1"
|
||||
appsv1beta1 "k8s.io/api/apps/v1beta1"
|
||||
batchv1 "k8s.io/api/batch/v1"
|
||||
batchv1beta1 "k8s.io/api/batch/v1beta1"
|
||||
batchv2alpha1 "k8s.io/api/batch/v2alpha1"
|
||||
extensionsv1beta1 "k8s.io/api/extensions/v1beta1"
|
||||
apierrors "k8s.io/apimachinery/pkg/api/errors"
|
||||
"k8s.io/apimachinery/pkg/runtime/schema"
|
||||
"k8s.io/client-go/discovery"
|
||||
"k8s.io/kubernetes/pkg/kubectl"
|
||||
)
|
||||
|
||||
const (
|
||||
// TODO(sig-cli): Enforce consistent naming for generators here.
|
||||
// See discussion in https://github.com/kubernetes/kubernetes/issues/46237
|
||||
// before you add any more.
|
||||
RunV1GeneratorName = "run/v1"
|
||||
RunPodV1GeneratorName = "run-pod/v1"
|
||||
ServiceV1GeneratorName = "service/v1"
|
||||
ServiceV2GeneratorName = "service/v2"
|
||||
ServiceNodePortGeneratorV1Name = "service-nodeport/v1"
|
||||
ServiceClusterIPGeneratorV1Name = "service-clusterip/v1"
|
||||
ServiceLoadBalancerGeneratorV1Name = "service-loadbalancer/v1"
|
||||
ServiceExternalNameGeneratorV1Name = "service-externalname/v1"
|
||||
ServiceAccountV1GeneratorName = "serviceaccount/v1"
|
||||
HorizontalPodAutoscalerV1GeneratorName = "horizontalpodautoscaler/v1"
|
||||
DeploymentV1Beta1GeneratorName = "deployment/v1beta1"
|
||||
DeploymentAppsV1Beta1GeneratorName = "deployment/apps.v1beta1"
|
||||
DeploymentBasicV1Beta1GeneratorName = "deployment-basic/v1beta1"
|
||||
DeploymentBasicAppsV1Beta1GeneratorName = "deployment-basic/apps.v1beta1"
|
||||
DeploymentBasicAppsV1GeneratorName = "deployment-basic/apps.v1"
|
||||
JobV1GeneratorName = "job/v1"
|
||||
CronJobV2Alpha1GeneratorName = "cronjob/v2alpha1"
|
||||
CronJobV1Beta1GeneratorName = "cronjob/v1beta1"
|
||||
NamespaceV1GeneratorName = "namespace/v1"
|
||||
ResourceQuotaV1GeneratorName = "resourcequotas/v1"
|
||||
SecretV1GeneratorName = "secret/v1"
|
||||
SecretForDockerRegistryV1GeneratorName = "secret-for-docker-registry/v1"
|
||||
SecretForTLSV1GeneratorName = "secret-for-tls/v1"
|
||||
ConfigMapV1GeneratorName = "configmap/v1"
|
||||
ClusterRoleBindingV1GeneratorName = "clusterrolebinding.rbac.authorization.k8s.io/v1alpha1"
|
||||
RoleBindingV1GeneratorName = "rolebinding.rbac.authorization.k8s.io/v1alpha1"
|
||||
PodDisruptionBudgetV1GeneratorName = "poddisruptionbudget/v1beta1"
|
||||
PodDisruptionBudgetV2GeneratorName = "poddisruptionbudget/v1beta1/v2"
|
||||
PriorityClassV1Alpha1GeneratorName = "priorityclass/v1alpha1"
|
||||
)
|
||||
|
||||
// GeneratorFunc returns the generators for the provided command
|
||||
type GeneratorFunc func(cmdName string) map[string]kubectl.Generator
|
||||
|
||||
// GeneratorFn gives a way to easily override the function for unit testing if needed
|
||||
var GeneratorFn GeneratorFunc = defaultGenerators
|
||||
|
||||
// defaultGenerators returns the set of default generators for use in Factory instances
|
||||
func defaultGenerators(cmdName string) map[string]kubectl.Generator {
|
||||
var generator map[string]kubectl.Generator
|
||||
switch cmdName {
|
||||
case "expose":
|
||||
generator = map[string]kubectl.Generator{
|
||||
ServiceV1GeneratorName: kubectl.ServiceGeneratorV1{},
|
||||
ServiceV2GeneratorName: kubectl.ServiceGeneratorV2{},
|
||||
}
|
||||
case "service-clusterip":
|
||||
generator = map[string]kubectl.Generator{
|
||||
ServiceClusterIPGeneratorV1Name: kubectl.ServiceClusterIPGeneratorV1{},
|
||||
}
|
||||
case "service-nodeport":
|
||||
generator = map[string]kubectl.Generator{
|
||||
ServiceNodePortGeneratorV1Name: kubectl.ServiceNodePortGeneratorV1{},
|
||||
}
|
||||
case "service-loadbalancer":
|
||||
generator = map[string]kubectl.Generator{
|
||||
ServiceLoadBalancerGeneratorV1Name: kubectl.ServiceLoadBalancerGeneratorV1{},
|
||||
}
|
||||
case "deployment":
|
||||
// Create Deployment has only StructuredGenerators and no
|
||||
// param-based Generators.
|
||||
// The StructuredGenerators are as follows (as of 2018-03-16):
|
||||
// DeploymentBasicV1Beta1GeneratorName -> kubectl.DeploymentBasicGeneratorV1
|
||||
// DeploymentBasicAppsV1Beta1GeneratorName -> kubectl.DeploymentBasicAppsGeneratorV1Beta1
|
||||
// DeploymentBasicAppsV1GeneratorName -> kubectl.DeploymentBasicAppsGeneratorV1
|
||||
generator = map[string]kubectl.Generator{}
|
||||
case "run":
|
||||
generator = map[string]kubectl.Generator{
|
||||
RunV1GeneratorName: kubectl.BasicReplicationController{},
|
||||
RunPodV1GeneratorName: kubectl.BasicPod{},
|
||||
DeploymentV1Beta1GeneratorName: kubectl.DeploymentV1Beta1{},
|
||||
DeploymentAppsV1Beta1GeneratorName: kubectl.DeploymentAppsV1Beta1{},
|
||||
JobV1GeneratorName: kubectl.JobV1{},
|
||||
CronJobV2Alpha1GeneratorName: kubectl.CronJobV2Alpha1{},
|
||||
CronJobV1Beta1GeneratorName: kubectl.CronJobV1Beta1{},
|
||||
}
|
||||
case "namespace":
|
||||
generator = map[string]kubectl.Generator{
|
||||
NamespaceV1GeneratorName: kubectl.NamespaceGeneratorV1{},
|
||||
}
|
||||
case "quota":
|
||||
generator = map[string]kubectl.Generator{
|
||||
ResourceQuotaV1GeneratorName: kubectl.ResourceQuotaGeneratorV1{},
|
||||
}
|
||||
case "secret":
|
||||
generator = map[string]kubectl.Generator{
|
||||
SecretV1GeneratorName: kubectl.SecretGeneratorV1{},
|
||||
}
|
||||
case "secret-for-docker-registry":
|
||||
generator = map[string]kubectl.Generator{
|
||||
SecretForDockerRegistryV1GeneratorName: kubectl.SecretForDockerRegistryGeneratorV1{},
|
||||
}
|
||||
case "secret-for-tls":
|
||||
generator = map[string]kubectl.Generator{
|
||||
SecretForTLSV1GeneratorName: kubectl.SecretForTLSGeneratorV1{},
|
||||
}
|
||||
}
|
||||
|
||||
return generator
|
||||
}
|
||||
|
||||
// FallbackGeneratorNameIfNecessary returns the name of the old generator
|
||||
// if server does not support new generator. Otherwise, the
|
||||
// generator string is returned unchanged.
|
||||
//
|
||||
// If the generator name is changed, print a warning message to let the user
|
||||
// know.
|
||||
func FallbackGeneratorNameIfNecessary(
|
||||
generatorName string,
|
||||
discoveryClient discovery.DiscoveryInterface,
|
||||
cmdErr io.Writer,
|
||||
) (string, error) {
|
||||
switch generatorName {
|
||||
case DeploymentAppsV1Beta1GeneratorName:
|
||||
hasResource, err := HasResource(discoveryClient, appsv1beta1.SchemeGroupVersion.WithResource("deployments"))
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
if !hasResource {
|
||||
return FallbackGeneratorNameIfNecessary(DeploymentV1Beta1GeneratorName, discoveryClient, cmdErr)
|
||||
}
|
||||
case DeploymentV1Beta1GeneratorName:
|
||||
hasResource, err := HasResource(discoveryClient, extensionsv1beta1.SchemeGroupVersion.WithResource("deployments"))
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
if !hasResource {
|
||||
return RunV1GeneratorName, nil
|
||||
}
|
||||
case DeploymentBasicAppsV1GeneratorName:
|
||||
hasResource, err := HasResource(discoveryClient, appsv1.SchemeGroupVersion.WithResource("deployments"))
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
if !hasResource {
|
||||
return FallbackGeneratorNameIfNecessary(DeploymentBasicAppsV1Beta1GeneratorName, discoveryClient, cmdErr)
|
||||
}
|
||||
case DeploymentBasicAppsV1Beta1GeneratorName:
|
||||
hasResource, err := HasResource(discoveryClient, appsv1beta1.SchemeGroupVersion.WithResource("deployments"))
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
if !hasResource {
|
||||
return DeploymentBasicV1Beta1GeneratorName, nil
|
||||
}
|
||||
case JobV1GeneratorName:
|
||||
hasResource, err := HasResource(discoveryClient, batchv1.SchemeGroupVersion.WithResource("jobs"))
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
if !hasResource {
|
||||
return RunPodV1GeneratorName, nil
|
||||
}
|
||||
case CronJobV1Beta1GeneratorName:
|
||||
hasResource, err := HasResource(discoveryClient, batchv1beta1.SchemeGroupVersion.WithResource("cronjobs"))
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
if !hasResource {
|
||||
return FallbackGeneratorNameIfNecessary(CronJobV2Alpha1GeneratorName, discoveryClient, cmdErr)
|
||||
}
|
||||
case CronJobV2Alpha1GeneratorName:
|
||||
hasResource, err := HasResource(discoveryClient, batchv2alpha1.SchemeGroupVersion.WithResource("cronjobs"))
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
if !hasResource {
|
||||
return JobV1GeneratorName, nil
|
||||
}
|
||||
}
|
||||
return generatorName, nil
|
||||
}
|
||||
|
||||
func HasResource(client discovery.DiscoveryInterface, resource schema.GroupVersionResource) (bool, error) {
|
||||
resources, err := client.ServerResourcesForGroupVersion(resource.GroupVersion().String())
|
||||
if apierrors.IsNotFound(err) {
|
||||
// entire group is missing
|
||||
return false, nil
|
||||
}
|
||||
if err != nil {
|
||||
// other errors error
|
||||
return false, fmt.Errorf("failed to discover supported resources: %v", err)
|
||||
}
|
||||
for _, serverResource := range resources.APIResources {
|
||||
if serverResource.Name == resource.Resource {
|
||||
return true, nil
|
||||
}
|
||||
}
|
||||
return false, nil
|
||||
}
|
||||
|
||||
func Warning(cmdErr io.Writer, newGeneratorName, oldGeneratorName string) {
|
||||
fmt.Fprintf(cmdErr, "WARNING: New generator %q specified, "+
|
||||
"but it isn't available. "+
|
||||
"Falling back to %q.\n",
|
||||
newGeneratorName,
|
||||
oldGeneratorName,
|
||||
)
|
||||
}
|
699
vendor/k8s.io/kubernetes/pkg/kubectl/cmd/util/helpers.go
generated
vendored
Normal file
699
vendor/k8s.io/kubernetes/pkg/kubectl/cmd/util/helpers.go
generated
vendored
Normal file
@@ -0,0 +1,699 @@
|
||||
/*
|
||||
Copyright 2014 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 (
|
||||
"bytes"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/url"
|
||||
"os"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/evanphx/json-patch"
|
||||
"github.com/golang/glog"
|
||||
"github.com/spf13/cobra"
|
||||
"github.com/spf13/pflag"
|
||||
|
||||
kerrors "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/yaml"
|
||||
"k8s.io/client-go/dynamic"
|
||||
"k8s.io/client-go/rest"
|
||||
"k8s.io/client-go/scale"
|
||||
"k8s.io/client-go/tools/clientcmd"
|
||||
"k8s.io/kubernetes/pkg/client/clientset_generated/internalclientset"
|
||||
"k8s.io/kubernetes/pkg/kubectl/genericclioptions"
|
||||
"k8s.io/kubernetes/pkg/kubectl/genericclioptions/resource"
|
||||
"k8s.io/kubernetes/pkg/printers"
|
||||
printersinternal "k8s.io/kubernetes/pkg/printers/internalversion"
|
||||
utilexec "k8s.io/utils/exec"
|
||||
)
|
||||
|
||||
const (
|
||||
ApplyAnnotationsFlag = "save-config"
|
||||
DefaultErrorExitCode = 1
|
||||
IncludeUninitializedFlag = "include-uninitialized"
|
||||
)
|
||||
|
||||
type debugError interface {
|
||||
DebugError() (msg string, args []interface{})
|
||||
}
|
||||
|
||||
// AddSourceToErr adds handleResourcePrefix and source string to error message.
|
||||
// verb is the string like "creating", "deleting" etc.
|
||||
// source is the filename or URL to the template file(*.json or *.yaml), or stdin to use to handle the resource.
|
||||
func AddSourceToErr(verb string, source string, err error) error {
|
||||
if source != "" {
|
||||
if statusError, ok := err.(kerrors.APIStatus); ok {
|
||||
status := statusError.Status()
|
||||
status.Message = fmt.Sprintf("error when %s %q: %v", verb, source, status.Message)
|
||||
return &kerrors.StatusError{ErrStatus: status}
|
||||
}
|
||||
return fmt.Errorf("error when %s %q: %v", verb, source, err)
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
var fatalErrHandler = fatal
|
||||
|
||||
// BehaviorOnFatal allows you to override the default behavior when a fatal
|
||||
// error occurs, which is to call os.Exit(code). You can pass 'panic' as a function
|
||||
// here if you prefer the panic() over os.Exit(1).
|
||||
func BehaviorOnFatal(f func(string, int)) {
|
||||
fatalErrHandler = f
|
||||
}
|
||||
|
||||
// DefaultBehaviorOnFatal allows you to undo any previous override. Useful in
|
||||
// tests.
|
||||
func DefaultBehaviorOnFatal() {
|
||||
fatalErrHandler = fatal
|
||||
}
|
||||
|
||||
// fatal prints the message (if provided) and then exits. If V(2) or greater,
|
||||
// glog.Fatal is invoked for extended information.
|
||||
func fatal(msg string, code int) {
|
||||
if glog.V(2) {
|
||||
glog.FatalDepth(2, msg)
|
||||
}
|
||||
if len(msg) > 0 {
|
||||
// add newline if needed
|
||||
if !strings.HasSuffix(msg, "\n") {
|
||||
msg += "\n"
|
||||
}
|
||||
fmt.Fprint(os.Stderr, msg)
|
||||
}
|
||||
os.Exit(code)
|
||||
}
|
||||
|
||||
// ErrExit may be passed to CheckError to instruct it to output nothing but exit with
|
||||
// status code 1.
|
||||
var ErrExit = fmt.Errorf("exit")
|
||||
|
||||
// CheckErr prints a user friendly error to STDERR and exits with a non-zero
|
||||
// exit code. Unrecognized errors will be printed with an "error: " prefix.
|
||||
//
|
||||
// This method is generic to the command in use and may be used by non-Kubectl
|
||||
// commands.
|
||||
func CheckErr(err error) {
|
||||
checkErr(err, fatalErrHandler)
|
||||
}
|
||||
|
||||
// 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)) {
|
||||
// unwrap aggregates of 1
|
||||
if agg, ok := err.(utilerrors.Aggregate); ok && len(agg.Errors()) == 1 {
|
||||
err = agg.Errors()[0]
|
||||
}
|
||||
|
||||
if err == nil {
|
||||
return
|
||||
}
|
||||
|
||||
switch {
|
||||
case err == ErrExit:
|
||||
handleErr("", DefaultErrorExitCode)
|
||||
case kerrors.IsInvalid(err):
|
||||
details := err.(*kerrors.StatusError).Status().Details
|
||||
s := fmt.Sprintf("The %s %q is invalid", details.Kind, details.Name)
|
||||
if len(details.Causes) > 0 {
|
||||
errs := statusCausesToAggrError(details.Causes)
|
||||
handleErr(MultilineError(s+": ", errs), DefaultErrorExitCode)
|
||||
} else {
|
||||
handleErr(s, DefaultErrorExitCode)
|
||||
}
|
||||
case clientcmd.IsConfigurationInvalid(err):
|
||||
handleErr(MultilineError("Error in configuration: ", err), DefaultErrorExitCode)
|
||||
default:
|
||||
switch err := err.(type) {
|
||||
case *meta.NoResourceMatchError:
|
||||
switch {
|
||||
case len(err.PartialResource.Group) > 0 && len(err.PartialResource.Version) > 0:
|
||||
handleErr(fmt.Sprintf("the server doesn't have a resource type %q in group %q and version %q", err.PartialResource.Resource, err.PartialResource.Group, err.PartialResource.Version), DefaultErrorExitCode)
|
||||
case len(err.PartialResource.Group) > 0:
|
||||
handleErr(fmt.Sprintf("the server doesn't have a resource type %q in group %q", err.PartialResource.Resource, err.PartialResource.Group), DefaultErrorExitCode)
|
||||
case len(err.PartialResource.Version) > 0:
|
||||
handleErr(fmt.Sprintf("the server doesn't have a resource type %q in version %q", err.PartialResource.Resource, err.PartialResource.Version), DefaultErrorExitCode)
|
||||
default:
|
||||
handleErr(fmt.Sprintf("the server doesn't have a resource type %q", err.PartialResource.Resource), DefaultErrorExitCode)
|
||||
}
|
||||
case utilerrors.Aggregate:
|
||||
handleErr(MultipleErrors(``, err.Errors()), DefaultErrorExitCode)
|
||||
case utilexec.ExitError:
|
||||
handleErr(err.Error(), err.ExitStatus())
|
||||
default: // for any other error type
|
||||
msg, ok := StandardErrorMessage(err)
|
||||
if !ok {
|
||||
msg = err.Error()
|
||||
if !strings.HasPrefix(msg, "error: ") {
|
||||
msg = fmt.Sprintf("error: %s", msg)
|
||||
}
|
||||
}
|
||||
handleErr(msg, DefaultErrorExitCode)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func statusCausesToAggrError(scs []metav1.StatusCause) utilerrors.Aggregate {
|
||||
errs := make([]error, 0, len(scs))
|
||||
errorMsgs := sets.NewString()
|
||||
for _, sc := range scs {
|
||||
// check for duplicate error messages and skip them
|
||||
msg := fmt.Sprintf("%s: %s", sc.Field, sc.Message)
|
||||
if errorMsgs.Has(msg) {
|
||||
continue
|
||||
}
|
||||
errorMsgs.Insert(msg)
|
||||
errs = append(errs, errors.New(msg))
|
||||
}
|
||||
return utilerrors.NewAggregate(errs)
|
||||
}
|
||||
|
||||
// StandardErrorMessage translates common errors into a human readable message, or returns
|
||||
// false if the error is not one of the recognized types. It may also log extended
|
||||
// information to glog.
|
||||
//
|
||||
// This method is generic to the command in use and may be used by non-Kubectl
|
||||
// commands.
|
||||
func StandardErrorMessage(err error) (string, bool) {
|
||||
if debugErr, ok := err.(debugError); ok {
|
||||
glog.V(4).Infof(debugErr.DebugError())
|
||||
}
|
||||
status, isStatus := err.(kerrors.APIStatus)
|
||||
switch {
|
||||
case isStatus:
|
||||
switch s := status.Status(); {
|
||||
case s.Reason == metav1.StatusReasonUnauthorized:
|
||||
return fmt.Sprintf("error: You must be logged in to the server (%s)", s.Message), true
|
||||
case len(s.Reason) > 0:
|
||||
return fmt.Sprintf("Error from server (%s): %s", s.Reason, err.Error()), true
|
||||
default:
|
||||
return fmt.Sprintf("Error from server: %s", err.Error()), true
|
||||
}
|
||||
case kerrors.IsUnexpectedObjectError(err):
|
||||
return fmt.Sprintf("Server returned an unexpected response: %s", err.Error()), true
|
||||
}
|
||||
switch t := err.(type) {
|
||||
case *url.Error:
|
||||
glog.V(4).Infof("Connection error: %s %s: %v", t.Op, t.URL, t.Err)
|
||||
switch {
|
||||
case strings.Contains(t.Err.Error(), "connection refused"):
|
||||
host := t.URL
|
||||
if server, err := url.Parse(t.URL); err == nil {
|
||||
host = server.Host
|
||||
}
|
||||
return fmt.Sprintf("The connection to the server %s was refused - did you specify the right host or port?", host), true
|
||||
}
|
||||
return fmt.Sprintf("Unable to connect to the server: %v", t.Err), true
|
||||
}
|
||||
return "", false
|
||||
}
|
||||
|
||||
// MultilineError returns a string representing an error that splits sub errors into their own
|
||||
// lines. The returned string will end with a newline.
|
||||
func MultilineError(prefix string, err error) string {
|
||||
if agg, ok := err.(utilerrors.Aggregate); ok {
|
||||
errs := utilerrors.Flatten(agg).Errors()
|
||||
buf := &bytes.Buffer{}
|
||||
switch len(errs) {
|
||||
case 0:
|
||||
return fmt.Sprintf("%s%v\n", prefix, err)
|
||||
case 1:
|
||||
return fmt.Sprintf("%s%v\n", prefix, messageForError(errs[0]))
|
||||
default:
|
||||
fmt.Fprintln(buf, prefix)
|
||||
for _, err := range errs {
|
||||
fmt.Fprintf(buf, "* %v\n", messageForError(err))
|
||||
}
|
||||
return buf.String()
|
||||
}
|
||||
}
|
||||
return fmt.Sprintf("%s%s\n", prefix, err)
|
||||
}
|
||||
|
||||
// PrintErrorWithCauses prints an error's kind, name, and each of the error's causes in a new line.
|
||||
// The returned string will end with a newline.
|
||||
// Returns true if a case exists to handle the error type, or false otherwise.
|
||||
func PrintErrorWithCauses(err error, errOut io.Writer) bool {
|
||||
switch t := err.(type) {
|
||||
case *kerrors.StatusError:
|
||||
errorDetails := t.Status().Details
|
||||
if errorDetails != nil {
|
||||
fmt.Fprintf(errOut, "error: %s %q is invalid\n\n", errorDetails.Kind, errorDetails.Name)
|
||||
for _, cause := range errorDetails.Causes {
|
||||
fmt.Fprintf(errOut, "* %s: %s\n", cause.Field, cause.Message)
|
||||
}
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
fmt.Fprintf(errOut, "error: %v\n", err)
|
||||
return false
|
||||
}
|
||||
|
||||
// MultipleErrors returns a newline delimited string containing
|
||||
// the prefix and referenced errors in standard form.
|
||||
func MultipleErrors(prefix string, errs []error) string {
|
||||
buf := &bytes.Buffer{}
|
||||
for _, err := range errs {
|
||||
fmt.Fprintf(buf, "%s%v\n", prefix, messageForError(err))
|
||||
}
|
||||
return buf.String()
|
||||
}
|
||||
|
||||
// messageForError returns the string representing the error.
|
||||
func messageForError(err error) string {
|
||||
msg, ok := StandardErrorMessage(err)
|
||||
if !ok {
|
||||
msg = err.Error()
|
||||
}
|
||||
return msg
|
||||
}
|
||||
|
||||
func UsageErrorf(cmd *cobra.Command, format string, args ...interface{}) error {
|
||||
msg := fmt.Sprintf(format, args...)
|
||||
return fmt.Errorf("%s\nSee '%s -h' for help and examples.", msg, cmd.CommandPath())
|
||||
}
|
||||
|
||||
func IsFilenameSliceEmpty(filenames []string) bool {
|
||||
return len(filenames) == 0
|
||||
}
|
||||
|
||||
func GetFlagString(cmd *cobra.Command, flag string) string {
|
||||
s, err := cmd.Flags().GetString(flag)
|
||||
if err != nil {
|
||||
glog.Fatalf("error accessing flag %s for command %s: %v", flag, cmd.Name(), err)
|
||||
}
|
||||
return s
|
||||
}
|
||||
|
||||
// GetFlagStringSlice can be used to accept multiple argument with flag repetition (e.g. -f arg1,arg2 -f arg3 ...)
|
||||
func GetFlagStringSlice(cmd *cobra.Command, flag string) []string {
|
||||
s, err := cmd.Flags().GetStringSlice(flag)
|
||||
if err != nil {
|
||||
glog.Fatalf("error accessing flag %s for command %s: %v", flag, cmd.Name(), err)
|
||||
}
|
||||
return s
|
||||
}
|
||||
|
||||
// GetFlagStringArray can be used to accept multiple argument with flag repetition (e.g. -f arg1 -f arg2 ...)
|
||||
func GetFlagStringArray(cmd *cobra.Command, flag string) []string {
|
||||
s, err := cmd.Flags().GetStringArray(flag)
|
||||
if err != nil {
|
||||
glog.Fatalf("err accessing flag %s for command %s: %v", flag, cmd.Name(), err)
|
||||
}
|
||||
return s
|
||||
}
|
||||
|
||||
func GetFlagBool(cmd *cobra.Command, flag string) bool {
|
||||
b, err := cmd.Flags().GetBool(flag)
|
||||
if err != nil {
|
||||
glog.Fatalf("error accessing flag %s for command %s: %v", flag, cmd.Name(), err)
|
||||
}
|
||||
return b
|
||||
}
|
||||
|
||||
// Assumes the flag has a default value.
|
||||
func GetFlagInt(cmd *cobra.Command, flag string) int {
|
||||
i, err := cmd.Flags().GetInt(flag)
|
||||
if err != nil {
|
||||
glog.Fatalf("error accessing flag %s for command %s: %v", flag, cmd.Name(), err)
|
||||
}
|
||||
return i
|
||||
}
|
||||
|
||||
// Assumes the flag has a default value.
|
||||
func GetFlagInt32(cmd *cobra.Command, flag string) int32 {
|
||||
i, err := cmd.Flags().GetInt32(flag)
|
||||
if err != nil {
|
||||
glog.Fatalf("error accessing flag %s for command %s: %v", flag, cmd.Name(), err)
|
||||
}
|
||||
return i
|
||||
}
|
||||
|
||||
// Assumes the flag has a default value.
|
||||
func GetFlagInt64(cmd *cobra.Command, flag string) int64 {
|
||||
i, err := cmd.Flags().GetInt64(flag)
|
||||
if err != nil {
|
||||
glog.Fatalf("error accessing flag %s for command %s: %v", flag, cmd.Name(), err)
|
||||
}
|
||||
return i
|
||||
}
|
||||
|
||||
func GetFlagDuration(cmd *cobra.Command, flag string) time.Duration {
|
||||
d, err := cmd.Flags().GetDuration(flag)
|
||||
if err != nil {
|
||||
glog.Fatalf("error accessing flag %s for command %s: %v", flag, cmd.Name(), err)
|
||||
}
|
||||
return d
|
||||
}
|
||||
|
||||
func GetPodRunningTimeoutFlag(cmd *cobra.Command) (time.Duration, error) {
|
||||
timeout := GetFlagDuration(cmd, "pod-running-timeout")
|
||||
if timeout <= 0 {
|
||||
return timeout, fmt.Errorf("--pod-running-timeout must be higher than zero")
|
||||
}
|
||||
return timeout, nil
|
||||
}
|
||||
|
||||
func AddValidateFlags(cmd *cobra.Command) {
|
||||
cmd.Flags().Bool("validate", true, "If true, use a schema to validate the input before sending it")
|
||||
}
|
||||
|
||||
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")
|
||||
}
|
||||
|
||||
func AddFilenameOptionFlags(cmd *cobra.Command, options *resource.FilenameOptions, usage string) {
|
||||
AddJsonFilenameFlag(cmd.Flags(), &options.Filenames, "Filename, directory, or URL to files "+usage)
|
||||
cmd.Flags().BoolVarP(&options.Recursive, "recursive", "R", options.Recursive, "Process the directory used in -f, --filename recursively. Useful when you want to manage related manifests organized within the same directory.")
|
||||
}
|
||||
|
||||
func AddJsonFilenameFlag(flags *pflag.FlagSet, value *[]string, usage string) {
|
||||
flags.StringSliceVarP(value, "filename", "f", *value, usage)
|
||||
annotations := make([]string, 0, len(resource.FileExtensions))
|
||||
for _, ext := range resource.FileExtensions {
|
||||
annotations = append(annotations, strings.TrimLeft(ext, "."))
|
||||
}
|
||||
flags.SetAnnotation("filename", cobra.BashCompFilenameExt, annotations)
|
||||
}
|
||||
|
||||
// AddDryRunFlag adds dry-run flag to a command. Usually used by mutations.
|
||||
func AddDryRunFlag(cmd *cobra.Command) {
|
||||
cmd.Flags().Bool("dry-run", false, "If true, only print the object that would be sent, without sending it.")
|
||||
}
|
||||
|
||||
func AddIncludeUninitializedFlag(cmd *cobra.Command) {
|
||||
cmd.Flags().Bool(IncludeUninitializedFlag, false, `If true, the kubectl command applies to uninitialized objects. If explicitly set to false, this flag overrides other flags that make the kubectl commands apply to uninitialized objects, e.g., "--all". Objects with empty metadata.initializers are regarded as initialized.`)
|
||||
}
|
||||
|
||||
func AddPodRunningTimeoutFlag(cmd *cobra.Command, defaultTimeout time.Duration) {
|
||||
cmd.Flags().Duration("pod-running-timeout", defaultTimeout, "The length of time (like 5s, 2m, or 3h, higher than zero) to wait until at least one pod is running")
|
||||
}
|
||||
|
||||
func AddApplyAnnotationFlags(cmd *cobra.Command) {
|
||||
cmd.Flags().Bool(ApplyAnnotationsFlag, false, "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.")
|
||||
}
|
||||
|
||||
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.")
|
||||
AddDryRunFlag(cmd)
|
||||
}
|
||||
|
||||
type ValidateOptions struct {
|
||||
EnableValidation bool
|
||||
}
|
||||
|
||||
// Merge requires JSON serialization
|
||||
// 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
|
||||
target, err := runtime.Encode(codec, dst)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
patched, err := jsonpatch.MergePatch(target, []byte(fragment))
|
||||
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 {
|
||||
f, err := os.OpenFile(filename, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
buffer := make([]byte, 1024)
|
||||
for {
|
||||
count, err := reader.Read(buffer)
|
||||
if err == io.EOF {
|
||||
break
|
||||
}
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
_, err = f.Write(buffer[:count])
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func GetDryRunFlag(cmd *cobra.Command) bool {
|
||||
return GetFlagBool(cmd, "dry-run")
|
||||
}
|
||||
|
||||
// GetResourcesAndPairs retrieves resources and "KEY=VALUE or KEY-" pair args from given args
|
||||
func GetResourcesAndPairs(args []string, pairType string) (resources []string, pairArgs []string, err error) {
|
||||
foundPair := false
|
||||
for _, s := range args {
|
||||
nonResource := (strings.Contains(s, "=") && s[0] != '=') || (strings.HasSuffix(s, "-") && s != "-")
|
||||
switch {
|
||||
case !foundPair && nonResource:
|
||||
foundPair = true
|
||||
fallthrough
|
||||
case foundPair && nonResource:
|
||||
pairArgs = append(pairArgs, s)
|
||||
case !foundPair && !nonResource:
|
||||
resources = append(resources, s)
|
||||
case foundPair && !nonResource:
|
||||
err = fmt.Errorf("all resources must be specified before %s changes: %s", pairType, s)
|
||||
return
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// ParsePairs retrieves new and remove pairs (if supportRemove is true) from "KEY=VALUE or KEY-" pair args
|
||||
func ParsePairs(pairArgs []string, pairType string, supportRemove bool) (newPairs map[string]string, removePairs []string, err error) {
|
||||
newPairs = map[string]string{}
|
||||
if supportRemove {
|
||||
removePairs = []string{}
|
||||
}
|
||||
var invalidBuf bytes.Buffer
|
||||
var invalidBufNonEmpty bool
|
||||
for _, pairArg := range pairArgs {
|
||||
if strings.Contains(pairArg, "=") && pairArg[0] != '=' {
|
||||
parts := strings.SplitN(pairArg, "=", 2)
|
||||
if len(parts) != 2 {
|
||||
if invalidBufNonEmpty {
|
||||
invalidBuf.WriteString(", ")
|
||||
}
|
||||
invalidBuf.WriteString(pairArg)
|
||||
invalidBufNonEmpty = true
|
||||
} else {
|
||||
newPairs[parts[0]] = parts[1]
|
||||
}
|
||||
} else if supportRemove && strings.HasSuffix(pairArg, "-") && pairArg != "-" {
|
||||
removePairs = append(removePairs, pairArg[:len(pairArg)-1])
|
||||
} else {
|
||||
if invalidBufNonEmpty {
|
||||
invalidBuf.WriteString(", ")
|
||||
}
|
||||
invalidBuf.WriteString(pairArg)
|
||||
invalidBufNonEmpty = true
|
||||
}
|
||||
}
|
||||
if invalidBufNonEmpty {
|
||||
err = fmt.Errorf("invalid %s format: %s", pairType, invalidBuf.String())
|
||||
return
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// IsSiblingCommandExists receives a pointer to a cobra command and a target string.
|
||||
// Returns true if the target string is found in the list of sibling commands.
|
||||
func IsSiblingCommandExists(cmd *cobra.Command, targetCmdName string) bool {
|
||||
for _, c := range cmd.Parent().Commands() {
|
||||
if c.Name() == targetCmdName {
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
// DefaultSubCommandRun prints a command's help string to the specified output if no
|
||||
// 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)
|
||||
RequireNoArguments(c, args)
|
||||
c.Help()
|
||||
CheckErr(ErrExit)
|
||||
}
|
||||
}
|
||||
|
||||
// RequireNoArguments exits with a usage error if extra arguments are provided.
|
||||
func RequireNoArguments(c *cobra.Command, args []string) {
|
||||
if len(args) > 0 {
|
||||
CheckErr(UsageErrorf(c, "unknown command %q", strings.Join(args, " ")))
|
||||
}
|
||||
}
|
||||
|
||||
// StripComments will transform a YAML file into JSON, thus dropping any comments
|
||||
// in it. Note that if the given file has a syntax error, the transformation will
|
||||
// fail and we will manually drop all comments from the file.
|
||||
func StripComments(file []byte) []byte {
|
||||
stripped := file
|
||||
stripped, err := yaml.ToJSON(stripped)
|
||||
if err != nil {
|
||||
stripped = ManualStrip(file)
|
||||
}
|
||||
return stripped
|
||||
}
|
||||
|
||||
// ManualStrip is used for dropping comments from a YAML file
|
||||
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("#")) {
|
||||
continue
|
||||
}
|
||||
stripped = append(stripped, line...)
|
||||
if i < len(lines)-1 {
|
||||
stripped = append(stripped, '\n')
|
||||
}
|
||||
}
|
||||
return stripped
|
||||
}
|
||||
|
||||
// ShouldIncludeUninitialized identifies whether to include uninitialized objects.
|
||||
// includeUninitialized is the default value.
|
||||
// Assume we can parse `all` and `selector` from cmd.
|
||||
func ShouldIncludeUninitialized(cmd *cobra.Command, includeUninitialized bool) bool {
|
||||
shouldIncludeUninitialized := includeUninitialized
|
||||
if cmd.Flags().Lookup("all") != nil && GetFlagBool(cmd, "all") {
|
||||
// include the uninitialized objects by default
|
||||
// unless explicitly set --include-uninitialized=false
|
||||
shouldIncludeUninitialized = true
|
||||
}
|
||||
if cmd.Flags().Lookup("selector") != nil && GetFlagString(cmd, "selector") != "" {
|
||||
// does not include the uninitialized objects by default
|
||||
// unless explicitly set --include-uninitialized=true
|
||||
shouldIncludeUninitialized = false
|
||||
}
|
||||
if cmd.Flags().Changed(IncludeUninitializedFlag) {
|
||||
// get explicit value
|
||||
shouldIncludeUninitialized = GetFlagBool(cmd, IncludeUninitializedFlag)
|
||||
}
|
||||
return shouldIncludeUninitialized
|
||||
}
|
||||
|
||||
// DescriberFunc gives a way to display the specified RESTMapping type
|
||||
type DescriberFunc func(restClientGetter genericclioptions.RESTClientGetter, mapping *meta.RESTMapping) (printers.Describer, error)
|
||||
|
||||
// DescriberFn gives a way to easily override the function for unit testing if needed
|
||||
var DescriberFn DescriberFunc = describer
|
||||
|
||||
// Returns a Describer for displaying the specified RESTMapping type or an error.
|
||||
func describer(restClientGetter genericclioptions.RESTClientGetter, mapping *meta.RESTMapping) (printers.Describer, error) {
|
||||
clientConfig, err := restClientGetter.ToRESTConfig()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
// try to get a describer
|
||||
if describer, ok := printersinternal.DescriberFor(mapping.GroupVersionKind.GroupKind(), clientConfig); ok {
|
||||
return describer, nil
|
||||
}
|
||||
// if this is a kind we don't have a describer for yet, go generic if possible
|
||||
if genericDescriber, genericErr := genericDescriber(restClientGetter, mapping); genericErr == nil {
|
||||
return genericDescriber, nil
|
||||
}
|
||||
// otherwise return an unregistered error
|
||||
return nil, fmt.Errorf("no description has been implemented for %s", mapping.GroupVersionKind.String())
|
||||
}
|
||||
|
||||
// helper function to make a generic describer, or return an error
|
||||
func genericDescriber(restClientGetter genericclioptions.RESTClientGetter, mapping *meta.RESTMapping) (printers.Describer, error) {
|
||||
clientConfig, err := restClientGetter.ToRESTConfig()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// used to fetch the resource
|
||||
dynamicClient, err := dynamic.NewForConfig(clientConfig)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// used to get events for the resource
|
||||
clientSet, err := internalclientset.NewForConfig(clientConfig)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
eventsClient := clientSet.Core()
|
||||
return printersinternal.GenericDescriberFor(mapping, dynamicClient, eventsClient), nil
|
||||
}
|
||||
|
||||
// ScaleClientFunc provides a ScalesGetter
|
||||
type ScaleClientFunc func(genericclioptions.RESTClientGetter) (scale.ScalesGetter, error)
|
||||
|
||||
// ScaleClientFn gives a way to easily override the function for unit testing if needed.
|
||||
var ScaleClientFn ScaleClientFunc = scaleClient
|
||||
|
||||
// scaleClient gives you back scale getter
|
||||
func scaleClient(restClientGetter genericclioptions.RESTClientGetter) (scale.ScalesGetter, error) {
|
||||
discoveryClient, err := restClientGetter.ToDiscoveryClient()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
clientConfig, err := restClientGetter.ToRESTConfig()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
setKubernetesDefaults(clientConfig)
|
||||
restClient, err := rest.RESTClientFor(clientConfig)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
resolver := scale.NewDiscoveryScaleKindResolver(discoveryClient)
|
||||
mapper, err := restClientGetter.ToRESTMapper()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return scale.New(restClient, mapper, dynamic.LegacyAPIPathResolverFunc, resolver), nil
|
||||
}
|
307
vendor/k8s.io/kubernetes/pkg/kubectl/cmd/util/helpers_test.go
generated
vendored
Normal file
307
vendor/k8s.io/kubernetes/pkg/kubectl/cmd/util/helpers_test.go
generated
vendored
Normal file
@@ -0,0 +1,307 @@
|
||||
/*
|
||||
Copyright 2014 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"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"strings"
|
||||
"syscall"
|
||||
"testing"
|
||||
|
||||
apiequality "k8s.io/apimachinery/pkg/api/equality"
|
||||
"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"
|
||||
"k8s.io/apimachinery/pkg/runtime/schema"
|
||||
"k8s.io/apimachinery/pkg/util/validation/field"
|
||||
"k8s.io/kubernetes/pkg/api/testapi"
|
||||
apitesting "k8s.io/kubernetes/pkg/api/testing"
|
||||
api "k8s.io/kubernetes/pkg/apis/core"
|
||||
"k8s.io/utils/exec"
|
||||
)
|
||||
|
||||
func TestMerge(t *testing.T) {
|
||||
grace := int64(30)
|
||||
tests := []struct {
|
||||
obj runtime.Object
|
||||
fragment string
|
||||
expected runtime.Object
|
||||
expectErr bool
|
||||
}{
|
||||
{
|
||||
obj: &api.Pod{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Name: "foo",
|
||||
},
|
||||
},
|
||||
fragment: fmt.Sprintf(`{ "apiVersion": "%s" }`, "v1"),
|
||||
expected: &api.Pod{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Name: "foo",
|
||||
},
|
||||
Spec: apitesting.DeepEqualSafePodSpec(),
|
||||
},
|
||||
},
|
||||
/* TODO: uncomment this test once Merge is updated to use
|
||||
strategic-merge-patch. See #8449.
|
||||
{
|
||||
obj: &api.Pod{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Name: "foo",
|
||||
},
|
||||
Spec: api.PodSpec{
|
||||
Containers: []api.Container{
|
||||
api.Container{
|
||||
Name: "c1",
|
||||
Image: "red-image",
|
||||
},
|
||||
api.Container{
|
||||
Name: "c2",
|
||||
Image: "blue-image",
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
fragment: fmt.Sprintf(`{ "apiVersion": "%s", "spec": { "containers": [ { "name": "c1", "image": "green-image" } ] } }`, schema.GroupVersion{Group:"", Version: "v1"}.String()),
|
||||
expected: &api.Pod{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Name: "foo",
|
||||
},
|
||||
Spec: api.PodSpec{
|
||||
Containers: []api.Container{
|
||||
api.Container{
|
||||
Name: "c1",
|
||||
Image: "green-image",
|
||||
},
|
||||
api.Container{
|
||||
Name: "c2",
|
||||
Image: "blue-image",
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
}, */
|
||||
{
|
||||
obj: &api.Pod{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Name: "foo",
|
||||
},
|
||||
},
|
||||
fragment: fmt.Sprintf(`{ "apiVersion": "%s", "spec": { "volumes": [ {"name": "v1"}, {"name": "v2"} ] } }`, "v1"),
|
||||
expected: &api.Pod{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Name: "foo",
|
||||
},
|
||||
Spec: api.PodSpec{
|
||||
Volumes: []api.Volume{
|
||||
{
|
||||
Name: "v1",
|
||||
VolumeSource: api.VolumeSource{EmptyDir: &api.EmptyDirVolumeSource{}},
|
||||
},
|
||||
{
|
||||
Name: "v2",
|
||||
VolumeSource: api.VolumeSource{EmptyDir: &api.EmptyDirVolumeSource{}},
|
||||
},
|
||||
},
|
||||
RestartPolicy: api.RestartPolicyAlways,
|
||||
DNSPolicy: api.DNSClusterFirst,
|
||||
TerminationGracePeriodSeconds: &grace,
|
||||
SecurityContext: &api.PodSecurityContext{},
|
||||
SchedulerName: api.DefaultSchedulerName,
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
obj: &api.Pod{},
|
||||
fragment: "invalid json",
|
||||
expected: &api.Pod{},
|
||||
expectErr: true,
|
||||
},
|
||||
{
|
||||
obj: &api.Service{},
|
||||
fragment: `{ "apiVersion": "badVersion" }`,
|
||||
expectErr: true,
|
||||
},
|
||||
{
|
||||
obj: &api.Service{
|
||||
Spec: api.ServiceSpec{},
|
||||
},
|
||||
fragment: fmt.Sprintf(`{ "apiVersion": "%s", "spec": { "ports": [ { "port": 0 } ] } }`, "v1"),
|
||||
expected: &api.Service{
|
||||
Spec: api.ServiceSpec{
|
||||
SessionAffinity: "None",
|
||||
Type: api.ServiceTypeClusterIP,
|
||||
Ports: []api.ServicePort{
|
||||
{
|
||||
Protocol: api.ProtocolTCP,
|
||||
Port: 0,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
obj: &api.Service{
|
||||
Spec: api.ServiceSpec{
|
||||
Selector: map[string]string{
|
||||
"version": "v1",
|
||||
},
|
||||
},
|
||||
},
|
||||
fragment: fmt.Sprintf(`{ "apiVersion": "%s", "spec": { "selector": { "version": "v2" } } }`, "v1"),
|
||||
expected: &api.Service{
|
||||
Spec: api.ServiceSpec{
|
||||
SessionAffinity: "None",
|
||||
Type: api.ServiceTypeClusterIP,
|
||||
Selector: map[string]string{
|
||||
"version": "v2",
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for i, test := range tests {
|
||||
out, err := Merge(testapi.Default.Codec(), test.obj, test.fragment)
|
||||
if !test.expectErr {
|
||||
if err != nil {
|
||||
t.Errorf("testcase[%d], unexpected error: %v", i, err)
|
||||
} else if !apiequality.Semantic.DeepEqual(out, test.expected) {
|
||||
t.Errorf("\n\ntestcase[%d]\nexpected:\n%+v\nsaw:\n%+v", i, test.expected, out)
|
||||
}
|
||||
}
|
||||
if test.expectErr && err == nil {
|
||||
t.Errorf("testcase[%d], unexpected non-error", i)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
type checkErrTestCase struct {
|
||||
err error
|
||||
expectedErr string
|
||||
expectedCode int
|
||||
}
|
||||
|
||||
func TestCheckInvalidErr(t *testing.T) {
|
||||
testCheckError(t, []checkErrTestCase{
|
||||
{
|
||||
errors.NewInvalid(api.Kind("Invalid1"), "invalidation", field.ErrorList{field.Invalid(field.NewPath("field"), "single", "details")}),
|
||||
"The Invalid1 \"invalidation\" is invalid: field: Invalid value: \"single\": details\n",
|
||||
DefaultErrorExitCode,
|
||||
},
|
||||
{
|
||||
errors.NewInvalid(api.Kind("Invalid2"), "invalidation", field.ErrorList{field.Invalid(field.NewPath("field1"), "multi1", "details"), field.Invalid(field.NewPath("field2"), "multi2", "details")}),
|
||||
"The Invalid2 \"invalidation\" is invalid: \n* field1: Invalid value: \"multi1\": details\n* field2: Invalid value: \"multi2\": details\n",
|
||||
DefaultErrorExitCode,
|
||||
},
|
||||
{
|
||||
errors.NewInvalid(api.Kind("Invalid3"), "invalidation", field.ErrorList{}),
|
||||
"The Invalid3 \"invalidation\" is invalid",
|
||||
DefaultErrorExitCode,
|
||||
},
|
||||
{
|
||||
errors.NewInvalid(api.Kind("Invalid4"), "invalidation", field.ErrorList{field.Invalid(field.NewPath("field4"), "multi4", "details"), field.Invalid(field.NewPath("field4"), "multi4", "details")}),
|
||||
"The Invalid4 \"invalidation\" is invalid: field4: Invalid value: \"multi4\": details\n",
|
||||
DefaultErrorExitCode,
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
func TestCheckNoResourceMatchError(t *testing.T) {
|
||||
testCheckError(t, []checkErrTestCase{
|
||||
{
|
||||
&meta.NoResourceMatchError{PartialResource: schema.GroupVersionResource{Resource: "foo"}},
|
||||
`the server doesn't have a resource type "foo"`,
|
||||
DefaultErrorExitCode,
|
||||
},
|
||||
{
|
||||
&meta.NoResourceMatchError{PartialResource: schema.GroupVersionResource{Version: "theversion", Resource: "foo"}},
|
||||
`the server doesn't have a resource type "foo" in version "theversion"`,
|
||||
DefaultErrorExitCode,
|
||||
},
|
||||
{
|
||||
&meta.NoResourceMatchError{PartialResource: schema.GroupVersionResource{Group: "thegroup", Version: "theversion", Resource: "foo"}},
|
||||
`the server doesn't have a resource type "foo" in group "thegroup" and version "theversion"`,
|
||||
DefaultErrorExitCode,
|
||||
},
|
||||
{
|
||||
&meta.NoResourceMatchError{PartialResource: schema.GroupVersionResource{Group: "thegroup", Resource: "foo"}},
|
||||
`the server doesn't have a resource type "foo" in group "thegroup"`,
|
||||
DefaultErrorExitCode,
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
func TestCheckExitError(t *testing.T) {
|
||||
testCheckError(t, []checkErrTestCase{
|
||||
{
|
||||
exec.CodeExitError{Err: fmt.Errorf("pod foo/bar terminated"), Code: 42},
|
||||
"pod foo/bar terminated",
|
||||
42,
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
func testCheckError(t *testing.T, tests []checkErrTestCase) {
|
||||
var errReturned string
|
||||
var codeReturned int
|
||||
errHandle := func(err string, code int) {
|
||||
errReturned = err
|
||||
codeReturned = code
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
checkErr(test.err, errHandle)
|
||||
|
||||
if errReturned != test.expectedErr {
|
||||
t.Fatalf("Got: %s, expected: %s", errReturned, test.expectedErr)
|
||||
}
|
||||
if codeReturned != test.expectedCode {
|
||||
t.Fatalf("Got: %d, expected: %d", codeReturned, test.expectedCode)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestDumpReaderToFile(t *testing.T) {
|
||||
testString := "TEST STRING"
|
||||
tempFile, err := ioutil.TempFile("", "hlpers_test_dump_")
|
||||
if err != nil {
|
||||
t.Errorf("unexpected error setting up a temporary file %v", err)
|
||||
}
|
||||
defer syscall.Unlink(tempFile.Name())
|
||||
defer tempFile.Close()
|
||||
defer func() {
|
||||
if !t.Failed() {
|
||||
os.Remove(tempFile.Name())
|
||||
}
|
||||
}()
|
||||
err = DumpReaderToFile(strings.NewReader(testString), tempFile.Name())
|
||||
if err != nil {
|
||||
t.Errorf("error in DumpReaderToFile: %v", err)
|
||||
}
|
||||
data, err := ioutil.ReadFile(tempFile.Name())
|
||||
if err != nil {
|
||||
t.Errorf("error when reading %s: %v", tempFile.Name(), err)
|
||||
}
|
||||
stringData := string(data)
|
||||
if stringData != testString {
|
||||
t.Fatalf("Wrong file content %s != %s", testString, stringData)
|
||||
}
|
||||
}
|
126
vendor/k8s.io/kubernetes/pkg/kubectl/cmd/util/kubectl_match_version.go
generated
vendored
Normal file
126
vendor/k8s.io/kubernetes/pkg/kubectl/cmd/util/kubectl_match_version.go
generated
vendored
Normal file
@@ -0,0 +1,126 @@
|
||||
/*
|
||||
Copyright 2018 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 (
|
||||
"sync"
|
||||
|
||||
"github.com/spf13/pflag"
|
||||
|
||||
"k8s.io/apimachinery/pkg/api/meta"
|
||||
"k8s.io/apimachinery/pkg/runtime/schema"
|
||||
"k8s.io/client-go/discovery"
|
||||
"k8s.io/client-go/rest"
|
||||
"k8s.io/client-go/tools/clientcmd"
|
||||
|
||||
"k8s.io/kubernetes/pkg/api/legacyscheme"
|
||||
"k8s.io/kubernetes/pkg/kubectl/genericclioptions"
|
||||
"k8s.io/kubernetes/pkg/version"
|
||||
)
|
||||
|
||||
const (
|
||||
flagMatchBinaryVersion = "match-server-version"
|
||||
)
|
||||
|
||||
// MatchVersionFlags is for setting the "match server version" function.
|
||||
type MatchVersionFlags struct {
|
||||
Delegate genericclioptions.RESTClientGetter
|
||||
|
||||
RequireMatchedServerVersion bool
|
||||
checkServerVersion sync.Once
|
||||
matchesServerVersionErr error
|
||||
}
|
||||
|
||||
var _ genericclioptions.RESTClientGetter = &MatchVersionFlags{}
|
||||
|
||||
func (f *MatchVersionFlags) checkMatchingServerVersion() error {
|
||||
f.checkServerVersion.Do(func() {
|
||||
if !f.RequireMatchedServerVersion {
|
||||
return
|
||||
}
|
||||
discoveryClient, err := f.Delegate.ToDiscoveryClient()
|
||||
if err != nil {
|
||||
f.matchesServerVersionErr = err
|
||||
return
|
||||
}
|
||||
f.matchesServerVersionErr = discovery.MatchesServerVersion(version.Get(), discoveryClient)
|
||||
})
|
||||
|
||||
return f.matchesServerVersionErr
|
||||
}
|
||||
|
||||
// ToRESTConfig implements RESTClientGetter.
|
||||
// Returns a REST client configuration based on a provided path
|
||||
// to a .kubeconfig file, loading rules, and config flag overrides.
|
||||
// Expects the AddFlags method to have been called.
|
||||
func (f *MatchVersionFlags) ToRESTConfig() (*rest.Config, error) {
|
||||
if err := f.checkMatchingServerVersion(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
clientConfig, err := f.Delegate.ToRESTConfig()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
// TODO we should not have to do this. It smacks of something going wrong.
|
||||
setKubernetesDefaults(clientConfig)
|
||||
return clientConfig, nil
|
||||
}
|
||||
|
||||
func (f *MatchVersionFlags) ToRawKubeConfigLoader() clientcmd.ClientConfig {
|
||||
return f.Delegate.ToRawKubeConfigLoader()
|
||||
}
|
||||
|
||||
func (f *MatchVersionFlags) ToDiscoveryClient() (discovery.CachedDiscoveryInterface, error) {
|
||||
if err := f.checkMatchingServerVersion(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return f.Delegate.ToDiscoveryClient()
|
||||
}
|
||||
|
||||
// RESTMapper returns a mapper.
|
||||
func (f *MatchVersionFlags) ToRESTMapper() (meta.RESTMapper, error) {
|
||||
if err := f.checkMatchingServerVersion(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return f.Delegate.ToRESTMapper()
|
||||
}
|
||||
|
||||
func (f *MatchVersionFlags) AddFlags(flags *pflag.FlagSet) {
|
||||
flags.BoolVar(&f.RequireMatchedServerVersion, flagMatchBinaryVersion, f.RequireMatchedServerVersion, "Require server version to match client version")
|
||||
}
|
||||
|
||||
func NewMatchVersionFlags(delegate genericclioptions.RESTClientGetter) *MatchVersionFlags {
|
||||
return &MatchVersionFlags{
|
||||
Delegate: delegate,
|
||||
}
|
||||
}
|
||||
|
||||
// setKubernetesDefaults sets default values on the provided client config for accessing the
|
||||
// Kubernetes API or returns an error if any of the defaults are impossible or invalid.
|
||||
// TODO this isn't what we want. Each clientset should be setting defaults as it sees fit.
|
||||
func setKubernetesDefaults(config *rest.Config) error {
|
||||
// TODO remove this hack. This is allowing the GetOptions to be serialized.
|
||||
config.GroupVersion = &schema.GroupVersion{Group: "", Version: "v1"}
|
||||
|
||||
if config.APIPath == "" {
|
||||
config.APIPath = "/api"
|
||||
}
|
||||
if config.NegotiatedSerializer == nil {
|
||||
config.NegotiatedSerializer = legacyscheme.Codecs
|
||||
}
|
||||
return rest.SetKubernetesDefaults(config)
|
||||
}
|
69
vendor/k8s.io/kubernetes/pkg/kubectl/cmd/util/openapi/BUILD
generated
vendored
Normal file
69
vendor/k8s.io/kubernetes/pkg/kubectl/cmd/util/openapi/BUILD
generated
vendored
Normal file
@@ -0,0 +1,69 @@
|
||||
package(default_visibility = ["//visibility:public"])
|
||||
|
||||
load(
|
||||
"@io_bazel_rules_go//go:def.bzl",
|
||||
"go_library",
|
||||
"go_test",
|
||||
)
|
||||
|
||||
go_library(
|
||||
name = "go_default_library",
|
||||
srcs = [
|
||||
"doc.go",
|
||||
"extensions.go",
|
||||
"openapi.go",
|
||||
"openapi_getter.go",
|
||||
],
|
||||
importpath = "k8s.io/kubernetes/pkg/kubectl/cmd/util/openapi",
|
||||
deps = [
|
||||
"//vendor/github.com/go-openapi/spec:go_default_library",
|
||||
"//vendor/github.com/googleapis/gnostic/OpenAPIv2:go_default_library",
|
||||
"//vendor/k8s.io/apimachinery/pkg/runtime/schema:go_default_library",
|
||||
"//vendor/k8s.io/client-go/discovery:go_default_library",
|
||||
"//vendor/k8s.io/kube-openapi/pkg/util/proto:go_default_library",
|
||||
],
|
||||
)
|
||||
|
||||
go_test(
|
||||
name = "go_default_xtest",
|
||||
size = "small",
|
||||
srcs = [
|
||||
"openapi_getter_test.go",
|
||||
"openapi_suite_test.go",
|
||||
"openapi_test.go",
|
||||
],
|
||||
data = ["//api/openapi-spec:swagger-spec"],
|
||||
deps = [
|
||||
":go_default_library",
|
||||
"//vendor/github.com/googleapis/gnostic/OpenAPIv2:go_default_library",
|
||||
"//vendor/github.com/onsi/ginkgo:go_default_library",
|
||||
"//vendor/github.com/onsi/ginkgo/config:go_default_library",
|
||||
"//vendor/github.com/onsi/ginkgo/types:go_default_library",
|
||||
"//vendor/github.com/onsi/gomega:go_default_library",
|
||||
"//vendor/k8s.io/apimachinery/pkg/runtime/schema:go_default_library",
|
||||
"//vendor/k8s.io/kube-openapi/pkg/util/proto:go_default_library",
|
||||
"//vendor/k8s.io/kube-openapi/pkg/util/proto/testing:go_default_library",
|
||||
],
|
||||
)
|
||||
|
||||
filegroup(
|
||||
name = "package-srcs",
|
||||
srcs = glob(["**"]),
|
||||
tags = ["automanaged"],
|
||||
visibility = ["//visibility:private"],
|
||||
)
|
||||
|
||||
filegroup(
|
||||
name = "all-srcs",
|
||||
srcs = [
|
||||
":package-srcs",
|
||||
"//pkg/kubectl/cmd/util/openapi/testing:all-srcs",
|
||||
"//pkg/kubectl/cmd/util/openapi/validation:all-srcs",
|
||||
],
|
||||
tags = ["automanaged"],
|
||||
)
|
||||
|
||||
filegroup(
|
||||
name = "testdata",
|
||||
srcs = glob(["testdata/*"]),
|
||||
)
|
4
vendor/k8s.io/kubernetes/pkg/kubectl/cmd/util/openapi/OWNERS
generated
vendored
Normal file
4
vendor/k8s.io/kubernetes/pkg/kubectl/cmd/util/openapi/OWNERS
generated
vendored
Normal file
@@ -0,0 +1,4 @@
|
||||
approvers:
|
||||
- apelisse
|
||||
reviewers:
|
||||
- apelisse
|
21
vendor/k8s.io/kubernetes/pkg/kubectl/cmd/util/openapi/doc.go
generated
vendored
Normal file
21
vendor/k8s.io/kubernetes/pkg/kubectl/cmd/util/openapi/doc.go
generated
vendored
Normal file
@@ -0,0 +1,21 @@
|
||||
/*
|
||||
Copyright 2017 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 openapi is a collection of libraries for fetching the openapi spec
|
||||
// from a Kubernetes server and then indexing the type definitions.
|
||||
// The openapi spec contains the object model definitions and extensions metadata
|
||||
// such as the patchStrategy and patchMergeKey for creating patches.
|
||||
package openapi
|
26
vendor/k8s.io/kubernetes/pkg/kubectl/cmd/util/openapi/extensions.go
generated
vendored
Normal file
26
vendor/k8s.io/kubernetes/pkg/kubectl/cmd/util/openapi/extensions.go
generated
vendored
Normal file
@@ -0,0 +1,26 @@
|
||||
/*
|
||||
Copyright 2017 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 openapi
|
||||
|
||||
import "github.com/go-openapi/spec"
|
||||
|
||||
const PrintColumnsKey = "x-kubernetes-print-columns"
|
||||
|
||||
// GetPrintColumns looks for the open API extension for the display columns.
|
||||
func GetPrintColumns(extensions spec.Extensions) (string, bool) {
|
||||
return extensions.GetString(PrintColumnsKey)
|
||||
}
|
127
vendor/k8s.io/kubernetes/pkg/kubectl/cmd/util/openapi/openapi.go
generated
vendored
Normal file
127
vendor/k8s.io/kubernetes/pkg/kubectl/cmd/util/openapi/openapi.go
generated
vendored
Normal file
@@ -0,0 +1,127 @@
|
||||
/*
|
||||
Copyright 2017 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 openapi
|
||||
|
||||
import (
|
||||
openapi_v2 "github.com/googleapis/gnostic/OpenAPIv2"
|
||||
|
||||
"k8s.io/apimachinery/pkg/runtime/schema"
|
||||
"k8s.io/kube-openapi/pkg/util/proto"
|
||||
)
|
||||
|
||||
// Resources interface describe a resources provider, that can give you
|
||||
// resource based on group-version-kind.
|
||||
type Resources interface {
|
||||
LookupResource(gvk schema.GroupVersionKind) proto.Schema
|
||||
}
|
||||
|
||||
// groupVersionKindExtensionKey is the key used to lookup the
|
||||
// GroupVersionKind value for an object definition from the
|
||||
// definition's "extensions" map.
|
||||
const groupVersionKindExtensionKey = "x-kubernetes-group-version-kind"
|
||||
|
||||
// document is an implementation of `Resources`. It looks for
|
||||
// resources in an openapi Schema.
|
||||
type document struct {
|
||||
// Maps gvk to model name
|
||||
resources map[schema.GroupVersionKind]string
|
||||
models proto.Models
|
||||
}
|
||||
|
||||
var _ Resources = &document{}
|
||||
|
||||
func NewOpenAPIData(doc *openapi_v2.Document) (Resources, error) {
|
||||
models, err := proto.NewOpenAPIData(doc)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
resources := map[schema.GroupVersionKind]string{}
|
||||
for _, modelName := range models.ListModels() {
|
||||
model := models.LookupModel(modelName)
|
||||
if model == nil {
|
||||
panic("ListModels returns a model that can't be looked-up.")
|
||||
}
|
||||
gvkList := parseGroupVersionKind(model)
|
||||
for _, gvk := range gvkList {
|
||||
if len(gvk.Kind) > 0 {
|
||||
resources[gvk] = modelName
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return &document{
|
||||
resources: resources,
|
||||
models: models,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (d *document) LookupResource(gvk schema.GroupVersionKind) proto.Schema {
|
||||
modelName, found := d.resources[gvk]
|
||||
if !found {
|
||||
return nil
|
||||
}
|
||||
return d.models.LookupModel(modelName)
|
||||
}
|
||||
|
||||
// Get and parse GroupVersionKind from the extension. Returns empty if it doesn't have one.
|
||||
func parseGroupVersionKind(s proto.Schema) []schema.GroupVersionKind {
|
||||
extensions := s.GetExtensions()
|
||||
|
||||
gvkListResult := []schema.GroupVersionKind{}
|
||||
|
||||
// Get the extensions
|
||||
gvkExtension, ok := extensions[groupVersionKindExtensionKey]
|
||||
if !ok {
|
||||
return []schema.GroupVersionKind{}
|
||||
}
|
||||
|
||||
// gvk extension must be a list of at least 1 element.
|
||||
gvkList, ok := gvkExtension.([]interface{})
|
||||
if !ok {
|
||||
return []schema.GroupVersionKind{}
|
||||
}
|
||||
|
||||
for _, gvk := range gvkList {
|
||||
// gvk extension list must be a map with group, version, and
|
||||
// kind fields
|
||||
gvkMap, ok := gvk.(map[interface{}]interface{})
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
group, ok := gvkMap["group"].(string)
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
version, ok := gvkMap["version"].(string)
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
kind, ok := gvkMap["kind"].(string)
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
|
||||
gvkListResult = append(gvkListResult, schema.GroupVersionKind{
|
||||
Group: group,
|
||||
Version: version,
|
||||
Kind: kind,
|
||||
})
|
||||
}
|
||||
|
||||
return gvkListResult
|
||||
}
|
65
vendor/k8s.io/kubernetes/pkg/kubectl/cmd/util/openapi/openapi_getter.go
generated
vendored
Normal file
65
vendor/k8s.io/kubernetes/pkg/kubectl/cmd/util/openapi/openapi_getter.go
generated
vendored
Normal file
@@ -0,0 +1,65 @@
|
||||
/*
|
||||
Copyright 2017 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 openapi
|
||||
|
||||
import (
|
||||
"sync"
|
||||
|
||||
"k8s.io/client-go/discovery"
|
||||
)
|
||||
|
||||
// synchronizedOpenAPIGetter fetches the openapi schema once and then caches it in memory
|
||||
type synchronizedOpenAPIGetter struct {
|
||||
// Cached results
|
||||
sync.Once
|
||||
openAPISchema Resources
|
||||
err error
|
||||
|
||||
openAPIClient discovery.OpenAPISchemaInterface
|
||||
}
|
||||
|
||||
var _ Getter = &synchronizedOpenAPIGetter{}
|
||||
|
||||
// Getter is an interface for fetching openapi specs and parsing them into an Resources struct
|
||||
type Getter interface {
|
||||
// OpenAPIData returns the parsed OpenAPIData
|
||||
Get() (Resources, error)
|
||||
}
|
||||
|
||||
// NewOpenAPIGetter returns an object to return OpenAPIDatas which reads
|
||||
// from a server, and then stores in memory for subsequent invocations
|
||||
func NewOpenAPIGetter(openAPIClient discovery.OpenAPISchemaInterface) Getter {
|
||||
return &synchronizedOpenAPIGetter{
|
||||
openAPIClient: openAPIClient,
|
||||
}
|
||||
}
|
||||
|
||||
// Resources implements Getter
|
||||
func (g *synchronizedOpenAPIGetter) Get() (Resources, error) {
|
||||
g.Do(func() {
|
||||
s, err := g.openAPIClient.OpenAPISchema()
|
||||
if err != nil {
|
||||
g.err = err
|
||||
return
|
||||
}
|
||||
|
||||
g.openAPISchema, g.err = NewOpenAPIData(s)
|
||||
})
|
||||
|
||||
// Return the save result
|
||||
return g.openAPISchema, g.err
|
||||
}
|
86
vendor/k8s.io/kubernetes/pkg/kubectl/cmd/util/openapi/openapi_getter_test.go
generated
vendored
Normal file
86
vendor/k8s.io/kubernetes/pkg/kubectl/cmd/util/openapi/openapi_getter_test.go
generated
vendored
Normal file
@@ -0,0 +1,86 @@
|
||||
/*
|
||||
Copyright 2017 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 openapi_test
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
openapi_v2 "github.com/googleapis/gnostic/OpenAPIv2"
|
||||
. "github.com/onsi/ginkgo"
|
||||
. "github.com/onsi/gomega"
|
||||
|
||||
"k8s.io/kubernetes/pkg/kubectl/cmd/util/openapi"
|
||||
)
|
||||
|
||||
// FakeCounter returns a "null" document and the specified error. It
|
||||
// also counts how many times the OpenAPISchema method has been called.
|
||||
type FakeCounter struct {
|
||||
Calls int
|
||||
Err error
|
||||
}
|
||||
|
||||
func (f *FakeCounter) OpenAPISchema() (*openapi_v2.Document, error) {
|
||||
f.Calls = f.Calls + 1
|
||||
return nil, f.Err
|
||||
}
|
||||
|
||||
var _ = Describe("Getting the Resources", func() {
|
||||
var client FakeCounter
|
||||
var instance openapi.Getter
|
||||
var expectedData openapi.Resources
|
||||
|
||||
BeforeEach(func() {
|
||||
client = FakeCounter{}
|
||||
instance = openapi.NewOpenAPIGetter(&client)
|
||||
var err error
|
||||
expectedData, err = openapi.NewOpenAPIData(nil)
|
||||
Expect(err).To(BeNil())
|
||||
})
|
||||
|
||||
Context("when the server returns a successful result", func() {
|
||||
It("should return the same data for multiple calls", func() {
|
||||
Expect(client.Calls).To(Equal(0))
|
||||
|
||||
result, err := instance.Get()
|
||||
Expect(err).To(BeNil())
|
||||
Expect(result).To(Equal(expectedData))
|
||||
Expect(client.Calls).To(Equal(1))
|
||||
|
||||
result, err = instance.Get()
|
||||
Expect(err).To(BeNil())
|
||||
Expect(result).To(Equal(expectedData))
|
||||
// No additional client calls expected
|
||||
Expect(client.Calls).To(Equal(1))
|
||||
})
|
||||
})
|
||||
|
||||
Context("when the server returns an unsuccessful result", func() {
|
||||
It("should return the same instance for multiple calls.", func() {
|
||||
Expect(client.Calls).To(Equal(0))
|
||||
|
||||
client.Err = fmt.Errorf("expected error")
|
||||
_, err := instance.Get()
|
||||
Expect(err).To(Equal(client.Err))
|
||||
Expect(client.Calls).To(Equal(1))
|
||||
|
||||
_, err = instance.Get()
|
||||
Expect(err).To(Equal(client.Err))
|
||||
// No additional client calls expected
|
||||
Expect(client.Calls).To(Equal(1))
|
||||
})
|
||||
})
|
||||
})
|
49
vendor/k8s.io/kubernetes/pkg/kubectl/cmd/util/openapi/openapi_suite_test.go
generated
vendored
Normal file
49
vendor/k8s.io/kubernetes/pkg/kubectl/cmd/util/openapi/openapi_suite_test.go
generated
vendored
Normal file
@@ -0,0 +1,49 @@
|
||||
/*
|
||||
Copyright 2017 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 openapi_test
|
||||
|
||||
import (
|
||||
. "github.com/onsi/ginkgo"
|
||||
. "github.com/onsi/ginkgo/config"
|
||||
. "github.com/onsi/ginkgo/types"
|
||||
. "github.com/onsi/gomega"
|
||||
|
||||
"fmt"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestOpenapi(t *testing.T) {
|
||||
RegisterFailHandler(Fail)
|
||||
RunSpecsWithDefaultAndCustomReporters(t, "Openapi Suite", []Reporter{newlineReporter{}})
|
||||
}
|
||||
|
||||
// Print a newline after the default newlineReporter due to issue
|
||||
// https://github.com/jstemmer/go-junit-report/issues/31
|
||||
type newlineReporter struct{}
|
||||
|
||||
func (newlineReporter) SpecSuiteWillBegin(config GinkgoConfigType, summary *SuiteSummary) {}
|
||||
|
||||
func (newlineReporter) BeforeSuiteDidRun(setupSummary *SetupSummary) {}
|
||||
|
||||
func (newlineReporter) AfterSuiteDidRun(setupSummary *SetupSummary) {}
|
||||
|
||||
func (newlineReporter) SpecWillRun(specSummary *SpecSummary) {}
|
||||
|
||||
func (newlineReporter) SpecDidComplete(specSummary *SpecSummary) {}
|
||||
|
||||
// SpecSuiteDidEnd Prints a newline between "35 Passed | 0 Failed | 0 Pending | 0 Skipped" and "--- PASS:"
|
||||
func (newlineReporter) SpecSuiteDidEnd(summary *SuiteSummary) { fmt.Printf("\n") }
|
93
vendor/k8s.io/kubernetes/pkg/kubectl/cmd/util/openapi/openapi_test.go
generated
vendored
Normal file
93
vendor/k8s.io/kubernetes/pkg/kubectl/cmd/util/openapi/openapi_test.go
generated
vendored
Normal file
@@ -0,0 +1,93 @@
|
||||
/*
|
||||
Copyright 2017 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 openapi_test
|
||||
|
||||
import (
|
||||
"path/filepath"
|
||||
|
||||
. "github.com/onsi/ginkgo"
|
||||
. "github.com/onsi/gomega"
|
||||
|
||||
"k8s.io/apimachinery/pkg/runtime/schema"
|
||||
"k8s.io/kube-openapi/pkg/util/proto"
|
||||
"k8s.io/kube-openapi/pkg/util/proto/testing"
|
||||
"k8s.io/kubernetes/pkg/kubectl/cmd/util/openapi"
|
||||
)
|
||||
|
||||
var fakeSchema = testing.Fake{Path: filepath.Join("..", "..", "..", "..", "..", "api", "openapi-spec", "swagger.json")}
|
||||
|
||||
var _ = Describe("Reading apps/v1beta1/Deployment from openAPIData", func() {
|
||||
var resources openapi.Resources
|
||||
BeforeEach(func() {
|
||||
s, err := fakeSchema.OpenAPISchema()
|
||||
Expect(err).To(BeNil())
|
||||
resources, err = openapi.NewOpenAPIData(s)
|
||||
Expect(err).To(BeNil())
|
||||
})
|
||||
|
||||
gvk := schema.GroupVersionKind{
|
||||
Kind: "Deployment",
|
||||
Version: "v1beta1",
|
||||
Group: "apps",
|
||||
}
|
||||
|
||||
var schema proto.Schema
|
||||
It("should lookup the Schema by its GroupVersionKind", func() {
|
||||
schema = resources.LookupResource(gvk)
|
||||
Expect(schema).ToNot(BeNil())
|
||||
})
|
||||
|
||||
var deployment *proto.Kind
|
||||
It("should be a Kind", func() {
|
||||
deployment = schema.(*proto.Kind)
|
||||
Expect(deployment).ToNot(BeNil())
|
||||
})
|
||||
})
|
||||
|
||||
var _ = Describe("Reading authorization.k8s.io/v1/SubjectAccessReview from openAPIData", func() {
|
||||
var resources openapi.Resources
|
||||
BeforeEach(func() {
|
||||
s, err := fakeSchema.OpenAPISchema()
|
||||
Expect(err).To(BeNil())
|
||||
resources, err = openapi.NewOpenAPIData(s)
|
||||
Expect(err).To(BeNil())
|
||||
})
|
||||
|
||||
gvk := schema.GroupVersionKind{
|
||||
Kind: "SubjectAccessReview",
|
||||
Version: "v1",
|
||||
Group: "authorization.k8s.io",
|
||||
}
|
||||
|
||||
var schema proto.Schema
|
||||
It("should lookup the Schema by its GroupVersionKind", func() {
|
||||
schema = resources.LookupResource(gvk)
|
||||
Expect(schema).ToNot(BeNil())
|
||||
})
|
||||
|
||||
var sarspec *proto.Kind
|
||||
It("should be a Kind and have a spec", func() {
|
||||
sar := schema.(*proto.Kind)
|
||||
Expect(sar).ToNot(BeNil())
|
||||
Expect(sar.Fields).To(HaveKey("spec"))
|
||||
specRef := sar.Fields["spec"].(proto.Reference)
|
||||
Expect(specRef).ToNot(BeNil())
|
||||
Expect(specRef.Reference()).To(Equal("io.k8s.api.authorization.v1.SubjectAccessReviewSpec"))
|
||||
sarspec = specRef.SubSchema().(*proto.Kind)
|
||||
Expect(sarspec).ToNot(BeNil())
|
||||
})
|
||||
})
|
31
vendor/k8s.io/kubernetes/pkg/kubectl/cmd/util/openapi/testing/BUILD
generated
vendored
Normal file
31
vendor/k8s.io/kubernetes/pkg/kubectl/cmd/util/openapi/testing/BUILD
generated
vendored
Normal file
@@ -0,0 +1,31 @@
|
||||
package(default_visibility = ["//visibility:public"])
|
||||
|
||||
load(
|
||||
"@io_bazel_rules_go//go:def.bzl",
|
||||
"go_library",
|
||||
)
|
||||
|
||||
go_library(
|
||||
name = "go_default_library",
|
||||
srcs = ["openapi.go"],
|
||||
importpath = "k8s.io/kubernetes/pkg/kubectl/cmd/util/openapi/testing",
|
||||
deps = [
|
||||
"//pkg/kubectl/cmd/util/openapi:go_default_library",
|
||||
"//vendor/k8s.io/apimachinery/pkg/runtime/schema:go_default_library",
|
||||
"//vendor/k8s.io/kube-openapi/pkg/util/proto:go_default_library",
|
||||
"//vendor/k8s.io/kube-openapi/pkg/util/proto/testing:go_default_library",
|
||||
],
|
||||
)
|
||||
|
||||
filegroup(
|
||||
name = "package-srcs",
|
||||
srcs = glob(["**"]),
|
||||
tags = ["automanaged"],
|
||||
visibility = ["//visibility:private"],
|
||||
)
|
||||
|
||||
filegroup(
|
||||
name = "all-srcs",
|
||||
srcs = [":package-srcs"],
|
||||
tags = ["automanaged"],
|
||||
)
|
71
vendor/k8s.io/kubernetes/pkg/kubectl/cmd/util/openapi/testing/openapi.go
generated
vendored
Normal file
71
vendor/k8s.io/kubernetes/pkg/kubectl/cmd/util/openapi/testing/openapi.go
generated
vendored
Normal file
@@ -0,0 +1,71 @@
|
||||
/*
|
||||
Copyright 2017 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 testing
|
||||
|
||||
import (
|
||||
"k8s.io/apimachinery/pkg/runtime/schema"
|
||||
"k8s.io/kube-openapi/pkg/util/proto"
|
||||
"k8s.io/kube-openapi/pkg/util/proto/testing"
|
||||
"k8s.io/kubernetes/pkg/kubectl/cmd/util/openapi"
|
||||
)
|
||||
|
||||
// FakeResources is a wrapper to directly load the openapi schema from a
|
||||
// file, and get the schema for given GVK. This is only for test since
|
||||
// it's assuming that the file is there and everything will go fine.
|
||||
type FakeResources struct {
|
||||
fake testing.Fake
|
||||
}
|
||||
|
||||
var _ openapi.Resources = &FakeResources{}
|
||||
|
||||
// NewFakeResources creates a new FakeResources.
|
||||
func NewFakeResources(path string) *FakeResources {
|
||||
return &FakeResources{
|
||||
fake: testing.Fake{Path: path},
|
||||
}
|
||||
}
|
||||
|
||||
// LookupResource will read the schema, parse it and return the
|
||||
// resources. It doesn't return errors and will panic instead.
|
||||
func (f *FakeResources) LookupResource(gvk schema.GroupVersionKind) proto.Schema {
|
||||
s, err := f.fake.OpenAPISchema()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
resources, err := openapi.NewOpenAPIData(s)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return resources.LookupResource(gvk)
|
||||
}
|
||||
|
||||
// EmptyResources implement a Resources that just doesn't have any resources.
|
||||
type EmptyResources struct{}
|
||||
|
||||
var _ openapi.Resources = EmptyResources{}
|
||||
|
||||
// LookupResource will always return nil. It doesn't have any resources.
|
||||
func (f EmptyResources) LookupResource(gvk schema.GroupVersionKind) proto.Schema {
|
||||
return nil
|
||||
}
|
||||
|
||||
// CreateOpenAPISchemaFunc returns a function useful for the TestFactory.
|
||||
func CreateOpenAPISchemaFunc(path string) func() (openapi.Resources, error) {
|
||||
return func() (openapi.Resources, error) {
|
||||
return NewFakeResources(path), nil
|
||||
}
|
||||
}
|
55
vendor/k8s.io/kubernetes/pkg/kubectl/cmd/util/openapi/validation/BUILD
generated
vendored
Normal file
55
vendor/k8s.io/kubernetes/pkg/kubectl/cmd/util/openapi/validation/BUILD
generated
vendored
Normal file
@@ -0,0 +1,55 @@
|
||||
package(default_visibility = ["//visibility:public"])
|
||||
|
||||
load(
|
||||
"@io_bazel_rules_go//go:def.bzl",
|
||||
"go_library",
|
||||
"go_test",
|
||||
)
|
||||
|
||||
go_library(
|
||||
name = "go_default_library",
|
||||
srcs = ["validation.go"],
|
||||
importpath = "k8s.io/kubernetes/pkg/kubectl/cmd/util/openapi/validation",
|
||||
deps = [
|
||||
"//pkg/kubectl/cmd/util/openapi:go_default_library",
|
||||
"//vendor/k8s.io/apimachinery/pkg/runtime/schema:go_default_library",
|
||||
"//vendor/k8s.io/apimachinery/pkg/util/errors:go_default_library",
|
||||
"//vendor/k8s.io/apimachinery/pkg/util/json:go_default_library",
|
||||
"//vendor/k8s.io/apimachinery/pkg/util/yaml:go_default_library",
|
||||
"//vendor/k8s.io/kube-openapi/pkg/util/proto/validation:go_default_library",
|
||||
],
|
||||
)
|
||||
|
||||
go_test(
|
||||
name = "go_default_test",
|
||||
srcs = [
|
||||
"validation_suite_test.go",
|
||||
"validation_test.go",
|
||||
],
|
||||
data = ["//api/openapi-spec:swagger-spec"],
|
||||
embed = [":go_default_library"],
|
||||
deps = [
|
||||
"//pkg/api/testapi:go_default_library",
|
||||
"//pkg/kubectl/cmd/util/openapi:go_default_library",
|
||||
"//vendor/github.com/onsi/ginkgo:go_default_library",
|
||||
"//vendor/github.com/onsi/ginkgo/config:go_default_library",
|
||||
"//vendor/github.com/onsi/ginkgo/types:go_default_library",
|
||||
"//vendor/github.com/onsi/gomega:go_default_library",
|
||||
"//vendor/k8s.io/apimachinery/pkg/util/errors:go_default_library",
|
||||
"//vendor/k8s.io/kube-openapi/pkg/util/proto/testing:go_default_library",
|
||||
"//vendor/k8s.io/kube-openapi/pkg/util/proto/validation:go_default_library",
|
||||
],
|
||||
)
|
||||
|
||||
filegroup(
|
||||
name = "package-srcs",
|
||||
srcs = glob(["**"]),
|
||||
tags = ["automanaged"],
|
||||
visibility = ["//visibility:private"],
|
||||
)
|
||||
|
||||
filegroup(
|
||||
name = "all-srcs",
|
||||
srcs = [":package-srcs"],
|
||||
tags = ["automanaged"],
|
||||
)
|
140
vendor/k8s.io/kubernetes/pkg/kubectl/cmd/util/openapi/validation/validation.go
generated
vendored
Normal file
140
vendor/k8s.io/kubernetes/pkg/kubectl/cmd/util/openapi/validation/validation.go
generated
vendored
Normal file
@@ -0,0 +1,140 @@
|
||||
/*
|
||||
Copyright 2017 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 validation
|
||||
|
||||
import (
|
||||
"errors"
|
||||
|
||||
"k8s.io/apimachinery/pkg/runtime/schema"
|
||||
utilerrors "k8s.io/apimachinery/pkg/util/errors"
|
||||
"k8s.io/apimachinery/pkg/util/json"
|
||||
"k8s.io/apimachinery/pkg/util/yaml"
|
||||
"k8s.io/kube-openapi/pkg/util/proto/validation"
|
||||
"k8s.io/kubernetes/pkg/kubectl/cmd/util/openapi"
|
||||
)
|
||||
|
||||
// SchemaValidation validates the object against an OpenAPI schema.
|
||||
type SchemaValidation struct {
|
||||
resources openapi.Resources
|
||||
}
|
||||
|
||||
// NewSchemaValidation creates a new SchemaValidation that can be used
|
||||
// to validate objects.
|
||||
func NewSchemaValidation(resources openapi.Resources) *SchemaValidation {
|
||||
return &SchemaValidation{
|
||||
resources: resources,
|
||||
}
|
||||
}
|
||||
|
||||
// ValidateBytes will validates the object against using the Resources
|
||||
// object.
|
||||
func (v *SchemaValidation) ValidateBytes(data []byte) error {
|
||||
obj, err := parse(data)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
gvk, errs := getObjectKind(obj)
|
||||
if errs != nil {
|
||||
return utilerrors.NewAggregate(errs)
|
||||
}
|
||||
|
||||
if (gvk == schema.GroupVersionKind{Version: "v1", Kind: "List"}) {
|
||||
return utilerrors.NewAggregate(v.validateList(obj))
|
||||
}
|
||||
|
||||
return utilerrors.NewAggregate(v.validateResource(obj, gvk))
|
||||
}
|
||||
|
||||
func (v *SchemaValidation) validateList(object interface{}) []error {
|
||||
fields, ok := object.(map[string]interface{})
|
||||
if !ok || fields == nil {
|
||||
return []error{errors.New("invalid object to validate")}
|
||||
}
|
||||
|
||||
allErrors := []error{}
|
||||
if _, ok := fields["items"].([]interface{}); !ok {
|
||||
return []error{errors.New("invalid object to validate")}
|
||||
}
|
||||
for _, item := range fields["items"].([]interface{}) {
|
||||
if gvk, errs := getObjectKind(item); errs != nil {
|
||||
allErrors = append(allErrors, errs...)
|
||||
} else {
|
||||
allErrors = append(allErrors, v.validateResource(item, gvk)...)
|
||||
}
|
||||
}
|
||||
return allErrors
|
||||
}
|
||||
|
||||
func (v *SchemaValidation) validateResource(obj interface{}, gvk schema.GroupVersionKind) []error {
|
||||
resource := v.resources.LookupResource(gvk)
|
||||
if resource == nil {
|
||||
// resource is not present, let's just skip validation.
|
||||
return nil
|
||||
}
|
||||
|
||||
return validation.ValidateModel(obj, resource, gvk.Kind)
|
||||
}
|
||||
|
||||
func parse(data []byte) (interface{}, error) {
|
||||
var obj interface{}
|
||||
out, err := yaml.ToJSON(data)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := json.Unmarshal(out, &obj); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return obj, nil
|
||||
}
|
||||
|
||||
func getObjectKind(object interface{}) (schema.GroupVersionKind, []error) {
|
||||
var listErrors []error
|
||||
fields, ok := object.(map[string]interface{})
|
||||
if !ok || fields == nil {
|
||||
listErrors = append(listErrors, errors.New("invalid object to validate"))
|
||||
return schema.GroupVersionKind{}, listErrors
|
||||
}
|
||||
|
||||
var group string
|
||||
var version string
|
||||
apiVersion := fields["apiVersion"]
|
||||
if apiVersion == nil {
|
||||
listErrors = append(listErrors, errors.New("apiVersion not set"))
|
||||
} else if _, ok := apiVersion.(string); !ok {
|
||||
listErrors = append(listErrors, errors.New("apiVersion isn't string type"))
|
||||
} else {
|
||||
gv, err := schema.ParseGroupVersion(apiVersion.(string))
|
||||
if err != nil {
|
||||
listErrors = append(listErrors, err)
|
||||
} else {
|
||||
group = gv.Group
|
||||
version = gv.Version
|
||||
}
|
||||
}
|
||||
kind := fields["kind"]
|
||||
if kind == nil {
|
||||
listErrors = append(listErrors, errors.New("kind not set"))
|
||||
} else if _, ok := kind.(string); !ok {
|
||||
listErrors = append(listErrors, errors.New("kind isn't string type"))
|
||||
}
|
||||
if listErrors != nil {
|
||||
return schema.GroupVersionKind{}, listErrors
|
||||
}
|
||||
|
||||
return schema.GroupVersionKind{Group: group, Version: version, Kind: kind.(string)}, nil
|
||||
}
|
49
vendor/k8s.io/kubernetes/pkg/kubectl/cmd/util/openapi/validation/validation_suite_test.go
generated
vendored
Normal file
49
vendor/k8s.io/kubernetes/pkg/kubectl/cmd/util/openapi/validation/validation_suite_test.go
generated
vendored
Normal file
@@ -0,0 +1,49 @@
|
||||
/*
|
||||
Copyright 2017 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 validation
|
||||
|
||||
import (
|
||||
. "github.com/onsi/ginkgo"
|
||||
. "github.com/onsi/ginkgo/config"
|
||||
. "github.com/onsi/ginkgo/types"
|
||||
. "github.com/onsi/gomega"
|
||||
|
||||
"fmt"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestOpenapi(t *testing.T) {
|
||||
RegisterFailHandler(Fail)
|
||||
RunSpecsWithDefaultAndCustomReporters(t, "Openapi Suite", []Reporter{newlineReporter{}})
|
||||
}
|
||||
|
||||
// Print a newline after the default newlineReporter due to issue
|
||||
// https://github.com/jstemmer/go-junit-report/issues/31
|
||||
type newlineReporter struct{}
|
||||
|
||||
func (newlineReporter) SpecSuiteWillBegin(config GinkgoConfigType, summary *SuiteSummary) {}
|
||||
|
||||
func (newlineReporter) BeforeSuiteDidRun(setupSummary *SetupSummary) {}
|
||||
|
||||
func (newlineReporter) AfterSuiteDidRun(setupSummary *SetupSummary) {}
|
||||
|
||||
func (newlineReporter) SpecWillRun(specSummary *SpecSummary) {}
|
||||
|
||||
func (newlineReporter) SpecDidComplete(specSummary *SpecSummary) {}
|
||||
|
||||
// SpecSuiteDidEnd Prints a newline between "35 Passed | 0 Failed | 0 Pending | 0 Skipped" and "--- PASS:"
|
||||
func (newlineReporter) SpecSuiteDidEnd(summary *SuiteSummary) { fmt.Printf("\n") }
|
409
vendor/k8s.io/kubernetes/pkg/kubectl/cmd/util/openapi/validation/validation_test.go
generated
vendored
Normal file
409
vendor/k8s.io/kubernetes/pkg/kubectl/cmd/util/openapi/validation/validation_test.go
generated
vendored
Normal file
@@ -0,0 +1,409 @@
|
||||
/*
|
||||
Copyright 2017 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 validation
|
||||
|
||||
import (
|
||||
"path/filepath"
|
||||
|
||||
. "github.com/onsi/ginkgo"
|
||||
. "github.com/onsi/gomega"
|
||||
|
||||
utilerrors "k8s.io/apimachinery/pkg/util/errors"
|
||||
"k8s.io/kube-openapi/pkg/util/proto/validation"
|
||||
// This dependency is needed to register API types.
|
||||
"k8s.io/kube-openapi/pkg/util/proto/testing"
|
||||
_ "k8s.io/kubernetes/pkg/api/testapi"
|
||||
"k8s.io/kubernetes/pkg/kubectl/cmd/util/openapi"
|
||||
)
|
||||
|
||||
var fakeSchema = testing.Fake{Path: filepath.Join("..", "..", "..", "..", "..", "..", "api", "openapi-spec", "swagger.json")}
|
||||
|
||||
var _ = Describe("resource validation using OpenAPI Schema", func() {
|
||||
var validator *SchemaValidation
|
||||
BeforeEach(func() {
|
||||
s, err := fakeSchema.OpenAPISchema()
|
||||
Expect(err).To(BeNil())
|
||||
resources, err := openapi.NewOpenAPIData(s)
|
||||
Expect(err).To(BeNil())
|
||||
validator = NewSchemaValidation(resources)
|
||||
Expect(validator).ToNot(BeNil())
|
||||
})
|
||||
|
||||
It("finds Deployment in Schema and validates it", func() {
|
||||
err := validator.ValidateBytes([]byte(`
|
||||
apiVersion: extensions/v1beta1
|
||||
kind: Deployment
|
||||
metadata:
|
||||
labels:
|
||||
name: redis-master
|
||||
name: name
|
||||
spec:
|
||||
replicas: 1
|
||||
template:
|
||||
metadata:
|
||||
labels:
|
||||
app: redis
|
||||
spec:
|
||||
containers:
|
||||
- image: redis
|
||||
name: redis
|
||||
`))
|
||||
Expect(err).To(BeNil())
|
||||
})
|
||||
|
||||
It("validates a valid pod", func() {
|
||||
err := validator.ValidateBytes([]byte(`
|
||||
apiVersion: v1
|
||||
kind: Pod
|
||||
metadata:
|
||||
labels:
|
||||
name: redis-master
|
||||
name: name
|
||||
spec:
|
||||
containers:
|
||||
- args:
|
||||
- this
|
||||
- is
|
||||
- an
|
||||
- ok
|
||||
- command
|
||||
image: gcr.io/fake_project/fake_image:fake_tag
|
||||
name: master
|
||||
`))
|
||||
Expect(err).To(BeNil())
|
||||
})
|
||||
|
||||
It("finds invalid command (string instead of []string) in Json Pod", func() {
|
||||
err := validator.ValidateBytes([]byte(`
|
||||
{
|
||||
"kind": "Pod",
|
||||
"apiVersion": "v1",
|
||||
"metadata": {
|
||||
"name": "name",
|
||||
"labels": {
|
||||
"name": "redis-master"
|
||||
}
|
||||
},
|
||||
"spec": {
|
||||
"containers": [
|
||||
{
|
||||
"name": "master",
|
||||
"image": "gcr.io/fake_project/fake_image:fake_tag",
|
||||
"args": "this is a bad command"
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
`))
|
||||
Expect(err).To(Equal(utilerrors.NewAggregate([]error{
|
||||
validation.ValidationError{
|
||||
Path: "Pod.spec.containers[0].args",
|
||||
Err: validation.InvalidTypeError{
|
||||
Path: "io.k8s.api.core.v1.Container.args",
|
||||
Expected: "array",
|
||||
Actual: "string",
|
||||
},
|
||||
},
|
||||
})))
|
||||
})
|
||||
|
||||
It("fails because hostPort is string instead of int", func() {
|
||||
err := validator.ValidateBytes([]byte(`
|
||||
{
|
||||
"kind": "Pod",
|
||||
"apiVersion": "v1",
|
||||
"metadata": {
|
||||
"name": "apache-php",
|
||||
"labels": {
|
||||
"name": "apache-php"
|
||||
}
|
||||
},
|
||||
"spec": {
|
||||
"volumes": [{
|
||||
"name": "shared-disk"
|
||||
}],
|
||||
"containers": [
|
||||
{
|
||||
"name": "apache-php",
|
||||
"image": "gcr.io/fake_project/fake_image:fake_tag",
|
||||
"ports": [
|
||||
{
|
||||
"name": "apache",
|
||||
"hostPort": "13380",
|
||||
"containerPort": 80,
|
||||
"protocol": "TCP"
|
||||
}
|
||||
],
|
||||
"volumeMounts": [
|
||||
{
|
||||
"name": "shared-disk",
|
||||
"mountPath": "/var/www/html"
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
`))
|
||||
|
||||
Expect(err).To(Equal(utilerrors.NewAggregate([]error{
|
||||
validation.ValidationError{
|
||||
Path: "Pod.spec.containers[0].ports[0].hostPort",
|
||||
Err: validation.InvalidTypeError{
|
||||
Path: "io.k8s.api.core.v1.ContainerPort.hostPort",
|
||||
Expected: "integer",
|
||||
Actual: "string",
|
||||
},
|
||||
},
|
||||
})))
|
||||
|
||||
})
|
||||
|
||||
It("fails because volume is not an array of object", func() {
|
||||
err := validator.ValidateBytes([]byte(`
|
||||
{
|
||||
"kind": "Pod",
|
||||
"apiVersion": "v1",
|
||||
"metadata": {
|
||||
"name": "apache-php",
|
||||
"labels": {
|
||||
"name": "apache-php"
|
||||
}
|
||||
},
|
||||
"spec": {
|
||||
"volumes": [
|
||||
"name": "shared-disk"
|
||||
],
|
||||
"containers": [
|
||||
{
|
||||
"name": "apache-php",
|
||||
"image": "gcr.io/fake_project/fake_image:fake_tag",
|
||||
"ports": [
|
||||
{
|
||||
"name": "apache",
|
||||
"hostPort": 13380,
|
||||
"containerPort": 80,
|
||||
"protocol": "TCP"
|
||||
}
|
||||
],
|
||||
"volumeMounts": [
|
||||
{
|
||||
"name": "shared-disk",
|
||||
"mountPath": "/var/www/html"
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
`))
|
||||
Expect(err.Error()).To(Equal("invalid character ':' after array element"))
|
||||
})
|
||||
|
||||
It("fails because some string lists have empty strings", func() {
|
||||
err := validator.ValidateBytes([]byte(`
|
||||
apiVersion: v1
|
||||
kind: Pod
|
||||
metadata:
|
||||
labels:
|
||||
name: redis-master
|
||||
name: name
|
||||
spec:
|
||||
containers:
|
||||
- image: gcr.io/fake_project/fake_image:fake_tag
|
||||
name: master
|
||||
args:
|
||||
-
|
||||
command:
|
||||
-
|
||||
`))
|
||||
|
||||
Expect(err).To(Equal(utilerrors.NewAggregate([]error{
|
||||
validation.ValidationError{
|
||||
Path: "Pod.spec.containers[0].args",
|
||||
Err: validation.InvalidObjectTypeError{
|
||||
Path: "Pod.spec.containers[0].args[0]",
|
||||
Type: "nil",
|
||||
},
|
||||
},
|
||||
validation.ValidationError{
|
||||
Path: "Pod.spec.containers[0].command",
|
||||
Err: validation.InvalidObjectTypeError{
|
||||
Path: "Pod.spec.containers[0].command[0]",
|
||||
Type: "nil",
|
||||
},
|
||||
},
|
||||
})))
|
||||
})
|
||||
|
||||
It("fails if required fields are missing", func() {
|
||||
err := validator.ValidateBytes([]byte(`
|
||||
apiVersion: v1
|
||||
kind: Pod
|
||||
metadata:
|
||||
labels:
|
||||
name: redis-master
|
||||
name: name
|
||||
spec:
|
||||
containers:
|
||||
- command: ["my", "command"]
|
||||
`))
|
||||
|
||||
Expect(err).To(Equal(utilerrors.NewAggregate([]error{
|
||||
validation.ValidationError{
|
||||
Path: "Pod.spec.containers[0]",
|
||||
Err: validation.MissingRequiredFieldError{
|
||||
Path: "io.k8s.api.core.v1.Container",
|
||||
Field: "name",
|
||||
},
|
||||
},
|
||||
})))
|
||||
})
|
||||
|
||||
It("fails if required fields are empty", func() {
|
||||
err := validator.ValidateBytes([]byte(`
|
||||
apiVersion: v1
|
||||
kind: Pod
|
||||
metadata:
|
||||
labels:
|
||||
name: redis-master
|
||||
name: name
|
||||
spec:
|
||||
containers:
|
||||
- image:
|
||||
name:
|
||||
`))
|
||||
|
||||
Expect(err).To(Equal(utilerrors.NewAggregate([]error{
|
||||
validation.ValidationError{
|
||||
Path: "Pod.spec.containers[0]",
|
||||
Err: validation.MissingRequiredFieldError{
|
||||
Path: "io.k8s.api.core.v1.Container",
|
||||
Field: "name",
|
||||
},
|
||||
},
|
||||
})))
|
||||
})
|
||||
|
||||
It("is fine with empty non-mandatory fields", func() {
|
||||
err := validator.ValidateBytes([]byte(`
|
||||
apiVersion: v1
|
||||
kind: Pod
|
||||
metadata:
|
||||
labels:
|
||||
name: redis-master
|
||||
name: name
|
||||
spec:
|
||||
containers:
|
||||
- image: image
|
||||
name: name
|
||||
command:
|
||||
`))
|
||||
|
||||
Expect(err).To(BeNil())
|
||||
})
|
||||
|
||||
It("can validate lists", func() {
|
||||
err := validator.ValidateBytes([]byte(`
|
||||
apiVersion: v1
|
||||
kind: List
|
||||
items:
|
||||
- apiVersion: v1
|
||||
kind: Pod
|
||||
metadata:
|
||||
labels:
|
||||
name: redis-master
|
||||
name: name
|
||||
spec:
|
||||
containers:
|
||||
- name: name
|
||||
`))
|
||||
|
||||
Expect(err).To(BeNil())
|
||||
})
|
||||
|
||||
It("fails because apiVersion is not provided", func() {
|
||||
err := validator.ValidateBytes([]byte(`
|
||||
kind: Pod
|
||||
metadata:
|
||||
name: name
|
||||
spec:
|
||||
containers:
|
||||
- name: name
|
||||
image: image
|
||||
`))
|
||||
Expect(err.Error()).To(Equal("apiVersion not set"))
|
||||
})
|
||||
|
||||
It("fails because apiVersion type is not string and kind is not provided", func() {
|
||||
err := validator.ValidateBytes([]byte(`
|
||||
apiVersion: 1
|
||||
metadata:
|
||||
name: name
|
||||
spec:
|
||||
containers:
|
||||
- name: name
|
||||
image: image
|
||||
`))
|
||||
Expect(err.Error()).To(Equal("[apiVersion isn't string type, kind not set]"))
|
||||
})
|
||||
|
||||
It("fails because List first item is missing kind and second item is missing apiVersion", func() {
|
||||
err := validator.ValidateBytes([]byte(`
|
||||
apiVersion: v1
|
||||
kind: List
|
||||
items:
|
||||
- apiVersion: v1
|
||||
metadata:
|
||||
name: name
|
||||
spec:
|
||||
replicas: 1
|
||||
template:
|
||||
metadata:
|
||||
labels:
|
||||
name: name
|
||||
spec:
|
||||
containers:
|
||||
- name: name
|
||||
image: image
|
||||
- kind: Service
|
||||
metadata:
|
||||
name: name
|
||||
spec:
|
||||
type: NodePort
|
||||
ports:
|
||||
- port: 123
|
||||
targetPort: 1234
|
||||
name: name
|
||||
selector:
|
||||
name: name
|
||||
`))
|
||||
Expect(err.Error()).To(Equal("[kind not set, apiVersion not set]"))
|
||||
})
|
||||
|
||||
It("is fine with crd resource with List as a suffix kind name, which may not be a list of resources", func() {
|
||||
err := validator.ValidateBytes([]byte(`
|
||||
apiVersion: fake.com/v1
|
||||
kind: FakeList
|
||||
metadata:
|
||||
name: fake
|
||||
spec:
|
||||
foo: bar
|
||||
`))
|
||||
Expect(err).To(BeNil())
|
||||
})
|
||||
})
|
29
vendor/k8s.io/kubernetes/pkg/kubectl/cmd/util/printing.go
generated
vendored
Normal file
29
vendor/k8s.io/kubernetes/pkg/kubectl/cmd/util/printing.go
generated
vendored
Normal file
@@ -0,0 +1,29 @@
|
||||
/*
|
||||
Copyright 2014 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"
|
||||
|
||||
"k8s.io/kubernetes/pkg/kubectl/cmd/templates"
|
||||
)
|
||||
|
||||
// SuggestApiResources returns a suggestion to use the "api-resources" command
|
||||
// to retrieve a supported list of resources
|
||||
func SuggestApiResources(parent string) string {
|
||||
return templates.LongDesc(fmt.Sprintf("Use \"%s api-resources\" for a complete list of supported resources.", parent))
|
||||
}
|
33
vendor/k8s.io/kubernetes/pkg/kubectl/cmd/util/sanity/BUILD
generated
vendored
Normal file
33
vendor/k8s.io/kubernetes/pkg/kubectl/cmd/util/sanity/BUILD
generated
vendored
Normal file
@@ -0,0 +1,33 @@
|
||||
load(
|
||||
"@io_bazel_rules_go//go:def.bzl",
|
||||
"go_library",
|
||||
)
|
||||
|
||||
go_library(
|
||||
name = "go_default_library",
|
||||
srcs = ["cmd_sanity.go"],
|
||||
importpath = "k8s.io/kubernetes/pkg/kubectl/cmd/util/sanity",
|
||||
visibility = [
|
||||
"//build/visible_to:pkg_kubectl_cmd_util_sanity_CONSUMERS",
|
||||
],
|
||||
deps = [
|
||||
"//pkg/kubectl/cmd/templates:go_default_library",
|
||||
"//vendor/github.com/spf13/cobra:go_default_library",
|
||||
"//vendor/github.com/spf13/pflag:go_default_library",
|
||||
],
|
||||
)
|
||||
|
||||
filegroup(
|
||||
name = "package-srcs",
|
||||
srcs = glob(["**"]),
|
||||
tags = ["automanaged"],
|
||||
)
|
||||
|
||||
filegroup(
|
||||
name = "all-srcs",
|
||||
srcs = [":package-srcs"],
|
||||
tags = ["automanaged"],
|
||||
visibility = [
|
||||
"//build/visible_to:pkg_kubectl_cmd_util_sanity_CONSUMERS",
|
||||
],
|
||||
)
|
149
vendor/k8s.io/kubernetes/pkg/kubectl/cmd/util/sanity/cmd_sanity.go
generated
vendored
Normal file
149
vendor/k8s.io/kubernetes/pkg/kubectl/cmd/util/sanity/cmd_sanity.go
generated
vendored
Normal file
@@ -0,0 +1,149 @@
|
||||
/*
|
||||
Copyright 2016 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 sanity
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"regexp"
|
||||
"strings"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
"github.com/spf13/pflag"
|
||||
"k8s.io/kubernetes/pkg/kubectl/cmd/templates"
|
||||
)
|
||||
|
||||
type CmdCheck func(cmd *cobra.Command) []error
|
||||
type GlobalCheck func() []error
|
||||
|
||||
var (
|
||||
AllCmdChecks = []CmdCheck{
|
||||
CheckLongDesc,
|
||||
CheckExamples,
|
||||
CheckFlags,
|
||||
}
|
||||
AllGlobalChecks = []GlobalCheck{
|
||||
CheckGlobalVarFlags,
|
||||
}
|
||||
)
|
||||
|
||||
func RunGlobalChecks(globalChecks []GlobalCheck) []error {
|
||||
fmt.Fprint(os.Stdout, "---+ RUNNING GLOBAL CHECKS\n")
|
||||
errors := []error{}
|
||||
for _, check := range globalChecks {
|
||||
errors = append(errors, check()...)
|
||||
}
|
||||
return errors
|
||||
}
|
||||
|
||||
func RunCmdChecks(cmd *cobra.Command, cmdChecks []CmdCheck, skipCmd []string) []error {
|
||||
cmdPath := cmd.CommandPath()
|
||||
|
||||
for _, skipCmdPath := range skipCmd {
|
||||
if cmdPath == skipCmdPath {
|
||||
fmt.Fprintf(os.Stdout, "---+ skipping command %s\n", cmdPath)
|
||||
return []error{}
|
||||
}
|
||||
}
|
||||
|
||||
errors := []error{}
|
||||
|
||||
if cmd.HasSubCommands() {
|
||||
for _, subCmd := range cmd.Commands() {
|
||||
errors = append(errors, RunCmdChecks(subCmd, cmdChecks, skipCmd)...)
|
||||
}
|
||||
}
|
||||
|
||||
fmt.Fprintf(os.Stdout, "---+ RUNNING COMMAND CHECKS on %q\n", cmdPath)
|
||||
|
||||
for _, check := range cmdChecks {
|
||||
if err := check(cmd); err != nil && len(err) > 0 {
|
||||
errors = append(errors, err...)
|
||||
}
|
||||
}
|
||||
|
||||
return errors
|
||||
}
|
||||
|
||||
func CheckLongDesc(cmd *cobra.Command) []error {
|
||||
fmt.Fprint(os.Stdout, " ↳ checking long description\n")
|
||||
cmdPath := cmd.CommandPath()
|
||||
long := cmd.Long
|
||||
if len(long) > 0 {
|
||||
if strings.Trim(long, " \t\n") != long {
|
||||
return []error{fmt.Errorf(`command %q: long description is not normalized, make sure you are calling templates.LongDesc (from pkg/cmd/templates) before assigning cmd.Long`, cmdPath)}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func CheckExamples(cmd *cobra.Command) []error {
|
||||
fmt.Fprint(os.Stdout, " ↳ checking examples\n")
|
||||
cmdPath := cmd.CommandPath()
|
||||
examples := cmd.Example
|
||||
errors := []error{}
|
||||
if len(examples) > 0 {
|
||||
for _, line := range strings.Split(examples, "\n") {
|
||||
if !strings.HasPrefix(line, templates.Indentation) {
|
||||
errors = append(errors, fmt.Errorf(`command %q: examples are not normalized, make sure you are calling templates.Examples (from pkg/cmd/templates) before assigning cmd.Example`, cmdPath))
|
||||
}
|
||||
if trimmed := strings.TrimSpace(line); strings.HasPrefix(trimmed, "//") {
|
||||
errors = append(errors, fmt.Errorf(`command %q: we use # to start comments in examples instead of //`, cmdPath))
|
||||
}
|
||||
}
|
||||
}
|
||||
return errors
|
||||
}
|
||||
|
||||
func CheckFlags(cmd *cobra.Command) []error {
|
||||
allFlagsSlice := []*pflag.Flag{}
|
||||
|
||||
cmd.Flags().VisitAll(func(f *pflag.Flag) {
|
||||
allFlagsSlice = append(allFlagsSlice, f)
|
||||
})
|
||||
cmd.PersistentFlags().VisitAll(func(f *pflag.Flag) {
|
||||
allFlagsSlice = append(allFlagsSlice, f)
|
||||
})
|
||||
|
||||
fmt.Fprintf(os.Stdout, " ↳ checking %d flags\n", len(allFlagsSlice))
|
||||
|
||||
errors := []error{}
|
||||
|
||||
// check flags long names
|
||||
regex, err := regexp.Compile(`^[a-z]+[a-z\-]*$`)
|
||||
if err != nil {
|
||||
errors = append(errors, fmt.Errorf("command %q: unable to compile regex to check flags", cmd.CommandPath()))
|
||||
return errors
|
||||
}
|
||||
for _, flag := range allFlagsSlice {
|
||||
name := flag.Name
|
||||
if !regex.MatchString(name) {
|
||||
errors = append(errors, fmt.Errorf("command %q: flag name %q is invalid, long form of flag names can only contain lowercase characters or dash (must match %v)", cmd.CommandPath(), name, regex))
|
||||
}
|
||||
}
|
||||
|
||||
return errors
|
||||
}
|
||||
|
||||
func CheckGlobalVarFlags() []error {
|
||||
fmt.Fprint(os.Stdout, " ↳ checking flags from global vars\n")
|
||||
errors := []error{}
|
||||
pflag.CommandLine.VisitAll(func(f *pflag.Flag) {
|
||||
errors = append(errors, fmt.Errorf("flag %q is invalid, please don't register any flag under the global variable \"CommandLine\"", f.Name))
|
||||
})
|
||||
return errors
|
||||
}
|
Reference in New Issue
Block a user