github.com/stefanmcshane/helm@v0.0.0-20221213002717-88a4a2c6e77d/pkg/engine/lookup_func.go (about)

     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 func(apiversion string, resource string, namespace string, name string) (map[string]interface{}, error) {
    43  		var client dynamic.ResourceInterface
    44  		c, namespaced, err := getDynamicClientOnKind(apiversion, resource, config)
    45  		if err != nil {
    46  			return map[string]interface{}{}, err
    47  		}
    48  		if namespaced && namespace != "" {
    49  			client = c.Namespace(namespace)
    50  		} else {
    51  			client = c
    52  		}
    53  		if name != "" {
    54  			// this will return a single object
    55  			obj, err := client.Get(context.Background(), name, metav1.GetOptions{})
    56  			if err != nil {
    57  				if apierrors.IsNotFound(err) {
    58  					// Just return an empty interface when the object was not found.
    59  					// That way, users can use `if not (lookup ...)` in their templates.
    60  					return map[string]interface{}{}, nil
    61  				}
    62  				return map[string]interface{}{}, err
    63  			}
    64  			return obj.UnstructuredContent(), nil
    65  		}
    66  		// this will return a list
    67  		obj, err := client.List(context.Background(), metav1.ListOptions{})
    68  		if err != nil {
    69  			if apierrors.IsNotFound(err) {
    70  				// Just return an empty interface when the object was not found.
    71  				// That way, users can use `if not (lookup ...)` in their templates.
    72  				return map[string]interface{}{}, nil
    73  			}
    74  			return map[string]interface{}{}, err
    75  		}
    76  		return obj.UnstructuredContent(), nil
    77  	}
    78  }
    79  
    80  // getDynamicClientOnKind returns a dynamic client on an Unstructured type. This client can be further namespaced.
    81  func getDynamicClientOnKind(apiversion string, kind string, config *rest.Config) (dynamic.NamespaceableResourceInterface, bool, error) {
    82  	gvk := schema.FromAPIVersionAndKind(apiversion, kind)
    83  	apiRes, err := getAPIResourceForGVK(gvk, config)
    84  	if err != nil {
    85  		log.Printf("[ERROR] unable to get apiresource from unstructured: %s , error %s", gvk.String(), err)
    86  		return nil, false, errors.Wrapf(err, "unable to get apiresource from unstructured: %s", gvk.String())
    87  	}
    88  	gvr := schema.GroupVersionResource{
    89  		Group:    apiRes.Group,
    90  		Version:  apiRes.Version,
    91  		Resource: apiRes.Name,
    92  	}
    93  	intf, err := dynamic.NewForConfig(config)
    94  	if err != nil {
    95  		log.Printf("[ERROR] unable to get dynamic client %s", err)
    96  		return nil, false, err
    97  	}
    98  	res := intf.Resource(gvr)
    99  	return res, apiRes.Namespaced, nil
   100  }
   101  
   102  func getAPIResourceForGVK(gvk schema.GroupVersionKind, config *rest.Config) (metav1.APIResource, error) {
   103  	res := metav1.APIResource{}
   104  	discoveryClient, err := discovery.NewDiscoveryClientForConfig(config)
   105  	if err != nil {
   106  		log.Printf("[ERROR] unable to create discovery client %s", err)
   107  		return res, err
   108  	}
   109  	resList, err := discoveryClient.ServerResourcesForGroupVersion(gvk.GroupVersion().String())
   110  	if err != nil {
   111  		log.Printf("[ERROR] unable to retrieve resource list for: %s , error: %s", gvk.GroupVersion().String(), err)
   112  		return res, err
   113  	}
   114  	for _, resource := range resList.APIResources {
   115  		// if a resource contains a "/" it's referencing a subresource. we don't support suberesource for now.
   116  		if resource.Kind == gvk.Kind && !strings.Contains(resource.Name, "/") {
   117  			res = resource
   118  			res.Group = gvk.Group
   119  			res.Version = gvk.Version
   120  			break
   121  		}
   122  	}
   123  	return res, nil
   124  }