...

Source file src/k8s.io/apiextensions-apiserver/pkg/apiserver/helpers.go

Documentation: k8s.io/apiextensions-apiserver/pkg/apiserver

     1  /*
     2  Copyright 2019 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 apiserver
    18  
    19  import (
    20  	"fmt"
    21  
    22  	apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
    23  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    24  )
    25  
    26  var swaggerMetadataDescriptions = metav1.ObjectMeta{}.SwaggerDoc()
    27  
    28  // getColumnsForVersion returns the columns for given version or nil.
    29  // NOTE: the newly logically-defaulted columns is not pointing to the original CRD object.
    30  // One cannot mutate the original CRD columns using the logically-defaulted columns. Please iterate through
    31  // the original CRD object instead.
    32  func getColumnsForVersion(crd *apiextensionsv1.CustomResourceDefinition, version string) ([]apiextensionsv1.CustomResourceColumnDefinition, error) {
    33  	for _, v := range crd.Spec.Versions {
    34  		if version == v.Name {
    35  			return serveDefaultColumnsIfEmpty(v.AdditionalPrinterColumns), nil
    36  		}
    37  	}
    38  	return nil, fmt.Errorf("version %s not found in apiextensionsv1.CustomResourceDefinition: %v", version, crd.Name)
    39  }
    40  
    41  // getScaleColumnsForVersion returns 2 columns for the desired and actual number of replicas.
    42  func getScaleColumnsForVersion(crd *apiextensionsv1.CustomResourceDefinition, version string) ([]apiextensionsv1.CustomResourceColumnDefinition, error) {
    43  	for _, v := range crd.Spec.Versions {
    44  		if version != v.Name {
    45  			continue
    46  		}
    47  		var cols []apiextensionsv1.CustomResourceColumnDefinition
    48  		if v.Subresources != nil && v.Subresources.Scale != nil {
    49  			if v.Subresources.Scale.SpecReplicasPath != "" {
    50  				cols = append(cols, apiextensionsv1.CustomResourceColumnDefinition{
    51  					Name:        "Desired",
    52  					Type:        "integer",
    53  					Description: "Number of desired replicas",
    54  					JSONPath:    ".spec.replicas",
    55  				})
    56  			}
    57  			if v.Subresources.Scale.StatusReplicasPath != "" {
    58  				cols = append(cols, apiextensionsv1.CustomResourceColumnDefinition{
    59  					Name:        "Available",
    60  					Type:        "integer",
    61  					Description: "Number of actual replicas",
    62  					JSONPath:    ".status.replicas",
    63  				})
    64  			}
    65  		}
    66  		cols = append(cols, apiextensionsv1.CustomResourceColumnDefinition{
    67  			Name:        "Age",
    68  			Type:        "date",
    69  			Description: swaggerMetadataDescriptions["creationTimestamp"],
    70  			JSONPath:    ".metadata.creationTimestamp",
    71  		})
    72  		return cols, nil
    73  	}
    74  	return nil, fmt.Errorf("version %s not found in apiextensionsv1.CustomResourceDefinition: %v", version, crd.Name)
    75  }
    76  
    77  // serveDefaultColumnsIfEmpty applies logically defaulting to columns, if the input columns is empty.
    78  // NOTE: in this way, the newly logically-defaulted columns is not pointing to the original CRD object.
    79  // One cannot mutate the original CRD columns using the logically-defaulted columns. Please iterate through
    80  // the original CRD object instead.
    81  func serveDefaultColumnsIfEmpty(columns []apiextensionsv1.CustomResourceColumnDefinition) []apiextensionsv1.CustomResourceColumnDefinition {
    82  	if len(columns) > 0 {
    83  		return columns
    84  	}
    85  	return []apiextensionsv1.CustomResourceColumnDefinition{
    86  		{Name: "Age", Type: "date", Description: swaggerMetadataDescriptions["creationTimestamp"], JSONPath: ".metadata.creationTimestamp"},
    87  	}
    88  }
    89  

View as plain text