///////////////////////////////////////////////////////////////////////////////

    Copyright (c) 2018, 2019, Oracle and/or its affiliates. All rights reserved.

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

        http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.

///////////////////////////////////////////////////////////////////////////////
:adoc-dir: {guides-dir}

= Deploying to OKE
:description: Helidon Oracle Container Engine for Kubernetes (OKE) Guide
:keywords: helidon, guide, oracle, kubernetes
:oke-docs-url: http://www.oracle.com/webfolder/technetwork/tutorials/obe/oci/oke-full/index.html
:ocir-token-docs-url: https://docs.cloud.oracle.com/iaas/Content/Registry/Tasks/registrygettingauthtoken.htm
:oci-region-code-docs-url: https://docs.cloud.oracle.com/iaas/Content/General/Concepts/regions.htm

Push a Docker image of your Helidon application to Oracle Cloud Infrastructure
 Registry (OCIR), and deploy the image from the registry to Oracle Cloud
 Infrastructure Container Engine for Kubernetes (OKE). 

== What You Need

|===
|About 10 minutes
| <<about/03_prerequisites.adoc,Helidon prerequisites>>
|An OKE cluster. See the link:{oke-docs-url}[OKE documentation].
|A Helidon project created from the quickstart Maven archetype. See
 <<guides/02_quickstart-se.adoc,quickstart Maven archetype>>.
|===

== Push Your Image to OCIR

Your account must be in the `Administrators` group or another group that has
 the `REPOSITORY_CREATE` permission. 

Sign in to the Oracle Cloud Infrastructure (OCI) web console and generate an
 authentication token. See link:{ocir-token-docs-url}[Getting an Auth Token].

NOTE: Remember to copy the generated token. You won't be able to access it
 again.

[source,bash]
.Log in to the OCIR Docker registry:
----
docker login \
       -u <username> \ # <1>
       -p <password> \ # <2>
       <region-code>.ocir.io # <3>
----
<1> The user name in the format `<tenancy_name>/<username>`.
<2> The password is the generated token.
<3> `<region-code>` is the code for the OCI region that you're using. For
 example, the region code for Phoenix is `phx`. See
 link:{oci-region-code-docs-url}[Regions and Availability Domains].

[source,bash]
.Tag the image that you want to push to the registry:
----
docker tag \
       helidon-quickstart-se:latest \ # <1>
       <region-code>.ocir.io/<tenancy-name>/<repo-name>/<image-name>:<tag> # <2>
----
<1> the local image to tag
<2> `<repo-name>` is optional. It is the name of a repository to which you want
 to push the image (for example, `project01`).

[source,bash]
.Push the image to the Registry:
----
docker push \
    <region-code>.ocir.io/<tenancy-name>/<repo-name>/<image-name>:<tag>
----

You can pull your image with the image path used above, for example:
 `phx.ocir.io/helidon/example/helidon-quickstart-se:latest`

== Setup your K8s Cluster

Create a namespace (for example, `helidon`) for the project:

[source,bash]
kubectl create namespace helidon

The repository that you created is private. To allow Kubernetes to
 authenticate with the container registry and pull the private image, you must
 create and use an image-pull secret.

[source,bash]
.Create an image-pull secret:
----
kubectl create secret docker-registry \
    ocirsecret \ # <1>
    --docker-server=<region-code>.ocir.io \ # <2>
    --docker-username='<tenancy-name>/<oci-username>' \ # <3>
    --docker-password='<oci-auth-token>' \ # <4>
    --docker-email='<email-address>' \
    --namespace helidon # <5>
----
<1> The name of the config secret
<2> The docker registry (see docker tag step above)
<3> The user name (see docker login step above)
<4> The password (see docker login step above)
<5> The namespace created in the previous step

=== Deploy the Image to Kubernetes

First, change to the `helidon-quickstart-se` directory.

Then edit `app.yaml` and add the following under `spec` in the `deployment`
 section:

[source, yaml]
----
spec:
  imagePullSecrets:
  - name: ocirsecret # <1>
  containers:
  - name: helidon-quickstart-se
    image: phx.ocir.io/helidon/example/helidon-quickstart-se:latest # <2>
    imagePullPolicy: Always
    ports:
    - containerPort: 8080
----
<1> The config secret name
<2> The image path

[source,bash]
.Deploy the application:
----
kubectl create -f app.yaml -n helidon
----

[source,bash]
.Get the `NodePort` number for your new pod:
----
kubectl get svc -n helidon
----

[source,bash]
.Get the IP address for your cluster nodes:
----
kubectl get nodes
----

You can now access the application at `\http://<NodeIpAddress>:<NodePort>/greet`.
