Repository: redhat-openshift-ecosystem/openshift-preflight (go)

Issue:
`DOCKERCONFIG` should be `PFLT_DOCKERCONFIG` and should not be required
## Bug Description
PR #282 resolves an issue with private registry access when running the DeployableByOLM check by pushing an explicitly provided dockerconfigjson file to the cluster so that the CatalogSource can pull from a private registry.

With that said, it is **now** required and it should not be. If a user does _not_ set this environment variable, preflight currently exits with an error. 

It's plausible that the index image used by the CatalogSource will be publicly available, and so we should not have to create any secret, or pass a secret reference to a CatalogSource resource in order to run the check.


## Version and Command Invocation
ef91135080a4a4a8d927b7d78b797d269be837f9

## Steps to Reproduce
Attempt to run `preflight check operator <bundle>` without DOCKERCONFIG set in the environment.

## Current result:

```
$ ./preflight check operator quay.io/opdev/simple-demo-operator-bundle:latest
time="2021-10-05T11:58:01-05:00" level=info msg="config file not found, proceeding without it"
time="2021-10-05T11:58:01-05:00" level=info msg="certification library version 0.0.0 <commit: ef91135080a4a4a8d927b7d78b797d269be837f9>"
Error: no environment variable DOCKERCONFIG could be found

The checks that will be executed are the following:
- DeployableByOLM
- ValidateOperatorBundle
- OperatorPackageNameIsUnique
- ScorecardBasicSpecCheck
- ScorecardOlmSuiteCheck

Usage:
  preflight check operator <url to Operator bundle image> [flags]

Flags:
  -h, --help   help for operator

time="2021-10-05T11:58:01-05:00" level=fatal msg="no environment variable DOCKERCONFIG could be found"
```

## Expected Result
Preflight to execute for a publicly available image without having to set the variable.

Pull request description:
Make docker config file optional
This PR makes the docker config file optional. It also leverages
viper to fetch the docker config file path.

Fixes #284

