github.com/GoogleCloudPlatform/terraformer@v0.8.18/providers/kubernetes/kind.go (about)

     1  // Copyright 2018 The Terraformer Authors.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package kubernetes
    16  
    17  import (
    18  	"context"
    19  	"reflect"
    20  
    21  	"github.com/GoogleCloudPlatform/terraformer/terraformutils"
    22  
    23  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    24  	"k8s.io/client-go/kubernetes"
    25  )
    26  
    27  type Kind struct {
    28  	KubernetesService
    29  	Name       string
    30  	Group      string
    31  	Version    string
    32  	Namespaced bool
    33  }
    34  
    35  // Generate TerraformResources from Kubernetes API,
    36  // from each kubernetes object 1 TerraformResource.
    37  // Use UID as the resource IDs.
    38  func (k *Kind) InitResources() error {
    39  	config, _, err := initClientAndConfig()
    40  	if err != nil {
    41  		return err
    42  	}
    43  
    44  	clientset, err := kubernetes.NewForConfig(config)
    45  	if err != nil {
    46  		return err
    47  	}
    48  
    49  	group := reflect.ValueOf(clientset).MethodByName(
    50  		extractClientSetFuncGroupName(k.Group, k.Version)).Call(
    51  		[]reflect.Value{})[0]
    52  
    53  	param := []reflect.Value{}
    54  	namespace := ""
    55  	if k.Namespaced {
    56  		param = append(param, reflect.ValueOf(namespace))
    57  	}
    58  
    59  	resource := group.MethodByName(extractClientSetFuncTypeName(k.Name)).Call(param)[0]
    60  
    61  	results := resource.MethodByName("List").Call([]reflect.Value{reflect.ValueOf(context.Background()),
    62  		reflect.ValueOf(metav1.ListOptions{})})
    63  
    64  	if !results[1].IsNil() {
    65  		return results[1].Interface().(error)
    66  	}
    67  	items := reflect.Indirect(results[0]).FieldByName("Items")
    68  
    69  	for i := 0; i < items.Len(); i++ {
    70  		item := items.Index(i)
    71  		// Filter to resources that aren't owned by any other resource
    72  		if item.FieldByName("OwnerReferences").Len() > 0 {
    73  			continue
    74  		}
    75  
    76  		name := ""
    77  		if k.Namespaced {
    78  			name = item.FieldByName("Namespace").String() + "/" + item.FieldByName("Name").String()
    79  		} else {
    80  			name = item.FieldByName("Name").String()
    81  		}
    82  
    83  		k.Resources = append(k.Resources, terraformutils.NewSimpleResource(
    84  			name,
    85  			name,
    86  			extractTfResourceName(k.Name),
    87  			"kubernetes",
    88  			[]string{},
    89  		))
    90  	}
    91  	return nil
    92  }