Skip to content

Commit

Permalink
Fix fetching test route URL
Browse files Browse the repository at this point in the history
Signed-off-by: Sergii Leshchenko <sleshche@redhat.com>
  • Loading branch information
sleshchenko committed Mar 18, 2020
1 parent 3438312 commit c2d5aa9
Showing 1 changed file with 21 additions and 18 deletions.
39 changes: 21 additions & 18 deletions pkg/controller/che/k8s_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,22 @@ import (
"context"
"crypto/tls"
"encoding/pem"
"io"
"net/http"
"time"

"errors"
orgv1 "github.com/eclipse/che-operator/pkg/apis/org/v1"
"github.com/eclipse/che-operator/pkg/deploy"
"github.com/eclipse/che-operator/pkg/util"
routev1 "github.com/openshift/api/route/v1"
"github.com/sirupsen/logrus"
"io"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/fields"
"k8s.io/apimachinery/pkg/watch"
"k8s.io/client-go/kubernetes"
"net/http"
"sigs.k8s.io/controller-runtime/pkg/client/config"
"time"
)

type k8s struct {
Expand Down Expand Up @@ -252,9 +252,10 @@ func (cl *k8s) DeleteDeployment(deploymentName string, ns string) {
}

// GetLogs prints stderr or stdout from a selected pod. Log size is capped at 60000 bytes
func (cl *k8s) GetPodLogs(podName string, ns string) {
func (cl *k8s) GetPodLogs(podName string, ns string) () {
var limitBytes int64 = 60000
req := cl.clientset.CoreV1().Pods(ns).GetLogs(podName, &corev1.PodLogOptions{LimitBytes: &limitBytes})
req := cl.clientset.CoreV1().Pods(ns).GetLogs(podName, &corev1.PodLogOptions{LimitBytes: &limitBytes},
)
readCloser, err := req.Stream()
if err != nil {
logrus.Errorf("Pod error log: %v", err)
Expand Down Expand Up @@ -287,23 +288,24 @@ func (cl *k8s) GetDeploymentPod(name string, ns string) (podName string, err err
// There's an easier way which is to read tls secret in default (3.11) or openshift-ingress (4.0) namespace
// which however requires extra privileges for operator service account
func (r *ReconcileChe) GetEndpointTlsCrt(instance *orgv1.CheCluster, url string) (certificate []byte, err error) {
testRoute := &routev1.Route{}
var requestURL string
var testRoute *routev1.Route
if len(url) < 1 {
testRoute = deploy.NewTlsRoute(instance, "test", "test", 8080)
logrus.Infof("Creating a test route %s to extract router crt", testRoute.Name)
if err := r.CreateNewRoute(instance, testRoute); err != nil {
logrus.Errorf("Failed to create test route %s: %s", testRoute.Name, err)
return nil, err
testRoute = r.GetEffectiveRoute(instance, "test")
if testRoute == nil {
testRoute = deploy.NewTlsRoute(instance, "test", "test", 8080)
logrus.Infof("Creating a test route %s to extract router crt", testRoute.Name)
if err := r.CreateNewRoute(instance, testRoute); err != nil {
logrus.Errorf("Failed to create test route %s: %s", testRoute.Name, err)
return nil, err
}
}
// sometimes timing conditions apply, and host isn't available right away

if len(testRoute.Spec.Host) < 1 {
time.Sleep(time.Duration(1) * time.Second)
testRoute := r.GetEffectiveRoute(instance, "test")
requestURL = "https://" + testRoute.Spec.Host
return nil, errors.New("Unable to get test route host for fetching certificate")
}
requestURL = "https://" + testRoute.Spec.Host

requestURL = "https://" + testRoute.Spec.Host
} else {
requestURL = url
}
Expand All @@ -329,11 +331,12 @@ func (r *ReconcileChe) GetEndpointTlsCrt(instance *orgv1.CheCluster, url string)
certificate = append(certificate, crt...)
}

if len(url) < 1 {
if testRoute != nil {
logrus.Infof("Deleting a test route %s to extract routes crt", testRoute.Name)
if err := r.client.Delete(context.TODO(), testRoute); err != nil {
logrus.Errorf("Failed to delete test route %s: %s", testRoute.Name, err)
}
}
logrus.Info("Self-signed certificate is automatically fetched from test route")
return certificate, nil
}

0 comments on commit c2d5aa9

Please sign in to comment.