THIS IS ONLY A TEST INSTANCE. DON'T DO IMPORTANT WORK HERE!

Unverified Commit 46c98c20 authored by Anatolii Bazko's avatar Anatolii Bazko Committed by GitHub
Browse files

Enable to debug operator locally (#182)



* Enable to debug operator locally
Signed-off-by: default avatarOleksandr Andriienko <oandriie@redhat.com>
Co-authored-by: default avatarOleksandr Andriienko <oandriie@redhat.com>
parent b001254b
{
"version": "0.2.0",
"configurations": [
{
"name": "Che Operator",
"type": "go",
"request": "launch",
"mode": "auto",
"program": "${workspaceFolder}/cmd/manager/main.go",
"env": {
"WATCH_NAMESPACE": "che"
},
"cwd": "${workspaceFolder}",
"args": [
"--defaults-path",
"deploy/operator.yaml"
]
}
]
}
......@@ -23,8 +23,8 @@ import (
"runtime"
"github.com/eclipse/che-operator/pkg/apis"
"github.com/eclipse/che-operator/pkg/deploy"
"github.com/eclipse/che-operator/pkg/controller"
"github.com/eclipse/che-operator/pkg/deploy"
"github.com/operator-framework/operator-sdk/pkg/leader"
"github.com/operator-framework/operator-sdk/pkg/ready"
sdkVersion "github.com/operator-framework/operator-sdk/version"
......@@ -35,6 +35,14 @@ import (
//logf "sigs.k8s.io/controller-runtime/pkg/runtime/log"
)
var (
defaultsPath string
)
func init() {
flag.StringVar(&defaultsPath, "defaults-path", "", "Path to file with operator deployment defaults. This option is useful for local development.")
}
func setLogLevel() {
logLevel, isFound := os.LookupEnv("LOG_LEVEL")
if isFound && len(logLevel) > 0 {
......@@ -76,8 +84,7 @@ func printVersion() {
func main() {
flag.Parse()
deploy.InitDefaultsFromEnv()
//logf.SetLogger(logf.ZapLogger(false))
deploy.InitDefaults(defaultsPath)
printVersion()
namespace, err := k8sutil.GetWatchNamespace()
if err != nil {
......
#!/bin/bash
#
# Copyright (c) 2019 Red Hat, Inc.
# This program and the accompanying materials are made
# available under the terms of the Eclipse Public License 2.0
# which is available at https://www.eclipse.org/legal/epl-2.0/
#
# SPDX-License-Identifier: EPL-2.0
#
# Contributors:
# Red Hat, Inc. - initial API and implementation
set -e
if [ $# -ne 1 ]; then
echo -e "Wrong number of parameters.\nUsage: ./loca-debug.sh <custom-resource-yaml>\n"
exit 1
fi
command -v delv >/dev/null 2>&1 || { echo "operator-sdk is not installed. Aborting."; exit 1; }
command -v operator-sdk >/dev/null 2>&1 || { echo -e $RED"operator-sdk is not installed. Aborting."$NC; exit 1; }
CHE_NAMESPACE=che
kubectl create namespace $CHE_NAMESPACE
kubectl apply -f deploy/crds/org_v1_che_crd.yaml
kubectl apply -f $1 -n che
operator-sdk up local --namespace=${CHE_NAMESPACE} --enable-delve
......@@ -1161,8 +1161,8 @@ func (r *ReconcileChe) Reconcile(request reconcile.Request) (reconcile.Result, e
effectiveImagePullPolicy := string(effectiveCheDeployment.Spec.Template.Spec.Containers[0].ImagePullPolicy)
desiredSelfSignedCert := instance.Spec.Server.SelfSignedCert
desiredGitSelfSignedCert := instance.Spec.Server.GitSelfSignedCert
effectiveSelfSignedCert := r.GetDeploymentEnvVarSource(effectiveCheDeployment, "CHE_SELF__SIGNED__CERT") != nil
effectiveGitSelfSignedCert := r.GetDeploymentEnvVarSource(effectiveCheDeployment, "CHE_GIT_SELF__SIGNED__CERT") != nil
effectiveSelfSignedCert := util.GetDeploymentEnvVarSource(effectiveCheDeployment, "CHE_SELF__SIGNED__CERT") != nil
effectiveGitSelfSignedCert := util.GetDeploymentEnvVarSource(effectiveCheDeployment, "CHE_GIT_SELF__SIGNED__CERT") != nil
if desiredMemRequest.Cmp(effectiveMemRequest) != 0 ||
desiredMemLimit.Cmp(effectiveMemLimit) != 0 ||
effectiveImagePullPolicy != desiredImagePullPolicy ||
......
......@@ -294,6 +294,7 @@ func (r *ReconcileChe) CreateIdentityProviderItems(instance *orgv1.CheCluster, r
return err
}
}
keycloakURL := instance.Spec.Auth.IdentityProviderURL
keycloakRealm := util.GetValue(instance.Spec.Auth.IdentityProviderRealm, cheFlavor)
oAuthClient := deploy.NewOAuthClient(oAuthClientName, oauthSecret, keycloakURL, keycloakRealm, isOpenShift4)
......@@ -367,7 +368,7 @@ func (r *ReconcileChe) GenerateAndSaveFields(instance *orgv1.CheCluster, request
if err != nil {
logrus.Info("Disregard the error. No existing Identity provider deployment found. Generating passwd")
} else {
keycloakPostgresPassword = r.GetDeploymentEnv(keycloakDeployment, "DB_PASSWORD")
keycloakPostgresPassword = util.GetDeploymentEnv(keycloakDeployment, "DB_PASSWORD")
}
if err := r.UpdateCheCRSpec(instance, "auto-generated Keycloak DB password", "password-hidden"); err != nil {
return err
......@@ -379,7 +380,7 @@ func (r *ReconcileChe) GenerateAndSaveFields(instance *orgv1.CheCluster, request
if err != nil {
logrus.Info("Disregard the error. No existing Identity provider deployment found. Generating passwd")
} else {
keycloakAdminPassword = r.GetDeploymentEnv(keycloakDeployment, "SSO_ADMIN_PASSWORD")
keycloakAdminPassword = util.GetDeploymentEnv(keycloakDeployment, "SSO_ADMIN_PASSWORD")
}
instance.Spec.Auth.IdentityProviderPassword = keycloakAdminPassword
if err := r.UpdateCheCRSpec(instance, "Keycloak admin password", "password hidden"); err != nil {
......@@ -392,7 +393,7 @@ func (r *ReconcileChe) GenerateAndSaveFields(instance *orgv1.CheCluster, request
if err != nil {
logrus.Info("Disregard the error. No existing Identity provider deployment found. Generating admin username")
} else {
keycloakAdminUserName = r.GetDeploymentEnv(keycloakDeployment, "SSO_ADMIN_USERNAME")
keycloakAdminUserName = util.GetDeploymentEnv(keycloakDeployment, "SSO_ADMIN_USERNAME")
}
instance.Spec.Auth.IdentityProviderAdminUserName = keycloakAdminUserName
if err := r.UpdateCheCRSpec(instance, "Keycloak admin username", keycloakAdminUserName); err != nil {
......
......@@ -96,27 +96,3 @@ func (r *ReconcileChe) GetOAuthClient(oAuthClientName string) (oAuthClient *oaut
}
return oAuthClient, nil
}
func (r *ReconcileChe)GetDeploymentEnv(deployment *appsv1.Deployment, key string) (value string) {
env := deployment.Spec.Template.Spec.Containers[0].Env
for i := range env {
name := env[i].Name
if name == key {
value = env[i].Value
break
}
}
return value
}
func (r *ReconcileChe)GetDeploymentEnvVarSource(deployment *appsv1.Deployment, key string) (valueFrom *corev1.EnvVarSource) {
env := deployment.Spec.Template.Spec.Containers[0].Env
for i := range env {
name := env[i].Name
if name == key {
valueFrom = env[i].ValueFrom
break
}
}
return valueFrom
}
\ No newline at end of file
......@@ -14,10 +14,15 @@ package deploy
import (
"fmt"
"github.com/sirupsen/logrus"
"io/ioutil"
"os"
"strings"
util "github.com/eclipse/che-operator/pkg/util"
"github.com/sirupsen/logrus"
v1 "k8s.io/api/apps/v1"
"k8s.io/client-go/kubernetes/scheme"
orgv1 "github.com/eclipse/che-operator/pkg/apis/org/v1"
)
......@@ -89,6 +94,14 @@ const (
defaultConsoleLinkDisplayName = "CodeReady Workspaces"
)
func InitDefaults(defaultsPath string) {
if defaultsPath == "" {
InitDefaultsFromEnv()
} else {
InitDefaultsFromFile(defaultsPath)
}
}
func InitDefaultsFromEnv() {
defaultCheVersion = getDefaultFromEnv("CHE_VERSION")
defaultCheServerImage = getDefaultFromEnv("IMAGE_default_che_server")
......@@ -106,6 +119,41 @@ func InitDefaultsFromEnv() {
defaultCheServerSecureExposerJwtProxyImage = getDefaultFromEnv("IMAGE_default_che_server_secure_exposer_jwt_proxy_image")
}
func InitDefaultsFromFile(defaultsPath string) {
operatorDeployment := getDefaultsFromFile(defaultsPath)
defaultCheVersion = util.GetDeploymentEnv(operatorDeployment, "CHE_VERSION")
defaultCheServerImage = util.GetDeploymentEnv(operatorDeployment, "IMAGE_default_che_server")
defaultPluginRegistryImage = util.GetDeploymentEnv(operatorDeployment, "IMAGE_default_plugin_registry")
defaultDevfileRegistryImage = util.GetDeploymentEnv(operatorDeployment, "IMAGE_default_devfile_registry")
defaultPvcJobsImage = util.GetDeploymentEnv(operatorDeployment, "IMAGE_default_pvc_jobs")
defaultPostgresImage = util.GetDeploymentEnv(operatorDeployment, "IMAGE_default_postgres")
defaultKeycloakImage = util.GetDeploymentEnv(operatorDeployment, "IMAGE_default_keycloak")
defaultCheWorkspacePluginBrokerMetadataImage = util.GetDeploymentEnv(operatorDeployment, "IMAGE_default_che_workspace_plugin_broker_metadata")
defaultCheWorkspacePluginBrokerArtifactsImage = util.GetDeploymentEnv(operatorDeployment, "IMAGE_default_che_workspace_plugin_broker_artifacts")
defaultCheServerSecureExposerJwtProxyImage = util.GetDeploymentEnv(operatorDeployment, "IMAGE_default_che_server_secure_exposer_jwt_proxy_image")
}
func getDefaultsFromFile(defaultsPath string) *v1.Deployment {
bytes, err := ioutil.ReadFile(defaultsPath)
if err != nil {
logrus.Fatalf("Unable to read file with defaults by path %s", defaultsPath)
}
decode := scheme.Codecs.UniversalDeserializer().Decode
obj, _, err := decode(bytes, nil, nil)
if err != nil {
logrus.Fatalf(fmt.Sprintf("Error while decoding YAML object with defaults. Err was: %s", err))
}
deployment, ok := obj.(*v1.Deployment)
if ok {
return deployment
}
logrus.Fatalf("File %s doesn't contains real deployment.", defaultsPath)
return nil
}
func getDefaultFromEnv(envName string) string {
value := os.Getenv(envName)
......
......@@ -24,6 +24,8 @@ import (
"net/http"
"os"
"sigs.k8s.io/controller-runtime/pkg/client/config"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
"strings"
"time"
"bytes"
......@@ -263,3 +265,27 @@ func GenerateProxyEnvs(proxyHost string, proxyPort string, nonProxyHosts string,
return proxyUrl, noProxy
}
func GetDeploymentEnv(deployment *appsv1.Deployment, key string) (value string) {
env := deployment.Spec.Template.Spec.Containers[0].Env
for i := range env {
name := env[i].Name
if name == key {
value = env[i].Value
break
}
}
return value
}
func GetDeploymentEnvVarSource(deployment *appsv1.Deployment, key string) (valueFrom *corev1.EnvVarSource) {
env := deployment.Spec.Template.Spec.Containers[0].Env
for i := range env {
name := env[i].Name
if name == key {
valueFrom = env[i].ValueFrom
break
}
}
return valueFrom
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment