...

Source file src/k8s.io/cli-runtime/pkg/printers/sourcechecker.go

Documentation: k8s.io/cli-runtime/pkg/printers

     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 printers
    18  
    19  import (
    20  	"strings"
    21  )
    22  
    23  var (
    24  	InternalObjectPrinterErr = "a versioned object must be passed to a printer"
    25  
    26  	// disallowedPackagePrefixes contains regular expression templates
    27  	// for object package paths that are not allowed by printers.
    28  	disallowedPackagePrefixes = []string{
    29  		"k8s.io/kubernetes/pkg/apis/",
    30  	}
    31  )
    32  
    33  var InternalObjectPreventer = &illegalPackageSourceChecker{disallowedPackagePrefixes}
    34  
    35  func IsInternalObjectError(err error) bool {
    36  	if err == nil {
    37  		return false
    38  	}
    39  
    40  	return err.Error() == InternalObjectPrinterErr
    41  }
    42  
    43  // illegalPackageSourceChecker compares a given
    44  // object's package path, and determines if the
    45  // object originates from a disallowed source.
    46  type illegalPackageSourceChecker struct {
    47  	// disallowedPrefixes is a slice of disallowed package path
    48  	// prefixes for a given runtime.Object that we are printing.
    49  	disallowedPrefixes []string
    50  }
    51  
    52  func (c *illegalPackageSourceChecker) IsForbidden(pkgPath string) bool {
    53  	for _, forbiddenPrefix := range c.disallowedPrefixes {
    54  		if strings.HasPrefix(pkgPath, forbiddenPrefix) || strings.Contains(pkgPath, "/vendor/"+forbiddenPrefix) {
    55  			return true
    56  		}
    57  	}
    58  
    59  	return false
    60  }
    61  

View as plain text