Skip to content

Commit

Permalink
Add server TLS key and cert into Che configmap if a separate namespac…
Browse files Browse the repository at this point in the history
…e for Che workspaces is configured

Signed-off-by: Mykola Morhun <mmorhun@redhat.com>
  • Loading branch information
mmorhun committed Aug 28, 2020
1 parent 69b308a commit 8d2148d
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 15 deletions.
40 changes: 27 additions & 13 deletions pkg/deploy/che_configmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,10 @@ type CheConfigMap struct {
}

func SyncCheConfigMapToCluster(checluster *orgv1.CheCluster, proxy *Proxy, clusterAPI ClusterAPI) (*corev1.ConfigMap, error) {
data := GetCheConfigMapData(checluster, proxy)
data, err := GetCheConfigMapData(checluster, proxy, clusterAPI)
if err != nil {
return nil, err
}
specConfigMap, err := GetSpecConfigMap(checluster, CheConfigMapName, data, clusterAPI)
if err != nil {
return nil, err
Expand All @@ -87,9 +90,9 @@ func SyncCheConfigMapToCluster(checluster *orgv1.CheCluster, proxy *Proxy, clust
return SyncConfigMapToCluster(checluster, specConfigMap, clusterAPI)
}

// GetConfigMapData gets env values from CR spec and returns a map with key:value
// GetCheConfigMapData gets env values from CR spec and returns a map with key:value
// which is used in CheCluster ConfigMap to configure CheCluster master behavior
func GetCheConfigMapData(cr *orgv1.CheCluster, proxy *Proxy) (cheEnv map[string]string) {
func GetCheConfigMapData(cr *orgv1.CheCluster, proxy *Proxy, clusterAPI ClusterAPI) (cheEnv map[string]string, err error) {
cheHost := cr.Spec.Server.CheHost
keycloakURL := cr.Spec.Auth.IdentityProviderURL
isOpenShift, isOpenshift4, err := util.DetectOpenShift()
Expand Down Expand Up @@ -228,19 +231,30 @@ func GetCheConfigMapData(cr *orgv1.CheCluster, proxy *Proxy) (cheEnv map[string]
err = json.Unmarshal(out, &cheEnv)

// k8s specific envs
k8sCheEnv := map[string]string{
"CHE_INFRA_KUBERNETES_POD_SECURITY__CONTEXT_FS__GROUP": securityContextFsGroup,
"CHE_INFRA_KUBERNETES_POD_SECURITY__CONTEXT_RUN__AS__USER": securityContextRunAsUser,
"CHE_INFRA_KUBERNETES_INGRESS_DOMAIN": ingressDomain,
"CHE_INFRA_KUBERNETES_SERVER__STRATEGY": ingressStrategy,
"CHE_INFRA_KUBERNETES_TLS__SECRET": tlsSecretName,
"CHE_INFRA_KUBERNETES_INGRESS_ANNOTATIONS__JSON": "{\"kubernetes.io/ingress.class\": " + ingressClass + ", \"nginx.ingress.kubernetes.io/rewrite-target\": \"/$1\",\"nginx.ingress.kubernetes.io/ssl-redirect\": " + tls + ",\"nginx.ingress.kubernetes.io/proxy-connect-timeout\": \"3600\",\"nginx.ingress.kubernetes.io/proxy-read-timeout\": \"3600\"}",
"CHE_INFRA_KUBERNETES_INGRESS_PATH__TRANSFORM": "%s(.*)",
}
if !isOpenShift {
k8sCheEnv := map[string]string{
"CHE_INFRA_KUBERNETES_POD_SECURITY__CONTEXT_FS__GROUP": securityContextFsGroup,
"CHE_INFRA_KUBERNETES_POD_SECURITY__CONTEXT_RUN__AS__USER": securityContextRunAsUser,
"CHE_INFRA_KUBERNETES_INGRESS_DOMAIN": ingressDomain,
"CHE_INFRA_KUBERNETES_SERVER__STRATEGY": ingressStrategy,
"CHE_INFRA_KUBERNETES_TLS__SECRET": tlsSecretName,
"CHE_INFRA_KUBERNETES_INGRESS_ANNOTATIONS__JSON": "{\"kubernetes.io/ingress.class\": " + ingressClass + ", \"nginx.ingress.kubernetes.io/rewrite-target\": \"/$1\",\"nginx.ingress.kubernetes.io/ssl-redirect\": " + tls + ",\"nginx.ingress.kubernetes.io/proxy-connect-timeout\": \"3600\",\"nginx.ingress.kubernetes.io/proxy-read-timeout\": \"3600\"}",
"CHE_INFRA_KUBERNETES_INGRESS_PATH__TRANSFORM": "%s(.*)",
}
// Add TLS key and server certificate to properties when user workspaces should be created in another
// than Che server namespace, from where the Che TLS secret is not accesible.
if _, keyExists := cr.Spec.Server.CustomCheProperties["CHE_INFRA_KUBERNETES_NAMESPACE_DEFAULT"]; keyExists {
cheTLSSecret, err := GetClusterSecret(cr.Spec.K8s.TlsSecretName, cr.ObjectMeta.Namespace, clusterAPI)
if err != nil {
return nil, err
}
k8sCheEnv["CHE_INFRA_KUBERNETES_TLS__KEY"] = string(cheTLSSecret.Data["tls.key"])
k8sCheEnv["CHE_INFRA_KUBERNETES_TLS__CERT"] = string(cheTLSSecret.Data["tls.crt"])
}

addMap(cheEnv, k8sCheEnv)
}

addMap(cheEnv, cr.Spec.Server.CustomCheProperties)
return cheEnv
return cheEnv, nil
}
4 changes: 2 additions & 2 deletions pkg/deploy/che_configmap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func TestNewCheConfigMap(t *testing.T) {
cr.Spec.Server.CheHost = "myhostname.com"
cr.Spec.Server.TlsSupport = true
cr.Spec.Auth.OpenShiftoAuth = true
cheEnv := GetCheConfigMapData(cr, &Proxy{})
cheEnv, _ := GetCheConfigMapData(cr, &Proxy{}, ClusterAPI{})
testCm, _ := GetSpecConfigMap(cr, CheConfigMapName, cheEnv, ClusterAPI{})
identityProvider := testCm.Data["CHE_INFRA_OPENSHIFT_OAUTH__IDENTITY__PROVIDER"]
_, isOpenshiftv4, _ := util.DetectOpenShift()
Expand All @@ -53,7 +53,7 @@ func TestConfigMapOverride(t *testing.T) {
"CHE_WORKSPACE_NO_PROXY": "myproxy.myhostname.com",
}
cr.Spec.Auth.OpenShiftoAuth = true
cheEnv := GetCheConfigMapData(cr, &Proxy{})
cheEnv, _ := GetCheConfigMapData(cr, &Proxy{}, ClusterAPI{})
testCm, _ := GetSpecConfigMap(cr, CheConfigMapName, cheEnv, ClusterAPI{})
if testCm.Data["CHE_WORKSPACE_NO_PROXY"] != "myproxy.myhostname.com" {
t.Errorf("Test failed. Expected myproxy.myhostname.com but was %s", testCm.Data["CHE_WORKSPACE_NO_PROXY"])
Expand Down

0 comments on commit 8d2148d

Please sign in to comment.