github.com/wmuizelaar/kpt@v0.0.0-20221018115725-bd564717b2ed/site/book/08-package-orchestration/04-package-authoring.md (about)

     1  There are several ways to author a package revision, including creating
     2  a completely new one, cloning an existing package, or creating a new revision
     3  of an existing package. In this section we will explore the different ways to
     4  author package revisions, and explore how to modify package contents.
     5  
     6  ## Create a new package revision
     7  
     8  Create a new package revision in a repository managed by Porch:
     9  
    10  ```sh
    11  # Initialize a new (empty) package revision:
    12  $ kpt alpha rpkg init new-package --repository=deployments --revision=v1 -ndefault
    13  
    14  deployments-c32b851b591b860efda29ba0e006725c8c1f7764 created
    15  
    16  # List the available package revisions.
    17  $ kpt alpha rpkg get
    18  
    19  NAME                                                   PACKAGE       REVISION   LATEST   LIFECYCLE   REPOSITORY
    20  deployments-c32b851b591b860efda29ba0e006725c8c1f7764   new-package   v1         false    Draft       deployments
    21  kpt-samples-da07e9611f9b99028f761c07a79e3c746d6fc43b   basens        main       false    Published   kpt-samples
    22  kpt-samples-afcf4d1fac605a60ba1ea4b87b5b5b82e222cb69   basens        v0         true     Published   kpt-samples
    23  ...
    24  ```
    25  
    26  ?> Refer to the [init command reference][rpkg-init] for usage.
    27  
    28  You can see the `new-package` is created in the `Draft` lifecycle stage. This
    29  means that the package is being authored.
    30  
    31  > You may notice that the name of the package revision
    32  > `deployments-c32b851b591b860efda29ba0e006725c8c1f7764` was assigned
    33  > automatically. Packages in a git repository may be located in subdirectories
    34  > and to make sure Porch works well with the rest of the Kubernetes ecosystem,
    35  > the resource names must meet Kubernetes requirements. The resource names
    36  > assigned by Porch are stable, and computed as hash of the repository name,
    37  > directory path within the repository, and revision.
    38  
    39  The contents of the new package revision are the same as if it was created using
    40  the [`kpt pkg init`](/book/03-packages/06-creating-a-package) command, except it
    41  was created by the Package Orchestration service in your repository.
    42  
    43  In fact, if you check your Git repository, you will see a new branch called
    44  `drafts/new-package/v1` which Porch created for the draft authoring. You will
    45  also see one or more commits made into the branch by Porch on your behalf.
    46  
    47  ## Clone an existing package
    48  
    49  Another way to create a new package revision is by cloning an already existing
    50  package. The existing package is referred to as *upstream* and the newly created
    51  package is *downstream*.
    52  
    53  Use `kpt alpha rpkg clone` command to create a new *downstream* package
    54  `istions` by cloning the sample `basens/v0` package revision:
    55  
    56  ```sh
    57  # Clone an upstream package to create a downstream package
    58  $ kpt alpha rpkg clone \
    59    kpt-samples-afcf4d1fac605a60ba1ea4b87b5b5b82e222cb69 \
    60    istions \
    61    --repository=deployments -ndefault
    62  
    63  deployments-eeb52a8072ca2602e7ee27f3c56ad6344b024f5b created
    64  
    65  # Confirm the package revision was created
    66  kpt alpha rpkg get deployments-eeb52a8072ca2602e7ee27f3c56ad6344b024f5b -ndefault
    67  NAME                                                   PACKAGE   REVISION   LATEST   LIFECYCLE   REPOSITORY
    68  deployments-eeb52a8072ca2602e7ee27f3c56ad6344b024f5b   istions   v1         false    Draft       deployments
    69  ```
    70  
    71  ?> Refer to the [clone command reference][rpkg-clone] for usage.
    72  
    73  Cloning a package using the Package Orchestration service is an action similar to
    74  [`kpt pkg get`](/book/03-packages/01-getting-a-package) command. Porch will
    75  create the appropriate upstream package links in the new package's `Kptfile`.
    76  Let's take a look:
    77  
    78  ```sh
    79  # Examine the new package's upstream link (the output has been abbreviated):
    80  $ kpt alpha rpkg pull deployments-eeb52a8072ca2602e7ee27f3c56ad6344b024f5b -ndefault
    81  
    82  kpt alpha rpkg pull deployments-eeb52a8072ca2602e7ee27f3c56ad6344b024f5b -ndefault
    83  apiVersion: config.kubernetes.io/v1
    84  kind: ResourceList
    85  items:
    86  - apiVersion: kpt.dev/v1
    87    kind: Kptfile
    88    metadata:
    89      name: istions
    90    upstream:
    91      type: git
    92      git:
    93        repo: https://github.com/GoogleContainerTools/kpt-samples.git
    94        directory: basens
    95        ref: basens/v0
    96    upstreamLock:
    97      type: git
    98      git:
    99        repo: https://github.com/GoogleContainerTools/kpt-samples.git
   100        directory: basens
   101        ref: basens/v0
   102        commit: 026dfe8e3ef8d99993bc8f7c0c6ba639faa9a634
   103    info:
   104      description: kpt package for provisioning namespace
   105  ...
   106  ```
   107  
   108  You can find out more about the `upstream` and `upstreamLock` sections of the
   109  `Kptfile` in an [earlier chapter](/book/03-packages/01-getting-a-package)
   110  of the book.
   111  
   112  You can also clone a package from a repository that is _not_ registered with
   113  Porch, for example:
   114  
   115  ```sh
   116  # Clone a package from Git repository directly (repository is not registered)
   117  $ kpt alpha rpkg clone \
   118    https://github.com/GoogleCloudPlatform/blueprints.git/catalog/bucket@main my-bucket \
   119    --repository=deployments \
   120    --namespace=default
   121  
   122  deployments-8baf4892d6bdeda0f26ef4b1088fddb85c5a2486 created
   123  
   124  # Confirm the package revision was created
   125  $ kpt alpha rpkg get deployments-8baf4892d6bdeda0f26ef4b1088fddb85c5a2486 -ndefault
   126  NAME                                                   PACKAGE     REVISION   LATEST   LIFECYCLE   REPOSITORY
   127  deployments-8baf4892d6bdeda0f26ef4b1088fddb85c5a2486   my-bucket   v1         false    Draft       deployments
   128  ```
   129  
   130  ## Create a new revision of an existing package
   131  
   132  Finally, with Porch you can create a new revision of an existing,
   133  **`Published`** package. All the package revisions in your repository are
   134  **`Draft`** revisions and need to be published first. We will cover the package
   135  approval flow in more detail in the next section. For now we will quickly
   136  propose and approve one of our draft package revisions and create a new revision
   137  from it.
   138  
   139  ```sh
   140  # Propose the package draft to be published
   141  $ kpt alpha rpkg propose deployments-c32b851b591b860efda29ba0e006725c8c1f7764 -ndefault
   142  deployments-c32b851b591b860efda29ba0e006725c8c1f7764 proposed
   143  
   144  # Approve the proposed package revision for publishing
   145  $ kpt alpha rpkg approve deployments-c32b851b591b860efda29ba0e006725c8c1f7764 -ndefault
   146  deployments-c32b851b591b860efda29ba0e006725c8c1f7764 approved
   147  ```
   148  
   149  You now have a **`Published`** package revision in the repository managed by Porch
   150  and next you will create a new revision of it. A **`Published`** package is ready
   151  to be used, such as deployed or copied.
   152  
   153  ```sh
   154  # Confirm the package is published:
   155  $ kpt alpha rpkg get deployments-c32b851b591b860efda29ba0e006725c8c1f7764 -ndefault
   156  NAME                                                   PACKAGE       REVISION   LATEST   LIFECYCLE   REPOSITORY
   157  deployments-c32b851b591b860efda29ba0e006725c8c1f7764   new-package   v1         true     Published   deployments
   158  ```
   159  
   160  Copy the existing, **`Published`** package revision to create a **`Draft`** of
   161  a new package revision that you can furthe customize:
   162  
   163  ```sh
   164  # Copy the published package:
   165  $ kpt alpha rpkg copy deployments-c32b851b591b860efda29ba0e006725c8c1f7764 \
   166    -ndefault --revision v2
   167  deployments-93bb9ac8c2fb7a5759547a38f5f48b369f42d08a created
   168  
   169  # List all revisions of the new-package that we just copied:
   170  $ kpt alpha rpkg get --name new-package
   171  NAME                                                   PACKAGE       REVISION   LATEST   LIFECYCLE   REPOSITORY
   172  deployments-af86ae3c767b0602a198856af513733e4e37bf10   new-package   main       false    Published   deployments
   173  deployments-c32b851b591b860efda29ba0e006725c8c1f7764   new-package   v1         true     Published   deployments
   174  deployments-93bb9ac8c2fb7a5759547a38f5f48b369f42d08a   new-package   v2         false    Draft       deployments
   175  ```
   176  
   177  ?> Refer to the [copy command reference][rpkg-copy] for usage.
   178  
   179  Unlike `clone` of a package which establishes the upstream-downstream
   180  relationship between the respective packages, and updates the `Kptfile`
   181  to reflect the relationship, the `copy` command does *not* change the
   182  upstream-downstream relationships. The copy of a package shares the same
   183  upstream package as the package from which it was copied. Specifically,
   184  in this case both `new-package/v1` and `new-package/v2` have identical contents,
   185  including upstream information, and differ in revision only.
   186  
   187  ## Editing package revision resources
   188  
   189  One of the driving motivations for the Package Orchestration service is enabling
   190  WYSIWYG authoring of packages, including their contents, in highly usable UIs.
   191  Porch therefore supports reading and updating package *contents*.
   192  
   193  In addition to using a [UI](/guides/namespace-provisioning-ui) with Porch, we
   194  can change the package contents by pulling the package from Porch onto the local
   195  disk, make any desired changes, and then pushing the updated contents to Porch.
   196  
   197  ```sh
   198  # Pull the package contents of istions/v1 onto the local disk:
   199  $ kpt alpha rpkg pull deployments-eeb52a8072ca2602e7ee27f3c56ad6344b024f5b ./istions -ndefault
   200  ```
   201  
   202  ?> Refer to the [pull command reference][rpkg-pull] for usage.
   203  
   204  The command downloaded the `istions/v1` package revision contents and saved
   205  them in the `./istions` directory. Now you will make some changes.
   206  
   207  First, note that even though Porch updated the namespace name (in
   208  `namespace.yaml`) to `istions` when the package was cloned, the `README.md`
   209  was not updated. Let's fix it first.
   210  
   211  Open the `README.md` in your favorite editor and update its contents, for
   212  example:
   213  
   214  ```
   215  # istions
   216  
   217  ## Description
   218  kpt package for provisioning Istio namespace
   219  ```
   220  
   221  In the second change, add a new mutator to the `Kptfile` pipeline. Use the
   222  [set-labels](https://catalog.kpt.dev/set-labels/v0.1/) function which will add
   223  labels to all resources in the package. Add the following mutator to the
   224  `Kptfile` `pipeline` section:
   225  
   226  ```yaml
   227    - image: gcr.io/kpt-fn/set-labels:v0.1.5
   228      configMap:
   229        color: orange
   230        fruit: apple
   231  ```
   232  
   233  The while `pipeline` section now looks like this:
   234  
   235  ```yaml
   236  pipeline:
   237    mutators:
   238    - image: gcr.io/kpt-fn/set-namespace:v0.4.1
   239      configPath: package-context.yaml
   240    - image: gcr.io/kpt-fn/apply-replacements:v0.1.1
   241      configPath: update-rolebinding.yaml
   242    - image: gcr.io/kpt-fn/set-labels:v0.1.5
   243      configMap:
   244        color: orange
   245        fruit: apple
   246  ```
   247  
   248  Save the changes and push the package contents back to the server:
   249  
   250  ```sh
   251  # Push updated package contents to the server
   252  $ kpt alpha rpkg push deployments-eeb52a8072ca2602e7ee27f3c56ad6344b024f5b ./istions -ndefault
   253  ```
   254  
   255  ?> Refer to the [push command reference][rpkg-push] for usage.
   256  
   257  Now, pull the contents of the package revision again, and inspect one of the
   258  configuration files.
   259  
   260  ```sh
   261  # Pull the updated package contents to local drive for inspection:
   262  $ kpt alpha rpkg pull deployments-eeb52a8072ca2602e7ee27f3c56ad6344b024f5b ./updated-istions -ndefault
   263  
   264  # Inspect updated-istions/namespace.yaml
   265  $ cat updated-istions/namespace.yaml 
   266  
   267  apiVersion: v1
   268  kind: Namespace
   269  metadata:
   270    name: istions
   271    labels:
   272      color: orange
   273      fruit: apple
   274  spec: {}
   275  ```
   276  
   277  The updated namespace now has new labels! What happened?
   278  
   279  Whenever package is updated during the authoring process, Porch automatically
   280  re-renders the package to make sure that all mutators and validators are
   281  executed. So when we added the new `set-labels` mutator, as soon as we pushed
   282  the updated package contents to Porch, Porch re-rendered the package and
   283  the `set-labels` function applied the labels we requested (`color: orange` and
   284  `fruit: apple`).
   285  
   286  ## Summary of package authoring
   287  
   288  In this section we reviewed how to use Porch to author packages, including
   289  
   290  * creating a new package ([`kpt alpha rpkg init`][rpkg-init])
   291  * cloning an existing package ([`kpt alpha rpkg clone`][rpkg-clone])
   292  * creating a new revision of an existing package
   293    ([`kpt alpha rpkg copy`][rpkg-copy])
   294  * pulling package contents for local editing
   295    ([`kpt alpha rpkg pull`][rpkg-pull])
   296  * and pushing updated package contents to Porch
   297    ([`kpt alpha rpkg push`][rpkg-push])
   298  
   299  [rpkg-init]: /reference/cli/alpha/rpkg/init/
   300  [rpkg-get]: /reference/cli/alpha/rpkg/get/
   301  [rpkg-clone]: /reference/cli/alpha/rpkg/clone/
   302  [rpkg-copy]: /reference/cli/alpha/rpkg/copy/
   303  [rpkg-pull]: /reference/cli/alpha/rpkg/pull/
   304  [rpkg-push]: /reference/cli/alpha/rpkg/push/