Skip to content

Commit

Permalink
virt-operator: Deploy node-restriction policy
Browse files Browse the repository at this point in the history
virt-operator will deploy the validatingAdmission policy
and binding objects implementing the node restriction for
virt-handler.

Signed-off-by: Ram Lavi <[email protected]>
  • Loading branch information
RamLavi committed Jun 5, 2024
1 parent 613ef9c commit a8dcb76
Show file tree
Hide file tree
Showing 11 changed files with 683 additions and 131 deletions.
50 changes: 50 additions & 0 deletions pkg/controller/virtinformers.go
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,18 @@ type KubeInformerFactory interface {
// Fake ServiceMonitor informer used when Prometheus is not installed
DummyOperatorServiceMonitor() cache.SharedIndexInformer

// ValidatingAdmissionPolicyBinding created/managed by virt operator
OperatorValidatingAdmissionPolicyBinding() cache.SharedIndexInformer

// Fake OperatorValidatingAdmissionPolicyBinding informer used when ValidatingAdmissionPolicyBinding is not installed
DummyOperatorValidatingAdmissionPolicyBinding() cache.SharedIndexInformer

// ValidatingAdmissionPolicies created/managed by virt operator
OperatorValidatingAdmissionPolicy() cache.SharedIndexInformer

// Fake OperatorValidatingAdmissionPolicy informer used when ValidatingAdmissionPolicy is not installed
DummyOperatorValidatingAdmissionPolicy() cache.SharedIndexInformer

// The namespace where kubevirt is deployed in
Namespace() cache.SharedIndexInformer

Expand Down Expand Up @@ -1283,6 +1295,44 @@ func (f *kubeInformerFactory) DummyOperatorServiceMonitor() cache.SharedIndexInf
})
}

func (f *kubeInformerFactory) OperatorValidatingAdmissionPolicyBinding() cache.SharedIndexInformer {
return f.getInformer("operatorValidatingAdmissionPolicyBindingInformer", func() cache.SharedIndexInformer {
labelSelector, err := labels.Parse(OperatorLabel)
if err != nil {
panic(err)
}

lw := NewListWatchFromClient(f.clientSet.AdmissionregistrationV1().RESTClient(), "validatingadmissionpolicybindings", k8sv1.NamespaceAll, fields.Everything(), labelSelector)
return cache.NewSharedIndexInformer(lw, &admissionregistrationv1.ValidatingAdmissionPolicyBinding{}, f.defaultResync, cache.Indexers{cache.NamespaceIndex: cache.MetaNamespaceIndexFunc})
})
}

func (f *kubeInformerFactory) DummyOperatorValidatingAdmissionPolicyBinding() cache.SharedIndexInformer {
return f.getInformer("FakeOperatorValidatingAdmissionPolicyBindingInformer", func() cache.SharedIndexInformer {
informer, _ := testutils.NewFakeInformerFor(&admissionregistrationv1.ValidatingAdmissionPolicyBinding{})
return informer
})
}

func (f *kubeInformerFactory) OperatorValidatingAdmissionPolicy() cache.SharedIndexInformer {
return f.getInformer("operatorValidatingAdmissionPolicyInformer", func() cache.SharedIndexInformer {
labelSelector, err := labels.Parse(OperatorLabel)
if err != nil {
panic(err)
}

lw := NewListWatchFromClient(f.clientSet.AdmissionregistrationV1().RESTClient(), "validatingadmissionpolicies", k8sv1.NamespaceAll, fields.Everything(), labelSelector)
return cache.NewSharedIndexInformer(lw, &admissionregistrationv1.ValidatingAdmissionPolicy{}, f.defaultResync, cache.Indexers{cache.NamespaceIndex: cache.MetaNamespaceIndexFunc})
})
}

func (f *kubeInformerFactory) DummyOperatorValidatingAdmissionPolicy() cache.SharedIndexInformer {
return f.getInformer("FakeOperatorValidatingAdmissionPolicyInformer", func() cache.SharedIndexInformer {
informer, _ := testutils.NewFakeInformerFor(&admissionregistrationv1.ValidatingAdmissionPolicy{})
return informer
})
}

func (f *kubeInformerFactory) K8SInformerFactory() informers.SharedInformerFactory {
return f.k8sInformers
}
Expand Down
30 changes: 30 additions & 0 deletions pkg/virt-operator/application.go
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,36 @@ func Execute() {
app.stores.PrometheusRuleCache = app.informerFactory.DummyOperatorPrometheusRule().GetStore()
}

validatingAdmissionPolicyBindingEnabled, err := util.IsValidatingAdmissionPolicyBindingEnabled(app.clientSet)
if err != nil {
golog.Fatalf("Error checking for ValidatingAdmissionPolicyBinding: %v", err)
}
if validatingAdmissionPolicyBindingEnabled {
log.Log.Info("validatingAdmissionPolicyBindingEnabled is defined")
app.informers.ValidatingAdmissionPolicyBinding = app.informerFactory.OperatorValidatingAdmissionPolicyBinding()
app.stores.ValidatingAdmissionPolicyBindingCache = app.informerFactory.OperatorValidatingAdmissionPolicyBinding().GetStore()
app.stores.ValidatingAdmissionPolicyBindingEnabled = true
} else {
log.Log.Info("validatingAdmissionPolicyBindingEnabled is not defined")
app.informers.ValidatingAdmissionPolicyBinding = app.informerFactory.DummyOperatorValidatingAdmissionPolicyBinding()
app.stores.ValidatingAdmissionPolicyBindingCache = app.informerFactory.DummyOperatorValidatingAdmissionPolicyBinding().GetStore()
}