Patch:
diff --git a/certification/errors/errors.go b/certification/errors/errors.go
index 12dc20c..6dd69af 100644
--- a/certification/errors/errors.go
+++ b/certification/errors/errors.go
@@ -38,4 +38,3 @@ var ErrRemoveOSPIDFromProjectID = errors.New("please remove leading character os
 var ErrRemoveSpecialCharFromProjectID = errors.New("please remove all special characters from project id")
 var ErrPullRequestURL = errors.New("please enter a valid url: including scheme, host, and path to pull request")
 var ErrIndexImageUndefined = errors.New("no environment variable PFLT_INDEXIMAGE could be found")
-var ErrNoDockerconfig = errors.New("no environment variable DOCKERCONFIG could be found")
diff --git a/certification/internal/policy/operator/deployable_by_olm.go b/certification/internal/policy/operator/deployable_by_olm.go
index b7c8d86..fb3c567 100644
--- a/certification/internal/policy/operator/deployable_by_olm.go
+++ b/certification/internal/policy/operator/deployable_by_olm.go
@@ -104,15 +104,20 @@ func (p *DeployableByOlmCheck) setUp(operatorData OperatorData) error {
 		return err
 	}
 
-	content, err := p.readFileAsByteArray(os.Getenv("DOCKERCONFIG"))
-	if err != nil {
-		return err
-	}
+	dockerconfig := viper.GetString("dockerConfig")
+	if len(dockerconfig) != 0 {
+		content, err := p.readFileAsByteArray(dockerconfig)
+		if err != nil {
+			return err
+		}
 
-	data := map[string]string{".dockerconfigjson": string(content)}
+		data := map[string]string{".dockerconfigjson": string(content)}
 
-	if _, err := p.OpenshiftEngine.CreateSecret(secretName, data, corev1.SecretTypeDockerConfigJson, cli.OpenshiftOptions{Namespace: operatorData.InstallNamespace}); err != nil && !kubeErr.IsAlreadyExists(err) {
-		return err
+		if _, err := p.OpenshiftEngine.CreateSecret(secretName, data, corev1.SecretTypeDockerConfigJson, cli.OpenshiftOptions{Namespace: operatorData.InstallNamespace}); err != nil && !kubeErr.IsAlreadyExists(err) {
+			return err
+		}
+	} else {
+		log.Debug("No docker config file is found to access the index image in private registries. Proceeding...")
 	}
 
 	if _, err := p.OpenshiftEngine.CreateCatalogSource(cli.CatalogSourceData{Name: operatorData.App, Image: operatorData.CatalogImage, Secrets: []string{secretName}}, cli.OpenshiftOptions{Namespace: operatorData.InstallNamespace}); err != nil && !kubeErr.IsAlreadyExists(err) {
diff --git a/cmd/check_operator.go b/cmd/check_operator.go
index a4b234c..09b2266 100644
--- a/cmd/check_operator.go
+++ b/cmd/check_operator.go
@@ -41,10 +41,6 @@ var checkOperatorCmd = &cobra.Command{
 			return errors.ErrNoKubeconfig
 		}
 
-		if _, ok := os.LookupEnv("DOCKERCONFIG"); !ok {
-			return errors.ErrNoDockerconfig
-		}
-
 		if catalogImage := viper.GetString("indexImage"); len(catalogImage) == 0 {
 			return errors.ErrIndexImageUndefined
 		}
diff --git a/docs/BUILDING_AN_INDEX.md b/docs/BUILDING_AN_INDEX.md
index 453528d..7c9d43b 100644
--- a/docs/BUILDING_AN_INDEX.md
+++ b/docs/BUILDING_AN_INDEX.md
@@ -48,7 +48,7 @@ accessible to Preflight as well as the target cluster.
 ```shell
 podman push registry.example.org/your-namespace/your-index:0.0.1
 ```
-If the index image is stored in a private repository, set the value of `DOCKERCONFIG` to the path of the docker configuration path.
+If the index image is stored in a private repository, set the value of `PFLT_DOCKERCONFIG` to the path of the docker configuration file.
 
 Finally, set the value of `PFLT_INDEXIMAGE` to this value and run preflight:
 
diff --git a/docs/RECIPES.md b/docs/RECIPES.md
index da7a3c2..9f29776 100644
--- a/docs/RECIPES.md
+++ b/docs/RECIPES.md
@@ -31,7 +31,7 @@ test asset by executing the following.
 ```bash
 export KUBECONFIG=/path/to/your/kubeconfig 
 export PFLT_INDEXIMAGE=registry.example.org/your-namespace/your-index-image:sometag
-export DOCKERCONFIG=~/.docker/config.json
+export PFLT_DOCKERCONFIG=~/.docker/config.json
 preflight check operator registry.example.org/your-namespace/your-bundle-image:sometag
 ```
 
@@ -59,7 +59,7 @@ $CONTAINER_TOOL run \
   --env KUBECONFIG=/kubeconfig \
   --env PFLT_LOGLEVEL=trace \
   --env PFLT_INDEXIMAGE=registry.example.org/your-namespace/your-index-image:sometag \
-  --env DOCKERCONFIG=~/.docker/config.json \
+  --env PFLT_DOCKERCONFIG=~/.docker/config.json \
   --env PFLT_ARTIFACTS=/artifacts \
   --env PFLT_LOGFILE=/artifacts/preflight.log \
   -v /some/path/on/your/host/artifacts:/artifacts \
@@ -111,7 +111,7 @@ spec:
             value: trace
           - name: PFLT_INDEXIMAGE
             value: "registry.example.org/your-namespace/your-index-image:sometag"
-          - name: DOCKERCONFIG
+          - name: PFLT_DOCKERCONFIG
             value: "/root/.docker/config.json"
           - name: PFLT_LOGFILE
             value: "/artifacts/preflight.log"


Test patch:
diff --git a/certification/internal/policy/operator/deployable_by_olm_test.go b/certification/internal/policy/operator/deployable_by_olm_test.go
index 67197e1..4543c1b 100644
--- a/certification/internal/policy/operator/deployable_by_olm_test.go
+++ b/certification/internal/policy/operator/deployable_by_olm_test.go
@@ -68,7 +68,7 @@ var _ = Describe("DeployableByOLMCheck", func() {
 		// set env var for index image
 		os.Setenv("PFLT_INDEXIMAGE", "test_indeximage")
 		os.Setenv("PFLT_ARTIFACTS", tmpDir)
-		os.Setenv("DOCKERCONFIG", filepath.Join(tmpDockerDir, registryConfigDir, registryConfigFilename))
+		os.Setenv("PFLT_DOCKERCONFIG", filepath.Join(tmpDockerDir, registryConfigDir, registryConfigFilename))
 
 	})
 	Describe("When deploying an operator using OLM", func() {