Skip to content

Commit

Permalink
Create a PriorityClass for KubeVirt on startup (#669) (#686)
Browse files Browse the repository at this point in the history
Until now, the PriorityClass for KubeVirt was created during
reconciliation (after creating the CR for the HCO).  This caused the
virt-operator to exit with a FailedCreate status, and the CSV to stay in
a Failed status.
This patch creates the PriorityClass when the HCO starts, allowing the
virt-operator to be installed properly and the CSV's status to change to
Succeeded.

Signed-off-by: Yuval Turgeman <yturgema@redhat.com>
  • Loading branch information
Yuval Turgeman committed Jul 6, 2020
1 parent f1f02d2 commit fed3bad
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 26 deletions.
32 changes: 30 additions & 2 deletions cmd/hyperconverged-cluster-operator/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ import (
"context"
"flag"
"fmt"
"os"
"runtime"

"github.com/kubevirt/hyperconverged-cluster-operator/pkg/apis"
"github.com/kubevirt/hyperconverged-cluster-operator/pkg/controller"
securityv1 "github.com/openshift/api/security/v1"
Expand All @@ -15,8 +18,7 @@ import (
"github.com/spf13/pflag"
"k8s.io/apimachinery/pkg/util/intstr"
"k8s.io/client-go/rest"
"os"
"runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/client/config"
"sigs.k8s.io/controller-runtime/pkg/manager"
"sigs.k8s.io/controller-runtime/pkg/runtime/signals"
Expand All @@ -29,6 +31,7 @@ import (
kubemetrics "github.com/operator-framework/operator-sdk/pkg/kube-metrics"
sdkVersion "github.com/operator-framework/operator-sdk/version"
corev1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
apiruntime "k8s.io/apimachinery/pkg/runtime"
_ "k8s.io/client-go/plugin/pkg/client/auth/gcp"
cdiv1alpha1 "kubevirt.io/containerized-data-importer/pkg/apis/core/v1alpha1"
Expand Down Expand Up @@ -189,6 +192,12 @@ func main() {
}
}

err = createPriorityClass(ctx, mgr)
if err != nil {
log.Error(err, "Failed creating PriorityClass")
os.Exit(1)
}

log.Info("Starting the Cmd.")

// Start the Cmd
Expand All @@ -198,6 +207,25 @@ func main() {
}
}

func createPriorityClass(ctx context.Context, mgr manager.Manager) error {
pc := hcoutil.NewKubeVirtPriorityClass()

key, err := client.ObjectKeyFromObject(pc)
if err != nil {
log.Error(err, "Failed to get object key for KubeVirt PriorityClass")
return err
}

err = mgr.GetAPIReader().Get(ctx, key, pc)

if err != nil && apierrors.IsNotFound(err) {
log.Info("Creating KubeVirt PriorityClass")
return mgr.GetClient().Create(ctx, pc, &client.CreateOptions{})
}

return err
}

// serveCRMetrics gets the Operator/CustomResource GVKs and generates metrics based on those types.
// It serves those metrics on "http://metricsHost:operatorMetricsPort".
func serveCRMetrics(cfg *rest.Config) error {
Expand Down
21 changes: 2 additions & 19 deletions pkg/controller/hyperconverged/hyperconverged_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -873,26 +873,9 @@ func newKubeVirtForCR(cr *hcov1alpha1.HyperConverged, namespace string) *kubevir
}
}

func newKubeVirtPriorityClass() *schedulingv1.PriorityClass {
return &schedulingv1.PriorityClass{
TypeMeta: metav1.TypeMeta{
APIVersion: "scheduling.k8s.io/v1",
Kind: "PriorityClass",
},
ObjectMeta: metav1.ObjectMeta{
Name: "kubevirt-cluster-critical",
},
// 1 billion is the highest value we can set
// https://kubernetes.io/docs/concepts/configuration/pod-priority-preemption/#priorityclass
Value: 1000000000,
GlobalDefault: false,
Description: "This priority class should be used for KubeVirt core components only.",
}
}

func (r *ReconcileHyperConverged) ensureKubeVirtPriorityClass(req *hcoRequest) (upgradeDone bool, err error) {
req.logger.Info("Reconciling KubeVirt PriorityClass")
pc := newKubeVirtPriorityClass()
pc := hcoutil.NewKubeVirtPriorityClass()

key, err := client.ObjectKeyFromObject(pc)
if err != nil {
Expand Down Expand Up @@ -1819,7 +1802,7 @@ func componentResourceRemoval(o interface{}, c client.Client, req *hcoRequest) e
}

func ensureKubeVirtPriorityClassDeleted(c client.Client, req *hcoRequest) error {
pc := newKubeVirtPriorityClass()
pc := hcoutil.NewKubeVirtPriorityClass()
key, err := client.ObjectKeyFromObject(pc)
if err != nil {
req.logger.Error(err, "Failed to get object key for KubeVirt PriorityClass")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ var _ = Describe("HyperconvergedController", func() {
})

It("should create if not present", func() {
expectedResource := newKubeVirtPriorityClass()
expectedResource := util.NewKubeVirtPriorityClass()
cl := initClient([]runtime.Object{})
r := initReconciler(cl)
upgradeDone, err := r.ensureKubeVirtPriorityClass(req)
Expand All @@ -113,7 +113,7 @@ var _ = Describe("HyperconvergedController", func() {
})

It("should do nothing if already exists", func() {
expectedResource := newKubeVirtPriorityClass()
expectedResource := util.NewKubeVirtPriorityClass()
cl := initClient([]runtime.Object{expectedResource})
r := initReconciler(cl)
upgradeDone, err := r.ensureKubeVirtPriorityClass(req)
Expand All @@ -132,7 +132,7 @@ var _ = Describe("HyperconvergedController", func() {
Expect(upgradeDone).To(BeFalse())
Expect(err).To(BeNil())

expectedResource := newKubeVirtPriorityClass()
expectedResource := util.NewKubeVirtPriorityClass()
key, err := client.ObjectKeyFromObject(expectedResource)
Expect(err).ToNot(HaveOccurred())
foundResource := &schedulingv1.PriorityClass{}
Expand Down Expand Up @@ -2380,7 +2380,7 @@ func getBasicDeployment() *basicExpected {
}
res.hco = hco

res.pc = newKubeVirtPriorityClass()
res.pc = util.NewKubeVirtPriorityClass()
// These are all of the objects that we expect to "find" in the client because
// we already created them in a previous reconcile.
expectedKVConfig := newKubeVirtConfigForCR(hco, namespace)
Expand Down
21 changes: 20 additions & 1 deletion pkg/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,16 @@ import (
"fmt"

"errors"
"os"

"github.com/go-logr/logr"
"github.com/operator-framework/operator-sdk/pkg/k8sutil"
"os"
"sigs.k8s.io/controller-runtime/pkg/client"

csvv1alpha1 "github.com/operator-framework/operator-lifecycle-manager/pkg/api/apis/operators/v1alpha1"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
schedulingv1 "k8s.io/api/scheduling/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

Expand Down Expand Up @@ -96,3 +98,20 @@ func GetCSVfromPod(pod *corev1.Pod, c client.Client, logger logr.Logger) (*csvv1

return csv, nil
}

func NewKubeVirtPriorityClass() *schedulingv1.PriorityClass {
return &schedulingv1.PriorityClass{
TypeMeta: metav1.TypeMeta{
APIVersion: "scheduling.k8s.io/v1",
Kind: "PriorityClass",
},
ObjectMeta: metav1.ObjectMeta{
Name: "kubevirt-cluster-critical",
},
// 1 billion is the highest value we can set
// https://kubernetes.io/docs/concepts/configuration/pod-priority-preemption/#priorityclass
Value: 1000000000,
GlobalDefault: false,
Description: "This priority class should be used for KubeVirt core components only.",
}
}

0 comments on commit fed3bad

Please sign in to comment.