github.com/wmuizelaar/kpt@v0.0.0-20221018115725-bd564717b2ed/site/book/03-packages/07-composing-a-package.md (about)

     1  A package can be _composed_ of subpackages (_HAS A_ relationship). _Package
     2  composition_ is when you change the package hierarchy by adding or removing
     3  subpackages.
     4  
     5  There are two different ways to add a subpackage to a package on the local
     6  filesystem:
     7  
     8  1. [Create a new package] in a subdirectory
     9  2. [Get an existing package] in a subdirectory
    10  
    11  Let's revisit the `wordpress` package and see how it was composed in the first
    12  place. Currently, it has the following package hierarchy:
    13  
    14  ```shell
    15  $ kpt pkg tree wordpress/
    16  Package "wordpress"
    17  ├── [Kptfile]  Kptfile wordpress
    18  ├── [service.yaml]  Service wordpress
    19  ├── deployment
    20  │   ├── [deployment.yaml]  Deployment wordpress
    21  │   └── [volume.yaml]  PersistentVolumeClaim wp-pv-claim
    22  └── Package "mysql"
    23      ├── [Kptfile]  Kptfile mysql
    24      ├── [deployment.yaml]  PersistentVolumeClaim mysql-pv-claim
    25      ├── [deployment.yaml]  Deployment wordpress-mysql
    26      └── [deployment.yaml]  Service wordpress-mysql
    27  ```
    28  
    29  First, let's delete the `mysql` subpackage. Deleting a subpackage is done by
    30  simply deleting the subdirectory:
    31  
    32  ```shell
    33  $ rm -r wordpress/mysql
    34  ```
    35  
    36  We're going to add back the `mysql` subpackage using the two different
    37  approaches:
    38  
    39  ## Create a new package
    40  
    41  Create the directory:
    42  
    43  ```shell
    44  $ mkdir wordpress/mysql
    45  ```
    46  
    47  Initialize the package:
    48  
    49  ```shell
    50  $ kpt pkg init wordpress/mysql
    51  # author resources in mysql
    52  ```
    53  
    54  This creates a [dependent package].
    55  
    56  ## Get an existing package
    57  
    58  Remove the existing directory if it exists:
    59  
    60  ```shell
    61  $ rm -rf wordpress/mysql
    62  ```
    63  
    64  Fetch the package:
    65  
    66  ```shell
    67  $ kpt pkg get https://github.com/kubernetes/website.git/content/en/examples/application/mysql@snapshot-initial-v1.20 wordpress/mysql
    68  ```
    69  
    70  This creates an [independent package]. If you wish to make this a dependent
    71  package, you can delete the `upstream` and `upstreamLock` sections of the
    72  `Kptfile` in `mysql` directory.
    73  
    74  [create a new package]: /book/03-packages/06-creating-a-package
    75  [get an existing package]: /book/03-packages/01-getting-a-package
    76  [dependent package]: /book/03-packages/01-getting-a-package
    77  [independent package]: /book/03-packages/01-getting-a-package