Skip to content

Commit

Permalink
Merge pull request #77 from mareklibra/fixDeploymentNamespace
Browse files Browse the repository at this point in the history
Fix deployment namespace
  • Loading branch information
mareklibra committed May 13, 2019
2 parents 27c6f43 + c7dfacf commit a6e2e5f
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@
| {{ openshift_client_binary }} apply -f -
- name: Add service-ca ConfigMap
shell: "{{ openshift_client_binary }} apply -f {{ __service_ca_config_file }}"
shell: "{{ openshift_client_binary }} apply -f {{ __service_ca_config_file }} -n {{ kubevirt_web_ui_namespace }}"

- name: Add V2VVmware Custom Resource Definition
shell: "{{ openshift_client_binary }} apply -f {{ __v2vvmware_crd_file }}"
Expand Down
11 changes: 5 additions & 6 deletions cmd/manager/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,12 @@ import (
"os"
"runtime"

"github.com/operator-framework/operator-sdk/pkg/k8sutil"
"github.com/kubevirt/web-ui-operator/pkg/apis"
"github.com/kubevirt/web-ui-operator/pkg/controller"
"github.com/operator-framework/operator-sdk/pkg/leader"
"github.com/operator-framework/operator-sdk/pkg/ready"
sdkVersion "github.com/operator-framework/operator-sdk/version"
_ "k8s.io/client-go/plugin/pkg/client/auth/gcp"
"github.com/kubevirt/web-ui-operator/pkg/apis"
"github.com/kubevirt/web-ui-operator/pkg/controller"
"sigs.k8s.io/controller-runtime/pkg/client/config"
"sigs.k8s.io/controller-runtime/pkg/manager"
logf "sigs.k8s.io/controller-runtime/pkg/runtime/log"
Expand All @@ -39,13 +38,13 @@ func main() {
logf.SetLogger(logf.ZapLogger(false))

printVersion()

/*
namespace, err := k8sutil.GetWatchNamespace()
if err != nil {
log.Error(err, "failed to get watch namespace")
os.Exit(1)
}

*/
// Get a config to talk to the apiserver
cfg, err := config.GetConfig()
if err != nil {
Expand All @@ -67,7 +66,7 @@ func main() {
defer r.Unset()

// Create a new Cmd to provide shared dependencies and start components
mgr, err := manager.New(cfg, manager.Options{Namespace: namespace})
mgr, err := manager.New(cfg, manager.Options{Namespace: ""}) // Resources will be watched in all namespaces to support even the cluster-scoped deployment (HCO)
if err != nil {
log.Error(err, "")
os.Exit(1)
Expand Down
4 changes: 4 additions & 0 deletions pkg/components/components.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,10 @@ func GetDeployment(namespace string, repository string, tag string, imagePullPol
return deployment
}


// The GetRole() is probably not needed since the use of `components.go` implicates operator's deployment for cluster-scope (within HCO).
// To avoid confusion: If deployed independently using the deploy/*.yaml files, the operator is namespace-scoped.
// TODO: validate the statement above and optionally remove this GetRole() function.
func GetRole(namespace string) *rbacv1.Role {
role := &rbacv1.Role{
TypeMeta: metav1.TypeMeta{
Expand Down
12 changes: 10 additions & 2 deletions pkg/controller/kwebui/kwebui_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ package kwebui

import (
"context"
"fmt"

extenstionsv1beta1 "k8s.io/api/extensions/v1beta1"
extenstionsv1beta1 "k8s.io/api/extensions/v1beta1"
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
Expand Down Expand Up @@ -95,10 +96,17 @@ func (r *ReconcileKWebUI) Reconcile(request reconcile.Request) (reconcile.Result
}
reqLogger.Info("Desired kubevirt-web-ui version: ", "instance.Spec.Version", instance.Spec.Version)

if instance.Spec.Version == VersionAutomatic {
instance.Spec.Version = getWebUIVersion("")
log.Info(fmt.Sprintf("Requested 'automatic' version which is resolved to: %s", instance.Spec.Version))
updateVersion(r, request, instance.Spec.Version)
}

// Fetch the kubevirt-web-ui Deployment
deployment := &extenstionsv1beta1.Deployment{}
err = r.client.Get(context.TODO(), types.NamespacedName{Name: "console", Namespace: request.Namespace}, deployment)
err = r.client.Get(context.TODO(), types.NamespacedName{Name: "console", Namespace: getWebUINamespace()}, deployment)
if err != nil {
reqLogger.Error(err, "Looking for the console Deployment object")
if errors.IsNotFound(err) {
return freshProvision(r, request, instance)
}
Expand Down
27 changes: 18 additions & 9 deletions pkg/controller/kwebui/provision.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,6 @@ func ReconcileExistingDeployment(r *ReconcileKWebUI, request reconcile.Request,
return reconcile.Result{}, nil
}

if instance.Spec.Version == VersionAutomatic {
instance.Spec.Version = getWebUIVersion("")
log.Info(fmt.Sprintf("Requested 'automatic' version which is resolved to: %s", instance.Spec.Version))
updateVersion(r, request, instance.Spec.Version)
}

if instance.Spec.Version == existingVersion {
msg := fmt.Sprintf("Existing version conforms the requested one: %s. Nothing to do.", existingVersion)
log.Info(msg)
Expand Down Expand Up @@ -114,7 +108,7 @@ func freshProvision(r *ReconcileKWebUI, request reconcile.Request, instance *kub
// Kubevirt-web-ui deployment is not present yet
log.Info("kubevirt-web-ui Deployment is not present. Ansible playbook will be executed to provision it.")
updateStatus(r, request, PhaseFreshProvision, fmt.Sprintf("Target version: %s", instance.Spec.Version))
res, err := runPlaybookWithSetup(request.Namespace, instance, "provision")
res, err := runPlaybookWithSetup(getWebUINamespace(), instance, "provision")
if err == nil {
setOwnerReference(r, request, instance)
updateStatus(r, request, PhaseProvisioned, "Provision finished.")
Expand All @@ -127,7 +121,7 @@ func freshProvision(r *ReconcileKWebUI, request reconcile.Request, instance *kub
func deprovision(r *ReconcileKWebUI, request reconcile.Request, instance *kubevirtv1alpha1.KWebUI) (reconcile.Result, error) {
log.Info("Existing kubevirt-web-ui deployment is about to be deprovisioned.")
updateStatus(r, request, PhaseDeprovision, "")
res, err := runPlaybookWithSetup(request.Namespace, instance, "deprovision")
res, err := runPlaybookWithSetup(getWebUINamespace(), instance, "deprovision")
if err == nil {
updateStatus(r, request, PhaseDeprovisioned, "Deprovision finished.")
} else {
Expand Down Expand Up @@ -166,6 +160,17 @@ func loginClient(namespace string) (string, error) {
}
err = RunCommand(cmd, args, env, args)
if err != nil {
log.Error(err, "Failed to switch to the project. Trying to create it.", "Namespace", namespace)

cmd, args = "oc", []string{
"new-project",
namespace,
}
err = RunCommand(cmd, args, env, args)
if err != nil {
log.Error(err, "Failed to create project for the web-ui.", "Namespace", namespace)
}

return "", err
}

Expand All @@ -176,6 +181,10 @@ func getWebUIVersion(versionInCR string) string {
return Def(versionInCR, os.Getenv("WEBUI_TAG"),"v1.4")
}

func getWebUINamespace() string {
return "kubevirt-web-ui"
}

func generateInventory(instance *kubevirtv1alpha1.KWebUI, namespace string, action string) (string, error) {
log.Info("Writing inventory file")
inventoryFile := fmt.Sprintf(InventoryFilePattern, Unique())
Expand Down Expand Up @@ -226,7 +235,7 @@ func generateInventory(instance *kubevirtv1alpha1.KWebUI, namespace string, acti

func setOwnerReference(r *ReconcileKWebUI, request reconcile.Request, instance *kubevirtv1alpha1.KWebUI) error {
deployment := &extenstionsv1beta1.Deployment{}
err := r.client.Get(context.TODO(), types.NamespacedName{Name: "console", Namespace: request.Namespace}, deployment)
err := r.client.Get(context.TODO(), types.NamespacedName{Name: "console", Namespace: getWebUINamespace()}, deployment)
if err != nil {
msg := "Failed to retrieve the just created kubevirt-web-ui Deployment object to set owner reference."
log.Error(err, msg)
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit a6e2e5f

Please sign in to comment.