...

Source file src/sigs.k8s.io/kustomize/api/internal/accumulator/loadconfigfromcrds_test.go

Documentation: sigs.k8s.io/kustomize/api/internal/accumulator

     1  // Copyright 2019 The Kubernetes Authors.
     2  // SPDX-License-Identifier: Apache-2.0
     3  
     4  package accumulator_test
     5  
     6  import (
     7  	"reflect"
     8  	"testing"
     9  
    10  	"github.com/stretchr/testify/require"
    11  	. "sigs.k8s.io/kustomize/api/internal/accumulator"
    12  	"sigs.k8s.io/kustomize/api/internal/loader"
    13  	"sigs.k8s.io/kustomize/api/internal/plugins/builtinconfig"
    14  	"sigs.k8s.io/kustomize/api/types"
    15  	"sigs.k8s.io/kustomize/kyaml/filesys"
    16  	"sigs.k8s.io/kustomize/kyaml/resid"
    17  )
    18  
    19  // This defines two CRD's:  Bee and MyKind.
    20  //
    21  // Bee is boring, it's spec has no dependencies.
    22  //
    23  // MyKind, however, has a spec that contains
    24  // a Bee and a (k8s native) Secret.
    25  const (
    26  	crdContent = `
    27  {
    28  	"github.com/example/pkg/apis/jingfang/v1beta1.Bee": {
    29  		"Schema": {
    30  			"description": "Bee",
    31  			"properties": {
    32  				"apiVersion": {
    33  					"description": "APIVersion defines the versioned schema of this representation of an object. Servers should convert 
    34  recognized schemas to the latest internal value, and may reject unrecognized values. 
    35  More info: https://git.k8s.io/community/contributors/devel/api-conventions.md#resources",
    36  					"type": "string"
    37  				},
    38  				"kind": {
    39  					"description": "Kind is a string value representing the REST resource this object represents. Servers may infer
    40  this from the endpoint the client submits requests to. Cannot be updated. In CamelCase.
    41  More info: https://git.k8s.io/community/contributors/devel/api-conventions.md#types-kinds",
    42  					"type": "string"
    43  				},
    44  				"metadata": {
    45  					"$ref": "k8s.io/apimachinery/pkg/apis/meta/v1.ObjectMeta"
    46  				},
    47  				"spec": {
    48  					"$ref": "github.com/example/pkg/apis/jingfang/v1beta1.BeeSpec"
    49  				},
    50  				"status": {
    51  					"$ref": "github.com/example/pkg/apis/jingfang/v1beta1.BeeStatus"
    52  				}
    53  			}
    54  		},
    55  		"Dependencies": [
    56  			"github.com/example/pkg/apis/jingfang/v1beta1.BeeSpec",
    57  			"github.com/example/pkg/apis/jingfang/v1beta1.BeeStatus",
    58  			"k8s.io/apimachinery/pkg/apis/meta/v1.ObjectMeta"
    59  		]
    60  	},
    61  	"github.com/example/pkg/apis/jingfang/v1beta1.BeeSpec": {
    62  		"Schema": {
    63  			"description": "BeeSpec defines the desired state of Bee"
    64  		},
    65  		"Dependencies": []
    66  	},
    67  	"github.com/example/pkg/apis/jingfang/v1beta1.BeeStatus": {
    68  		"Schema": {
    69  			"description": "BeeStatus defines the observed state of Bee"
    70  		},
    71  		"Dependencies": []
    72  	},
    73  	"github.com/example/pkg/apis/jingfang/v1beta1.MyKind": {
    74  		"Schema": {
    75  			"description": "MyKind",
    76  			"properties": {
    77  				"apiVersion": {
    78  					"description": "APIVersion defines the versioned schema of this representation of an object.
    79  Servers should convert recognized schemas to the latest internal value, and may reject unrecognized values.
    80  More info: https://git.k8s.io/community/contributors/devel/api-conventions.md#resources",
    81  					"type": "string"
    82  				},
    83  				"kind": {
    84  					"description": "Kind is a string value representing the REST resource this object represents.
    85  Servers may infer this from the endpoint the client submits requests to. Cannot be updated.
    86  In CamelCase. More info: https://git.k8s.io/community/contributors/devel/api-conventions.md#types-kinds",
    87  					"type": "string"
    88  				},
    89  				"metadata": {
    90  					"$ref": "k8s.io/apimachinery/pkg/apis/meta/v1.ObjectMeta"
    91  				},
    92  				"spec": {
    93  					"$ref": "github.com/example/pkg/apis/jingfang/v1beta1.MyKindSpec"
    94  				},
    95  				"status": {
    96  					"$ref": "github.com/example/pkg/apis/jingfang/v1beta1.MyKindStatus"
    97  				}
    98  			}
    99  		},
   100  		"Dependencies": [
   101  			"github.com/example/pkg/apis/jingfang/v1beta1.MyKindSpec",
   102  			"github.com/example/pkg/apis/jingfang/v1beta1.MyKindStatus",
   103  			"k8s.io/apimachinery/pkg/apis/meta/v1.ObjectMeta"
   104  		]
   105  	},
   106  	"github.com/example/pkg/apis/jingfang/v1beta1.MyKindSpec": {
   107  		"Schema": {
   108  			"description": "MyKindSpec defines the desired state of MyKind",
   109  			"properties": {
   110  				"beeRef": {
   111  					"x-kubernetes-object-ref-api-version": "v1beta1",
   112  					"x-kubernetes-object-ref-kind": "Bee",
   113  					"$ref": "github.com/example/pkg/apis/jingfang/v1beta1.Bee"
   114  				},
   115  				"secretRef": {
   116  					"description": "If defined, we use this secret for configuring the MYSQL_ROOT_PASSWORD 
   117  If it is not set we generate a secret dynamically",
   118  					"x-kubernetes-object-ref-api-version": "v1",
   119  					"x-kubernetes-object-ref-kind": "Secret",
   120  					"$ref": "k8s.io/api/core/v1.LocalObjectReference"
   121  				}
   122  			}
   123  		},
   124  		"Dependencies": [
   125  			"github.com/example/pkg/apis/jingfang/v1beta1.Bee",
   126  			"k8s.io/api/core/v1.LocalObjectReference"
   127  		]
   128  	},
   129  	"github.com/example/pkg/apis/jingfang/v1beta1.MyKindStatus": {
   130  		"Schema": {
   131  			"description": "MyKindStatus defines the observed state of MyKind"
   132  		},
   133  		"Dependencies": []
   134  	}
   135  }
   136  `
   137  )
   138  
   139  func TestLoadCRDs(t *testing.T) {
   140  	nbrs := []builtinconfig.NameBackReferences{
   141  		{
   142  			Gvk: resid.Gvk{Kind: "Secret", Version: "v1"},
   143  			Referrers: []types.FieldSpec{
   144  				{
   145  					Gvk:  resid.Gvk{Kind: "MyKind"},
   146  					Path: "spec/secretRef/name",
   147  				},
   148  			},
   149  		},
   150  		{
   151  			Gvk: resid.Gvk{Kind: "Bee", Version: "v1beta1"},
   152  			Referrers: []types.FieldSpec{
   153  				{
   154  					Gvk:  resid.Gvk{Kind: "MyKind"},
   155  					Path: "spec/beeRef/name",
   156  				},
   157  			},
   158  		},
   159  	}
   160  
   161  	expectedTc := &builtinconfig.TransformerConfig{
   162  		NameReference: nbrs,
   163  	}
   164  
   165  	fSys := filesys.MakeFsInMemory()
   166  	err := fSys.WriteFile("/testpath/crd.json", []byte(crdContent))
   167  	require.NoError(t, err)
   168  	ldr, err := loader.NewLoader(loader.RestrictionRootOnly, "/testpath", fSys)
   169  	require.NoError(t, err)
   170  
   171  	actualTc, err := LoadConfigFromCRDs(ldr, []string{"crd.json"})
   172  	require.NoError(t, err)
   173  	if !reflect.DeepEqual(actualTc, expectedTc) {
   174  		t.Fatalf("expected\n %v\n but got\n %v\n", expectedTc, actualTc)
   175  	}
   176  }
   177  

View as plain text