Skip to content

Commit

Permalink
hook improves
Browse files Browse the repository at this point in the history
Signed-off-by: Viktor Kramarenko <[email protected]>
  • Loading branch information
ViktorKram committed Sep 5, 2024
1 parent d6f7eb1 commit 5e45bb9
Show file tree
Hide file tree
Showing 8 changed files with 41 additions and 35 deletions.
4 changes: 3 additions & 1 deletion hooks/convert_bd_names_to_selector.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ def main(ctx: hook.Context):
api_v1.patch_namespaced_daemon_set(name=ds_name, namespace=ds_ns, body=daemonset)
print(f"{migrate_script} daemon set has been successfully scaled down")



# get current lvgs with old CRD (which is LvmVolumeGroup)
try:
# this list might be LvmVolumeGroups as LVMVolumeGroups
Expand Down Expand Up @@ -314,7 +316,7 @@ def configure_new_lvg(backup):
def turn_on_daemonset(api_v1, name, namespace, daemonset):
_ = daemonset.spec.template.spec.node_selector.pop('exclude')
try:
api_v1.patch_namespaced_daemon_set(name=name, namespace=namespace, body=daemonset)
api_v1.replace_namespaced_daemon_set(name=name, namespace=namespace, body=daemonset)
print(f"{migrate_script} successfully migrated LvmVolumeGroup kind to LVMVolumeGroup")
except Exception as e:
print(f"{migrate_script} an ERROR occurred while turning on the daemonset, err: {e}")
Expand Down
4 changes: 3 additions & 1 deletion images/sds-health-watcher-controller/src/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ go 1.22.3

