...

Source file src/k8s.io/kubectl/pkg/explain/v2/explain.go

Documentation: k8s.io/kubectl/pkg/explain/v2

     1  /*
     2  Copyright 2022 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 v2
    18  
    19  import (
    20  	"encoding/json"
    21  	"errors"
    22  	"fmt"
    23  	"io"
    24  
    25  	"k8s.io/apimachinery/pkg/runtime"
    26  	"k8s.io/apimachinery/pkg/runtime/schema"
    27  	"k8s.io/client-go/openapi"
    28  )
    29  
    30  // PrintModelDescription prints the description of a specific model or dot path.
    31  // If recursive, all components nested within the fields of the schema will be
    32  // printed.
    33  func PrintModelDescription(
    34  	fieldsPath []string,
    35  	w io.Writer,
    36  	client openapi.Client,
    37  	gvr schema.GroupVersionResource,
    38  	recursive bool,
    39  	outputFormat string,
    40  ) error {
    41  	generator := NewGenerator()
    42  	if err := registerBuiltinTemplates(generator); err != nil {
    43  		return fmt.Errorf("error parsing builtin templates. Please file a bug on GitHub: %w", err)
    44  	}
    45  
    46  	return printModelDescriptionWithGenerator(
    47  		generator, fieldsPath, w, client, gvr, recursive, outputFormat)
    48  }
    49  
    50  // Factored out for testability
    51  func printModelDescriptionWithGenerator(
    52  	generator Generator,
    53  	fieldsPath []string,
    54  	w io.Writer,
    55  	client openapi.Client,
    56  	gvr schema.GroupVersionResource,
    57  	recursive bool,
    58  	outputFormat string,
    59  ) error {
    60  	paths, err := client.Paths()
    61  
    62  	if err != nil {
    63  		return fmt.Errorf("failed to fetch list of groupVersions: %w", err)
    64  	}
    65  
    66  	var resourcePath string
    67  	if len(gvr.Group) == 0 {
    68  		resourcePath = fmt.Sprintf("api/%s", gvr.Version)
    69  	} else {
    70  		resourcePath = fmt.Sprintf("apis/%s/%s", gvr.Group, gvr.Version)
    71  	}
    72  
    73  	gv, exists := paths[resourcePath]
    74  
    75  	if !exists {
    76  		return fmt.Errorf("couldn't find resource for \"%v\"", gvr)
    77  	}
    78  
    79  	openAPISchemaBytes, err := gv.Schema(runtime.ContentTypeJSON)
    80  	if err != nil {
    81  		return fmt.Errorf("failed to fetch openapi schema for %s: %w", resourcePath, err)
    82  	}
    83  
    84  	var parsedV3Schema map[string]interface{}
    85  	if err := json.Unmarshal(openAPISchemaBytes, &parsedV3Schema); err != nil {
    86  		return fmt.Errorf("failed to parse openapi schema for %s: %w", resourcePath, err)
    87  	}
    88  
    89  	err = generator.Render(outputFormat, parsedV3Schema, gvr, fieldsPath, recursive, w)
    90  
    91  	explainErr := explainError("")
    92  	if errors.As(err, &explainErr) {
    93  		return explainErr
    94  	}
    95  
    96  	return err
    97  }
    98  

View as plain text