|
| 1 | +# Using an external Type |
| 2 | + |
| 3 | + |
| 4 | +# Introduction |
| 5 | + |
| 6 | +There are several different external types that may be referenced when writing a controller. |
| 7 | +* A Custom Resource Definition (CRD) that is defined in the current project `kubebuilder create api`. |
| 8 | +* A Core Kubernetes Resources eg. `kubebuilder create api --group apps --version v1 --kind Deployment`. |
| 9 | +* A CRD that is created and installed in another project. |
| 10 | +* A CR defined via an API Aggregation (AA). Aggregated APIs are subordinate APIServers that sit behind the primary API server, which acts as a proxy. |
| 11 | + |
| 12 | +Currrently Kubebuilder handles the first two, CRDs and Core Resources, seamlessly. External CRDs and CRs created via Aggregation must be scaffolded manually. |
| 13 | + |
| 14 | +In order to use a Kubernetes Custom Resource that has been defined in another project |
| 15 | +you will need to have several items of information. |
| 16 | +* The Domain of the CR |
| 17 | +* The Group under the Domain |
| 18 | +* The Go import path of the CR Type definition. |
| 19 | + |
| 20 | +The Domain and Group variables have been discussed in other parts of the documentation. The import path would be located in the project that installs the CR. |
| 21 | + |
| 22 | +Example API Aggregation directory structure |
| 23 | +``` |
| 24 | +github.com |
| 25 | + ├── example |
| 26 | + ├── myproject |
| 27 | + ├── apis |
| 28 | + ├── mygroup |
| 29 | + ├── doc.go |
| 30 | + ├── install |
| 31 | + │ ├── install.go |
| 32 | + ├── v1alpha1 |
| 33 | + │ ├── doc.go |
| 34 | + │ ├── register.go |
| 35 | + │ ├── types.go |
| 36 | + │ ├── zz_generated.deepcopy.go |
| 37 | +``` |
| 38 | + |
| 39 | +In the case above the import path would be `github.com/example/myproject/apis/mygroup/v1alpha1` |
| 40 | + |
| 41 | +### Create a project |
| 42 | + |
| 43 | +``` |
| 44 | +kubebuilder init --domain $APIDOMAIN --owner "MyCompany" |
| 45 | +``` |
| 46 | + |
| 47 | +### Add a controller |
| 48 | + |
| 49 | +be sure to answer no when it asks if you would like to create an api? [Y/n] |
| 50 | +``` |
| 51 | +kubebuilder create api --group $APIGROUP --version $APIVERSION --kind MyKind |
| 52 | +
|
| 53 | +``` |
| 54 | + |
| 55 | +## Edit the Api files. |
| 56 | + |
| 57 | +### Register your Types |
| 58 | + |
| 59 | +Add the following file to the pkg/apis directory |
| 60 | + |
| 61 | +file: pkg/apis/mytype_addtoscheme.go |
| 62 | +``` |
| 63 | +package apis |
| 64 | +
|
| 65 | +import ( |
| 66 | + mygroupv1alpha1 "github.com/username/myapirepo/apis/mygroup/v1alpha1" |
| 67 | +) |
| 68 | +
|
| 69 | +func init() { |
| 70 | + // Register the types with the Scheme so the components can map objects |
| 71 | + // to GroupVersionKinds and back |
| 72 | + AddToSchemes = append(AddToSchemes, mygroupv1alpha1.AddToScheme) |
| 73 | +} |
| 74 | +
|
| 75 | +``` |
| 76 | + |
| 77 | +## Edit the Controller files |
| 78 | + |
| 79 | +### Use the correct import for your api |
| 80 | + |
| 81 | +file: pkg/controllers/mytype_controller.go |
| 82 | +``` |
| 83 | +import mygroupv1alpha1 "github.com/example/myproject/apis/mygroup/v1alpha1" |
| 84 | +
|
| 85 | +``` |
| 86 | + |
| 87 | +## Prepare for testing |
| 88 | + |
| 89 | +#### Register your resource |
| 90 | + |
| 91 | +Add the registration code to the test framework |
| 92 | + |
| 93 | +file pkg/controller/my_agg_resource_controller_suite_test.go |
| 94 | +``` |
| 95 | +import ( |
| 96 | +
|
| 97 | +) |
| 98 | +var cfg *rest.Config |
| 99 | +
|
| 100 | +func TestMain(m *testing.M) { |
| 101 | + // Get a config to talk to the apiserver |
| 102 | + t := &envtest.Environment{ |
| 103 | + Config: cfg, |
| 104 | + CRDDirectoryPaths: []string{filepath.Join("..", "..", "..", "config", "crds")}, |
| 105 | + UseExistingCluster: true, |
| 106 | + } |
| 107 | +
|
| 108 | + apis.AddToScheme(scheme.Scheme) |
| 109 | +
|
| 110 | + var err error |
| 111 | + if cfg, err = t.Start(); err != nil { |
| 112 | + log.Fatal(err) |
| 113 | + } |
| 114 | +
|
| 115 | + code := m.Run() |
| 116 | + t.Stop() |
| 117 | + os.Exit(code) |
| 118 | +} |
| 119 | +
|
| 120 | +``` |
| 121 | + |
| 122 | +### Update dependencies |
| 123 | + |
| 124 | +``` |
| 125 | +dep ensure --add |
| 126 | +``` |
| 127 | + |
| 128 | +## Helpful Tips |
| 129 | + |
| 130 | +### Locate your domain and group variables |
| 131 | + |
| 132 | +The following kubectl commands may be useful |
| 133 | + |
| 134 | +``` |
| 135 | +kubectl api-resources --verbs=list -o name |
| 136 | +
|
| 137 | +kubectl api-resources --verbs=list -o name | grep mydomain.com |
| 138 | +``` |
| 139 | + |
0 commit comments