sigs.k8s.io/cluster-api@v1.6.3/docs/book/src/clusterctl/developers.md (about)

     1  # clusterctl for Developers
     2  
     3  This document describes how to use `clusterctl` during the development workflow.
     4  
     5  ## Prerequisites
     6  
     7  * A Cluster API development setup (go, git, kind v0.9 or newer, Docker v19.03 or newer etc.)
     8  * A local clone of the Cluster API GitHub repository
     9  * A local clone of the GitHub repositories for the providers you want to install
    10  
    11  ## Build clusterctl
    12  
    13  From the root of the local copy of Cluster API, you can build the `clusterctl` binary by running:
    14  
    15  ```bash
    16  make clusterctl
    17  ```
    18  
    19  The output of the build is saved in the `bin/` folder; In order to use it you have to specify
    20  the full path, create an alias or copy it into a folder under your `$PATH`.
    21  
    22  ## Use local artifacts
    23  
    24  Clusterctl by default uses artifacts published in the [providers repositories];
    25  during the development workflow you may want to use artifacts from your local workstation.
    26  
    27  There are two options to do so:
    28  
    29  * Use the [overrides layer], when you want to override a single published artifact with a local one.
    30  * Create a local repository, when you want to avoid using published artifacts and use the local ones instead.
    31  
    32  If you want to create a local artifact, follow these instructions:
    33  
    34  ### Build artifacts locally
    35  
    36  In order to build artifacts for the CAPI core provider, the kubeadm bootstrap provider, the kubeadm control plane provider and the Docker infrastructure provider:
    37  
    38  ```bash
    39  make docker-build REGISTRY=gcr.io/k8s-staging-cluster-api PULL_POLICY=IfNotPresent
    40  ```
    41  
    42  ### Create a clusterctl-settings.json file
    43  
    44  Next, create a `clusterctl-settings.json` file and place it in your local copy
    45  of Cluster API. This file will be used by [create-local-repository.py](#create-the-local-repository). Here is an example:
    46  
    47  ```yaml
    48  {
    49    "providers": ["cluster-api","bootstrap-kubeadm","control-plane-kubeadm", "infrastructure-aws", "infrastructure-docker"],
    50    "provider_repos": ["../cluster-api-provider-aws"]
    51  }
    52  ```
    53  
    54  **providers** (Array[]String, default=[]): A list of the providers to enable.
    55  See [available providers](#available-providers) for more details.
    56  
    57  **provider_repos** (Array[]String, default=[]): A list of paths to all the providers you want to use. Each provider must have
    58  a `clusterctl-settings.json` file describing how to build the provider assets.
    59  
    60  ### Create the local repository
    61  
    62  Run the create-local-repository hack from the root of the local copy of Cluster API:
    63  
    64  ```bash
    65  cmd/clusterctl/hack/create-local-repository.py
    66  ```
    67  
    68  The script reads from the source folders for the providers you want to install, builds the providers' assets,
    69  and places them in a local repository folder located under `$XDG_CONFIG_HOME/cluster-api/dev-repository/`.
    70  Additionally, the command output provides you the `clusterctl init` command with all the necessary flags.
    71  The output should be similar to:
    72  
    73  ```bash
    74  clusterctl local overrides generated from local repositories for the cluster-api, bootstrap-kubeadm, control-plane-kubeadm, infrastructure-docker, infrastructure-aws providers.
    75  in order to use them, please run:
    76  
    77  clusterctl init \
    78     --core cluster-api:v0.3.8 \
    79     --bootstrap kubeadm:v0.3.8 \
    80     --control-plane kubeadm:v0.3.8 \
    81     --infrastructure aws:v0.5.0 \
    82     --infrastructure docker:v0.3.8 \
    83     --config $XDG_CONFIG_HOME/cluster-api/dev-repository/config.yaml
    84  ```
    85  
    86  As you might notice, the command is using the `$XDG_CONFIG_HOME/cluster-api/dev-repository/config.yaml` config file,
    87  containing all the required setting to make clusterctl use the local repository.
    88  
    89  <aside class="note warning">
    90  
    91  <h1>Warnings</h1>
    92  
    93  You must pass `--config $XDG_CONFIG_HOME/cluster-api/dev-repository/config.yaml` to all the clusterctl commands you are running
    94  during your dev session.
    95  
    96  The above config file changes the location of the [overrides layer] folder thus ensuring
    97  you dev session isn't hijacked by other local artifacts.
    98  
    99  With the only exception of the Docker provider, the local repository folder does not contain cluster templates,
   100  so the `clusterctl generate cluster` command will fail.
   101  
   102  </aside>
   103  
   104  #### Available providers
   105  
   106  The following providers are currently defined in the script:
   107  
   108  * `cluster-api`
   109  * `bootstrap-kubeadm`
   110  * `control-plane-kubeadm`
   111  * `infrastructure-docker`
   112  
   113  More providers can be added by editing the `clusterctl-settings.json` in your local copy of Cluster API;
   114  please note that each `provider_repo` should have its own `clusterctl-settings.json` describing how to build the provider assets, e.g.
   115  
   116  ```json
   117  {
   118    "name": "infrastructure-aws",
   119    "config": {
   120      "componentsFile": "infrastructure-components.yaml",
   121      "nextVersion": "v0.5.0"
   122    }
   123  }
   124  ```
   125  
   126  ## Create a kind management cluster
   127  
   128  [kind] can provide a Kubernetes cluster to be used as a management cluster.
   129  See [Install and/or configure a Kubernetes cluster] for more information.
   130  
   131  *Before* running clusterctl init, you must ensure all the required images are available in the kind cluster.
   132  
   133  This is always the case for images published in some image repository like Docker Hub or gcr.io, but it can't be
   134  the case for images built locally; in this case, you can use `kind load` to move the images built locally. e.g.
   135  
   136  ```bash
   137  kind load docker-image gcr.io/k8s-staging-cluster-api/cluster-api-controller-amd64:dev
   138  kind load docker-image gcr.io/k8s-staging-cluster-api/kubeadm-bootstrap-controller-amd64:dev
   139  kind load docker-image gcr.io/k8s-staging-cluster-api/kubeadm-control-plane-controller-amd64:dev
   140  kind load docker-image gcr.io/k8s-staging-cluster-api/capd-manager-amd64:dev
   141  ```
   142  
   143  to make the controller images available for the kubelet in the management cluster.
   144  
   145  When the kind cluster is ready and all the required images are in place, run
   146  the clusterctl init command generated by the create-local-repository.py
   147  script.
   148  
   149  Optionally, you may want to check if the components are running properly. The
   150  exact components are dependent on which providers you have initialized. Below
   151  is an example output with the Docker provider being installed.
   152  
   153  ```bash
   154  kubectl get deploy -A | grep  "cap\|cert"
   155  capd-system
   156  ```
   157  ```bash
   158  capd-controller-manager                         1/1     1            1           25m
   159  capi-kubeadm-bootstrap-system       capi-kubeadm-bootstrap-controller-manager       1/1     1            1           25m
   160  capi-kubeadm-control-plane-system   capi-kubeadm-control-plane-controller-manager   1/1     1            1           25m
   161  capi-system                         capi-controller-manager                         1/1     1            1           25m
   162  cert-manager                        cert-manager                                    1/1     1            1           27m
   163  cert-manager                        cert-manager-cainjector                         1/1     1            1           27m
   164  cert-manager                        cert-manager-webhook                            1/1     1            1           27m
   165  ```
   166  
   167  ## Additional Notes for the Docker Provider
   168  
   169  ### Select the appropriate Kubernetes version
   170  
   171  When selecting the `--kubernetes-version`, ensure that the `kindest/node`
   172  image is available.
   173  
   174  For example, assuming that on [docker hub][kind-docker-hub] there is no
   175  image for version `vX.Y.Z`, therefore creating a CAPD workload cluster with
   176  `--kubernetes-version=vX.Y.Z` will fail. See [issue 3795] for more details.
   177  
   178  ### Get the kubeconfig for the workload cluster when using Docker Desktop
   179  
   180  For Docker Desktop on macOS, Linux or Windows use kind to retrieve the kubeconfig.
   181  
   182  ```bash
   183  kind get kubeconfig --name capi-quickstart > capi-quickstart.kubeconfig
   184  ````
   185  
   186  Docker Engine for Linux works with the default clusterctl approach.
   187  ```bash
   188  clusterctl get kubeconfig capi-quickstart > capi-quickstart.kubeconfig
   189  ```
   190  
   191  ### Fix kubeconfig when using Docker Desktop and clusterctl
   192  When retrieving the kubeconfig using `clusterctl` with Docker Desktop on macOS or Windows or Docker Desktop (Docker Engine works fine) on Linux, you'll need to take a few extra steps to get the kubeconfig for a workload cluster created with the Docker provider.
   193  
   194  ```bash
   195  clusterctl get kubeconfig capi-quickstart > capi-quickstart.kubeconfig
   196  ```
   197  
   198  To fix the kubeconfig run:
   199  ```bash
   200  # Point the kubeconfig to the exposed port of the load balancer, rather than the inaccessible container IP.
   201  sed -i -e "s/server:.*/server: https:\/\/$(docker port capi-quickstart-lb 6443/tcp | sed "s/0.0.0.0/127.0.0.1/")/g" ./capi-quickstart.kubeconfig
   202  ```
   203  
   204  <!-- links -->
   205  [kind]: https://kind.sigs.k8s.io/
   206  [providers repositories]: configuration.md#provider-repositories
   207  [overrides layer]: configuration.md#overrides-layer
   208  [Install and/or configure a Kubernetes cluster]: ../user/quick-start.md#install-andor-configure-a-kubernetes-cluster
   209  [kind-docker-hub]: https://hub.docker.com/r/kindest/node/tags
   210  [issue 3795]: https://github.com/kubernetes-sigs/cluster-api/issues/3795