...

Source file src/helm.sh/helm/v3/pkg/engine/lookup_func.go

Documentation: helm.sh/helm/v3/pkg/engine

     1  /*
     2  Copyright The Helm 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 engine
    18  
    19  import (
    20  	"context"
    21  	"log"
    22  	"strings"
    23  
    24  	"github.com/pkg/errors"
    25  	apierrors "k8s.io/apimachinery/pkg/api/errors"
    26  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    27  	"k8s.io/apimachinery/pkg/runtime/schema"
    28  	"k8s.io/client-go/discovery"
    29  	"k8s.io/client-go/dynamic"
    30  	"k8s.io/client-go/rest"
    31  )
    32  
    33  type lookupFunc = func(apiversion string, resource string, namespace string, name string) (map[string]interface{}, error)
    34  
    35  // NewLookupFunction returns a function for looking up objects in the cluster.
    36  //
    37  // If the resource does not exist, no error is raised.
    38  //
    39  // This function is considered deprecated, and will be renamed in Helm 4. It will no
    40  // longer be a public function.
    41  func NewLookupFunction(config *rest.Config) lookupFunc {
    42  	return newLookupFunction(clientProviderFromConfig{config: config})
    43  }
    44  
    45  type ClientProvider interface {
    46  	// GetClientFor returns a dynamic.NamespaceableResourceInterface suitable for interacting with resources
    47  	// corresponding to the provided apiVersion and kind, as well as a boolean indicating whether the resources
    48  	// are namespaced.
    49  	GetClientFor(apiVersion, kind string) (dynamic.NamespaceableResourceInterface, bool, error)
    50  }
    51  
    52  type clientProviderFromConfig struct {
    53  	config *rest.Config
    54  }
    55  
    56  func (c clientProviderFromConfig) GetClientFor(apiVersion, kind string) (dynamic.NamespaceableResourceInterface, bool, error) {
    57  	return getDynamicClientOnKind(apiVersion, kind, c.config)
    58  }
    59  
    60  func newLookupFunction(clientProvider ClientProvider) lookupFunc {
    61  	return func(apiversion string, kind string, namespace string, name string) (map[string]interface{}, error) {
    62  		var client dynamic.ResourceInterface
    63  		c, namespaced, err := clientProvider.GetClientFor(apiversion, kind)
    64  		if err != nil {
    65  			return map[string]interface{}{}, err
    66  		}
    67  		if namespaced && namespace != "" {
    68  			client = c.Namespace(namespace)
    69  		} else {
    70  			client = c
    71  		}
    72  		if name != "" {
    73  			// this will return a single object
    74  			obj, err := client.Get(context.Background(), name, metav1.GetOptions{})
    75  			if err != nil {
    76  				if apierrors.IsNotFound(err) {
    77  					// Just return an empty interface when the object was not found.
    78  					// That way, users can use `if not (lookup ...)` in their templates.
    79  					return map[string]interface{}{}, nil
    80  				}
    81  				return map[string]interface{}{}, err
    82  			}
    83  			return obj.UnstructuredContent(), nil
    84  		}
    85  		// this will return a list
    86  		obj, err := client.List(context.Background(), metav1.ListOptions{})
    87  		if err != nil {
    88  			if apierrors.IsNotFound(err) {
    89  				// Just return an empty interface when the object was not found.
    90  				// That way, users can use `if not (lookup ...)` in their templates.
    91  				return map[string]interface{}{}, nil
    92  			}
    93  			return map[string]interface{}{}, err
    94  		}
    95  		return obj.UnstructuredContent(), nil
    96  	}
    97  }
    98  
    99  // getDynamicClientOnKind returns a dynamic client on an Unstructured type. This client can be further namespaced.
   100  func getDynamicClientOnKind(apiversion string, kind string, config *rest.Config) (dynamic.NamespaceableResourceInterface, bool, error) {
   101  	gvk := schema.FromAPIVersionAndKind(apiversion, kind)
   102  	apiRes, err := getAPIResourceForGVK(gvk, config)
   103  	if err != nil {
   104  		log.Printf("[ERROR] unable to get apiresource from unstructured: %s , error %s", gvk.String(), err)
   105  		return nil, false, errors.Wrapf(err, "unable to get apiresource from unstructured: %s", gvk.String())
   106  	}
   107  	gvr := schema.GroupVersionResource{
   108  		Group:    apiRes.Group,
   109  		Version:  apiRes.Version,
   110  		Resource: apiRes.Name,
   111  	}
   112  	intf, err := dynamic.NewForConfig(config)
   113  	if err != nil {
   114  		log.Printf("[ERROR] unable to get dynamic client %s", err)
   115  		return nil, false, err
   116  	}
   117  	res := intf.Resource(gvr)
   118  	return res, apiRes.Namespaced, nil
   119  }
   120  
   121  func getAPIResourceForGVK(gvk schema.GroupVersionKind, config *rest.Config) (metav1.APIResource, error) {
   122  	res := metav1.APIResource{}
   123  	discoveryClient, err := discovery.NewDiscoveryClientForConfig(config)
   124  	if err != nil {
   125  		log.Printf("[ERROR] unable to create discovery client %s", err)
   126  		return res, err
   127  	}
   128  	resList, err := discoveryClient.ServerResourcesForGroupVersion(gvk.GroupVersion().String())
   129  	if err != nil {
   130  		log.Printf("[ERROR] unable to retrieve resource list for: %s , error: %s", gvk.GroupVersion().String(), err)
   131  		return res, err
   132  	}
   133  	for _, resource := range resList.APIResources {
   134  		// if a resource contains a "/" it's referencing a subresource. we don't support suberesource for now.
   135  		if resource.Kind == gvk.Kind && !strings.Contains(resource.Name, "/") {
   136  			res = resource
   137  			res.Group = gvk.Group
   138  			res.Version = gvk.Version
   139  			break
   140  		}
   141  	}
   142  	return res, nil
   143  }
   144  

View as plain text