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

share logic for getting the in-use MachineConfigs #1783

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ require (
github.com/opencontainers/selinux v1.11.0
github.com/openshift/api v0.0.0-20240527133614-ba11c1587003
github.com/openshift/build-machinery-go v0.0.0-20240419090851-af9c868bcf52
github.com/openshift/client-go v0.0.0-20240528061634-b054aa794d87
github.com/openshift/client-go v0.0.0-20240821135114-75c118605d5f
github.com/pkg/errors v0.9.1
github.com/pkg/profile v1.3.0
github.com/prometheus/client_golang v1.16.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,8 @@ github.com/openshift/build-machinery-go v0.0.0-20240419090851-af9c868bcf52 h1:bq
github.com/openshift/build-machinery-go v0.0.0-20240419090851-af9c868bcf52/go.mod h1:b1BuldmJlbA/xYtdZvKi+7j5YGB44qJUJDZ9zwiNCfE=
github.com/openshift/client-go v0.0.0-20240528061634-b054aa794d87 h1:JtLhaGpSEconE+1IKmIgCOof/Len5ceG6H1pk43yv5U=
github.com/openshift/client-go v0.0.0-20240528061634-b054aa794d87/go.mod h1:3IPD4U0qyovZS4EFady2kqY32m8lGcbs/Wx+yprg9z8=
github.com/openshift/client-go v0.0.0-20240821135114-75c118605d5f h1:iYIcy36QilP/1KeiVYeqXP9oLsP2gCytGIG5+doS7x8=
github.com/openshift/client-go v0.0.0-20240821135114-75c118605d5f/go.mod h1:3IPD4U0qyovZS4EFady2kqY32m8lGcbs/Wx+yprg9z8=
github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
Expand Down
3 changes: 3 additions & 0 deletions pkg/machineconfig/OWNERS
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
reviewers:

approvers:
58 changes: 58 additions & 0 deletions pkg/machineconfig/in_use_machineconfigs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package machineconfig

import (
"context"
"fmt"

mcfgclientset "github.com/openshift/client-go/machineconfiguration/clientset/versioned"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/sets"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
)

// GetInUseMachineConfigs filters in-use MachineConfig resources and returns set of their names.
func GetInUseMachineConfigs(ctx context.Context, clientConfig *rest.Config, poolFilter string) (sets.Set[string], error) {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Optionally I can pass the client instances (instead of the config). I think it would allow for some testing.

// Create a set to store in-use configs
inuseConfigs := sets.New[string]()

machineConfigClient, err := mcfgclientset.NewForConfig(clientConfig)
if err != nil {
return nil, err
}

poolList, err := machineConfigClient.MachineconfigurationV1().MachineConfigPools().List(ctx, metav1.ListOptions{})
if err != nil {
return nil, fmt.Errorf("getting MachineConfigPools failed: %w", err)
}

for _, pool := range poolList.Items {
// Check if the pool matches the specified pool name (if provided)
if poolFilter == "" || poolFilter == pool.Name {
// Get the rendered config name from the status section
inuseConfigs.Insert(pool.Status.Configuration.Name)
inuseConfigs.Insert(pool.Spec.Configuration.Name)
}
}

kubeClient, err := kubernetes.NewForConfig(clientConfig)
if err != nil {
return nil, err
}
nodeList, err := kubeClient.CoreV1().Nodes().List(ctx, metav1.ListOptions{})
if err != nil {
return nil, err
}
for _, node := range nodeList.Items {
current, ok := node.Annotations["machineconfiguration.openshift.io/currentConfig"]
if ok {
inuseConfigs.Insert(current)
}
desired, ok := node.Annotations["machineconfiguration.openshift.io/desiredConfig"]
if ok {
inuseConfigs.Insert(desired)
}
}

return inuseConfigs, nil
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading