-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
baremetal: monitor bootstrap process
- Loading branch information
Showing
4 changed files
with
184 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# See the OWNERS docs: https://git.k8s.io/community/contributors/guide/owners.md | ||
# This file just uses aliases defined in OWNERS_ALIASES. | ||
|
||
approvers: | ||
- baremetal-approvers | ||
reviewers: | ||
- baremetal-reviewers |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
package baremetal | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
baremetalhost "github.com/metal3-io/baremetal-operator/apis/metal3.io/v1alpha1" | ||
"github.com/sirupsen/logrus" | ||
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" | ||
"k8s.io/apimachinery/pkg/runtime" | ||
"k8s.io/apimachinery/pkg/watch" | ||
"k8s.io/client-go/dynamic" | ||
"k8s.io/client-go/rest" | ||
clientwatch "k8s.io/client-go/tools/watch" | ||
) | ||
|
||
// WaitForBaremetalBootstrapControlPlane will watch baremetalhost resources on the bootstrap | ||
// and wait for the control plane to finish provisioning. | ||
func WaitForBaremetalBootstrapControlPlane(ctx context.Context, config *rest.Config) error { | ||
client, err := dynamic.NewForConfig(config) | ||
if err != nil { | ||
return fmt.Errorf("creating a baremetal client: %w", err) | ||
} | ||
|
||
r := client.Resource(baremetalhost.GroupVersion.WithResource("baremetalhosts")).Namespace("openshift-machine-api") | ||
blw := BmhCacheListerWatcher{ | ||
Resource: r, | ||
RetryWatch: true, | ||
} | ||
|
||
logrus.Infof(" Waiting for baremetal control plane to provision...") | ||
|
||
masters := map[string]baremetalhost.BareMetalHost{} | ||
|
||
_, err = clientwatch.UntilWithSync( | ||
ctx, | ||
blw, | ||
&unstructured.Unstructured{}, | ||
nil, | ||
func(event watch.Event) (bool, error) { | ||
switch event.Type { | ||
case watch.Added, watch.Modified: | ||
default: | ||
return false, nil | ||
} | ||
|
||
bmh := &baremetalhost.BareMetalHost{} | ||
|
||
unstr, err := runtime.DefaultUnstructuredConverter.ToUnstructured(event.Object) | ||
if err != nil { | ||
return false, err | ||
} | ||
|
||
if err := runtime.DefaultUnstructuredConverter.FromUnstructured(unstr, bmh); err != nil { | ||
logrus.Error("failed to convert to bmh", err) | ||
return false, err | ||
} | ||
|
||
role, found := bmh.Labels["installer.openshift.io/role"] | ||
|
||
if found && role == "control-plane" { | ||
prev, found := masters[bmh.Name] | ||
|
||
if !found || bmh.Status.Provisioning.State != prev.Status.Provisioning.State { | ||
if bmh.Status.Provisioning.State == baremetalhost.StateNone { | ||
// StateNone is an empty string | ||
logrus.Infof(" baremetalhost: %s: uninitialized", bmh.Name) | ||
} else { | ||
logrus.Infof(" baremetalhost: %s: %s", bmh.Name, bmh.Status.Provisioning.State) | ||
} | ||
|
||
if bmh.Status.OperationalStatus == baremetalhost.OperationalStatusError { | ||
logrus.Warnf(" baremetalhost: %s: %s: %s", bmh.Name, bmh.Status.ErrorType, bmh.Status.ErrorMessage) | ||
} | ||
} | ||
|
||
masters[bmh.Name] = *bmh | ||
} | ||
|
||
if len(masters) == 0 { | ||
return false, nil | ||
} | ||
|
||
for _, master := range masters { | ||
if master.Status.Provisioning.State != baremetalhost.StateProvisioned { | ||
return false, nil | ||
} | ||
} | ||
|
||
return true, nil | ||
}, | ||
) | ||
|
||
return err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package baremetal | ||
|
||
import ( | ||
"context" | ||
"time" | ||
|
||
"github.com/sirupsen/logrus" | ||
apierrors "k8s.io/apimachinery/pkg/api/errors" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" | ||
"k8s.io/apimachinery/pkg/runtime" | ||
"k8s.io/apimachinery/pkg/watch" | ||
"k8s.io/client-go/dynamic" | ||
) | ||
|
||
// BmhCacheListerWatcher is an object that wraps the listing and wrapping | ||
// functionality for baremetal host resources. | ||
type BmhCacheListerWatcher struct { | ||
Resource dynamic.ResourceInterface | ||
RetryWatch bool | ||
} | ||
|
||
// List returns a list of baremetal hosts as dynamic objects. | ||
func (bc BmhCacheListerWatcher) List(options metav1.ListOptions) (runtime.Object, error) { | ||
list, err := bc.Resource.List(context.TODO(), options) | ||
if apierrors.IsNotFound(err) { | ||
logrus.Debug(" baremetalhost resource not yet available, will retry") | ||
return &unstructured.UnstructuredList{}, nil | ||
} | ||
|
||
return list, err | ||
} | ||
|
||
// Watch starts a watch over baremetal hosts. | ||
func (bc BmhCacheListerWatcher) Watch(options metav1.ListOptions) (watch.Interface, error) { | ||
w, err := bc.Resource.Watch(context.TODO(), options) | ||
if apierrors.IsNotFound(err) && bc.RetryWatch { | ||
logrus.Debug(" baremetalhost resource not yet available, will retry") | ||
// When the Resource isn't installed yet, we can encourage the caller to keep | ||
// retrying by supplying an empty watcher. In the case of | ||
// UntilWithSync, the caller also checks how long it takes to create the | ||
// watch. To avoid errors, we introduce an artificial delay of one | ||
// second. | ||
w := watch.NewEmptyWatch() | ||
time.Sleep(time.Second) | ||
return w, nil | ||
} | ||
return w, err | ||
} |