Skip to content

Commit

Permalink
siteconfig: add WaitForExtraLabel method to CIBuilder
Browse files Browse the repository at this point in the history
When provisioning clusters using oran, the ClusterInstance is managed by the oran-o2ims operator. It is useful to wait for changes to the ExtraLabels field to be propogated to the ClusterInstance by the operator, something the WaitForExtraLabel method allows.
  • Loading branch information
klaskosk committed Feb 3, 2025
1 parent f088d00 commit ed3e6ac
Show file tree
Hide file tree
Showing 2 changed files with 118 additions and 0 deletions.
52 changes: 52 additions & 0 deletions pkg/siteconfig/clusterinstance.go
Original file line number Diff line number Diff line change
Expand Up @@ -491,6 +491,58 @@ func (builder *CIBuilder) WaitForCondition(expected metav1.Condition, timeout ti
return builder, err
}

// WaitForExtraLabel waits up to timeout until the ExtraLabel label exists for manifest of kind.
func (builder *CIBuilder) WaitForExtraLabel(kind, label string, timeout time.Duration) (*CIBuilder, error) {
if valid, err := builder.validate(); !valid {
return nil, err
}

glog.V(100).Infof("Waiting up to %s until ClusterInstance %s in namespace %s has extra label %s on kind %s",
timeout, builder.Definition.Name, builder.Definition.Namespace, label, kind)

if !builder.Exists() {
glog.V(100).Infof("ClusterInstance %s does not exist in namespace %s",
builder.Definition.Name, builder.Definition.Namespace)

return nil, fmt.Errorf(
"clusterinstance object %s does not exist in namespace %s", builder.Definition.Name, builder.Definition.Namespace)
}

err := wait.PollUntilContextTimeout(
context.TODO(), 3*time.Second, timeout, true, func(ctx context.Context) (bool, error) {
var err error
builder.Object, err = builder.Get()

if err != nil {
glog.V(100).Info("Failed to get ClusterInstance %s in namespace %s: %v",
builder.Definition.Name, builder.Definition.Namespace, err)

return false, nil
}

builder.Definition = builder.Object

if builder.Definition.Spec.ExtraLabels == nil {
return false, nil
}

kindLabels, exists := builder.Definition.Spec.ExtraLabels[kind]
if !exists {
return false, nil
}

_, exists = kindLabels[label]

return exists, nil
})

if err != nil {
return nil, err
}

return builder, nil
}

// Get fetches the defined ClusterInstance from the cluster.
func (builder *CIBuilder) Get() (*siteconfigv1alpha1.ClusterInstance, error) {
if valid, err := builder.validate(); !valid {
Expand Down
66 changes: 66 additions & 0 deletions pkg/siteconfig/clusterinstance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -592,6 +592,72 @@ func TestClusterInstanceWaitForCondition(t *testing.T) {
}
}

func TestClusterInstanceWaitForExtraLabel(t *testing.T) {
testCases := []struct {
exists bool
valid bool
hasLabel bool
expectedError error
}{
{
exists: true,
valid: true,
hasLabel: true,
expectedError: nil,
},
{
exists: false,
valid: true,
hasLabel: true,
expectedError: fmt.Errorf("clusterinstance object %s does not exist in namespace %s",
testClusterInstance, testClusterInstance),
},
{
exists: true,
valid: false,
hasLabel: true,
expectedError: fmt.Errorf("clusterinstance 'nsname' cannot be empty"),
},
{
exists: true,
valid: true,
hasLabel: false,
expectedError: context.DeadlineExceeded,
},
}

for _, testCase := range testCases {
var (
runtimeObjects []runtime.Object
clusterInstanceBuilder *CIBuilder
)

if testCase.exists {
clusterinstance := generateClusterInstance()

if testCase.hasLabel {
clusterinstance.Spec.ExtraLabels = map[string]map[string]string{"test": {"test": ""}}
}

runtimeObjects = append(runtimeObjects, clusterinstance)
}

testSettings := clients.GetTestClients(clients.TestClientParams{
K8sMockObjects: runtimeObjects,
SchemeAttachers: testSchemes,
})

if testCase.valid {
clusterInstanceBuilder = buildValidClusterInstanceTestBuilder(testSettings)
} else {
clusterInstanceBuilder = buildInvalidClusterInstanceTestBuilder(testSettings)
}

_, err := clusterInstanceBuilder.WaitForExtraLabel("test", "test", time.Second)
assert.Equal(t, testCase.expectedError, err)
}
}

func TestClusterInstanceGet(t *testing.T) {
testCases := []struct {
exists bool
Expand Down

0 comments on commit ed3e6ac

Please sign in to comment.