Cannot see own created temapltes in openshift via OC commands - openshift

Developer Catalog have dozens of templates provided by default & After creating own custom templates either via openshift console or via oc create command the teamplate when listed does not shows up which are self created.
$ oc get templates -n openshift
Lists all default teamplates but does not appers the self created one.
After creating from oc command like below:
$ oc create -f testtemplate.json
This also does not list the created template.
Please suggest any clue if there is any restrictions on it & path forward.

You Should add the default namespace on the question.
Otherwise, might using the -n option with the name of the project -n <namespace> to list/create templates.
-- Create template
$ oc create -f testtemplate -n target-namespace
-- Listing all templates in target-namespace project
$ oc get templates -n target-namespace

How about add annotations for metadata to your custom template like existing Templates ? Refer Writing the template description for more details.

Related

Apply changes dynamically when OpenShift template is modified (and applied)

I defined a template (let's call it template.yaml) with a service, deploymentconfig, buildconfig and imagestream, applied it with oc apply -f template.yaml and ran oc new-app app-name to create new app from the template. What the app basically does is to build a Node.js application with S2I, write it to a new ImageStream and deploy it to a pod with the necessary service exposed.
Now I've decided to make some changes to the template and have applied it on OpenShift. How do I go about ensuring that all resources in the said template also get reconfigured without having to delete all resources associated with that template and recreating it again?
I think the template is only used to create the related resource first time. Even though you modify the template, it's not associated with created resources. So you should recreate or modify each resource that is modified.
But you can modify simply all resources created by template using the following cmd.
# oc apply -f template_modified.yaml | oc replace -f -
I hope it help you
The correct command turned out to be:
$ oc apply -f template_modified.yaml
$ oc process -f template_modified.yaml | oc replace -f -
That worked for me on OpenShift 3.9.

How to retrieve only names from oc get dc (openshift cli get deployment configurations)?

Using the openshift cli ("oc") we can retrieve a list of deployment configurations within the current project with:
$/> oc get dc
I'd like to itereate over that list and query certain properties on the "oc describe [dcname]" for each deployment config.
What's a convenient way of doing that?
You can get just the names by using:
oc get dc -o name
You may though also want to look at using -o template and provide a template spec to extract the various settings you want.
oc get dc -o template --template '{{range .items}}{{.metadata.name}},{{index .metadata.labels "app"}}{{"\n"}}{{end }}'

Openshift: Hide templates in playground project

How do I hide the default templates that are there in openshift project by default. I do not want users to use any of those templates and use only the one I create for them.
Just delete all templates in openshift project using oc delete template --all -n openshift.
You can also first backup these templates in case you want them back. oc export template -n openshift > templates.yaml

How do you create a deployment configuration in OpenShift? Is it automatic for new-app based on a docker image?

I'm creating a new-app based on an image stream that corresponds to a docker image in a private OpenShift docker registry. The command is:
oc new-app mynamespace/my-image:latest -n=my-project
Question 1: Does this command automatically create a deployment configuration (dc) that can be referrenced as dc/my-image? Is this deployment configuration associated with my-project?
Question 2: What is the oc command to create a deployment configuration? The OpenShift developer guide has a section titled Creating a Deployment Configuration, but surprisingly it does not say how to create a DC or give any examples. It just shows a JSON structure and says DCs can be managed with the oc command.
Yes, your command will create stuff in the specified project. You can check what objects are created using the oc get command. i.e. to check what DCs you have, you'd do oc get dc or oc get deploymentconfigs.
Other useful commands are oc describe - similar to get but more information. oc status -v - see more broad information about project including warnings and errors.
You create DC and any other resource types using the oc create command. e.g. you copy the example DC off the URL you link to and put it into a file. Finally you do oc create -f mydc.yaml. Both YAML and JSON are supported.
As you see some commands can create DCs by themselves without you providing them with YAML or JSON. You can later modify existing resources with oc edit service/my-app. There is the oc patch command suitable for scripting.
You can see existing resource YAML doing oc get dc/myds -o yaml. Same with any other resource. Keep in mind you are presently using the desired project or use the -n option as you are doing in your example.
Not that hard once you understand some basics and learn to use the oc describe and oc logs command to debug issues with your images/pods. e.g. oc describe pod/my-app-1-asdfg, oc logs my-app-1-asdfg, oc logs -f dc/my-app.
HTH

Validate OpenShift objects defined in yaml before actually applying or executing it

I have a OpenShift template in template.yaml file which includes following objects - deployment-config, pod, service and route. I am using the following command to execute the yaml:
oc process -f template.yml | oc apply -f -
I want to perform following validations before I actually apply/execute the yaml:
YAML syntax validation - if there are any issues with the YAML syntax.
OpenShift schema validation - to check if the object definition abides by the OpenShift object schema.
It seems that the command 'oc process' is doing following checking:
Basic YAML syntax validation
Template object schema validation
How to perform schema validation of other objects (e.g. deployment-config, service, pod, etc.) that are defined in template.yaml?
This is now possible with the OpenShift client (and on Kubernetes in general), e.g.
$ oc login
Username: john.doe
Password:
Login successful.
$ oc apply -f openshift/template-app.yaml --dry-run
template "foobar-app" created (dry run)
It's also possible to process the template locally, thus you can avoid sending it to the server first, e.g.
$ oc process -f openshift/template-app.yaml --local -p APP_NAME=foo | oc apply --dry-run --validate -f -
deploymentconfig "foo" created (dry run)
service "foo" created (dry run)
Also note the --validate option I'm using for schema validation. Unfortunately, you still have to log in for the apply command to work (there's no --local option for apply).
Oddly, this feature is not described in the CLI documentation, however it's mentioned on the help screen:
$ oc apply --help
Apply a configuration to a resource by filename or stdin.
JSON and YAML formats are accepted.
Usage:
oc apply -f FILENAME [options]
...
Options:
...
--dry-run=false: If true, only print the object that would be sent, without sending it.
...
--validate=false: If true, use a schema to validate the input before sending it
Use "oc <command> --help" for more information about a given command.
Use "oc options" for a list of global command-line options (applies to all commands).
I'm having the same issue with cryptic errors coming back from the oc process command.
However if you go into the Openshift Console and use the "Add to Project" link at the top of the console, choose the "Import YAML / JSON" option and import your YAML/JSON that way you get slightly more useful errors.