github.com/SamarSidharth/kpt@v0.0.0-20231122062228-c7d747ae3ace/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  > A cloned package must be created in a repository in the same namespace as
   113  > the source package. Cloning a package with the Package Orchestration Service
   114  > retains a reference to the upstream package revision in the clone, and
   115  > cross-namespace references are not allowed. Package revisions in repositories
   116  > in other namespaces can be cloned using a reference directly to the underlying
   117  > oci or git repository as described below.
   118  
   119  You can also clone a package from a repository that is _not_ registered with
   120  Porch, for example:
   121  
   122  ```sh
   123  # Clone a package from Git repository directly (repository is not registered)
   124  $ kpt alpha rpkg clone \
   125    https://github.com/GoogleCloudPlatform/blueprints.git/catalog/bucket@main my-bucket \
   126    --repository=deployments \
   127    --namespace=default
   128  
   129  deployments-8baf4892d6bdeda0f26ef4b1088fddb85c5a2486 created
   130  
   131  # Confirm the package revision was created
   132  $ kpt alpha rpkg get deployments-8baf4892d6bdeda0f26ef4b1088fddb85c5a2486 -ndefault
   133  NAME                                                   PACKAGE     REVISION   LATEST   LIFECYCLE   REPOSITORY
   134  deployments-8baf4892d6bdeda0f26ef4b1088fddb85c5a2486   my-bucket   v1         false    Draft       deployments
   135  ```
   136  
   137  ## Create a new revision of an existing package
   138  
   139  Finally, with Porch you can create a new revision of an existing,
   140  **`Published`** package. All the package revisions in your repository are
   141  **`Draft`** revisions and need to be published first. We will cover the package
   142  approval flow in more detail in the next section. For now we will quickly
   143  propose and approve one of our draft package revisions and create a new revision
   144  from it.
   145  
   146  ```sh
   147  # Propose the package draft to be published
   148  $ kpt alpha rpkg propose deployments-c32b851b591b860efda29ba0e006725c8c1f7764 -ndefault
   149  deployments-c32b851b591b860efda29ba0e006725c8c1f7764 proposed
   150  
   151  # Approve the proposed package revision for publishing
   152  $ kpt alpha rpkg approve deployments-c32b851b591b860efda29ba0e006725c8c1f7764 -ndefault
   153  deployments-c32b851b591b860efda29ba0e006725c8c1f7764 approved
   154  ```
   155  
   156  You now have a **`Published`** package revision in the repository managed by Porch
   157  and next you will create a new revision of it. A **`Published`** package is ready
   158  to be used, such as deployed or copied.
   159  
   160  ```sh
   161  # Confirm the package is published:
   162  $ kpt alpha rpkg get deployments-c32b851b591b860efda29ba0e006725c8c1f7764 -ndefault
   163  NAME                                                   PACKAGE       REVISION   LATEST   LIFECYCLE   REPOSITORY
   164  deployments-c32b851b591b860efda29ba0e006725c8c1f7764   new-package   v1         true     Published   deployments
   165  ```
   166  
   167  Copy the existing, **`Published`** package revision to create a **`Draft`** of
   168  a new package revision that you can furthe customize:
   169  
   170  ```sh
   171  # Copy the published package:
   172  $ kpt alpha rpkg copy deployments-c32b851b591b860efda29ba0e006725c8c1f7764 \
   173    -ndefault --revision v2
   174  deployments-93bb9ac8c2fb7a5759547a38f5f48b369f42d08a created
   175  
   176  # List all revisions of the new-package that we just copied:
   177  $ kpt alpha rpkg get --name new-package
   178  NAME                                                   PACKAGE       REVISION   LATEST   LIFECYCLE   REPOSITORY
   179  deployments-af86ae3c767b0602a198856af513733e4e37bf10   new-package   main       false    Published   deployments
   180  deployments-c32b851b591b860efda29ba0e006725c8c1f7764   new-package   v1         true     Published   deployments
   181  deployments-93bb9ac8c2fb7a5759547a38f5f48b369f42d08a   new-package   v2         false    Draft       deployments
   182  ```
   183  
   184  ?> Refer to the [copy command reference][rpkg-copy] for usage.
   185  
   186  Unlike `clone` of a package which establishes the upstream-downstream
   187  relationship between the respective packages, and updates the `Kptfile`
   188  to reflect the relationship, the `copy` command does *not* change the
   189  upstream-downstream relationships. The copy of a package shares the same
   190  upstream package as the package from which it was copied. Specifically,
   191  in this case both `new-package/v1` and `new-package/v2` have identical contents,
   192  including upstream information, and differ in revision only.
   193  
   194  ## Editing package revision resources
   195  
   196  One of the driving motivations for the Package Orchestration service is enabling
   197  WYSIWYG authoring of packages, including their contents, in highly usable UIs.
   198  Porch therefore supports reading and updating package *contents*.
   199  
   200  In addition to using a [UI](/guides/namespace-provisioning-ui) with Porch, we
   201  can change the package contents by pulling the package from Porch onto the local
   202  disk, make any desired changes, and then pushing the updated contents to Porch.
   203  
   204  ```sh
   205  # Pull the package contents of istions/v1 onto the local disk:
   206  $ kpt alpha rpkg pull deployments-eeb52a8072ca2602e7ee27f3c56ad6344b024f5b ./istions -ndefault
   207  ```
   208  
   209  ?> Refer to the [pull command reference][rpkg-pull] for usage.
   210  
   211  The command downloaded the `istions/v1` package revision contents and saved
   212  them in the `./istions` directory. Now you will make some changes.
   213  
   214  First, note that even though Porch updated the namespace name (in
   215  `namespace.yaml`) to `istions` when the package was cloned, the `README.md`
   216  was not updated. Let's fix it first.
   217  
   218  Open the `README.md` in your favorite editor and update its contents, for
   219  example:
   220  
   221  ```
   222  # istions
   223  
   224  ## Description
   225  kpt package for provisioning Istio namespace
   226  ```
   227  
   228  In the second change, add a new mutator to the `Kptfile` pipeline. Use the
   229  [set-labels](https://catalog.kpt.dev/set-labels/v0.1/) function which will add
   230  labels to all resources in the package. Add the following mutator to the
   231  `Kptfile` `pipeline` section:
   232  
   233  ```yaml
   234    - image: gcr.io/kpt-fn/set-labels:v0.1.5
   235      configMap:
   236        color: orange
   237        fruit: apple
   238  ```
   239  
   240  The while `pipeline` section now looks like this:
   241  
   242  ```yaml
   243  pipeline:
   244    mutators:
   245    - image: gcr.io/kpt-fn/set-namespace:v0.4.1
   246      configPath: package-context.yaml
   247    - image: gcr.io/kpt-fn/apply-replacements:v0.1.1
   248      configPath: update-rolebinding.yaml
   249    - image: gcr.io/kpt-fn/set-labels:v0.1.5
   250      configMap:
   251        color: orange
   252        fruit: apple
   253  ```
   254  
   255  Save the changes and push the package contents back to the server:
   256  
   257  ```sh
   258  # Push updated package contents to the server
   259  $ kpt alpha rpkg push deployments-eeb52a8072ca2602e7ee27f3c56ad6344b024f5b ./istions -ndefault
   260  ```
   261  
   262  ?> Refer to the [push command reference][rpkg-push] for usage.
   263  
   264  Now, pull the contents of the package revision again, and inspect one of the
   265  configuration files.
   266  
   267  ```sh
   268  # Pull the updated package contents to local drive for inspection:
   269  $ kpt alpha rpkg pull deployments-eeb52a8072ca2602e7ee27f3c56ad6344b024f5b ./updated-istions -ndefault
   270  
   271  # Inspect updated-istions/namespace.yaml
   272  $ cat updated-istions/namespace.yaml 
   273  
   274  apiVersion: v1
   275  kind: Namespace
   276  metadata:
   277    name: istions
   278    labels:
   279      color: orange
   280      fruit: apple
   281  spec: {}
   282  ```
   283  
   284  The updated namespace now has new labels! What happened?
   285  
   286  Whenever package is updated during the authoring process, Porch automatically
   287  re-renders the package to make sure that all mutators and validators are
   288  executed. So when we added the new `set-labels` mutator, as soon as we pushed
   289  the updated package contents to Porch, Porch re-rendered the package and
   290  the `set-labels` function applied the labels we requested (`color: orange` and
   291  `fruit: apple`).
   292  
   293  ## Summary of package authoring
   294  
   295  In this section we reviewed how to use Porch to author packages, including
   296  
   297  * creating a new package ([`kpt alpha rpkg init`][rpkg-init])
   298  * cloning an existing package ([`kpt alpha rpkg clone`][rpkg-clone])
   299  * creating a new revision of an existing package
   300    ([`kpt alpha rpkg copy`][rpkg-copy])
   301  * pulling package contents for local editing
   302    ([`kpt alpha rpkg pull`][rpkg-pull])
   303  * and pushing updated package contents to Porch
   304    ([`kpt alpha rpkg push`][rpkg-push])
   305  
   306  [rpkg-init]: /reference/cli/alpha/rpkg/init/
   307  [rpkg-get]: /reference/cli/alpha/rpkg/get/
   308  [rpkg-clone]: /reference/cli/alpha/rpkg/clone/
   309  [rpkg-copy]: /reference/cli/alpha/rpkg/copy/
   310  [rpkg-pull]: /reference/cli/alpha/rpkg/pull/
   311  [rpkg-push]: /reference/cli/alpha/rpkg/push/