Skip to content

Commit

Permalink
cnf ran: reorganize the gitopsztp helpers
Browse files Browse the repository at this point in the history
* Reorganizes the gitopsztp helpers to make gitdetails and nodedelete their own modules
* Moves some functions into the ran/internal/ranhelper module for better reusability. The ranhelper package itself will be updated in the next PR.
* Removes some code that has been shifted to eco-goinfra
* Removes the argocd-operator dependency and shifts to the eco-goinfra types. This also allows for dumping the application CRs.
  • Loading branch information
klaskosk committed Sep 3, 2024
1 parent 54dacb2 commit 3b3c9b5
Show file tree
Hide file tree
Showing 32 changed files with 227 additions and 6,639 deletions.
1 change: 0 additions & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ linters-settings:
- "github.com/grafana-operator/grafana-operator"
- "github.com/NVIDIA/gpu-operator"
- "github.com/operator-framework/api"
- "github.com/argoproj-labs/argocd-operator/api"
- "github.com/vmware-tanzu/velero/pkg/apis/velero/v1"
- "github.com/vmware-tanzu/velero/pkg/generated/clientset/versioned/scheme"
- "github.com/golang/glog"
Expand Down
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,6 @@ require (
github.com/Masterminds/semver/v3 v3.2.1 // indirect
github.com/Masterminds/sprig/v3 v3.2.3 // indirect
github.com/ajeddeloh/go-json v0.0.0-20200220154158-5ae607161559 // indirect
github.com/argoproj-labs/argocd-operator v0.10.0
github.com/asaskevich/govalidator v0.0.0-20230301143203-a9d515a09cc2 // indirect
github.com/aws/aws-sdk-go v1.50.25 // indirect
github.com/bahlo/generic-list-go v0.2.0 // indirect
Expand Down
4 changes: 0 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -818,8 +818,6 @@ github.com/apache/arrow/go/v10 v10.0.1/go.mod h1:YvhnlEePVnBS4+0z3fhPfUy7W1Ikj0I
github.com/apache/arrow/go/v11 v11.0.0/go.mod h1:Eg5OsL5H+e299f7u5ssuXsuHQVEGC4xei5aX110hRiI=
github.com/apache/arrow/go/v12 v12.0.0/go.mod h1:d+tV/eHZZ7Dz7RPrFKtPK02tpr+c9/PEd/zm8mDS9Vg=
github.com/apache/thrift v0.16.0/go.mod h1:PHK3hniurgQaNMZYaCLEqXKsYK8upmhPbmdP2FXSqgU=
github.com/argoproj-labs/argocd-operator v0.10.0 h1:B2k6Rwiff3O7fG3K7nNiFuo9TV7Kr0s/K9f4Mo+5Qak=
github.com/argoproj-labs/argocd-operator v0.10.0/go.mod h1:4TThdvK88j46P6ybACEhHHqJstdnF+CFHkmlS068dqk=
github.com/armon/circbuf v0.0.0-20150827004946-bbbad097214e/go.mod h1:3U/XgcO3hCbHZ8TKRvWD2dDTCfh9M9ya+I9JpbB7O8o=
github.com/armon/go-metrics v0.0.0-20180917152333-f0300d1749da/go.mod h1:Q73ZrmVTwzkszR9V5SSuryQ31EELlFMUz1kKyl939pY=
github.com/armon/go-radix v0.0.0-20180808171621-7fddfc383310/go.mod h1:ufUuZ+zHj4x4TnLV4JWEpy2hxWSpsRywHrMgIH9cCH8=
Expand Down Expand Up @@ -1570,8 +1568,6 @@ github.com/onsi/gomega v1.33.1 h1:dsYjIxxSR755MDmKVsaFQTE22ChNBcuuTWgkUDSubOk=
github.com/onsi/gomega v1.33.1/go.mod h1:U4R44UsT+9eLIaYRB2a5qajjtQYn0hauxvRm16AVYg0=
github.com/openshift-kni/cluster-group-upgrades-operator v0.0.0-20240423171335-f07cdbf8af2c h1:wAPCXsnAXOUAJ5DYlVgGUcV9YBSiVlH4o9tbQ9py8ZY=
github.com/openshift-kni/cluster-group-upgrades-operator v0.0.0-20240423171335-f07cdbf8af2c/go.mod h1:hkzqKpmQvh7vgPx8Hw6IExJorKPM0dEeJdOXjIW3gNw=
github.com/openshift-kni/eco-goinfra v0.0.0-20240819205434-62b2119d4777 h1:fkQhAU+1ZE5/f2OmPIJKeJ5khlHYJ4B+Bsz611QmkZo=
github.com/openshift-kni/eco-goinfra v0.0.0-20240819205434-62b2119d4777/go.mod h1:f3XX4dM4BbVlqRTl3GrQqC0yuSJL8tA7Ebv6skpc7RE=
github.com/openshift-kni/eco-goinfra v0.0.0-20240827150327-fe103730d840 h1:FJgW8VwqVPluYEt48RCkjUMBQzfyGiuOS2hdq5QLibo=
github.com/openshift-kni/eco-goinfra v0.0.0-20240827150327-fe103730d840/go.mod h1:0C2ea2ASJPLb6gky5BSzUW6HzNe2fnJskb9s9FXx4Ng=
github.com/openshift-kni/k8sreporter v1.0.5 h1:1GYBc/BTZyVoXilHef43v9A8BSzw700zAPZ6zsZvo6Y=
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package helper
package gitdetails

import (
"context"
Expand Down
158 changes: 104 additions & 54 deletions tests/cnf/ran/gitopsztp/internal/helper/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,16 @@ import (
"time"

"github.com/golang/glog"
"github.com/openshift-kni/eco-goinfra/pkg/argocd"
"github.com/openshift-kni/eco-goinfra/pkg/clients"
"github.com/openshift-kni/eco-goinfra/pkg/imageregistry"
"github.com/openshift-kni/eco-goinfra/pkg/ocm"
"github.com/openshift-kni/eco-goinfra/pkg/serviceaccount"
"github.com/openshift-kni/eco-goinfra/pkg/storage"
"github.com/openshift-kni/eco-gotests/tests/cnf/ran/gitopsztp/internal/tsparams"
"github.com/openshift-kni/eco-gotests/tests/cnf/ran/internal/ranhelper"
operatorv1 "github.com/openshift/api/operator/v1"
"k8s.io/apimachinery/pkg/util/wait"
configurationPolicyV1 "open-cluster-management.io/config-policy-controller/api/v1"
)

// WaitForPolicyToExist waits for up to the specified timeout until the policy exists.
Expand All @@ -38,72 +43,117 @@ func WaitForPolicyToExist(
return policy, err
}

// WaitForPolicyMessageToContainSubstring waits for the policy status message to contain the specified substring.
func WaitForPolicyMessageToContainSubstring(client *clients.Settings, name, namespace, subString string) error {
glog.V(tsparams.LogLevel).Infof("Checking policy '%s' in namespace '%s' for message substring", name, namespace)
// WaitForServiceAccountToExist waits for up to the specified timeout until the service account exists.
func WaitForServiceAccountToExist(
client *clients.Settings, name, namespace string, timeout time.Duration) (*serviceaccount.Builder, error) {
var builder *serviceaccount.Builder

return wait.PollUntilContextTimeout(context.TODO(),
tsparams.ArgoCdChangeInterval, tsparams.ArgoCdChangeTimeout, true, func(ctx context.Context) (bool, error) {
policy, err := ocm.PullPolicy(client, name, namespace)
if err != nil {
return false, err
}

details := policy.Definition.Status.Details
if len(details) > 0 && len(details[0].History) > 0 {
message := details[0].History[0].Message

glog.V(tsparams.LogLevel).Infof("Checking if message '%s' contains substring '%s'", message, subString)
err := wait.PollUntilContextTimeout(
context.TODO(), tsparams.ArgoCdChangeInterval, timeout, true, func(ctx context.Context) (bool, error) {
var err error
builder, err = serviceaccount.Pull(client, name, namespace)

return strings.Contains(message, subString), nil
if err == nil {
return true, nil
}

return false, nil
})
}

// WaitUntilSearchCollectorEnabled waits up to timeout until the KAC has the search collector addon enabled.
func WaitUntilSearchCollectorEnabled(kac *ocm.KACBuilder, timeout time.Duration) error {
glog.V(tsparams.LogLevel).Infof(
"Waiting until search collector is enabled for KAC %s in namespace %s", kac.Definition.Name, kac.Definition.Namespace)

return wait.PollUntilContextTimeout(
context.TODO(), tsparams.ArgoCdChangeInterval, timeout, true, func(ctx context.Context) (bool, error) {
if !kac.Exists() {
glog.V(tsparams.LogLevel).Infof(
"KAC %s in namespace %s does not exist", kac.Definition.Name, kac.Definition.Namespace)

return false, fmt.Errorf("kac %s in namespace %s does not exist", kac.Definition.Name, kac.Definition.Namespace)
if strings.Contains(err.Error(), "does not exist") {
return false, nil
}

return kac.Definition.Spec.SearchCollectorConfig.Enabled, nil
return false, err
})

return builder, err
}

// WaitForConditionInArgoCdApp waits up to timeout until the specified Argo CD app has a condition containing the
// expectedMessage.
func WaitForConditionInArgoCdApp(
client *clients.Settings, appName, namespace, expectedMessage string, timeout time.Duration) error {
// GetPolicyEvaluationIntervals is used to get the configured evaluation intervals for the specified policy.
func GetPolicyEvaluationIntervals(policy *ocm.PolicyBuilder) (string, string, error) {
glog.V(tsparams.LogLevel).Infof(
"Checking application '%s' in namespace '%s' for condition with message '%s'", appName, namespace, expectedMessage)
"Checking policy '%s' in namespace '%s' to fetch evaluation intervals",
policy.Definition.Name, policy.Definition.Namespace)

return wait.PollUntilContextTimeout(
context.TODO(), tsparams.ArgoCdChangeInterval, timeout, true, func(ctx context.Context) (bool, error) {
app, err := argocd.PullApplication(client, appName, namespace)
if err != nil {
return false, err
}
policyTemplates := policy.Definition.Spec.PolicyTemplates
if len(policyTemplates) < 1 {
return "", "", fmt.Errorf(
"could not find policy template for policy %s/%s", policy.Definition.Namespace, policy.Definition.Name)
}

for _, condition := range app.Definition.Status.Conditions {
if strings.Contains(condition.Message, expectedMessage) {
glog.V(tsparams.LogLevel).Info("Found matching condition")
configPolicy, err := ranhelper.UnmarshalRaw[configurationPolicyV1.ConfigurationPolicy](
policyTemplates[0].ObjectDefinition.Raw)
if err != nil {
return "", "", err
}

return true, nil
}
complianceInterval := configPolicy.Spec.EvaluationInterval.Compliant
nonComplianceInterval := configPolicy.Spec.EvaluationInterval.NonCompliant

glog.V(tsparams.LogLevel).Infof("Condition message '%s' did not match", condition.Message)
}
return complianceInterval, nonComplianceInterval, nil
}

return false, nil
})
// RestoreImageRegistry restores the image registry with the provided name back to imageRegistryConfig, copying over the
// labels, annotations, and spec from imageRegistryConfig, then waiting until the image registry is available again.
func RestoreImageRegistry(
client *clients.Settings, imageRegistryName string, imageRegistryConfig *imageregistry.Builder) error {
currentImageRegistry, err := imageregistry.Pull(client, imageRegistryName)
if err != nil {
return err
}

if imageRegistryConfig.Definition.GetAnnotations() != nil {
currentImageRegistry.Definition.SetAnnotations(imageRegistryConfig.Definition.GetAnnotations())
}

if imageRegistryConfig.Definition.GetLabels() != nil {
currentImageRegistry.Definition.SetLabels(imageRegistryConfig.Definition.GetLabels())
}

currentImageRegistry.Definition.Spec = imageRegistryConfig.Definition.Spec

currentImageRegistry, err = currentImageRegistry.Update()
if err != nil {
return err
}

_, err = currentImageRegistry.WaitForCondition(operatorv1.OperatorCondition{
Type: "Available",
Reason: "Removed",
Status: operatorv1.ConditionTrue,
}, tsparams.ArgoCdChangeTimeout)

return err
}

// CleanupImageRegistryConfig deletes the specified resources in the necessary order.
func CleanupImageRegistryConfig(client *clients.Settings) error {
glog.V(tsparams.LogLevel).Infof(
"Cleaning up image registry resources with sc=%s, pv=%s, pvc=%s",
tsparams.ImageRegistrySC, tsparams.ImageRegistryPV, tsparams.ImageRegistryPVC)

// The resources must be deleted in the order of PVC, PV, then SC to avoid errors.
pvc, err := storage.PullPersistentVolumeClaim(client, tsparams.ImageRegistryPVC, tsparams.ImageRegistryNamespace)
if err == nil {
err = pvc.DeleteAndWait(tsparams.ArgoCdChangeTimeout)
if err != nil {
return err
}
}

pv, err := storage.PullPersistentVolume(client, tsparams.ImageRegistryPV)
if err == nil {
err = pv.DeleteAndWait(tsparams.ArgoCdChangeTimeout)
if err != nil {
return err
}
}

sc, err := storage.PullClass(client, tsparams.ImageRegistrySC)
if err == nil {
err = sc.DeleteAndWait(tsparams.ArgoCdChangeTimeout)
if err != nil {
return err
}
}

return nil
}
166 changes: 0 additions & 166 deletions tests/cnf/ran/gitopsztp/internal/helper/policiesapp.go

This file was deleted.

Loading

0 comments on commit 3b3c9b5

Please sign in to comment.