diff --git a/Makefile b/Makefile index 2c11f178..096faed4 100644 --- a/Makefile +++ b/Makefile @@ -108,6 +108,9 @@ validate: generate docs fmt ## Validate the project checking for any dependency .PHONY: install install: ## Install K3k with Helm on the targeted Kubernetes cluster helm upgrade --install --namespace k3k-system --create-namespace \ + --set controller.extraEnv[0].name=LOG_FORMAT \ + --set controller.extraEnv[0].value=console \ + --set controller.extraEnv[1].name=DEBUG \ --set controller.image.repository=$(REPO)/k3k \ --set controller.image.tag=$(VERSION) \ --set agent.shared.image.repository=$(REPO)/k3k-kubelet \ diff --git a/pkg/controller/cluster/agent/agent.go b/pkg/controller/cluster/agent/agent.go index ba673918..a17986c2 100644 --- a/pkg/controller/cluster/agent/agent.go +++ b/pkg/controller/cluster/agent/agent.go @@ -42,11 +42,8 @@ func configSecretName(clusterName string) string { } func ensureObject(ctx context.Context, cfg *Config, obj ctrlruntimeclient.Object) error { - log := ctrl.LoggerFrom(ctx) - key := ctrlruntimeclient.ObjectKeyFromObject(obj) - - log.Info(fmt.Sprintf("ensuring %T", obj), "key", key) + log := ctrl.LoggerFrom(ctx).WithValues("key", key) if err := controllerutil.SetControllerReference(cfg.cluster, obj, cfg.scheme); err != nil { return err @@ -54,11 +51,15 @@ func ensureObject(ctx context.Context, cfg *Config, obj ctrlruntimeclient.Object if err := cfg.client.Create(ctx, obj); err != nil { if apierrors.IsAlreadyExists(err) { + log.V(1).Info(fmt.Sprintf("Resource %T already exists, updating.", obj)) + return cfg.client.Update(ctx, obj) } return err } + log.V(1).Info(fmt.Sprintf("Creating %T.", obj)) + return nil } diff --git a/pkg/controller/cluster/cluster.go b/pkg/controller/cluster/cluster.go index d83080c9..d5bc9c8b 100644 --- a/pkg/controller/cluster/cluster.go +++ b/pkg/controller/cluster/cluster.go @@ -161,10 +161,8 @@ func namespaceEventHandler(r *ClusterReconciler) handler.Funcs { } func (c *ClusterReconciler) Reconcile(ctx context.Context, req reconcile.Request) (reconcile.Result, error) { - log := ctrl.LoggerFrom(ctx).WithValues("cluster", req.NamespacedName) - ctx = ctrl.LoggerInto(ctx, log) // enrich the current logger - - log.Info("reconciling cluster") + log := ctrl.LoggerFrom(ctx) + log.Info("Reconciling Cluster") var cluster v1beta1.Cluster if err := c.Client.Get(ctx, req.NamespacedName, &cluster); err != nil { @@ -178,6 +176,8 @@ func (c *ClusterReconciler) Reconcile(ctx context.Context, req reconcile.Request // Set initial status if not already set if cluster.Status.Phase == "" || cluster.Status.Phase == v1beta1.ClusterUnknown { + log.V(1).Info("Updating Cluster status phase") + cluster.Status.Phase = v1beta1.ClusterProvisioning meta.SetStatusCondition(&cluster.Status.Conditions, metav1.Condition{ Type: ConditionReady, @@ -195,6 +195,8 @@ func (c *ClusterReconciler) Reconcile(ctx context.Context, req reconcile.Request // add finalizer if controllerutil.AddFinalizer(&cluster, clusterFinalizerName) { + log.V(1).Info("Updating Cluster adding finalizer") + if err := c.Client.Update(ctx, &cluster); err != nil { return reconcile.Result{}, err } @@ -207,6 +209,8 @@ func (c *ClusterReconciler) Reconcile(ctx context.Context, req reconcile.Request reconcilerErr := c.reconcileCluster(ctx, &cluster) if !equality.Semantic.DeepEqual(orig.Status, cluster.Status) { + log.Info("Updating Cluster status") + if err := c.Client.Status().Update(ctx, &cluster); err != nil { return reconcile.Result{}, err } @@ -215,7 +219,7 @@ func (c *ClusterReconciler) Reconcile(ctx context.Context, req reconcile.Request // if there was an error during the reconciliation, return if reconcilerErr != nil { if errors.Is(reconcilerErr, bootstrap.ErrServerNotReady) { - log.Info("server not ready, requeueing") + log.V(1).Info("Server not ready, requeueing") return reconcile.Result{RequeueAfter: time.Second * 10}, nil } @@ -224,6 +228,8 @@ func (c *ClusterReconciler) Reconcile(ctx context.Context, req reconcile.Request // update Cluster if needed if !equality.Semantic.DeepEqual(orig.Spec, cluster.Spec) { + log.Info("Updating Cluster") + if err := c.Client.Update(ctx, &cluster); err != nil { return reconcile.Result{}, err } @@ -234,7 +240,7 @@ func (c *ClusterReconciler) Reconcile(ctx context.Context, req reconcile.Request func (c *ClusterReconciler) reconcileCluster(ctx context.Context, cluster *v1beta1.Cluster) error { err := c.reconcile(ctx, cluster) - c.updateStatus(cluster, err) + c.updateStatus(ctx, cluster, err) return err } @@ -264,7 +270,7 @@ func (c *ClusterReconciler) reconcile(ctx context.Context, cluster *v1beta1.Clus // if the Version is not specified we will try to use the same Kubernetes version of the host. // This version is stored in the Status object, and it will not be updated if already set. if cluster.Spec.Version == "" && cluster.Status.HostVersion == "" { - log.Info("cluster version not set") + log.V(1).Info("Cluster version not set. Using host version.") hostVersion, err := c.DiscoveryClient.ServerVersion() if err != nil { @@ -295,7 +301,7 @@ func (c *ClusterReconciler) reconcile(ctx context.Context, cluster *v1beta1.Clus if cluster.Status.ServiceCIDR == "" { // in shared mode try to lookup the serviceCIDR if cluster.Spec.Mode == v1beta1.SharedClusterMode { - log.Info("looking up Service CIDR for shared mode") + log.V(1).Info("Looking up Service CIDR for shared mode") cluster.Status.ServiceCIDR, err = c.lookupServiceCIDR(ctx) if err != nil { @@ -307,7 +313,7 @@ func (c *ClusterReconciler) reconcile(ctx context.Context, cluster *v1beta1.Clus // in virtual mode assign a default serviceCIDR if cluster.Spec.Mode == v1beta1.VirtualClusterMode { - log.Info("assign default service CIDR for virtual mode") + log.V(1).Info("assign default service CIDR for virtual mode") cluster.Status.ServiceCIDR = defaultVirtualServiceCIDR } @@ -354,7 +360,7 @@ func (c *ClusterReconciler) reconcile(ctx context.Context, cluster *v1beta1.Clus // ensureBootstrapSecret will create or update the Secret containing the bootstrap data from the k3s server func (c *ClusterReconciler) ensureBootstrapSecret(ctx context.Context, cluster *v1beta1.Cluster, serviceIP, token string) error { log := ctrl.LoggerFrom(ctx) - log.Info("ensuring bootstrap secret") + log.V(1).Info("Ensuring bootstrap secret") bootstrapData, err := bootstrap.GenerateBootstrapData(ctx, cluster, serviceIP, token) if err != nil { @@ -386,7 +392,7 @@ func (c *ClusterReconciler) ensureBootstrapSecret(ctx context.Context, cluster * // ensureKubeconfigSecret will create or update the Secret containing the kubeconfig data from the k3s server func (c *ClusterReconciler) ensureKubeconfigSecret(ctx context.Context, cluster *v1beta1.Cluster, serviceIP string, port int) error { log := ctrl.LoggerFrom(ctx) - log.Info("ensuring kubeconfig secret") + log.V(1).Info("Ensuring Kubeconfig Secret") adminKubeconfig := kubeconfig.New() @@ -460,7 +466,7 @@ func (c *ClusterReconciler) createClusterConfigs(ctx context.Context, cluster *v func (c *ClusterReconciler) ensureNetworkPolicy(ctx context.Context, cluster *v1beta1.Cluster) error { log := ctrl.LoggerFrom(ctx) - log.Info("ensuring network policy") + log.V(1).Info("Ensuring network policy") networkPolicyName := controller.SafeConcatNameWithPrefix(cluster.Name) @@ -544,7 +550,7 @@ func (c *ClusterReconciler) ensureNetworkPolicy(ctx context.Context, cluster *v1 key := client.ObjectKeyFromObject(currentNetworkPolicy) if result != controllerutil.OperationResultNone { - log.Info("cluster network policy updated", "key", key, "result", result) + log.V(1).Info("Cluster network policy updated", "key", key, "result", result) } return nil @@ -552,7 +558,7 @@ func (c *ClusterReconciler) ensureNetworkPolicy(ctx context.Context, cluster *v1 func (c *ClusterReconciler) ensureClusterService(ctx context.Context, cluster *v1beta1.Cluster) (*v1.Service, error) { log := ctrl.LoggerFrom(ctx) - log.Info("ensuring cluster service") + log.V(1).Info("Ensuring Cluster Service") expectedService := server.Service(cluster) currentService := expectedService.DeepCopy() @@ -572,7 +578,7 @@ func (c *ClusterReconciler) ensureClusterService(ctx context.Context, cluster *v key := client.ObjectKeyFromObject(currentService) if result != controllerutil.OperationResultNone { - log.Info("cluster service updated", "key", key, "result", result) + log.V(1).Info("Cluster service updated", "key", key, "result", result) } return currentService, nil @@ -580,7 +586,7 @@ func (c *ClusterReconciler) ensureClusterService(ctx context.Context, cluster *v func (c *ClusterReconciler) ensureIngress(ctx context.Context, cluster *v1beta1.Cluster) error { log := ctrl.LoggerFrom(ctx) - log.Info("ensuring cluster ingress") + log.V(1).Info("Ensuring cluster ingress") expectedServerIngress := server.Ingress(ctx, cluster) @@ -608,7 +614,7 @@ func (c *ClusterReconciler) ensureIngress(ctx context.Context, cluster *v1beta1. key := client.ObjectKeyFromObject(currentServerIngress) if result != controllerutil.OperationResultNone { - log.Info("cluster ingress updated", "key", key, "result", result) + log.V(1).Info("Cluster ingress updated", "key", key, "result", result) } return nil @@ -650,7 +656,7 @@ func (c *ClusterReconciler) server(ctx context.Context, cluster *v1beta1.Cluster if result != controllerutil.OperationResultNone { key := client.ObjectKeyFromObject(currentServerStatefulSet) - log.Info("ensuring serverStatefulSet", "key", key, "result", result) + log.V(1).Info("Ensuring server StatefulSet", "key", key, "result", result) } return err @@ -753,7 +759,7 @@ func (c *ClusterReconciler) lookupServiceCIDR(ctx context.Context) (string, erro // Try to look for the serviceCIDR creating a failing service. // The error should contain the expected serviceCIDR - log.Info("looking up serviceCIDR from a failing service creation") + log.V(1).Info("Looking up Service CIDR from a failing service creation") failingSvc := v1.Service{ ObjectMeta: metav1.ObjectMeta{Name: "fail", Namespace: "default"}, @@ -765,7 +771,7 @@ func (c *ClusterReconciler) lookupServiceCIDR(ctx context.Context) (string, erro if len(splittedErrMsg) > 1 { serviceCIDR := strings.TrimSpace(splittedErrMsg[1]) - log.Info("found serviceCIDR from failing service creation: " + serviceCIDR) + log.V(1).Info("Found Service CIDR from failing service creation: " + serviceCIDR) // validate serviceCIDR _, serviceCIDRAddr, err := net.ParseCIDR(serviceCIDR) @@ -779,7 +785,7 @@ func (c *ClusterReconciler) lookupServiceCIDR(ctx context.Context) (string, erro // Try to look for the the kube-apiserver Pod, and look for the '--service-cluster-ip-range' flag. - log.Info("looking up serviceCIDR from kube-apiserver pod") + log.V(1).Info("Looking up Service CIDR from kube-apiserver pod") matchingLabels := client.MatchingLabels(map[string]string{ "component": "kube-apiserver", @@ -802,12 +808,12 @@ func (c *ClusterReconciler) lookupServiceCIDR(ctx context.Context) (string, erro for _, arg := range apiServerArgs { if strings.HasPrefix(arg, "--service-cluster-ip-range=") { serviceCIDR := strings.TrimPrefix(arg, "--service-cluster-ip-range=") - log.Info("found serviceCIDR from kube-apiserver pod: " + serviceCIDR) + log.V(1).Info("Found Service CIDR from kube-apiserver pod: " + serviceCIDR) // validate serviceCIDR _, serviceCIDRAddr, err := net.ParseCIDR(serviceCIDR) if err != nil { - log.Error(err, "serviceCIDR is not valid") + log.Error(err, "Service CIDR is not valid") break } diff --git a/pkg/controller/cluster/cluster_finalize.go b/pkg/controller/cluster/cluster_finalize.go index dd80ee58..08861fd9 100644 --- a/pkg/controller/cluster/cluster_finalize.go +++ b/pkg/controller/cluster/cluster_finalize.go @@ -23,7 +23,7 @@ import ( func (c *ClusterReconciler) finalizeCluster(ctx context.Context, cluster *v1beta1.Cluster) (reconcile.Result, error) { log := ctrl.LoggerFrom(ctx) - log.Info("finalizing Cluster") + log.V(1).Info("Deleting Cluster") // Set the Terminating phase and condition cluster.Status.Phase = v1beta1.ClusterTerminating @@ -40,7 +40,7 @@ func (c *ClusterReconciler) finalizeCluster(ctx context.Context, cluster *v1beta // Deallocate ports for kubelet and webhook if used if cluster.Spec.Mode == v1beta1.SharedClusterMode && cluster.Spec.MirrorHostNodes { - log.Info("dellocating ports for kubelet and webhook") + log.V(1).Info("dellocating ports for kubelet and webhook") if err := c.PortAllocator.DeallocateKubeletPort(ctx, cluster.Name, cluster.Namespace, cluster.Status.KubeletPort); err != nil { return reconcile.Result{}, err @@ -53,6 +53,8 @@ func (c *ClusterReconciler) finalizeCluster(ctx context.Context, cluster *v1beta // Remove finalizer from the cluster and update it only when all resources are cleaned up if controllerutil.RemoveFinalizer(cluster, clusterFinalizerName) { + log.Info("Deleting Cluster removing finalizer") + if err := c.Client.Update(ctx, cluster); err != nil { return reconcile.Result{}, err } @@ -62,6 +64,9 @@ func (c *ClusterReconciler) finalizeCluster(ctx context.Context, cluster *v1beta } func (c *ClusterReconciler) unbindClusterRoles(ctx context.Context, cluster *v1beta1.Cluster) error { + log := ctrl.LoggerFrom(ctx) + log.V(1).Info("Unbinding ClusterRoles") + clusterRoles := []string{"k3k-kubelet-node", "k3k-priorityclass"} var err error diff --git a/pkg/controller/cluster/pod.go b/pkg/controller/cluster/pod.go index 0e2a6864..cdd18d4a 100644 --- a/pkg/controller/cluster/pod.go +++ b/pkg/controller/cluster/pod.go @@ -9,7 +9,6 @@ import ( "sigs.k8s.io/controller-runtime/pkg/reconcile" v1 "k8s.io/api/core/v1" - apierrors "k8s.io/apimachinery/pkg/api/errors" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" ctrl "sigs.k8s.io/controller-runtime" ctrlruntimeclient "sigs.k8s.io/controller-runtime/pkg/client" @@ -44,14 +43,10 @@ func AddPodController(ctx context.Context, mgr manager.Manager, maxConcurrentRec func (r *PodReconciler) Reconcile(ctx context.Context, req reconcile.Request) (reconcile.Result, error) { log := ctrl.LoggerFrom(ctx) - log.Info("reconciling pod") + log.V(1).Info("Reconciling Pod") var pod v1.Pod if err := r.Client.Get(ctx, req.NamespacedName, &pod); err != nil { - if !apierrors.IsNotFound(err) { - return reconcile.Result{}, err - } - return reconcile.Result{}, ctrlruntimeclient.IgnoreNotFound(err) } @@ -74,6 +69,8 @@ func (r *PodReconciler) Reconcile(ctx context.Context, req reconcile.Request) (r }, } + log.V(1).Info("Deleting Virtual Pod", "name", virtName, "namespace", virtNamespace) + return reconcile.Result{}, ctrlruntimeclient.IgnoreNotFound(virtualClient.Delete(ctx, &virtPod)) } diff --git a/pkg/controller/cluster/service.go b/pkg/controller/cluster/service.go index 1bf1b771..e62dc771 100644 --- a/pkg/controller/cluster/service.go +++ b/pkg/controller/cluster/service.go @@ -39,7 +39,7 @@ func AddServiceController(ctx context.Context, mgr manager.Manager, maxConcurren func (r *ServiceReconciler) Reconcile(ctx context.Context, req reconcile.Request) (reconcile.Result, error) { log := ctrl.LoggerFrom(ctx) - log.Info("ensuring service status to virtual cluster") + log.V(1).Info("Reconciling Service") var hostService v1.Service if err := r.HostClient.Get(ctx, req.NamespacedName, &hostService); err != nil { @@ -53,7 +53,7 @@ func (r *ServiceReconciler) Reconcile(ctx context.Context, req reconcile.Request virtualServiceNamespace, virtualServiceNamespaceFound := hostService.Annotations[translate.ResourceNamespaceAnnotation] if !virtualServiceNameFound || !virtualServiceNamespaceFound { - log.V(1).Info(fmt.Sprintf("service %s/%s does not have virtual service annotations, skipping", hostService.Namespace, hostService.Name)) + log.V(1).Info(fmt.Sprintf("Service %s/%s does not have virtual service annotations, skipping", hostService.Namespace, hostService.Name)) return reconcile.Result{}, nil } @@ -80,7 +80,10 @@ func (r *ServiceReconciler) Reconcile(ctx context.Context, req reconcile.Request } if !equality.Semantic.DeepEqual(virtualService.Status.LoadBalancer, hostService.Status.LoadBalancer) { + log.V(1).Info("Updating Virtual Service Status", "name", virtualServiceName, "namespace", virtualServiceNamespace) + virtualService.Status.LoadBalancer = hostService.Status.LoadBalancer + if err := virtualClient.Status().Update(ctx, &virtualService); err != nil { return reconcile.Result{}, err } diff --git a/pkg/controller/cluster/statefulset.go b/pkg/controller/cluster/statefulset.go index 155e6156..f41c1d7d 100644 --- a/pkg/controller/cluster/statefulset.go +++ b/pkg/controller/cluster/statefulset.go @@ -62,7 +62,7 @@ func AddStatefulSetController(ctx context.Context, mgr manager.Manager, maxConcu func (p *StatefulSetReconciler) Reconcile(ctx context.Context, req reconcile.Request) (reconcile.Result, error) { log := ctrl.LoggerFrom(ctx) - log.Info("reconciling statefulset") + log.Info("Reconciling StatefulSet") var sts apps.StatefulSet if err := p.Client.Get(ctx, req.NamespacedName, &sts); err != nil { @@ -116,10 +116,12 @@ func (p *StatefulSetReconciler) Reconcile(ctx context.Context, req reconcile.Req func (p *StatefulSetReconciler) handleServerPod(ctx context.Context, cluster v1beta1.Cluster, pod *v1.Pod) error { log := ctrl.LoggerFrom(ctx) - log.Info("handling server pod") + log.V(1).Info("Handling Server Pod") if pod.DeletionTimestamp.IsZero() { if controllerutil.AddFinalizer(pod, etcdPodFinalizerName) { + log.V(1).Info("Server Pod is being deleted. Removing finalizer", "pod", pod.Name, "namespace", pod.Namespace) + return p.Client.Update(ctx, pod) } @@ -131,6 +133,8 @@ func (p *StatefulSetReconciler) handleServerPod(ctx context.Context, cluster v1b // check if cluster is deleted then remove the finalizer from the pod if cluster.Name == "" { if controllerutil.RemoveFinalizer(pod, etcdPodFinalizerName) { + log.V(1).Info("Cluster was deleted. Deleting Server Pod removing finalizer", "pod", pod.Name, "namespace", pod.Namespace) + if err := p.Client.Update(ctx, pod); err != nil { return err } @@ -161,6 +165,8 @@ func (p *StatefulSetReconciler) handleServerPod(ctx context.Context, cluster v1b // remove our finalizer from the list and update it. if controllerutil.RemoveFinalizer(pod, etcdPodFinalizerName) { + log.V(1).Info("Deleting Server Pod removing finalizer", "pod", pod.Name, "namespace", pod.Namespace) + if err := p.Client.Update(ctx, pod); err != nil { return err } @@ -171,7 +177,7 @@ func (p *StatefulSetReconciler) handleServerPod(ctx context.Context, cluster v1b func (p *StatefulSetReconciler) getETCDTLS(ctx context.Context, cluster *v1beta1.Cluster) (*tls.Config, error) { log := ctrl.LoggerFrom(ctx) - log.Info("generating etcd TLS client certificate", "cluster", cluster) + log.V(1).Info("Generating ETCD TLS client certificate", "cluster", cluster) token, err := p.clusterToken(ctx, cluster) if err != nil { @@ -219,7 +225,7 @@ func (p *StatefulSetReconciler) getETCDTLS(ctx context.Context, cluster *v1beta1 // removePeer removes a peer from the cluster. The peer name and IP address must both match. func removePeer(ctx context.Context, client *clientv3.Client, name, address string) error { log := ctrl.LoggerFrom(ctx) - log.Info("removing peer from cluster", "name", name, "address", address) + log.V(1).Info("Removing peer from cluster", "name", name, "address", address) ctx, cancel := context.WithTimeout(ctx, memberRemovalTimeout) defer cancel() @@ -241,7 +247,7 @@ func removePeer(ctx context.Context, client *clientv3.Client, name, address stri } if u.Hostname() == address { - log.Info("removing member from etcd", "name", member.Name, "id", member.ID, "address", address) + log.V(1).Info("Removing member from ETCD", "name", member.Name, "id", member.ID, "address", address) _, err := client.MemberRemove(ctx, member.ID) if errors.Is(err, rpctypes.ErrGRPCMemberNotFound) { @@ -280,6 +286,8 @@ func (p *StatefulSetReconciler) clusterToken(ctx context.Context, cluster *v1bet } func (p *StatefulSetReconciler) handleDeletion(ctx context.Context, sts *apps.StatefulSet) (ctrl.Result, error) { + log := ctrl.LoggerFrom(ctx) + podList, err := p.listPods(ctx, sts) if err != nil { return reconcile.Result{}, err @@ -287,6 +295,8 @@ func (p *StatefulSetReconciler) handleDeletion(ctx context.Context, sts *apps.St for _, pod := range podList.Items { if controllerutil.RemoveFinalizer(&pod, etcdPodFinalizerName) { + log.V(1).Info("Updating Server Pod removing finalizer", "name", pod.Name, "namespace", pod.Namespace) + if err := p.Client.Update(ctx, &pod); err != nil { return reconcile.Result{}, err } diff --git a/pkg/controller/cluster/status.go b/pkg/controller/cluster/status.go index 0857490b..06310f29 100644 --- a/pkg/controller/cluster/status.go +++ b/pkg/controller/cluster/status.go @@ -1,12 +1,14 @@ package cluster import ( + "context" "errors" "k8s.io/apimachinery/pkg/api/meta" v1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + ctrl "sigs.k8s.io/controller-runtime" "github.com/rancher/k3k/pkg/apis/k3k.io/v1beta1" "github.com/rancher/k3k/pkg/controller/cluster/server/bootstrap" @@ -24,7 +26,10 @@ const ( ReasonTerminating = "Terminating" ) -func (c *ClusterReconciler) updateStatus(cluster *v1beta1.Cluster, reconcileErr error) { +func (c *ClusterReconciler) updateStatus(ctx context.Context, cluster *v1beta1.Cluster, reconcileErr error) { + log := ctrl.LoggerFrom(ctx) + log.V(1).Info("Updating Cluster Conditions") + if !cluster.DeletionTimestamp.IsZero() { cluster.Status.Phase = v1beta1.ClusterTerminating meta.SetStatusCondition(&cluster.Status.Conditions, metav1.Condition{ diff --git a/pkg/controller/cluster/token.go b/pkg/controller/cluster/token.go index 75185966..4cce5ec4 100644 --- a/pkg/controller/cluster/token.go +++ b/pkg/controller/cluster/token.go @@ -62,7 +62,7 @@ func (c *ClusterReconciler) ensureTokenSecret(ctx context.Context, cluster *v1be return string(tokenSecret.Data["token"]), nil } - log.Info("Token secret is not specified, creating a random token") + log.V(1).Info("Token secret is not specified, creating a random token") token, err := random(16) if err != nil { @@ -77,7 +77,7 @@ func (c *ClusterReconciler) ensureTokenSecret(ctx context.Context, cluster *v1be }) if result != controllerutil.OperationResultNone { - log.Info("ensuring tokenSecret", "key", key, "result", result) + log.V(1).Info("Ensuring tokenSecret", "key", key, "result", result) } return token, err diff --git a/pkg/controller/policy/namespace.go b/pkg/controller/policy/namespace.go index f65d98bc..4cb5ca05 100644 --- a/pkg/controller/policy/namespace.go +++ b/pkg/controller/policy/namespace.go @@ -17,7 +17,7 @@ import ( // reconcileNamespacePodSecurityLabels will update the labels of the namespace to reconcile the PSA level specified in the VirtualClusterPolicy func (c *VirtualClusterPolicyReconciler) reconcileNamespacePodSecurityLabels(ctx context.Context, namespace *v1.Namespace, policy *v1beta1.VirtualClusterPolicy) { log := ctrl.LoggerFrom(ctx) - log.Info("reconciling PSA labels") + log.V(1).Info("Reconciling PSA labels") // cleanup of old labels delete(namespace.Labels, "pod-security.kubernetes.io/enforce") @@ -44,7 +44,7 @@ func (c *VirtualClusterPolicyReconciler) reconcileNamespacePodSecurityLabels(ctx // deleting the resources in them with the "app.kubernetes.io/managed-by=k3k-policy-controller" label func (c *VirtualClusterPolicyReconciler) cleanupNamespaces(ctx context.Context) error { log := ctrl.LoggerFrom(ctx) - log.Info("deleting resources") + log.V(1).Info("Cleanup Namespace resources") var namespaces v1.NamespaceList if err := c.Client.List(ctx, &namespaces); err != nil { diff --git a/pkg/controller/policy/networkpolicy.go b/pkg/controller/policy/networkpolicy.go index 4f3d6bec..4ce5a677 100644 --- a/pkg/controller/policy/networkpolicy.go +++ b/pkg/controller/policy/networkpolicy.go @@ -17,7 +17,7 @@ import ( func (c *VirtualClusterPolicyReconciler) reconcileNetworkPolicy(ctx context.Context, namespace string, policy *v1beta1.VirtualClusterPolicy) error { log := ctrl.LoggerFrom(ctx) - log.Info("reconciling NetworkPolicy") + log.V(1).Info("Reconciling NetworkPolicy") var cidrList []string @@ -46,13 +46,18 @@ func (c *VirtualClusterPolicyReconciler) reconcileNetworkPolicy(ctx context.Cont // if disabled then delete the existing network policy if policy.Spec.DisableNetworkPolicy { - err := c.Client.Delete(ctx, networkPolicy) - return client.IgnoreNotFound(err) + log.V(1).Info("Deleting NetworkPolicy") + + return client.IgnoreNotFound(c.Client.Delete(ctx, networkPolicy)) } + log.V(1).Info("Creating NetworkPolicy") + // otherwise try to create/update err := c.Client.Create(ctx, networkPolicy) if apierrors.IsAlreadyExists(err) { + log.V(1).Info("NetworkPolicy already exists, updating.") + return c.Client.Update(ctx, networkPolicy) } diff --git a/pkg/controller/policy/policy.go b/pkg/controller/policy/policy.go index d05b5b8b..2e89ecb7 100644 --- a/pkg/controller/policy/policy.go +++ b/pkg/controller/policy/policy.go @@ -248,7 +248,7 @@ func clusterEventHandler(r *VirtualClusterPolicyReconciler) handler.Funcs { func (c *VirtualClusterPolicyReconciler) Reconcile(ctx context.Context, req reconcile.Request) (reconcile.Result, error) { log := ctrl.LoggerFrom(ctx) - log.Info("reconciling VirtualClusterPolicy") + log.Info("Reconciling VirtualClusterPolicy") var policy v1beta1.VirtualClusterPolicy if err := c.Client.Get(ctx, req.NamespacedName, &policy); err != nil { @@ -261,6 +261,8 @@ func (c *VirtualClusterPolicyReconciler) Reconcile(ctx context.Context, req reco // update Status if needed if !reflect.DeepEqual(orig.Status, policy.Status) { + log.Info("Updating VirtualClusterPolicy Status") + if err := c.Client.Status().Update(ctx, &policy); err != nil { return reconcile.Result{}, err } @@ -273,6 +275,8 @@ func (c *VirtualClusterPolicyReconciler) Reconcile(ctx context.Context, req reco // update VirtualClusterPolicy if needed if !reflect.DeepEqual(orig, policy) { + log.Info("Updating VirtualClusterPolicy") + if err := c.Client.Update(ctx, &policy); err != nil { return reconcile.Result{}, err } @@ -295,7 +299,7 @@ func (c *VirtualClusterPolicyReconciler) reconcileVirtualClusterPolicy(ctx conte func (c *VirtualClusterPolicyReconciler) reconcileMatchingNamespaces(ctx context.Context, policy *v1beta1.VirtualClusterPolicy) error { log := ctrl.LoggerFrom(ctx) - log.Info("reconciling matching Namespaces") + log.V(1).Info("Reconciling matching Namespaces") listOpts := client.MatchingLabels{ PolicyNameLabelKey: policy.Name, @@ -307,8 +311,10 @@ func (c *VirtualClusterPolicyReconciler) reconcileMatchingNamespaces(ctx context } for _, ns := range namespaces.Items { - ctx = ctrl.LoggerInto(ctx, log.WithValues("namespace", ns.Name)) - log.Info("reconciling Namespace") + log = log.WithValues("namespace", ns.Name) + ctx = ctrl.LoggerInto(ctx, log) + + log.V(1).Info("Reconciling Namespace") orig := ns.DeepCopy() @@ -331,6 +337,8 @@ func (c *VirtualClusterPolicyReconciler) reconcileMatchingNamespaces(ctx context c.reconcileNamespacePodSecurityLabels(ctx, &ns, policy) if !reflect.DeepEqual(orig, &ns) { + log.Info("Updating Namespace") + if err := c.Client.Update(ctx, &ns); err != nil { return err } @@ -342,7 +350,7 @@ func (c *VirtualClusterPolicyReconciler) reconcileMatchingNamespaces(ctx context func (c *VirtualClusterPolicyReconciler) reconcileQuota(ctx context.Context, namespace string, policy *v1beta1.VirtualClusterPolicy) error { log := ctrl.LoggerFrom(ctx) - log.Info("reconciling ResourceQuota") + log.V(1).Info("Reconciling ResourceQuota") if policy.Spec.Quota == nil { // check if resourceQuota object exists and deletes it. @@ -357,6 +365,8 @@ func (c *VirtualClusterPolicyReconciler) reconcileQuota(ctx context.Context, nam return client.IgnoreNotFound(err) } + log.V(1).Info("Deleting ResourceQuota") + return c.Client.Delete(ctx, &toDeleteResourceQuota) } @@ -381,8 +391,12 @@ func (c *VirtualClusterPolicyReconciler) reconcileQuota(ctx context.Context, nam return err } + log.V(1).Info("Creating ResourceQuota") + err := c.Client.Create(ctx, resourceQuota) if apierrors.IsAlreadyExists(err) { + log.V(1).Info("ResourceQuota already exists, updating.") + return c.Client.Update(ctx, resourceQuota) } @@ -391,7 +405,7 @@ func (c *VirtualClusterPolicyReconciler) reconcileQuota(ctx context.Context, nam func (c *VirtualClusterPolicyReconciler) reconcileLimit(ctx context.Context, namespace string, policy *v1beta1.VirtualClusterPolicy) error { log := ctrl.LoggerFrom(ctx) - log.Info("reconciling LimitRange") + log.V(1).Info("Reconciling LimitRange") // delete limitrange if spec.limits isnt specified. if policy.Spec.Limit == nil { @@ -406,6 +420,8 @@ func (c *VirtualClusterPolicyReconciler) reconcileLimit(ctx context.Context, nam return client.IgnoreNotFound(err) } + log.V(1).Info("Deleting LimitRange") + return c.Client.Delete(ctx, &toDeleteLimitRange) } @@ -429,8 +445,12 @@ func (c *VirtualClusterPolicyReconciler) reconcileLimit(ctx context.Context, nam return err } + log.V(1).Info("Creating LimitRange") + err := c.Client.Create(ctx, limitRange) if apierrors.IsAlreadyExists(err) { + log.V(1).Info("LimitRange already exists, updating.") + return c.Client.Update(ctx, limitRange) } @@ -439,7 +459,7 @@ func (c *VirtualClusterPolicyReconciler) reconcileLimit(ctx context.Context, nam func (c *VirtualClusterPolicyReconciler) reconcileClusters(ctx context.Context, namespace *v1.Namespace, policy *v1beta1.VirtualClusterPolicy) error { log := ctrl.LoggerFrom(ctx) - log.Info("reconciling Clusters") + log.V(1).Info("Reconciling Clusters") var clusters v1beta1.ClusterList if err := c.Client.List(ctx, &clusters, client.InNamespace(namespace.Name)); err != nil { @@ -455,6 +475,8 @@ func (c *VirtualClusterPolicyReconciler) reconcileClusters(ctx context.Context, cluster.Spec.NodeSelector = policy.Spec.DefaultNodeSelector if !reflect.DeepEqual(orig, cluster) { + log.V(1).Info("Updating Cluster", "cluster", cluster.Name, "namespace", namespace.Name) + // continue updating also the other clusters even if an error occurred clusterUpdateErrs = append(clusterUpdateErrs, c.Client.Update(ctx, &cluster)) }