diff --git a/build/kubevirt-web-ui-ansible/roles/kubevirt_web_ui/tasks/provision.yml b/build/kubevirt-web-ui-ansible/roles/kubevirt_web_ui/tasks/provision.yml index a22ff23..4f54a2a 100644 --- a/build/kubevirt-web-ui-ansible/roles/kubevirt_web_ui/tasks/provision.yml +++ b/build/kubevirt-web-ui-ansible/roles/kubevirt_web_ui/tasks/provision.yml @@ -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 }}" diff --git a/cmd/manager/main.go b/cmd/manager/main.go index 682c27d..e773afa 100644 --- a/cmd/manager/main.go +++ b/cmd/manager/main.go @@ -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" @@ -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 { @@ -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) diff --git a/pkg/components/components.go b/pkg/components/components.go index dff3cbc..ed37959 100644 --- a/pkg/components/components.go +++ b/pkg/components/components.go @@ -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{ diff --git a/pkg/controller/kwebui/kwebui_controller.go b/pkg/controller/kwebui/kwebui_controller.go index 1b5ffee..05298a6 100644 --- a/pkg/controller/kwebui/kwebui_controller.go +++ b/pkg/controller/kwebui/kwebui_controller.go @@ -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" @@ -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) } diff --git a/pkg/controller/kwebui/provision.go b/pkg/controller/kwebui/provision.go index 6a256d8..47cfda9 100644 --- a/pkg/controller/kwebui/provision.go +++ b/pkg/controller/kwebui/provision.go @@ -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) @@ -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.") @@ -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 { @@ -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 } @@ -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()) @@ -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) diff --git a/vendor/github.com/operator-framework/operator-sdk/pkg/leader/leader.go b/vendor/github.com/operator-framework/operator-sdk/pkg/leader/leader.go index 4b977f9..d269af6 100644 --- a/vendor/github.com/operator-framework/operator-sdk/pkg/leader/leader.go +++ b/vendor/github.com/operator-framework/operator-sdk/pkg/leader/leader.go @@ -96,7 +96,7 @@ func Become(ctx context.Context, lockName string) error { log.Info("Continuing as the leader.") return nil } else { - log.Info("Found existing lock", "LockOwner", existingOwner.Name) + log.Info("Found existing lock", "LockOwner", existingOwner.Name, "lockName", lockName, "Namespace", ns) } } case apierrors.IsNotFound(err):