1 /* 2 Copyright 2018 The Kubernetes Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package describe 18 19 import ( 20 "fmt" 21 "k8s.io/apimachinery/pkg/api/meta" 22 "k8s.io/cli-runtime/pkg/genericclioptions" 23 ) 24 25 const ( 26 // LoadBalancerWidth is the width how we describe load balancer 27 LoadBalancerWidth = 16 28 29 // LabelNodeRolePrefix is a label prefix for node roles 30 // It's copied over to here until it's merged in core: https://github.com/kubernetes/kubernetes/pull/39112 31 LabelNodeRolePrefix = "node-role.kubernetes.io/" 32 33 // NodeLabelRole specifies the role of a node 34 NodeLabelRole = "kubernetes.io/role" 35 ) 36 37 // DescriberFunc gives a way to display the specified RESTMapping type 38 type DescriberFunc func(restClientGetter genericclioptions.RESTClientGetter, mapping *meta.RESTMapping) (ResourceDescriber, error) 39 40 // ResourceDescriber generates output for the named resource or an error 41 // if the output could not be generated. Implementers typically 42 // abstract the retrieval of the named object from a remote server. 43 type ResourceDescriber interface { 44 Describe(namespace, name string, describerSettings DescriberSettings) (output string, err error) 45 } 46 47 // DescriberSettings holds display configuration for each object 48 // describer to control what is printed. 49 type DescriberSettings struct { 50 ShowEvents bool 51 ChunkSize int64 52 } 53 54 // ObjectDescriber is an interface for displaying arbitrary objects with extra 55 // information. Use when an object is in hand (on disk, or already retrieved). 56 // Implementers may ignore the additional information passed on extra, or use it 57 // by default. ObjectDescribers may return ErrNoDescriber if no suitable describer 58 // is found. 59 type ObjectDescriber interface { 60 DescribeObject(object interface{}, extra ...interface{}) (output string, err error) 61 } 62 63 // ErrNoDescriber is a structured error indicating the provided object or objects 64 // cannot be described. 65 type ErrNoDescriber struct { 66 Types []string 67 } 68 69 // Error implements the error interface. 70 func (e ErrNoDescriber) Error() string { 71 return fmt.Sprintf("no describer has been defined for %v", e.Types) 72 } 73