validatingAdmissionPolicyEnabled, err := util.IsValidatingAdmissionPolicyEnabled(app.clientSet)
if err != nil {
golog.Fatalf("Error checking for ValidatingAdmissionPolicy: %v", err)
}
if validatingAdmissionPolicyEnabled {
log.Log.Info("validatingAdmissionPolicyEnabled is defined")
app.informers.ValidatingAdmissionPolicy = app.informerFactory.OperatorValidatingAdmissionPolicy()
app.stores.ValidatingAdmissionPolicyCache = app.informerFactory.OperatorValidatingAdmissionPolicy().GetStore()
app.stores.ValidatingAdmissionPolicyEnabled = true
} else {
log.Log.Info("validatingAdmissionPolicyEnabled is not defined")
app.informers.ValidatingAdmissionPolicy = app.informerFactory.DummyOperatorValidatingAdmissionPolicy()
app.stores.ValidatingAdmissionPolicyCache = app.informerFactory.DummyOperatorValidatingAdmissionPolicy().GetStore()
}

app.prepareCertManagers()

app.kubeVirtRecorder = app.getNewRecorder(k8sv1.NamespaceAll, VirtOperator)
Expand Down
76 changes: 55 additions & 21 deletions pkg/virt-operator/kubevirt.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,27 +99,29 @@ func NewKubeVirtController(
stores: stores,
informers: informers,
kubeVirtExpectations: util.Expectations{
ServiceAccount: controller.NewUIDTrackingControllerExpectations(controller.NewControllerExpectationsWithName("ServiceAccount")),
ClusterRole: controller.NewUIDTrackingControllerExpectations(controller.NewControllerExpectationsWithName("ClusterRole")),
ClusterRoleBinding: controller.NewUIDTrackingControllerExpectations(controller.NewControllerExpectationsWithName("ClusterRoleBinding")),
Role: controller.NewUIDTrackingControllerExpectations(controller.NewControllerExpectationsWithName("Role")),
RoleBinding: controller.NewUIDTrackingControllerExpectations(controller.NewControllerExpectationsWithName("RoleBinding")),
Crd: controller.NewUIDTrackingControllerExpectations(controller.NewControllerExpectationsWithName("Crd")),
Service: controller.NewUIDTrackingControllerExpectations(controller.NewControllerExpectationsWithName("Service")),
Deployment: controller.NewUIDTrackingControllerExpectations(controller.NewControllerExpectationsWithName("Deployment")),
DaemonSet: controller.NewUIDTrackingControllerExpectations(controller.NewControllerExpectationsWithName("DaemonSet")),
ValidationWebhook: controller.NewUIDTrackingControllerExpectations(controller.NewControllerExpectationsWithName("ValidationWebhook")),
MutatingWebhook: controller.NewUIDTrackingControllerExpectations(controller.NewControllerExpectationsWithName("MutatingWebhook")),
APIService: controller.NewUIDTrackingControllerExpectations(controller.NewControllerExpectationsWithName("APIService")),
SCC: controller.NewUIDTrackingControllerExpectations(controller.NewControllerExpectationsWithName("SCC")),
Route: controller.NewUIDTrackingControllerExpectations(controller.NewControllerExpectationsWithName("Route")),
InstallStrategyConfigMap: controller.NewUIDTrackingControllerExpectations(controller.NewControllerExpectationsWithName("InstallStrategyConfigMap")),
InstallStrategyJob: controller.NewUIDTrackingControllerExpectations(controller.NewControllerExpectationsWithName("Jobs")),
PodDisruptionBudget: controller.NewUIDTrackingControllerExpectations(controller.NewControllerExpectationsWithName("PodDisruptionBudgets")),
ServiceMonitor: controller.NewUIDTrackingControllerExpectations(controller.NewControllerExpectationsWithName("ServiceMonitor")),
PrometheusRule: controller.NewUIDTrackingControllerExpectations(controller.NewControllerExpectationsWithName("PrometheusRule")),
Secrets: controller.NewUIDTrackingControllerExpectations(controller.NewControllerExpectationsWithName("Secret")),
ConfigMap: controller.NewUIDTrackingControllerExpectations(controller.NewControllerExpectationsWithName("ConfigMap")),
ServiceAccount: controller.NewUIDTrackingControllerExpectations(controller.NewControllerExpectationsWithName("ServiceAccount")),
ClusterRole: controller.NewUIDTrackingControllerExpectations(controller.NewControllerExpectationsWithName("ClusterRole")),
ClusterRoleBinding: controller.NewUIDTrackingControllerExpectations(controller.NewControllerExpectationsWithName("ClusterRoleBinding")),
Role: controller.NewUIDTrackingControllerExpectations(controller.NewControllerExpectationsWithName("Role")),
RoleBinding: controller.NewUIDTrackingControllerExpectations(controller.NewControllerExpectationsWithName("RoleBinding")),
Crd: controller.NewUIDTrackingControllerExpectations(controller.NewControllerExpectationsWithName("Crd")),
Service: controller.NewUIDTrackingControllerExpectations(controller.NewControllerExpectationsWithName("Service")),
Deployment: controller.NewUIDTrackingControllerExpectations(controller.NewControllerExpectationsWithName("Deployment")),
DaemonSet: controller.NewUIDTrackingControllerExpectations(controller.NewControllerExpectationsWithName("DaemonSet")),
ValidationWebhook: controller.NewUIDTrackingControllerExpectations(controller.NewControllerExpectationsWithName("ValidationWebhook")),
MutatingWebhook: controller.NewUIDTrackingControllerExpectations(controller.NewControllerExpectationsWithName("MutatingWebhook")),
APIService: controller.NewUIDTrackingControllerExpectations(controller.NewControllerExpectationsWithName("APIService")),
SCC: controller.NewUIDTrackingControllerExpectations(controller.NewControllerExpectationsWithName("SCC")),
Route: controller.NewUIDTrackingControllerExpectations(controller.NewControllerExpectationsWithName("Route")),
InstallStrategyConfigMap: controller.NewUIDTrackingControllerExpectations(controller.NewControllerExpectationsWithName("InstallStrategyConfigMap")),
InstallStrategyJob: controller.NewUIDTrackingControllerExpectations(controller.NewControllerExpectationsWithName("Jobs")),
PodDisruptionBudget: controller.NewUIDTrackingControllerExpectations(controller.NewControllerExpectationsWithName("PodDisruptionBudgets")),
ServiceMonitor: controller.NewUIDTrackingControllerExpectations(controller.NewControllerExpectationsWithName("ServiceMonitor")),
PrometheusRule: controller.NewUIDTrackingControllerExpectations(controller.NewControllerExpectationsWithName("PrometheusRule")),
Secrets: controller.NewUIDTrackingControllerExpectations(controller.NewControllerExpectationsWithName("Secret")),
ConfigMap: controller.NewUIDTrackingControllerExpectations(controller.NewControllerExpectationsWithName("ConfigMap")),
ValidatingAdmissionPolicyBinding: controller.NewUIDTrackingControllerExpectations(controller.NewControllerExpectationsWithName("ValidatingAdmissionPolicyBinding")),
ValidatingAdmissionPolicy: controller.NewUIDTrackingControllerExpectations(controller.NewControllerExpectationsWithName("ValidatingAdmissionPolicy")),
},

operatorNamespace: operatorNamespace,
Expand Down Expand Up @@ -479,6 +481,36 @@ func NewKubeVirtController(
return nil, err
}

_, err = c.informers.ValidatingAdmissionPolicyBinding.AddEventHandler(cache.ResourceEventHandlerFuncs{
AddFunc: func(obj interface{}) {
c.genericAddHandler(obj, c.kubeVirtExpectations.ValidatingAdmissionPolicyBinding)
},
DeleteFunc: func(obj interface{}) {
c.genericDeleteHandler(obj, c.kubeVirtExpectations.ValidatingAdmissionPolicyBinding)
},
UpdateFunc: func(oldObj, newObj interface{}) {
c.genericUpdateHandler(oldObj, newObj, c.kubeVirtExpectations.ValidatingAdmissionPolicyBinding)
},
})
if err != nil {
return nil, err
}

_, err = c.informers.ValidatingAdmissionPolicy.AddEventHandler(cache.ResourceEventHandlerFuncs{
AddFunc: func(obj interface{}) {
c.genericAddHandler(obj, c.kubeVirtExpectations.ValidatingAdmissionPolicy)
},
DeleteFunc: func(obj interface{}) {
c.genericDeleteHandler(obj, c.kubeVirtExpectations.ValidatingAdmissionPolicy)
},
UpdateFunc: func(oldObj, newObj interface{}) {
c.genericUpdateHandler(oldObj, newObj, c.kubeVirtExpectations.ValidatingAdmissionPolicy)
},
})
if err != nil {
return nil, err
}

return &c, nil
}

Expand Down Expand Up @@ -652,6 +684,8 @@ func (c *KubeVirtController) Run(threadiness int, stopCh <-chan struct{}) {
cache.WaitForCacheSync(stopCh, c.informers.PrometheusRule.HasSynced)
cache.WaitForCacheSync(stopCh, c.informers.Secrets.HasSynced)
cache.WaitForCacheSync(stopCh, c.informers.ConfigMap.HasSynced)
cache.WaitForCacheSync(stopCh, c.informers.ValidatingAdmissionPolicyBinding.HasSynced)
cache.WaitForCacheSync(stopCh, c.informers.ValidatingAdmissionPolicy.HasSynced)

// Start the actual work
for i := 0; i < threadiness; i++ {
Expand Down
Loading

0 comments on commit a8dcb76

Please sign in to comment.