Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simple scheduler #4

Merged
merged 10 commits into from
Jul 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ issues:
- path: "api/*"
linters:
- lll
- goimports
- path: "internal/*"
linters:
- dupl
Expand Down
20 changes: 10 additions & 10 deletions api/v1alpha1/groupversion_info.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
/*
Copyright 2024.
Copyright 2024, NVIDIA CORPORATION & AFFILIATES

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0
http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

// Package v1alpha1 contains API Schema definitions for the maintenance v1alpha1 API group
Expand Down
22 changes: 12 additions & 10 deletions api/v1alpha1/nodemaintenance_types.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
/*
Copyright 2024.
Copyright 2024, NVIDIA CORPORATION & AFFILIATES

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0
http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package v1alpha1
Expand All @@ -26,6 +26,8 @@ const (
)

const (
// ConditionReasonUninitialized means that Condition is unset for NodeMaintenance
ConditionReasonUninitialized string = ""
// ConditionReasonPending means that NodeMaintenance is in Pending state
ConditionReasonPending string = "Pending"
// ConditionReasonScheduled means that NodeMaintenance is in Scheduled state
Expand Down
24 changes: 24 additions & 0 deletions api/v1alpha1/utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
Copyright 2024, NVIDIA CORPORATION & AFFILIATES

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package v1alpha1

import "fmt"

// CanonicalString is a canonical string representation of NodeMaintenance
func (nm *NodeMaintenance) CanonicalString() string {
return fmt.Sprintf("%s/%s:%s@%s", nm.Namespace, nm.Name, nm.Spec.RequestorID, nm.Spec.NodeName)
}
14 changes: 14 additions & 0 deletions cmd/maintenance-manager/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import (

maintenancev1alpha1 "github.com/Mellanox/maintenance-operator/api/v1alpha1"
"github.com/Mellanox/maintenance-operator/internal/controller"
"github.com/Mellanox/maintenance-operator/internal/scheduler"
"github.com/Mellanox/maintenance-operator/internal/version"
//+kubebuilder:scaffold:imports
)
Expand All @@ -53,6 +54,7 @@ func init() {
//+kubebuilder:scaffold:scheme
}

//nolint:funlen
func main() {
var metricsAddr string
var enableLeaderElection bool
Expand Down Expand Up @@ -148,6 +150,18 @@ func main() {
setupLog.Error(err, "unable to create controller", "controller", "MaintenanceOperatorConfig")
os.Exit(1)
}
nmSchedulerReconcilerLog := ctrl.Log.WithName("NodeMaintenanceScheduler")
if err = (&controller.NodeMaintenanceSchedulerReconciler{
Client: mgr.GetClient(),
Scheme: mgr.GetScheme(),
MaxUnavailable: nil,
MaxParallelOperations: nil,
Log: nmSchedulerReconcilerLog,
Sched: scheduler.NewDefaultScheduler(nmSchedulerReconcilerLog.WithName("DefaultScheduler")),
}).SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "NodeMaintenanceScheduler")
os.Exit(1)
}
//+kubebuilder:scaffold:builder

if err := mgr.AddHealthzCheck("healthz", healthz.Ping); err != nil {
Expand Down
2 changes: 1 addition & 1 deletion config/default/kustomization.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ patches:
# Protect the /metrics endpoint by putting it behind auth.
# If you want your controller-manager to expose the /metrics
# endpoint w/o any authn/z, please comment the following line.
- path: manager_auth_proxy_patch.yaml
#- path: manager_auth_proxy_patch.yaml

# [WEBHOOK] To enable webhook, uncomment all the sections with [WEBHOOK] prefix including the one in
# crd/kustomization.yaml
Expand Down
8 changes: 8 additions & 0 deletions config/rbac/role.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,14 @@ kind: ClusterRole
metadata:
name: manager-role
rules:
- apiGroups:
- ""
resources:
- nodes
verbs:
- get
- list
- watch
- apiGroups:
- maintenance.nvidia.com
resources:
Expand Down
6 changes: 3 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@ go 1.22.0
toolchain go1.22.4

require (
github.com/go-logr/logr v1.4.1
github.com/onsi/ginkgo/v2 v2.19.0
github.com/onsi/gomega v1.33.1
github.com/pkg/errors v0.9.1
k8s.io/api v0.30.2
k8s.io/apimachinery v0.30.2
k8s.io/client-go v0.30.2
sigs.k8s.io/controller-runtime v0.18.4
Expand All @@ -19,7 +22,6 @@ require (
github.com/emicklei/go-restful/v3 v3.11.0 // indirect
github.com/evanphx/json-patch/v5 v5.9.0 // indirect
github.com/fsnotify/fsnotify v1.7.0 // indirect
github.com/go-logr/logr v1.4.1 // indirect
github.com/go-logr/zapr v1.3.0 // indirect
github.com/go-openapi/jsonpointer v0.19.6 // indirect
github.com/go-openapi/jsonreference v0.20.2 // indirect
Expand All @@ -41,7 +43,6 @@ require (
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/prometheus/client_golang v1.16.0 // indirect
github.com/prometheus/client_model v0.4.0 // indirect
github.com/prometheus/common v0.44.0 // indirect
Expand All @@ -63,7 +64,6 @@ require (
gopkg.in/inf.v0 v0.9.1 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
k8s.io/api v0.30.2 // indirect
k8s.io/apiextensions-apiserver v0.30.1 // indirect
k8s.io/klog/v2 v2.120.1 // indirect
k8s.io/kube-openapi v0.0.0-20240228011516-70dd3763d340 // indirect
Expand Down
4 changes: 0 additions & 4 deletions internal/controller/maintenanceoperatorconfig_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,6 @@ type MaintenanceOperatorConfigReconciler struct {

// Reconcile is part of the main kubernetes reconciliation loop which aims to
// move the current state of the cluster closer to the desired state.
// TODO(user): Modify the Reconcile function to compare the state specified by
// the MaintenanceOperatorConfig object against the actual cluster state, and then
// perform operations to make the cluster state reflect the state specified by
// the user.
//
// For more details, check Reconcile and its Result here:
// - https://pkg.go.dev/sigs.k8s.io/[email protected]/pkg/reconcile
Expand Down
63 changes: 13 additions & 50 deletions internal/controller/maintenanceoperatorconfig_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,64 +21,27 @@ import (

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/controller-runtime/pkg/reconcile"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

maintenancev1alpha1 "github.com/Mellanox/maintenance-operator/api/v1alpha1"
maintenancev1 "github.com/Mellanox/maintenance-operator/api/v1alpha1"
)

var _ = Describe("MaintenanceOperatorConfig Controller", func() {
Context("When reconciling a resource", func() {
const resourceName = "test-resource"
// test context, TODO(adrianc): use ginkgo spec context
var testCtx context.Context

ctx := context.Background()
BeforeEach(func() {
testCtx = context.Background()
})

typeNamespacedName := types.NamespacedName{
Name: resourceName,
Namespace: "default", // TODO(user):Modify as needed
It("Should Reconcile MaintenanceOperatorConfig resource", func() {
oc := &maintenancev1.MaintenanceOperatorConfig{
ObjectMeta: metav1.ObjectMeta{Name: "default", Namespace: "default"},
Spec: maintenancev1.MaintenanceOperatorConfigSpec{},
}
maintenanceoperatorconfig := &maintenancev1alpha1.MaintenanceOperatorConfig{}

BeforeEach(func() {
By("creating the custom resource for the Kind MaintenanceOperatorConfig")
err := k8sClient.Get(ctx, typeNamespacedName, maintenanceoperatorconfig)
if err != nil && errors.IsNotFound(err) {
resource := &maintenancev1alpha1.MaintenanceOperatorConfig{
ObjectMeta: metav1.ObjectMeta{
Name: resourceName,
Namespace: "default",
},
// TODO(user): Specify other spec details if needed.
}
Expect(k8sClient.Create(ctx, resource)).To(Succeed())
}
})

AfterEach(func() {
// TODO(user): Cleanup logic after each test, like removing the resource instance.
resource := &maintenancev1alpha1.MaintenanceOperatorConfig{}
err := k8sClient.Get(ctx, typeNamespacedName, resource)
Expect(err).NotTo(HaveOccurred())

By("Cleanup the specific resource instance MaintenanceOperatorConfig")
Expect(k8sClient.Delete(ctx, resource)).To(Succeed())
})
It("should successfully reconcile the resource", func() {
By("Reconciling the created resource")
controllerReconciler := &MaintenanceOperatorConfigReconciler{
Client: k8sClient,
Scheme: k8sClient.Scheme(),
}

_, err := controllerReconciler.Reconcile(ctx, reconcile.Request{
NamespacedName: typeNamespacedName,
})
Expect(err).NotTo(HaveOccurred())
// TODO(user): Add more specific assertions depending on your controller's reconciliation logic.
// Example: If you expect a certain status condition after reconciliation, verify it here.
Expect(k8sClient.Create(testCtx, oc)).ToNot(HaveOccurred())
DeferCleanup(func() {
Expect(k8sClient.Delete(testCtx, oc)).ToNot(HaveOccurred())
})
})
})
52 changes: 47 additions & 5 deletions internal/controller/nodemaintenance_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,17 @@ package controller
import (
"context"

"github.com/go-logr/logr"
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/reconcile"

maintenancev1 "github.com/Mellanox/maintenance-operator/api/v1alpha1"
"github.com/Mellanox/maintenance-operator/internal/k8sutils"
)

// NodeMaintenanceReconciler reconciles a NodeMaintenance object
Expand All @@ -39,18 +44,55 @@ type NodeMaintenanceReconciler struct {

// Reconcile is part of the main kubernetes reconciliation loop which aims to
// move the current state of the cluster closer to the desired state.
// TODO(user): Modify the Reconcile function to compare the state specified by
// the NodeMaintenance object against the actual cluster state, and then
// perform operations to make the cluster state reflect the state specified by
// the user.
//
// For more details, check Reconcile and its Result here:
// - https://pkg.go.dev/sigs.k8s.io/[email protected]/pkg/reconcile
func (r *NodeMaintenanceReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
reqLog := log.FromContext(ctx)
reqLog.Info("got request", "name", req.NamespacedName)

return ctrl.Result{}, nil
// get NodeMaintenance object
nm := &maintenancev1.NodeMaintenance{}
if err := r.Get(ctx, types.NamespacedName{Namespace: req.Namespace, Name: req.Name}, nm); err != nil {
if errors.IsNotFound(err) {
reqLog.Info("NodeMaintenance object not found, nothing to do.")
return reconcile.Result{}, nil
}
return reconcile.Result{}, err
}

// Handle its state according to Ready Condition
state := k8sutils.GetReadyConditionReason(nm)

var err error
//nolint:gocritic
switch state {
case maintenancev1.ConditionReasonUninitialized:
err = r.handleUninitiaized(ctx, reqLog, nm)
if err != nil {
reqLog.Error(err, "failed to handle uninitialized NodeMaintenance object")
}
}

return ctrl.Result{}, err
}

// handleUninitiaized handles NodeMaintenance in ConditionReasonUninitialized state
// it eventually sets NodeMaintenance Ready condition Reason to ConditionReasonPending
func (r *NodeMaintenanceReconciler) handleUninitiaized(ctx context.Context, reqLog logr.Logger, nm *maintenancev1.NodeMaintenance) error {
ykulazhenkov marked this conversation as resolved.
Show resolved Hide resolved
reqLog.Info("Handle Uninitialized NodeMaintenance")

// set Ready condition to ConditionReasonPending and update object
changed := k8sutils.SetReadyConditionReason(nm, maintenancev1.ConditionReasonPending)
var err error
if changed {
err = r.Status().Update(ctx, nm)
if err != nil {
reqLog.Error(err, "failed to update status for NodeMaintenance object")
}
}

return err
}

// SetupWithManager sets up the controller with the Manager.
Expand Down
Loading
Loading