require (
github.com/cloudflare/cfssl v1.5.0
github.com/deckhouse/sds-node-configurator/api v0.0.0-20240805103635-969dc811217b
github.com/deckhouse/sds-node-configurator/api v0.0.0-20240905123334-64f17b70f035
github.com/go-logr/logr v1.4.2
github.com/prometheus/client_golang v1.19.1
github.com/stretchr/testify v1.9.0
Expand All @@ -18,6 +18,8 @@ require (
sigs.k8s.io/controller-runtime v0.19.0
)

replace github.com/deckhouse/sds-node-configurator/api => ../../../api

require (
github.com/beorn7/perks v1.0.1 // indirect
github.com/cespare/xxhash/v2 v2.3.0 // indirect
Expand Down
2 changes: 2 additions & 0 deletions images/sds-health-watcher-controller/src/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/deckhouse/sds-node-configurator/api v0.0.0-20240805103635-969dc811217b h1:EYmHWTWcWMpyxJGZK05ZxlIFnh9s66DRrxLw/LNb/xw=
github.com/deckhouse/sds-node-configurator/api v0.0.0-20240805103635-969dc811217b/go.mod h1:H71+9G0Jr46Qs0BA3z3/xt0h9lbnJnCEYcaCJCWFBf0=
github.com/deckhouse/sds-node-configurator/api v0.0.0-20240905123334-64f17b70f035 h1:2kluZX0T5gk8YgNRk2bzd+m/mSkNmcKKaDHd6sVHP8I=
github.com/deckhouse/sds-node-configurator/api v0.0.0-20240905123334-64f17b70f035/go.mod h1:H71+9G0Jr46Qs0BA3z3/xt0h9lbnJnCEYcaCJCWFBf0=
github.com/emicklei/go-restful/v3 v3.11.0 h1:rAQeMHw1c7zTmncogyy8VvRZwtkmkZ4FxERmMY4rD+g=
github.com/emicklei/go-restful/v3 v3.11.0/go.mod h1:6n3XBCmQQb25CM2LCACGz8ukIrRry+4bhvbpWn3mrbc=
github.com/evanphx/json-patch v0.5.2 h1:xVCHIVMUu1wtM/VkR9jVZ45N3FhZfYMMYGorLCR8P3k=
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func RunLVGConditionsWatcher(
Reconciler: reconcile.Func(func(ctx context.Context, request reconcile.Request) (reconcile.Result, error) {
log.Info(fmt.Sprintf("[RunLVGConditionsWatcher] Reconciler got a request %s", request.String()))

lvg := &v1alpha1.LvmVolumeGroup{}
lvg := &v1alpha1.LVMVolumeGroup{}
err := cl.Get(ctx, request.NamespacedName, lvg)
if err != nil {
log.Error(err, fmt.Sprintf("[RunLVGConditionsWatcher] unable to get the LVMVolumeGroup %s", request.Name))
Expand Down Expand Up @@ -95,16 +95,16 @@ func RunLVGConditionsWatcher(
return err
}

err = c.Watch(source.Kind(mgr.GetCache(), &v1alpha1.LvmVolumeGroup{}, handler.TypedFuncs[*v1alpha1.LvmVolumeGroup, reconcile.Request]{
CreateFunc: func(_ context.Context, e event.TypedCreateEvent[*v1alpha1.LvmVolumeGroup], q workqueue.TypedRateLimitingInterface[reconcile.Request]) {
err = c.Watch(source.Kind(mgr.GetCache(), &v1alpha1.LVMVolumeGroup{}, handler.TypedFuncs[*v1alpha1.LVMVolumeGroup, reconcile.Request]{
CreateFunc: func(_ context.Context, e event.TypedCreateEvent[*v1alpha1.LVMVolumeGroup], q workqueue.TypedRateLimitingInterface[reconcile.Request]) {
log.Info(fmt.Sprintf("[RunLVGConditionsWatcher] got a create event for the LVMVolumeGroup %s", e.Object.GetName()))

request := reconcile.Request{NamespacedName: types.NamespacedName{Namespace: e.Object.GetNamespace(), Name: e.Object.GetName()}}
q.Add(request)

log.Info(fmt.Sprintf("[RunLVGConditionsWatcher] createFunc added a request for the LVMVolumeGroup %s to the Reconcilers queue", e.Object.GetName()))
},
UpdateFunc: func(_ context.Context, e event.TypedUpdateEvent[*v1alpha1.LvmVolumeGroup], q workqueue.TypedRateLimitingInterface[reconcile.Request]) {
UpdateFunc: func(_ context.Context, e event.TypedUpdateEvent[*v1alpha1.LVMVolumeGroup], q workqueue.TypedRateLimitingInterface[reconcile.Request]) {
log.Info(fmt.Sprintf("[RunLVGConditionsWatcher] got a update event for the LVMVolumeGroup %s", e.ObjectNew.GetName()))
if reflect.DeepEqual(e.ObjectOld.Status.Conditions, e.ObjectNew.Status.Conditions) {
log.Info(fmt.Sprintf("[RunLVGConditionsWatcher] no condition changes for the LVMVolumeGroup %s. No need to reconcile", e.ObjectNew.Name))
Expand All @@ -123,7 +123,7 @@ func RunLVGConditionsWatcher(
return nil
}

func reconcileLVGConditions(ctx context.Context, cl client.Client, log logger.Logger, lvg *v1alpha1.LvmVolumeGroup) (bool, error) {
func reconcileLVGConditions(ctx context.Context, cl client.Client, log logger.Logger, lvg *v1alpha1.LVMVolumeGroup) (bool, error) {
log.Debug(fmt.Sprintf("[reconcileLVGConditions] starts the reconciliation for the LVMVolumeGroup %s", lvg.Name))

if lvg.Status.Conditions == nil {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func getCRD(ctx context.Context, cl client.Client, crdName string) (*v1.CustomRe
return crd, err
}

func updateLVMVolumeGroupPhaseIfNeeded(ctx context.Context, cl client.Client, lvg *v1alpha1.LvmVolumeGroup, phase string) error {
func updateLVMVolumeGroupPhaseIfNeeded(ctx context.Context, cl client.Client, lvg *v1alpha1.LVMVolumeGroup, phase string) error {
if lvg.Status.Phase == phase {
return nil
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func RunLVGStatusWatcher(
Reconciler: reconcile.Func(func(ctx context.Context, request reconcile.Request) (reconcile.Result, error) {
log.Info(fmt.Sprintf("[RunLVGStatusWatcher] Reconciler got a request %s", request.String()))

lvg := &v1alpha1.LvmVolumeGroup{}
lvg := &v1alpha1.LVMVolumeGroup{}
err := cl.Get(ctx, request.NamespacedName, lvg)
if err != nil {
if errors2.IsNotFound(err) {
Expand Down Expand Up @@ -67,14 +67,14 @@ func RunLVGStatusWatcher(
return err
}

err = c.Watch(source.Kind(mgr.GetCache(), &v1alpha1.LvmVolumeGroup{}, handler.TypedFuncs[*v1alpha1.LvmVolumeGroup, reconcile.Request]{
CreateFunc: func(_ context.Context, e event.TypedCreateEvent[*v1alpha1.LvmVolumeGroup], q workqueue.TypedRateLimitingInterface[reconcile.Request]) {
err = c.Watch(source.Kind(mgr.GetCache(), &v1alpha1.LVMVolumeGroup{}, handler.TypedFuncs[*v1alpha1.LVMVolumeGroup, reconcile.Request]{
CreateFunc: func(_ context.Context, e event.TypedCreateEvent[*v1alpha1.LVMVolumeGroup], q workqueue.TypedRateLimitingInterface[reconcile.Request]) {
log.Info(fmt.Sprintf("[RunLVGStatusWatcher] got a create event for the LVMVolumeGroup %s", e.Object.GetName()))
request := reconcile.Request{NamespacedName: types.NamespacedName{Namespace: e.Object.GetNamespace(), Name: e.Object.GetName()}}
q.Add(request)
log.Info(fmt.Sprintf("[RunLVGStatusWatcher] CreateFunc added a request for the LVMVolumeGroup %s to the Reconcilers queue", e.Object.GetName()))
},
UpdateFunc: func(_ context.Context, e event.TypedUpdateEvent[*v1alpha1.LvmVolumeGroup], q workqueue.TypedRateLimitingInterface[reconcile.Request]) {
UpdateFunc: func(_ context.Context, e event.TypedUpdateEvent[*v1alpha1.LVMVolumeGroup], q workqueue.TypedRateLimitingInterface[reconcile.Request]) {
log.Info(fmt.Sprintf("[RunLVGStatusWatcher] got an update event for the LVMVolumeGroup %s", e.ObjectNew.GetName()))
request := reconcile.Request{NamespacedName: types.NamespacedName{Namespace: e.ObjectNew.GetNamespace(), Name: e.ObjectNew.GetName()}}
q.Add(request)
Expand All @@ -89,7 +89,7 @@ func RunLVGStatusWatcher(
return nil
}

func reconcileLVGStatus(ctx context.Context, cl client.Client, log logger.Logger, lvg *v1alpha1.LvmVolumeGroup) error {
func reconcileLVGStatus(ctx context.Context, cl client.Client, log logger.Logger, lvg *v1alpha1.LVMVolumeGroup) error {
log.Debug(fmt.Sprintf("[reconcileLVGStatus] starts to reconcile the LVMVolumeGroup %s", lvg.Name))
shouldUpdate := false

Expand Down Expand Up @@ -118,7 +118,7 @@ func reconcileLVGStatus(ctx context.Context, cl client.Client, log logger.Logger
return err
}

func getActualThinPoolReadyCount(statusTp []v1alpha1.LvmVolumeGroupThinPoolStatus) int {
func getActualThinPoolReadyCount(statusTp []v1alpha1.LVMVolumeGroupThinPoolStatus) int {
count := 0

for _, tp := range statusTp {
Expand All @@ -130,7 +130,7 @@ func getActualThinPoolReadyCount(statusTp []v1alpha1.LvmVolumeGroupThinPoolStatu
return count
}

func getUniqueThinPoolCount(specTp []v1alpha1.LvmVolumeGroupThinPoolSpec, statusTp []v1alpha1.LvmVolumeGroupThinPoolStatus) int {
func getUniqueThinPoolCount(specTp []v1alpha1.LVMVolumeGroupThinPoolSpec, statusTp []v1alpha1.LVMVolumeGroupThinPoolStatus) int {
unique := make(map[string]struct{}, len(specTp)+len(statusTp))

for _, tp := range specTp {
Expand All @@ -144,7 +144,7 @@ func getUniqueThinPoolCount(specTp []v1alpha1.LvmVolumeGroupThinPoolSpec, status
return len(unique)
}

func getVGConfigurationAppliedStatus(lvg *v1alpha1.LvmVolumeGroup) v1.ConditionStatus {
func getVGConfigurationAppliedStatus(lvg *v1alpha1.LVMVolumeGroup) v1.ConditionStatus {
for _, c := range lvg.Status.Conditions {
if c.Type == internal.TypeVGConfigurationApplied {
return c.Status
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,8 @@ func getPodsBySelector(ctx context.Context, cl client.Client, selector map[strin
return pods, nil
}

func findLVMVolumeGroupsByNodeNames(lvgs map[string]v1alpha1.LvmVolumeGroup, nodeNames []string) map[string]v1alpha1.LvmVolumeGroup {
result := make(map[string]v1alpha1.LvmVolumeGroup, len(lvgs))
func findLVMVolumeGroupsByNodeNames(lvgs map[string]v1alpha1.LVMVolumeGroup, nodeNames []string) map[string]v1alpha1.LVMVolumeGroup {
result := make(map[string]v1alpha1.LVMVolumeGroup, len(lvgs))

names := make(map[string]struct{}, len(nodeNames))
for _, n := range nodeNames {
Expand Down Expand Up @@ -141,7 +141,7 @@ func getNodesByNames(ctx context.Context, cl client.Client, lvgNodeNames []strin
return usedNodes, missedNodes, nil
}

func getNodeNamesFromLVGs(lvgs map[string]v1alpha1.LvmVolumeGroup) []string {
func getNodeNamesFromLVGs(lvgs map[string]v1alpha1.LVMVolumeGroup) []string {
nodes := make([]string, 0, len(lvgs))

for _, lvg := range lvgs {
Expand All @@ -153,27 +153,27 @@ func getNodeNamesFromLVGs(lvgs map[string]v1alpha1.LvmVolumeGroup) []string {
return nodes
}

func GetLVMVolumeGroups(ctx context.Context, cl client.Client, metrics monitoring.Metrics) (map[string]v1alpha1.LvmVolumeGroup, error) {
lvgList := &v1alpha1.LvmVolumeGroupList{}
func GetLVMVolumeGroups(ctx context.Context, cl client.Client, metrics monitoring.Metrics) (map[string]v1alpha1.LVMVolumeGroup, error) {
lvgList := &v1alpha1.LVMVolumeGroupList{}

start := time.Now()
err := cl.List(ctx, lvgList)
metrics.APIMethodsDuration(SdsInfraWatcherCtrlName, "list").Observe(metrics.GetEstimatedTimeInSeconds(start))
metrics.APIMethodsExecutionCount(SdsInfraWatcherCtrlName, "list").Inc()
if err != nil {
metrics.APIMethodsErrors(SdsInfraWatcherCtrlName, "list").Inc()
return nil, fmt.Errorf("[GetApiLVMVolumeGroups] unable to list LvmVolumeGroups, err: %w", err)
return nil, fmt.Errorf("[GetApiLVMVolumeGroups] unable to list LVMVolumeGroups, err: %w", err)
}

lvgs := make(map[string]v1alpha1.LvmVolumeGroup, len(lvgList.Items))
lvgs := make(map[string]v1alpha1.LVMVolumeGroup, len(lvgList.Items))
for _, lvg := range lvgList.Items {
lvgs[lvg.Name] = lvg
}

return lvgs, nil
}

func updateLVGConditionIfNeeded(ctx context.Context, cl client.Client, log logger.Logger, lvg *v1alpha1.LvmVolumeGroup, status metav1.ConditionStatus, conType, reason, message string) error {
func updateLVGConditionIfNeeded(ctx context.Context, cl client.Client, log logger.Logger, lvg *v1alpha1.LVMVolumeGroup, status metav1.ConditionStatus, conType, reason, message string) error {
exist := false
index := 0
newCondition := metav1.Condition{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ func TestHealthWatcher(t *testing.T) {
})

t.Run("GetLVMVolumeGroups_returns_lvgs", func(t *testing.T) {
lvgsToCreate := []v1alpha1.LvmVolumeGroup{
lvgsToCreate := []v1alpha1.LVMVolumeGroup{
{
ObjectMeta: metav1.ObjectMeta{
Name: "test-lvg-1",
Expand Down Expand Up @@ -157,19 +157,19 @@ func TestHealthWatcher(t *testing.T) {
node1 = "node1"
node2 = "node2"
)
lvgs := map[string]v1alpha1.LvmVolumeGroup{
lvgs := map[string]v1alpha1.LVMVolumeGroup{
"first": {
Status: v1alpha1.LvmVolumeGroupStatus{
Nodes: []v1alpha1.LvmVolumeGroupNode{
Status: v1alpha1.LVMVolumeGroupStatus{
Nodes: []v1alpha1.LVMVolumeGroupNode{
{
Name: node1,
},
},
},
},
"second": {
Status: v1alpha1.LvmVolumeGroupStatus{
Nodes: []v1alpha1.LvmVolumeGroupNode{
Status: v1alpha1.LVMVolumeGroupStatus{
Nodes: []v1alpha1.LVMVolumeGroupNode{
{
Name: node2,
},
Expand All @@ -190,13 +190,13 @@ func TestHealthWatcher(t *testing.T) {
node1 = "node1"
node2 = "node2"
)
lvgs := map[string]v1alpha1.LvmVolumeGroup{
lvgs := map[string]v1alpha1.LVMVolumeGroup{
"first": {
ObjectMeta: metav1.ObjectMeta{
Name: "first",
},
Status: v1alpha1.LvmVolumeGroupStatus{
Nodes: []v1alpha1.LvmVolumeGroupNode{
Status: v1alpha1.LVMVolumeGroupStatus{
Nodes: []v1alpha1.LVMVolumeGroupNode{
{
Name: node1,
},
Expand All @@ -207,8 +207,8 @@ func TestHealthWatcher(t *testing.T) {
ObjectMeta: metav1.ObjectMeta{
Name: "second",
},
Status: v1alpha1.LvmVolumeGroupStatus{
Nodes: []v1alpha1.LvmVolumeGroupNode{
Status: v1alpha1.LVMVolumeGroupStatus{
Nodes: []v1alpha1.LVMVolumeGroupNode{
{
Name: node2,
},
Expand Down

0 comments on commit 5e45bb9

Please sign in to comment.