Skip to content

Commit

Permalink
Create a general deployContext to pass to all functions
Browse files Browse the repository at this point in the history
Signed-off-by: Flavius Lacatusu <flacatus@redhat.com>
  • Loading branch information
flacatus committed Aug 27, 2020
1 parent 027b1b7 commit ed65945
Show file tree
Hide file tree
Showing 28 changed files with 594 additions and 600 deletions.
78 changes: 42 additions & 36 deletions pkg/controller/che/che_controller.go

Large diffs are not rendered by default.

196 changes: 98 additions & 98 deletions pkg/controller/che/create.go

Large diffs are not rendered by default.

10 changes: 5 additions & 5 deletions pkg/controller/che/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,14 @@ func (r *ReconcileChe) getProxyConfiguration(checluster *orgv1.CheCluster) (*dep
return proxy, nil
}

func (r *ReconcileChe) putOpenShiftCertsIntoConfigMap(checluster *orgv1.CheCluster, proxy *deploy.Proxy, clusterAPI deploy.ClusterAPI) (bool, error) {
if checluster.Spec.Server.ServerTrustStoreConfigMapName == "" {
checluster.Spec.Server.ServerTrustStoreConfigMapName = deploy.DefaultServerTrustStoreConfigMapName()
if err := r.UpdateCheCRSpec(checluster, "truststore configmap", deploy.DefaultServerTrustStoreConfigMapName()); err != nil {
func (r *ReconcileChe) putOpenShiftCertsIntoConfigMap(deployContext *deploy.DeployContext) (bool, error) {
if deployContext.CheCluster.Spec.Server.ServerTrustStoreConfigMapName == "" {
deployContext.CheCluster.Spec.Server.ServerTrustStoreConfigMapName = deploy.DefaultServerTrustStoreConfigMapName()
if err := r.UpdateCheCRSpec(deployContext.CheCluster, "truststore configmap", deploy.DefaultServerTrustStoreConfigMapName()); err != nil {
return false, err
}
}

certConfigMap, err := deploy.SyncTrustStoreConfigMapToCluster(checluster, clusterAPI)
certConfigMap, err := deploy.SyncTrustStoreConfigMapToCluster(deployContext)
return certConfigMap != nil, err
}
105 changes: 52 additions & 53 deletions pkg/deploy/che_configmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import (
"os"
"strconv"

orgv1 "github.com/eclipse/che-operator/pkg/apis/org/v1"
"github.com/eclipse/che-operator/pkg/util"
"github.com/sirupsen/logrus"
corev1 "k8s.io/api/core/v1"
Expand Down Expand Up @@ -77,34 +76,34 @@ type CheConfigMap struct {
CheTrustedCABundlesConfigMap string `json:"CHE_TRUSTED__CA__BUNDLES__CONFIGMAP,omitempty"`
}

func SyncCheConfigMapToCluster(checluster *orgv1.CheCluster, proxy *Proxy, clusterAPI ClusterAPI) (*corev1.ConfigMap, error) {
data := GetCheConfigMapData(checluster, proxy)
specConfigMap, err := GetSpecConfigMap(checluster, CheConfigMapName, data, clusterAPI)
func SyncCheConfigMapToCluster(deployContext *DeployContext) (*corev1.ConfigMap, error) {
data := GetCheConfigMapData(deployContext)
specConfigMap, err := GetSpecConfigMap(deployContext, CheConfigMapName, data)
if err != nil {
return nil, err
}

return SyncConfigMapToCluster(checluster, specConfigMap, clusterAPI)
return SyncConfigMapToCluster(deployContext, specConfigMap)
}

// GetConfigMapData 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) {
cheHost := cr.Spec.Server.CheHost
keycloakURL := cr.Spec.Auth.IdentityProviderURL
func GetCheConfigMapData(deployContext *DeployContext) (cheEnv map[string]string) {
cheHost := deployContext.CheCluster.Spec.Server.CheHost
keycloakURL := deployContext.CheCluster.Spec.Auth.IdentityProviderURL
isOpenShift, isOpenshift4, err := util.DetectOpenShift()
if err != nil {
logrus.Errorf("Failed to get current infra: %s", err)
}
cheFlavor := DefaultCheFlavor(cr)
cheFlavor := DefaultCheFlavor(deployContext.CheCluster)
infra := "kubernetes"
if isOpenShift {
infra = "openshift"
}
tls := "false"
openShiftIdentityProviderId := "NULL"
openshiftOAuth := cr.Spec.Auth.OpenShiftoAuth
defaultTargetNamespaceDefault := cr.Namespace // By default Che SA has right in the namespace where Che in installed ...
openshiftOAuth := deployContext.CheCluster.Spec.Auth.OpenShiftoAuth
defaultTargetNamespaceDefault := deployContext.CheCluster.Namespace // By default Che SA has right in the namespace where Che in installed ...
if openshiftOAuth && isOpenShift {
// ... But if the workspace is created under the openshift identity of the end-user,
// Then we'll have rights to create any new namespace
Expand All @@ -114,9 +113,9 @@ func GetCheConfigMapData(cr *orgv1.CheCluster, proxy *Proxy) (cheEnv map[string]
openShiftIdentityProviderId = "openshift-v4"
}
}
defaultTargetNamespace := util.GetValue(cr.Spec.Server.WorkspaceNamespaceDefault, defaultTargetNamespaceDefault)
namespaceAllowUserDefined := strconv.FormatBool(cr.Spec.Server.AllowUserDefinedWorkspaceNamespaces)
tlsSupport := cr.Spec.Server.TlsSupport
defaultTargetNamespace := util.GetValue(deployContext.CheCluster.Spec.Server.WorkspaceNamespaceDefault, defaultTargetNamespaceDefault)
namespaceAllowUserDefined := strconv.FormatBool(deployContext.CheCluster.Spec.Server.AllowUserDefinedWorkspaceNamespaces)
tlsSupport := deployContext.CheCluster.Spec.Server.TlsSupport
protocol := "http"
wsprotocol := "ws"
if tlsSupport {
Expand All @@ -126,50 +125,50 @@ func GetCheConfigMapData(cr *orgv1.CheCluster, proxy *Proxy) (cheEnv map[string]
}

proxyJavaOpts := ""
cheWorkspaceNoProxy := proxy.NoProxy
if proxy.HttpProxy != "" {
if proxy.NoProxy == "" {
cheWorkspaceNoProxy := deployContext.Proxy.NoProxy
if deployContext.Proxy.HttpProxy != "" {
if deployContext.Proxy.NoProxy == "" {
cheWorkspaceNoProxy = os.Getenv("KUBERNETES_SERVICE_HOST")
} else {
cheWorkspaceNoProxy = cheWorkspaceNoProxy + "," + os.Getenv("KUBERNETES_SERVICE_HOST")
}
proxyJavaOpts, err = GenerateProxyJavaOpts(proxy, cheWorkspaceNoProxy)
proxyJavaOpts, err = GenerateProxyJavaOpts(deployContext.Proxy, cheWorkspaceNoProxy)
if err != nil {
logrus.Errorf("Failed to generate java proxy options: %v", err)
}
}

ingressDomain := cr.Spec.K8s.IngressDomain
tlsSecretName := cr.Spec.K8s.TlsSecretName
ingressDomain := deployContext.CheCluster.Spec.K8s.IngressDomain
tlsSecretName := deployContext.CheCluster.Spec.K8s.TlsSecretName
if tlsSupport && tlsSecretName == "" {
tlsSecretName = "che-tls"
}
securityContextFsGroup := util.GetValue(cr.Spec.K8s.SecurityContextFsGroup, DefaultSecurityContextFsGroup)
securityContextRunAsUser := util.GetValue(cr.Spec.K8s.SecurityContextRunAsUser, DefaultSecurityContextRunAsUser)
pvcStrategy := util.GetValue(cr.Spec.Storage.PvcStrategy, DefaultPvcStrategy)
pvcClaimSize := util.GetValue(cr.Spec.Storage.PvcClaimSize, DefaultPvcClaimSize)
workspacePvcStorageClassName := cr.Spec.Storage.WorkspacePVCStorageClassName

defaultPVCJobsImage := DefaultPvcJobsImage(cr)
pvcJobsImage := util.GetValue(cr.Spec.Storage.PvcJobsImage, defaultPVCJobsImage)
securityContextFsGroup := util.GetValue(deployContext.CheCluster.Spec.K8s.SecurityContextFsGroup, DefaultSecurityContextFsGroup)
securityContextRunAsUser := util.GetValue(deployContext.CheCluster.Spec.K8s.SecurityContextRunAsUser, DefaultSecurityContextRunAsUser)
pvcStrategy := util.GetValue(deployContext.CheCluster.Spec.Storage.PvcStrategy, DefaultPvcStrategy)
pvcClaimSize := util.GetValue(deployContext.CheCluster.Spec.Storage.PvcClaimSize, DefaultPvcClaimSize)
workspacePvcStorageClassName := deployContext.CheCluster.Spec.Storage.WorkspacePVCStorageClassName

defaultPVCJobsImage := DefaultPvcJobsImage(deployContext.CheCluster)
pvcJobsImage := util.GetValue(deployContext.CheCluster.Spec.Storage.PvcJobsImage, defaultPVCJobsImage)
preCreateSubPaths := "true"
if !cr.Spec.Storage.PreCreateSubPaths {
if !deployContext.CheCluster.Spec.Storage.PreCreateSubPaths {
preCreateSubPaths = "false"
}
chePostgresHostName := util.GetValue(cr.Spec.Database.ChePostgresHostName, DefaultChePostgresHostName)
chePostgresPort := util.GetValue(cr.Spec.Database.ChePostgresPort, DefaultChePostgresPort)
chePostgresDb := util.GetValue(cr.Spec.Database.ChePostgresDb, DefaultChePostgresDb)
keycloakRealm := util.GetValue(cr.Spec.Auth.IdentityProviderRealm, cheFlavor)
keycloakClientId := util.GetValue(cr.Spec.Auth.IdentityProviderClientId, cheFlavor+"-public")
ingressStrategy := util.GetValue(cr.Spec.K8s.IngressStrategy, DefaultIngressStrategy)
ingressClass := util.GetValue(cr.Spec.K8s.IngressClass, DefaultIngressClass)
devfileRegistryUrl := cr.Status.DevfileRegistryURL
pluginRegistryUrl := cr.Status.PluginRegistryURL
cheLogLevel := util.GetValue(cr.Spec.Server.CheLogLevel, DefaultCheLogLevel)
cheDebug := util.GetValue(cr.Spec.Server.CheDebug, DefaultCheDebug)
cheMetrics := strconv.FormatBool(cr.Spec.Metrics.Enable)
cheLabels := util.MapToKeyValuePairs(GetLabels(cr, DefaultCheFlavor(cr)))
cheMultiUser := GetCheMultiUser(cr)
chePostgresHostName := util.GetValue(deployContext.CheCluster.Spec.Database.ChePostgresHostName, DefaultChePostgresHostName)
chePostgresPort := util.GetValue(deployContext.CheCluster.Spec.Database.ChePostgresPort, DefaultChePostgresPort)
chePostgresDb := util.GetValue(deployContext.CheCluster.Spec.Database.ChePostgresDb, DefaultChePostgresDb)
keycloakRealm := util.GetValue(deployContext.CheCluster.Spec.Auth.IdentityProviderRealm, cheFlavor)
keycloakClientId := util.GetValue(deployContext.CheCluster.Spec.Auth.IdentityProviderClientId, cheFlavor+"-public")
ingressStrategy := util.GetValue(deployContext.CheCluster.Spec.K8s.IngressStrategy, DefaultIngressStrategy)
ingressClass := util.GetValue(deployContext.CheCluster.Spec.K8s.IngressClass, DefaultIngressClass)
devfileRegistryUrl := deployContext.CheCluster.Status.DevfileRegistryURL
pluginRegistryUrl := deployContext.CheCluster.Status.PluginRegistryURL
cheLogLevel := util.GetValue(deployContext.CheCluster.Spec.Server.CheLogLevel, DefaultCheLogLevel)
cheDebug := util.GetValue(deployContext.CheCluster.Spec.Server.CheDebug, DefaultCheDebug)
cheMetrics := strconv.FormatBool(deployContext.CheCluster.Spec.Metrics.Enable)
cheLabels := util.MapToKeyValuePairs(GetLabels(deployContext.CheCluster, DefaultCheFlavor(deployContext.CheCluster)))
cheMultiUser := GetCheMultiUser(deployContext.CheCluster)

data := &CheConfigMap{
CheMultiUser: cheMultiUser,
Expand All @@ -196,27 +195,27 @@ func GetCheConfigMapData(cr *orgv1.CheCluster, proxy *Proxy) (cheEnv map[string]
WorkspaceJavaOpts: DefaultWorkspaceJavaOpts + " " + proxyJavaOpts,
WorkspaceMavenOpts: DefaultWorkspaceJavaOpts + " " + proxyJavaOpts,
WorkspaceProxyJavaOpts: proxyJavaOpts,
WorkspaceHttpProxy: proxy.HttpProxy,
WorkspaceHttpsProxy: proxy.HttpsProxy,
WorkspaceHttpProxy: deployContext.Proxy.HttpProxy,
WorkspaceHttpsProxy: deployContext.Proxy.HttpsProxy,
WorkspaceNoProxy: cheWorkspaceNoProxy,
PluginRegistryUrl: pluginRegistryUrl,
DevfileRegistryUrl: devfileRegistryUrl,
CheWorkspacePluginBrokerMetadataImage: DefaultCheWorkspacePluginBrokerMetadataImage(cr),
CheWorkspacePluginBrokerArtifactsImage: DefaultCheWorkspacePluginBrokerArtifactsImage(cr),
CheServerSecureExposerJwtProxyImage: DefaultCheServerSecureExposerJwtProxyImage(cr),
CheWorkspacePluginBrokerMetadataImage: DefaultCheWorkspacePluginBrokerMetadataImage(deployContext.CheCluster),
CheWorkspacePluginBrokerArtifactsImage: DefaultCheWorkspacePluginBrokerArtifactsImage(deployContext.CheCluster),
CheServerSecureExposerJwtProxyImage: DefaultCheServerSecureExposerJwtProxyImage(deployContext.CheCluster),
CheJGroupsKubernetesLabels: cheLabels,
CheMetricsEnabled: cheMetrics,
CheTrustedCABundlesConfigMap: cr.Spec.Server.ServerTrustStoreConfigMapName,
CheTrustedCABundlesConfigMap: deployContext.CheCluster.Spec.Server.ServerTrustStoreConfigMapName,
}

if cheMultiUser == "true" {
data.KeycloakURL = keycloakURL + "/auth"
data.KeycloakRealm = keycloakRealm
data.KeycloakClientId = keycloakClientId
data.DatabaseURL = "jdbc:postgresql://" + chePostgresHostName + ":" + chePostgresPort + "/" + chePostgresDb
if len(cr.Spec.Database.ChePostgresSecret) < 1 {
data.DbUserName = cr.Spec.Database.ChePostgresUser
data.DbPassword = cr.Spec.Database.ChePostgresPassword
if len(deployContext.CheCluster.Spec.Database.ChePostgresSecret) < 1 {
data.DbUserName = deployContext.CheCluster.Spec.Database.ChePostgresUser
data.DbPassword = deployContext.CheCluster.Spec.Database.ChePostgresPassword
}
}

Expand All @@ -241,6 +240,6 @@ func GetCheConfigMapData(cr *orgv1.CheCluster, proxy *Proxy) (cheEnv map[string]
addMap(cheEnv, k8sCheEnv)
}

addMap(cheEnv, cr.Spec.Server.CustomCheProperties)
addMap(cheEnv, deployContext.CheCluster.Spec.Server.CustomCheProperties)
return cheEnv
}
18 changes: 14 additions & 4 deletions pkg/deploy/che_configmap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,13 @@ 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{})
testCm, _ := GetSpecConfigMap(cr, CheConfigMapName, cheEnv, ClusterAPI{})
deployContext := &DeployContext{
CheCluster: cr,
Proxy: &Proxy{},
ClusterAPI: ClusterAPI{},
}
cheEnv := GetCheConfigMapData(deployContext)
testCm, _ := GetSpecConfigMap(deployContext, CheConfigMapName, cheEnv)
identityProvider := testCm.Data["CHE_INFRA_OPENSHIFT_OAUTH__IDENTITY__PROVIDER"]
_, isOpenshiftv4, _ := util.DetectOpenShift()
protocol := strings.Split(testCm.Data["CHE_API"], "://")[0]
Expand All @@ -53,8 +58,13 @@ func TestConfigMapOverride(t *testing.T) {
"CHE_WORKSPACE_NO_PROXY": "myproxy.myhostname.com",
}
cr.Spec.Auth.OpenShiftoAuth = true
cheEnv := GetCheConfigMapData(cr, &Proxy{})
testCm, _ := GetSpecConfigMap(cr, CheConfigMapName, cheEnv, ClusterAPI{})
deployContext := &DeployContext{
CheCluster: cr,
Proxy: &Proxy{},
ClusterAPI: ClusterAPI{},
}
cheEnv := GetCheConfigMapData(deployContext)
testCm, _ := GetSpecConfigMap(deployContext, CheConfigMapName, cheEnv)
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
27 changes: 22 additions & 5 deletions pkg/deploy/che_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,11 @@ func TestCreateCheDefaultService(t *testing.T) {
Server: orgv1.CheClusterSpecServer{},
},
}

service, err := GetSpecCheService(cheCluster, ClusterAPI{})
deployContext := &DeployContext{
CheCluster: cheCluster,
ClusterAPI: ClusterAPI{},
}
service, err := GetSpecCheService(deployContext)

if service == nil || err != nil {
t.Error("service should be created witn no error")
Expand All @@ -60,8 +63,12 @@ func TestCreateCheServerDebug(t *testing.T) {
},
},
}
deployContext := &DeployContext{
CheCluster: cheCluster,
ClusterAPI: ClusterAPI{},
}

service, err := GetSpecCheService(cheCluster, ClusterAPI{})
service, err := GetSpecCheService(deployContext)

if service == nil || err != nil {
t.Error("service should be created without error")
Expand All @@ -83,7 +90,12 @@ func TestCreateCheServiceEnableMetrics(t *testing.T) {
},
}

service, err := GetSpecCheService(cheCluster, ClusterAPI{})
deployContext := &DeployContext{
CheCluster: cheCluster,
ClusterAPI: ClusterAPI{},
}

service, err := GetSpecCheService(deployContext)

if service == nil || err != nil {
t.Error("service should be created witn no error")
Expand All @@ -104,7 +116,12 @@ func TestCreateCheServiceDisableMetrics(t *testing.T) {
},
}

service, err := GetSpecCheService(cheCluster, ClusterAPI{})
deployContext := &DeployContext{
CheCluster: cheCluster,
ClusterAPI: ClusterAPI{},
}

service, err := GetSpecCheService(deployContext)

if service == nil || err != nil {
t.Error("service should be created witn no error")
Expand Down
20 changes: 9 additions & 11 deletions pkg/deploy/configmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import (
"context"
"fmt"

orgv1 "github.com/eclipse/che-operator/pkg/apis/org/v1"
"github.com/eclipse/che-operator/pkg/util"
"github.com/google/go-cmp/cmp"
"github.com/sirupsen/logrus"
Expand All @@ -27,15 +26,15 @@ import (
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
)

func SyncConfigMapToCluster(checluster *orgv1.CheCluster, specConfigMap *corev1.ConfigMap, clusterAPI ClusterAPI) (*corev1.ConfigMap, error) {
clusterConfigMap, err := getClusterConfigMap(specConfigMap.Name, specConfigMap.Namespace, clusterAPI.Client)
func SyncConfigMapToCluster(deployContext *DeployContext, specConfigMap *corev1.ConfigMap) (*corev1.ConfigMap, error) {
clusterConfigMap, err := getClusterConfigMap(specConfigMap.Name, specConfigMap.Namespace, deployContext.ClusterAPI.Client)
if err != nil {
return nil, err
}

if clusterConfigMap == nil {
logrus.Infof("Creating a new object: %s, name %s", specConfigMap.Kind, specConfigMap.Name)
err := clusterAPI.Client.Create(context.TODO(), specConfigMap)
err := deployContext.ClusterAPI.Client.Create(context.TODO(), specConfigMap)
return nil, err
}

Expand All @@ -44,35 +43,34 @@ func SyncConfigMapToCluster(checluster *orgv1.CheCluster, specConfigMap *corev1.
logrus.Infof("Updating existed object: %s, name: %s", specConfigMap.Kind, specConfigMap.Name)
fmt.Printf("Difference:\n%s", diff)
clusterConfigMap.Data = specConfigMap.Data
err := clusterAPI.Client.Update(context.TODO(), clusterConfigMap)
err := deployContext.ClusterAPI.Client.Update(context.TODO(), clusterConfigMap)
return nil, err
}

return clusterConfigMap, nil
}

func GetSpecConfigMap(
checluster *orgv1.CheCluster,
deployContext *DeployContext,
name string,
data map[string]string,
clusterAPI ClusterAPI) (*corev1.ConfigMap, error) {
data map[string]string) (*corev1.ConfigMap, error) {

labels := GetLabels(checluster, DefaultCheFlavor(checluster))
labels := GetLabels(deployContext.CheCluster, DefaultCheFlavor(deployContext.CheCluster))
configMap := &corev1.ConfigMap{
TypeMeta: metav1.TypeMeta{
Kind: "ConfigMap",
APIVersion: "v1",
},
ObjectMeta: metav1.ObjectMeta{
Name: name,
Namespace: checluster.Namespace,
Namespace: deployContext.CheCluster.Namespace,
Labels: labels,
},
Data: data,
}

if !util.IsTestMode() {
err := controllerutil.SetControllerReference(checluster, configMap, clusterAPI.Scheme)
err := controllerutil.SetControllerReference(deployContext.CheCluster, configMap, deployContext.ClusterAPI.Scheme)
if err != nil {
return nil, err
}
Expand Down
Loading

0 comments on commit ed65945

Please sign in to comment.